use of org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder 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.its.setup.VirtualInstanceBuilder in project sling by apache.
the class AbstractClusterLoadTest method doDoTest.
private void doDoTest(final int size, final int loopCnt) throws Throwable {
if (size < 2) {
fail("can only test 2 or more instances");
}
VirtualInstanceBuilder builder = newBuilder().newRepository("/var/discovery/impl/ClusterLoadTest/doTest-" + size + "-" + loopCnt + "/", true).setDebugName("firstInstance-" + size + "_" + loopCnt).setConnectorPingTimeout(3).setConnectorPingInterval(20).setMinEventDelay(0);
VirtualInstance firstInstance = builder.build();
firstInstance.startViewChecker(1);
instances.add(firstInstance);
for (int i = 1; i < size; i++) {
VirtualInstanceBuilder builder2 = newBuilder().useRepositoryOf(builder).setDebugName("subsequentInstance-" + i + "-" + size + "_" + loopCnt).setConnectorPingTimeout(3).setMinEventDelay(0).setConnectorPingInterval(20);
VirtualInstance subsequentInstance = builder2.build();
instances.add(subsequentInstance);
subsequentInstance.startViewChecker(1);
}
for (int i = 0; i < loopCnt; i++) {
logger.info("=====================");
logger.info(" START of LOOP " + i);
logger.info("=====================");
// count how many instances had heartbeats running in the first place
int aliveCnt = 0;
for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
VirtualInstance instance = it.next();
if (instance.isViewCheckerRunning()) {
aliveCnt++;
}
}
logger.info("=====================");
logger.info(" original aliveCnt " + aliveCnt);
logger.info("=====================");
if (aliveCnt == 0) {
// if no one is sending heartbeats, all instances go back to isolated mode
aliveCnt = 1;
}
final int aliveCntFinal = aliveCnt;
for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
VirtualInstance instance = it.next();
try {
instance.dumpRepo();
} catch (Exception e) {
logger.error("Failed dumping repo for instance " + instance.getSlingId(), e);
}
}
// then verify that each instance sees that many instances
for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
final VirtualInstance instance = it.next();
if (!instance.isViewCheckerRunning()) {
// if the heartbeat is not running, this instance is considered dead
// hence we're not doing any assert here (as the count is only
// valid if heartbeat/checkView is running and that would void the test)
} else {
new RetryLoop(new ConditionImplementation(instance, aliveCntFinal), INSTANCE_VIEW_TIMEOUT_SECONDS, INSTANCE_VIEW_POLL_INTERVAL_MILLIS);
}
}
// start/stop heartbeats accordingly
logger.info("Starting/Stopping heartbeats with count=" + instances.size());
for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
VirtualInstance instance = it.next();
if (random.nextBoolean()) {
logger.info("Starting heartbeats with " + instance.slingId);
instance.startViewChecker(1);
logger.info("Started heartbeats with " + instance.slingId);
} else {
logger.info("Stopping heartbeats with " + instance.slingId);
instance.stopViewChecker();
logger.info("Stopped heartbeats with " + instance.slingId);
}
}
}
}
use of org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder in project sling by apache.
the class OakDiscoveryServiceTest method testOldView.
/**
* Tests whether the not-current view returned by getTopology()
* matches what listeners get in TOPOLOGY_CHANGING - it should
* basically be the same.
*/
@Test
public void testOldView() throws Throwable {
final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
// info should do
discoveryLogger.setLevel(Level.INFO);
OakVirtualInstanceBuilder builder = newBuilder();
builder.setDebugName("firstInstanceA").newRepository("/var/discovery/oak/", true).setConnectorPingTimeout(3).setMinEventDelay(3);
instance1 = builder.build();
instance1.stopViewChecker();
// instance1.stopVoting();
TopologyView t1 = instance1.getDiscoveryService().getTopology();
// current it should not be
assertFalse(t1.isCurrent());
// but it can as well contain the local instance
assertEquals(1, t1.getInstances().size());
AssertingTopologyEventListener l1 = new AssertingTopologyEventListener("instance1.l1");
l1.addExpected(Type.TOPOLOGY_INIT);
instance1.bindTopologyEventListener(l1);
logger.info("testOldView: instance1 created, no events expected yet. slingId=" + instance1.slingId);
instance1.heartbeatsAndCheckView();
Thread.sleep(200);
instance1.heartbeatsAndCheckView();
Thread.sleep(200);
instance1.heartbeatsAndCheckView();
Thread.sleep(200);
logger.info("testOldView: 2nd/3rd heartbeat sent - now expecting a TOPOLOGY_INIT");
instance1.dumpRepo();
// one event
assertEquals(1, l1.getEvents().size());
// the expected one
assertEquals(0, l1.getRemainingExpectedCount());
assertEquals(0, l1.getUnexpectedCount());
t1 = instance1.getDiscoveryService().getTopology();
// current it should now be
assertTrue(t1.isCurrent());
// and it must contain the local instance
assertEquals(1, t1.getInstances().size());
logger.info("testOldView: creating instance2");
builder.getSimulatedLeaseCollection().setFinal(false);
l1.addExpected(Type.TOPOLOGY_CHANGING);
VirtualInstanceBuilder builder2 = newBuilder();
builder2.setDebugName("secondInstanceB").useRepositoryOf(instance1).setConnectorPingTimeout(3).setMinEventDelay(3);
instance2 = builder2.build();
instance2.stopViewChecker();
// instance2.stopVoting();
logger.info("testOldView: instance2 created, now issuing one heartbeat with instance2 first, so that instance1 can take note of it");
instance2.getViewChecker().checkView();
OakDiscoveryService oakDisco1 = (OakDiscoveryService) instance2.getDiscoveryService();
oakDisco1.checkForTopologyChange();
logger.info("testOldView: now instance1 is also doing a heartbeat and should see that a new instance is there");
instance1.getViewChecker().checkView();
OakDiscoveryService oakDisco2 = (OakDiscoveryService) instance1.getDiscoveryService();
oakDisco2.checkForTopologyChange();
logger.info("testOldView: 500ms sleep...");
// allow some time for CHANGING to be sent
Thread.sleep(500);
logger.info("testOldView: 500ms sleep done.");
// INIT and CHANGING
assertEquals(2, l1.getEvents().size());
// no remaining expected
assertEquals(0, l1.getRemainingExpectedCount());
// and no unexpected
assertEquals(0, l1.getUnexpectedCount());
t1 = instance1.getDiscoveryService().getTopology();
// current it should not be
assertFalse(t1.isCurrent());
// but it should still contain the local instance from before
assertEquals(1, t1.getInstances().size());
builder.getSimulatedLeaseCollection().setFinal(true);
l1.addExpected(Type.TOPOLOGY_CHANGED);
logger.info("testOldView: now issuing 3 rounds of heartbeats/checks and expecting a TOPOLOGY_CHANGED then");
instance2.heartbeatsAndCheckView();
// instance2.analyzeVotings();
instance1.heartbeatsAndCheckView();
// instance1.analyzeVotings();
Thread.sleep(1200);
instance2.heartbeatsAndCheckView();
instance1.heartbeatsAndCheckView();
Thread.sleep(1200);
instance2.heartbeatsAndCheckView();
instance1.heartbeatsAndCheckView();
Thread.sleep(1200);
// INIT, CHANGING and CHANGED
assertEquals(3, l1.getEvents().size());
// no remaining expected
assertEquals(0, l1.getRemainingExpectedCount());
// and no unexpected
assertEquals(0, l1.getUnexpectedCount());
t1 = instance1.getDiscoveryService().getTopology();
// and we should be current again
assertTrue(t1.isCurrent());
// and contain both instances now
assertEquals(2, t1.getInstances().size());
// timeout is set to 3sec, so we now do heartbeats for 4sec with only instance1
// to let instance2 crash
builder.getSimulatedLeaseCollection().setFinal(false);
l1.addExpected(Type.TOPOLOGY_CHANGING);
for (int i = 0; i < 8; i++) {
instance1.heartbeatsAndCheckView();
Thread.sleep(500);
}
// INIT, CHANGING, CHANGED and CHANGING
assertEquals(4, l1.getEvents().size());
// no remaining expected
assertEquals(0, l1.getRemainingExpectedCount());
// and no unexpected
assertEquals(0, l1.getUnexpectedCount());
t1 = instance1.getDiscoveryService().getTopology();
// and we should be current again
assertFalse(t1.isCurrent());
// and contain both instances now
assertEquals(2, t1.getInstances().size());
}
use of org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder in project sling by apache.
the class RepositoryDelaysTest method testSlowSessionSaves.
/**
* SLING-5195 : simulate slow session.saves that block
* the calling thread for non-trivial amounts of time,
* typically for longer than the configured heartbeat
* timeout
*/
//TODO: test takes couple minutes!
@Category(Slow.class)
@Test
public void testSlowSessionSaves() throws Exception {
VirtualInstanceBuilder builder1 = newBuilder();
instance1 = (FullJR2VirtualInstance) builder1.setDebugName("firstInstance").newRepository("/var/discovery/impl/", true).setMinEventDelay(0).setConnectorPingInterval(1).setConnectorPingTimeout(3).build();
VirtualInstanceBuilder builder2 = newBuilder();
instance2 = (FullJR2VirtualInstance) builder2.setDebugName("secondInstance").useRepositoryOf(instance1).setMinEventDelay(0).setConnectorPingInterval(1).setConnectorPingTimeout(3).build();
instance1.setDelay("pre.commit", 12000);
instance1.startViewChecker(1);
instance2.startViewChecker(1);
Thread.sleep(5000);
// after 3 sec - without the 12sec pre-commit delay
// the view would normally be established - but this time
// round instance1 should still be pre-init and instance2
// should be init but alone
TopologyView t1 = instance1.getDiscoveryService().getTopology();
assertFalse(t1.isCurrent());
TopologyView t2 = instance2.getDiscoveryService().getTopology();
assertTrue(t2.isCurrent());
assertEquals(1, t2.getInstances().size());
instance1.setDelay("pre.commit", -1);
Thread.sleep(3000);
TopologyView t1b = instance1.getDiscoveryService().getTopology();
assertTrue(t1b.isCurrent());
assertEquals(2, t1b.getInstances().size());
TopologyView t2b = instance2.getDiscoveryService().getTopology();
assertTrue(t2b.isCurrent());
assertEquals(2, t2b.getInstances().size());
instance1.setDelay("pre.commit", 59876);
instance2.setDelay("pre.commit", 60000);
logger.info("<main> both instances marked as delaying 1min - but with new background checks we should go changing within 3sec");
Thread.sleep(8000);
TopologyView t1c = instance1.getDiscoveryService().getTopology();
assertFalse(t1c.isCurrent());
TopologyView t2c = instance2.getDiscoveryService().getTopology();
assertFalse(t2c.isCurrent());
}
use of org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder in project sling by apache.
the class ConnectorRegistryImplTest method setup.
@Before
public void setup() throws Exception {
VirtualInstanceBuilder builder = newBuilder().newRepository("/var/discovery/impl/", true).setDebugName("i").setConnectorPingInterval(20).setConnectorPingTimeout(20);
i = builder.build();
}
Aggregations