Search in sources :

Example 61 with Watcher

use of org.apache.zookeeper.Watcher in project distributedlog by twitter.

the class TestZooKeeperClient method awaitConnectionEvent.

private CountDownLatch awaitConnectionEvent(final KeeperState state, final ZooKeeperClient zkc) {
    final CountDownLatch connected = new CountDownLatch(1);
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == state) {
                connected.countDown();
            }
        }
    };
    zkc.register(watcher);
    return connected;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 62 with Watcher

use of org.apache.zookeeper.Watcher in project distributedlog by twitter.

the class ZooKeeperClientUtils method expireSession.

/**
     * Expire given zookeeper client's session.
     *
     * @param zkc
     *          zookeeper client
     * @param zkServers
     *          zookeeper servers
     * @param timeout
     *          timeout
     * @throws Exception
     */
public static void expireSession(ZooKeeperClient zkc, String zkServers, int timeout) throws Exception {
    final CountDownLatch expireLatch = new CountDownLatch(1);
    final CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper oldZk = zkc.get();
    oldZk.exists("/", new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            logger.debug("Receive event : {}", event);
            if (event.getType() == Event.EventType.None && event.getState() == Event.KeeperState.Expired) {
                expireLatch.countDown();
            }
        }
    });
    ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (Event.EventType.None == event.getType() && Event.KeeperState.SyncConnected == event.getState()) {
                latch.countDown();
            }
        }
    }, oldZk.getSessionId(), oldZk.getSessionPasswd());
    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }
    newZk.close();
    boolean done = false;
    Stopwatch expireWait = Stopwatch.createStarted();
    while (!done && expireWait.elapsed(TimeUnit.MILLISECONDS) < timeout * 2) {
        try {
            zkc.get().exists("/", false);
            done = true;
        } catch (KeeperException ke) {
            done = (ke.code() == KeeperException.Code.SESSIONEXPIRED);
        }
    }
    assertTrue("Client should receive session expired event.", expireLatch.await(timeout, TimeUnit.MILLISECONDS));
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stopwatch(com.google.common.base.Stopwatch) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException)

Example 63 with Watcher

use of org.apache.zookeeper.Watcher 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 64 with Watcher

use of org.apache.zookeeper.Watcher in project disgear by yangbutao.

the class ZkStateReader method createClusterStateWatchersAndUpdate.

public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException {
    // We need to fetch the current cluster state and the set of live nodes
    synchronized (getUpdateLock()) {
        new ZookeeperClient(zkClient).ensureExists(CLUSTER_STATE, null, CreateMode.PERSISTENT);
        new ZookeeperClient(zkClient).ensureExists(ALIASES, null, CreateMode.PERSISTENT);
        log.info("Updating cluster state from ZooKeeper... ");
        zkClient.exists(CLUSTER_STATE, new Watcher() {

            public void process(WatchedEvent event) {
                // and do not remove the watcher
                if (EventType.None.equals(event.getType())) {
                    return;
                }
                try {
                    // ZkStateReader.this.updateClusterState(false, false);
                    synchronized (ZkStateReader.this.getUpdateLock()) {
                        // remake watch
                        final Watcher thisWatch = this;
                        Stat stat = new Stat();
                        byte[] data = zkClient.getData(CLUSTER_STATE, thisWatch, stat);
                        List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this);
                        Set<String> liveNodesSet = new HashSet<String>();
                        liveNodesSet.addAll(liveNodes);
                        Set<String> ln = ZkStateReader.this.clusterState.getLiveNodes();
                        ClusterState clusterState = ClusterState.load(stat.getVersion(), data, liveNodesSet);
                        // if leader is changed��then change current node slave status to sync from new leader
                        String currNodeName = ZkStateReader.this.zkController.getNodeName();
                        Replica leader = clusterState.getLeader(ZkStateReader.this.zkController.getCollectionName(), ZkStateReader.this.zkController.getShardName());
                        String leaderNodeName = leader == null ? null : leader.getNodeName();
                        Replica oldLeader = ZkStateReader.this.clusterState.getLeader(ZkStateReader.this.zkController.getCollectionName(), ZkStateReader.this.zkController.getShardName());
                        String oldLeaderNodeName = oldLeader == null ? null : oldLeader.getNodeName();
                        BaseElectionContext electContext = ZkStateReader.this.zkController.getElectionContexts().get(currNodeName);
                        if (electContext != null && electContext.leaderPath != null) {
                            if (zkClient.exists(electContext.leaderPath, null) != null) {
                                byte[] data2 = zkClient.getData(electContext.leaderPath, null, stat);
                                Map<String, Object> stateMap2 = (Map<String, Object>) ZkStateReader.fromJSON(data2);
                                if (!currNodeName.equals(leaderNodeName) && (leaderNodeName != null & !leaderNodeName.equals(oldLeaderNodeName)) && liveNodes.contains(leaderNodeName)) {
                                    // current node is slave
                                    String base_url = (String) stateMap2.get("base_url");
                                    if (base_url != null) {
                                        String[] args = base_url.split(":");
                                        ShellExec.runExec(zkController.getScriptPath() + File.separator + "redis_slaves.sh " + args[0] + " " + args[1]);
                                    }
                                }
                            }
                        }
                        // update volatile
                        ZkStateReader.this.clusterState = clusterState;
                        System.out.println(clusterState.toString());
                    }
                } catch (KeeperException e) {
                    if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                        log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK");
                        return;
                    }
                    log.error("", e);
                    throw new RuntimeException(e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    log.warn("", e);
                    return;
                }
            }
        });
    }
    synchronized (ZkStateReader.this.getUpdateLock()) {
        List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, new Watcher() {

            public void process(WatchedEvent event) {
                // and do not remove the watcher
                if (EventType.None.equals(event.getType())) {
                    return;
                }
                try {
                    // true);
                    synchronized (ZkStateReader.this.getUpdateLock()) {
                        List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this);
                        log.info("Updating live nodes... ({})", liveNodes.size());
                        Set<String> liveNodesSet = new HashSet<String>();
                        liveNodesSet.addAll(liveNodes);
                        ClusterState clusterState = new ClusterState(ZkStateReader.this.clusterState.getZkClusterStateVersion(), liveNodesSet, ZkStateReader.this.clusterState.getCollectionStates());
                        ZkStateReader.this.clusterState = clusterState;
                        System.out.println(clusterState.toString());
                    }
                } catch (KeeperException e) {
                    if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                        log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK");
                        return;
                    }
                    log.error("", e);
                    throw new RuntimeException(e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    log.warn("", e);
                    return;
                }
            }
        });
        Set<String> liveNodeSet = new HashSet<String>();
        liveNodeSet.addAll(liveNodes);
        ClusterState clusterState = ClusterState.load(zkClient, liveNodeSet);
        this.clusterState = clusterState;
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Watcher(org.apache.zookeeper.Watcher) Replica(com.newcosoft.cache.Replica) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Example 65 with Watcher

