Search in sources :

Example 6 with ClusterInfoException

use of com.nokia.dempsy.cluster.ClusterInfoException in project Dempsy by Dempsy.

the class LocalClusterSessionFactory method doormdir.

private static synchronized Pair<Entry, Entry> doormdir(String path) throws ClusterInfoException {
    Entry ths = entries.get(path);
    if (ths == null)
        throw new ClusterInfoException("rmdir of non existant node \"" + path + "\"");
    Entry parent = entries.get(parent(path));
    entries.remove(path);
    if (parent != null) {
        int lastSlash = path.lastIndexOf('/');
        parent.children.remove(path.substring(lastSlash + 1));
    }
    return new Pair<Entry, Entry>(ths, parent);
}
Also used : ClusterInfoException(com.nokia.dempsy.cluster.ClusterInfoException) Pair(com.nokia.dempsy.util.Pair)

Example 7 with ClusterInfoException

use of com.nokia.dempsy.cluster.ClusterInfoException in project Dempsy by Dempsy.

the class LocalClusterSessionFactory method doomkdir.

private static synchronized Pair<Entry, String> doomkdir(String path, Object data, DirMode mode) throws ClusterInfoException {
    if (oexists(path, null))
        return new Pair<Entry, String>(null, null);
    String parentPath = parent(path);
    Entry parent = entries.get(parentPath);
    if (parent == null) {
        throw new ClusterInfoException("No Parent for \"" + path + "\" which is expected to be \"" + parent(path) + "\"");
    }
    long seq = -1;
    if (mode.isSequential()) {
        AtomicLong cseq = parent.childSequences.get(path);
        if (cseq == null)
            parent.childSequences.put(path, cseq = new AtomicLong(0));
        seq = cseq.getAndIncrement();
    }
    String pathToUse = seq >= 0 ? (path + seq) : path;
    entries.put(pathToUse, new Entry(data));
    // find the relative path
    int lastSlash = pathToUse.lastIndexOf('/');
    parent.children.add(pathToUse.substring(lastSlash + 1));
    return new Pair<Entry, String>(parent, pathToUse);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterInfoException(com.nokia.dempsy.cluster.ClusterInfoException) SafeString(com.nokia.dempsy.internal.util.SafeString) Pair(com.nokia.dempsy.util.Pair)

Example 8 with ClusterInfoException

use of com.nokia.dempsy.cluster.ClusterInfoException in project Dempsy by Dempsy.

the class DecentralizedRoutingStrategy method fillMapFromActiveSlots.

/**
    * Fill the map of slots to slotinfos for internal use. 
    * @return the totalAddressCount from each slot. These are supposed to be repeated.
    */
private static int fillMapFromActiveSlots(Map<Integer, DefaultRouterSlotInfo> mapToFill, ClusterInfoSession session, ClusterId clusterId, ClusterInfoWatcher watcher) throws ClusterInfoException {
    int totalAddressCounts = -1;
    Collection<String> slotsFromClusterManager;
    try {
        slotsFromClusterManager = session.getSubdirs(clusterId.asPath(), watcher);
    } catch (ClusterInfoException.NoNodeException e) {
        // mkdir and retry
        session.mkdir("/" + clusterId.getApplicationName(), null, DirMode.PERSISTENT);
        session.mkdir(clusterId.asPath(), null, DirMode.PERSISTENT);
        slotsFromClusterManager = session.getSubdirs(clusterId.asPath(), watcher);
    }
    if (slotsFromClusterManager != null) {
        // going to enter into the loop below.
        if (slotsFromClusterManager.size() == 0)
            totalAddressCounts = 0;
        for (String node : slotsFromClusterManager) {
            DefaultRouterSlotInfo slotInfo = (DefaultRouterSlotInfo) session.getData(clusterId.asPath() + "/" + node, null);
            if (slotInfo != null) {
                mapToFill.put(slotInfo.getSlotIndex(), slotInfo);
                if (totalAddressCounts == -1)
                    totalAddressCounts = slotInfo.getTotalAddress();
                else if (totalAddressCounts != slotInfo.getTotalAddress())
                    logger.error("There is a problem with the slots taken by the cluster manager for the cluster " + clusterId + ". Slot " + slotInfo.getSlotIndex() + " from " + SafeString.objectDescription(slotInfo.getDestination()) + " thinks the total number of slots for this cluster it " + slotInfo.getTotalAddress() + " but a former slot said the total was " + totalAddressCounts);
            } else
                throw new ClusterInfoException("There is an empty shard directory at " + clusterId.asPath() + "/" + node + " which ought to be impossible!");
        }
    }
    return totalAddressCounts;
}
Also used : ClusterInfoException(com.nokia.dempsy.cluster.ClusterInfoException) SafeString(com.nokia.dempsy.internal.util.SafeString)

Example 9 with ClusterInfoException

use of com.nokia.dempsy.cluster.ClusterInfoException in project Dempsy by Dempsy.

the class ZookeeperSession method callZookeeper.

private Object callZookeeper(final String name, final String path, final ClusterInfoWatcher watcher, final Object userdata, final ZookeeperCall callee) throws ClusterInfoException {
    if (isRunning) {
        final WatcherProxy wp = watcher != null ? makeWatcherProxy(watcher) : null;
        if (wp != null) {
            synchronized (registeredWatchers) {
                registeredWatchers.add(wp);
            }
        }
        final ZooKeeper cur = zkref.get();
        try {
            return callee.call(cur, path, wp, userdata);
        } catch (final KeeperException.NodeExistsException e) {
            if (logger.isTraceEnabled())
                logger.trace("Failed call to " + name + " at " + path + " because the node already exists.", e);
            else if (logger.isDebugEnabled())
                logger.debug("Failed call to " + name + " at " + path + " because the node already exists.");
            // this is only thrown from mkdir and so if the Node Exists
            return null;
        // we simply want to return a null String
        } catch (final KeeperException.NoNodeException e) {
            throw new ClusterInfoException.NoNodeException("Node doesn't exist at " + path + " while running " + name, e);
        } catch (final KeeperException e) {
            resetZookeeper(cur);
            throw new ClusterInfoException("Zookeeper failed while trying to " + name + " at " + path, e);
        } catch (final InterruptedException e) {
            throw new ClusterInfoException("Interrupted while trying to " + name + " at " + path, e);
        } catch (final SerializationException e) {
            throw new ClusterInfoException("Failed to deserialize the object durring a " + name + " call at " + path, e);
        }
    }
    throw new ClusterInfoException(name + " called on stopped ZookeeperSession.");
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) SerializationException(com.nokia.dempsy.serialization.SerializationException) ClusterInfoException(com.nokia.dempsy.cluster.ClusterInfoException) KeeperException(org.apache.zookeeper.KeeperException)

Example 10 with ClusterInfoException

use of com.nokia.dempsy.cluster.ClusterInfoException 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)

Aggregations

ClusterInfoException (com.nokia.dempsy.cluster.ClusterInfoException)13 Condition (com.nokia.dempsy.TestUtils.Condition)8 ClusterId (com.nokia.dempsy.config.ClusterId)8 Test (org.junit.Test)8 ZooKeeper (org.apache.zookeeper.ZooKeeper)5 ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)4 Dempsy (com.nokia.dempsy.Dempsy)3 MyMp (com.nokia.dempsy.cluster.zookeeper.FullApplication.MyMp)2 SafeString (com.nokia.dempsy.internal.util.SafeString)2 StatsCollector (com.nokia.dempsy.monitoring.StatsCollector)2 Pair (com.nokia.dempsy.util.Pair)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 ClusterInfoSessionFactory (com.nokia.dempsy.cluster.ClusterInfoSessionFactory)1 ClusterInfoWatcher (com.nokia.dempsy.cluster.ClusterInfoWatcher)1 DisruptibleSession (com.nokia.dempsy.cluster.DisruptibleSession)1 ApplicationDefinition (com.nokia.dempsy.config.ApplicationDefinition)1 DefaultRouterSlotInfo (com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo)1 SerializationException (com.nokia.dempsy.serialization.SerializationException)1 IOException (java.io.IOException)1