Example usage for org.apache.zookeeper KeeperException getMessage

List of usage examples for org.apache.zookeeper KeeperException getMessage

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:cc.alessandro.zookeeper.example.Main.java

License:Open Source License

private void queue_test() throws IOException, KeeperException, InterruptedException {
    Queue q = new Queue(address, "/app1");

    if (producer != null) {
        LOG.info("Producer...");
        for (int i = 0; i < max; i++) {
            q.produce(10 + i);// www . j  av a 2  s .c o m
        }
    } else {
        LOG.info("Consumer...");
        for (int i = 0; i < max; i++) {
            try {
                int r = q.consume();
                LOG.info("Item: {}", r);
            } catch (KeeperException exception) {
                i--;
                LOG.error(exception.getMessage());
            }
        }
    }
}

From source file:com.alibaba.wasp.zookeeper.ZKUtil.java

License:Apache License

/** @return String dump of everything in ZooKeeper. */
public static String dump(ZooKeeperWatcher zkw) {
    StringBuilder sb = new StringBuilder();
    try {/*from www  .j  a v  a  2  s .  c  om*/
        sb.append("Wasp is rooted at ").append(zkw.baseZNode);
        sb.append("\nActive master address: ");
        try {
            sb.append(MasterAddressTracker.getMasterAddress(zkw));
        } catch (IOException e) {
            sb.append("<<FAILED LOOKUP: " + e.getMessage() + ">>");
        }
        sb.append("\nBackup master addresses:");
        for (String child : listChildrenNoWatch(zkw, zkw.backupMasterAddressesZNode)) {
            sb.append("\n ").append(child);
        }
        sb.append("\nFServers:");
        for (String child : listChildrenNoWatch(zkw, zkw.fsZNode)) {
            sb.append("\n ").append(child);
        }
        sb.append("\nQuorum Server Statistics:");
        String[] servers = zkw.getQuorum().split(",");
        for (String server : servers) {
            sb.append("\n ").append(server);
            try {
                String[] stat = getServerStats(server, ZKUtil.zkDumpConnectionTimeOut);

                if (stat == null) {
                    sb.append("[Error] invalid quorum server: " + server);
                    break;
                }

                for (String s : stat) {
                    sb.append("\n  ").append(s);
                }
            } catch (Exception e) {
                sb.append("\n  ERROR: ").append(e.getMessage());
            }
        }
    } catch (KeeperException ke) {
        sb.append("\nFATAL ZooKeeper Exception!\n");
        sb.append("\n" + ke.getMessage());
    }
    return sb.toString();
}

From source file:com.andyadc.menagerie.latches.AbstractZkBarrier.java

License:Apache License

/**
 * Causes the current thread to wait until one of the following conditions are met:
 * <p>/*from ww  w .j  av  a 2 s . c o m*/
 * 1. All required entities join the barrier
 * 2. The Current thread is interrupted
 * 3. The specified timeout is reached.
 * <p>
 *
 * @param timeout     the maximum time to wait
 * @param unit        the time unit to wait in
 * @param latchPrefix the prefix for latch nodes to use
 * @return true if the barrier was successfully reached before the timeout was reached, false otherwise.
 * @throws InterruptedException if the current thread is interrupted, or if there is communication trouble
 *                              between the ZooKeeper service and this client.
 */
protected final boolean doWait(long timeout, TimeUnit unit, String latchPrefix) throws InterruptedException {
    if (Thread.interrupted())
        throw new InterruptedException();
    //attach ourselves as a session listener
    zkSessionManager.addConnectionListener(connectionListener);
    boolean barrierReached;
    while (true) {
        if (Thread.interrupted())
            throw new InterruptedException();
        localLock.lock();
        try {
            List<String> children = ZkUtils.filterByPrefix(
                    zkSessionManager.getZooKeeper().getChildren(baseNode, signalWatcher), latchPrefix);

            long count = children.size();
            if (count >= total) {
                barrierReached = true;
                break;
            } else {
                boolean alerted = condition.await(timeout, unit);
                if (!alerted) {
                    barrierReached = false;
                    break;
                }
            }
        } catch (KeeperException e) {
            throw new InterruptedException(e.getMessage());
        } finally {
            localLock.unlock();
        }
    }
    zkSessionManager.removeConnectionListener(connectionListener);
    return barrierReached;
}

