From 09d8e0f6bb53b289d0cb87b17a4423d86838b826 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Wed, 27 Sep 2023 12:29:05 +0800 Subject: [PATCH] Support customize collector sync period --- .../dubbo/common/constants/MetricsConstants.java | 2 ++ .../java/org/apache/dubbo/config/MetricsConfig.java | 13 +++++++++++++ .../metrics/report/AbstractMetricsReporter.java | 7 +++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java index 4c4437d4275..e891fe9ad36 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java @@ -51,6 +51,8 @@ public interface MetricsConstants { String ENABLE_COLLECTOR_SYNC_KEY = "enable.collector.sync"; + String COLLECTOR_SYNC_PERIOD_KEY = "collector.sync.period"; + String AGGREGATION_COLLECTOR_KEY = "aggregation"; String AGGREGATION_ENABLED_KEY = "aggregation.enabled"; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/MetricsConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/MetricsConfig.java index 98d05209a52..d1cf0c1d434 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/MetricsConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/MetricsConfig.java @@ -71,6 +71,11 @@ public class MetricsConfig extends AbstractConfig { */ private Boolean enableCollectorSync; + /** + * Collector sync period. + */ + private Integer collectorSyncPeriod; + /** * @deprecated After metrics config is refactored. * This parameter should no longer use and will be deleted in the future. @@ -248,6 +253,14 @@ public void setEnableCollectorSync(Boolean enableCollectorSync) { this.enableCollectorSync = enableCollectorSync; } + public Integer getCollectorSyncPeriod() { + return collectorSyncPeriod; + } + + public void setCollectorSyncPeriod(Integer collectorSyncPeriod) { + this.collectorSyncPeriod = collectorSyncPeriod; + } + public Boolean getUseGlobalRegistry() { return useGlobalRegistry; } diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java index 13c8cecdc41..70033771c53 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java @@ -53,6 +53,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_METRICS_COLLECTOR_EXCEPTION; +import static org.apache.dubbo.common.constants.MetricsConstants.COLLECTOR_SYNC_PERIOD_KEY; import static org.apache.dubbo.common.constants.MetricsConstants.ENABLE_COLLECTOR_SYNC_KEY; import static org.apache.dubbo.common.constants.MetricsConstants.ENABLE_JVM_METRICS_KEY; @@ -77,7 +78,7 @@ public abstract class AbstractMetricsReporter implements MetricsReporter { private ScheduledExecutorService collectorSyncJobExecutor = null; private static final int DEFAULT_SCHEDULE_INITIAL_DELAY = 5; - private static final int DEFAULT_SCHEDULE_PERIOD = 3; + private static final int DEFAULT_SCHEDULE_PERIOD = 60; protected AbstractMetricsReporter(URL url, ApplicationModel applicationModel) { this.url = url; @@ -143,9 +144,11 @@ private void initCollectors() { private void scheduleMetricsCollectorSyncJob() { boolean enableCollectorSync = url.getParameter(ENABLE_COLLECTOR_SYNC_KEY, true); if (enableCollectorSync) { + int collectSyncPeriod = url.getParameter(COLLECTOR_SYNC_PERIOD_KEY, DEFAULT_SCHEDULE_PERIOD); + NamedThreadFactory threadFactory = new NamedThreadFactory("metrics-collector-sync-job", true); collectorSyncJobExecutor = Executors.newScheduledThreadPool(1, threadFactory); - collectorSyncJobExecutor.scheduleWithFixedDelay(this::refreshData, DEFAULT_SCHEDULE_INITIAL_DELAY, DEFAULT_SCHEDULE_PERIOD, TimeUnit.SECONDS); + collectorSyncJobExecutor.scheduleWithFixedDelay(this::refreshData, DEFAULT_SCHEDULE_INITIAL_DELAY, collectSyncPeriod, TimeUnit.SECONDS); } }