use of org.apache.sling.discovery.InstanceDescription in project sling by apache.
the class DefaultTopologyView method compareTopology.
/**
* Compare this topology with the given one and determine how they compare
* @param other the other topology against which to compare
* @return the type describing how these two compare
* @see Type
*/
public Type compareTopology(final DefaultTopologyView other) {
if (other == null) {
throw new IllegalArgumentException("other must not be null");
}
if ((localClusterSyncTokenId == null && other.localClusterSyncTokenId != null) || (other.localClusterSyncTokenId == null && localClusterSyncTokenId != null) || (localClusterSyncTokenId != null && !localClusterSyncTokenId.equals(other.localClusterSyncTokenId))) {
logger.debug("compareTopology: different localClusterSyncTokenId");
return Type.TOPOLOGY_CHANGED;
}
if (this.instances.size() != other.instances.size()) {
logger.debug("compareTopology: different number of instances");
return Type.TOPOLOGY_CHANGED;
}
boolean propertiesChanged = false;
for (final InstanceDescription instance : this.instances) {
final Iterator<InstanceDescription> it2 = other.instances.iterator();
InstanceDescription matchingInstance = null;
while (it2.hasNext()) {
final InstanceDescription otherInstance = it2.next();
if (instance.getSlingId().equals(otherInstance.getSlingId())) {
matchingInstance = otherInstance;
break;
}
}
if (matchingInstance == null) {
if (logger.isDebugEnabled()) {
logger.debug("compareTopology: no matching instance found for {}", instance);
}
return Type.TOPOLOGY_CHANGED;
}
if (!instance.getClusterView().getId().equals(matchingInstance.getClusterView().getId())) {
logger.debug("compareTopology: cluster view id does not match");
return Type.TOPOLOGY_CHANGED;
}
if (!instance.isLeader() == matchingInstance.isLeader()) {
logger.debug("compareTopology: leaders differ");
return Type.TOPOLOGY_CHANGED;
}
if (!instance.getProperties().equals(matchingInstance.getProperties())) {
propertiesChanged = true;
}
}
if (propertiesChanged) {
return Type.PROPERTIES_CHANGED;
} else {
return null;
}
}
use of org.apache.sling.discovery.InstanceDescription in project sling by apache.
the class DefaultTopologyView method toString.
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
try {
boolean firstCluster = true;
for (ClusterView clusterView : getClusterViews()) {
if (!firstCluster) {
sb.append(", ");
}
firstCluster = false;
sb.append("[clusterId=" + clusterView.getId() + ", instances=");
boolean firstInstance = true;
for (InstanceDescription id : clusterView.getInstances()) {
if (!firstInstance) {
sb.append(", ");
}
firstInstance = false;
sb.append("[id=" + id.getSlingId() + ", isLeader=" + id.isLeader() + ", isLocal=" + id.isLocal() + "]");
}
sb.append("]");
}
} catch (Exception e) {
// paranoia fallback
sb = new StringBuilder(instances.toString());
}
return "DefaultTopologyView[current=" + isCurrent() + ", num=" + instances.size() + ", instances=" + sb + "]";
}
use of org.apache.sling.discovery.InstanceDescription in project sling by apache.
the class DefaultTopologyViewTest method testCompare.
@Test
public void testCompare() throws Exception {
DefaultTopologyView newView = TopologyHelper.createTopologyView(UUID.randomUUID().toString(), UUID.randomUUID().toString());
try {
newView.compareTopology(null);
fail("Should complain about null");
} catch (Exception e) {
// ok
}
DefaultTopologyView oldView = TopologyHelper.cloneTopologyView(newView);
assertNull(newView.compareTopology(oldView));
DefaultInstanceDescription id = TopologyHelper.createInstanceDescription(newView.getClusterViews().iterator().next());
TopologyHelper.addInstanceDescription(newView, id);
assertEquals(Type.TOPOLOGY_CHANGED, newView.compareTopology(oldView));
assertEquals(2, newView.getInstances().size());
// addInstanceDescription now no longer throws an exception if you add
// the same
// instance twice. this provides greater stability
TopologyHelper.addInstanceDescription(newView, id);
assertEquals(2, newView.getInstances().size());
// try{
// TopologyTestHelper.addInstanceDescription(newView, id);
// fail("should not be able to add twice");
// } catch(Exception e) {
// // ok
// }
oldView = TopologyHelper.cloneTopologyView(newView);
assertNull(newView.compareTopology(oldView));
DefaultInstanceDescription instance = (DefaultInstanceDescription) newView.getInstances().iterator().next();
instance.setProperty("a", "b");
assertEquals(Type.PROPERTIES_CHANGED, newView.compareTopology(oldView));
oldView = TopologyHelper.cloneTopologyView(newView);
assertNull(newView.compareTopology(oldView));
instance.setProperty("a", "B");
assertEquals(Type.PROPERTIES_CHANGED, newView.compareTopology(oldView));
oldView = TopologyHelper.cloneTopologyView(newView);
assertNull(newView.compareTopology(oldView));
instance.setProperty("a", "B");
assertNull(newView.compareTopology(oldView));
// now change the properties of the first instance but modify the second instance' cluster
Iterator<InstanceDescription> it = newView.getInstances().iterator();
DefaultInstanceDescription firstInstance = (DefaultInstanceDescription) it.next();
assertNotNull(firstInstance);
DefaultInstanceDescription secondInstance = (DefaultInstanceDescription) it.next();
assertNotNull(secondInstance);
firstInstance.setProperty("c", "d");
DefaultClusterView cluster = new DefaultClusterView(UUID.randomUUID().toString());
PrivateAccessor.setField(secondInstance, "clusterView", null);
cluster.addInstanceDescription(secondInstance);
assertEquals(Type.TOPOLOGY_CHANGED, newView.compareTopology(oldView));
}
use of org.apache.sling.discovery.InstanceDescription in project sling by apache.
the class DiscoveryServiceImpl method activate.
/**
* Activate this service
*/
@Activate
protected void activate(final BundleContext bundleContext) {
logger.debug("DiscoveryServiceImpl activating...");
if (settingsService == null) {
throw new IllegalStateException("settingsService not found");
}
if (heartbeatHandler == null) {
throw new IllegalStateException("heartbeatHandler not found");
}
slingId = settingsService.getSlingId();
final ClusterSyncService clusterSyncService;
if (!config.useSyncTokenService()) {
logger.info("activate: useSyncTokenService is configured to false. Using pass-through cluster-sync-service.");
clusterSyncService = PASS_THROUGH_CLUSTER_SYNC_SERVICE;
} else if (syncTokenService == null) {
logger.warn("activate: useSyncTokenService is configured to true but there's no SyncTokenService! Using pass-through cluster-sync-service instead.");
clusterSyncService = syncTokenService;
} else {
logger.info("activate: useSyncTokenService is configured to true, using the available SyncTokenService: " + syncTokenService);
clusterSyncService = syncTokenService;
}
viewStateManager = ViewStateManagerFactory.newViewStateManager(viewStateManagerLock, clusterSyncService);
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).
heartbeatHandler.initialize(this, isolatedClusterId);
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);
}
}
}
}
registerMBean(bundleContext);
logger.debug("DiscoveryServiceImpl activated.");
}
use of org.apache.sling.discovery.InstanceDescription in project sling by apache.
the class DummyTopologyView method hashCode.
@Override
public int hashCode() {
int c = 0;
for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
InstanceDescription instanceDescription = (InstanceDescription) it.next();
c += instanceDescription.hashCode();
}
return c;
}
Aggregations