Skip to content

Commit be0ca7b

Browse files
author
liwang
committed
ZOOKEEPER-4952:Reduce the GC overhead of Prometheus reject exception handling
Author: Li Wang <liwang@apple.com>
1 parent 9d1d25c commit be0ca7b

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.concurrent.ConcurrentMap;
3434
import java.util.concurrent.ExecutorService;
3535
import java.util.concurrent.LinkedBlockingQueue;
36-
import java.util.concurrent.RejectedExecutionException;
36+
import java.util.concurrent.RejectedExecutionHandler;
3737
import java.util.concurrent.ThreadFactory;
3838
import java.util.concurrent.ThreadPoolExecutor;
3939
import java.util.concurrent.TimeUnit;
@@ -677,7 +677,9 @@ private Optional<ExecutorService> createExecutor() {
677677
numWorkerThreads,
678678
0L,
679679
TimeUnit.MILLISECONDS,
680-
queue, new PrometheusWorkerThreadFactory());
680+
queue,
681+
new PrometheusWorkerThreadFactory(),
682+
new PrometheusRejectedExecutionHandler());
681683
LOG.info("Executor service was created with numWorkerThreads {} and maxQueueSize {}",
682684
numWorkerThreads,
683685
maxQueueSize);
@@ -714,14 +716,18 @@ public Thread newThread(final Runnable runnable) {
714716
}
715717
}
716718

719+
private class PrometheusRejectedExecutionHandler implements RejectedExecutionHandler {
720+
private final String queueExceededMessage = "Prometheus metrics queue size exceeded the max " + maxQueueSize;
721+
722+
@Override
723+
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor e) {
724+
rateLogger.rateLimitLog(queueExceededMessage);
725+
}
726+
}
727+
717728
private void reportMetrics(final Runnable task) {
718729
if (executorOptional.isPresent()) {
719-
try {
720-
executorOptional.get().submit(task);
721-
} catch (final RejectedExecutionException e) {
722-
rateLogger.rateLimitLog("Prometheus metrics reporting task queue size exceeded the max",
723-
String.valueOf(maxQueueSize));
724-
}
730+
executorOptional.get().submit(task);
725731
} else {
726732
task.run();
727733
}

zookeeper-server/src/main/java/org/apache/zookeeper/server/RateLogger.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,20 @@ public RateLogger(Logger log, long interval) {
4646

4747
public void flush() {
4848
if (msg != null && count > 0) {
49-
String log = "";
49+
// Optimized logging using parameterized messages to reduce string concatenation overhead
5050
if (count > 1) {
51-
log = "[" + count + " times] ";
51+
if (value != null) {
52+
LOG.warn("[{} times] Message: {} Last value:{}", count, msg, value);
53+
} else {
54+
LOG.warn("[{} times] Message: {}", count, msg);
55+
}
56+
} else {
57+
if (value != null) {
58+
LOG.warn("Message: {} Value:{}", msg, value);
59+
} else {
60+
LOG.warn("Message: {}", msg);
61+
}
5262
}
53-
log += "Message: " + msg;
54-
if (value != null) {
55-
log += " Last value:" + value;
56-
}
57-
LOG.warn(log);
5863
}
5964
msg = null;
6065
value = null;
@@ -70,7 +75,8 @@ public void rateLimitLog(String newMsg) {
7075
*/
7176
public void rateLimitLog(String newMsg, String newValue) {
7277
long now = Time.currentElapsedTime();
73-
if (Objects.equals(newMsg, msg)) {
78+
// Optimized string comparison: use reference equality first for constant strings
79+
if (newMsg == msg || Objects.equals(newMsg, msg)) {
7480
++count;
7581
value = newValue;
7682
if (now - timestamp >= LOG_INTERVAL) {

0 commit comments

Comments
 (0)