List of usage examples for org.apache.zookeeper Watcher Watcher
Watcher
From source file:org.apache.nifi.controller.state.providers.zookeeper.ZooKeeperStateProvider.java
License:Apache License
synchronized ZooKeeper getZooKeeper() throws IOException { if (zooKeeper != null && !zooKeeper.getState().isAlive()) { invalidateClient();//from w ww. ja v a 2 s.c om } if (zooKeeper == null) { zooKeeper = new ZooKeeper(connectionString, timeoutMillis, new Watcher() { @Override public void process(WatchedEvent event) { } }); if (auth != null) { zooKeeper.addAuthInfo("digest", auth); } } return zooKeeper; }
From source file:org.apache.s4.benchmark.utils.Utils.java
License:Apache License
public static void watchAndSignalChildrenReachedCount(final String path, final CountDownLatch latch, final ZooKeeper zk, final int count) throws KeeperException, InterruptedException { List<String> children = zk.getChildren(path, new Watcher() { @Override//from w w w . j av a2 s. c om public void process(WatchedEvent event) { if (EventType.NodeChildrenChanged.equals(event.getType())) { try { if (count == zk.getChildren(path, false).size()) { latch.countDown(); } } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } latch.countDown(); } } }); if (children.size() == count) { latch.countDown(); } }
From source file:org.apache.s4.comm.test.TestTaskSetupApp.java
License:Apache License
public void testTaskSetup1() throws Exception { String address = "localhost:2181"; Watcher watcher = new Watcher() { @Override//from w w w. j av a 2 s .c o m public void process(WatchedEvent event) { } }; // setup ZooKeeper zk = new ZooKeeper(address, 30000, watcher); String root = "/tasksetup_app_test"; ZkTaskSetup zkSetup = new ZkTaskSetup(address, root, ClusterType.S4); Map<String, String> task1 = new HashMap<String, String>(); task1.put("name", "task-1"); Map<String, String> task2 = new HashMap<String, String>(); task2.put("name", "task-2"); String tasksListRoot = root + "/tasks"; zkSetup.cleanUp(); Stat exists = zk.exists(tasksListRoot, false); myassert(exists == null); Object[] data = new Object[] { task1, task2 }; zkSetup.setUpTasks(data); // verify that tasks are created exists = zk.exists(tasksListRoot, false); myassert(exists != null); List<String> children = zk.getChildren(tasksListRoot, false); myassert(children.size() == data.length); boolean[] matched = new boolean[data.length]; for (String child : children) { System.out.println(child); String childPath = tasksListRoot + "/" + child; Stat sTemp = zk.exists(childPath, false); byte[] tempData = zk.getData(tasksListRoot + "/" + child, false, sTemp); Map<String, Object> map = (Map<String, Object>) JSONUtil.getMapFromJson(new String(tempData)); // check if it matches any of the data for (int i = 0; i < data.length; i++) { Map<String, Object> newData = (Map<String, Object>) data[i]; if (!matched[i] && CommUtil.compareMaps(newData, map)) { matched[i] = true; break; } } } for (int i = 0; i < matched.length; i++) { myassert(matched[i]); } // try running again and make verify new node is not created Stat oldStat = zk.exists(tasksListRoot, false); System.out.println("oldStat=" + oldStat); zkSetup.setUpTasks(data); Stat newStat = zk.exists(tasksListRoot, false); System.out.println("newstat=" + newStat); myassert(oldStat.getMtime() == newStat.getMtime()); // make change to task config and try running again and verify new // config is uploaded oldStat = zk.exists(tasksListRoot, false); System.out.println("oldStat=" + oldStat.getVersion()); ((Map<String, String>) data[data.length - 1]).put("name", "changedname"); zkSetup.setUpTasks(data); newStat = zk.exists(tasksListRoot, false); System.out.println("newstat=" + newStat.getVersion()); System.out.println(); myassert(oldStat.getMtime() != newStat.getMtime()); // ensure version change is working zkSetup.setUpTasks("1.0.0.0", data); }
From source file:org.apache.s4.fixtures.CommTestUtils.java
License:Apache License
public static ZooKeeper createZkClient() throws IOException { final ZooKeeper zk = new ZooKeeper("localhost:" + ZK_PORT, 4000, new Watcher() { @Override//from w w w. ja v a 2 s .c om public void process(WatchedEvent event) { } }); return zk; }
From source file:org.apache.s4.fixtures.CommTestUtils.java
License:Apache License
public static void watchAndSignalCreation(String path, final CountDownLatch latch, final ZooKeeper zk, boolean deleteIfExists) throws KeeperException, InterruptedException { if (zk.exists(path, false) != null) { if (deleteIfExists) { zk.delete(path, -1);//w w w. ja v a 2 s . co m } else { latch.countDown(); } } zk.exists(path, new Watcher() { @Override public void process(WatchedEvent event) { if (EventType.NodeCreated.equals(event.getType())) { latch.countDown(); } } }); }
From source file:org.apache.s4.fixtures.CommTestUtils.java
License:Apache License
public static void watchAndSignalChangedChildren(String path, final CountDownLatch latch, final ZooKeeper zk) throws KeeperException, InterruptedException { zk.getChildren(path, new Watcher() { @Override/*from www . j a va2s . co m*/ public void process(WatchedEvent event) { if (EventType.NodeChildrenChanged.equals(event.getType())) { latch.countDown(); } } }); }
From source file:org.apache.solr.cloud.LeaderElector.java
License:Apache License
/** * Check if the candidate with the given n_* sequence number is the leader. * If it is, set the leaderId on the leader zk node. If it is not, start * watching the candidate that is in line before this one - if it goes down, check * if this candidate is the leader again. * * @param replacement has someone else been the leader already? *//*w ww . j a v a 2 s. c o m*/ private void checkIfIamLeader(final int seq, final ElectionContext context, boolean replacement) throws KeeperException, InterruptedException, IOException { // get all other numbers... final String holdElectionPath = context.electionPath + ELECTION_NODE; List<String> seqs = zkClient.getChildren(holdElectionPath, null, true); sortSeqs(seqs); List<Integer> intSeqs = getSeqs(seqs); if (intSeqs.size() == 0) { log.warn("Our node is no longer in line to be leader"); return; } if (seq <= intSeqs.get(0)) { // first we delete the node advertising the old leader in case the ephem is still there try { zkClient.delete(context.leaderPath, -1, true); } catch (Exception e) { // fine } runIamLeaderProcess(context, replacement); } else { // I am not the leader - watch the node below me int i = 1; for (; i < intSeqs.size(); i++) { int s = intSeqs.get(i); if (seq < s) { // we found who we come before - watch the guy in front break; } } int index = i - 2; if (index < 0) { log.warn("Our node is no longer in line to be leader"); return; } try { zkClient.getData(holdElectionPath + "/" + seqs.get(index), new Watcher() { @Override public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } // am I the next leader? try { checkIfIamLeader(seq, context, true); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); } catch (IOException e) { log.warn("", e); } catch (Exception e) { log.warn("", e); } } }, null, true); } catch (KeeperException.SessionExpiredException e) { throw e; } catch (KeeperException e) { log.warn("Failed setting watch", e); // we couldn't set our watch - the node before us may already be down? // we need to check if we are the leader again checkIfIamLeader(seq, context, true); } } }
From source file:org.apache.solr.cloud.ZkSolrClientTest.java
License:Apache License
public void testWatchChildren() throws Exception { String zkDir = dataDir.getAbsolutePath() + File.separator + "zookeeper/server1/data"; final AtomicInteger cnt = new AtomicInteger(); ZkTestServer server = new ZkTestServer(zkDir); server.run();//from www . j av a 2 s .c o m AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost()); Thread.sleep(400); AbstractZkTestCase.makeSolrZkNode(server.getZkHost()); final SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), AbstractZkTestCase.TIMEOUT); try { final CountDownLatch latch = new CountDownLatch(1); zkClient.makePath("/collections", true); zkClient.getChildren("/collections", new Watcher() { @Override public void process(WatchedEvent event) { if (DEBUG) { System.out.println("children changed"); } cnt.incrementAndGet(); // remake watch try { zkClient.getChildren("/collections", this, true); latch.countDown(); } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } } }, true); zkClient.makePath("/collections/collection99/shards", true); latch.await(); //wait until watch has been re-created zkClient.makePath("collections/collection99/config=collection1", true); zkClient.makePath("collections/collection99/config=collection3", true); zkClient.makePath("/collections/collection97/shards", true); if (DEBUG) { zkClient.printLayoutToStdOut(); } // pause for the watches to fire Thread.sleep(700); if (cnt.intValue() < 2) { Thread.sleep(4000); // wait a bit more } if (cnt.intValue() < 2) { Thread.sleep(4000); // wait a bit more } assertEquals(2, cnt.intValue()); } finally { if (zkClient != null) { zkClient.close(); } if (server != null) { server.shutdown(); } } }
From source file:org.apache.solr.codecs.onsql.ONSQLCodecFactory.java
License:Apache License
@Override public void inform(SolrCore core) { log.info("=>>>>>>>>>>>>>codecfactory inform was called"); final String dir = Util.tidyIndexDir(core.getIndexDir()); log.info("inform.dir=" + dir); //Util.printStackTrace(); final CloudDescriptor cldesc = core.getCoreDescriptor().getCloudDescriptor(); if (cldesc != null) { log.info("cloud config available"); ONSQLKVstoreHandler.getInstance().setAllowWriting(dir, cldesc.isLeader()); ONSQLKVstoreHandler.getInstance().setShardId(dir, cldesc.getShardId()); final SolrZkClient zkclient = core.getCoreDescriptor().getCoreContainer().getZkController() .getZkClient();// w w w . j av a 2 s . c om ZkSolrResourceLoader loader = (ZkSolrResourceLoader) core.getResourceLoader(); String zkconfigpath = loader.getCollectionZkPath(); try { // load NoSQL config from ZKregistry byte[] content = zkclient.getData(zkconfigpath + "/kvstore.properties", null, null, true); ByteArrayInputStream is = new ByteArrayInputStream(content); Properties kvstore_props = new Properties(); kvstore_props.load(is); is.close(); ONSQLKVstoreHandler.getInstance().setKVStore(dir, kvstore_props); // hook watcher to the cluster update zkclient.exists(ZkStateReader.CLUSTER_STATE, new Watcher() { @Override public void process(WatchedEvent event) { if (EventType.None.equals(event.getType())) return; log.info("got event type=" + event.getType()); try { final Watcher thisWatch = this; zkclient.exists(ZkStateReader.CLUSTER_STATE, thisWatch, true); //clstate.getReplica(arg0, arg1) ONSQLKVstoreHandler.getInstance().setAllowWriting(dir, cldesc.isLeader()); } catch (KeeperException e) { if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) { log.warn("we have been disconnected from registry"); // TODO add code for stopping all the jobs in case it was a network failure in the cluster return; } throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { log.error(" we have been interrupted", e); // Restore the interrupted status Thread.currentThread().interrupt(); return; } } }, true); } catch (IOException e) { log.error("error while reading key-value store properties", e); throw new IllegalStateException("error while reading key-value store properties"); } catch (KeeperException e) { log.error("we have been disconnected from Zookeeper registry", e); throw new IllegalStateException("error while reading key-value store properties"); } catch (InterruptedException e) { log.error(" we have been interrupted", e); // Restore the interrupted status Thread.currentThread().interrupt(); } } else { log.info("no cloud available, using local configs from filesystem, collection name=" + core.getName()); ONSQLKVstoreHandler.getInstance().setAllowWriting(dir, false); ONSQLKVstoreHandler.getInstance().setShardId(dir, "shard1"); Properties props = new Properties(); try { FileInputStream propstream = new FileInputStream( core.getResourceLoader().getConfigDir().concat("/kvstore.properties")); props.load(propstream); propstream.close(); } catch (FileNotFoundException fnex) { throw new IllegalStateException( "kvstore.properties file is missing or non-readable in core config directory"); } catch (IOException fnex) { throw new IllegalStateException( "kvstore.properties file is missing or non-readable in core config directory"); } ONSQLKVstoreHandler.getInstance().setKVStore(dir, props); } }
From source file:org.apache.solr.common.cloud.ZkStateReader.java
License:Apache License
public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException { // We need to fetch the current cluster state and the set of live nodes synchronized (getUpdateLock()) { cmdExecutor.ensureExists(CLUSTER_STATE, zkClient); cmdExecutor.ensureExists(ALIASES, zkClient); log.info("Updating cluster state from ZooKeeper... "); zkClient.exists(CLUSTER_STATE, new Watcher() { @Override/*from w w w . j a v a 2 s .co m*/ public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } log.info("A cluster state change: {}, has occurred - updating... (live nodes size: {})", (event), ZkStateReader.this.clusterState == null ? 0 : ZkStateReader.this.clusterState.getLiveNodes().size()); try { // delayed approach // 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, true); Set<String> ln = ZkStateReader.this.clusterState.getLiveNodes(); ClusterState clusterState = ClusterState.load(stat.getVersion(), data, ln); // update volatile ZkStateReader.this.clusterState = clusterState; } } 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(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); return; } } }, true); } synchronized (ZkStateReader.this.getUpdateLock()) { List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, new Watcher() { @Override public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } try { // delayed approach // ZkStateReader.this.updateClusterState(false, true); synchronized (ZkStateReader.this.getUpdateLock()) { List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this, true); 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; } } 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(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); return; } } }, true); Set<String> liveNodeSet = new HashSet<String>(); liveNodeSet.addAll(liveNodes); ClusterState clusterState = ClusterState.load(zkClient, liveNodeSet); this.clusterState = clusterState; zkClient.exists(ALIASES, new Watcher() { @Override public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } try { synchronized (ZkStateReader.this.getUpdateLock()) { log.info("Updating aliases... "); // remake watch final Watcher thisWatch = this; Stat stat = new Stat(); byte[] data = zkClient.getData(ALIASES, thisWatch, stat, true); Aliases aliases = ClusterState.load(data); ZkStateReader.this.aliases = aliases; } } 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(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); return; } } }, true); } updateAliases(); }