Search in sources :

Example 1 with DefaultRouterSlotInfo

use of com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo 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;
}
Also used : ClusterInfoException(com.nokia.dempsy.cluster.ClusterInfoException) DisruptibleSession(com.nokia.dempsy.cluster.DisruptibleSession) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterInfoSession(com.nokia.dempsy.cluster.ClusterInfoSession) DefaultRouterSlotInfo(com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo)

Example 2 with DefaultRouterSlotInfo

use of com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo 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)

Aggregations

ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)2 DisruptibleSession (com.nokia.dempsy.cluster.DisruptibleSession)2 DefaultRouterSlotInfo (com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo)2 Condition (com.nokia.dempsy.TestUtils.Condition)1 JunkDestination (com.nokia.dempsy.TestUtils.JunkDestination)1 ClusterInfoException (com.nokia.dempsy.cluster.ClusterInfoException)1 ClusterInfoSessionFactory (com.nokia.dempsy.cluster.ClusterInfoSessionFactory)1 ClusterId (com.nokia.dempsy.config.ClusterId)1 MetricGetters (com.nokia.dempsy.monitoring.coda.MetricGetters)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1