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;
}
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();
}
}
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());
}
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);
}
}
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());
}
Aggregations