Example usage for org.apache.zookeeper ZooKeeper getChildren

List of usage examples for org.apache.zookeeper ZooKeeper getChildren

Introduction

In this page you can find the example usage for org.apache.zookeeper ZooKeeper getChildren.

Prototype

public List<String> getChildren(String path, boolean watch) throws KeeperException, InterruptedException 

Source Link

Document

Return the list of the children of the node of the given path.

Usage

From source file:alluxio.master.ZkMasterInquireClient.java

License:Apache License

@Override
public synchronized InetSocketAddress getPrimaryRpcAddress() throws UnavailableException {
    ensureStarted();/*  w w w  . j a  v  a 2 s . co m*/
    long startTime = System.currentTimeMillis();
    int tried = 0;
    try {
        CuratorZookeeperClient curatorClient = mClient.getZookeeperClient();
        // #blockUntilConnectedOrTimedOut() will block for at least 1 second, even if the client is
        // connected within a few milliseconds. We improve the latency here by first waiting on the
        // connection status explicitly.
        for (int i = 0; i < 50; i++) {
            if (curatorClient.isConnected()) {
                break;
            }
            CommonUtils.sleepMs(20);
        }
        curatorClient.blockUntilConnectedOrTimedOut();
        String leaderPath = mConnectDetails.getLeaderPath();
        while (tried++ < mInquireRetryCount) {
            ZooKeeper zookeeper = curatorClient.getZooKeeper();
            if (zookeeper.exists(leaderPath, false) != null) {
                List<String> masters = zookeeper.getChildren(leaderPath, null);
                LOG.debug("Master addresses: {}", masters);
                if (masters.size() >= 1) {
                    if (masters.size() == 1) {
                        return NetworkAddressUtils.parseInetSocketAddress(masters.get(0));
                    }

                    long maxTime = 0;
                    String leader = "";
                    for (String master : masters) {
                        Stat stat = zookeeper.exists(PathUtils.concatPath(leaderPath, master), null);
                        if (stat != null && stat.getCtime() > maxTime) {
                            maxTime = stat.getCtime();
                            leader = master;
                        }
                    }
                    LOG.debug("The leader master: {}", leader);
                    return NetworkAddressUtils.parseInetSocketAddress(leader);
                }
            } else {
                LOG.info("{} does not exist ({})", leaderPath, tried);
            }
            CommonUtils.sleepMs(LOG, Constants.SECOND_MS);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        LOG.error("Error getting the leader master address from zookeeper. Zookeeper: {}", mConnectDetails, e);
    } finally {
        LOG.debug("Finished getPrimaryRpcAddress() in {}ms", System.currentTimeMillis() - startTime);
    }

    throw new UnavailableException("Failed to determine primary master rpc address");
}

From source file:alluxio.MasterInquireClient.java

License:Apache License

/**
 * @return the address of the current leader master
 *//*  w ww  .  j a  v a2  s  .  c  om*/
public synchronized String getLeaderAddress() {
    long startTime = System.currentTimeMillis();
    int tried = 0;
    try {
        CuratorZookeeperClient curatorClient = mClient.getZookeeperClient();
        // #blockUntilConnectedOrTimedOut() will block for at least 1 second, even if the client is
        // connected within a few milliseconds. We improve the latency here by first waiting on the
        // connection status explicitly.
        for (int i = 0; i < 50; i++) {
            if (curatorClient.isConnected()) {
                break;
            }
            CommonUtils.sleepMs(20);
        }
        curatorClient.blockUntilConnectedOrTimedOut();
        while (tried < mMaxTry) {
            ZooKeeper zookeeper = curatorClient.getZooKeeper();
            if (zookeeper.exists(mLeaderPath, false) != null) {
                List<String> masters = zookeeper.getChildren(mLeaderPath, null);
                LOG.debug("Master addresses: {}", masters);
                if (masters.size() >= 1) {
                    if (masters.size() == 1) {
                        return masters.get(0);
                    }

                    long maxTime = 0;
                    String leader = "";
                    for (String master : masters) {
                        Stat stat = zookeeper.exists(PathUtils.concatPath(mLeaderPath, master), null);
                        if (stat != null && stat.getCtime() > maxTime) {
                            maxTime = stat.getCtime();
                            leader = master;
                        }
                    }
                    LOG.debug("The leader master: {}", leader);
                    return leader;
                }
            } else {
                LOG.info("{} does not exist ({})", mLeaderPath, ++tried);
            }
            CommonUtils.sleepMs(LOG, Constants.SECOND_MS);
        }
    } catch (Exception e) {
        LOG.error("Error getting the leader master address from zookeeper. Zookeeper address: {}",
                mZookeeperAddress, e);
    } finally {
        LOG.debug("Finished getLeaderAddress() in {}ms", System.currentTimeMillis() - startTime);
    }

    return null;
}

From source file:cn.dataprocess.cfzk.ZkWatherUtil.java

/**
 * ?//  ww w  .  j  a v a  2 s  .c o m
 *
 * @param zooKeeper
 * @param watcher
 * @param pPath
 * @throws KeeperException
 * @throws InterruptedException
 */
public static void watch(ZooKeeper zooKeeper, Watcher watcher, String pPath)
        throws KeeperException, InterruptedException {
    zooKeeper.exists(pPath, watcher);
    zooKeeper.getData(pPath, watcher, null);
    zooKeeper.getChildren(pPath, watcher);
}

From source file:cn.dataprocess.cfzk.ZkWatherUtil.java

/**
 * ??//from w  w  w .  ja va2 s  . c om
 *
 * @param zooKeeper
 * @param watcher
 * @param pPath
 * @throws KeeperException
 * @throws InterruptedException
 */
public static void watchChilds(ZooKeeper zooKeeper, Watcher watcher, String pPath)
        throws KeeperException, InterruptedException {
    watch(zooKeeper, watcher, pPath);
    List<String> childs = zooKeeper.getChildren(pPath, null);
    if (childs != null && !childs.isEmpty()) {
        for (String child : childs) {
            log.debug("child=" + child);
            String cpatch = pPath + "/" + child;
            log.debug("cpatch=" + cpatch);
            watchChilds(zooKeeper, watcher, cpatch);
        }
    }
}

From source file:cn.dataprocess.cfzk.ZkWatherUtil.java

public static Object fromZkChildMain(ZooKeeper zooKeeper, String pPath)
        throws KeeperException, InterruptedException, IOException {
    Object ret = null;//from   w w  w.  ja va2s  . c o m
    log.debug("path=" + pPath);
    //??
    List<String> childs = zooKeeper.getChildren(pPath, null);
    if (childs != null && !childs.isEmpty()) {
        ret = new HashMap<>();
        Map<String, Object> map = (Map<String, Object>) ret;
        for (String child : childs) {
            log.debug("child=" + child);
            String cpatch = pPath + "/" + child;
            log.debug("cpatch=" + cpatch);
            map.put(child, fromZkChild(zooKeeper, cpatch));
        }
    }
    Object data = getData(zooKeeper, pPath);
    //??
    if (data != null) {
        if (data instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) data;
            if (ret != null) {
                Map<String, Object> retMap = (Map<String, Object>) ret;
                retMap.putAll(map);
            } else {
                ret = map;
            }
            log.debug("data instanceof Map  ret=" + ret);
        } else if (data instanceof Collection) {
            Collection<Object> ocoll = (Collection<Object>) data;
            if (ret != null) {
                Map<String, Object> retMap = (Map<String, Object>) ret;
                retMap.put(pPath.substring(pPath.lastIndexOf("/")), ocoll);
            } else {
                ret = ocoll;
            }
            log.debug(" else if (data instanceof Collection)   ret=" + ret);
        } else if (ret != null) {
            Map<String, Object> retMap = (Map<String, Object>) ret;
            String key = pPath.substring(pPath.lastIndexOf("/") + 1);
            log.debug(" else if (ret != null)   key=" + key);
            retMap.put(key, data);
        } else {
            ret = new HashMap<>();
            Map<String, Object> map = (Map<String, Object>) ret;
            String key = pPath.substring(pPath.lastIndexOf("/") + 1);
            log.debug(" else if (ret != null)   key=" + key);
            map.put(key, data);
        }
    }
    log.debug("ret=" + ret);
    return ret;
}

