ztzg commented on a change in pull request #1644:
URL: https://github.com/apache/zookeeper/pull/1644#discussion_r598152755
##########
File path:
zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerMetrics.java
##########
@@ -259,6 +260,8 @@ private ServerMetrics(MetricsProvider metricsProvider) {
WATCH_BYTES = metricsContext.getCounter("watch_bytes");
JVM_PAUSE_TIME = metricsContext.getSummary("jvm_pause_time_ms",
DetailLevel.ADVANCED);
+
+ QUOTA_EXCEEDED_ERROR_PER_NAMESPACE =
metricsContext.getSummarySet(QuotaMetricsUtils.QUOTA_EXCEEDED_ERROR_PER_NAMESPACE,
DetailLevel.BASIC);
Review comment:
I suppose you used a `SummarySet` because we are missing a `CounterSet`,
but this generates strange results with default endpoint as well as the
Prometheus one. `cnt` and `sum` are effectively duplicate, whereas `min`,
`max`, `avg` are just nonsensical:
```
zk_avg_ZOOKEEPER-4211_quota_exceeded_error_per_namespace 1.0
zk_min_ZOOKEEPER-4211_quota_exceeded_error_per_namespace 1
zk_max_ZOOKEEPER-4211_quota_exceeded_error_per_namespace 1
zk_cnt_ZOOKEEPER-4211_quota_exceeded_error_per_namespace 5
zk_sum_ZOOKEEPER-4211_quota_exceeded_error_per_namespace 5
```
##########
File path:
zookeeper-server/src/main/java/org/apache/zookeeper/server/util/QuotaMetricsUtils.java
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.util;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.zookeeper.Quotas;
+import org.apache.zookeeper.StatsTrack;
+import org.apache.zookeeper.common.PathUtils;
+import org.apache.zookeeper.server.DataNode;
+import org.apache.zookeeper.server.DataTree;
+
+public final class QuotaMetricsUtils {
+ public static final String QUOTA_COUNT_LIMIT_PER_NAMESPACE =
"quota_count_limit_per_namespace";
+ public static final String QUOTA_BYTES_LIMIT_PER_NAMESPACE =
"quota_bytes_limit_per_namespace";
+ public static final String QUOTA_COUNT_USAGE_PER_NAMESPACE =
"quota_count_usage_per_namespace";
+ public static final String QUOTA_BYTES_USAGE_PER_NAMESPACE =
"quota_bytes_usage_per_namespace";
+ public static final String QUOTA_EXCEEDED_ERROR_PER_NAMESPACE =
"quota_exceeded_error_per_namespace";
+
+ enum QUOTA_LIMIT_USAGE_METRIC_TYPE {QUOTA_COUNT_LIMIT, QUOTA_BYTES_LIMIT,
QUOTA_COUNT_USAGE, QUOTA_BYTES_USAGE}
+ static final String LIMIT_END_STRING = "/" + Quotas.limitNode;
+ static final String STATS_END_STRING = "/" + Quotas.statNode;
+
+ private QuotaMetricsUtils() {
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count limit
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes limit
+ *`
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE);
+ }
+
+ // traverse the quota subtree and read the quota limit or usage data
+ private static Map<String, Number> getQuotaLimitOrUsage(final DataTree
dataTree,
+ final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ final Map<String, Number> metricsMap = new ConcurrentHashMap<>();
+ if (dataTree != null) {
+ getQuotaLimitOrUsage(Quotas.quotaZookeeper, metricsMap, type,
dataTree);
+ }
+ return metricsMap;
+ }
Review comment:
Hmm… I understand that you are constrained by the current interface, but
it also means we iterate four times on the `/zookeeper/quota` tree. It also
"forces" the implementation to report unset (count or byte) quotas as `-1`, as
it cannot correlate limits and values without doing an even larger amount of
duplicate work.
##########
File path:
zookeeper-server/src/main/java/org/apache/zookeeper/server/util/QuotaMetricsUtils.java
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.util;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.zookeeper.Quotas;
+import org.apache.zookeeper.StatsTrack;
+import org.apache.zookeeper.common.PathUtils;
+import org.apache.zookeeper.server.DataNode;
+import org.apache.zookeeper.server.DataTree;
+
+public final class QuotaMetricsUtils {
+ public static final String QUOTA_COUNT_LIMIT_PER_NAMESPACE =
"quota_count_limit_per_namespace";
+ public static final String QUOTA_BYTES_LIMIT_PER_NAMESPACE =
"quota_bytes_limit_per_namespace";
+ public static final String QUOTA_COUNT_USAGE_PER_NAMESPACE =
"quota_count_usage_per_namespace";
+ public static final String QUOTA_BYTES_USAGE_PER_NAMESPACE =
"quota_bytes_usage_per_namespace";
+ public static final String QUOTA_EXCEEDED_ERROR_PER_NAMESPACE =
"quota_exceeded_error_per_namespace";
+
+ enum QUOTA_LIMIT_USAGE_METRIC_TYPE {QUOTA_COUNT_LIMIT, QUOTA_BYTES_LIMIT,
QUOTA_COUNT_USAGE, QUOTA_BYTES_USAGE}
+ static final String LIMIT_END_STRING = "/" + Quotas.limitNode;
+ static final String STATS_END_STRING = "/" + Quotas.statNode;
+
+ private QuotaMetricsUtils() {
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count limit
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes limit
+ *`
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE);
+ }
+
+ // traverse the quota subtree and read the quota limit or usage data
+ private static Map<String, Number> getQuotaLimitOrUsage(final DataTree
dataTree,
+ final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ final Map<String, Number> metricsMap = new ConcurrentHashMap<>();
+ if (dataTree != null) {
+ getQuotaLimitOrUsage(Quotas.quotaZookeeper, metricsMap, type,
dataTree);
+ }
+ return metricsMap;
+ }
+
+ static void getQuotaLimitOrUsage(final String path,
+ final Map<String, Number> metricsMap,
+ final QUOTA_LIMIT_USAGE_METRIC_TYPE type,
+ final DataTree dataTree) {
+ final DataNode node = dataTree.getNode(path);
+ if (node == null) {
+ return;
+ }
+ final Set<String> children = node.getChildren();
+ if (children.isEmpty()) {
+ if (shouldCollect(path, type)) {
+ collectQuotaLimitOrUsage(path, node, metricsMap, type);
+ }
+ return;
+ }
+ for (final String child : children) {
+ getQuotaLimitOrUsage(path + "/" + child, metricsMap, type,
dataTree);
+ }
+ }
+
+ static boolean shouldCollect(final String path, final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ return path.endsWith(LIMIT_END_STRING)
+ && (QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT == type ||
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT == type)
+ || path.endsWith(STATS_END_STRING)
+ && (QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE == type ||
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE == type);
+ }
+
+ 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;
+ }
+ if (node.getData() == null) {
+ return;
+ }
+ final StatsTrack statsTrack = new StatsTrack(node.getData());
+ switch (type) {
+ case QUOTA_COUNT_LIMIT:
+ metricsMap.put(namespace,
getQuotaLimit(statsTrack.getCountHardLimit(), statsTrack.getCount()));
+ break;
+ case QUOTA_BYTES_LIMIT:
+ metricsMap.put(namespace,
getQuotaLimit(statsTrack.getByteHardLimit(), statsTrack.getBytes()));
+ break;
+ case QUOTA_COUNT_USAGE:
+ metricsMap.put(namespace, statsTrack.getCount());
+ break;
+ case QUOTA_BYTES_USAGE:
+ metricsMap.put(namespace, statsTrack.getBytes());
+ break;
+ default:
+ }
+ }
+
+ // hard limit takes precedence if specified
+ static long getQuotaLimit(final long hardLimit, final long limit) {
+ return hardLimit > -1 ? hardLimit : limit;
+ }
Review comment:
This is another case of policy and mechanism getting mixed up; do we
really want to prevent people from monitoring their soft quota errors?
##########
File path:
zookeeper-server/src/main/java/org/apache/zookeeper/server/util/QuotaMetricsUtils.java
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.util;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.zookeeper.Quotas;
+import org.apache.zookeeper.StatsTrack;
+import org.apache.zookeeper.common.PathUtils;
+import org.apache.zookeeper.server.DataNode;
+import org.apache.zookeeper.server.DataTree;
+
+public final class QuotaMetricsUtils {
+ public static final String QUOTA_COUNT_LIMIT_PER_NAMESPACE =
"quota_count_limit_per_namespace";
+ public static final String QUOTA_BYTES_LIMIT_PER_NAMESPACE =
"quota_bytes_limit_per_namespace";
+ public static final String QUOTA_COUNT_USAGE_PER_NAMESPACE =
"quota_count_usage_per_namespace";
+ public static final String QUOTA_BYTES_USAGE_PER_NAMESPACE =
"quota_bytes_usage_per_namespace";
+ public static final String QUOTA_EXCEEDED_ERROR_PER_NAMESPACE =
"quota_exceeded_error_per_namespace";
+
+ enum QUOTA_LIMIT_USAGE_METRIC_TYPE {QUOTA_COUNT_LIMIT, QUOTA_BYTES_LIMIT,
QUOTA_COUNT_USAGE, QUOTA_BYTES_USAGE}
+ static final String LIMIT_END_STRING = "/" + Quotas.limitNode;
+ static final String STATS_END_STRING = "/" + Quotas.statNode;
+
+ private QuotaMetricsUtils() {
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count limit
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes limit
+ *`
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE);
+ }
+
+ // traverse the quota subtree and read the quota limit or usage data
+ private static Map<String, Number> getQuotaLimitOrUsage(final DataTree
dataTree,
+ final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ final Map<String, Number> metricsMap = new ConcurrentHashMap<>();
+ if (dataTree != null) {
+ getQuotaLimitOrUsage(Quotas.quotaZookeeper, metricsMap, type,
dataTree);
+ }
+ return metricsMap;
+ }
+
+ static void getQuotaLimitOrUsage(final String path,
+ final Map<String, Number> metricsMap,
+ final QUOTA_LIMIT_USAGE_METRIC_TYPE type,
+ final DataTree dataTree) {
+ final DataNode node = dataTree.getNode(path);
+ if (node == null) {
+ return;
+ }
+ final Set<String> children = node.getChildren();
+ if (children.isEmpty()) {
+ if (shouldCollect(path, type)) {
+ collectQuotaLimitOrUsage(path, node, metricsMap, type);
+ }
+ return;
+ }
+ for (final String child : children) {
+ getQuotaLimitOrUsage(path + "/" + child, metricsMap, type,
dataTree);
+ }
Review comment:
I believe this is illegal: the `Set<String>` returned by
`DataNode.getChildren()` is not synchronized, and other ZooKeeper operations
could be adding or removing nodes while you are iterating on it. You need
something like:
```java
synchronized(node) {
children = new ArrayList<>(node.getChildren())
}
```
@eolivelli: do you agree, or am I missing something?
##########
File path:
zookeeper-server/src/main/java/org/apache/zookeeper/server/util/QuotaMetricsUtils.java
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.util;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.zookeeper.Quotas;
+import org.apache.zookeeper.StatsTrack;
+import org.apache.zookeeper.common.PathUtils;
+import org.apache.zookeeper.server.DataNode;
+import org.apache.zookeeper.server.DataTree;
+
+public final class QuotaMetricsUtils {
+ public static final String QUOTA_COUNT_LIMIT_PER_NAMESPACE =
"quota_count_limit_per_namespace";
+ public static final String QUOTA_BYTES_LIMIT_PER_NAMESPACE =
"quota_bytes_limit_per_namespace";
+ public static final String QUOTA_COUNT_USAGE_PER_NAMESPACE =
"quota_count_usage_per_namespace";
+ public static final String QUOTA_BYTES_USAGE_PER_NAMESPACE =
"quota_bytes_usage_per_namespace";
+ public static final String QUOTA_EXCEEDED_ERROR_PER_NAMESPACE =
"quota_exceeded_error_per_namespace";
+
+ enum QUOTA_LIMIT_USAGE_METRIC_TYPE {QUOTA_COUNT_LIMIT, QUOTA_BYTES_LIMIT,
QUOTA_COUNT_USAGE, QUOTA_BYTES_USAGE}
+ static final String LIMIT_END_STRING = "/" + Quotas.limitNode;
+ static final String STATS_END_STRING = "/" + Quotas.statNode;
+
+ private QuotaMetricsUtils() {
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count limit
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes limit
+ *`
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE);
+ }
+
+ // traverse the quota subtree and read the quota limit or usage data
+ private static Map<String, Number> getQuotaLimitOrUsage(final DataTree
dataTree,
+ final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ final Map<String, Number> metricsMap = new ConcurrentHashMap<>();
+ if (dataTree != null) {
+ getQuotaLimitOrUsage(Quotas.quotaZookeeper, metricsMap, type,
dataTree);
+ }
+ return metricsMap;
+ }
+
+ static void getQuotaLimitOrUsage(final String path,
+ final Map<String, Number> metricsMap,
+ final QUOTA_LIMIT_USAGE_METRIC_TYPE type,
+ final DataTree dataTree) {
+ final DataNode node = dataTree.getNode(path);
+ if (node == null) {
+ return;
+ }
+ final Set<String> children = node.getChildren();
+ if (children.isEmpty()) {
+ if (shouldCollect(path, type)) {
+ collectQuotaLimitOrUsage(path, node, metricsMap, type);
+ }
+ return;
+ }
+ for (final String child : children) {
+ getQuotaLimitOrUsage(path + "/" + child, metricsMap, type,
dataTree);
+ }
+ }
+
+ static boolean shouldCollect(final String path, final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ return path.endsWith(LIMIT_END_STRING)
+ && (QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT == type ||
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT == type)
+ || path.endsWith(STATS_END_STRING)
+ && (QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE == type ||
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE == type);
+ }
+
+ 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;
+ }
+ if (node.getData() == null) {
+ return;
+ }
+ final StatsTrack statsTrack = new StatsTrack(node.getData());
Review comment:
I believe you want this:
```java
final byte[] data = node.getData();
if (data == null) { ... }
...
// use data
```
as unless I am missing something, the second call to `getData()` could
return `null` even if the first did not.
##########
File path:
zookeeper-server/src/main/java/org/apache/zookeeper/server/util/QuotaMetricsUtils.java
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.util;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.zookeeper.Quotas;
+import org.apache.zookeeper.StatsTrack;
+import org.apache.zookeeper.common.PathUtils;
+import org.apache.zookeeper.server.DataNode;
+import org.apache.zookeeper.server.DataTree;
+
+public final class QuotaMetricsUtils {
+ public static final String QUOTA_COUNT_LIMIT_PER_NAMESPACE =
"quota_count_limit_per_namespace";
+ public static final String QUOTA_BYTES_LIMIT_PER_NAMESPACE =
"quota_bytes_limit_per_namespace";
+ public static final String QUOTA_COUNT_USAGE_PER_NAMESPACE =
"quota_count_usage_per_namespace";
+ public static final String QUOTA_BYTES_USAGE_PER_NAMESPACE =
"quota_bytes_usage_per_namespace";
+ public static final String QUOTA_EXCEEDED_ERROR_PER_NAMESPACE =
"quota_exceeded_error_per_namespace";
+
+ enum QUOTA_LIMIT_USAGE_METRIC_TYPE {QUOTA_COUNT_LIMIT, QUOTA_BYTES_LIMIT,
QUOTA_COUNT_USAGE, QUOTA_BYTES_USAGE}
+ static final String LIMIT_END_STRING = "/" + Quotas.limitNode;
+ static final String STATS_END_STRING = "/" + Quotas.statNode;
+
+ private QuotaMetricsUtils() {
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count limit
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes limit
+ *`
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes limit as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesLimit(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota count usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota count usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaCountUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE);
+ }
+
+ /**
+ * Traverse the quota subtree and return per namespace quota bytes usage
+ *
+ * @param dataTree dataTree that contains the quota limit and usage data
+ * @return a map with top namespace as the key and quota bytes usage as
the value
+ *
+ */
+ public static Map<String, Number> getQuotaBytesUsage(final DataTree
dataTree) {
+ return getQuotaLimitOrUsage(dataTree,
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE);
+ }
+
+ // traverse the quota subtree and read the quota limit or usage data
+ private static Map<String, Number> getQuotaLimitOrUsage(final DataTree
dataTree,
+ final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ final Map<String, Number> metricsMap = new ConcurrentHashMap<>();
+ if (dataTree != null) {
+ getQuotaLimitOrUsage(Quotas.quotaZookeeper, metricsMap, type,
dataTree);
+ }
+ return metricsMap;
+ }
+
+ static void getQuotaLimitOrUsage(final String path,
+ final Map<String, Number> metricsMap,
+ final QUOTA_LIMIT_USAGE_METRIC_TYPE type,
+ final DataTree dataTree) {
+ final DataNode node = dataTree.getNode(path);
+ if (node == null) {
+ return;
+ }
+ final Set<String> children = node.getChildren();
+ if (children.isEmpty()) {
+ if (shouldCollect(path, type)) {
+ collectQuotaLimitOrUsage(path, node, metricsMap, type);
+ }
+ return;
+ }
+ for (final String child : children) {
+ getQuotaLimitOrUsage(path + "/" + child, metricsMap, type,
dataTree);
+ }
+ }
+
+ static boolean shouldCollect(final String path, final
QUOTA_LIMIT_USAGE_METRIC_TYPE type) {
+ return path.endsWith(LIMIT_END_STRING)
+ && (QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_LIMIT == type ||
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_LIMIT == type)
+ || path.endsWith(STATS_END_STRING)
+ && (QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_COUNT_USAGE == type ||
QUOTA_LIMIT_USAGE_METRIC_TYPE.QUOTA_BYTES_USAGE == type);
+ }
+
+ 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;
+ }
+ if (node.getData() == null) {
+ return;
+ }
+ final StatsTrack statsTrack = new StatsTrack(node.getData());
+ switch (type) {
+ case QUOTA_COUNT_LIMIT:
+ metricsMap.put(namespace,
getQuotaLimit(statsTrack.getCountHardLimit(), statsTrack.getCount()));
+ break;
+ case QUOTA_BYTES_LIMIT:
+ metricsMap.put(namespace,
getQuotaLimit(statsTrack.getByteHardLimit(), statsTrack.getBytes()));
+ break;
+ case QUOTA_COUNT_USAGE:
+ metricsMap.put(namespace, statsTrack.getCount());
+ break;
+ case QUOTA_BYTES_USAGE:
+ metricsMap.put(namespace, statsTrack.getBytes());
+ break;
Review comment:
There is a mismatch, in general, between the notions of quota path
(which cover subtrees, potentially deep in the hierarchy) and that of "top
namespace." I know you did not introduce the latter, and I understand that
splitting just below the root is convenient in most deployments—but that
mismatch is bound to rear its ugly head at the most inopportune moments :)
The above suffers from it, as:
```
create /ZOOKEEPER-4211
create /ZOOKEEPER-4211/a
create /ZOOKEEPER-4211/a/b
setquota -B 32 /ZOOKEEPER-4211/a/b
create /ZOOKEEPER-4211/a/z
setquota -B 128 /ZOOKEEPER-4211/a/z
set /ZOOKEEPER-4211/a/b 0123456789012345678901234567890123456789
```
causes the `/a/z` subtree to "cover" `/a/b` and report the wrong results:
```
quota_bytes_limit_per_namespace{key="ZOOKEEPER-4211",} 128.0
quota_bytes_usage_per_namespace{key="ZOOKEEPER-4211",} 0.0
```
I would suggest *summing* the values found in multiple subtrees of a given
"namespace."
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]