use of org.apache.zookeeper.Watcher in project disgear by yangbutao.

the class ZookeeperController method setNode2SlaveStatus.

public void setNode2SlaveStatus() throws Exception {
    byte[] initData = zkClient.getData(electionContexts.get(nodeName).leaderPath, new Watcher() {

        public void process(WatchedEvent event) {
            if (EventType.None.equals(event.getType())) {
                return;
            }
            try {
                synchronized (this) {
                    final Watcher thisWatch = this;
                    Stat stat = new Stat();
                    byte[] data = zkClient.getData(electionContexts.get(nodeName).leaderPath, thisWatch, stat);
                    Map<String, Object> stateMap = (Map<String, Object>) ZkStateReader.fromJSON(data);
                    // [core=col1, node_name=node1,
                    // base_url=10.1.1.26:6379]
                    System.out.print(stateMap);
                    // compare with old leader
                    String leaderNodeName = (String) stateMap.get("node_name");
                    String nodeName = getNodeName();
                    if (!nodeName.equals(leaderNodeName)) {
                        // set current mode to slave mode
                        String base_url = (String) stateMap.get("base_url");
                        String[] args = base_url.split(":");
                        ShellExec.runExec(scriptPath + File.separator + "redis_slaves.sh " + args[0] + " " + args[1]);
                    }
                }
            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                    log.warn("ZooKeeper watch triggered, but agent cannot talk to ZK");
                    return;
                }
                log.error("", e);
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.warn("", e);
                return;
            }
        }
    }, null);
    Map<String, Object> stateMap = (Map<String, Object>) ZkStateReader.fromJSON(initData);
    // [core=col1, node_name=node1, base_url=10.1.1.26:6379]
    System.out.print(stateMap);
    String leaderNodeName = (String) stateMap.get("node_name");
    String nodeName = getNodeName();
    if (!nodeName.equals(leaderNodeName)) {
        // set current  node to slave mode
        String base_url = (String) stateMap.get("base_url");
        String[] args = base_url.split(":");
        ShellExec.runExec(scriptPath + File.separator + "redis_slaves.sh " + args[0] + " " + args[1]);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

Watcher (org.apache.zookeeper.Watcher)78 WatchedEvent (org.apache.zookeeper.WatchedEvent)62 KeeperException (org.apache.zookeeper.KeeperException)34 CountDownLatch (java.util.concurrent.CountDownLatch)26 ZooKeeper (org.apache.zookeeper.ZooKeeper)24 Stat (org.apache.zookeeper.data.Stat)21 Test (org.junit.Test)18 IOException (java.io.IOException)11 AsyncCallback (org.apache.zookeeper.AsyncCallback)10 List (java.util.List)8 Test (org.testng.annotations.Test)8 None (com.linkedin.common.util.None)7 HashSet (java.util.HashSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Map (java.util.Map)5 Set (java.util.Set)5 HashMap (java.util.HashMap)4 TimeoutException (java.util.concurrent.TimeoutException)4 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3