Search in sources :

Example 6 with ApplicationDefinition

use of com.nokia.dempsy.config.ApplicationDefinition 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 7 with ApplicationDefinition

use of com.nokia.dempsy.config.ApplicationDefinition in project Dempsy by Dempsy.

the class Dempsy method start.

public synchronized void start() throws DempsyException {
    if (isRunning())
        throw new DempsyException("The Dempsy application " + applicationDefinitions + " has already been started.");
    if (applicationDefinitions == null || applicationDefinitions.size() == 0)
        throw new DempsyException("Cannot start this application because there are no ApplicationDefinitions");
    if (clusterSessionFactory == null)
        throw new DempsyException("Cannot start this application because there was no ClusterFactory implementaiton set.");
    if (clusterCheck == null)
        throw new DempsyException("Cannot start this application because there's no way to tell which cluster to start. Make sure the appropriate " + CurrentClusterCheck.class.getSimpleName() + " is set.");
    if (defaultRoutingStrategy == null)
        throw new DempsyException("Cannot start this application because there's no default routing strategy defined.");
    if (defaultSerializer == null)
        throw new DempsyException("Cannot start this application because there's no default serializer defined.");
    if (transport == null)
        throw new DempsyException("Cannot start this application because there's no transport implementation defined");
    if (defaultStatsCollectorFactory == null)
        throw new DempsyException("Cannot start this application because there's no default stats collector factory defined.");
    try {
        applications = new ArrayList<Application>(applicationDefinitions.size());
        for (ApplicationDefinition appDef : this.applicationDefinitions) {
            appDef.initialize();
            if (clusterCheck.isThisNodePartOfApplication(appDef.getApplicationName())) {
                Application app = new Application(appDef);
                // set the default routing strategy if there isn't one already set.
                if (appDef.getRoutingStrategy() == null)
                    appDef.setRoutingStrategy(defaultRoutingStrategy);
                if (appDef.getSerializer() == null)
                    appDef.setSerializer(defaultSerializer);
                if (appDef.getStatsCollectorFactory() == null)
                    appDef.setStatsCollectorFactory(defaultStatsCollectorFactory);
                applications.add(app);
            }
        }
        boolean clusterStarted = false;
        for (Application app : applications) clusterStarted = app.start();
        if (!clusterStarted) {
            throw new DempsyException("Cannot start this application because cluster defination was not found.");
        }
        // if we got to here we can assume we're started
        synchronized (isRunningEvent) {
            isRunning = true;
        }
    } catch (RuntimeException rte) {
        logger.error("Failed to start Dempsy. Attempting to stop.");
        // if something unpexpected happened then we should attempt to stop
        try {
            stop();
        } catch (Throwable th) {
        }
        throw rte;
    }
}
Also used : ApplicationDefinition(com.nokia.dempsy.config.ApplicationDefinition)

Example 8 with ApplicationDefinition

use of com.nokia.dempsy.config.ApplicationDefinition in project Dempsy by Dempsy.

the class TestRouterClusterManagement method init.

