use of org.apache.sling.discovery.TopologyView in project sling by apache.
the class HeartbeatTest method doTestSlowAndFastMachine.
public void doTestSlowAndFastMachine(boolean withFailingScheduler) throws Throwable {
logger.info("doTestSlowAndFastMachine: creating slowMachine... (w/o heartbeat runner)");
FullJR2VirtualInstanceBuilder slowBuilder = newBuilder();
slowBuilder.setDebugName("slow").newRepository("/var/discovery/impl/", true).setConnectorPingTimeout(5).setConnectorPingInterval(999).setMinEventDelay(0).withFailingScheduler(withFailingScheduler);
FullJR2VirtualInstance slowMachine = slowBuilder.fullBuild();
instances.add(slowMachine);
SimpleTopologyEventListener slowListener = new SimpleTopologyEventListener("slow");
slowMachine.bindTopologyEventListener(slowListener);
logger.info("doTestSlowAndFastMachine: creating fastMachine... (w/o heartbeat runner)");
FullJR2VirtualInstanceBuilder fastBuilder = newBuilder();
fastBuilder.setDebugName("fast").useRepositoryOf(slowMachine).setConnectorPingTimeout(5).setConnectorPingInterval(999).setMinEventDelay(0).withFailingScheduler(withFailingScheduler);
FullJR2VirtualInstance fastMachine = fastBuilder.fullBuild();
instances.add(fastMachine);
SimpleTopologyEventListener fastListener = new SimpleTopologyEventListener("fast");
fastMachine.bindTopologyEventListener(fastListener);
HeartbeatHandler hhSlow = slowMachine.getHeartbeatHandler();
HeartbeatHandler hhFast = fastMachine.getHeartbeatHandler();
Thread.sleep(1000);
logger.info("doTestSlowAndFastMachine: no event should have been triggered yet");
assertFalse(fastMachine.getDiscoveryService().getTopology().isCurrent());
assertFalse(slowMachine.getDiscoveryService().getTopology().isCurrent());
assertNull(fastListener.getLastEvent());
assertNull(slowListener.getLastEvent());
// make few rounds of heartbeats so that the two instances see each other
logger.info("doTestSlowAndFastMachine: send a couple of heartbeats to connect the two..");
for (int i = 0; i < 5; i++) {
synchronized (lock(hhSlow)) {
hhSlow.issueHeartbeat();
hhSlow.doCheckView();
}
synchronized (lock(hhFast)) {
hhFast.issueHeartbeat();
hhFast.doCheckView();
}
Thread.sleep(100);
}
logger.info("doTestSlowAndFastMachine: now the two instances should be connected.");
slowMachine.dumpRepo();
assertEquals(2, slowMachine.getDiscoveryService().getTopology().getInstances().size());
assertEquals(2, fastMachine.getDiscoveryService().getTopology().getInstances().size());
assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, fastListener.getLastEvent().getType());
assertEquals(1, fastListener.getEventCount());
assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, slowListener.getLastEvent().getType());
assertEquals(1, slowListener.getEventCount());
// now let the slow machine be slow while the fast one updates as expected
logger.info("doTestSlowAndFastMachine: last heartbeat of slowMachine.");
synchronized (lock(hhSlow)) {
hhSlow.issueHeartbeat();
}
logger.info("doTestSlowAndFastMachine: while the fastMachine still sends heartbeats...");
for (int i = 0; i < 6; i++) {
Thread.sleep(1500);
synchronized (lock(hhFast)) {
hhFast.issueHeartbeat();
hhFast.doCheckView();
}
}
logger.info("doTestSlowAndFastMachine: now the fastMachine should have decoupled the slow one");
fastMachine.dumpRepo();
// one more for the start of the vote
hhFast.doCheckView();
fastMachine.dumpRepo();
// and one for the promotion
hhFast.doCheckView();
// after 9 sec hhSlow's heartbeat will have timed out, so hhFast will not see hhSlow anymore
fastMachine.dumpRepo();
assertEquals(TopologyEvent.Type.TOPOLOGY_CHANGED, fastListener.getLastEvent().getType());
assertEquals(3, fastListener.getEventCount());
assertEquals(1, fastMachine.getDiscoveryService().getTopology().getInstances().size());
TopologyView topo = slowMachine.getDiscoveryService().getTopology();
assertFalse(topo.isCurrent());
// after those 6 sec, hhSlow does the check (6sec between heartbeat and check)
logger.info("doTestSlowAndFastMachine: slowMachine is going to do a checkView next - and will detect being decoupled");
hhSlow.doCheckView();
slowMachine.dumpRepo();
logger.info("doTestSlowAndFastMachine: slowMachine is going to also do a heartbeat next");
synchronized (lock(hhSlow)) {
hhSlow.issueHeartbeat();
}
assertEquals(TopologyEvent.Type.TOPOLOGY_CHANGED, fastListener.getLastEvent().getType());
assertEquals(3, fastListener.getEventCount());
assertEquals(TopologyEvent.Type.TOPOLOGY_CHANGING, slowListener.getLastEvent().getType());
assertEquals(2, slowListener.getEventCount());
Thread.sleep(8000);
// even after 8 sec the slow lsitener did not send a TOPOLOGY_CHANGED yet
logger.info("doTestSlowAndFastMachine: after another 8 sec of silence from slowMachine, it should still remain in CHANGING state");
assertEquals(TopologyEvent.Type.TOPOLOGY_CHANGING, slowListener.getLastEvent().getType());
assertFalse(slowMachine.getDiscoveryService().getTopology().isCurrent());
assertEquals(2, slowListener.getEventCount());
assertEquals(TopologyEvent.Type.TOPOLOGY_CHANGED, fastListener.getLastEvent().getType());
assertEquals(1, fastMachine.getDiscoveryService().getTopology().getInstances().size());
assertEquals(3, fastListener.getEventCount());
// make few rounds of heartbeats so that the two instances see each other again
logger.info("doTestSlowAndFastMachine: now let both fast and slow issue heartbeats...");
for (int i = 0; i < 4; i++) {
synchronized (lock(hhFast)) {
hhFast.issueHeartbeat();
hhFast.doCheckView();
}
synchronized (lock(hhSlow)) {
hhSlow.issueHeartbeat();
hhSlow.doCheckView();
}
Thread.sleep(1000);
}
logger.info("doTestSlowAndFastMachine: by now the two should have joined");
// this should have put the two together again
// even after 8 sec the slow lsitener did not send a TOPOLOGY_CHANGED yet
assertEquals(TopologyEvent.Type.TOPOLOGY_CHANGED, fastListener.getLastEvent().getType());
assertTrue(fastMachine.getDiscoveryService().getTopology().isCurrent());
assertEquals(2, fastMachine.getDiscoveryService().getTopology().getInstances().size());
assertEquals(5, fastListener.getEventCount());
assertEquals(TopologyEvent.Type.TOPOLOGY_CHANGED, slowListener.getLastEvent().getType());
assertTrue(slowMachine.getDiscoveryService().getTopology().isCurrent());
assertEquals(2, slowMachine.getDiscoveryService().getTopology().getInstances().size());
assertEquals(3, slowListener.getEventCount());
}
use of org.apache.sling.discovery.TopologyView in project sling by apache.
the class NoClusterDiscoveryService method createNewView.
private void createNewView(final Type eventType, boolean inform) {
final TopologyEventListener[] registeredServices;
final TopologyView newView;
final TopologyView oldView;
synchronized (lock) {
// invalidate old view
if (this.currentTopologyView != null) {
this.currentTopologyView.invalidate();
oldView = currentTopologyView;
} else {
oldView = null;
}
final InstanceDescription myInstanceDescription = new InstanceDescriptionImpl(this.settingsService.getSlingId(), this.cachedProperties);
this.currentTopologyView = new TopologyViewImpl(myInstanceDescription);
registeredServices = this.listeners;
newView = this.currentTopologyView;
if (inform) {
for (final TopologyEventListener da : registeredServices) {
da.handleTopologyEvent(new TopologyEvent(eventType, oldView, newView));
}
}
}
}
use of org.apache.sling.discovery.TopologyView 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.TopologyView in project sling by apache.
the class JobManagerConfigurationTest method createView.
private TopologyView createView() {
final TopologyView view = Mockito.mock(TopologyView.class);
Mockito.when(view.isCurrent()).thenReturn(true);
final InstanceDescription local = Mockito.mock(InstanceDescription.class);
Mockito.when(local.isLeader()).thenReturn(true);
Mockito.when(local.isLocal()).thenReturn(true);
Mockito.when(local.getSlingId()).thenReturn("id");
Mockito.when(view.getLocalInstance()).thenReturn(local);
final ClusterView localView = Mockito.mock(ClusterView.class);
Mockito.when(localView.getId()).thenReturn("1");
Mockito.when(localView.getInstances()).thenReturn(Collections.singletonList(local));
Mockito.when(view.getClusterViews()).thenReturn(Collections.singleton(localView));
Mockito.when(local.getClusterView()).thenReturn(localView);
return view;
}
use of org.apache.sling.discovery.TopologyView in project sling by apache.
the class TopologyWebConsolePlugin method renderProperties.
/**
* Render the properties page of a particular instance
*/
private void renderProperties(final PrintWriter pw, final String contextPath, final String nodeId) {
if (logger.isDebugEnabled()) {
logger.debug("renderProperties: nodeId=" + nodeId);
}
final TopologyView tv = this.currentView;
@SuppressWarnings("unchecked") Set<InstanceDescription> instances = (tv == null ? (Set<InstanceDescription>) Collections.EMPTY_SET : tv.findInstances(new InstanceFilter() {
@Override
public boolean accept(InstanceDescription instance) {
String slingId = instance.getSlingId();
if (logger.isDebugEnabled()) {
logger.debug("renderProperties/picks: slingId={}", slingId);
}
return (slingId.equals(nodeId));
}
}));
if (instances != null && instances.size() == 1) {
InstanceDescription instance = instances.iterator().next();
pw.println("Properties of " + instance.getSlingId() + ":<br/>");
pw.println("<table class=\"adapters nicetable ui-widget tablesorter\">");
pw.println("<thead>");
pw.println("<tr>");
pw.println("<th class=\"header ui-widget-header\">Key</th>");
pw.println("<th class=\"header ui-widget-header\">Value</th>");
pw.println("</tr>");
pw.println("</thead>");
pw.println("<tbody>");
boolean odd = true;
for (Iterator<Entry<String, String>> it = instance.getProperties().entrySet().iterator(); it.hasNext(); ) {
Entry<String, String> entry = it.next();
String oddEven = odd ? "odd" : "even";
odd = !odd;
pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
pw.println("<td>" + entry.getKey() + "</td>");
pw.println("<td>" + entry.getValue() + "</td>");
pw.println("</tr>");
}
pw.println("</tbody>");
pw.println("</table>");
}
}
Aggregations