use of org.apache.sling.discovery.base.connectors.announcement.Announcement in project sling by apache.
the class AbstractClusterTest method pingConnector.
private boolean pingConnector(final VirtualInstance from, final VirtualInstance to) throws UndefinedClusterViewException {
final Announcement fromAnnouncement = createFromAnnouncement(from);
Announcement replyAnnouncement = null;
try {
replyAnnouncement = ping(to, fromAnnouncement);
} catch (AssertionError e) {
logger.warn("pingConnector: ping failed, assertionError: " + e);
return false;
} catch (UndefinedClusterViewException e) {
logger.warn("pingConnector: ping failed, currently the cluster view is undefined: " + e);
return false;
}
registerReplyAnnouncement(from, replyAnnouncement);
return true;
}
use of org.apache.sling.discovery.base.connectors.announcement.Announcement in project sling by apache.
the class AbstractClusterTest method ping.
private Announcement ping(VirtualInstance to, final Announcement incomingTopologyAnnouncement) throws UndefinedClusterViewException {
final String slingId = to.slingId;
final ClusterViewService clusterViewService = to.getClusterViewService();
final AnnouncementRegistry announcementRegistry = to.getAnnouncementRegistry();
incomingTopologyAnnouncement.removeInherited(slingId);
final Announcement replyAnnouncement = new Announcement(slingId);
long backoffInterval = -1;
final ClusterView clusterView = clusterViewService.getLocalClusterView();
if (!incomingTopologyAnnouncement.isCorrectVersion()) {
fail("incorrect version");
// never reached
return null;
} else if (ClusterViewHelper.contains(clusterView, incomingTopologyAnnouncement.getOwnerId())) {
fail("loop=true");
// never reached
return null;
} else if (ClusterViewHelper.containsAny(clusterView, incomingTopologyAnnouncement.listInstances())) {
fail("incoming announcement contains instances that are part of my cluster");
// never reached
return null;
} else {
backoffInterval = announcementRegistry.registerAnnouncement(incomingTopologyAnnouncement);
if (backoffInterval == -1) {
fail("rejecting an announcement from an instance that I already see in my topology: ");
// never reached
return null;
} else {
// normal, successful case: replying with the part of the topology which this instance sees
replyAnnouncement.setLocalCluster(clusterView);
announcementRegistry.addAllExcept(replyAnnouncement, clusterView, new AnnouncementFilter() {
@Override
public boolean accept(final String receivingSlingId, Announcement announcement) {
if (announcement.getPrimaryKey().equals(incomingTopologyAnnouncement.getPrimaryKey())) {
return false;
}
return true;
}
});
return replyAnnouncement;
}
}
}
use of org.apache.sling.discovery.base.connectors.announcement.Announcement in project sling by apache.
the class TopologyTest method testTwoNodes.
@Test
public void testTwoNodes() throws Throwable {
VirtualInstanceBuilder builder1 = newBuilder().newRepository("/var/discovery/impl/", true).setDebugName("instance1").setConnectorPingInterval(20).setConnectorPingTimeout(200);
VirtualInstance instance1 = builder1.build();
instances.add(instance1);
VirtualInstanceBuilder builder2 = newBuilder().useRepositoryOf(builder1).setDebugName("instance2").setConnectorPingInterval(20).setConnectorPingTimeout(200);
VirtualInstance instance2 = builder2.build();
instances.add(instance2);
instance1.getConfig().setViewCheckTimeout(8);
instance1.getConfig().setViewCheckInterval(1);
instance2.getConfig().setViewCheckTimeout(2);
instance2.getConfig().setViewCheckInterval(1);
for (int i = 0; i < 5; i++) {
instance1.heartbeatsAndCheckView();
instance2.heartbeatsAndCheckView();
Thread.sleep(500);
}
Set<InstanceDescription> instances1 = instance1.getDiscoveryService().getTopology().getInstances();
Set<InstanceDescription> instances2 = instance2.getDiscoveryService().getTopology().getInstances();
assertEquals(1, instances1.size());
assertEquals(1, instances2.size());
assertEquals(instance1.getSlingId(), instances1.iterator().next().getSlingId());
assertEquals(instance2.getSlingId(), instances2.iterator().next().getSlingId());
new VirtualConnector(instance1, instance2);
// check instance 1's announcements
Collection<Announcement> instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(1, instance1LocalAnnouncements.size());
Announcement instance1LocalAnnouncement = instance1LocalAnnouncements.iterator().next();
assertEquals(instance2.getSlingId(), instance1LocalAnnouncement.getOwnerId());
assertEquals(true, instance1LocalAnnouncement.isInherited());
// check instance 2's announcements
Collection<Announcement> instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(1, instance2LocalAnnouncements.size());
Announcement instance2LocalAnnouncement = instance2LocalAnnouncements.iterator().next();
assertEquals(instance1.getSlingId(), instance2LocalAnnouncement.getOwnerId());
assertEquals(false, instance2LocalAnnouncement.isInherited());
// check topology
TopologyHelper.assertTopologyConsistsOf(instance1.getDiscoveryService().getTopology(), instance1.getSlingId(), instance2.getSlingId());
TopologyHelper.assertTopologyConsistsOf(instance2.getDiscoveryService().getTopology(), instance1.getSlingId(), instance2.getSlingId());
instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(1, instance1LocalAnnouncements.size());
instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(1, instance2LocalAnnouncements.size());
// sleep of 2.2sec ensures instance2's heartbeat timeout (which is 2sec) hits
Thread.sleep(2200);
instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(1, instance1LocalAnnouncements.size());
instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(0, instance2LocalAnnouncements.size());
logger.info("testTwoNodes: instance1: " + instance1.getSlingId());
instance1.dumpRepo();
logger.info("testTwoNodes: instance2: " + instance2.getSlingId());
instance2.dumpRepo();
TopologyHelper.assertTopologyConsistsOf(instance1.getDiscoveryService().getTopology(), instance1.getSlingId(), instance2.getSlingId());
TopologyHelper.assertTopologyConsistsOf(instance2.getDiscoveryService().getTopology(), instance2.getSlingId());
// another sleep 6s (2.2+6 = 8.2sec) ensures instance1's heartbeat timeout (which is 8sec) hits as well
Thread.sleep(6000);
instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(0, instance1LocalAnnouncements.size());
instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
assertEquals(0, instance2LocalAnnouncements.size());
TopologyHelper.assertTopologyConsistsOf(instance1.getDiscoveryService().getTopology(), instance1.getSlingId());
TopologyHelper.assertTopologyConsistsOf(instance2.getDiscoveryService().getTopology(), instance2.getSlingId());
}
use of org.apache.sling.discovery.base.connectors.announcement.Announcement in project sling by apache.
the class TopologyWebConsolePlugin method printCluster.
/**
* Render a particular cluster
*/
private void printCluster(final PrintWriter pw, final ClusterView renderCluster, final ClusterView localCluster) {
final Collection<Announcement> announcements = announcementRegistry.listAnnouncementsInSameCluster(localCluster);
for (final InstanceDescription instanceDescription : renderCluster.getInstances()) {
final boolean inLocalCluster = renderCluster == localCluster;
Announcement parentAnnouncement = null;
for (Iterator<Announcement> it2 = announcements.iterator(); it2.hasNext(); ) {
Announcement announcement = it2.next();
for (Iterator<InstanceDescription> it3 = announcement.listInstances().iterator(); it3.hasNext(); ) {
InstanceDescription announcedInstance = it3.next();
if (announcedInstance.getSlingId().equals(instanceDescription.getSlingId())) {
parentAnnouncement = announcement;
break;
}
}
}
final boolean isLocal = instanceDescription.isLocal();
final String slingId = instanceDescription.getSlingId();
pw.print("Sling ID : ");
pw.print(slingId);
pw.println();
pw.print("Cluster View ID : ");
pw.print(instanceDescription.getClusterView() == null ? "null" : instanceDescription.getClusterView().getId());
pw.println();
pw.print("Local instance : ");
pw.print(isLocal);
pw.println();
pw.print("Leader instance : ");
pw.print(instanceDescription.isLeader());
pw.println();
pw.print("In local cluster : ");
if (inLocalCluster) {
pw.print("local");
} else {
pw.print("remote");
}
pw.println();
pw.print("Announced by : ");
if (inLocalCluster) {
pw.print("n/a");
} else {
if (parentAnnouncement != null) {
pw.print(parentAnnouncement.getOwnerId());
} else {
pw.print("(changing)");
}
}
pw.println();
pw.println("Properties:");
for (final Map.Entry<String, String> entry : instanceDescription.getProperties().entrySet()) {
pw.print("- ");
pw.print(entry.getKey());
pw.print(" : ");
pw.print(entry.getValue());
pw.println();
}
pw.println();
pw.println();
}
}
use of org.apache.sling.discovery.base.connectors.announcement.Announcement in project sling by apache.
the class TopologyWebConsolePlugin method listIncomingTopologyConnectors.
/**
* Render the incoming topology connectors - including the header-div and table
*/
private void listIncomingTopologyConnectors(final PrintWriter pw) {
boolean odd = false;
pw.println("<div class=\"ui-widget-header ui-corner-top buttonGroup\" style=\"height: 15px;\">");
pw.println("<span style=\"float: left; margin-left: 1em;\">Incoming topology connectors</span>");
pw.println("</div>");
pw.println("<table class=\"adapters nicetable ui-widget tablesorter\">");
pw.println("<thead>");
pw.println("<tr>");
pw.println("<th class=\"header ui-widget-header\">Owner slingId</th>");
pw.println("<th class=\"header ui-widget-header\">Server info</th>");
pw.println("<th class=\"header ui-widget-header\">Last heartbeat</th>");
pw.println("<th class=\"header ui-widget-header\">Timeout</th>");
pw.println("</tr>");
pw.println("</thead>");
pw.println("<tbody>");
Collection<CachedAnnouncement> incomingConnections = announcementRegistry.listLocalIncomingAnnouncements();
for (Iterator<CachedAnnouncement> it = incomingConnections.iterator(); it.hasNext(); ) {
CachedAnnouncement incomingCachedAnnouncement = it.next();
Announcement incomingAnnouncement = incomingCachedAnnouncement.getAnnouncement();
String oddEven = odd ? "odd" : "even";
odd = !odd;
pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
pw.println("<td>" + incomingAnnouncement.getOwnerId() + "</td>");
if (incomingAnnouncement.getServerInfo() != null) {
pw.println("<td>" + incomingAnnouncement.getServerInfo() + "</td>");
} else {
pw.println("<td><i>n/a</i></td>");
}
pw.println("<td>" + beautifiedTimeDiff(incomingCachedAnnouncement.getLastPing()) + "</td>");
pw.println("<td>" + beautifiedDueTime(incomingCachedAnnouncement.getSecondsUntilTimeout()) + "</td>");
pw.println("</tr>");
}
pw.println("</tbody>");
pw.println("</table>");
pw.println("<br/>");
pw.println("<br/>");
}
Aggregations