You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by "Shawyeok (via GitHub)" <gi...@apache.org> on 2023/11/01 18:30:33 UTC
[PR] ZOOKEEPER-4741: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Shawyeok opened a new pull request, #2086:
URL: https://github.com/apache/zookeeper/pull/2086
If the built-in Prometheus metrics feature introduced after version 3.6 is enabled, under high-load scenarios (such as when there are a large number of read requests), the percentile metrics (Summary) used to collect request latencies can easily become a bottleneck and impact the service itself. This is because the internal implementation of Summary involves the overhead of lock operations. In scenarios with a large number of requests, lock contention can lead to a dramatic deterioration in request latency. The details of this issue and related profiling can be viewed in ZOOKEEPER-4741.
In ZOOKEEPER-4289, the updates to Summary were switched to be executed in a separate thread pool. While this approach avoids the overhead of lock contention caused by multiple threads updating Summary simultaneously, it introduces the operational overhead of the thread pool queue and additional garbage collection (GC) overhead. Especially when the thread pool queue is full, a large number of RejectedExecutionException instances will be thrown, further increasing the pressure on GC.
To address problems above, I have implemented an almost lock-free solution based on DataSketches. Benchmark results show that it offers over a 10x speed improvement compared to version 3.9.1 and avoids frequent GC caused by creating a large number of temporary objects. The trade-off is that the latency percentiles will be displayed with a relative delay (default is 60 seconds), and each Summary metric will have a certain amount of permanent memory overhead.
This solution refers to Matteo Merli's optimization work on the percentile latency metrics for Bookkeeper, as detailed in https://github.com/apache/bookkeeper/commit/3bff19956e70e37c025a8e29aa8428937af77aa1.
[Benchmark with 3.9.1][1]
Benchmark | (detailLevel) | Mode | Cnt | Score | Error | Units
-- | -- | -- | -- | -- | -- | --
PrometheusMetricsProviderBenchmark.summaryAdd | BASIC | thrpt | 3 | 1.128 | ± 1.305 | ops/us
PrometheusMetricsProviderBenchmark.summaryAdd | ADVANCED | thrpt | 3 | 0.972 | ± 0.755 | ops/us
[Benchmark with new implementation][2]
Benchmark | (detailLevel) | Mode | Cnt | Score | Error | Units
-- | -- | -- | -- | -- | -- | --
PrometheusMetricsProviderBenchmark.summaryAdd | BASIC | thrpt | 3 | 16.016 | ± 0.571 | ops/us
PrometheusMetricsProviderBenchmark.summaryAdd | ADVANCED | thrpt | 3 | 15.360 | ± 4.103 | ops/us
[1]: https://github.com/Shawyeok/day290_zkMetricsBenchmark/tree/master
[2]: https://github.com/Shawyeok/day290_zkMetricsBenchmark/tree/datasketches-summary
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-1853223616
> Sorry didn't get chance to get to this until today. Thanks for contributing to this @Shawyeok . I like the idea of fixing the underneath Prometheus quantile computation issue in the prometheus client library. Can you provide some details on the trade off this patch?
>
> > latency percentiles will be displayed with a relative delay (default is 60 seconds)
>
> 1. Where does the latency come from? Is it possible to avoid it or reduce it?
>
> > each Summary metric will have a certain amount of permanent memory overhead
>
> 2. Do you mean it takes more heap space permanently? How much is the overhead?
It would be better to answer both these questions together.
Here is the design overview:
- Each thread maintains a local buffer (ThreadLocal) to record metrics.
- A background thread aggregates all these local buffers and computes the quantile results periodically.
Therefore, the latency of monitoring depends on how frequently the background thread aggregates buffers scattered across various threads. This interval is set to 60 seconds by default and is configurable. Currently, it seems to work well in Bookkeeper.
The heap space overhead refers to each thread's local buffer. According to the [DoublesSketch][1] documentation, with the default configuration of k (128, approximating an error of about 1.725%), if a latency metric records 65K data points in 60 seconds, each metric will occupy about 2 x 10KB of heap space in its respective thread (the x2 is due to the current/replacement swap strategy). In a real production environment, we observed an instance handling an average of 60K read requests and 600 write requests per second (8 cores with default configuration), where the memory usage for the buffers stayed below 1MB.
[1]: https://github.com/apache/datasketches-java/blob/master/src/main/java/org/apache/datasketches/quantiles/DoublesSketch.java
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1400658972
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -483,79 +487,70 @@ private void unregister() {
}
}
- private class PrometheusSummary implements Summary {
+ // VisibleForTesting
+ class PrometheusSummary implements Summary {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ SketchesSummary inner;
private final String name;
public PrometheusSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median)
+ .quantile(0.9) // Add 90th percentile
+ .quantile(0.99) // Add 99th percentile
.register(collectorRegistry);
} else {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median) with 5% tolerated error
.register(collectorRegistry);
}
}
@Override
public void add(long delta) {
- reportMetrics(() -> observe(delta));
- }
-
- private void observe(final long delta) {
try {
inner.observe(delta);
- } catch (final IllegalArgumentException err) {
+ } catch (IllegalArgumentException err) {
LOG.error("invalid delta {} for metric {}", delta, name, err);
}
}
}
- private class PrometheusLabelledSummary implements SummarySet {
+ // VisibleForTesting
+ class PrometheusLabelledSummary implements SummarySet {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ final SketchesSummary inner;
private final String name;
public PrometheusLabelledSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
+ this.inner = SketchesSummary.build(name, name)
.labelNames(LABELS)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ .quantile(0.5) // Add 50th percentile (= median)
Review Comment:
What is the tolerated error part mapped to in the new implementation?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894518732
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.StampedLock;
+import org.apache.datasketches.quantiles.DoublesSketch;
+import org.apache.datasketches.quantiles.DoublesUnion;
+import org.apache.datasketches.quantiles.DoublesUnionBuilder;
+
+/**
+ * A DataSketches based Summary metric, aim for lock-free and low gc overhead.
+ */
+public class SketchesSummary extends SimpleCollector<SketchesSummary.Child> implements Counter.Describable {
+
+ final List<Double> quantiles;
+
+ SketchesSummary(SketchesSummary.Builder b) {
+ super(b);
+ this.quantiles = b.quantiles;
+ initializeNoLabelsChild();
+ }
+
+ public static class Builder extends SimpleCollector.Builder<Builder, SketchesSummary> {
+
+ private final List<Double> quantiles = new ArrayList<>();
+
+ public Builder quantile(double quantile) {
+ if (quantile < 0.0 || quantile > 1.0) {
+ throw new IllegalArgumentException("Quantile " + quantile
+ + " invalid: Expected number between 0.0 and 1.0.");
+ }
+ quantiles.add(quantile);
+ return this;
+ }
+
+ @Override
+ public SketchesSummary create() {
+ for (String label : labelNames) {
+ if (label.equals("quantile")) {
+ throw new IllegalStateException("Summary cannot have a label named 'quantile'.");
+ }
+ }
+ dontInitializeNoLabelsChild = true;
+ return new SketchesSummary(this);
+ }
+ }
+
+ /**
+ * Return a Builder to allow configuration of a new Summary. Ensures required fields are provided.
+ *
+ * @param name The name of the metric
+ * @param help The help string of the metric
+ */
+ public static Builder build(String name, String help) {
+ return new Builder().name(name).help(help);
+ }
+
+ /**
+ * Return a Builder to allow configuration of a new Summary.
+ */
+ public static Builder build() {
+ return new Builder();
+ }
+
+ @Override
+ protected Child newChild() {
+ return new Child(quantiles);
+ }
+
+ /**
+ * The value of a single Summary.
+ *
+ * <p>* <em>Warning:</em> References to a Child become invalid after using
+ * {@link SimpleCollector#remove} or {@link SimpleCollector#clear}.
+ */
+ public static class Child {
+
+ public static class Value {
+ public final double count;
+ public final double sum;
+ public final SortedMap<Double, Double> quantiles;
+
+ private Value(double count, double sum, List<Double> quantiles, DoublesSketch doublesSketch) {
+ this.count = count;
+ this.sum = sum;
+ this.quantiles = Collections.unmodifiableSortedMap(snapshot(quantiles, doublesSketch));
+ }
+
+ private SortedMap<Double, Double> snapshot(List<Double> quantiles, DoublesSketch doublesSketch) {
+ SortedMap<Double, Double> result = new TreeMap<Double, Double>();
+ for (Double q : quantiles) {
+ result.put(q, doublesSketch != null && !doublesSketch.isEmpty() ? doublesSketch.getQuantile(q)
+ : Double.NaN);
+ }
+ return result;
+ }
+ }
+
+ // Having these separate leaves us open to races,
+ // however Prometheus as whole has other races
+ // that mean adding atomicity here wouldn't be useful.
+ // This should be reevaluated in the future.
+ private final DoubleAdder count = new DoubleAdder();
+ private final DoubleAdder sum = new DoubleAdder();
+ private final List<Double> quantiles;
+ /*
+ * Use 2 rotating thread local accessor so that we can safely swap them.
+ */
+ private volatile ThreadLocalAccessor current;
+ private volatile ThreadLocalAccessor replacement;
+ private volatile DoublesSketch result;
+
+ private Child(List<Double> quantiles) {
+ this.quantiles = quantiles;
+ this.current = new ThreadLocalAccessor();
+ this.replacement = new ThreadLocalAccessor();
+ }
+
+ /**
+ * Observe the given amount.
+ */
+ public void observe(double amt) {
+ count.add(1);
+ sum.add(amt);
+ LocalSketch localSketch = current.localData.get();
+ long stamp = localSketch.lock.readLock();
+ try {
+ localSketch.sketch.update(amt);
+ } finally {
+ localSketch.lock.unlockRead(stamp);
+ }
+ }
+
+ public void rotate() {
Review Comment:
Documentation on the rotate() API?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894516271
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml:
##########
@@ -33,6 +33,7 @@
<properties>
<prometheus.version>0.9.0</prometheus.version>
+ <datasketches.version>4.2.0</datasketches.version>
Review Comment:
Do you know if the lock contention issue is fixed in the latest prometheus client library?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894515883
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -91,6 +91,11 @@ public class PrometheusMetricsProvider implements MetricsProvider {
*/
static final String WORKER_SHUTDOWN_TIMEOUT_MS = "workerShutdownTimeoutMs";
+ /**
+ * The interval in seconds for Prometheus summary metrics rotation. Default value is 60.
+ */
+ static final String PROMETHEUS_SUMMARY_ROTATE_SECONDS = "prometheusMetricsSummaryRotateSeconds";
Review Comment:
```suggestion
static final String PROMETHEUS_SUMMARY_ROTATE_INTERVAL_SECONDS = "prometheusMetricsSummaryRotateIntervalSeconds";
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "anmolnar (via GitHub)" <gi...@apache.org>.
anmolnar commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4927527412
> > Please let me know your jira id, so that I could assign the ticket to you.
>
> @anmolnar It's `shawyeok`, thanks.
It's done. You're already a ZK contributor, feel free to assign Jiras to yourself. Thanks again!
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "anmolnar (via GitHub)" <gi...@apache.org>.
anmolnar commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4481903261
@Shawyeok Please rebase your patch.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1566969943
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml:
##########
@@ -59,6 +59,11 @@
<artifactId>simpleclient_servlet</artifactId>
<version>${prometheus.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.datasketches</groupId>
+ <artifactId>datasketches-java</artifactId>
+ <version>4.2.0</version>
Review Comment:
done, extracted a property `datasketches.version`
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-1853158516
Instead of completely silently dropping the rejected tasks, we can also create a `PrometheusRejectedExecutionHandler` that logs a WARN if the queue is full to help tuning the queue size. This WARN is only logged once and the condition check is very light weight, so the GC impact should be small.
```
private static class PrometheusRejectedExecutionHandler implements RejectedExecutionHandler {
private static boolean maxQueueSizeExceeded = false;
@Override
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor e) {
if (!maxQueueSizeExceeded) {
maxQueueSizeExceeded = true;
LOG.warn("Prometheus metrics queue size exceeded the max");
}
}
}
```
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1896632109
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -91,6 +91,11 @@ public class PrometheusMetricsProvider implements MetricsProvider {
*/
static final String WORKER_SHUTDOWN_TIMEOUT_MS = "workerShutdownTimeoutMs";
+ /**
+ * The interval in seconds for Prometheus summary metrics rotation. Default value is 60.
+ */
+ static final String PROMETHEUS_SUMMARY_ROTATE_SECONDS = "prometheusMetricsSummaryRotateSeconds";
Review Comment:
updated
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894517142
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
Review Comment:
It would be nice if we don't need to put `SketchesSummary` in the io.prometheus.client package.
Have you thought about submitting the patch to the prometheus client library and have the issue fixed there?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894517142
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
Review Comment:
It would be nice if we don't need to put `SketchesSummary` in the io.prometheus.client package.
Have you thought about submitting the patch to the prometheus client library and fixing the issue there, so the fix will be leverage to all users?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "hezhangjian (via GitHub)" <gi...@apache.org>.
hezhangjian commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4931003714
Good Job!
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1402063487
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -483,79 +487,70 @@ private void unregister() {
}
}
- private class PrometheusSummary implements Summary {
+ // VisibleForTesting
+ class PrometheusSummary implements Summary {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ SketchesSummary inner;
private final String name;
public PrometheusSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median)
+ .quantile(0.9) // Add 90th percentile
+ .quantile(0.99) // Add 99th percentile
.register(collectorRegistry);
} else {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median) with 5% tolerated error
.register(collectorRegistry);
}
}
@Override
public void add(long delta) {
- reportMetrics(() -> observe(delta));
- }
-
- private void observe(final long delta) {
try {
inner.observe(delta);
- } catch (final IllegalArgumentException err) {
+ } catch (IllegalArgumentException err) {
LOG.error("invalid delta {} for metric {}", delta, name, err);
}
}
}
- private class PrometheusLabelledSummary implements SummarySet {
+ // VisibleForTesting
+ class PrometheusLabelledSummary implements SummarySet {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ final SketchesSummary inner;
private final String name;
public PrometheusLabelledSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
+ this.inner = SketchesSummary.build(name, name)
.labelNames(LABELS)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ .quantile(0.5) // Add 50th percentile (= median)
Review Comment:
About `1.7%`, it's depends on _k_ param in [DoublesSketch][1] which default is 128.
[1]: https://github.com/apache/datasketches-java/blob/007f35b59ec8b7f56fbfdf4145af2974be3c06f0/src/main/java/org/apache/datasketches/quantiles/DoublesSketch.java#L51
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1402081996
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -77,12 +73,14 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* Default value is 1.
* If the number is less than 1, the main thread will be used.
*/
+ @Deprecated
Review Comment:
Addressed, feel free to take a look again, thanks.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1400657176
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -99,35 +102,34 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* </p>
*/
private final CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry;
- private final RateLogger rateLogger = new RateLogger(LOG, 60 * 1000);
private String host = "0.0.0.0";
private int port = 7000;
private boolean exportJvmInfo = true;
private Server server;
private final MetricsServletImpl servlet = new MetricsServletImpl();
private final Context rootContext = new Context();
- private int numWorkerThreads = 1;
- private int maxQueueSize = 1000000;
private long workerShutdownTimeoutMs = 1000;
- private Optional<ExecutorService> executorOptional = Optional.empty();
+ private int summaryRotateSeconds = 60;
+ private ScheduledExecutorService executorService;
@Override
public void configure(Properties configuration) throws MetricsProviderLifeCycleException {
LOG.info("Initializing metrics, configuration: {}", configuration);
this.host = configuration.getProperty("httpHost", "0.0.0.0");
this.port = Integer.parseInt(configuration.getProperty("httpPort", "7000"));
this.exportJvmInfo = Boolean.parseBoolean(configuration.getProperty("exportJvmInfo", "true"));
- this.numWorkerThreads = Integer.parseInt(
- configuration.getProperty(NUM_WORKER_THREADS, "1"));
- this.maxQueueSize = Integer.parseInt(
- configuration.getProperty(MAX_QUEUE_SIZE, "1000000"));
+ if (configuration.containsKey(NUM_WORKER_THREADS) || configuration.containsKey(MAX_QUEUE_SIZE)) {
+ LOG.warn("The configuration {} and {} are deprecated, it is ignored. See details in ZOOKEEPER-4741",
Review Comment:
OK I see that these two options are ignored. Please add to the field comment also.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "DCjanus (via GitHub)" <gi...@apache.org>.
DCjanus commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4341647410
This PR has been inactive for a while, but I want to leave one data point.
We recently hit the same Prometheus Summary performance issue in our internal ZooKeeper 3.9.2-based fork. We backported the approach from this PR, and it significantly improved our benchmark results. Even unmerged, this PR was very useful for us.
One thing worth mentioning is the default 60s rotation interval. In our fork, we benchmarked the same implementation with 60s, 5s, and 1s intervals under the same high-read workload. We did not see a significant performance difference. With a 1s interval, the rotate path was still only `0.12%` in the leader CPU flame graph.
From an operational perspective, 60s of quantile lag is quite visible in dashboards and incident debugging, while 1s seems acceptable for most practical cases. This might be useful if this PR is picked up again, or if someone tries to move a similar DataSketches-based Summary implementation into the Prometheus Java Client.
Thanks again to the PR author and the reviewers for the work and discussion here. It helped us a lot.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4914168736
retest this please
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-1853151200
We also looked into how to enable Prometheus metrics in production and did quite a lot perf tests and profiling recently. The
1. The metrics queue size is 1M by default it can be tuned. 1M of queue size seems too large. We reduced the queue size from 1M to 100K, the max GC pause was reduced 78% and the GC counts was reduced 80%
2. We also noticed that when the thread pool queue is full, a large number of RejectedExecutionException instances was created, which added more GC overhead. This is because `ThreadPoolExecutor` uses `AbortPolicy` as the `RejectedExecutionHandler`. AbortPolicy instantiates RejectedExecutionException object and makes two quite involved `toString` calls.
```
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
```
3. We created patch that uses the `DiscardPolicy` instead of `AbortPolicy`, which silently drop the rejected task instead of throwing `RejectedExecutionException`. With the patch, the max GC paused was reduced further about 7% and GC counts was reduced about 61% for 100K queue size. As a result, the latency of read operation was reduced 59% and throughput increased 140% .
2.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1402063487
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -483,79 +487,70 @@ private void unregister() {
}
}
- private class PrometheusSummary implements Summary {
+ // VisibleForTesting
+ class PrometheusSummary implements Summary {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ SketchesSummary inner;
private final String name;
public PrometheusSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median)
+ .quantile(0.9) // Add 90th percentile
+ .quantile(0.99) // Add 99th percentile
.register(collectorRegistry);
} else {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median) with 5% tolerated error
.register(collectorRegistry);
}
}
@Override
public void add(long delta) {
- reportMetrics(() -> observe(delta));
- }
-
- private void observe(final long delta) {
try {
inner.observe(delta);
- } catch (final IllegalArgumentException err) {
+ } catch (IllegalArgumentException err) {
LOG.error("invalid delta {} for metric {}", delta, name, err);
}
}
}
- private class PrometheusLabelledSummary implements SummarySet {
+ // VisibleForTesting
+ class PrometheusLabelledSummary implements SummarySet {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ final SketchesSummary inner;
private final String name;
public PrometheusLabelledSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
+ this.inner = SketchesSummary.build(name, name)
.labelNames(LABELS)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ .quantile(0.5) // Add 50th percentile (= median)
Review Comment:
About `1.7%`, it's depends on _k_ param in [DoublesSketch][1] which default is 128.
I've write an unit test `SketchesSummaryTest#testQuantileError` here to make sure the calculation error is in the range of `1.7%`.
[1]: https://github.com/apache/datasketches-java/blob/007f35b59ec8b7f56fbfdf4145af2974be3c06f0/src/main/java/org/apache/datasketches/quantiles/DoublesSketch.java#L51
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894509471
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -91,6 +91,11 @@ public class PrometheusMetricsProvider implements MetricsProvider {
*/
static final String WORKER_SHUTDOWN_TIMEOUT_MS = "workerShutdownTimeoutMs";
+ /**
+ * The interval in seconds for Prometheus summary metrics rotation. Default value is 60.
+ */
+ static final String PROMETHEUS_SUMMARY_ROTATE_SECONDS = "prometheusMetricsSummaryRotateSeconds";
Review Comment:
Can you update Zookeeper admin doc to include the property? https://zookeeper.apache.org/doc/r3.9.3/zookeeperAdmin.html#Debug+Log+Cleanup+Logback
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894509680
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -77,12 +73,14 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* Default value is 1.
* If the number is less than 1, the main thread will be used.
*/
+ @Deprecated
Review Comment:
Please update the admin doc for the deprecation.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "anmolnar (via GitHub)" <gi...@apache.org>.
anmolnar merged PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1400663413
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
Review Comment:
Why do we have to locate `SketchesSummary` in this package?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1400944637
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
Review Comment:
Since my intention here is not to implement a new type of Summary from scratch, but rather to extend the capabilities of SimpleCollector (such as metric definition, child management, and initialization) by inheriting from it. The `SketchesSummary.Builder` class needs access to some `package-private` fields in `io.prometheus.client.SimpleCollector.Builder` class, such as `labelNames` and `dontInitializeNoLabelsChild`.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-1853233745
> The metrics queue size is 1M by default it can be tuned. 1M of queue size seems too large. We reduced the queue size from 1M to 100K, the max GC pause was reduced 78% and the GC counts was reduced 80%
That's cool!
However, in my opinion, tuning the configuration for this queue size could be quite challenging for users, especially if they don't understand the working principle of the queue. It's hard to define a good value for this parameter. What do you think?
> Instead of completely silently dropping the rejected tasks, we can create a PrometheusRejectedExecutionHandler that logs a WARN if the queue is full to help tuning the queue size.
When the queue is full, some data points will be discarded, which might lead to a bias in the monitoring results.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-2063850364
@tisonkun @shoothzj ci needs a approval, thanks.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1896631818
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
Review Comment:
> Have you thought about submitting the patch to the Prometheus client library and fixing the issue there, so the fix will be leveraged by all users?
That's a great idea.
Since the sketch-based implementation introduces a delay in quantile values, it might not be acceptable for certain user scenarios. Btw, it's definitely worth discussing this approach with the maintainers of the Prometheus client library.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "anmolnar (via GitHub)" <gi...@apache.org>.
anmolnar commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4915582167
CI build was successful, but the script was unable to access hadoop16 node to report it back. I'll merge the patch.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1400654747
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -77,12 +73,14 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* Default value is 1.
* If the number is less than 1, the main thread will be used.
*/
+ @Deprecated
Review Comment:
Please add a comment (`@deprecated`) for why it's deprecated and/or what should be used instead.
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -77,12 +73,14 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* Default value is 1.
* If the number is less than 1, the main thread will be used.
*/
+ @Deprecated
static final String NUM_WORKER_THREADS = "numWorkerThreads";
/**
* The max queue size for Prometheus summary metrics reporting task.
* Default value is 1000000.
*/
+ @Deprecated
Review Comment:
Please add a comment (`@deprecated`) for why it's deprecated and/or what should be used instead.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894509743
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -77,12 +73,14 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* Default value is 1.
* If the number is less than 1, the main thread will be used.
*/
+ @Deprecated
static final String NUM_WORKER_THREADS = "numWorkerThreads";
/**
* The max queue size for Prometheus summary metrics reporting task.
* Default value is 1000000.
*/
+ @Deprecated
Review Comment:
Please update the admin doc for the deprecation.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1896624968
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -77,12 +73,14 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* Default value is 1.
* If the number is less than 1, the main thread will be used.
*/
+ @Deprecated
Review Comment:
updated
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -77,12 +73,14 @@ public class PrometheusMetricsProvider implements MetricsProvider {
* Default value is 1.
* If the number is less than 1, the main thread will be used.
*/
+ @Deprecated
static final String NUM_WORKER_THREADS = "numWorkerThreads";
/**
* The max queue size for Prometheus summary metrics reporting task.
* Default value is 1000000.
*/
+ @Deprecated
Review Comment:
updated
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1896616814
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml:
##########
@@ -33,6 +33,7 @@
<properties>
<prometheus.version>0.9.0</prometheus.version>
+ <datasketches.version>4.2.0</datasketches.version>
Review Comment:
@li4wang
I've setup a benchmark for `Summary#observe` performance between versions `0.16.0` and `1.3.5` (latest) of the Prometheus Java client library. The result shows minimal improvement:
```
Benchmark (detailLevel) (version) Mode Cnt Score Error Units
SummaryBenchmark.summaryObserve BASIC 1.x thrpt 3 1.027 ± 0.446 ops/us
SummaryBenchmark.summaryObserve BASIC 0.x thrpt 3 1.041 ± 0.843 ops/us
SummaryBenchmark.summaryObserve ADVANCED 1.x thrpt 3 1.009 ± 0.671 ops/us
SummaryBenchmark.summaryObserve ADVANCED 0.x thrpt 3 0.967 ± 0.979 ops/us
```
Full result: https://gist.github.com/Shawyeok/4a7ef034baa187b27a9321145d2f05c9
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-2560960328
@li4wang
Thanks for reviewing, I did some doc updates you mentioned, please feel free to take a look.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4921335024
> Please let me know your jira id, so that I could assign the ticket to you.
@anmolnar It's `shawyeok`, thanks.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "anmolnar (via GitHub)" <gi...@apache.org>.
anmolnar commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-4915595165
Merged to master branch. Thanks @Shawyeok !
Please let me know your jira id, so that I could assign the ticket to you.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-1853119385
Sorry didn't get chance to get to this until today. Thanks for contributing to this @Shawyeok . I like the idea of fixing the underneath Prometheus quantile computation issue in the prometheus client library. Can you provide some details on the trade off this patch?
> latency percentiles will be displayed with a relative delay (default is 60 seconds)
Where does the latency come from? Is it possible to avoid it or reduce it?
> each Summary metric will have a certain amount of permanent memory overhead
Do you mean it takes more heap space permanently? How much is the overhead?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1400658972
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java:
##########
@@ -483,79 +487,70 @@ private void unregister() {
}
}
- private class PrometheusSummary implements Summary {
+ // VisibleForTesting
+ class PrometheusSummary implements Summary {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ SketchesSummary inner;
private final String name;
public PrometheusSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median)
+ .quantile(0.9) // Add 90th percentile
+ .quantile(0.99) // Add 99th percentile
.register(collectorRegistry);
} else {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
+ this.inner = SketchesSummary.build(name, name)
+ .quantile(0.5) // Add 50th percentile (= median) with 5% tolerated error
.register(collectorRegistry);
}
}
@Override
public void add(long delta) {
- reportMetrics(() -> observe(delta));
- }
-
- private void observe(final long delta) {
try {
inner.observe(delta);
- } catch (final IllegalArgumentException err) {
+ } catch (IllegalArgumentException err) {
LOG.error("invalid delta {} for metric {}", delta, name, err);
}
}
}
- private class PrometheusLabelledSummary implements SummarySet {
+ // VisibleForTesting
+ class PrometheusLabelledSummary implements SummarySet {
- private final io.prometheus.client.Summary inner;
+ // VisibleForTesting
+ final SketchesSummary inner;
private final String name;
public PrometheusLabelledSummary(String name, MetricsContext.DetailLevel level) {
this.name = name;
if (level == MetricsContext.DetailLevel.ADVANCED) {
- this.inner = io.prometheus.client.Summary
- .build(name, name)
+ this.inner = SketchesSummary.build(name, name)
.labelNames(LABELS)
- .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
- .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
- .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
+ .quantile(0.5) // Add 50th percentile (= median)
Review Comment:
What is the tolerated error part in the new implementation?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "moscicky (via GitHub)" <gi...@apache.org>.
moscicky commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-2058454827
@Shawyeok looking forward to this PR. We have observed significant performance impact with `PrometheusMetricsProvider` enabled.
For high load scenarios with versions 3.7.0 and 3.8.4 read and update latency is drastically increased. Profiling data shows that 30-40% of CPU is spent on Prometheus related computations and GC activity is greatly increased.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "shoothzj (via GitHub)" <gi...@apache.org>.
shoothzj commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1566927978
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml:
##########
@@ -59,6 +59,11 @@
<artifactId>simpleclient_servlet</artifactId>
<version>${prometheus.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.datasketches</groupId>
+ <artifactId>datasketches-java</artifactId>
+ <version>4.2.0</version>
Review Comment:
should we extract version to `pom.xml`?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#issuecomment-2058486392
@eolivelli @anmolnar @symat Please take a look.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "li4wang (via GitHub)" <gi...@apache.org>.
li4wang commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1894517142
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
Review Comment:
It would be nice if we don't need to put `SketchesSummary` in the io.prometheus.client package.
Have you thought about submit the patch to the prometheus client library and have the issue fixed there?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1896616814
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml:
##########
@@ -33,6 +33,7 @@
<properties>
<prometheus.version>0.9.0</prometheus.version>
+ <datasketches.version>4.2.0</datasketches.version>
Review Comment:
@li4wang
I conducted a benchmark for `Summary#observe` performance between versions `0.16.0` and `1.3.5` (latest) of the Prometheus Java client library. The result shows minimal improvement:
```
Benchmark (detailLevel) (version) Mode Cnt Score Error Units
SummaryBenchmark.summaryObserve BASIC 1.x thrpt 3 1.027 ± 0.446 ops/us
SummaryBenchmark.summaryObserve BASIC 0.x thrpt 3 1.041 ± 0.843 ops/us
SummaryBenchmark.summaryObserve ADVANCED 1.x thrpt 3 1.009 ± 0.671 ops/us
SummaryBenchmark.summaryObserve ADVANCED 0.x thrpt 3 0.967 ± 0.979 ops/us
```
Full result: https://gist.github.com/Shawyeok/4a7ef034baa187b27a9321145d2f05c9
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] ZOOKEEPER-4767: New implementation of prometheus quantile metrics based on DataSketches [zookeeper]
Posted by "Shawyeok (via GitHub)" <gi...@apache.org>.
Shawyeok commented on code in PR #2086:
URL: https://github.com/apache/zookeeper/pull/2086#discussion_r1896617048
##########
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/io/prometheus/client/SketchesSummary.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 io.prometheus.client;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.StampedLock;
+import org.apache.datasketches.quantiles.DoublesSketch;
+import org.apache.datasketches.quantiles.DoublesUnion;
+import org.apache.datasketches.quantiles.DoublesUnionBuilder;
+
+/**
+ * A DataSketches based Summary metric, aim for lock-free and low gc overhead.
+ */
+public class SketchesSummary extends SimpleCollector<SketchesSummary.Child> implements Counter.Describable {
+
+ final List<Double> quantiles;
+
+ SketchesSummary(SketchesSummary.Builder b) {
+ super(b);
+ this.quantiles = b.quantiles;
+ initializeNoLabelsChild();
+ }
+
+ public static class Builder extends SimpleCollector.Builder<Builder, SketchesSummary> {
+
+ private final List<Double> quantiles = new ArrayList<>();
+
+ public Builder quantile(double quantile) {
+ if (quantile < 0.0 || quantile > 1.0) {
+ throw new IllegalArgumentException("Quantile " + quantile
+ + " invalid: Expected number between 0.0 and 1.0.");
+ }
+ quantiles.add(quantile);
+ return this;
+ }
+
+ @Override
+ public SketchesSummary create() {
+ for (String label : labelNames) {
+ if (label.equals("quantile")) {
+ throw new IllegalStateException("Summary cannot have a label named 'quantile'.");
+ }
+ }
+ dontInitializeNoLabelsChild = true;
+ return new SketchesSummary(this);
+ }
+ }
+
+ /**
+ * Return a Builder to allow configuration of a new Summary. Ensures required fields are provided.
+ *
+ * @param name The name of the metric
+ * @param help The help string of the metric
+ */
+ public static Builder build(String name, String help) {
+ return new Builder().name(name).help(help);
+ }
+
+ /**
+ * Return a Builder to allow configuration of a new Summary.
+ */
+ public static Builder build() {
+ return new Builder();
+ }
+
+ @Override
+ protected Child newChild() {
+ return new Child(quantiles);
+ }
+
+ /**
+ * The value of a single Summary.
+ *
+ * <p>* <em>Warning:</em> References to a Child become invalid after using
+ * {@link SimpleCollector#remove} or {@link SimpleCollector#clear}.
+ */
+ public static class Child {
+
+ public static class Value {
+ public final double count;
+ public final double sum;
+ public final SortedMap<Double, Double> quantiles;
+
+ private Value(double count, double sum, List<Double> quantiles, DoublesSketch doublesSketch) {
+ this.count = count;
+ this.sum = sum;
+ this.quantiles = Collections.unmodifiableSortedMap(snapshot(quantiles, doublesSketch));
+ }
+
+ private SortedMap<Double, Double> snapshot(List<Double> quantiles, DoublesSketch doublesSketch) {
+ SortedMap<Double, Double> result = new TreeMap<Double, Double>();
+ for (Double q : quantiles) {
+ result.put(q, doublesSketch != null && !doublesSketch.isEmpty() ? doublesSketch.getQuantile(q)
+ : Double.NaN);
+ }
+ return result;
+ }
+ }
+
+ // Having these separate leaves us open to races,
+ // however Prometheus as whole has other races
+ // that mean adding atomicity here wouldn't be useful.
+ // This should be reevaluated in the future.
+ private final DoubleAdder count = new DoubleAdder();
+ private final DoubleAdder sum = new DoubleAdder();
+ private final List<Double> quantiles;
+ /*
+ * Use 2 rotating thread local accessor so that we can safely swap them.
+ */
+ private volatile ThreadLocalAccessor current;
+ private volatile ThreadLocalAccessor replacement;
+ private volatile DoublesSketch result;
+
+ private Child(List<Double> quantiles) {
+ this.quantiles = quantiles;
+ this.current = new ThreadLocalAccessor();
+ this.replacement = new ThreadLocalAccessor();
+ }
+
+ /**
+ * Observe the given amount.
+ */
+ public void observe(double amt) {
+ count.add(1);
+ sum.add(amt);
+ LocalSketch localSketch = current.localData.get();
+ long stamp = localSketch.lock.readLock();
+ try {
+ localSketch.sketch.update(amt);
+ } finally {
+ localSketch.lock.unlockRead(stamp);
+ }
+ }
+
+ public void rotate() {
Review Comment:
updated
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org