Example usage for org.apache.zookeeper StatsTrack toString

List of usage examples for org.apache.zookeeper StatsTrack toString

Introduction

In this page you can find the example usage for org.apache.zookeeper StatsTrack toString.

Prototype

@Override
     public String toString() 

Source Link

Usage

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

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {/*from ww w .j  a va2s.  c  o  m*/
        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.util.QuotaUtil.java

License:Apache License

public static boolean createQuota(ZooKeeper zk, String path, long bytes, int numNodes)
        throws KeeperException, IOException, InterruptedException {
    // check if the path exists. We cannot create
    // quota for a path that already exists in zookeeper
    // for now.//from w  ww .j a  va 2 s.c o  m
    Stat initStat = zk.exists(path, false);
    if (initStat == null) {
        throw new IllegalArgumentException(path + " does not exist.");
    }
    // now check if their is already existing
    // parent or child that has quota

    String quotaPath = Quotas.quotaZookeeper;
    // check for more than 2 children --
    // if zookeeper_stats and zookeeper_qutoas
    // are not the children then this path
    // is an ancestor of some path that
    // already has quota
    String realPath = Quotas.quotaZookeeper + path;
    try {
        List<String> children = zk.getChildren(realPath, false);
        for (String child : children) {
            if (!child.startsWith("zookeeper_")) {
                throw new IllegalArgumentException(path + " has child " + child + " which has a quota");
            }
        }
    } catch (KeeperException.NoNodeException ne) {
        // this is fine
    }

    // check for any parent that has been quota
    checkIfParentQuota(zk, path);

    // this is valid node for quota
    // start creating all the parents
    if (zk.exists(quotaPath, false) == null) {
        try {
            zk.create(Quotas.procZookeeper, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.create(Quotas.quotaZookeeper, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
            // do nothing
        }
    }

    // now create the direct children
    // and the stat and quota nodes
    String[] splits = path.split("/");
    StringBuilder sb = new StringBuilder();
    sb.append(quotaPath);
    for (int i = 1; i < splits.length; i++) {
        sb.append("/" + splits[i]);
        quotaPath = sb.toString();
        try {
            zk.create(quotaPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
            // do nothing
        }
    }
    String statPath = quotaPath + "/" + Quotas.statNode;
    quotaPath = quotaPath + "/" + Quotas.limitNode;
    StatsTrack strack = new StatsTrack(null);
    strack.setBytes(bytes);
    strack.setCount(numNodes);
    try {
        zk.create(quotaPath, strack.toString().getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        StatsTrack stats = new StatsTrack(null);
        stats.setBytes(0L);
        stats.setCount(0);
        zk.create(statPath, stats.toString().getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException ne) {
        byte[] data = zk.getData(quotaPath, false, new Stat());
        StatsTrack strackC = new StatsTrack(new String(data));
        if (bytes != -1L) {
            strackC.setBytes(bytes);
        }
        if (numNodes != -1) {
            strackC.setCount(numNodes);
        }
        zk.setData(quotaPath, strackC.toString().getBytes(), -1);
    }
    return true;
}

From source file:org.lab.mars.onem2m.ZooKeeperMain.java

License:Apache License

/**
 * this method deletes quota for a node.
 * /* w  ww  .j  a  v a2s  .c  om*/
 * @param zk
 *            the zookeeper client
 * @param path
 *            the path to delete quota for
 * @param bytes
 *            true if number of bytes needs to be unset
 * @param numNodes
 *            true if number of nodes needs to be unset
 * @return true if quota deletion is successful
 * @throws KeeperException
 * @throws IOException
 * @throws InterruptedException
 */
public static boolean delQuota(ZooKeeper zk, String path, boolean bytes, boolean numNodes)
        throws KeeperException, IOException, InterruptedException {
    String parentPath = Quotas.quotaZookeeper + path;
    String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
    if (zk.exists(quotaPath, false) == null) {
        System.out.println("Quota does not exist for " + path);
        return true;
    }
    byte[] data = null;
    try {
        data = zk.getData(quotaPath, false, new Stat());
    } catch (KeeperException.NoNodeException ne) {
        System.err.println("quota does not exist for " + path);
        return true;
    }
    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) {
        // delete till you can find a node with more than
        // one child
        List<String> children = zk.getChildren(parentPath, false);
        // / delete the direct children first
        for (String child : children) {
            zk.delete(parentPath + "/" + child, -1);
        }
        // cut the tree till their is more than one child
        trimProcQuotas(zk, parentPath);
    }
    return true;
}

From source file:org.lab.mars.onem2m.ZooKeeperMain.java

License:Apache License

/**
 * this method creates a quota node for the path
 * //w w w  .j a  v  a  2  s. co m
 * @param zk
 *            the ZooKeeper client
 * @param path
 *            the path for which quota needs to be created
 * @param bytes
 *            the limit of bytes on this path
 * @param numNodes
 *            the limit of number of nodes on this path
 * @return true if its successful and false if not.
 */
public static boolean createQuota(ZooKeeper zk, String path, long bytes, int numNodes)
        throws KeeperException, IOException, InterruptedException {
    // check if the path exists. We cannot create
    // quota for a path that already exists in zookeeper
    // for now.
    Stat initStat = zk.exists(path, false);
    if (initStat == null) {
        throw new IllegalArgumentException(path + " does not exist.");
    }
    // now check if their is already existing
    // parent or child that has quota

    String quotaPath = Quotas.quotaZookeeper;
    // check for more than 2 children --
    // if zookeeper_stats and zookeeper_qutoas
    // are not the children then this path
    // is an ancestor of some path that
    // already has quota
    String realPath = Quotas.quotaZookeeper + path;
    try {
        List<String> children = zk.getChildren(realPath, false);
        for (String child : children) {
            if (!child.startsWith("zookeeper_")) {
                throw new IllegalArgumentException(path + " has child " + child + " which has a quota");
            }
        }
    } catch (KeeperException.NoNodeException ne) {
        // this is fine
    }

    // check for any parent that has been quota
    checkIfParentQuota(zk, path);

    // this is valid node for quota
    // start creating all the parents
    if (zk.exists(quotaPath, false) == null) {
        try {
            zk.create(Quotas.procZookeeper, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.create(Quotas.quotaZookeeper, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
            // do nothing
        }
    }

    // now create the direct children
    // and the stat and quota nodes
    String[] splits = path.split("/");
    StringBuilder sb = new StringBuilder();
    sb.append(quotaPath);
    for (int i = 1; i < splits.length; i++) {
        sb.append("/" + splits[i]);
        quotaPath = sb.toString();
        try {
            zk.create(quotaPath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
            // do nothing
        }
    }
    String statPath = quotaPath + "/" + Quotas.statNode;
    quotaPath = quotaPath + "/" + Quotas.limitNode;
    StatsTrack strack = new StatsTrack(null);
    strack.setBytes(bytes);
    strack.setCount(numNodes);
    try {
        zk.create(quotaPath, strack.toString().getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        StatsTrack stats = new StatsTrack(null);
        stats.setBytes(0L);
        stats.setCount(0);
        zk.create(statPath, stats.toString().getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException ne) {
        byte[] data = zk.getData(quotaPath, false, new Stat());
        StatsTrack strackC = new StatsTrack(new String(data));
        if (bytes != -1L) {
            strackC.setBytes(bytes);
        }
        if (numNodes != -1) {
            strackC.setCount(numNodes);
        }
        zk.setData(quotaPath, strackC.toString().getBytes(), -1);
    }
    return true;
}

From source file:org.lab.mars.onem2m.ZooKeeperMain.java

License:Apache License

protected boolean processZKCmd(MyCommandOptions co) throws KeeperException, IOException, InterruptedException {
    Stat stat = new Stat();
    String[] args = co.getArgArray();
    String cmd = co.getCommand();
    if (args.length < 1) {
        usage();//ww  w .j  a v  a  2 s.  c  o m
        return false;
    }

    if (!commandMap.containsKey(cmd)) {
        usage();
        return false;
    }

    boolean watch = args.length > 2;
    String path = null;
    List<ACL> acl = Ids.OPEN_ACL_UNSAFE;
    LOG.debug("Processing " + cmd);

    if (cmd.equals("quit")) {
        System.out.println("Quitting...");
        zk.close();
        System.exit(0);
    } else if (cmd.equals("redo") && args.length >= 2) {
        Integer i = Integer.decode(args[1]);
        if (commandCount <= i) { // don't allow redoing this redo
            System.out.println("Command index out of range");
            return false;
        }
        cl.parseCommand(history.get(i));
        if (cl.getCommand().equals("redo")) {
            System.out.println("No redoing redos");
            return false;
        }
        history.put(commandCount, history.get(i));
        processCmd(cl);
    } else if (cmd.equals("history")) {
        for (int i = commandCount - 10; i <= commandCount; ++i) {
            if (i < 0)
                continue;
            System.out.println(i + " - " + history.get(i));
        }
    } else if (cmd.equals("printwatches")) {
        if (args.length == 1) {
            System.out.println("printwatches is " + (printWatches ? "on" : "off"));
        } else {
            printWatches = args[1].equals("on");
        }
    } else if (cmd.equals("connect")) {
        if (args.length >= 2) {
            connectToZK(args[1]);
        } else {
            connectToZK(host);
        }
    }

    if (cmd.equals("create") && args.length >= 3) {
        int first = 0;
        CreateMode flags = CreateMode.PERSISTENT;
        if ((args[1].equals("-e") && args[2].equals("-s"))
                || (args[1]).equals("-s") && (args[2].equals("-e"))) {
            first += 2;
            flags = CreateMode.EPHEMERAL_SEQUENTIAL;
        } else if (args[1].equals("-e")) {
            first++;
            flags = CreateMode.EPHEMERAL;
        } else if (args[1].equals("-s")) {
            first++;
            flags = CreateMode.PERSISTENT_SEQUENTIAL;
        }
        if (args.length == first + 4) {
            acl = parseACLs(args[first + 3]);
        }
        path = args[first + 1];
        String newPath = zk.create(path, args[first + 2].getBytes(), acl, flags);
        System.err.println("Created " + newPath);
    } else if (cmd.equals("delete") && args.length >= 2) {
        path = args[1];
        zk.delete(path, watch ? Integer.parseInt(args[2]) : -1);
    } else if (cmd.equals("rmr") && args.length >= 2) {
        path = args[1];
        ZKUtil.deleteRecursive(zk, path);
    } else if (cmd.equals("set") && args.length >= 3) {
        path = args[1];
        stat = zk.setData(path, args[2].getBytes(), args.length > 3 ? Integer.parseInt(args[3]) : -1);
        printStat(stat);
    } else if (cmd.equals("aget") && args.length >= 2) {
        path = args[1];
    } else if (cmd.equals("get") && args.length >= 2) {
        path = args[1];
        byte data[] = zk.getData(path, watch, stat);
        data = (data == null) ? "null".getBytes() : data;
        System.out.println(new String(data));
        printStat(stat);
    } else if (cmd.equals("ls") && args.length >= 2) {
        path = args[1];
        List<String> children = zk.getChildren(path, watch);
        System.out.println(children);
    } else if (cmd.equals("ls2") && args.length >= 2) {
        path = args[1];
        List<String> children = zk.getChildren(path, watch, stat);
        System.out.println(children);
        printStat(stat);
    } else if (cmd.equals("getAcl") && args.length >= 2) {
        path = args[1];
        acl = zk.getACL(path, stat);
    } else if (cmd.equals("setAcl") && args.length >= 3) {
        path = args[1];
        stat = zk.setACL(path, parseACLs(args[2]), args.length > 4 ? Integer.parseInt(args[3]) : -1);
        printStat(stat);
    } else if (cmd.equals("stat") && args.length >= 2) {
        path = args[1];
        stat = zk.exists(path, watch);
        if (stat == null) {
            throw new KeeperException.NoNodeException(path);
        }
        printStat(stat);
    } else if (cmd.equals("listquota") && args.length >= 2) {
        path = args[1];
        String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
        byte[] data = null;
        try {
            System.err.println("absolute path is " + absolutePath);
            data = zk.getData(absolutePath, false, stat);
            StatsTrack st = new StatsTrack(new String(data));
            System.out.println("Output quota for " + path + " " + st.toString());

            data = zk.getData(Quotas.quotaZookeeper + path + "/" + Quotas.statNode, false, stat);
            System.out.println("Output stat for " + path + " " + new StatsTrack(new String(data)).toString());
        } catch (KeeperException.NoNodeException ne) {
            System.err.println("quota for " + path + " does not exist.");
        }
    } else if (cmd.equals("setquota") && args.length >= 4) {
        String option = args[1];
        String val = args[2];
        path = args[3];
        System.err.println("Comment: the parts are " + "option " + option + " val " + val + " path " + path);
        if ("-b".equals(option)) {
            // we are setting the bytes quota
            createQuota(zk, path, Long.parseLong(val), -1);
        } else if ("-n".equals(option)) {
            // we are setting the num quota
            createQuota(zk, path, -1L, Integer.parseInt(val));
        } else {
            usage();
        }

    } else if (cmd.equals("delquota") && args.length >= 2) {
        // if neither option -n or -b is specified, we delete
        // the quota node for thsi node.
        if (args.length == 3) {
            // this time we have an option
            String option = args[1];
            path = args[2];
            if ("-b".equals(option)) {
                delQuota(zk, path, true, false);
            } else if ("-n".equals(option)) {
                delQuota(zk, path, false, true);
            }
        } else if (args.length == 2) {
            path = args[1];
            // we dont have an option specified.
            // just delete whole quota node
            delQuota(zk, path, true, true);
        } else if (cmd.equals("help")) {
            usage();
        }
    } else if (cmd.equals("close")) {
        zk.close();
    } else if (cmd.equals("sync") && args.length >= 2) {
        path = args[1];
        zk.sync(path, new AsyncCallback.VoidCallback() {
            public void processResult(int rc, String path, Object ctx) {
                System.out.println("Sync returned " + rc);
            }
        }, null);
    } else if (cmd.equals("addauth") && args.length >= 2) {
        byte[] b = null;
        if (args.length >= 3)
            b = args[2].getBytes();

        zk.addAuthInfo(args[1], b);
    } else if (!commandMap.containsKey(cmd)) {
        usage();
    }
    return watch;
}