From source file:cn.dataprocess.cfzk.ZkWatherUtil.java

public static Object fromZkChild(ZooKeeper zooKeeper, String pPath)
        throws KeeperException, InterruptedException, IOException {
    Object ret = null;//from   w w w.ja v a  2 s.  c  o m
    log.debug("path=" + pPath);
    //??
    List<String> childs = zooKeeper.getChildren(pPath, null);
    if (childs != null && !childs.isEmpty()) {
        ret = new HashMap<>();
        Map<String, Object> map = (Map<String, Object>) ret;
        for (String child : childs) {
            log.debug("child=" + child);
            String cpatch = pPath + "/" + child;
            log.debug("cpatch=" + cpatch);
            map.put(child, fromZkChild(zooKeeper, cpatch));
        }
    }
    Object data = getData(zooKeeper, pPath);
    //??
    if (data != null) {
        if (data instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) data;
            if (ret != null) {
                Map<String, Object> retMap = (Map<String, Object>) ret;
                retMap.putAll(map);
            } else {
                ret = map;
            }
            log.debug("data instanceof Map  ret=" + ret);
        } else if (data instanceof Collection) {
            Collection<Object> ocoll = (Collection<Object>) data;
            if (ret != null) {
                Map<String, Object> retMap = (Map<String, Object>) ret;
                retMap.put(pPath.substring(pPath.lastIndexOf("/")), ocoll);
            } else {
                ret = ocoll;
            }
            log.debug(" else if (data instanceof Collection)   ret=" + ret);
        } else if (ret != null) {
            Map<String, Object> retMap = (Map<String, Object>) ret;
            String key = pPath.substring(pPath.lastIndexOf("/") + 1);
            log.debug(" else if (ret != null)   key=" + key);
            retMap.put(key, data);
        } else {
            ret = data;
        }
    }
    log.debug("ret=" + ret);
    return ret;
}

