Search in sources :

Example 1 with MessageProcessorLifecycle

use of net.dempsy.messages.MessageProcessorLifecycle in project Dempsy by Dempsy.

the class TestWordCount method runTestWithOutputCycle.

private void runTestWithOutputCycle(final String testName, final String outputSchedCtx) throws Exception {
    final String[][] ctxs = { // adaptor only node
    { "classpath:/word-count/adaptor-kjv.xml" }, { "classpath:/word-count/mp-word-count.xml", "classpath:/word-count/mp-word-count-output.xml", outputSchedCtx }, { "classpath:/word-count/mp-word-count.xml", "classpath:/word-count/mp-word-count-output.xml", outputSchedCtx }, { "classpath:/word-count/mp-rank-catcher.xml" } };
    // the adaptor is the first one, rank catcher the last.
    final int NUM_WC = ctxs.length - 2;
    try (@SuppressWarnings("resource") final SystemPropertyManager props = new SystemPropertyManager().set("min_nodes", Integer.toString(NUM_WC)).set("routing-group", ":group").set("send_threads", "1").set("receive_threads", "1").set("blocking-queue-size", "500000")) {
        // need to make it wait.
        WordProducer.latch = new CountDownLatch(1);
        runCombos(testName, (r, c, s, t, ser) -> isElasticRoutingStrategy(r) && isContainerOkay(c), ctxs, n -> {
            final List<NodeManagerWithContext> nodes = n.nodes;
            final NodeManager[] managers = nodes.stream().map(nm -> nm.manager).toArray(NodeManager[]::new);
            // wait until I can reach the cluster from the adaptor.
            assertTrue(poll(o -> managers[0].getRouter().allReachable("test-cluster1").size() == NUM_WC));
            assertTrue(poll(o -> managers[0].getRouter().allReachable("test-cluster2").size() == NUM_WC));
            assertTrue(poll(o -> managers[0].getRouter().allReachable("test-cluster3").size() == 1));
            for (int i = 0; i < NUM_WC; i++) {
                // the +1 is because the first (0th) manager is the adaptor
                final int managerIndex = i + 1;
                assertTrue(poll(o -> managers[managerIndex].getRouter().allReachable("test-cluster3").size() == 1));
            }
            WordProducer.latch.countDown();
            final WordProducer adaptor = nodes.get(0).ctx.getBean(WordProducer.class);
            waitForAllSent(adaptor);
            // the last node has the RankCatcher
            final NodeManager manager = nodes.get(nodes.size() - 1).manager;
            final MessageProcessorLifecycle<?> mp = AccessUtil.getMp(manager, "test-cluster3");
            @SuppressWarnings("unchecked") final RankCatcher prototype = ((MessageProcessor<RankCatcher>) mp).getPrototype();
            if (strict) {
                final HashSet<String> expected = new HashSet<>(Arrays.asList("the", "that", "unto", "in", "and", "And", "of", "shall", "to", "he"));
                assertTrue(() -> {
                    return "FAILURE:" + expected.toString() + " != " + prototype.topRef.get();
                }, poll(prototype.topRef, tr -> {
                    final List<Rank> cur = tr.get();
                    final HashSet<String> topSet = new HashSet<>(cur.stream().map(r -> r.word).collect(Collectors.toSet()));
                    // once more than 1/2 of the list is there then we're done.
                    final int threshold = (expected.size() / 2) + 1;
                    int matches = 0;
                    for (final String exp : expected) if (topSet.contains(exp))
                        matches++;
                    return matches >= threshold;
                }));
            }
        });
    }
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentSet(io.netty.util.internal.ConcurrentSet) Triplet(org.javatuples.Triplet) Map(java.util.Map) ThreadingModel(net.dempsy.threading.ThreadingModel) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Activation(net.dempsy.lifecycle.annotation.Activation) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Pair(org.javatuples.Pair) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) ConditionPoll.assertTrue(net.dempsy.utils.test.ConditionPoll.assertTrue) Optional(java.util.Optional) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Mp(net.dempsy.lifecycle.annotation.Mp) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Tuple(org.javatuples.Tuple) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) NodeMetricGetters(net.dempsy.container.NodeMetricGetters) StringWriter(java.io.StringWriter) ClusterMetricGetters(net.dempsy.container.ClusterMetricGetters) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Passivation(net.dempsy.lifecycle.annotation.Passivation) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) MessageProcessorLifecycle(net.dempsy.messages.MessageProcessorLifecycle) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) CountDownLatch(java.util.concurrent.CountDownLatch) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 2 with MessageProcessorLifecycle

