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, Stat stat)
        throws KeeperException, InterruptedException 

Source Link

Document

For the given znode path return the stat and children list.

Usage

From source file:com.alibaba.otter.shared.arbitrate.impl.ArbitrateViewServiceImpl.java

License:Apache License

public Long getNextProcessId(Long channelId, Long pipelineId) {
    String processRoot = ManagePathUtils.getProcessRoot(channelId, pipelineId);
    IZkConnection connection = zookeeper.getConnection();
    // zkclient?stat??zk
    ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();

    Stat processParentStat = new Stat();
    // ?process/* w  w w . ja v a  2s  . c om*/
    try {
        orginZk.getChildren(processRoot, false, processParentStat);
        return (Long) ((processParentStat.getCversion() + processParentStat.getNumChildren()) / 2L);
    } catch (Exception e) {
        return -1L;
    }
}

From source file:com.alibaba.otter.shared.arbitrate.impl.ArbitrateViewServiceImpl.java

License:Apache License

public List<ProcessStat> listProcesses(Long channelId, Long pipelineId) {
    List<ProcessStat> processStats = new ArrayList<ProcessStat>();
    String processRoot = ManagePathUtils.getProcessRoot(channelId, pipelineId);
    IZkConnection connection = zookeeper.getConnection();
    // zkclient?stat??zk
    ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();

    // ?process//from  w  ww. java  2 s  .c  o  m
    List<String> processNodes = zookeeper.getChildren(processRoot);
    List<Long> processIds = new ArrayList<Long>();
    for (String processNode : processNodes) {
        processIds.add(ManagePathUtils.getProcessId(processNode));
    }

    Collections.sort(processIds);

    for (int i = 0; i < processIds.size(); i++) {
        Long processId = processIds.get(i);
        // ?process??
        ProcessStat processStat = new ProcessStat();
        processStat.setPipelineId(pipelineId);
        processStat.setProcessId(processId);

        List<StageStat> stageStats = new ArrayList<StageStat>();
        processStat.setStageStats(stageStats);
        try {
            String processPath = ManagePathUtils.getProcess(channelId, pipelineId, processId);
            Stat zkProcessStat = new Stat();
            List<String> stages = orginZk.getChildren(processPath, false, zkProcessStat);
            Collections.sort(stages, new StageComparator());

            StageStat prev = null;
            for (String stage : stages) {// ?processstage
                String stagePath = processPath + "/" + stage;
                Stat zkStat = new Stat();

                StageStat stageStat = new StageStat();
                stageStat.setPipelineId(pipelineId);
                stageStat.setProcessId(processId);

                byte[] bytes = orginZk.getData(stagePath, false, zkStat);
                if (bytes != null && bytes.length > 0) {
                    // ?zookeeperdata?managernodePipeKey?????'@'?
                    String json = StringUtils.remove(new String(bytes, "UTF-8"), '@');
                    EtlEventData data = JsonUtils.unmarshalFromString(json, EtlEventData.class);
                    stageStat.setNumber(data.getNumber());
                    stageStat.setSize(data.getSize());

                    Map exts = new HashMap();
                    if (!CollectionUtils.isEmpty(data.getExts())) {
                        exts.putAll(data.getExts());
                    }
                    exts.put("currNid", data.getCurrNid());
                    exts.put("nextNid", data.getNextNid());
                    exts.put("desc", data.getDesc());
                    stageStat.setExts(exts);
                }
                if (prev != null) {// start?
                    stageStat.setStartTime(prev.getEndTime());
                } else {
                    stageStat.setStartTime(zkProcessStat.getMtime()); // process?,select
                                                                      // await??USED?
                }
                stageStat.setEndTime(zkStat.getMtime());
                if (ArbitrateConstants.NODE_SELECTED.equals(stage)) {
                    stageStat.setStage(StageType.SELECT);
                } else if (ArbitrateConstants.NODE_EXTRACTED.equals(stage)) {
                    stageStat.setStage(StageType.EXTRACT);
                } else if (ArbitrateConstants.NODE_TRANSFORMED.equals(stage)) {
                    stageStat.setStage(StageType.TRANSFORM);
                    // } else if
                    // (ArbitrateConstants.NODE_LOADED.equals(stage)) {
                    // stageStat.setStage(StageType.LOAD);
                }

                prev = stageStat;
                stageStats.add(stageStat);
            }

            // ??
            StageStat currentStageStat = new StageStat();
            currentStageStat.setPipelineId(pipelineId);
            currentStageStat.setProcessId(processId);
            if (prev == null) {
                byte[] bytes = orginZk.getData(processPath, false, zkProcessStat);
                if (bytes == null || bytes.length == 0) {
                    continue; // 
                }

                ProcessNodeEventData nodeData = JsonUtils.unmarshalFromByte(bytes, ProcessNodeEventData.class);
                if (nodeData.getStatus().isUnUsed()) {// process,
                    continue; // process
                } else {
                    currentStageStat.setStage(StageType.SELECT);// select?
                    currentStageStat.setStartTime(zkProcessStat.getMtime());
                }
            } else {
                // ?stage
                StageType stage = prev.getStage();
                if (stage.isSelect()) {
                    currentStageStat.setStage(StageType.EXTRACT);
                } else if (stage.isExtract()) {
                    currentStageStat.setStage(StageType.TRANSFORM);
                } else if (stage.isTransform()) {
                    currentStageStat.setStage(StageType.LOAD);
                } else if (stage.isLoad()) {// ??
                    continue;
                }

                currentStageStat.setStartTime(prev.getEndTime());// ?
            }

            if (currentStageStat.getStage().isLoad()) {// loadprocess
                if (i == 0) {
                    stageStats.add(currentStageStat);
                }
            } else {
                stageStats.add(currentStageStat);// 
            }

        } catch (NoNodeException e) {
            // ignore
        } catch (KeeperException e) {
            throw new ArbitrateException(e);
        } catch (InterruptedException e) {
            // ignore
        } catch (UnsupportedEncodingException e) {
            // ignore
        }

        processStats.add(processStat);
    }

    return processStats;
}

