use of org.apache.sling.discovery.InstanceDescription in project sling by apache.
the class TopologyCapabilitiesTest method setup.
@Before
public void setup() {
// local cluster view
final ClusterView cv = Mockito.mock(ClusterView.class);
Mockito.when(cv.getId()).thenReturn("cluster");
// local description
final InstanceDescription local = Mockito.mock(InstanceDescription.class);
Mockito.when(local.isLeader()).thenReturn(true);
Mockito.when(local.getSlingId()).thenReturn("local");
Mockito.when(local.getProperty(TopologyCapabilities.PROPERTY_TOPICS)).thenReturn("foo,bar/*,a/**,d/1/2,d/1/*,d/**");
Mockito.when(local.getClusterView()).thenReturn(cv);
// topology view
final TopologyView tv = Mockito.mock(TopologyView.class);
Mockito.when(tv.getInstances()).thenReturn(Collections.singleton(local));
Mockito.when(tv.getLocalInstance()).thenReturn(local);
final JobManagerConfiguration config = Mockito.mock(JobManagerConfiguration.class);
caps = new TopologyCapabilities(tv, config);
}
use of org.apache.sling.discovery.InstanceDescription 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.InstanceDescription in project sling by apache.
the class SyncTokenService method seenAllSyncTokens.
private boolean seenAllSyncTokens(BaseTopologyView view) {
logger.trace("seenAllSyncTokens: start");
ResourceResolver resourceResolver = null;
try {
resourceResolver = getResourceResolver();
Resource resource = ResourceHelper.getOrCreateResource(resourceResolver, getSyncTokenPath());
ValueMap syncTokens = resource.adaptTo(ValueMap.class);
String syncToken = view.getLocalClusterSyncTokenId();
boolean success = true;
StringBuffer historyEntry = new StringBuffer();
for (InstanceDescription instance : view.getLocalInstance().getClusterView().getInstances()) {
Object currentValue = syncTokens.get(instance.getSlingId());
if (currentValue == null) {
String msg = "no syncToken yet of " + instance.getSlingId();
logger.info("seenAllSyncTokens: " + msg);
if (historyEntry.length() != 0) {
historyEntry.append(",");
}
historyEntry.append(msg);
success = false;
} else if (!syncToken.equals(currentValue)) {
String msg = "syncToken of " + instance.getSlingId() + " is " + currentValue + " waiting for " + syncToken;
logger.info("seenAllSyncTokens: " + msg);
if (historyEntry.length() != 0) {
historyEntry.append(",");
}
historyEntry.append(msg);
success = false;
}
}
if (!success) {
logger.info("seenAllSyncTokens: not yet seen all expected syncTokens (see above for details)");
clusterSyncHistory.addHistoryEntry(view, historyEntry.toString());
return false;
} else {
clusterSyncHistory.addHistoryEntry(view, "seen all syncTokens");
}
resourceResolver.commit();
logger.info("seenAllSyncTokens: seen all syncTokens!");
return true;
} catch (LoginException e) {
logger.error("seenAllSyncTokens: could not login: " + e, e);
return false;
} catch (PersistenceException e) {
logger.error("seenAllSyncTokens: got PersistenceException: " + e, e);
return false;
} finally {
logger.trace("seenAllSyncTokens: end");
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
use of org.apache.sling.discovery.InstanceDescription 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.InstanceDescription in project sling by apache.
the class BaseTopologyView method toShortString.
public String toShortString() {
StringBuffer sb = new StringBuffer();
for (InstanceDescription instance : getInstances()) {
if (sb.length() != 0) {
sb.append(",");
}
sb.append(instance.getSlingId());
sb.append("[");
sb.append("local=");
sb.append(instance.isLocal());
sb.append(",leader=");
sb.append(instance.isLeader());
sb.append("]");
}
return "DefaultTopologyView[current=" + isCurrent() + ", num=" + getInstances().size() + ", instances=" + sb.toString() + "]";
}
Aggregations