Java Code Examples for org.apache.zookeeper.ZooKeeper#exists()

The following examples show how to use org.apache.zookeeper.ZooKeeper#exists() . 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: ZkDistributeLock.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
/**
 * @param zkServers
 * @param lockName
 * @param sessionTimeout
 */
public ZkDistributeLock(String lockName,int sessionTimeout) {
	
	Validate.notBlank(zkServers, "config[jeesuite.lock.zkServers] not found");
	if(lockName.contains(LOCK_KEY_SUFFIX)){
		throw new LockException("lockName 不能包含[" + LOCK_KEY_SUFFIX + "]");
	}
	this.lockName = lockName;
	this.sessionTimeout = sessionTimeout;
	try {
		zk = new ZooKeeper(zkServers, sessionTimeout, this);
		Stat stat = zk.exists(ROOT_PATH, false);
		if (stat == null) {
			// 创建根节点
			zk.create(ROOT_PATH, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		}
	} catch (Exception e) {
		throw new LockException(e);
	}
}
 
Example 2
Source File: ZKManager.java    From uncode-schedule with GNU General Public License v2.0 6 votes vote down vote up
private 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 (StringUtils.isNotEmpty(str)) {
            zkPath = zkPath + "/" + str;
            if (zk.exists(zkPath, false) != null) {
                byte[] value = zk.getData(zkPath, false, null);
                if(value != null && new String(value).contains("uncode-schedule-")){
                    throw new Exception("\"" + zkPath +"\"  is already a schedule instance's root directory, its any subdirectory cannot as the root directory of others");
                }
            }
        }
    }
}
 
Example 3
Source File: RegisterDataSourceNode.java    From shark with Apache License 2.0 6 votes vote down vote up
@Override
public void register(ZooKeeper zk_client, String nodePath) {
	String nodePathValeu = null;
	if (null != nodePath) {
		zookeeperWatcher.init(zk_client, nodePath);
		try {
			/* 注册节点 */
			if (null != zk_client.exists(nodePath, zookeeperWatcher))
				nodePathValeu = new String(zk_client.getData(nodePath, false, null));
			/* 动态向spring的ioc容器中注册相关bean */
			RegisterDataSource.register(nodePathValeu, "zookeeper");
		} catch (Exception e) {
			throw new ConnectionException(e.toString());
		}
	}
}
 
Example 4
Source File: ZookeeperDriverImpl.java    From disconf with Apache License 2.0 6 votes vote down vote up
/**
 * 广度搜索法:搜索分布式配置对应的两层数据
 *
 * @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 5
Source File: LocalZooKeeperConnectionService.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Check if a persist node exists. If not, it attempts to create the znode.
 *
 * @param path
 *            znode path
 * @throws KeeperException
 *             zookeeper exception.
 * @throws InterruptedException
 *             zookeeper exception.
 */
public static void checkAndCreatePersistNode(ZooKeeper zkc, String path)
        throws KeeperException, InterruptedException {

    // check if the node exists
    if (zkc.exists(path, false) == null) {
        /**
         * create znode
         */
        try {
            // do create the node
            zkc.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            LOG.info("created znode, path={}", path);
        } catch (Exception e) {
            LOG.warn("create znode failed, path={} : {}", path, e.getMessage(), e);
        }
    }
}
 
