Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,11 @@ private void handleChanged(final DependencyContext dc, final Event e) {
return;
}
Set<Event> dependencyEvents = m_dependencyEvents.get(dc);
// Remove using the old ordering key, then refresh the key (service ranking may have changed) and add it back.
dependencyEvents.remove(e);
if (e instanceof ServiceEventImpl) {
((ServiceEventImpl) e).refreshRanking();
}
dependencyEvents.add(e);

switch (m_state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,31 @@ public class ServiceEventImpl extends Event {

/**
* The actual service.
* <p>
* Volatile: this field is accessed from several threads which are not serialized by the component queue.
* It may be lazily written by {@link #getEvent()} from a tracker thread or from any user thread (for example
* when a temporal dependency proxy is invoked), while {@link #close()} may concurrently read it from the thread
* unregistering the service, in order to unget it.
*/
private volatile Object m_service;

/**
* The service ranking used to order this event. It is a snapshot, so that the ordering key of an
* event stored in a sorted collection does not change behind the collection's back when the
* service ranking is modified. See {@link #refreshRanking()}.
* <p>
* Not volatile: this field is only written by {@link #refreshRanking()} (called from the component serial queue) and
* only read by {@link #compareTo(Event)}, which is only called by the sorted set of dependency events
* when it is modified (add/remove/contains), also from the component serial queue. The initial value written by
* the constructor (possibly from a tracker thread) is safely published to the queue when the event is scheduled.
*/
private int m_ranking;

/**
* The service id used to order this event.
*/
private final long m_serviceId;

public ServiceEventImpl(ComponentContext ctx, ServiceReference<?> reference, Object service) {
super(service);
m_service = service;
Expand All @@ -80,6 +102,26 @@ public ServiceEventImpl(ComponentContext ctx, ServiceReference<?> reference, Obj
m_serviceObjects = (m_bundleContext != null) ? m_bundleContext.getServiceObjects(reference) : null;
m_reference = reference;
m_logger = ctx.getLogger();
m_ranking = readRanking(reference);
m_serviceId = readServiceId(reference);
}

/**
* Updates the ordering key with the current service ranking. Must be called while this event is not
* stored in a sorted collection (typically: remove it, refresh it, add it back).
*/
public void refreshRanking() {
m_ranking = readRanking(m_reference);
}

private static int readRanking(ServiceReference<?> reference) {
Object ranking = reference.getProperty(Constants.SERVICE_RANKING);
return (ranking instanceof Integer) ? (Integer) ranking : 0;
}

private static long readServiceId(ServiceReference<?> reference) {
Object id = reference.getProperty(Constants.SERVICE_ID);
return (id instanceof Long) ? (Long) id : 0L;
}

/**
Expand Down Expand Up @@ -158,7 +200,12 @@ public int hashCode() {

@Override
public int compareTo(Event b) {
return getReference().compareTo(((ServiceEventImpl) b).getReference());
// Same ordering as ServiceReference.compareTo, but based on the snapshot taken at creation/refresh time.
ServiceEventImpl other = (ServiceEventImpl) b;
if (m_ranking != other.m_ranking) {
return m_ranking < other.m_ranking ? -1 : 1;
}
return Long.compare(other.m_serviceId, m_serviceId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package test;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.felix.dm.context.ComponentContext;
import org.apache.felix.dm.impl.ComponentImpl;
import org.apache.felix.dm.impl.ServiceEventImpl;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;

/**
* Checks that a required dependency becomes unavailable once all its services are gone, even if
* one of them has changed its service.ranking while it was tracked.
*/
public class FELIX6884_ServiceRankingChangeTest {

/**
* A service reference whose ranking can change, ordered like a real framework reference:
* higher ranking first, then lower service.id first.
*/
@SuppressWarnings("rawtypes")
static class RankedReference implements ServiceReference {
final long m_id;
volatile int m_ranking;

RankedReference(long id, int ranking) {
m_id = id;
m_ranking = ranking;
}

@Override
public Object getProperty(String key) {
if (Constants.SERVICE_ID.equals(key)) {
return m_id;
}
if (Constants.SERVICE_RANKING.equals(key)) {
return m_ranking;
}
return null;
}

@Override
public String[] getPropertyKeys() {
return new String[] { Constants.SERVICE_ID, Constants.SERVICE_RANKING };
}

@Override
public Bundle getBundle() {
return null;
}

@Override
public Bundle[] getUsingBundles() {
return null;
}

@Override
public boolean isAssignableTo(Bundle bundle, String className) {
return false;
}

@Override
public Object adapt(Class type) {
return null;
}

@Override
public java.util.Dictionary getProperties() {
return null;
}

@Override
public int compareTo(Object other) {
RankedReference o = (RankedReference) other;
if (m_ranking != o.m_ranking) {
return m_ranking < o.m_ranking ? -1 : 1;
}
if (m_id == o.m_id) {
return 0;
}
return m_id < o.m_id ? 1 : -1;
}

@Override
public String toString() {
return "ref[id=" + m_id + ", ranking=" + m_ranking + "]";
}
}

@Test
public void requiredDependencyIsUnavailableAfterRankingChangeAndRemoval() {
BundleContext bc = mock(BundleContext.class);
ComponentContext ctx = mock(ComponentContext.class);
when(ctx.getBundleContext()).thenReturn(bc);
when(bc.getService(any(ServiceReference.class))).thenReturn(new Object());

SimpleServiceDependency d = new SimpleServiceDependency();
d.setRequired(true);
ComponentImpl c = new ComponentImpl();
c.setImplementation(new Object());
c.add(d);
c.start();

RankedReference refA = new RankedReference(1, 0);
RankedReference refB = new RankedReference(2, 0);
ServiceEventImpl a = new ServiceEventImpl(ctx, refA, new Object());
ServiceEventImpl b = new ServiceEventImpl(ctx, refB, new Object());

d.add(a);
d.add(b);
Assert.assertTrue("dependency should be available with two services", d.isAvailable());

// A's ranking changes: the tracker hands over the same event object.
refA.m_ranking = -10;
d.change(a);
Assert.assertTrue("dependency should still be available", d.isAvailable());

// Both services go away.
d.remove(a);
d.remove(b);

Assert.assertFalse("no service left: the dependency must be unavailable", d.isAvailable());
Assert.assertFalse("no service left: the component must not be available", c.isAvailable());
c.stop();
}
}
Loading