Search in sources :

Example 1 with ClusterDefinition

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

the class TestFullApp method testSeparateClustersInOneVm.

@Test
public void testSeparateClustersInOneVm() throws Throwable {
    // now start each cluster
    ctx[0] = "fullApp/Dempsy-FullUp.xml";
    Map<ClusterId, DempsyHolder> dempsys = new HashMap<ClusterId, 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;
            }
        }));
    } finally {
        ctx[0] = dempsyConfig;
        for (DempsyHolder cur : dempsys.values()) {
            cur.dempsy.stop();
            cur.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) 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 2 with ClusterDefinition

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

the class Router method registerOutbound.

@Override
public void registerOutbound(RoutingStrategy.Outbound outbound, Collection<Class<?>> classes) {
    synchronized (outbound) {
        unregisterOutbound(outbound);
        ClusterId clusterId = outbound.getClusterId();
        if (classes != null && classes.size() > 0) {
            // find the appropriate ClusterDefinition
            ClusterDefinition curClusterDef = applicationDefinition.getClusterDefinition(clusterId);
            if (curClusterDef != null) {
                // create a corresponding ClusterRouter
                @SuppressWarnings("unchecked") ClusterRouter clusterRouter = new ClusterRouter((Serializer<Object>) curClusterDef.getSerializer(), outbound);
                for (Class<?> clazz : classes) {
                    // potential
                    Set<ClusterRouter> cur = Collections.newSetFromMap(new ConcurrentHashMap<ClusterRouter, Boolean>());
                    Set<ClusterRouter> tmp = routerMap.putIfAbsent(clazz, cur);
                    if (tmp != null)
                        cur = tmp;
                    cur.add(clusterRouter);
                }
            } else {
                logger.error("Couldn't find the ClusterDefinition for " + clusterId + " while registering the Outbound " + SafeString.objectDescription(outbound) + " given the ApplicationDefinition " + applicationDefinition);
            }
        }
    }
}
Also used : ClusterDefinition(com.nokia.dempsy.config.ClusterDefinition) ClusterId(com.nokia.dempsy.config.ClusterId)

Example 3 with ClusterDefinition

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

the class Router method initialize.

/**
    * Prior to the {@link Router} being used it needs to be initialized.
    */
public void initialize() throws ClusterInfoException, DempsyException {
    // applicationDefinition cannot be null because the constructor checks
    // put all of the cluster definitions into a map for easy lookup
    Map<ClusterId, ClusterDefinition> defs = new HashMap<ClusterId, ClusterDefinition>();
    for (ClusterDefinition clusterDef : applicationDefinition.getClusterDefinitions()) defs.put(clusterDef.getClusterId(), clusterDef);
    // now see about the one that we are.
    ClusterDefinition currentClusterDef = null;
    if (currentCluster != null) {
        currentClusterDef = defs.get(currentCluster);
        if (currentClusterDef == null)
            throw new DempsyException("This Dempsy instance seems to be misconfigured. While this VM thinks it's an instance of " + currentCluster + " the application it's configured with doesn't contain this cluster definition. The application configuration consists of: " + applicationDefinition);
    }
    // get the set of explicit destinations if they exist
    Set<ClusterId> explicitClusterDestinations = (currentClusterDef != null && currentClusterDef.hasExplicitDestinations()) ? new HashSet<ClusterId>() : null;
    if (explicitClusterDestinations != null)
        explicitClusterDestinations.addAll(Arrays.asList(currentClusterDef.getDestinations()));
    //  then those are the only ones we want to consider
    for (ClusterDefinition clusterDef : applicationDefinition.getClusterDefinitions()) {
        if ((explicitClusterDestinations == null || explicitClusterDestinations.contains(clusterDef.getClusterId())) && !clusterDef.isRouteAdaptorType()) {
            RoutingStrategy strategy = (RoutingStrategy) clusterDef.getRoutingStrategy();
            ClusterId clusterId = clusterDef.getClusterId();
            if (strategy == null)
                throw new DempsyException("Could not retrieve the routing strategy for " + SafeString.valueOf(clusterId));
            // This create will result in a callback on the Router as the Outbound.Coordinator with a 
            // registration event. The Outbound may (will) call back on the Router to retrieve the 
            // MpClusterSession and register itself with the appropriate cluster.
            outbounds.add(strategy.createOutbound(this, mpClusterSession, clusterId));
        }
    }
//-------------------------------------------------------------------------------------
}
Also used : ClusterDefinition(com.nokia.dempsy.config.ClusterDefinition) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterId(com.nokia.dempsy.config.ClusterId) DempsyException(com.nokia.dempsy.DempsyException)

Example 4 with ClusterDefinition

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

use of com.nokia.dempsy.config.ClusterDefinition 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)

Aggregations

ClusterDefinition (com.nokia.dempsy.config.ClusterDefinition)5 ClusterId (com.nokia.dempsy.config.ClusterId)5 ApplicationDefinition (com.nokia.dempsy.config.ApplicationDefinition)3 HashMap (java.util.HashMap)3 Dempsy (com.nokia.dempsy.Dempsy)2 Condition (com.nokia.dempsy.TestUtils.Condition)2 MyRankMp (com.nokia.dempsy.cluster.zookeeper.FullApplication.MyRankMp)2 Test (org.junit.Test)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 DempsyException (com.nokia.dempsy.DempsyException)1 ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)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 StatsCollector (com.nokia.dempsy.monitoring.StatsCollector)1 ClusterRouter (com.nokia.dempsy.router.Router.ClusterRouter)1 Inbound (com.nokia.dempsy.router.RoutingStrategy.Inbound)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Before (org.junit.Before)1