Example usage for org.apache.zookeeper StatsTrack StatsTrack

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

Introduction

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

Prototype

public StatsTrack(String stats) 

Source Link

Document

the stat string should be of the form count=int,bytes=long if stats is called with null the count and bytes are initialized to -1.

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 v a2 s  .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.command.ListQuotaCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    String path = null;// w  ww .j  a va 2 s  . c o m
    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());
    }
}

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./* w ww.j  av  a 2s. 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;
}