Search in sources :

Example 1 with MetricGetters

use of com.nokia.dempsy.monitoring.coda.MetricGetters 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);
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClusterInfoSessionFactory(com.nokia.dempsy.cluster.ClusterInfoSessionFactory) ClusterId(com.nokia.dempsy.config.ClusterId) ClusterInfoSession(com.nokia.dempsy.cluster.ClusterInfoSession) MetricGetters(com.nokia.dempsy.monitoring.coda.MetricGetters) Test(org.junit.Test)

Example 2 with MetricGetters

use of com.nokia.dempsy.monitoring.coda.MetricGetters 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);
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClusterId(com.nokia.dempsy.config.ClusterId) Node(com.nokia.dempsy.Dempsy.Application.Cluster.Node) MetricGetters(com.nokia.dempsy.monitoring.coda.MetricGetters)

Example 3 with MetricGetters

use of com.nokia.dempsy.monitoring.coda.MetricGetters in project Dempsy by Dempsy.

the class TestDempsy method testFailedMessageHandlingWithKeyStore.

@Test
public void testFailedMessageHandlingWithKeyStore() throws Throwable {
    final AtomicBoolean currentActivateCheckedException = new AtomicBoolean(false);
    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());
            // make sure that there are no Mps
            MetricGetters statsCollector = (MetricGetters) dempsy.getCluster(new ClusterId("test-app", "test-cluster1")).getNodes().get(0).getStatsCollector();
            Thread.sleep(10);
            assertEquals(0, statsCollector.getMessageProcessorsCreated());
            mp.failActivation.set("test1");
            TestAdaptor adaptor = (TestAdaptor) context.getBean("adaptor");
            // this causes the container to clone the Mp
            adaptor.pushMessage(new TestMessage("test1"));
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.cloneCalls.get() == 1;
                }
            }));
            Thread.sleep(100);
            assertEquals(0, statsCollector.getMessageProcessorsCreated());
            mp.failActivation.set(null);
            KeySourceImpl.pause.countDown();
            // instead of the latch we are going to poll for the correct result
            // wait for it to be received.
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.cloneCalls.get() == 3;
                }
            }));
            assertTrue(poll(baseTimeoutMillis, statsCollector, new Condition<MetricGetters>() {

                @Override
                public boolean conditionMet(MetricGetters mg) {
                    return mg.getMessageProcessorsCreated() == 2;
                }
            }));
            adaptor.pushMessage(new TestMessage("test1"));
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.handleCalls.get() == 1;
                }
            }));
            adaptor.pushMessage(new TestMessage("test2"));
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.handleCalls.get() == 2;
                }
            }));
            adaptor.pushMessage(new TestMessage("test1"));
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.handleCalls.get() == 3;
                }
            }));
            adaptor.pushMessage(new TestMessage("test2"));
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.handleCalls.get() == 4;
                }
            }));
            adaptor.pushMessage(new TestMessage("test1"));
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.handleCalls.get() == 5;
                }
            }));
            adaptor.pushMessage(new TestMessage("test2"));
            // instead of the latch we are going to poll for the correct result
            // wait for it to be received.
            assertTrue(poll(baseTimeoutMillis, mp, new Condition<TestMp>() {

                @Override
                public boolean conditionMet(TestMp mp) {
                    return mp.handleCalls.get() == 6;
                }
            }));
            Thread.sleep(100);
            assertEquals(6, mp.handleCalls.get());
            assertEquals(3, mp.cloneCalls.get());
            assertEquals(2, statsCollector.getMessageProcessorsCreated());
            // prepare for the next run
            KeySourceImpl.pause = new CountDownLatch(1);
        }

        public String toString() {
            return "testFailedMessageHandlingWithKeyStore";
        }

        public void setup() {
            KeySourceImpl.pause = new CountDownLatch(1);
            TestMp.activateCheckedException = currentActivateCheckedException.get();
        }
    };
    // make sure both exceptions are handled since the logic in the container
    // actually varies depending on whether or not the exception is checked or not.
    currentActivateCheckedException.set(true);
    runAllCombinations("SinglestageWithKeyStoreApplicationActx.xml", checker);
    currentActivateCheckedException.set(false);
    runAllCombinations("SinglestageWithKeyStoreAndExecutorApplicationActx.xml", checker);
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClusterId(com.nokia.dempsy.config.ClusterId) CountDownLatch(java.util.concurrent.CountDownLatch) MetricGetters(com.nokia.dempsy.monitoring.coda.MetricGetters) Test(org.junit.Test)

