Search in sources :

Example 1 with StatsTrack

use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.

the class DelQuotaCommand method delQuota.

/**
 * this method deletes quota for a node.
 *
 * @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, MalformedPathException {
    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 (IllegalArgumentException ex) {
        throw new MalformedPathException(ex.getMessage());
    } 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;
}
Also used : Stat(org.apache.zookeeper.data.Stat) StatsTrack(org.apache.zookeeper.StatsTrack) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with StatsTrack

use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.

the class DataTree method updateQuotaStat.

/**
 * update the count/bytes of this stat data node
 *
 * @param lastPrefix
 *            the path of the node that has a quota.
 * @param bytesDiff
 *            the diff to be added to number of bytes
 * @param countDiff
 *            the diff to be added to the count
 */
public void updateQuotaStat(String lastPrefix, long bytesDiff, int countDiff) {
    String statNodePath = Quotas.statPath(lastPrefix);
    DataNode statNode = nodes.get(statNodePath);
    StatsTrack updatedStat;
    if (statNode == null) {
        // should not happen
        LOG.error("Missing node for stat {}", statNodePath);
        return;
    }
    synchronized (statNode) {
        updatedStat = new StatsTrack(statNode.data);
        updatedStat.setCount(updatedStat.getCount() + countDiff);
        updatedStat.setBytes(updatedStat.getBytes() + bytesDiff);
        statNode.data = updatedStat.getStatsBytes();
    }
}
Also used : StatsTrack(org.apache.zookeeper.StatsTrack)

Example 3 with StatsTrack

use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.

the class StatsTrackTest method testBackwardCompatibility.

@Test
public void testBackwardCompatibility() {
    StatsTrack quota = new StatsTrack();
    quota.setCount(4);
    quota.setCountHardLimit(4);
    quota.setBytes(9L);
    quota.setByteHardLimit(15L);
    Assert.assertEquals("count=4,bytes=9=;byteHardLimit=15;countHardLimit=4", quota.toString());
    OldStatsTrack ost = new OldStatsTrack(quota.toString());
    Assert.assertTrue("bytes are set", ost.getBytes() == 9L);
    Assert.assertTrue("num count is set", ost.getCount() == 4);
    Assert.assertEquals("count=4,bytes=9", ost.toString());
}
Also used : StatsTrack(org.apache.zookeeper.StatsTrack) Test(org.junit.Test)

Example 4 with StatsTrack

use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.

the class ZooKeeperQuotaTest method testSetQuotaWhenSetQuotaLessThanExistCount.

@Test
public void testSetQuotaWhenSetQuotaLessThanExistCount() throws Exception {
    final String namespace = UUID.randomUUID().toString();
    final String path = "/" + namespace;
    zk.create(path, "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create(path + "/c1", "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create(path + "/c2", "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    int count = 2;
    StatsTrack st = new StatsTrack();
    st.setCountHardLimit(count);
    SetQuotaCommand.createQuota(zk, path, st);
    try {
        zk.create(path + "/c3", "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        fail("should not set quota when exceeds hard count quota");
    } catch (QuotaExceededException e) {
        // expected
        validateQuotaExceededMetrics(namespace);
    }
}
Also used : QuotaExceededException(org.apache.zookeeper.KeeperException.QuotaExceededException) OldStatsTrack(org.apache.zookeeper.test.StatsTrackTest.OldStatsTrack) StatsTrack(org.apache.zookeeper.StatsTrack) Test(org.junit.jupiter.api.Test)

Example 5 with StatsTrack

use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.

the class ZooKeeperQuotaTest method testSetQuota.

@Test
public void testSetQuota() throws IOException, InterruptedException, KeeperException, MalformedPathException {
    String path = "/c1";
    String nodeData = "foo";
    zk.create(path, nodeData.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    int count = 10;
    long bytes = 5L;
    StatsTrack quota = new StatsTrack();
    quota.setCount(count);
    quota.setBytes(bytes);
    SetQuotaCommand.createQuota(zk, path, quota);
    // check the limit
    String absoluteLimitPath = Quotas.limitPath(path);
    byte[] data = zk.getData(absoluteLimitPath, false, null);
    StatsTrack st = new StatsTrack(data);
    assertEquals(bytes, st.getBytes());
    assertEquals(count, st.getCount());
    // check the stats
    String absoluteStatPath = Quotas.statPath(path);
    data = zk.getData(absoluteStatPath, false, null);
    st = new StatsTrack(data);
    assertEquals(nodeData.length(), st.getBytes());
    assertEquals(1, st.getCount());
    // create another node
    String path2 = "/c1/c2";
    String nodeData2 = "bar";
    zk.create(path2, nodeData2.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    absoluteStatPath = Quotas.statPath(path);
    data = zk.getData(absoluteStatPath, false, null);
    st = new StatsTrack(data);
    // check the stats
    assertEquals(nodeData.length() + nodeData2.length(), st.getBytes());
    assertEquals(2, st.getCount());
}
Also used : OldStatsTrack(org.apache.zookeeper.test.StatsTrackTest.OldStatsTrack) StatsTrack(org.apache.zookeeper.StatsTrack) Test(org.junit.jupiter.api.Test)

Aggregations

StatsTrack (org.apache.zookeeper.StatsTrack)43 Test (org.junit.jupiter.api.Test)25 OldStatsTrack (org.apache.zookeeper.test.StatsTrackTest.OldStatsTrack)19 QuotaExceededException (org.apache.zookeeper.KeeperException.QuotaExceededException)14 KeeperException (org.apache.zookeeper.KeeperException)9 Stat (org.apache.zookeeper.data.Stat)6 DataTree (org.apache.zookeeper.server.DataTree)3 Op (org.apache.zookeeper.Op)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 MalformedPathException (org.apache.zookeeper.cli.MalformedPathException)1 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)1