use of net.dempsy.messages.MessageProcessorLifecycle in project Dempsy by Dempsy.

the class TestWordCount method testWordCountWithRank.

@Test
public void testWordCountWithRank() throws Throwable {
    try (@SuppressWarnings("resource") final SystemPropertyManager props = new SystemPropertyManager().set("min_nodes", "1")) {
        final String[][] ctxs = { { "classpath:/word-count/adaptor-kjv.xml", "classpath:/word-count/mp-word-count.xml", "classpath:/word-count/mp-word-rank.xml" } };
        WordCounter.aliveWordsToTestPassivate.clear();
        // need to make it wait.
        WordProducer.latch = new CountDownLatch(1);
        runCombos("testWordCountWithRank", (r, c, s, t, ser) -> isContainerOkay(c), ctxs, n -> {
            final ClusterId countCId = new ClusterId(currentAppName, "test-cluster1");
            final ClusterId rankCId = new ClusterId(currentAppName, "test-cluster2");
            final List<NodeManagerWithContext> nodes = n.nodes;
            final NodeManager manager = nodes.get(0).manager;
            // wait until I can reach the cluster from the adaptor.
            assertTrue(poll(o -> manager.getRouter().allReachable("test-cluster1").size() == 1));
            final ClassPathXmlApplicationContext ctx = nodes.get(0).ctx;
            final WordProducer adaptor;
            WordProducer.latch.countDown();
            adaptor = ctx.getBean(WordProducer.class);
            assertTrue(poll(o -> adaptor.done.get()));
            final var statsByNode = Triplet.with((NodeMetricGetters) manager.getNodeStatsCollector(), (ClusterMetricGetters) manager.getClusterStatsCollector(countCId), (ClusterMetricGetters) manager.getClusterStatsCollector(rankCId));
            // wait until all of the counts are also passed to WordRank
            assertTrue(poll(o -> {
                final int allDiscardedMessagesCount = allDiscardedMessagesCount(null, castToTupleList3(List.of(statsByNode)));
                final ClusterMetricGetters rstat = statsByNode.getValue2();
                return adaptor.numDispatched == allDiscardedMessagesCount + rstat.getDispatchedMessageCount();
            }));
            stopSystem();
            // pull the Rank mp from the manager
            final MessageProcessorLifecycle<?> mp = AccessUtil.getMp(manager, "test-cluster2");
            @SuppressWarnings("unchecked") final WordRank prototype = ((MessageProcessor<WordRank>) mp).getPrototype();
            final List<Rank> ranks = prototype.getPairs();
            Collections.sort(ranks, (o1, o2) -> o2.rank.compareTo(o1.rank));
            final int top10OrSo = (ranks.size() < 10) ? ranks.size() : 10;
            final List<Rank> top10 = ranks.subList(0, top10OrSo);
            final String errStr = "expected the top 10 results: " + top10 + " to all be in:" + finalResults;
            top10.stream().forEach(r -> assertTrue(errStr, finalResults.contains(r.word)));
        });
        final int remaining = WordCounter.aliveWordsToTestPassivate.size();
        WordCounter.aliveWordsToTestPassivate.clear();
        assertEquals(0, remaining);
    }
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentSet(io.netty.util.internal.ConcurrentSet) Triplet(org.javatuples.Triplet) Map(java.util.Map) ThreadingModel(net.dempsy.threading.ThreadingModel) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Activation(net.dempsy.lifecycle.annotation.Activation) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Pair(org.javatuples.Pair) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) ConditionPoll.assertTrue(net.dempsy.utils.test.ConditionPoll.assertTrue) Optional(java.util.Optional) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Mp(net.dempsy.lifecycle.annotation.Mp) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Tuple(org.javatuples.Tuple) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) NodeMetricGetters(net.dempsy.container.NodeMetricGetters) StringWriter(java.io.StringWriter) ClusterMetricGetters(net.dempsy.container.ClusterMetricGetters) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Passivation(net.dempsy.lifecycle.annotation.Passivation) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) MessageProcessorLifecycle(net.dempsy.messages.MessageProcessorLifecycle) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ClusterId(net.dempsy.config.ClusterId) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) CountDownLatch(java.util.concurrent.CountDownLatch) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterMetricGetters(net.dempsy.container.ClusterMetricGetters) Test(org.junit.Test)

Aggregations

ConcurrentSet (io.netty.util.internal.ConcurrentSet)2 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 StringWriter (java.io.StringWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2