@Before
public void init() throws Throwable {
    onodes = System.setProperty("min_nodes_for_cluster", "1");
    oslots = System.setProperty("total_slots_for_cluster", "20");
    final ClusterId clusterId = new ClusterId("test", "test-slot");
    Destination destination = new Destination() {
    };
    ApplicationDefinition app = new ApplicationDefinition(clusterId.getApplicationName());
    DecentralizedRoutingStrategy strategy = new DecentralizedRoutingStrategy(1, 1);
    app.setRoutingStrategy(strategy);
    app.setSerializer(new JavaSerializer<Object>());
    ClusterDefinition cd = new ClusterDefinition(clusterId.getMpClusterName());
    cd.setMessageProcessorPrototype(new GoodTestMp());
    app.add(cd);
    app.initialize();
    LocalClusterSessionFactory mpfactory = new LocalClusterSessionFactory();
    ClusterInfoSession session = mpfactory.createSession();
    TestUtils.createClusterLevel(clusterId, session);
    // fake the inbound side setup
    inbound = strategy.createInbound(session, clusterId, new Dempsy() {

        public List<Class<?>> gm(ClusterDefinition clusterDef) {
            return super.getAcceptedMessages(clusterDef);
        }
    }.gm(cd), destination, new RoutingStrategy.Inbound.KeyspaceResponsibilityChangeListener() {

        @Override
        public void keyspaceResponsibilityChanged(Inbound inbound, boolean less, boolean more) {
        }
    });
    routerFactory = new Router(app);
    routerFactory.setClusterSession(session);
    routerFactory.setCurrentCluster(clusterId);
    routerFactory.initialize();
}
Also used : Destination(com.nokia.dempsy.messagetransport.Destination) ClusterDefinition(com.nokia.dempsy.config.ClusterDefinition) ClusterId(com.nokia.dempsy.config.ClusterId) Dempsy(com.nokia.dempsy.Dempsy) LocalClusterSessionFactory(com.nokia.dempsy.cluster.invm.LocalClusterSessionFactory) ClusterRouter(com.nokia.dempsy.router.Router.ClusterRouter) Inbound(com.nokia.dempsy.router.RoutingStrategy.Inbound) ApplicationDefinition(com.nokia.dempsy.config.ApplicationDefinition) ClusterInfoSession(com.nokia.dempsy.cluster.ClusterInfoSession) List(java.util.List) Before(org.junit.Before)

Example 9 with ApplicationDefinition

use of com.nokia.dempsy.config.ApplicationDefinition in project Dempsy by Dempsy.

the class TestRouterInstantiation method testGetMessagesNester.

@Test
public void testGetMessagesNester() throws Throwable {
    ApplicationDefinition app = new ApplicationDefinition("test");
    Router router = new Router(app);
    List<Object> messages = new ArrayList<Object>();
    List<Object> parent = new ArrayList<Object>();
    List<Object> nested = new ArrayList<Object>();
    Object first = new Object();
    nested.add(first);
    parent.add(nested);
    router.getMessages(parent, messages);
    Assert.assertEquals(1, messages.size());
    Assert.assertSame(first, messages.get(0));
}
Also used : ApplicationDefinition(com.nokia.dempsy.config.ApplicationDefinition) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ApplicationDefinition (com.nokia.dempsy.config.ApplicationDefinition)9 Test (org.junit.Test)6 Dempsy (com.nokia.dempsy.Dempsy)4 ClusterId (com.nokia.dempsy.config.ClusterId)4 Condition (com.nokia.dempsy.TestUtils.Condition)3 ClusterDefinition (com.nokia.dempsy.config.ClusterDefinition)3 ArrayList (java.util.ArrayList)3 ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)2 MyRankMp (com.nokia.dempsy.cluster.zookeeper.FullApplication.MyRankMp)2 HashMap (java.util.HashMap)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 ClusterInfoException (com.nokia.dempsy.cluster.ClusterInfoException)1 ClusterInfoSessionFactory (com.nokia.dempsy.cluster.ClusterInfoSessionFactory)1 ClusterInfoWatcher (com.nokia.dempsy.cluster.ClusterInfoWatcher)1 LocalClusterSessionFactory (com.nokia.dempsy.cluster.invm.LocalClusterSessionFactory)1 MyMp (com.nokia.dempsy.cluster.zookeeper.FullApplication.MyMp)1 Destination (com.nokia.dempsy.messagetransport.Destination)1 TcpTransport (com.nokia.dempsy.messagetransport.tcp.TcpTransport)1 StatsCollector (com.nokia.dempsy.monitoring.StatsCollector)1 StatsCollectorFactoryCoda (com.nokia.dempsy.monitoring.coda.StatsCollectorFactoryCoda)1