Search in sources :

Example 11 with Condition

use of com.nokia.dempsy.TestUtils.Condition 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 12 with Condition

use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.

the class TestFullApp method testFailover.

@Test
public void testFailover() throws Throwable {
    // now start each cluster
    ctx[0] = "fullApp/Dempsy-FullUp.xml";
    Map<ClusterId, DempsyHolder> dempsys = new HashMap<ClusterId, DempsyHolder>();
    DempsyHolder spare = new 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;
            }
        }));
        // now start another MyMp cluster.
        spare = new DempsyHolder();
        spare.clusterid = new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName());
        CheckCluster.toCheckAgainst = spare.clusterid;
        spare.actx = new ClassPathXmlApplicationContext(ctx);
        spare.dempsy = (Dempsy) spare.actx.getBean("dempsy");
        spare.dempsy.start();
        Dempsy.Application.Cluster cluster = spare.dempsy.getCluster(spare.clusterid);
        Dempsy.Application.Cluster.Node node = cluster.getNodes().get(0);
        final StatsCollector collector = node.getStatsCollector();
        // we are going to create another node of the MyMp via a test hack
        cluster = spare.dempsy.getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName()));
        node = cluster.getNodes().get(0);
        final MyMp spareprototype = (MyMp) node.getMpContainer().getPrototype();
        // TODO, see if we really need that check, and if so, implement
        // an alternate way to get it, since with the stats collector rework
        // we no longer use an independent MetricsRegistry per StatsCollector 
        // instance.
        assertEquals(0, ((MetricGetters) collector).getDispatchedMessageCount());
        assertEquals(0, spareprototype.myMpReceived.get());
        // now bring down the original
        DempsyHolder original = dempsys.get(spare.clusterid);
        final MyMp originalprototype = (MyMp) original.dempsy.getCluster(spare.clusterid).getNodes().get(0).getMpContainer().getPrototype();
        final long originalNumMessages = originalprototype.myMpReceived.get();
        // makes sure the message count is still advancing
        assertTrue(poll(baseTimeoutMillis, app, new Condition<Object>() {

            @Override
            public boolean conditionMet(Object o) {
                return originalprototype.myMpReceived.get() > originalNumMessages;
            }
        }));
        // check one more time
        assertEquals(0, spareprototype.myMpReceived.get());
        // now stop the original ... the spare should pick up.
        original.dempsy.stop();
        // there's a race condition between the stop returning and the last message
        // being processed.
        // we need to check that a certain amount of time passes during which no more messages have been received.
        // if we haven't seen a message in 1/2 second then we
        final long numMillisecondsWithoutAMessage = 500;
        //  will assume that the messages have stopped.
        // now we wait until at least numMillisecondsWithoutAMessage goes by without the myMpReceived
        //  being incremented. This must happen within the baseTimeoutMillis or this check is
        //   considered failed.
        poll(baseTimeoutMillis + numMillisecondsWithoutAMessage, originalprototype, new Condition<Object>() {

            long startCheckingTime = System.currentTimeMillis();

            long lastMessage = originalprototype.myMpReceived.get();

            @Override
            public boolean conditionMet(Object o) {
                if (originalprototype.myMpReceived.get() != lastMessage) {
                    startCheckingTime = System.currentTimeMillis();
                    lastMessage = originalprototype.myMpReceived.get();
                    return false;
                } else
                    return (System.currentTimeMillis() - startCheckingTime) > numMillisecondsWithoutAMessage;
            }
        });
        // now check to see if the new one picked up.
        assertTrue(poll(baseTimeoutMillis, app, new Condition<Object>() {

            @Override
            public boolean conditionMet(Object o) {
                return spareprototype.myMpReceived.get() > 10;
            }
        }));
    } finally {
        ctx[0] = dempsyConfig;
        for (DempsyHolder cur : dempsys.values()) {
            cur.dempsy.stop();
            cur.actx.close();
        }
        if (spare.dempsy != null)
            spare.dempsy.stop();
        if (spare.actx != null)
            spare.actx.close();
    }
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) ClusterDefinition(com.nokia.dempsy.config.ClusterDefinition) HashMap(java.util.HashMap) ClusterId(com.nokia.dempsy.config.ClusterId) StatsCollector(com.nokia.dempsy.monitoring.StatsCollector) Dempsy(com.nokia.dempsy.Dempsy) MyMp(com.nokia.dempsy.cluster.zookeeper.FullApplication.MyMp) ApplicationDefinition(com.nokia.dempsy.config.ApplicationDefinition) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MyRankMp(com.nokia.dempsy.cluster.zookeeper.FullApplication.MyRankMp) Test(org.junit.Test)

Example 13 with Condition

use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.

the class TestFullApp method testStartForceMpDisconnectStop.

