Search in sources :

Example 31 with StatsTrack

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

the class ZooKeeperQuotaTest method testSetQuotaWhenExceedBytesHardQuota.

@Test
public void testSetQuotaWhenExceedBytesHardQuota() throws Exception {
    final String namespace = UUID.randomUUID().toString();
    final String path = "/" + namespace;
    zk.create(path, "12345".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    StatsTrack st = new StatsTrack();
    st.setByteHardLimit(5L);
    SetQuotaCommand.createQuota(zk, path, st);
    try {
        zk.setData(path, "123456".getBytes(), -1);
        fail("should not set data which exceeds the hard byte 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 32 with StatsTrack

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

the class ZooKeeperQuotaTest method testSetQuotaWhenCreateNodeExceedBytesQuota.

@Test
public void testSetQuotaWhenCreateNodeExceedBytesQuota() 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);
    StatsTrack quota = new StatsTrack();
    quota.setByteHardLimit(10);
    SetQuotaCommand.createQuota(zk, path, quota);
    try {
        zk.create(path + "/data", "567891".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        fail("should not set data when exceed hard byte 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 33 with StatsTrack

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

the class ZooKeeperServer method checkQuota.

/**
 * check a path whether exceeded the quota.
 *
 * @param lastPrefix
 *                  the path of the node which has a quota.
 * @param bytesDiff
 *            the diff to be added to number of bytes
 * @param countDiff
 *            the diff to be added to the count
 * @param namespace
 *           the namespace for collecting quota exceeded errors
 */
private void checkQuota(String lastPrefix, long bytesDiff, long countDiff, String namespace) throws KeeperException.QuotaExceededException {
    LOG.debug("checkQuota: lastPrefix={}, bytesDiff={}, countDiff={}", lastPrefix, bytesDiff, countDiff);
    // now check the quota we set
    String limitNode = Quotas.limitPath(lastPrefix);
    DataNode node = getZKDatabase().getNode(limitNode);
    StatsTrack limitStats;
    if (node == null) {
        // should not happen
        LOG.error("Missing limit node for quota {}", limitNode);
        return;
    }
    synchronized (node) {
        limitStats = new StatsTrack(node.data);
    }
    // check the quota
    boolean checkCountQuota = countDiff != 0 && (limitStats.getCount() > -1 || limitStats.getCountHardLimit() > -1);
    boolean checkByteQuota = bytesDiff != 0 && (limitStats.getBytes() > -1 || limitStats.getByteHardLimit() > -1);
    if (!checkCountQuota && !checkByteQuota) {
        return;
    }
    // check the statPath quota
    String statNode = Quotas.statPath(lastPrefix);
    node = getZKDatabase().getNode(statNode);
    StatsTrack currentStats;
    if (node == null) {
        // should not happen
        LOG.error("Missing node for stat {}", statNode);
        return;
    }
    synchronized (node) {
        currentStats = new StatsTrack(node.data);
    }
    // check the Count Quota
    if (checkCountQuota) {
        long newCount = currentStats.getCount() + countDiff;
        boolean isCountHardLimit = limitStats.getCountHardLimit() > -1;
        long countLimit = isCountHardLimit ? limitStats.getCountHardLimit() : limitStats.getCount();
        if (newCount > countLimit) {
            String msg = "Quota exceeded: " + lastPrefix + " [current count=" + newCount + ", " + (isCountHardLimit ? "hard" : "soft") + "CountLimit=" + countLimit + "]";
            RATE_LOGGER.rateLimitLog(msg);
            if (isCountHardLimit) {
                updateQuotaExceededMetrics(namespace);
                throw new KeeperException.QuotaExceededException(lastPrefix);
            }
        }
    }
    // check the Byte Quota
    if (checkByteQuota) {
        long newBytes = currentStats.getBytes() + bytesDiff;
        boolean isByteHardLimit = limitStats.getByteHardLimit() > -1;
        long byteLimit = isByteHardLimit ? limitStats.getByteHardLimit() : limitStats.getBytes();
        if (newBytes > byteLimit) {
            String msg = "Quota exceeded: " + lastPrefix + " [current bytes=" + newBytes + ", " + (isByteHardLimit ? "hard" : "soft") + "ByteLimit=" + byteLimit + "]";
            RATE_LOGGER.rateLimitLog(msg);
            if (isByteHardLimit) {
                updateQuotaExceededMetrics(namespace);
                throw new KeeperException.QuotaExceededException(lastPrefix);
            }
        }
    }
}
Also used : StatsTrack(org.apache.zookeeper.StatsTrack)

Example 34 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 quota the quotas to delete (set to 1), null to delete all
 * @return true if quota deletion is successful
 * @throws KeeperException
 * @throws MalformedPathException
 * @throws InterruptedException
 */
public static boolean delQuota(ZooKeeper zk, String path, StatsTrack quota) throws KeeperException, InterruptedException, MalformedPathException {
    String parentPath = Quotas.quotaPath(path);
    String quotaPath = Quotas.limitPath(path);
    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) {
        throw new KeeperException.NoNodeException(ne.getMessage());
    }
    StatsTrack strack = new StatsTrack(data);
    if (quota == null) {
        // 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);
    } else {
        if (quota.getCount() > 0) {
            strack.setCount(-1);
        }
        if (quota.getBytes() > 0) {
            strack.setBytes(-1L);
        }
        if (quota.getCountHardLimit() > 0) {
            strack.setCountHardLimit(-1);
        }
        if (quota.getByteHardLimit() > 0) {
            strack.setByteHardLimit(-1L);
        }
        zk.setData(quotaPath, strack.getStatsBytes(), -1);
    }
    return true;
}
Also used : Stat(org.apache.zookeeper.data.Stat) StatsTrack(org.apache.zookeeper.StatsTrack) KeeperException(org.apache.zookeeper.KeeperException)

Example 35 with StatsTrack

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

the class EnforceQuotaTest method testSetQuotaDisableWhenExceedBytesHardQuota.

@Test
public void testSetQuotaDisableWhenExceedBytesHardQuota() throws Exception {
    final String namespace = UUID.randomUUID().toString();
    final String path = "/" + namespace;
    zk.create(path, "12345".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    StatsTrack st = new StatsTrack();
    st.setByteHardLimit(5L);
    SetQuotaCommand.createQuota(zk, path, st);
    try {
        zk.setData(path, "123456".getBytes(), -1);
        ZooKeeperQuotaTest.validateNoQuotaExceededMetrics(namespace);
    } catch (KeeperException.QuotaExceededException e) {
        fail("should not throw Byte Quota Exceeded Exception when enforce quota disables");
    }
}
Also used : StatsTrack(org.apache.zookeeper.StatsTrack) KeeperException(org.apache.zookeeper.KeeperException) 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