From source file:com.asuraiv.coordination.util.ZooKeeperUtils.java

License:Open Source License

/**
 * @param string// w  ww .  ja  v a  2  s.c o  m
 * @return
 */
public String getData(String path) {
    try {
        return new String(zk.getData(path, false, null));
    } catch (KeeperException e) {
        if (e.getMessage().contains("NoNode")) {
            System.err.println(e.getMessage());
        } else {
            e.printStackTrace();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}

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

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {/*from  w  ww  .j a  v a  2s . c o m*/
        String path = (String) parameters.get("path");
        String data = parameters.containsKey("data") ? (String) parameters.get("data") : DEFAULT_DATA;
        String acl = parameters.containsKey("acl") ? (String) parameters.get("acl") : DEFAULT_ACL;
        boolean ephemeral = parameters.containsKey("ephemeral") ? (Boolean) parameters.get("ephemeral")
                : DEFAULT_EPHEMERAL;
        boolean sequential = parameters.containsKey("sequential") ? (Boolean) parameters.get("sequential")
                : DEFAULT_SEQUENTIAL;
        boolean parents = parameters.containsKey("parents") ? (Boolean) parameters.get("parents")
                : DEFAULT_PARENTS;
        boolean watch = parameters.containsKey("watch") ? (Boolean) parameters.get("watch") : DEFAULT_WATCH;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        byte[] byteData = data.getBytes();

        List<ACL> aclObj = Ids.OPEN_ACL_UNSAFE;
        if (acl.length() > 0) {
            aclObj = ACLUtil.parseACLs(acl);
        }

        CreateMode createMode = CreateMode.PERSISTENT;
        if (ephemeral && sequential) {
            createMode = CreateMode.EPHEMERAL_SEQUENTIAL;
        } else if (ephemeral) {
            createMode = CreateMode.EPHEMERAL;
        } else if (sequential) {
            createMode = CreateMode.PERSISTENT_SEQUENTIAL;
        }

        String newPath = null;
        if (parents) {
            StringBuilder sbPath = new StringBuilder("/");
            List<String> paths = new ArrayList<String>();
            if (path.equals("/")) {
                paths.add("");
            } else {
                paths = Arrays.asList(path.split("/"));
            }
            for (Iterator<String> i = paths.iterator(); i.hasNext();) {
                String p = i.next();
                sbPath.append(p);

                Stat stat = zk.exists(sbPath.toString(), watch);
                if (stat != null) {
                    // znode already exist
                    if (!i.hasNext()) {
                        // throw exception
                        throw KeeperException.NodeExistsException.create(Code.NODEEXISTS, sbPath.toString());
                    }
                } else {
                    // znode does not exist
                    if (i.hasNext()) {
                        // sub znode created by empty data
                        newPath = zk.create(sbPath.toString(), "".getBytes(), aclObj, createMode);
                    } else {
                        // create new znode by data
                        newPath = zk.create(sbPath.toString(), byteData, aclObj, createMode);
                    }
                }

                if (i.hasNext() && !sbPath.toString().endsWith("/")) {
                    sbPath.append("/");
                }
            }
        } else {
            newPath = zk.create(path, byteData, aclObj, createMode);
        }

        putResponse("new_path", newPath);

        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:com.github.mosuka.zookeeper.nicli.command.DeleteCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {//from   w ww. ja va2 s  .  c o m
        String path = (String) parameters.get("path");
        int version = parameters.containsKey("version") ? (Integer) parameters.get("version") : DEFAULT_VERSION;
        boolean recursive = parameters.containsKey("recursive") ? (Boolean) parameters.get("recursive")
                : DEFAULT_RECURSIVE;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        if (recursive) {
            ZKUtil.deleteRecursive(zk, path);
        } else {
            zk.delete(path, version);
        }

        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:com.github.mosuka.zookeeper.nicli.command.DelQuotaCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {//from  w w  w.  j  a v  a  2s . com
        String path = (String) parameters.get("path");
        boolean bytes = parameters.containsKey("bytes") ? (Boolean) parameters.get("bytes") : DEFAULT_BYTES;
        boolean numNodes = parameters.containsKey("num_nodes") ? (Boolean) parameters.get("num_nodes")
                : DEFAULT_NUM_NODES;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        String parentPath = Quotas.quotaZookeeper + path;
        String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;

        if (zk.exists(quotaPath, false) == null) {
            setStatus(Command.STATUS_ERROR);
            setMessage("quota for " + path + " does not exist.");
            return;
        }

        byte[] data = zk.getData(quotaPath, false, new Stat());
        StatsTrack strack = new StatsTrack(new String(data));

        if (bytes && !numNodes) {
            strack.setBytes(-1L);
            zk.setData(quotaPath, strack.toString().getBytes(), -1);
        } else if (!bytes && numNodes) {
            strack.setCount(-1);
            zk.setData(quotaPath, strack.toString().getBytes(), -1);
        } else if (bytes && numNodes) {
            List<String> children = zk.getChildren(parentPath, false);
            for (String child : children) {
                zk.delete(parentPath + "/" + child, -1);
            }
            QuotaUtil.trimProcQuotas(zk, parentPath);
        } else {
            setStatus(Command.STATUS_ERROR);
            setMessage("too few arguments");
            return;
        }

        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 (IOException 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:com.github.mosuka.zookeeper.nicli.command.GetAclCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {/*from  ww  w. ja v  a2s.  c  o m*/
        String path = (String) parameters.get("path");
        boolean withStat = parameters.containsKey("with_stat") ? (Boolean) parameters.get("with_stat")
                : DEFAULT_WITH_STAT;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        Stat stat = new Stat();
        List<ACL> acl = zk.getACL(path, stat);

        if (acl != null) {
            putResponse("acl", ACLUtil.acl2List(acl));
        }
        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:com.github.mosuka.zookeeper.nicli.command.GetCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {// w  w w.  j a  v a  2s.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();
        byte data[] = zk.getData(path, watch, stat);

        putResponse("data", data == null ? null : new String(data));
        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:com.github.mosuka.zookeeper.nicli.command.ListQuotaCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    String path = null;/*from w  w w.j av a  2 s.  c om*/
    try {
        path = (String) parameters.get("path");
        String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
        String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        Stat quotaStat = new Stat();
        byte[] quotaData = zk.getData(quotaPath, false, quotaStat);
        StatsTrack quotaStatsTrack = new StatsTrack(new String(quotaData));
        Map<String, Object> quotaMap = new LinkedHashMap<String, Object>();
        quotaMap.put("path", quotaPath);
        quotaMap.put("count", quotaStatsTrack.getCount());
        quotaMap.put("bytes", quotaStatsTrack.getBytes());
        putResponse("quota", quotaMap);

        Stat statStat = new Stat();
        byte[] statData = zk.getData(statPath, false, statStat);
        StatsTrack statStatsTrack = new StatsTrack(new String(statData));
        Map<String, Object> statMap = new LinkedHashMap<String, Object>();
        statMap.put("path", statPath);
        statMap.put("count", statStatsTrack.getCount());
        statMap.put("bytes", statStatsTrack.getBytes());
        putResponse("stat", statMap);

        setStatus(Command.STATUS_SUCCESS);
        setMessage(Command.SUCCESS_MESSAGE);
    } catch (KeeperException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage("quota for " + path + " does not exist.");
    } 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());
    }
}