From source file:com.alibaba.otter.shared.arbitrate.zookeeper.ZooKeeperClientTest.java

License:Apache License

@Test
public void testClient() {
    ZkClientx zk = ZooKeeperClient.getInstance();
    // ?zk??/*from  ww  w .  j  ava  2  s.c o m*/
    final ZooKeeper zkp = ((ZooKeeperx) zk.getConnection()).getZookeeper();
    ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zkp);
    HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
    List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils
            .getField(serverAddressesField, hostProvider);
    want.number(serverAddrs.size()).isEqualTo(3);
    String s1 = serverAddrs.get(0).getAddress().getHostAddress() + ":" + serverAddrs.get(0).getPort();
    want.string(s1).isEqualTo(cluster1);

    Stat stat = new Stat();
    try {
        zkp.getChildren("/otter/channel/304/388", false, stat);
        System.out.println(stat.getCversion());
    } catch (KeeperException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (InterruptedException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    // session timeout
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {

        public void run() {
            try {
                zkp.getChildren("/", false);
            } catch (KeeperException e1) {
                want.fail();
            } catch (InterruptedException e1) {
                want.fail();
            }
            int sessionTimeout = zkp.getSessionTimeout();
            long sessionId = zkp.getSessionId();
            byte[] passwd = zkp.getSessionPasswd();
            try {
                ZooKeeper newZk = new ZooKeeper(cluster1, sessionTimeout, new Watcher() {

                    public void process(WatchedEvent event) {
                        // do nothing
                    }

                }, sessionId, passwd);

                // ?sessionIdclose??SESSION_EXPIRED
                newZk.close();
            } catch (IOException e) {
                want.fail();
            } catch (InterruptedException e) {
                want.fail();
            }

            latch.countDown();
        }

    }.start();

    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }

    zk.getChildren("/");
}

From source file:com.asuraiv.coordination.menu.MainMenu.java

License:Open Source License

/**
 * ? ? ()./*from  w ww.  j ava 2s .  co m*/
 */