From source file:com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.StageMonitor.java

License:Apache License

/**
 * ?processId?//from w  w  w.  j a  v  a  2 s.  c  o  m
 */
private void syncStage(final Long processId) {
    // 1. ?pipelineId + processIdpath
    String path = null;
    try {
        path = StagePathUtils.getProcess(getPipelineId(), processId);
        // 2. ??process?
        IZkConnection connection = zookeeper.getConnection();
        // zkclient?zk?lock??watcher?zk?
        ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();
        List<String> currentStages = orginZk.getChildren(path, new AsyncWatcher() {

            public void asyncProcess(WatchedEvent event) {
                MDC.put(ArbitrateConstants.splitPipelineLogFileKey, String.valueOf(getPipelineId()));
                if (isStop()) {
                    return;
                }

                if (event.getType() == EventType.NodeDeleted) {
                    processTermined(processId); // ?
                    return;
                }

                // session expired/connection losscase?watcher???watcher?watcher?
                boolean dataChanged = event.getType() == EventType.NodeDataChanged
                        || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated
                        || event.getType() == EventType.NodeChildrenChanged;
                if (dataChanged) {
                    // boolean reply = initStage(processId);
                    // if (reply == false) {// load???????
                    syncStage(processId);
                    // }
                }
            }
        });

        Collections.sort(currentStages, new StageComparator());
        List<String> lastStages = this.currentStages.get(processId);
        if (lastStages == null || !lastStages.equals(currentStages)) {
            initProcessStage(processId); // ?
        }

    } catch (NoNodeException e) {
        processTermined(processId); // ?
    } catch (KeeperException e) {
        syncStage(processId);
    } catch (InterruptedException e) {
        // ignore
    }
}

From source file:com.andyadc.menagerie.collections.ZkHashMap.java

License:Apache License

