org.apache.zookeeper.ZooKeeper Java Examples
The following examples show how to use
org.apache.zookeeper.ZooKeeper.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestEnsurePath.java From xian with Apache License 2.0 | 6 votes |
@Test public void testBasic() throws Exception { ZooKeeper client = mock(ZooKeeper.class, Mockito.RETURNS_MOCKS); CuratorZookeeperClient curator = mock(CuratorZookeeperClient.class); RetryPolicy retryPolicy = new RetryOneTime(1); RetryLoop retryLoop = new RetryLoop(retryPolicy, null); when(curator.getZooKeeper()).thenReturn(client); when(curator.getRetryPolicy()).thenReturn(retryPolicy); when(curator.newRetryLoop()).thenReturn(retryLoop); Stat fakeStat = mock(Stat.class); when(client.exists(Mockito.<String>any(), anyBoolean())).thenReturn(fakeStat); EnsurePath ensurePath = new EnsurePath("/one/two/three"); ensurePath.ensure(curator); verify(client, times(3)).exists(Mockito.<String>any(), anyBoolean()); ensurePath.ensure(curator); verifyNoMoreInteractions(client); ensurePath.ensure(curator); verifyNoMoreInteractions(client); }
Example #2
Source File: ServiceDiscovery.java From rpc4j with MIT License | 6 votes |
private void watchNode(final ZooKeeper zk) { try { List<String> nodeList = zk.getChildren(Constant.ZK_REGISTRY_PATH, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged) { watchNode(zk); } } }); List<String> dataList = new ArrayList<>(); for (String node : nodeList) { byte[] bytes = zk.getData(Constant.ZK_REGISTRY_PATH + "/" + node, false, null); dataList.add(new String(bytes)); } Logger.info("node data: {}", dataList); this.dataList = dataList; } catch (KeeperException | InterruptedException e) { Logger.error("", e); } }
Example #3
Source File: ZKTools.java From uncode-schedule with Apache License 2.0 | 6 votes |
public static String[] getSortedTree(ZooKeeper zk, String path) throws Exception { if (zk.exists(path, false) == null) { return new String[0]; } List<String> dealList = new ArrayList<String>(); dealList.add(path); int index = 0; while (index < dealList.size()) { String tempPath = dealList.get(index); List<String> children = zk.getChildren(tempPath, false); if (tempPath.equalsIgnoreCase("/") == false) { tempPath = tempPath + "/"; } Collections.sort(children); for (int i = children.size() - 1; i >= 0; i--) { dealList.add(index + 1, tempPath + children.get(i)); } index++; } return (String[]) dealList.toArray(new String[0]); }
Example #4
Source File: ZKTestEnv.java From herddb with Apache License 2.0 | 6 votes |
public ZKTestEnv(Path path) throws Exception { zkServer = new TestingServer(1282, path.toFile(), true); // waiting for ZK to be reachable CountDownLatch latch = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper(zkServer.getConnectString(), herddb.server.ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT, (WatchedEvent event) -> { LOG.log(Level.INFO, "ZK EVENT {0}", event); if (event.getState() == KeeperState.SyncConnected) { latch.countDown(); } }); try { if (!latch.await(herddb.server.ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT, TimeUnit.MILLISECONDS)) { LOG.log(Level.INFO, "ZK client did not connect withing {0} seconds, maybe the server did not start up", herddb.server.ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT); } } finally { zk.close(1000); } this.path = path; }
Example #5
Source File: ZookeeperDriverImpl.java From disconf with Apache License 2.0 | 6 votes |
/** * 广度搜索法:搜索分布式配置对应的两层数据 * * @return * * @throws InterruptedException * @throws KeeperException */ private Map<String, ZkDisconfData> getDisconfData(String path) throws KeeperException, InterruptedException { Map<String, ZkDisconfData> ret = new HashMap<String, ZkDisconfData>(); ZookeeperMgr zooKeeperMgr = ZookeeperMgr.getInstance(); ZooKeeper zooKeeper = zooKeeperMgr.getZk(); if (zooKeeper.exists(path, false) == null) { return ret; } List<String> children = zooKeeper.getChildren(path, false); for (String firstKey : children) { ZkDisconfData zkDisconfData = getDisconfData(path, firstKey, zooKeeper); if (zkDisconfData != null) { ret.put(firstKey, zkDisconfData); } } return ret; }
Example #6
Source File: AccumuloMiniClusterDriver.java From accumulo-recipes with Apache License 2.0 | 6 votes |
private Boolean isZookeeperRunning(String host, int timeout) { final CountDownLatch connectedSignal = new CountDownLatch(1); try { new ZooKeeper(host, timeout, new Watcher() { public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { connectedSignal.countDown(); } } }); return connectedSignal.await(timeout,TimeUnit.MILLISECONDS); } catch (Throwable e) { return Boolean.FALSE; } }
Example #7
Source File: StoreClientFactory.java From pravega with Apache License 2.0 | 6 votes |
@Override @Synchronized public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception { // prevent creating a new client, stick to the same client created earlier // this trick prevents curator from re-creating ZK client on session expiry if (client == null) { Exceptions.checkNotNullOrEmpty(connectString, "connectString"); Preconditions.checkArgument(sessionTimeout > 0, "sessionTimeout should be a positive integer"); this.connectString = connectString; this.sessionTimeout = sessionTimeout; this.canBeReadOnly = canBeReadOnly; this.client = new ZooKeeper(connectString, sessionTimeout, watcher, canBeReadOnly); } else { try { Preconditions.checkArgument(this.connectString.equals(connectString), "connectString differs"); Preconditions.checkArgument(this.sessionTimeout == sessionTimeout, "sessionTimeout differs"); Preconditions.checkArgument(this.canBeReadOnly == canBeReadOnly, "canBeReadOnly differs"); this.client.register(watcher); } catch (IllegalArgumentException e) { log.warn("Input argument for new ZooKeeper client ({}, {}, {}) changed with respect to existing client ({}, {}, {}).", connectString, sessionTimeout, canBeReadOnly, this.connectString, this.sessionTimeout, this.canBeReadOnly); closeClient(client); } } return this.client; }
Example #8
Source File: ZookeeperResource.java From spring-zookeeper with Apache License 2.0 | 6 votes |
private void getDataInputStream(String currentZnode, List<InputStream> seqIsCollector, boolean regressionZnodes) throws KeeperException, InterruptedException { ZooKeeper zk = getZk(); seqIsCollector.add(new ByteArrayInputStream(zk.getData(currentZnode, true, null))); // need add return between every stream otherwise top/last line will be // join to one line. seqIsCollector.add(new ByteArrayInputStream("\n".getBytes())); if (regressionZnodes) { List<String> children = zk.getChildren(currentZnode, true); if (children != null) { for (String child : children) { String childZnode = currentZnode + "/" + child; getDataInputStream(childZnode, seqIsCollector, regressionZnodes); } } } }
Example #9
Source File: ZKManager.java From stategen with GNU Affero General Public License v3.0 | 6 votes |
public static void checkParent(ZooKeeper zk, String path) throws Exception { String[] list = path.split("/"); String zkPath = ""; for (int i =0;i< list.length -1;i++){ String str = list[i]; if (str.equals("") == false) { zkPath = zkPath + "/" + str; if (zk.exists(zkPath, false) != null) { byte[] value = zk.getData(zkPath, false, null); if(value != null){ String tmpVersion = new String(value); if(tmpVersion.indexOf("taobao-pamirs-schedule-") >=0){ throw new Exception("\"" + zkPath +"\" is already a schedule instance's root directory, its any subdirectory cannot as the root directory of others"); } } } } } }
Example #10
Source File: Backup.java From zoocreeper with Apache License 2.0 | 6 votes |
public void backup(OutputStream os) throws InterruptedException, IOException, KeeperException { JsonGenerator jgen = null; ZooKeeper zk = null; try { zk = options.createZooKeeper(LOGGER); jgen = JSON_FACTORY.createGenerator(os); if (options.prettyPrint) { jgen.setPrettyPrinter(new DefaultPrettyPrinter()); } jgen.writeStartObject(); if (zk.exists(options.rootPath, false) == null) { LOGGER.warn("Root path not found: {}", options.rootPath); } else { doBackup(zk, jgen, options.rootPath); } jgen.writeEndObject(); } finally { if (jgen != null) { jgen.close(); } if (zk != null) { zk.close(); } } }
Example #11
Source File: ZooKeeperMigrator.java From localization_nifi with Apache License 2.0 | 5 votes |
private void closeZooKeeper(ZooKeeper zooKeeper) { try { zooKeeper.close(); } catch (InterruptedException e) { LOGGER.warn("could not close ZooKeeper client due to interrupt", e); Thread.currentThread().interrupt(); } }
Example #12
Source File: ZookeeperClientFactoryImplTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testZKCreationRW() throws Exception { ZooKeeperClientFactory zkf = new ZookeeperClientFactoryImpl(); CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + localZkS.getZookeeperPort(), SessionType.ReadWrite, (int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS); localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); assertTrue(localZkc.getState().isConnected()); assertNotEquals(localZkc.getState(), States.CONNECTEDREADONLY); localZkc.close(); }
Example #13
Source File: MasterBasedDistributedLayoutFactoryTest.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private void rmr(ZooKeeper zooKeeper, String storagePath) throws KeeperException, InterruptedException { Stat stat = zooKeeper.exists(storagePath, false); if (stat == null) { return; } List<String> children = zooKeeper.getChildren(storagePath, false); for (String s : children) { rmr(zooKeeper, storagePath + "/" + s); } zooKeeper.delete(storagePath, -1); }
Example #14
Source File: ZKUtil.java From hbase with Apache License 2.0 | 5 votes |
/** * Waits for HBase installation's base (parent) znode to become available. * @throws IOException on ZK errors */ public static void waitForBaseZNode(Configuration conf) throws IOException { LOG.info("Waiting until the base znode is available"); String parentZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT); ZooKeeper zk = new ZooKeeper(ZKConfig.getZKQuorumServersString(conf), conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT), EmptyWatcher.instance); final int maxTimeMs = 10000; final int maxNumAttempts = maxTimeMs / HConstants.SOCKET_RETRY_WAIT_MS; KeeperException keeperEx = null; try { try { for (int attempt = 0; attempt < maxNumAttempts; ++attempt) { try { if (zk.exists(parentZNode, false) != null) { LOG.info("Parent znode exists: {}", parentZNode); keeperEx = null; break; } } catch (KeeperException e) { keeperEx = e; } Threads.sleepWithoutInterrupt(HConstants.SOCKET_RETRY_WAIT_MS); } } finally { zk.close(); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } if (keeperEx != null) { throw new IOException(keeperEx); } }
Example #15
Source File: ZookeeperDriverImpl.java From disconf with Apache License 2.0 | 5 votes |
/** * 获取指定 配置数据 * * @return * * @throws InterruptedException * @throws KeeperException */ private ZkDisconfData getDisconfData(String path, String keyName, ZooKeeper zooKeeper) throws KeeperException, InterruptedException { String curPath = path + "/" + keyName; if (zooKeeper.exists(curPath, false) == null) { return null; } ZkDisconfData zkDisconfData = new ZkDisconfData(); zkDisconfData.setKey(keyName); List<String> secChiList = zooKeeper.getChildren(curPath, false); List<ZkDisconfData.ZkDisconfDataItem> zkDisconfDataItems = new ArrayList<ZkDisconfData.ZkDisconfDataItem>(); // list for (String secKey : secChiList) { // machine ZkDisconfData.ZkDisconfDataItem zkDisconfDataItem = new ZkDisconfData.ZkDisconfDataItem(); zkDisconfDataItem.setMachine(secKey); String thirdPath = curPath + "/" + secKey; // value byte[] data = zooKeeper.getData(thirdPath, null, null); if (data != null) { zkDisconfDataItem.setValue(new String(data, CHARSET)); } // add zkDisconfDataItems.add(zkDisconfDataItem); } zkDisconfData.setData(zkDisconfDataItems); return zkDisconfData; }
Example #16
Source File: Backup.java From zoocreeper with Apache License 2.0 | 5 votes |
private void doBackup(ZooKeeper zk, JsonGenerator jgen, String path) throws KeeperException, InterruptedException, IOException { try { final Stat stat = new Stat(); List<ACL> acls = nullToEmpty(zk.getACL(path, stat)); if (stat.getEphemeralOwner() != 0 && !options.backupEphemeral) { LOGGER.debug("Skipping ephemeral node: {}", path); return; } final Stat dataStat = new Stat(); byte[] data = zk.getData(path, false, dataStat); for (int i = 0; stat.compareTo(dataStat) != 0 && i < options.numRetries; i++) { LOGGER.warn("Retrying getACL / getData to read consistent state"); acls = zk.getACL(path, stat); data = zk.getData(path, false, dataStat); } if (stat.compareTo(dataStat) != 0) { throw new IllegalStateException("Unable to read consistent data for znode: " + path); } LOGGER.debug("Backing up node: {}", path); dumpNode(jgen, path, stat, acls, data); final List<String> childPaths = nullToEmpty(zk.getChildren(path, false, null)); Collections.sort(childPaths); for (String childPath : childPaths) { final String fullChildPath = createFullPath(path, childPath); if (!this.options.isPathExcluded(LOGGER, fullChildPath)) { if (this.options.isPathIncluded(LOGGER, fullChildPath)) { doBackup(zk, jgen, fullChildPath); } } } } catch (NoNodeException e) { LOGGER.warn("Node disappeared during backup: {}", path); } }
Example #17
Source File: SafeModeTest.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private static void rm(ZooKeeper zk, String path) throws KeeperException, InterruptedException { List<String> children = zk.getChildren(path, false); for (String c : children) { rm(zk, path + "/" + c); } zk.delete(path, -1); }
Example #18
Source File: ModularLoadManagerImpl.java From pulsar with Apache License 2.0 | 5 votes |
private static void createZPathIfNotExists(final ZooKeeper zkClient, final String path) throws Exception { if (zkClient.exists(path, false) == null) { try { ZkUtils.createFullPathOptimistic(zkClient, path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException e) { // Ignore if already exists. } } }
Example #19
Source File: BookKeeperClientFactoryImplTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testSetDefaultEnsemblePlacementPolicyRackAwareDisabled() { AtomicReference<ZooKeeperCache> rackawarePolicyZkCache = new AtomicReference<>(); AtomicReference<ZooKeeperCache> clientIsolationZkCache = new AtomicReference<>(); ClientConfiguration bkConf = new ClientConfiguration(); ServiceConfiguration conf = new ServiceConfiguration(); ZooKeeper zkClient = mock(ZooKeeper.class); assertNull(bkConf.getProperty(REPP_ENABLE_VALIDATION)); assertNull(bkConf.getProperty(REPP_REGIONS_TO_WRITE)); assertNull(bkConf.getProperty(REPP_MINIMUM_REGIONS_FOR_DURABILITY)); assertNull(bkConf.getProperty(REPP_ENABLE_DURABILITY_ENFORCEMENT_IN_REPLACE)); assertNull(bkConf.getProperty(REPP_DNS_RESOLVER_CLASS)); BookKeeperClientFactoryImpl.setDefaultEnsemblePlacementPolicy( rackawarePolicyZkCache, clientIsolationZkCache, bkConf, conf, zkClient ); assertNull(bkConf.getProperty(REPP_ENABLE_VALIDATION)); assertNull(bkConf.getProperty(REPP_REGIONS_TO_WRITE)); assertNull(bkConf.getProperty(REPP_MINIMUM_REGIONS_FOR_DURABILITY)); assertNull(bkConf.getProperty(REPP_ENABLE_DURABILITY_ENFORCEMENT_IN_REPLACE)); assertEquals( bkConf.getProperty(REPP_DNS_RESOLVER_CLASS), ZkBookieRackAffinityMapping.class.getName()); ((ZooKeeperCache) bkConf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE)).stop(); }
Example #20
Source File: WriteLock.java From disconf with Apache License 2.0 | 5 votes |
/** * zookeeper contructor for writelock * * @param zookeeper zookeeper client instance * @param dir the parent path you want to use for locking * @param acls the acls that you want to use for all the paths, * if null world read/write is used. */ public WriteLock(ZooKeeper zookeeper, String dir, List<ACL> acl) { super(zookeeper); this.dir = dir; if (acl != null) { setAcl(acl); } this.zop = new LockZooKeeperOperation(); }
Example #21
Source File: ZookeeperClusterStatusTest.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private static void rmr(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException { List<String> children = zooKeeper.getChildren(path, false); for (String c : children) { rmr(zooKeeper, path + "/" + c); } zooKeeper.delete(path, -1); }
Example #22
Source File: ZookeeperClusterStatus.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
public ZookeeperClusterStatus(String connectionStr, BlurConfiguration configuration, Configuration config) throws IOException { this(new ZooKeeper(connectionStr, 30000, new Watcher() { @Override public void process(WatchedEvent event) { } }), configuration, config); }
Example #23
Source File: ResourceTestPoolHelper.java From java-uniqueid with Apache License 2.0 | 5 votes |
public static void deleteLockingTicket(ZooKeeper zookeeper, String znode) throws KeeperException, InterruptedException { try { zookeeper.delete(znode + "/queue/" + LOCKING_TICKET, -1); } catch (KeeperException e) { if (e.code() != KeeperException.Code.NONODE) { throw e; } } }
Example #24
Source File: TestZKRMStateStoreZKClientConnections.java From big-c with Apache License 2.0 | 5 votes |
@Override public ZooKeeper getNewZooKeeper() throws IOException, InterruptedException { oldWatcher = watcher; watcher = new TestForwardingWatcher(); return createClient(watcher, hostPort, ZK_TIMEOUT_MS); }
Example #25
Source File: DistributedQueue.java From yuzhouwan with Apache License 2.0 | 5 votes |
private static void consume(ZooKeeper zk) throws KeeperException, InterruptedException { List<String> list = zk.getChildren("/distributedQueue", true); if (list.size() > 0) { String subPath = list.get(0); String zNode = "/distributedQueue/" + subPath; System.out.println("Get the data:\t" + new String(zk.getData(zNode, false, null), StandardCharsets.UTF_8) + "\tfrom " + zNode); zk.delete(zNode, 0); } else { System.out.println("No node to consume."); } }
Example #26
Source File: ZookeeperResource.java From spring-zookeeper with Apache License 2.0 | 5 votes |
public ZkExecutor(ZookeeperResource zkRes) throws IOException { this.znodes = zkRes.znodes; this.cmd = zkRes.chKCmd; this.zk = new ZooKeeper(zkRes.connString, 3000, this); this.dm = new DataMonitor(zk, znodes, zkRes.watcher, this); this.zkRes = zkRes; }
Example #27
Source File: ReadOnlyZKClient.java From hbase with Apache License 2.0 | 5 votes |
public CompletableFuture<List<String>> list(String path) { if (closed.get()) { return FutureUtils.failedFuture(new DoNotRetryIOException("Client already closed")); } CompletableFuture<List<String>> future = new CompletableFuture<>(); tasks.add(new ZKTask<List<String>>(path, future, "list") { @Override protected void doExec(ZooKeeper zk) { zk.getChildren(path, false, (rc, path, ctx, children) -> onComplete(zk, rc, children, true), null); } }); return future; }
Example #28
Source File: ZookeeperResourceTest.java From spring-zookeeper with Apache License 2.0 | 5 votes |
@Ignore @Test public void insertData2Zk() throws KeeperException, InterruptedException, IOException { ZookeeperConfigurer zkConf = (ZookeeperConfigurer) ctx.getBean("zkPropConfigurer"); ZookeeperResource zkResource = (ZookeeperResource) zkConf.getZkResoucre(); ZooKeeper zk = zkResource.getZk(); try { zk.delete("/cn_dev", -1); } catch (Exception e) { e.printStackTrace(); } String fileName = "/zk_cnfig_cn_test.txt"; String fileContent = getFileContent(fileName); zk.create("/cn_dev", fileContent.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }
Example #29
Source File: ClusterIDIT.java From java-uniqueid with Apache License 2.0 | 5 votes |
@Test public void getClusterIDTest() throws Exception { ZooKeeper zookeeper = zkInstance.getZookeeperConnection(); ResourceTestPoolHelper.prepareClusterID(zookeeper, "/some-path", CLUSTER_ID); int id = ClusterID.get(zookeeper, "/some-path"); assertThat(id, is(CLUSTER_ID)); }
Example #30
Source File: ServiceRegistry.java From rpc4j with MIT License | 5 votes |
private void createNode(ZooKeeper zk, String data) { try { byte[] bytes = data.getBytes(); Stat stat = zk.exists(Constant.ZK_REGISTRY_PATH, false); if(stat==null) { String path1 = zk.create(Constant.ZK_REGISTRY_PATH, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } String path = zk.create(Constant.ZK_DATA_PATH, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); LOGGER.debug("create zookeeper node ({} => {})", path, data); } catch (KeeperException | InterruptedException e) { LOGGER.error("", e); } }