Example 4 with MetricGetters

use of com.nokia.dempsy.monitoring.coda.MetricGetters 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);
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) JunkDestination(com.nokia.dempsy.TestUtils.JunkDestination) ClusterInfoSessionFactory(com.nokia.dempsy.cluster.ClusterInfoSessionFactory) ClusterId(com.nokia.dempsy.config.ClusterId) DisruptibleSession(com.nokia.dempsy.cluster.DisruptibleSession) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClusterInfoSession(com.nokia.dempsy.cluster.ClusterInfoSession) DefaultRouterSlotInfo(com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo) MetricGetters(com.nokia.dempsy.monitoring.coda.MetricGetters) Test(org.junit.Test)

Example 5 with MetricGetters

use of com.nokia.dempsy.monitoring.coda.MetricGetters in project Dempsy by Dempsy.

the class TestMpContainer method testEvictCollision.

@Test
public void testEvictCollision() throws Throwable {
    // This forces the instantiation of an Mp
    BlockingQueueAdaptor adaptor = context.getBean(BlockingQueueAdaptor.class);
    assertNotNull(adaptor);
    adaptor.setFailFast(true);
    inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
    // Once the poll finishes the Mp is instantiated and handling messages.
    assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
    assertEquals("did not create MP", 1, container.getProcessorCount());
    TestProcessor mp = (TestProcessor) container.getMessageProcessor("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);
    // 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);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            container.evict();
            evictIsComplete.set(true);
        }
    });
    thread.start();
    //let it get going.
    Thread.sleep(50);
    // check to see we're hung.
    assertFalse(evictIsComplete.get());
    final MetricGetters sc = (MetricGetters) container.getStatsCollector();
    assertEquals(0, sc.getMessageCollisionCount());
    // sending it a message will now cause it to have the collision tick up
    inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
    assertTrue(TestUtils.poll(baseTimeoutMillis, sc, new TestUtils.Condition<MetricGetters>() {

        @Override
        public boolean conditionMet(MetricGetters o) {
            return o.getMessageCollisionCount() == 1;
        }
    }));
    // now let the evict finish
    mp.blockPassivate.countDown();
    // wait until the eviction completes
    assertTrue(TestUtils.poll(baseTimeoutMillis, evictIsComplete, new TestUtils.Condition<AtomicBoolean>() {

        @Override
        public boolean conditionMet(AtomicBoolean o) {
            return o.get();
        }
    }));
    // invocationCount should still be 1 from the initial invocation that caused the clone
    assertEquals(1, mp.invocationCount);
    // send a message that should go through
    inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
    // Once the poll finishes a new Mp is instantiated and handling messages.
    assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
    TestProcessor mp2 = (TestProcessor) container.getMessageProcessor("foo");
    // make sure this new Mp isn't the old one.
    assertTrue(mp != mp2);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingQueueAdaptor(com.nokia.dempsy.messagetransport.blockingqueue.BlockingQueueAdaptor) ContainerTestMessage(com.nokia.dempsy.container.mocks.ContainerTestMessage) CountDownLatch(java.util.concurrent.CountDownLatch) MetricGetters(com.nokia.dempsy.monitoring.coda.MetricGetters) Test(org.junit.Test)

Aggregations

MetricGetters (com.nokia.dempsy.monitoring.coda.MetricGetters)6 Test (org.junit.Test)5 Condition (com.nokia.dempsy.TestUtils.Condition)4 ClusterId (com.nokia.dempsy.config.ClusterId)4 ApplicationContext (org.springframework.context.ApplicationContext)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)2 ClusterInfoSessionFactory (com.nokia.dempsy.cluster.ClusterInfoSessionFactory)2 ContainerTestMessage (com.nokia.dempsy.container.mocks.ContainerTestMessage)2 BlockingQueueAdaptor (com.nokia.dempsy.messagetransport.blockingqueue.BlockingQueueAdaptor)2 Node (com.nokia.dempsy.Dempsy.Application.Cluster.Node)1 JunkDestination (com.nokia.dempsy.TestUtils.JunkDestination)1 DisruptibleSession (com.nokia.dempsy.cluster.DisruptibleSession)1 DefaultRouterSlotInfo (com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo)1