use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.
the class TestDempsy method testExpandingAndContractingKeySpace.
@Test
public void testExpandingAndContractingKeySpace() throws Throwable {
Checker checker = new Checker() {
@Override
public void check(ApplicationContext context) throws Throwable {
ClusterInfoSession session = null;
try {
// start things and verify that the init method was called
Dempsy dempsy = (Dempsy) context.getBean("dempsy");
TestMp mp = (TestMp) getMp(dempsy, "test-app", "test-cluster1");
final ClusterId clusterId = new ClusterId("test-app", "test-cluster1");
// verify we haven't called it again, not that there's really
// a way to given the code
assertEquals(1, mp.startCalls.get());
// make sure that there are no Mps
MetricGetters statsCollector = (MetricGetters) dempsy.getCluster(new ClusterId("test-app", "test-cluster1")).getNodes().get(0).getStatsCollector();
// This will wait until the keySpace is up to the maxcount which is set (in the setup, below) to 100000
assertTrue(poll(baseTimeoutMillis, statsCollector, new Condition<MetricGetters>() {
@Override
public boolean conditionMet(MetricGetters sc) {
return 100000 == sc.getMessageProcessorCount();
}
}));
// now push the cluster into backup node.
ClusterInfoSession originalSession = dempsy.getCluster(new ClusterId("test-app", "test-cluster1")).getNodes().get(0).retouRteg().getClusterSession();
ClusterInfoSessionFactory factory = dempsy.getClusterSessionFactory();
session = TestUtils.stealShard(originalSession, factory, clusterId.asPath() + "/" + String.valueOf(0), baseTimeoutMillis);
// If we got here then the MpContainer is on standby and the number of Mps should
// drop to zero.
assertTrue(poll(baseTimeoutMillis, statsCollector, new Condition<MetricGetters>() {
@Override
public boolean conditionMet(MetricGetters sc) {
return 0 == sc.getMessageProcessorCount();
}
}));
Thread.sleep(10);
assertEquals(0, statsCollector.getMessageProcessorCount());
// this should give control back over to the original session.
session.stop();
session = null;
// If we got here then the MpContainer is no longer on standby and the number of Mps should
// go back to the original amount.
assertTrue(poll(baseTimeoutMillis, statsCollector, new Condition<MetricGetters>() {
@Override
public boolean conditionMet(MetricGetters sc) {
return 100000 == sc.getMessageProcessorCount();
}
}));
Thread.sleep(10);
assertEquals(100000, statsCollector.getMessageProcessorCount());
} finally {
if (session != null)
session.stop();
}
}
public String toString() {
return "testExpandingAndContractingKeySpace";
}
public void setup() {
KeySourceImpl.maxcount = 100000;
System.setProperty("min_nodes_for_cluster", "1");
System.setProperty("total_slots_for_cluster", "1");
}
};
runAllCombinations("SinglestageWithKeyStoreAndExecutorApplicationActx.xml", checker);
}
use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.
the class TestDempsy method runMpKeyStoreTest.
public void runMpKeyStoreTest(final String methodName, final boolean disruptSession) throws Throwable {
Checker checker = new Checker() {
@Override
public void check(ApplicationContext context) throws Throwable {
// start things and verify that the init method was called
Dempsy dempsy = (Dempsy) context.getBean("dempsy");
TestMp mp = (TestMp) getMp(dempsy, "test-app", "test-cluster1");
// verify we haven't called it again, not that there's really
// a way to given the code
assertEquals(1, mp.startCalls.get());
// wait for clone calls to reach at least 2
assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {
@Override
public boolean conditionMet(TestMp mp) {
return mp.cloneCalls.get() == 2;
}
}));
final TestAdaptor adaptor = (TestAdaptor) context.getBean("adaptor");
// if the session has been disrupted then this may take some time to work again.
// wait until it works again.
assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {
@Override
public boolean conditionMet(TestMp mp) throws Throwable {
// this causes the container to clone the Mp
adaptor.pushMessage(new TestMessage("output"));
// wait for it to be received.
Thread.sleep(100);
// this will not go past 3 as long as the same TestMessage is sent.
return mp.cloneCalls.get() == 3;
}
}));
// this WONT causes the container to clone the Mp because test1 was already pre-instantiated.
adaptor.pushMessage(new TestMessage("test1"));
// give it a little time.
Thread.sleep(100);
// wait for it to be received.
assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {
@Override
public boolean conditionMet(TestMp mp) {
return mp.cloneCalls.get() == 3;
}
}));
List<Node> nodes = dempsy.getCluster(new ClusterId("test-app", "test-cluster1")).getNodes();
Assert.assertNotNull(nodes);
Assert.assertTrue(nodes.size() > 0);
Node node = nodes.get(0);
Assert.assertNotNull(node);
double duration = ((MetricGetters) node.getStatsCollector()).getPreInstantiationDuration();
Assert.assertTrue(duration > 0.0);
}
public String toString() {
return methodName;
}
@Override
public void setup() {
KeySourceImpl.disruptSession = disruptSession;
}
};
runAllCombinations("SinglestageWithKeyStoreApplicationActx.xml", checker);
runAllCombinations("SinglestageWithKeyStoreAndExecutorApplicationActx.xml", checker);
}
use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.
the class TestFullApp method testSeparateClustersInOneVm.
@Test
public void testSeparateClustersInOneVm() throws Throwable {
// now start each cluster
ctx[0] = "fullApp/Dempsy-FullUp.xml";
Map<ClusterId, DempsyHolder> dempsys = new HashMap<ClusterId, DempsyHolder>();
try {
ApplicationDefinition ad = new FullApplication().getTopology();
ad.initialize();
List<ClusterDefinition> clusters = ad.getClusterDefinitions();
for (int i = clusters.size() - 1; i >= 0; i--) {
ClusterDefinition cluster = clusters.get(i);
CheckCluster.toCheckAgainst = cluster.getClusterId();
DempsyHolder cur = new DempsyHolder();
cur.clusterid = cluster.getClusterId();
cur.actx = new ClassPathXmlApplicationContext(ctx);
cur.actx.registerShutdownHook();
cur.dempsy = (Dempsy) cur.actx.getBean("dempsy");
cur.dempsy.start();
dempsys.put(cluster.getClusterId(), cur);
}
// get the last FullApplication in the processing chain.
ClassPathXmlApplicationContext actx = dempsys.get(new ClusterId(FullApplication.class.getSimpleName(), MyRankMp.class.getSimpleName())).actx;
final FullApplication app = (FullApplication) actx.getBean("app");
// this checks that the throughput works.
assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {
@Override
public boolean conditionMet(Object o) {
return app.finalMessageCount.get() > 100;
}
}));
} finally {
ctx[0] = dempsyConfig;
for (DempsyHolder cur : dempsys.values()) {
cur.dempsy.stop();
cur.actx.close();
}
}
}
use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.
the class TestZookeeperClusterResilience method testNoServerOnStartup.
@Test
public void testNoServerOnStartup() throws Throwable {
// create a session factory
ZookeeperSessionFactory factory = new ZookeeperSessionFactory("127.0.0.1:" + port, 5000);
// create a session from the session factory
ZookeeperSession session = (ZookeeperSession) factory.createSession();
ClusterId clusterId = new ClusterId(appname, "testNoServerOnStartup");
// hook a test watch to make sure that callbacks work correctly
TestWatcher callback = new TestWatcher(session) {
@Override
public void process() {
called.set(true);
}
};
// now accessing the cluster should get us an error.
boolean gotCorrectError = false;
try {
session.getSubdirs(clusterId.asPath(), callback);
} catch (ClusterInfoException e) {
gotCorrectError = true;
}
assertTrue(gotCorrectError);
// now lets startup the server.
ZookeeperTestServer server = null;
try {
server = new ZookeeperTestServer();
server.start();
// create a cluster from the session
TestUtils.createClusterLevel(clusterId, session);
// wait until this works.
assertTrue(TestUtils.poll(baseTimeoutMillis, callback, new Condition<TestWatcher>() {
@Override
public boolean conditionMet(TestWatcher o) {
return o.called.get();
}
}));
// reset the callbacker ...
callback.called.set(false);
// now see if the cluster works.
assertTrue(TestUtils.poll(baseTimeoutMillis, callback, new Condition<TestWatcher>() {
@Override
public boolean conditionMet(TestWatcher o) {
return !o.called.get();
}
}));
session.getSubdirs(clusterId.asPath(), callback);
ZooKeeper origZk = session.zkref.get();
ZookeeperTestServer.forceSessionExpiration(origZk);
// wait for the callback
assertTrue(TestUtils.poll(baseTimeoutMillis, callback, new Condition<TestWatcher>() {
@Override
public boolean conditionMet(TestWatcher o) {
return o.called.get();
}
}));
// unfortunately I cannot check the getActiveSlots for failure because there's a race condition I can't fix.
// No matter how fast I check it's possible that it's okay again OR that allSlots hasn't been cleared.
//
// however, they should eventually recover.
gotCorrectError = true;
for (long endTime = System.currentTimeMillis() + baseTimeoutMillis; endTime > System.currentTimeMillis() && gotCorrectError; ) {
Thread.sleep(1);
try {
session.getSubdirs(clusterId.asPath(), callback);
gotCorrectError = false;
} catch (ClusterInfoException e) {
}
}
session.getSubdirs(clusterId.asPath(), callback);
// And join should work
gotCorrectError = true;
for (long endTime = System.currentTimeMillis() + baseTimeoutMillis; endTime > System.currentTimeMillis() && gotCorrectError; ) {
Thread.sleep(1);
try {
session.mkdir(clusterId.asPath() + "/join-1", null, DirMode.EPHEMERAL);
gotCorrectError = false;
} catch (ClusterInfoException e) {
}
}
assertFalse(gotCorrectError);
} finally {
if (server != null)
server.shutdown();
if (session != null)
session.stop();
}
}
use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.
the class TestZookeeperClusterResilience method testSessionExpiredWithFullApp.
@Test
public void testSessionExpiredWithFullApp() throws Throwable {
// now lets startup the server.
ZookeeperTestServer server = null;
final AtomicReference<ZookeeperSession> sessionRef = new AtomicReference<ZookeeperSession>();
ZookeeperSession session = null;
final AtomicLong processCount = new AtomicLong(0);
Dempsy[] dempsy = new Dempsy[3];
try {
server = new ZookeeperTestServer();
server.start();
session = new ZookeeperSession("127.0.0.1:" + port, 5000) {
@Override
public WatcherProxy makeWatcherProxy(ClusterInfoWatcher w) {
processCount.incrementAndGet();
return super.makeWatcherProxy(w);
}
;
};
sessionRef.set(session);
final FullApplication app = new FullApplication();
ApplicationDefinition ad = app.getTopology();
// no calls yet
assertEquals(0, processCount.intValue());
dempsy[0] = getDempsyFor(new ClusterId(FullApplication.class.getSimpleName(), FullApplication.MyAdaptor.class.getSimpleName()), ad);
dempsy[0].setClusterSessionFactory(new ZookeeperSessionFactory("127.0.0.1:" + port, 5000));
dempsy[1] = getDempsyFor(new ClusterId(FullApplication.class.getSimpleName(), FullApplication.MyMp.class.getSimpleName()), ad);
dempsy[1].setClusterSessionFactory(new ZookeeperSessionFactory("127.0.0.1:" + port, 5000));
dempsy[2] = getDempsyFor(new ClusterId(FullApplication.class.getSimpleName(), FullApplication.MyRankMp.class.getSimpleName()), ad);
// dempsy[2].setClusterSessionFactory(new ZookeeperSessionFactory<ClusterInformation, SlotInformation>("127.0.0.1:" + port,5000));
dempsy[2].setClusterSessionFactory(new ClusterInfoSessionFactory() {
@Override
public ClusterInfoSession createSession() throws ClusterInfoException {
return sessionRef.get();
}
});
// start everything in reverse order
for (int i = 2; i >= 0; i--) dempsy[i].start();
// make sure the final count is incrementing
long curCount = app.finalMessageCount.get();
assertTrue(poll(30000, curCount, new Condition<Long>() {
@Override
public boolean conditionMet(Long o) {
return app.finalMessageCount.get() > (o + 100L);
}
}));
logger.trace("Killing zookeeper");
ZooKeeper origZk = session.zkref.get();
ZookeeperTestServer.forceSessionExpiration(origZk);
logger.trace("Killed zookeeper");
// wait for the current session to go invalid
assertTrue(poll(baseTimeoutMillis, origZk, new Condition<ZooKeeper>() {
@Override
public boolean conditionMet(ZooKeeper o) {
return !o.getState().isAlive();
}
}));
// make sure the final count is STILL incrementing
curCount = app.finalMessageCount.get();
assertTrue(poll(30000, curCount, new Condition<Long>() {
@Override
public boolean conditionMet(Long o) {
return app.finalMessageCount.get() > (o + 100L);
}
}));
} finally {
if (server != null)
server.shutdown();
if (session != null)
session.stop();
for (int i = 0; i < dempsy.length; i++) if (dempsy[i] != null)
dempsy[i].stop();
for (int i = 0; i < dempsy.length; i++) if (dempsy[i] != null)
assertTrue(dempsy[i].waitToBeStopped(baseTimeoutMillis));
}
}
Aggregations