use of com.nokia.dempsy.cluster.DisruptibleSession in project Dempsy by Dempsy.
the class TestUtils method stealShard.
/**
* This method will grab the slot requested. It requires that it is already held by
* the session provided and that the entry there contains a valid DefaultRouterSlotInfo
* which it will extract, modify and use to replace.
*
* This will be accomplished by disrupting the session and trying to grab the slot
* at the same time. It will try this over and over until it gets it, or until the
* number of tries is exceeded.
*
* @param originalSession is the session that will be disrupted in order to grab the shard.
* @param factory is the {@link ClusterInfoSessionFactory} that will be used to create a new
* session that can be used to grab the slot.
* @param shardPath is the path all the way to the directory containing the shard that you
* want stolen.
*
* @throws Assert when one of the test condition fails or grabbing the slot fails.
*/
public static ClusterInfoSession stealShard(final ClusterInfoSession originalSession, final ClusterInfoSessionFactory factory, final String shardPath, final long timeoutmillis) throws InterruptedException, ClusterInfoException {
// get the current slot data to use as a template
final DefaultRouterSlotInfo newSlot = (DefaultRouterSlotInfo) originalSession.getData(shardPath, null);
final AtomicBoolean stillRunning = new AtomicBoolean(true);
final AtomicBoolean failed = new AtomicBoolean(false);
final ClusterInfoSession session = factory.createSession();
Runnable slotGrabber = new Runnable() {
@Override
public void run() {
try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
boolean haveSlot = false;
while (!haveSlot && stillRunning.get()) {
newSlot.setDestination(new JunkDestination());
if (session.mkdir(shardPath, newSlot, DirMode.EPHEMERAL) != null)
haveSlot = true;
Thread.yield();
}
} catch (ClusterInfoException e) {
failed.set(true);
} catch (RuntimeException re) {
re.printStackTrace();
failed.set(true);
} finally {
stillRunning.set(false);
}
}
};
try {
new Thread(slotGrabber).start();
boolean onStandby = false;
long startTime = System.currentTimeMillis();
while (!onStandby && timeoutmillis >= (System.currentTimeMillis() - startTime)) {
((DisruptibleSession) originalSession).disrupt();
Thread.sleep(100);
if (!stillRunning.get())
onStandby = true;
}
assertTrue(onStandby);
assertFalse(failed.get());
} catch (InterruptedException ie) {
session.stop();
throw ie;
} catch (Error cie) {
session.stop();
throw cie;
} finally {
stillRunning.set(false);
}
return session;
}
use of com.nokia.dempsy.cluster.DisruptibleSession in project Dempsy by Dempsy.
the class TestDempsy method testFailedClusterManagerDuringKeyStoreCalls.
@Test
public void testFailedClusterManagerDuringKeyStoreCalls() 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();
assertTrue(poll(baseTimeoutMillis, statsCollector, new Condition<MetricGetters>() {
@Override
public boolean conditionMet(MetricGetters sc) {
return 100000 == sc.getMessageProcessorCount();
}
}));
// now there's 100000 mps in the container created from the KeySource. So we steal the
// shard and force if offline but continuously disrupt it while it tries to come
// back up.
// 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();
String path = clusterId.asPath() + "/" + String.valueOf(0);
session = TestUtils.stealShard(originalSession, factory, path, baseTimeoutMillis);
DefaultRouterSlotInfo si = (DefaultRouterSlotInfo) session.getData(path, null);
// checks to see who actually has the slot.
assertTrue(si.getDestination() instanceof JunkDestination);
// we will keep disrupting the session but we should still end up with zero mps.
for (int i = 0; i < 100; i++) {
((DisruptibleSession) originalSession).disrupt();
Thread.sleep(1);
}
// now wait until we get 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());
// ok. Now we will close the session that's holding the shard and allow the container
// to re-establish control of that shard. During the KeyStore reinstantiation of the
// MPs we will be disrupting the session.
session.stop();
for (int i = 0; i < 100; i++) {
((DisruptibleSession) originalSession).disrupt();
Thread.sleep(1);
}
// Now we should get back to 100000 Mps.
poll(baseTimeoutMillis, statsCollector, new Condition<MetricGetters>() {
@Override
public boolean conditionMet(MetricGetters sc) {
return 100000 == sc.getMessageProcessorCount();
}
});
assertEquals(100000, statsCollector.getMessageProcessorCount());
} finally {
if (session != null)
session.stop();
}
}
public String toString() {
return "testFailedClusterManagerDuringKeyStoreCalls";
}
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.cluster.DisruptibleSession in project Dempsy by Dempsy.
the class TestDempsy method testForkedFailure.
@Test
public void testForkedFailure() throws Throwable {
runAllCombinations("SimpleMultistageApplicationActx.xml", new Checker() {
@Override
public void check(ApplicationContext context) throws Throwable {
final AtomicBoolean stopIt = new AtomicBoolean(false);
final AtomicBoolean failed = new AtomicBoolean(false);
final AtomicBoolean stopped = new AtomicBoolean(false);
try {
// start things and verify that the init method was called
Dempsy dempsy = (Dempsy) context.getBean("dempsy");
final TestAdaptor adaptor = (TestAdaptor) getAdaptor(dempsy, "test-app", "test-cluster0");
assertNotNull(adaptor);
Thread adaptorThread = new Thread(new Runnable() {
@Override
public void run() {
try {
long i = 0;
while (!stopIt.get()) {
adaptor.pushMessage(new TestMessage("" + i));
i++;
Thread.sleep(10);
}
} catch (Throwable th) {
failed.set(true);
} finally {
stopped.set(true);
}
}
}, "testForkedFailure-Adaptor Thread ");
adaptorThread.start();
TestMp[] mps = new TestMp[3];
DisruptibleSession[] sessions = new DisruptibleSession[3];
for (int i = 0; i < mps.length; i++) {
String cluster = "test-cluster" + (i + 1);
mps[i] = (TestMp) getMp(dempsy, "test-app", cluster);
sessions[i] = (DisruptibleSession) (dempsy.getCluster(new ClusterId("test-app", cluster)).getNodes().get(0).retouRteg().getClusterSession());
assertEquals(1, mps[i].startCalls.get());
}
for (int i = 0; i < mps.length; i++) {
for (int j = 0; j < mps.length; j++) {
if (i != j)
assertTrue(mps[i] != mps[j]);
}
}
// now check to see that data is going to all 3.
for (int i = 0; i < mps.length; i++) {
assertTrue(poll(baseTimeoutMillis, mps[i], new Condition<TestMp>() {
public boolean conditionMet(TestMp o) {
return o.handleCalls.get() > 0;
}
}));
}
int curPos = 0;
for (int j = 0; j < 3; j++) {
// now kill a cluster or 2 (or 3)
for (int k = 0; k <= j; k++) sessions[curPos++ % sessions.length].disrupt();
for (int i = 0; i < mps.length; i++) {
final long curCalls = mps[i].handleCalls.get();
assertTrue(poll(baseTimeoutMillis, mps[i], new Condition<TestMp>() {
public boolean conditionMet(TestMp o) {
return o.handleCalls.get() > curCalls;
}
}));
}
}
} finally {
stopIt.set(true);
assertFalse(failed.get());
assertTrue(poll(baseTimeoutMillis, stopped, new Condition<AtomicBoolean>() {
public boolean conditionMet(AtomicBoolean o) {
return o.get();
}
}));
}
}
public String toString() {
return "testForkedFailure";
}
});
}
Aggregations