@Test
public void testStartForceMpDisconnectStop() throws Throwable {
    ClassPathXmlApplicationContext actx = null;
    Dempsy dempsy = null;
    try {
        logger.debug("Starting up the appliction context ...");
        actx = new ClassPathXmlApplicationContext(ctx);
        actx.registerShutdownHook();
        final FullApplication app = (FullApplication) actx.getBean("app");
        dempsy = (Dempsy) actx.getBean("dempsy");
        // Override the cluster session factory to keep track of the sessions asked for.
        // This is so that I can grab the ZookeeperSession that's being instantiated by
        // the MyMp cluster.
        zookeeperCluster = null;
        dempsy.setClusterSessionFactory(new ZookeeperSessionFactory(System.getProperty("zk_connect"), 5000) {

            int sessionCount = 0;

            @Override
            public synchronized ClusterInfoSession createSession() throws ClusterInfoException {
                sessionCount++;
                ClusterInfoSession ret = super.createSession();
                if (sessionCount == 2)
                    zookeeperCluster = (ZookeeperSession) ret;
                return ret;
            }
        });
        dempsy.start();
        Dempsy.Application.Cluster cluster = dempsy.getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyAdaptor.class.getSimpleName()));
        Dempsy.Application.Cluster.Node node = cluster.getNodes().get(0);
        final StatsCollector collector = node.getStatsCollector();
        // this checks that the throughput works.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {

            @Override
            public boolean conditionMet(Object o) {
                return app.finalMessageCount.get() > 10;
            }
        }));
        assertNotNull(zookeeperCluster);
        assertEquals(0, ((MetricGetters) collector).getDiscardedMessageCount());
        assertEquals(0, ((MetricGetters) collector).getMessageFailedCount());
        // ok ... so now we have stuff going all the way through. let's kick
        // the middle Mp's zookeeper cluster and see what happens.
        ZooKeeper origZk = zookeeperCluster.zkref.get();
        long sessionid = origZk.getSessionId();
        ZooKeeper killer = new ZooKeeper(System.getProperty("zk_connect"), 5000, new Watcher() {

            @Override
            public void process(WatchedEvent arg0) {
            }
        }, sessionid, null);
        // tricks the server into expiring the other session
        killer.close();
        //         // we should be getting failures now ... 
        //         // but it's possible that it can reconnect prior to actually seeing an error so if this 
        //         //   fails frequently we need to remove this test.
        //         assertTrue(poll(baseTimeoutMillis, app, new Condition()
        //         {
        //            @Override
        //            public boolean conditionMet(Object o)
        //            {
        //               return collector.getMessageFailedCount() > 1;
        //            }
        //         }));
        //... and then recover.
        // get the MyMp prototype
        cluster = dempsy.getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName()));
        node = cluster.getNodes().get(0);
        final MyMp prototype = (MyMp) node.getMpContainer().getPrototype();
        // so let's see where we are
        final long interimMessageCount = prototype.myMpReceived.get();
        // and now we should eventually get more as the session recovers.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {

            @Override
            public boolean conditionMet(Object o) {
                return prototype.myMpReceived.get() > interimMessageCount + 100;
            }
        }));
    } finally {
        if (dempsy != null)
            dempsy.stop();
        if (actx != null)
            actx.close();
        if (dempsy != null)
            assertTrue(dempsy.waitToBeStopped(baseTimeoutMillis));
    }
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) ClusterId(com.nokia.dempsy.config.ClusterId) StatsCollector(com.nokia.dempsy.monitoring.StatsCollector) Dempsy(com.nokia.dempsy.Dempsy) ClusterInfoException(com.nokia.dempsy.cluster.ClusterInfoException) Watcher(org.apache.zookeeper.Watcher) MyMp(com.nokia.dempsy.cluster.zookeeper.FullApplication.MyMp) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterInfoSession(com.nokia.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Example 14 with Condition

use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.

the class TestFullApp method testStartStop.

@Test
public void testStartStop() throws Throwable {
    ClassPathXmlApplicationContext actx = null;
    Dempsy dempsy = null;
    try {
        logger.debug("Starting up the appliction context ...");
        actx = new ClassPathXmlApplicationContext(ctx);
        actx.registerShutdownHook();
        dempsy = (Dempsy) actx.getBean("dempsy");
        dempsy.start();
        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 {
        if (dempsy != null)
            dempsy.stop();
        if (actx != null)
            actx.close();
        if (dempsy != null)
            assertTrue(dempsy.waitToBeStopped(baseTimeoutMillis));
    }
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Dempsy(com.nokia.dempsy.Dempsy) Test(org.junit.Test)

Example 15 with Condition

use of com.nokia.dempsy.TestUtils.Condition in project Dempsy by Dempsy.

the class TestFullApp method testStartForceMpDisconnectWithStandby.

@Test
public void testStartForceMpDisconnectWithStandby() throws Throwable {
    ClassPathXmlApplicationContext actx = null;
    Dempsy dempsy = null;
    try {
        logger.debug("Starting up the appliction context ...");
        actx = new ClassPathXmlApplicationContext(ctx);
        actx.registerShutdownHook();
        final FullApplication app = (FullApplication) actx.getBean("app");
        dempsy = (Dempsy) actx.getBean("dempsy");
        // Override the cluster session factory to keep track of the sessions asked for.
        // This is so that I can grab the ZookeeperSession that's being instantiated by
        // the MyMp cluster.
        zookeeperCluster = null;
        dempsy.setClusterSessionFactory(new ZookeeperSessionFactory(System.getProperty("zk_connect"), 5000) {

            int sessionCount = 0;

            @Override
            public synchronized ClusterInfoSession createSession() throws ClusterInfoException {
                sessionCount++;
                ClusterInfoSession ret = super.createSession();
                if (sessionCount == 2)
                    zookeeperCluster = (ZookeeperSession) ret;
                return ret;
            }
        });
        dempsy.start();
        Dempsy.Application.Cluster cluster = dempsy.getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyAdaptor.class.getSimpleName()));
        Dempsy.Application.Cluster.Node node = cluster.getNodes().get(0);
        final StatsCollector collector = node.getStatsCollector();
        // we are going to create another node of the MyMp via a test hack
        cluster = dempsy.getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName()));
        Dempsy.Application.Cluster.Node mpnode = cluster.getNodes().get(0);
        // this actually hoses the output schedule. It moves it to the new node due
        //  to a second call to setOutputInvoker.
        // the code for start instantiates a new node
        cluster.instantiateAndStartAnotherNodeForTesting();
        assertNotNull(zookeeperCluster);
        assertEquals(0, ((MetricGetters) collector).getDiscardedMessageCount());
        assertEquals(0, ((MetricGetters) collector).getMessageFailedCount());
        // ok ... so now we have stuff going all the way through. let's kick
        // the middle Mp's zookeeper cluster and see what happens.
        ZooKeeper origZk = zookeeperCluster.zkref.get();
        // this should kill it.
        origZk.close();
        // but just to be sure actually stop the node.
        mpnode.stop();
        // we should be getting failures now ... and then recover.
        // get the MyMp prototype
        cluster = dempsy.getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName()));
        // notice, we're getting the SECOND node.
        node = cluster.getNodes().get(1);
        final MyMp prototype = (MyMp) node.getMpContainer().getPrototype();
        // so let's see where we are
        final long interimMessageCount = prototype.myMpReceived.get();
        // and now we should eventually get more as the session recovers.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {

            @Override
            public boolean conditionMet(Object o) {
                return prototype.myMpReceived.get() > interimMessageCount + 100;
            }
        }));
    } finally {
        if (dempsy != null)
            dempsy.stop();
        if (actx != null)
            actx.close();
        if (dempsy != null)
            assertTrue(dempsy.waitToBeStopped(baseTimeoutMillis));
    }
}
Also used : Condition(com.nokia.dempsy.TestUtils.Condition) ClusterId(com.nokia.dempsy.config.ClusterId) StatsCollector(com.nokia.dempsy.monitoring.StatsCollector) Dempsy(com.nokia.dempsy.Dempsy) ClusterInfoException(com.nokia.dempsy.cluster.ClusterInfoException) MyMp(com.nokia.dempsy.cluster.zookeeper.FullApplication.MyMp) ZooKeeper(org.apache.zookeeper.ZooKeeper) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterInfoSession(com.nokia.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Aggregations

Condition (com.nokia.dempsy.TestUtils.Condition)15 ClusterId (com.nokia.dempsy.config.ClusterId)13 Test (org.junit.Test)13 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)10 ClusterInfoException (com.nokia.dempsy.cluster.ClusterInfoException)6 Dempsy (com.nokia.dempsy.Dempsy)5 ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)5 ZooKeeper (org.apache.zookeeper.ZooKeeper)5 ApplicationContext (org.springframework.context.ApplicationContext)5 MetricGetters (com.nokia.dempsy.monitoring.coda.MetricGetters)4 ClusterInfoSessionFactory (com.nokia.dempsy.cluster.ClusterInfoSessionFactory)3 MyMp (com.nokia.dempsy.cluster.zookeeper.FullApplication.MyMp)3 ApplicationDefinition (com.nokia.dempsy.config.ApplicationDefinition)3 StatsCollector (com.nokia.dempsy.monitoring.StatsCollector)3 Node (com.nokia.dempsy.Dempsy.Application.Cluster.Node)2 MyRankMp (com.nokia.dempsy.cluster.zookeeper.FullApplication.MyRankMp)2 ClusterDefinition (com.nokia.dempsy.config.ClusterDefinition)2 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2