/**
 * Constructs a new HashMap with the specified ZooKeeper client and serializer,
 * located at the specifed base znode, with the specified concurrency level and ZooKeeper privileges.
 * <p>//from   w  w w  .j a  v  a 2s.  co  m
 * Note: {@code mapNode} <em>must </em> exist in zookeeper before calling this constructor, or else an exception
 * will be thrown when first attempting to use this map.
 *
 * @param mapNode          the znode to use
 * @param zkSessionManager the session manager for the ZooKeeper instance
 * @param privileges       the ZooKeeper privileges to use
 * @param serializer       the serializer to use
 * @param concurrency      the estimated number of concurrently updating parties.
 */
public ZkHashMap(String mapNode, ZkSessionManager zkSessionManager, List<ACL> privileges,
        Serializer<Entry<K, V>> serializer, int concurrency) {
    /*
    Shamelessly stolen from the implementation of ConcurrentHashMap
    */
    int sshift = 0;
    int ssize = 1;
    while (ssize < concurrency) {
        ++sshift;
        ssize <<= 1;
    }
    segmentShift = 32 - sshift;
    segmentMask = ssize - 1;
    this.segments = ZkSegment.newArray(ssize);

    //need to build the map in a single, synchronized activity across all members
    Lock mapLock = new ReentrantZkLock(mapNode, zkSessionManager, privileges);
    mapLock.lock();
    try {
        //need to read the data out of zookeeper
        ZooKeeper zk = zkSessionManager.getZooKeeper();
        for (int i = 0; i < segments.length; i++) {
            try {
                //attach the first segments to any segments which are already created
                List<String> zkBuckets = ZkUtils.filterByPrefix(zk.getChildren(mapNode, false), "bucket");
                ZkUtils.sortBySequence(zkBuckets, '-');
                int numBucketsBuilt = 0;
                for (int bucketIndex = 0; bucketIndex < zkBuckets.size()
                        && bucketIndex < segments.length; bucketIndex++) {
                    numBucketsBuilt++;
                    segments[bucketIndex] = new ZkSegment<K, V>(mapNode + "/" + zkBuckets.get(bucketIndex),
                            serializer, zkSessionManager, privileges);
                }

                //create any additional segments as needed
                while (numBucketsBuilt < segments.length) {
                    String bucketNode = ZkUtils.safeCreate(zk, mapNode + "/bucket-", new byte[] {}, privileges,
                            CreateMode.PERSISTENT_SEQUENTIAL);
                    segments[numBucketsBuilt] = new ZkSegment<K, V>(bucketNode, serializer, zkSessionManager,
                            privileges);
                    numBucketsBuilt++;
                }
            } catch (KeeperException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    } finally {
        mapLock.unlock();
    }
}

From source file:com.andyadc.menagerie.collections.ZkIterator.java

License:Apache License

@Override
public boolean hasNext() {
    ZooKeeper zk = zkSessionManager.getZooKeeper();
    try {//from   w ww. j  a v  a 2 s  .  c o  m
        //get the most up-to-date-list
        List<String> children = ZkUtils.filterByPrefix(zk.getChildren(baseNode, false), prefix);
        //now go through it until you find a position higher than your current cursor
        ZkUtils.sortBySequence(children, iteratorDelimiter);
        for (String child : children) {
            int pos = ZkUtils.parseSequenceNumber(child, iteratorDelimiter);

            if (pos >= cursor) {
                currentData = ZkUtils.safeGetData(zk, baseNode + "/" + child, false, new Stat());
                if (currentData.length > 0) {
                    currentNode = child;
                    return true;
                }
            }
        }
        currentData = null;
        return false;
    } catch (KeeperException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.andyadc.menagerie.collections.ZkListSet.java

License:Apache License

@Override
public boolean contains(Object o) {
    acquireReadLock();/*  w w w . ja v a2  s  .c  o  m*/
    try {
        ZooKeeper zk = sessionManager.getZooKeeper();
        List<String> children = ZkUtils.filterByPrefix(zk.getChildren(baseNode, false), prefix());
        for (String child : children) {
            byte[] data = ZkUtils.safeGetData(zk, baseNode + "/" + child, false, new Stat());
            if (data.length > 0) {
                if (serializer.deserialize(data).equals(o))
                    return true;
            }
        }
        return false;
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (KeeperException e) {
        throw new RuntimeException(e);
    } finally {
        releaseReadLock();
    }
}