use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.
the class ZooKeeperQuotaTest method testSetQuotaWhenSetChildDataExceedBytesQuota.
@Test
public void testSetQuotaWhenSetChildDataExceedBytesQuota() throws Exception {
final String namespace = UUID.randomUUID().toString();
final String path = "/" + namespace + "/quota";
zk.create("/" + namespace, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create(path, "01234".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create(path + "/data", "56789".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
StatsTrack quota = new StatsTrack();
quota.setByteHardLimit(10);
SetQuotaCommand.createQuota(zk, path, quota);
try {
zk.setData(path + "/data", "567891".getBytes(), -1);
fail("should not set data when exceed hard byte quota");
} catch (QuotaExceededException e) {
// expected
validateQuotaExceededMetrics(namespace);
}
}
use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.
the class ZooKeeperQuotaTest method testDeleteCountQuota.
@Test
public void testDeleteCountQuota() throws Exception {
final String namespace = UUID.randomUUID().toString();
final String path = "/" + namespace;
zk.create(path, "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
int count = 2;
StatsTrack st = new StatsTrack();
st.setCountHardLimit(count);
SetQuotaCommand.createQuota(zk, path, st);
zk.create(path + "/c2", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
try {
zk.create(path + "/c2" + "/c3", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
fail("should not set quota when exceeds hard count quota");
} catch (QuotaExceededException e) {
// expected
validateQuotaExceededMetrics(namespace);
}
// delete the Count Hard Quota
st = new StatsTrack();
st.setCountHardLimit(1);
DelQuotaCommand.delQuota(zk, path, st);
zk.create(path + "/c2" + "/c3", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
validateQuotaExceededMetrics(namespace);
}
use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.
the class QuotaMetricsUtils method collectQuotaLimitOrUsage.
static void collectQuotaLimitOrUsage(final String path, final DataNode node, final Map<String, Number> metricsMap, final QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
final String namespace = PathUtils.getTopNamespace(Quotas.trimQuotaPath(path));
if (namespace == null) {
return;
}
final byte[] data = node.getData();
if (data == null) {
return;
}
final StatsTrack statsTrack = new StatsTrack(data);
switch(type) {
case QUOTA_COUNT_LIMIT:
aggregateQuotaLimitOrUsage(namespace, metricsMap, getQuotaLimit(statsTrack.getCountHardLimit(), statsTrack.getCount()));
break;
case QUOTA_BYTES_LIMIT:
aggregateQuotaLimitOrUsage(namespace, metricsMap, getQuotaLimit(statsTrack.getByteHardLimit(), statsTrack.getBytes()));
break;
case QUOTA_COUNT_USAGE:
aggregateQuotaLimitOrUsage(namespace, metricsMap, statsTrack.getCount());
break;
case QUOTA_BYTES_USAGE:
aggregateQuotaLimitOrUsage(namespace, metricsMap, statsTrack.getBytes());
break;
default:
}
}
use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.
the class EnforceQuotaTest method testSetQuotaDisableWhenExceedCountHardQuota.
@Test
public void testSetQuotaDisableWhenExceedCountHardQuota() throws Exception {
final String namespace = UUID.randomUUID().toString();
final String path = "/" + namespace;
zk.create(path, "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
int count = 2;
StatsTrack st = new StatsTrack();
st.setCountHardLimit(count);
SetQuotaCommand.createQuota(zk, path, st);
zk.create(path + "/c2", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
try {
zk.create(path + "/c2" + "/c3", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
ZooKeeperQuotaTest.validateNoQuotaExceededMetrics(namespace);
} catch (KeeperException.QuotaExceededException e) {
fail("should not throw Count Quota Exceeded Exception when enforce quota disables");
}
}
use of org.apache.zookeeper.StatsTrack in project zookeeper by apache.
the class SetQuotaCommand method createQuota.
/**
* this method creates a quota node for the path
* @param zk the ZooKeeper client
* @param path the path for which quota needs to be created
* @param quota the quotas
* @return true if its successful and false if not.
*/
public static boolean createQuota(ZooKeeper zk, String path, StatsTrack quota) throws KeeperException, InterruptedException, MalformedPathException {
// check if the path exists. We cannot create
// quota for a path that doesn't exist in zookeeper
// for now.
Stat initStat;
try {
initStat = zk.exists(path, false);
} catch (IllegalArgumentException ex) {
throw new MalformedPathException(ex.getMessage());
}
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_quotas
// are not the children then this path
// is an ancestor of some path that
// already has quota
// check if the child node has a quota.
checkIfChildQuota(zk, path);
// check for any parent that has been quota
checkIfParentQuota(zk, path);
// 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("/").append(splits[i]);
quotaPath = sb.toString();
if (zk.exists(quotaPath, false) == null) {
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;
byte[] data;
if (zk.exists(quotaPath, false) == null) {
zk.create(quotaPath, quota.getStatsBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
StatsTrack stats = new StatsTrack();
stats.setCount(0);
stats.setBytes(0L);
zk.create(statPath, stats.getStatsBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
data = zk.getData(quotaPath, false, new Stat());
StatsTrack quotaStrack = new StatsTrack(data);
data = zk.getData(statPath, false, new Stat());
StatsTrack statStrack = new StatsTrack(data);
checkQuota(quotaStrack, statStrack);
} else {
data = zk.getData(quotaPath, false, new Stat());
StatsTrack quotaStrack = new StatsTrack(data);
if (quota.getCount() > -1) {
quotaStrack.setCount(quota.getCount());
}
if (quota.getBytes() > -1L) {
quotaStrack.setBytes(quota.getBytes());
}
if (quota.getCountHardLimit() > -1) {
quotaStrack.setCountHardLimit(quota.getCountHardLimit());
}
if (quota.getByteHardLimit() > -1L) {
quotaStrack.setByteHardLimit(quota.getByteHardLimit());
}
data = zk.getData(statPath, false, new Stat());
StatsTrack statStrack = new StatsTrack(data);
checkQuota(quotaStrack, statStrack);
zk.setData(quotaPath, quotaStrack.getStatsBytes(), -1);
}
return true;
}
Aggregations