use of net.dempsy.lifecycle.annotation.Mp in project Dempsy by Dempsy.
the class TestContainer method createAndGet.
private TestProcessor createAndGet(final String foo) throws Exception {
cache = new HashMap<>();
final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
assertNotNull(adaptor.dispatcher);
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage(foo));
assertTrue(poll(o -> container.getProcessorCount() > 0));
Thread.sleep(100);
assertEquals("did not create MP", 1, container.getProcessorCount());
assertTrue(poll(cache, c -> c.get(foo) != null));
final TestProcessor mp = cache.get(foo);
assertNotNull("MP not associated with expected key", mp);
assertEquals("activation count, 1st message", 1, mp.activationCount);
assertEquals("invocation count, 1st message", 1, mp.invocationCount);
return mp;
}
use of net.dempsy.lifecycle.annotation.Mp in project Dempsy by Dempsy.
the class TestContainer method testMtInvokeOutput.
@Test
public void testMtInvokeOutput() throws Exception {
outputMessages = Collections.newSetFromMap(new ConcurrentHashMap<>());
final int numInstances = 20;
final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
assertNotNull(adaptor.dispatcher);
for (int i = 0; i < numInstances; i++) {
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo" + i));
// help the container when it has a limited queue
Thread.yield();
}
assertTrue(poll(container, c -> (c.getProcessorCount() + ((ClusterMetricGetters) c.statCollector).getMessageDiscardedCount()) > 19));
Thread.sleep(100);
final long messagesDiscarded = ((ClusterMetricGetters) container.statCollector).getMessageDiscardedCount();
assertEquals("number of MP instances", 20, container.getProcessorCount() + messagesDiscarded);
try (NodeManager nman = addOutputCatchStage()) {
container.outputPass();
assertTrue(poll(outputMessages, o -> (o.size() + messagesDiscarded) > 19));
Thread.sleep(100);
assertEquals(20, outputMessages.size() + messagesDiscarded);
}
}
use of net.dempsy.lifecycle.annotation.Mp in project Dempsy by Dempsy.
the class TestContainer method testInvokeOutput.
@Test
public void testInvokeOutput() throws Exception {
outputMessages = Collections.newSetFromMap(new ConcurrentHashMap<>());
cache = new ConcurrentHashMap<>();
final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
assertNotNull(adaptor.dispatcher);
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("bar"));
assertTrue(poll(container, c -> (c.getProcessorCount() + ((ClusterMetricGetters) c.statCollector).getMessageDiscardedCount()) > 1));
Thread.sleep(100);
assertEquals("number of MP instances", 2, container.getProcessorCount());
try (NodeManager nman = addOutputCatchStage()) {
final TestProcessor mp = cache.get("foo");
assertTrue(poll(mp, m -> mp.invocationCount > 0));
Thread.sleep(100);
assertEquals("invocation count, 1st message", 1, mp.invocationCount);
// because the sessionFactory is shared and the appname is the same, we should be in the same app
container.outputPass();
assertTrue(poll(outputMessages, o -> o.size() > 1));
Thread.sleep(100);
assertEquals(2, outputMessages.size());
// no new mps created in the first one
assertEquals("did not create MP", 2, container.getProcessorCount());
// but the invocation count should have increased since the output cycles feeds messages back to this cluster
assertTrue(poll(mp, m -> mp.invocationCount > 1));
Thread.sleep(100);
assertEquals("invocation count, 1st message", 2, mp.invocationCount);
// // order of messages is not guaranteed, so we need to aggregate keys
final HashSet<String> messageKeys = new HashSet<String>();
final Iterator<OutputMessage> iter = outputMessages.iterator();
messageKeys.add(iter.next().getKey());
messageKeys.add(iter.next().getKey());
assertTrue("first MP sent output", messageKeys.contains("foo"));
assertTrue("second MP sent output", messageKeys.contains("bar"));
}
}
use of net.dempsy.lifecycle.annotation.Mp in project Dempsy by Dempsy.
the class TestContainer method testEvictCollisionWithBlocking.
@Test
public void testEvictCollisionWithBlocking() throws Throwable {
final TestProcessor mp = createAndGet("foo");
// now we're going to cause the passivate to be held up.
mp.blockPassivate = new CountDownLatch(1);
// allow eviction
mp.evict.set(true);
// now kick off the evict in a separate thread since we expect it to hang
// until the mp becomes unstuck.
// this will allow us to see the evict pass complete
final AtomicBoolean evictIsComplete = new AtomicBoolean(false);
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
container.evict();
evictIsComplete.set(true);
}
});
thread.start();
// let it get going.
Thread.sleep(500);
// check to see we're hung.
assertFalse(evictIsComplete.get());
final ClusterMetricGetters sc = (ClusterMetricGetters) statsCollector;
assertEquals(0, sc.getMessageCollisionCount());
// sending it a message will now cause it to have the collision tick up
final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
// give it some time.
Thread.sleep(100);
// make sure there's no collision
assertEquals(0, sc.getMessageCollisionCount());
// make sure no message got handled
// 1 is the initial invocation that caused the instantiation.
assertEquals(1, mp.invocationCount);
// now let the evict finish
mp.blockPassivate.countDown();
// wait until the eviction completes
assertTrue(poll(evictIsComplete, o -> o.get()));
// Once the poll finishes a new Mp is instantiated and handling messages.
assertTrue(poll(cache, c -> c.get("foo") != null));
final TestProcessor mp2 = cache.get("foo");
assertNotNull("MP not associated with expected key", mp);
// invocationCount should be 1 from the initial invocation that caused the clone, and no more
assertEquals(1, mp.invocationCount);
assertEquals(1, mp2.invocationCount);
assertTrue(mp != mp2);
// send a message that should go through
adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
assertTrue(poll(o -> mp2.invocationCount > 1));
Thread.sleep(100);
assertEquals(1, mp.invocationCount);
assertEquals(2, mp2.invocationCount);
}
use of net.dempsy.lifecycle.annotation.Mp in project Dempsy by Dempsy.
the class TestElasticity method testForProfiler.
@Test
public void testForProfiler() throws Throwable {
try {
// set up the test.
final Number[] numbers = new Number[profilerTestNumberCount];
final Random random = new Random();
for (int i = 0; i < numbers.length; i++) numbers[i] = new Number(random.nextInt(1000), 0);
final KeyExtractor ke = new KeyExtractor();
runCombos("testForProfiler", (r, c, s, t, ser) -> isElasticRoutingStrategy(r), actxPath, new String[][][] { null, { { "min_nodes", "3" } }, { { "min_nodes", "3" } }, { { "min_nodes", "3" } }, null }, ns -> {
final List<NodeManagerWithContext> nodes = ns.nodes;
// Grab the one NumberRank Mp from the single Node in the third (0 base 2nd) cluster.
final NumberRank rank = nodes.get(4).ctx.getBean(NumberRank.class);
try (final ClusterInfoSession session = ns.sessionFactory.createSession()) {
waitForEvenShardDistribution(session, "test-cluster1", 3, nodes);
// get the Adaptor Router's statCollector
final List<NodeMetricGetters> scs = nodes.stream().map(nwm -> nwm.manager).map(nm -> nm.getNodeStatsCollector()).map(sc -> (NodeMetricGetters) sc).collect(Collectors.toList());
// grab the adaptor from the 0'th cluster + the 0'th (only) node.
final NumberProducer adaptor = nodes.get(0).ctx.getBean(NumberProducer.class);
// grab access to the Dispatcher from the Adaptor
final Dispatcher dispatcher = adaptor.dispatcher;
final long startTime = System.currentTimeMillis();
for (int i = 0; i < numbers.length; i++) dispatcher.dispatch(ke.extract(numbers[i]));
LOGGER.info("====> Checking exact count.");
// keep going as long as they are trickling in.
assertTrue(() -> {
IntStream.range(0, scs.size()).forEach(i -> {
System.out.println("======> " + i);
final NodeMetricGetters mg = scs.get(i);
if (mg != null) {
System.out.println("discarded: " + mg.getDiscardedMessageCount());
System.out.println("not sent: " + mg.getMessagesNotSentCount());
}
});
return "expected: " + profilerTestNumberCount + " but was: " + (rank.totalMessages.get() + scs.get(0).getMessagesNotSentCount()) + ", (delivered: " + rank.totalMessages.get() + ", not sent: " + scs.get(0).getMessagesNotSentCount() + ")";
}, poll(o -> profilerTestNumberCount == (rank.totalMessages.get() + scs.stream().map(sc -> Long.valueOf(sc.getMessagesNotSentCount())).reduce(Long.valueOf(0), (v1, v2) -> Long.valueOf(v1.longValue() + v2.longValue()).longValue()))));
// assert that at least SOMETHING went through
assertTrue(rank.totalMessages.get() > 0);
LOGGER.info("testForProfiler time " + (System.currentTimeMillis() - startTime));
@SuppressWarnings("unchecked") final AtomicLong count = nodes.stream().map(// get the NumberCounter Mp
nmwc -> (MessageProcessor<NumberCounter>) nmwc.manager.getMp("test-cluster1")).filter(// if it exists
l -> l != null).map(// pull the prototype
l -> l.getPrototype().messageCount).reduce(new AtomicLong(0), // sum up all of the counts
(v1, v2) -> new AtomicLong(v1.get() + v2.get()));
assertEquals(profilerTestNumberCount, count.get());
}
});
} catch (final Throwable th) {
th.printStackTrace();
throw th;
}
}
Aggregations