use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class OakBacklogClusterSyncService method getBacklogStatus.
private BacklogStatus getBacklogStatus(BaseTopologyView view) {
logger.trace("getBacklogStatus: start");
ResourceResolver resourceResolver = null;
try {
resourceResolver = getResourceResolver();
DiscoveryLiteDescriptor descriptor = DiscoveryLiteDescriptor.getDescriptorFrom(resourceResolver);
// backlog-free means:
// 1) 'deactivating' must be empty
// (otherwise we indeed have a backlog)
// 2) all active ids of the descriptor must have a mapping to slingIds
// (otherwise the init failed or is pending for some instance(s))
// 3) all 'active' instances must be in the view
// (otherwise discovery lite might not yet consider
// an instance dead but discovery-service does)
// instead what is fine from a backlog point of view
// * instances in the view but listed as 'inactive'
// (this might be the case for just-started instances)
// * instances in the view but not contained in the descriptor at all
// (this might be the case for just-started instances)
int[] activeIds = descriptor.getActiveIds();
int[] deactivatingIds = descriptor.getDeactivatingIds();
// 1) 'deactivating' must be empty
if (deactivatingIds.length != 0) {
logger.info("getBacklogStatus: there are deactivating instances: " + Arrays.toString(deactivatingIds));
return BacklogStatus.HAS_BACKLOG;
}
ClusterView cluster = view.getLocalInstance().getClusterView();
Set<String> slingIds = new HashSet<>();
for (InstanceDescription instance : cluster.getInstances()) {
slingIds.add(instance.getSlingId());
}
for (int i = 0; i < activeIds.length; i++) {
int activeId = activeIds[i];
String slingId = idMapService.toSlingId(activeId, resourceResolver);
// 2) all ids of the descriptor must have a mapping to slingIds
if (slingId == null) {
logger.info("getBacklogStatus: no slingId found for active id: " + activeId);
return BacklogStatus.UNDEFINED;
}
// 3) all 'active' instances must be in the view
if (!slingIds.contains(slingId)) {
logger.info("getBacklogStatus: active instance's (" + activeId + ") slingId (" + slingId + ") not found in cluster (" + cluster + ")");
return BacklogStatus.HAS_BACKLOG;
}
}
logger.info("getBacklogStatus: no backlog (anymore)");
return BacklogStatus.NO_BACKLOG;
} catch (Exception e) {
logger.info("getBacklogStatus: failed to determine backlog status: " + e);
return BacklogStatus.UNDEFINED;
} finally {
logger.trace("getBacklogStatus: end");
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class AbstractClusterTest method createFromAnnouncement.
private Announcement createFromAnnouncement(final VirtualInstance from) throws UndefinedClusterViewException {
// TODO: refactor TopologyConnectorClient to avoid duplicating code from there (ping())
Announcement topologyAnnouncement = new Announcement(from.slingId);
topologyAnnouncement.setServerInfo(from.slingId);
final ClusterView clusterView = from.getClusterViewService().getLocalClusterView();
topologyAnnouncement.setLocalCluster(clusterView);
from.getAnnouncementRegistry().addAllExcept(topologyAnnouncement, clusterView, new AnnouncementFilter() {
@Override
public boolean accept(final String receivingSlingId, final Announcement announcement) {
// filter out announcements that are of old cluster instances
// which I dont really have in my cluster view at the moment
final Iterator<InstanceDescription> it = clusterView.getInstances().iterator();
while (it.hasNext()) {
final InstanceDescription instance = it.next();
if (instance.getSlingId().equals(receivingSlingId)) {
// all fine then
return true;
}
}
// then I should also not propagate that announcement anywhere
return false;
}
});
return topologyAnnouncement;
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class AbstractSingleInstanceTest method testGetters.
@Test
public void testGetters() throws UndefinedClusterViewException, InterruptedException {
logger.info("testGetters: start");
assertNotNull(instance);
logger.info("sling id=" + instance.getSlingId());
try {
instance.getClusterViewService().getLocalClusterView();
// SLING-5030
fail("should complain");
} catch (UndefinedClusterViewException e) {
// ok
}
instance.heartbeatsAndCheckView();
// wait 4000ms for the vote to happen
Thread.sleep(4000);
assertNotNull(instance.getClusterViewService().getLocalClusterView());
ClusterView cv = instance.getClusterViewService().getLocalClusterView();
logger.info("cluster view: id=" + cv.getId());
assertNotNull(cv.getId());
assertNotSame(cv.getId(), "");
List<InstanceDescription> instances = cv.getInstances();
assertNotNull(instances);
assertTrue(instances.size() == 1);
InstanceDescription myInstance = instances.get(0);
assertNotNull(myInstance);
assertTrue(myInstance.getClusterView() == cv);
logger.info("instance id: " + myInstance.getSlingId());
assertEquals(instance.getSlingId(), myInstance.getSlingId());
Map<String, String> properties = myInstance.getProperties();
assertNotNull(properties);
assertNull(myInstance.getProperty("foo"));
assertTrue(myInstance.isLeader());
assertTrue(myInstance.isLocal());
logger.info("testGetters: end");
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class InstancesDiffTest method testIsInClusterView.
@Test
public void testIsInClusterView() throws Exception {
ClusterView clusterView = clusterView("viewId");
Collection<InstanceDescription> instances = new InstancesDiff(Arrays.asList(new Instance("one", true, false, Collections.<String, String>emptyMap(), "otherView"), new Instance("two", false, true, Collections.<String, String>emptyMap(), "viewId")), empty()).all(true).isInClusterView(clusterView).get();
TestCase.assertEquals(1, instances.size());
TestCase.assertEquals("two", instances.iterator().next().getSlingId());
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class InstancesDiffTest method clusterView.
private ClusterView clusterView(String clusterViewId) {
ClusterView clusterView = Mockito.mock(ClusterView.class);
Mockito.when(clusterView.getId()).thenReturn(clusterViewId);
return clusterView;
}
Aggregations