private void doDeleteTasks() {
    try {

        ZooKeeper zk = new ZooKeeper("10.113.182.195:2181", 15000, this);

        System.out.println();
        System.out.println("###    ###");

        List<String> tasks = zk.getChildren("/tasks", false, null);

        if (CollectionUtils.isEmpty(tasks)) {
            System.out.println("[WARNNING]  ?? ? .");
            return;
        }

        for (String task : tasks) {
            zk.delete("/tasks/" + task, -1);
            System.out.println(task + " ? ");
        }

        zk.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeeperException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.asuraiv.coordination.menu.MainMenu.java

License:Open Source License

/**
 *  ? .//from  w  w  w .ja  v  a 2  s  . co  m
 */
private void doTaskListUp() {
    try {

        ZooKeeper zk = new ZooKeeper("10.113.182.195:2181", 15000, this);

        System.out.println();
        System.out.println("###   ###");

        List<String> tasks = zk.getChildren("/tasks", false, null);

        if (CollectionUtils.isEmpty(tasks)) {
            System.out.println("[WARNNING]  ?? ? .");
        }

        for (String task : tasks) {
            String status = new String(zk.getData("/tasks/" + task, false, null));
            System.out.println(task + ": " + status);
        }

        zk.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeeperException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.boundary.zoocreeper.Backup.java

License:Apache License

private void doBackup(ZooKeeper zk, JsonGenerator jgen, String path)
        throws KeeperException, InterruptedException, IOException {
    try {/*from w  w  w  .j  a  va2s  .co m*/
        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);
    }
}

From source file:com.github.mosuka.zookeeper.nicli.command.LsCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    Map<String, Object> statMap = new LinkedHashMap<String, Object>();

    try {//from   www . j  av a 2 s  .c o  m
        String path = (String) parameters.get("path");
        boolean watch = parameters.containsKey("watch") ? (Boolean) parameters.get("watch") : DEFAULT_WATCH;
        boolean withStat = parameters.containsKey("with_stat") ? (Boolean) parameters.get("with_stat")
                : DEFAULT_WITH_STAT;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        Stat stat = new Stat();
        List<String> children = null;
        if (withStat) {
            children = zk.getChildren(path, watch, stat);
        } else {
            children = zk.getChildren(path, watch);
        }

        putResponse("children", children);
        if (stat != null && withStat) {
            putResponse("stat", StatUtil.stat2Map(stat));
        }

        setStatus(Command.STATUS_SUCCESS);
        setMessage(Command.SUCCESS_MESSAGE);
    } catch (KeeperException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (InterruptedException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (ClassCastException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (NullPointerException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    }
}

From source file:org.apache.bookkeeper.zookeeper.BkZooKeeperClient.java

License:Apache License

@Override
public List<String> getChildren(final String path, final Watcher watcher, final Stat stat)
        throws KeeperException, InterruptedException {
    return BkZooWorker.syncCallWithRetries(this, new ZooCallable<List<String>>() {

        @Override//from w  w  w. ja  v  a  2s.  co m
        public List<String> call() throws KeeperException, InterruptedException {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                return BkZooKeeperClient.super.getChildren(path, watcher, stat);
            }
            return zkHandle.getChildren(path, watcher, stat);
        }

        @Override
        public String toString() {
            return String.format("getChildren (%s, watcher = %s)", path, watcher);
        }

    }, operationRetryPolicy, rateLimiter, getChildrenStats);
}

From source file:org.apache.bookkeeper.zookeeper.BkZooKeeperClient.java

License:Apache License

@Override
public List<String> getChildren(final String path, final boolean watch, final Stat stat)
        throws KeeperException, InterruptedException {
    return BkZooWorker.syncCallWithRetries(this, new ZooCallable<List<String>>() {

        @Override// ww  w.j  ava 2  s .co m
        public List<String> call() throws KeeperException, InterruptedException {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                return BkZooKeeperClient.super.getChildren(path, watch, stat);
            }
            return zkHandle.getChildren(path, watch, stat);
        }

        @Override
        public String toString() {
            return String.format("getChildren (%s, watcher = %s)", path, watch);
        }

    }, operationRetryPolicy, rateLimiter, getChildrenStats);
}

From source file:org.apache.bookkeeper.zookeeper.ZooKeeperClient.java

License:Apache License

@Override
public List<String> getChildren(final String path, final Watcher watcher, final Stat stat)
        throws KeeperException, InterruptedException {
    return ZooWorker.syncCallWithRetries(this, new ZooCallable<List<String>>() {

        @Override/*from w w w.  ja  v  a 2  s  . co  m*/
        public List<String> call() throws KeeperException, InterruptedException {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                return ZooKeeperClient.super.getChildren(path, watcher, stat);
            }
            return zkHandle.getChildren(path, watcher, stat);
        }

        @Override
        public String toString() {
            return String.format("getChildren (%s, watcher = %s)", path, watcher);
        }

    }, operationRetryPolicy, rateLimiter, getChildrenStats);
}