use of org.apache.sling.discovery.TopologyEventListener in project sling by apache.
the class NoClusterDiscoveryServiceTest method testListenerAfter.
@Test
public void testListenerAfter() throws Exception {
final DiscoveryService service = this.createService(true);
final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
final TopologyEventListener listener = new TopologyEventListener() {
@Override
public void handleTopologyEvent(final TopologyEvent event) {
events.add(event);
}
};
invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
assertEquals(1, events.size());
assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, events.get(0).getType());
assertNotNull(events.get(0).getNewView());
assertNull(events.get(0).getOldView());
}
use of org.apache.sling.discovery.TopologyEventListener in project sling by apache.
the class ChaosTest method setupChaosThreads.
/**
* Setup chaos thread(s)
*
* Chaos is right now created by sending topology changing/changed events randomly
*/
private void setupChaosThreads(final List<Thread> threads, final AtomicLong finishedThreads) {
final List<TopologyView> views = new ArrayList<>();
// register topology listener
final ServiceRegistration<TopologyEventListener> reg = this.bc.registerService(TopologyEventListener.class, new TopologyEventListener() {
@Override
public void handleTopologyEvent(final TopologyEvent event) {
if (event.getType() == Type.TOPOLOGY_INIT) {
views.add(event.getNewView());
}
}
}, null);
while (views.isEmpty()) {
this.sleep(10);
}
reg.unregister();
final TopologyView view = views.get(0);
try {
final Collection<ServiceReference<TopologyEventListener>> refs = this.bc.getServiceReferences(TopologyEventListener.class, null);
assertNotNull(refs);
assertFalse(refs.isEmpty());
TopologyEventListener found = null;
for (final ServiceReference<TopologyEventListener> ref : refs) {
final TopologyEventListener listener = this.bc.getService(ref);
if (listener != null && listener.getClass().getName().equals("org.apache.sling.event.impl.jobs.config.TopologyHandler")) {
found = listener;
break;
}
bc.ungetService(ref);
}
assertNotNull(found);
final TopologyEventListener tel = found;
threads.add(new Thread() {
private final Random random = new Random();
@Override
public void run() {
final long startTime = System.currentTimeMillis();
// this thread runs 30 seconds longer than the job creation thread
final long endTime = startTime + (DURATION + 30) * 1000;
while (System.currentTimeMillis() < endTime) {
final int sleepTime = random.nextInt(25) + 15;
try {
Thread.sleep(sleepTime * 1000);
} catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
}
tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGING, view, null));
final int changingTime = random.nextInt(20) + 3;
try {
Thread.sleep(changingTime * 1000);
} catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
}
tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGED, view, view));
}
tel.getClass().getName();
finishedThreads.incrementAndGet();
}
});
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
}
use of org.apache.sling.discovery.TopologyEventListener in project sling by apache.
the class OakDiscoveryService method activate.
/**
* Activate this service
*/
@Activate
protected void activate(final BundleContext bundleContext) {
logger.debug("OakDiscoveryService activating...");
if (settingsService == null) {
throw new IllegalStateException("settingsService not found");
}
if (oakViewChecker == null) {
throw new IllegalStateException("heartbeatHandler not found");
}
slingId = settingsService.getSlingId();
ClusterSyncService consistencyService;
if (config.getSyncTokenEnabled()) {
//TODO: ConsistencyHistory is implemented a little bit hacky ..
ClusterSyncHistory consistencyHistory = new ClusterSyncHistory();
oakBacklogClusterSyncService.setConsistencyHistory(consistencyHistory);
syncTokenService.setConsistencyHistory(consistencyHistory);
consistencyService = new ClusterSyncServiceChain(oakBacklogClusterSyncService, syncTokenService);
} else {
consistencyService = oakBacklogClusterSyncService;
}
viewStateManager = ViewStateManagerFactory.newViewStateManager(viewStateManagerLock, consistencyService);
if (config.getMinEventDelay() > 0) {
viewStateManager.installMinEventDelayHandler(this, scheduler, config.getMinEventDelay());
}
final String isolatedClusterId = UUID.randomUUID().toString();
{
// create a pre-voting/isolated topologyView which would be used
// until the first voting has finished.
// this way for the single-instance case the clusterId can
// remain the same between a getTopology() that is invoked before
// the first TOPOLOGY_INIT and afterwards
DefaultClusterView isolatedCluster = new DefaultClusterView(isolatedClusterId);
Map<String, String> emptyProperties = new HashMap<String, String>();
DefaultInstanceDescription isolatedInstance = new DefaultInstanceDescription(isolatedCluster, true, true, slingId, emptyProperties);
Collection<InstanceDescription> col = new ArrayList<InstanceDescription>();
col.add(isolatedInstance);
final DefaultTopologyView topology = new DefaultTopologyView();
topology.addInstances(col);
topology.setNotCurrent();
setOldView(topology);
}
setOldView((DefaultTopologyView) getTopology());
getOldView().setNotCurrent();
// make sure the first heartbeat is issued as soon as possible - which
// is right after this service starts. since the two (discoveryservice
// and heartbeatHandler need to know each other, the discoveryservice
// is passed on to the heartbeatHandler in this initialize call).
oakViewChecker.initialize(this);
viewStateManagerLock.lock();
try {
viewStateManager.handleActivated();
doUpdateProperties();
DefaultTopologyView newView = (DefaultTopologyView) getTopology();
if (newView.isCurrent()) {
viewStateManager.handleNewView(newView);
} else {
// SLING-3750: just issue a log.info about the delaying
logger.info("activate: this instance is in isolated mode and must yet finish voting before it can send out TOPOLOGY_INIT.");
}
activated = true;
setOldView(newView);
// bind them to the viewstatemanager too
for (TopologyEventListener listener : pendingListeners) {
viewStateManager.bind(listener);
}
pendingListeners.clear();
viewStateManager.bind(changePropagationListener);
} finally {
if (viewStateManagerLock != null) {
viewStateManagerLock.unlock();
}
}
URL[] topologyConnectorURLs = config.getTopologyConnectorURLs();
if (topologyConnectorURLs != null) {
for (int i = 0; i < topologyConnectorURLs.length; i++) {
final URL aURL = topologyConnectorURLs[i];
if (aURL != null) {
try {
logger.info("activate: registering outgoing topology connector to " + aURL);
connectorRegistry.registerOutgoingConnector(clusterViewService, aURL);
} catch (final Exception e) {
logger.info("activate: could not register url: " + aURL + " due to: " + e, e);
}
}
}
}
logger.debug("OakDiscoveryService activated.");
}
use of org.apache.sling.discovery.TopologyEventListener in project sling by apache.
the class NoClusterDiscoveryServiceTest method testListenerBefore.
@Test
public void testListenerBefore() throws Exception {
final DiscoveryService service = this.createService(false);
final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
final TopologyEventListener listener = new TopologyEventListener() {
@Override
public void handleTopologyEvent(final TopologyEvent event) {
events.add(event);
}
};
invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
assertEquals(0, events.size());
invoke(service, "activate");
assertEquals(1, events.size());
assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, events.get(0).getType());
assertNotNull(events.get(0).getNewView());
assertNull(events.get(0).getOldView());
}
use of org.apache.sling.discovery.TopologyEventListener in project sling by apache.
the class NoClusterDiscoveryServiceTest method testPropertyChanges.
@Test
public void testPropertyChanges() throws Exception {
final DiscoveryService service = this.createService(true);
final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
final TopologyEventListener listener = new TopologyEventListener() {
@Override
public void handleTopologyEvent(final TopologyEvent event) {
events.add(event);
}
};
invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
events.clear();
final PropertyProvider provider = new PropertyProvider() {
@Override
public String getProperty(final String name) {
if ("a".equals(name)) {
return "1";
}
if ("b".equals(name)) {
return "2";
}
if ("c".equals(name)) {
return "3";
}
return null;
}
};
final Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyProvider.PROPERTY_PROPERTIES, new String[] { "a", "b", "c" });
properties.put(Constants.SERVICE_ID, 1L);
invoke(service, "bindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
assertEquals(1, events.size());
assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
assertNotNull(events.get(0).getNewView());
assertTrue(events.get(0).getNewView().isCurrent());
assertNotNull(events.get(0).getOldView());
assertFalse(events.get(0).getOldView().isCurrent());
// test properties
assertEquals("1", events.get(0).getNewView().getLocalInstance().getProperty("a"));
assertEquals("2", events.get(0).getNewView().getLocalInstance().getProperty("b"));
assertEquals("3", events.get(0).getNewView().getLocalInstance().getProperty("c"));
assertNull(events.get(0).getOldView().getLocalInstance().getProperty("a"));
assertNull(events.get(0).getOldView().getLocalInstance().getProperty("b"));
assertNull(events.get(0).getOldView().getLocalInstance().getProperty("c"));
events.clear();
invoke(service, "unbindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
assertEquals(1, events.size());
assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
assertNotNull(events.get(0).getNewView());
assertTrue(events.get(0).getNewView().isCurrent());
assertNotNull(events.get(0).getOldView());
assertFalse(events.get(0).getOldView().isCurrent());
assertEquals("1", events.get(0).getOldView().getLocalInstance().getProperty("a"));
assertEquals("2", events.get(0).getOldView().getLocalInstance().getProperty("b"));
assertEquals("3", events.get(0).getOldView().getLocalInstance().getProperty("c"));
assertNull(events.get(0).getNewView().getLocalInstance().getProperty("a"));
assertNull(events.get(0).getNewView().getLocalInstance().getProperty("b"));
assertNull(events.get(0).getNewView().getLocalInstance().getProperty("c"));
}
Aggregations