Search in sources :

Example 66 with Watcher

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

the class ClusterStateCacheManager method createClusterStateWatcher.

/**
	 * monitor to clusterstate.json
	 */
public void createClusterStateWatcher() throws Exception {
    byte[] datas = zkClient.getData(CLUSTER_STATE, 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(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 = clusterState.getLiveNodes();
                    ClusterState newClusterState = ClusterState.load(stat.getVersion(), data, liveNodesSet);
                    clusterState = newClusterState;
                    CacheOperation.INSTANCE.clearJRedisPool();
                    System.out.println("CLUSTER_STATE changed");
                    System.out.println(clusterState.toString());
                }
            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                    return;
                }
                // log.error("", e);
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
    }, null);
    Stat stat = new Stat();
    List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, null);
    Set<String> liveNodesSet = new HashSet<String>();
    liveNodesSet.addAll(liveNodes);
    clusterState = ClusterState.load(stat.getVersion(), datas, liveNodesSet);
}
Also used : ClusterState(com.newcosoft.zookeeper.ClusterState) HashSet(java.util.HashSet) Set(java.util.Set) Watcher(org.apache.zookeeper.Watcher) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Example 67 with Watcher

use of org.apache.zookeeper.Watcher in project yyl_example by Relucent.

the class Test2 method main.

public static void main(String[] args) {
    ZooKeeper zk = null;
    try {
        zk = new ZooKeeper("localhost:2181", 1000, new Watcher() {

            // 监控所有被触发的事件
            public void process(WatchedEvent event) {
                System.out.println("已经触发了" + event.getType() + "事件!");
            }
        });
        printChildren("/", zk);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (Exception e1) {
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) KeeperException(org.apache.zookeeper.KeeperException)

Example 68 with Watcher

use of org.apache.zookeeper.Watcher in project lucene-solr by apache.

the class ZkIndexSchemaReader method createSchemaWatcher.

public void createSchemaWatcher() {
    log.info("Creating ZooKeeper watch for the managed schema at " + managedSchemaPath);
    try {
        zkClient.exists(managedSchemaPath, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                if (ZkIndexSchemaReader.this.isRemoved) {
                    // the core for this reader has already been removed, don't process this event
                    return;
                }
                // session events are not change events, and do not remove the watcher
                if (Event.EventType.None.equals(event.getType())) {
                    return;
                }
                log.info("A schema change: {}, has occurred - updating schema from ZooKeeper ...", event);
                try {
                    updateSchema(this, -1);
                } 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 ZooKeeperException(ErrorCode.SERVER_ERROR, "", e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    log.warn("", e);
                }
            }
        }, true);
    } catch (KeeperException e) {
        final String msg = "Error creating ZooKeeper watch for the managed schema";
        log.error(msg, e);
        throw new ZooKeeperException(ErrorCode.SERVER_ERROR, msg, e);
    } catch (InterruptedException e) {
        // Restore the interrupted status
        Thread.currentThread().interrupt();
        log.warn("", e);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) Watcher(org.apache.zookeeper.Watcher) KeeperException(org.apache.zookeeper.KeeperException) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException)

Example 69 with Watcher

use of org.apache.zookeeper.Watcher in project lucene-solr by apache.

the class ZkSolrClientTest method testMultipleWatchesAsync.

public void testMultipleWatchesAsync() throws Exception {
    try (ZkConnection conn = new ZkConnection()) {
        final SolrZkClient zkClient = conn.getClient();
        zkClient.makePath("/collections", true);
        final int numColls = random().nextInt(100);
        final CountDownLatch latch = new CountDownLatch(numColls);
        for (int i = 1; i <= numColls; i++) {
            String collPath = "/collections/collection" + i;
            zkClient.makePath(collPath, true);
            zkClient.getChildren(collPath, new Watcher() {

                @Override
                public void process(WatchedEvent event) {
                    latch.countDown();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                }
            }, true);
        }
        for (int i = 1; i <= numColls; i++) {
            String shardsPath = "/collections/collection" + i + "/shards";
            zkClient.makePath(shardsPath, true);
        }
        assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Example 70 with Watcher

use of org.apache.zookeeper.Watcher in project XRTB by benmfaul.

the class ZTester method connect.

// host should be 127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002
public ZooKeeper connect(String host) throws Exception {
    zk = new ZooKeeper(host, 2101, new Watcher() {

        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                connSignal.countDown();
            }
        }
    });
    connSignal.await();
    return zk;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher)

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