Example 6
Source File: ZKManager.java    From tbschedule with Apache License 2.0 6 votes vote down vote up
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 7
Source File: ZKTools.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String[] getTree(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 8
Source File: TestRoundRobinJedisPool.java    From jodis with MIT License 6 votes vote down vote up
private void addNode(String name, int port, String state)
        throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = new ZooKeeper("localhost:" + zkPort, 5000, null);
    try {
        if (zk.exists(zkProxyDir, null) == null) {
            zk.create(zkProxyDir, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        ObjectNode node = mapper.createObjectNode();
        node.put("addr", "127.0.0.1:" + port);
        node.put("state", state);
        zk.create(zkProxyDir + "/" + name, mapper.writer().writeValueAsBytes(node),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } finally {
        zk.close();
    }
}
 
Example 9
Source File: DistributedQueue.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
private static void initQueue(ZooKeeper zk) throws KeeperException, InterruptedException {

        if (zk.exists("/distributedQueue", false) == null) {
            System.out.println("Create queue that path is '/distributedQueue'.");
            zk.create("/distributedQueue", "DQ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } else {
            System.out.println("Queue is exist in '/distributedQueue'!");
        }
    }
 
Example 10
Source File: ZookeeperDriverImpl.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定 配置数据
 *
 * @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 11
Source File: ZkUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the provided <i>path</i> exists or not and wait it expired if possible.
 *
 * @param zk the zookeeper client instance
 * @param path the zookeeper path
 * @param sessionTimeoutMs session timeout in milliseconds
 * @return true if path exists, otherwise return false
 * @throws KeeperException when failed to access zookeeper
 * @throws InterruptedException interrupted when waiting for znode to be expired
 */
public static boolean checkNodeAndWaitExpired(ZooKeeper zk,
                                              String path,
                                              long sessionTimeoutMs) throws KeeperException, InterruptedException {
    final CountDownLatch prevNodeLatch = new CountDownLatch(1);
    Watcher zkPrevNodeWatcher = watchedEvent -> {
        // check for prev node deletion.
        if (EventType.NodeDeleted == watchedEvent.getType()) {
            prevNodeLatch.countDown();
        }
    };
    Stat stat = zk.exists(path, zkPrevNodeWatcher);
    if (null != stat) {
        // if the ephemeral owner isn't current zookeeper client
        // wait for it to be expired
        if (stat.getEphemeralOwner() != zk.getSessionId()) {
            log.info("Previous znode : {} still exists, so waiting {} ms for znode deletion",
                path, sessionTimeoutMs);
            if (!prevNodeLatch.await(sessionTimeoutMs, TimeUnit.MILLISECONDS)) {
                throw new NodeExistsException(path);
            } else {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}
 
Example 12
Source File: DataMonitor.java    From bidder with Apache License 2.0 5 votes vote down vote up
public DataMonitor(ZooKeeper zk, String znode, Watcher chainedWatcher,
                   DataMonitorListener listener) {
    this.zk = zk;
    this.znode = znode;
    this.chainedWatcher = chainedWatcher;
    this.listener = listener;
    // Get things started by checking if the node exists. We are going
    // to be completely event driven
    zk.exists(znode, true, this, null);
}
 
Example 13
Source File: TestZooKeeperScanPolicyObserver.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void setExpireBefore(long time)
    throws KeeperException, InterruptedException, IOException {
  ZooKeeper zk = UTIL.getZooKeeperWatcher().getRecoverableZooKeeper().getZooKeeper();
  if (zk.exists(ZooKeeperScanPolicyObserver.NODE, false) == null) {
    zk.create(ZooKeeperScanPolicyObserver.NODE, Bytes.toBytes(time), ZooDefs.Ids.OPEN_ACL_UNSAFE,
      CreateMode.PERSISTENT);
  } else {
    zk.setData(ZooKeeperScanPolicyObserver.NODE, Bytes.toBytes(time), -1);
  }
}
 
Example 14
Source File: ZooKeeperHelper.java    From azeroth with Apache License 2.0 5 votes vote down vote up
static void mkdirp(ZooKeeper zookeeper, String znode) throws KeeperException,
                                                      InterruptedException {
    boolean createPath = false;
    for (String path : pathParts(znode)) {
        if (!createPath) {
            Stat stat = zookeeper.exists(path, false);
            if (stat == null) {
                createPath = true;
            }
        }
        if (createPath) {
            create(zookeeper, path);
        }
    }
}
 
Example 15
Source File: BlurUtil.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public static void createIfMissing(ZooKeeper zookeeper, String path) throws KeeperException, InterruptedException {
  if (zookeeper.exists(path, false) == null) {
    try {
      zookeeper.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException e) {
      if (e.code() == Code.NODEEXISTS) {
        return;
      }
      throw e;
    }
  }
}
 
Example 16
Source File: ZookeeperTest.java    From java-study with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	// 创建一个与服务器的连接
	 zk = new ZooKeeper(url , 
			 CONNECTION_TIMEOUT, new Watcher() { 
	            // 监控所有被触发的事件
	            public void process(WatchedEvent event) {
	                System.out.println(event.getPath()+"已经触发了" + event.getType() + "事件!"); 
	            } 
	        }); 
	 
	 /*
	  * 创建一个给定的目录节点 path, 并给它设置数据,
	  * CreateMode 标识有四种形式的目录节点,分别是
	  *  PERSISTENT:持久化目录节点,这个目录节点存储的数据不会丢失;
	  *  PERSISTENT_SEQUENTIAL:顺序自动编号的目录节点,这种目录节点会根据当前已近存在的节点数自动加 1,
	  *  然后返回给客户端已经成功创建的目录节点名;
	  *  EPHEMERAL:临时目录节点,一旦创建这个节点的客户端与服务器端口也就是 session 超时,这种节点会被自动删除;
	  *  EPHEMERAL_SEQUENTIAL:临时自动编号节点
	  */
	 
	 // 创建一个父级目录节点
	 if(zk.exists("/test", true)==null){
		 //参数说明:目录,参数,参数权限,节点类型
		 zk.create("/test", "data1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
	 }
	 if(zk.exists("/test/test1", true)==null){
		 // 创建一个子目录节点
		 zk.create("/test/test1", "data2".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
	 }
	 
	 System.out.println("="+new String(zk.getData("/test",false,null))); 
	 // 取出子目录节点列表
	 System.out.println("=="+zk.getChildren("/test",true)); 
	
	 if(zk.exists("/test/test1", true)!=null){
		 // 修改子目录节点数据
		 zk.setData("/test/test1","testOne".getBytes(),-1); 
	 }
	 System.out.println("目录节点状态:["+zk.exists("/test",true)+"]"); 
	 if(zk.exists("/test/test1", true)!=null){
	  // 创建另外一个子目录节点
	  zk.create("/test/test2", "test2".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
	 }
	 System.out.println("==="+new String(zk.getData("/test/test2",true,null))); 

	 /*
	  * 删除 path 对应的目录节点,version 为 -1 可以匹配任何版本,也就删除了这个目录节点所有数据
	  */
	 // 删除子目录节点
	 zk.delete("/test/test2",-1); 
	 zk.delete("/test/test1",-1); 
	 
	 // 删除父目录节点
	 zk.delete("/test",-1); 
	 // 关闭连接
	 zk.close();
}
 
Example 17
Source File: ResourceClaim.java    From java-uniqueid with Apache License 2.0 4 votes vote down vote up
/**
 * Try to claim an available resource from the resource pool.
 *
 * @param zookeeper   ZooKeeper connection to use.
 * @param poolNode    Path to the znode representing the resource pool.
 * @param poolSize    Size of the resource pool.
 * @param giveUpAfter Give up after this instant in time.
 * @return The claimed resource.
 */
int claimResource(ZooKeeper zookeeper, String poolNode, int poolSize, Instant giveUpAfter)
        throws KeeperException, InterruptedException, IOException {

    logger.debug("Trying to claim a resource.");
    List<String> claimedResources = zookeeper.getChildren(poolNode, false);
    if (claimedResources.size() >= poolSize) {
        logger.debug("No resources available at the moment (pool size: {}), waiting.", poolSize);
        // No resources available. Wait for a resource to become available.
        final CountDownLatch latch = new CountDownLatch(1);
        zookeeper.getChildren(poolNode, event -> latch.countDown());
        awaitLatchUnlessItTakesTooLong(latch, giveUpAfter);
        return claimResource(zookeeper, poolNode, poolSize, giveUpAfter);
    }

    // Try to claim an available resource.
    for (int i = 0; i < poolSize; i++) {
        String resourcePath = Integer.toString(i);
        if (!claimedResources.contains(resourcePath)) {
            String node;
            try {
                logger.debug("Trying to claim seemingly available resource {}.", resourcePath);
                node = zookeeper.create(
                        poolNode + "/" + resourcePath,
                        new byte[0],
                        ZooDefs.Ids.OPEN_ACL_UNSAFE,
                        CreateMode.EPHEMERAL
                );
            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.NODEEXISTS) {
                    // Failed to claim this resource for some reason.
                    continue;
                } else {
                    // Unexpected failure.
                    throw e;
                }
            }

            zookeeper.exists(node, event -> {
                if (event.getType() == EventType.NodeDeleted) {
                    // Invalidate our claim when the node is deleted by some other process.
                    logger.debug("Resource-claim node unexpectedly deleted ({})", resource);
                    close(true);
                }
            });

            logger.debug("Successfully claimed resource {}.", resourcePath);
            return i;
        }
    }

    return claimResource(zookeeper, poolNode, poolSize, giveUpAfter);
}
 
Example 18
Source File: ClusterStatus.java    From common-docker with Apache License 2.0 4 votes vote down vote up
/**
 * Checks whether /brokers/ids is present. This signifies that at least one Kafka broker has
 * registered in ZK.
 *
 * @param timeoutMs timeout in ms.
 * @param zookeeper Zookeeper client.
 * @return True if /brokers/ids is present.
 */
private static boolean isKafkaRegisteredInZookeeper(ZooKeeper zookeeper, int timeoutMs)
    throws InterruptedException {
  // Make sure /brokers/ids exists. Countdown when one of the following happen:
  // 1. node created event is triggered (this happens when /brokers/ids is created after the
  // call is made).
  // 2. StatCallback gets a non-null callback (this happens when /brokers/ids exists when the
  // call is made) .
  final CountDownLatch kafkaRegistrationSignal = new CountDownLatch(1);
  zookeeper.exists(
      BROKERS_IDS_PATH,
      new Watcher() {
        @Override
        public void process(WatchedEvent event) {
          log.debug(
              "Got event when checking for existence of /brokers/ids. type={} path={}",
              event.getType(),
              event.getPath()
          );
          if (event.getType() == Watcher.Event.EventType.NodeCreated) {
            kafkaRegistrationSignal.countDown();
          }
        }
      },
      new StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
          log.debug(
              "StatsCallback got data for path={}, stat={}",
              path,
              stat
          );
          if (stat != null) {
            kafkaRegistrationSignal.countDown();
          }
        }
      },
      null
  );

  boolean kafkaRegistrationTimedOut = !kafkaRegistrationSignal.await(
      timeoutMs,
      TimeUnit.MILLISECONDS
  );
  if (kafkaRegistrationTimedOut) {
    String message = String.format(
        "Timed out waiting for Kafka to create /brokers/ids in Zookeeper. timeout (ms) = %s",
        timeoutMs
    );
    throw new TimeoutException(message);
  }

  return true;
}
 
Example 19
Source File: ResourceClaim.java    From azeroth with Apache License 2.0 4 votes vote down vote up
int claimResource(ZooKeeper zookeeper, String poolNode, int poolSize) throws KeeperException,
                                                                      InterruptedException {

    logger.debug("Trying to claim a resource.");
    List<String> claimedResources = zookeeper.getChildren(poolNode, false);
    if (claimedResources.size() >= poolSize) {
        logger.debug("No resources available at the moment (pool size: {}), waiting.",
            poolSize);
        final CountDownLatch latch = new CountDownLatch(1);
        zookeeper.getChildren(poolNode, event -> latch.countDown());
        latch.await();
        return claimResource(zookeeper, poolNode, poolSize);
    }

    for (int i = 0; i < poolSize; i++) {
        String resourcePath = Integer.toString(i);
        if (!claimedResources.contains(resourcePath)) {
            String node;
            try {
                logger.debug("Trying to claim seemingly available resource {}.", resourcePath);
                node = zookeeper.create(poolNode + "/" + resourcePath, new byte[0],
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.NODEEXISTS) {
                    continue;
                } else {
                    throw e;
                }
            }

            zookeeper.exists(node, event -> {
                if (event.getType() == EventType.NodeDeleted) {
                    logger.debug("Resource-claim node unexpectedly deleted ({})", resource);
                    close(true);
                }
            });

            logger.debug("Successfully claimed resource {}.", resourcePath);
            return i;
        }
    }

    return claimResource(zookeeper, poolNode, poolSize);
}
 
Example 20
Source File: ZnodeService.java    From shark with Apache License 2.0 2 votes vote down vote up
/**
 * 检查rootNode节点是否存在
 * 
 * @author gaoxianglong
 * 
 * @param zk_client
 *            session会话
 * 
 * @param rootNodePath
 *            zk根节点
 * 
 * @return void
 */
private void isRootNode(ZooKeeper zk_client, String rootNodePath) throws Exception {
	if (null == zk_client.exists(rootNodePath, false)) {
		zk_client.create(rootNodePath, new byte[] {}, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}
}