From e64c35b3d6f7a5944358b01a7b16ed1c7313f053 Mon Sep 17 00:00:00 2001 From: Rawven Date: Fri, 6 Dec 2024 17:24:35 +0800 Subject: [PATCH 1/3] fix: monitor arg cause npe (#86) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(): 解决metaFreshTime和configServiceUrl的NPE以及时间格式问题 * fix(): 消除external-type在启用monitor下但又未配置时的无用日志打印 * fix(): PrometheusApolloClientMetricsExporter下错误的logger配置 * fix(): ConfigMoniotr 方法命名与API不统一问题 * fix(): 优化代码以及增加DateUtil测试类 --- CHANGES.md | 2 +- .../internals/ConfigMonitorInitializer.java | 8 ++- .../apollo/monitor/api/ConfigMonitor.java | 2 +- .../internal/DefaultConfigMonitor.java | 2 +- .../DefaultApolloClientBootstrapArgsApi.java | 65 +++++++++++-------- .../impl/DefaultApolloClientNamespaceApi.java | 5 +- .../ApolloClientMonitorMessageProducer.java | 11 ++-- .../framework/apollo/util/ConfigUtil.java | 4 +- .../framework/apollo/util/date/DateUtil.java | 40 ++++++++++++ .../internal/DefaultConfigMonitorTest.java | 3 +- .../apollo/util/date/DateUtilTest.java | 42 ++++++++++++ ...PrometheusApolloClientMetricsExporter.java | 2 +- 12 files changed, 144 insertions(+), 42 deletions(-) create mode 100644 apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java create mode 100644 apollo-client/src/test/java/com/ctrip/framework/apollo/util/date/DateUtilTest.java diff --git a/CHANGES.md b/CHANGES.md index 56acae01..a8e204cf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,7 +11,7 @@ Apollo Java 2.4.0 * [Add more observability in apollo config client](https://github.com/apolloconfig/apollo-java/pull/74) * [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79) * [Feature support pulling configuration information from multiple AppIds](https://github.com/apolloconfig/apollo-java/pull/70) - +* [Fix monitor arg cause npe](https://github.com/apolloconfig/apollo-java/pull/86) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java index a6c0f421..3d8a7d7e 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java @@ -20,6 +20,7 @@ import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; +import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.monitor.internal.exporter.AbstractApolloClientMetricsExporter; import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporter; import com.ctrip.framework.apollo.monitor.internal.jmx.ApolloClientJmxMBeanRegister; @@ -102,10 +103,13 @@ private static void initializeMetricsEventListener() { private static void initializeMetricsExporter( ) { + if (StringUtils.isBlank(m_configUtil.getMonitorExternalType())) { + return; + } ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance( - ApolloClientMetricsExporterFactory.class); + ApolloClientMetricsExporterFactory.class); ApolloClientMetricsExporter metricsReporter = exporterFactory.getMetricsReporter( - MONITOR_CONTEXT.getApolloClientMonitorEventListeners()); + MONITOR_CONTEXT.getApolloClientMonitorEventListeners()); if (metricsReporter != null) { MONITOR_CONTEXT.setApolloClientMetricsExporter(metricsReporter); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java index a1731644..f350cfeb 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/api/ConfigMonitor.java @@ -39,7 +39,7 @@ public interface ConfigMonitor { /** * get running params monitor api */ - ApolloClientBootstrapArgsMonitorApi getRunningParamsMonitorApi(); + ApolloClientBootstrapArgsMonitorApi getBootstrapArgsMonitorApi(); /** * get monitor external system data diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java index 27b6606c..49a6cd41 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitor.java @@ -49,7 +49,7 @@ public ApolloClientNamespaceMonitorApi getNamespaceMonitorApi() { } @Override - public ApolloClientBootstrapArgsMonitorApi getRunningParamsMonitorApi() { + public ApolloClientBootstrapArgsMonitorApi getBootstrapArgsMonitorApi() { return apolloClientMonitorContext.getBootstrapArgsApi(); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java index 19ca9b30..29f8c9eb 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java @@ -23,13 +23,19 @@ import com.ctrip.framework.apollo.Apollo; import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory; +import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi; import com.ctrip.framework.apollo.monitor.internal.jmx.mbean.ApolloClientJmxBootstrapArgsMBean; import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener; import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent; import com.ctrip.framework.apollo.util.ConfigUtil; +import com.ctrip.framework.apollo.util.date.DateUtil; import com.google.common.collect.Maps; + +import java.time.LocalDateTime; import java.util.Map; +import java.util.Optional; + import org.slf4j.Logger; /** @@ -46,50 +52,55 @@ public class DefaultApolloClientBootstrapArgsApi extends public DefaultApolloClientBootstrapArgsApi(ConfigUtil configUtil) { super(TAG_BOOTSTRAP); - bootstrapArgs.put(APOLLO_ACCESS_KEY_SECRET, configUtil.getAccessKeySecret()); - bootstrapArgs.put(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES, + putAttachmentValue(APOLLO_ACCESS_KEY_SECRET, configUtil.getAccessKeySecret()); + putAttachmentValue(APOLLO_AUTO_UPDATE_INJECTED_SPRING_PROPERTIES, configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()); - bootstrapArgs.put(APOLLO_BOOTSTRAP_ENABLED, + putAttachmentValue(APOLLO_BOOTSTRAP_ENABLED, Boolean.parseBoolean(System.getProperty(APOLLO_BOOTSTRAP_ENABLED))); - bootstrapArgs.put(APOLLO_BOOTSTRAP_NAMESPACES, + putAttachmentValue(APOLLO_BOOTSTRAP_NAMESPACES, System.getProperty(APOLLO_BOOTSTRAP_NAMESPACES)); - bootstrapArgs.put(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, + putAttachmentValue(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, Boolean.parseBoolean(System.getProperty(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED))); - bootstrapArgs.put(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, configUtil.isOverrideSystemProperties()); - bootstrapArgs.put(APOLLO_CACHE_DIR, configUtil.getDefaultLocalCacheDir()); - bootstrapArgs.put(APOLLO_CLUSTER, configUtil.getCluster()); - bootstrapArgs.put(APOLLO_CONFIG_SERVICE, + putAttachmentValue(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, configUtil.isOverrideSystemProperties()); + putAttachmentValue(APOLLO_CACHE_DIR, configUtil.getDefaultLocalCacheDir()); + putAttachmentValue(APOLLO_CLUSTER, configUtil.getCluster()); + putAttachmentValue(APOLLO_CONFIG_SERVICE, System.getProperty(APOLLO_CONFIG_SERVICE)); - bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, configUtil.getMonitorExternalType()); - bootstrapArgs.put(APOLLO_CLIENT_MONITOR_ENABLED, configUtil.isClientMonitorEnabled()); - bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD, + putAttachmentValue(APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, configUtil.getMonitorExternalType()); + putAttachmentValue(APOLLO_CLIENT_MONITOR_ENABLED, configUtil.isClientMonitorEnabled()); + putAttachmentValue(APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD, configUtil.getMonitorExternalExportPeriod()); - bootstrapArgs.put(APOLLO_META, configUtil.getMetaServerDomainName()); - bootstrapArgs.put(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, configUtil.isPropertyNamesCacheEnabled()); - bootstrapArgs.put(APOLLO_PROPERTY_ORDER_ENABLE, configUtil.isPropertiesOrderEnabled()); - bootstrapArgs.put(APOLLO_CLIENT_MONITOR_JMX_ENABLED, configUtil.isClientMonitorJmxEnabled()); - bootstrapArgs.put(APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE, + putAttachmentValue(APOLLO_META, configUtil.getMetaServerDomainName()); + putAttachmentValue(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, configUtil.isPropertyNamesCacheEnabled()); + putAttachmentValue(APOLLO_PROPERTY_ORDER_ENABLE, configUtil.isPropertiesOrderEnabled()); + putAttachmentValue(APOLLO_CLIENT_MONITOR_JMX_ENABLED, configUtil.isClientMonitorJmxEnabled()); + putAttachmentValue(APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE, configUtil.getMonitorExceptionQueueSize()); - bootstrapArgs.put(APP_ID, configUtil.getAppId()); - bootstrapArgs.put(ENV, configUtil.getApolloEnv()); - bootstrapArgs.put(VERSION, Apollo.VERSION); - bootstrapArgs.forEach((key, value) -> { - if (value != null) { - bootstrapArgsString.put(key, value.toString()); - } - }); - + putAttachmentValue(APP_ID, configUtil.getAppId()); + putAttachmentValue(ENV, configUtil.getApolloEnv()); + putAttachmentValue(VERSION, Apollo.VERSION); + DateUtil.formatLocalDateTime(LocalDateTime.now()) + .ifPresent(s -> putAttachmentValue(META_FRESH, s)); + putAttachmentValue(CONFIG_SERVICE_URL,""); } @Override public void collect0(ApolloClientMonitorEvent event) { String argName = event.getName(); if (bootstrapArgs.containsKey(argName)) { - bootstrapArgs.put(argName, event.getAttachmentValue(argName)); + putAttachmentValue(argName, event.getAttachmentValue(argName)); } else { logger.warn("Unhandled event name: {}", argName); } } + + private void putAttachmentValue(String argName, Object value) { + if(StringUtils.isBlank(argName) || value == null) { + return; + } + bootstrapArgs.put(argName, value); + bootstrapArgsString.put(argName, String.valueOf(value)); + } @Override public boolean isMetricsSampleUpdated() { diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java index b6527b8b..91b1d28e 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java @@ -28,6 +28,7 @@ import com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant; import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener; import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent; +import com.ctrip.framework.apollo.util.date.DateUtil; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import java.time.LocalDateTime; @@ -35,6 +36,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.slf4j.Logger; @@ -182,7 +184,8 @@ public Map getNamespaceMetricsString() { namespaces.forEach((namespace, metrics) -> { NamespaceMetricsString namespaceMetricsString = new NamespaceMetricsString(); namespaceMetricsString.setFirstLoadTimeSpendInMs(metrics.getFirstLoadTimeSpendInMs()); - namespaceMetricsString.setLatestUpdateTime(metrics.getLatestUpdateTime().toString()); + DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime()) + .ifPresent(namespaceMetricsString::setLatestUpdateTime); namespaceMetricsString.setUsageCount(metrics.getUsageCount()); namespaceMetricsString.setReleaseKey(metrics.getReleaseKey()); namespaceMetricsStringMap.put(namespace, namespaceMetricsString); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java index b744ce2d..133d6671 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java @@ -25,11 +25,14 @@ import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEventPublisher; import com.ctrip.framework.apollo.tracer.spi.MessageProducer; import com.ctrip.framework.apollo.tracer.spi.Transaction; +import com.ctrip.framework.apollo.util.date.DateUtil; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; /** * @author Rawven @@ -97,7 +100,7 @@ private void handleTaggedEvent(String type, String name) { publishNamespaceNotFoundEvent(name); break; case APOLLO_CLIENT_CONFIGMETA: - // 不需要收集 + // No need to collect break; default: break; @@ -122,9 +125,9 @@ private void publishConfigChangeEvent(String name) { private void publishMetaServiceEvent() { ApolloClientMonitorEventPublisher.publish( - ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH) - .withTag(TAG_BOOTSTRAP) - .putAttachment(META_FRESH, LocalDate.now().toString())); + ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH) + .withTag(TAG_BOOTSTRAP) + .putAttachment(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now()).orElse(""))); } private void publishConfigServiceEvent(String name) { diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java index 457f3f32..af18c988 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java @@ -74,7 +74,7 @@ public class ConfigUtil { private boolean propertyKubernetesCacheEnabled = false; private boolean clientMonitorEnabled = false; private boolean clientMonitorJmxEnabled = false; - private String monitorExternalType = "NONE"; + private String monitorExternalType = ""; private long monitorExternalExportPeriod = 10; private int monitorExceptionQueueSize = 25; @@ -556,7 +556,7 @@ private void initClientMonitorExternalType() { monitorExternalType = System.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE); if (Strings.isNullOrEmpty(monitorExternalType)) { monitorExternalType = Foundation.app() - .getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, "NONE"); + .getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, ""); } } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java new file mode 100644 index 00000000..7a9bf6fa --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java @@ -0,0 +1,40 @@ +/* + * Copyright 2022 Apollo Authors + * + * Licensed 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 com.ctrip.framework.apollo.util.date; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Optional; + +/** + * @author Rawven + * @date 2024/10/19 + */ +public class DateUtil { + public static final DateTimeFormatter MEDIUM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + /** + * Formats a LocalDateTime object to a string using the MEDIUM_FORMATTER. + * + * @param localDateTime the LocalDateTime to format, can be null + * @return the formatted date-time string, or null if the input is null + */ + public static Optional formatLocalDateTime(LocalDateTime localDateTime) { + return Optional.ofNullable(localDateTime) + .map(dt -> dt.format(MEDIUM_FORMATTER)); + } +} diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitorTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitorTest.java index 2f1ffc6c..870b54c3 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitorTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/monitor/internal/DefaultConfigMonitorTest.java @@ -18,7 +18,6 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.build.MockInjector; import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi; import com.ctrip.framework.apollo.monitor.api.ApolloClientExceptionMonitorApi; @@ -65,7 +64,7 @@ public void setUp(){ public void testApis(){ assertSame(exceptionMonitorApi, configMonitor.getExceptionMonitorApi()); assertSame(namespaceMonitorApi, configMonitor.getNamespaceMonitorApi()); - assertSame(bootstrapArgsMonitorApi, configMonitor.getRunningParamsMonitorApi()); + assertSame(bootstrapArgsMonitorApi, configMonitor.getBootstrapArgsMonitorApi()); assertSame(threadPoolMonitorApi, configMonitor.getThreadPoolMonitorApi()); } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/date/DateUtilTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/date/DateUtilTest.java new file mode 100644 index 00000000..8bb54d08 --- /dev/null +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/date/DateUtilTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022 Apollo Authors + * + * Licensed 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 com.ctrip.framework.apollo.util.date; + +import org.junit.Test; +import static org.junit.Assert.*; +import java.time.LocalDateTime; +import java.util.Optional; + +public class DateUtilTest { + + @Test + public void testFormatLocalDateTime_validDate() { + LocalDateTime dateTime = LocalDateTime.of(2024, 12, 1, 10, 30, 0); + + Optional formattedDate = DateUtil.formatLocalDateTime(dateTime); + + assertTrue(formattedDate.isPresent()); + assertEquals("2024-12-01 10:30:00", formattedDate.get()); + } + + @Test + public void testFormatLocalDateTime_nullDate() { + Optional result = DateUtil.formatLocalDateTime(null); + + assertFalse(result.isPresent()); + } +} diff --git a/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/PrometheusApolloClientMetricsExporter.java b/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/PrometheusApolloClientMetricsExporter.java index a07e3209..4acf5b37 100644 --- a/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/PrometheusApolloClientMetricsExporter.java +++ b/apollo-plugin/apollo-plugin-client-prometheus/src/main/java/com/ctrip/framework/apollo/monitor/internal/exporter/impl/PrometheusApolloClientMetricsExporter.java @@ -40,7 +40,7 @@ public class PrometheusApolloClientMetricsExporter extends private static final String PROMETHEUS = "prometheus"; private final Logger logger = DeferredLoggerFactory.getLogger( - DefaultApolloClientNamespaceApi.class); + PrometheusApolloClientMetricsExporter.class); protected CollectorRegistry registry; protected Map map; From af2e2dd5208be78e6916a90fca6d3c8f565456ff Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sat, 14 Dec 2024 13:10:47 +0800 Subject: [PATCH 2/3] upgrade cache action to v4 --- .github/workflows/build.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3b03b15e..752b3409 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,11 +37,12 @@ jobs: with: java-version: ${{ matrix.jdk }} - name: Cache Maven packages - uses: actions/cache@v1 + uses: actions/cache@v4 with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- - name: JDK 8 if: matrix.jdk == '8' run: mvn -B clean package -P travis jacoco:report -Dmaven.gitcommitid.skip=true From eff90aacd252bb30f8573c6bcdbf76930be5f368 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sat, 14 Dec 2024 14:44:45 +0800 Subject: [PATCH 3/3] fix the concurrent issue in SpringValueRegistry.scanAndClean --- CHANGES.md | 1 + .../spring/property/SpringValueRegistry.java | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a8e204cf..7705f8ea 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ Apollo Java 2.4.0 * [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79) * [Feature support pulling configuration information from multiple AppIds](https://github.com/apolloconfig/apollo-java/pull/70) * [Fix monitor arg cause npe](https://github.com/apolloconfig/apollo-java/pull/86) +* [Fix the concurrent issue in SpringValueRegistry.scanAndClean](https://github.com/apolloconfig/apollo-java/pull/95) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java index 2600960e..db2467b4 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java @@ -84,12 +84,15 @@ private void scanAndClean() { Iterator> iterator = registry.values().iterator(); while (!Thread.currentThread().isInterrupted() && iterator.hasNext()) { Multimap springValues = iterator.next(); - Iterator> springValueIterator = springValues.entries().iterator(); - while (springValueIterator.hasNext()) { - Entry springValue = springValueIterator.next(); - if (!springValue.getValue().isTargetBeanValid()) { - // clear unused spring values - springValueIterator.remove(); + synchronized (springValues) { + Iterator> springValueIterator = springValues.entries() + .iterator(); + while (springValueIterator.hasNext()) { + Entry springValue = springValueIterator.next(); + if (!springValue.getValue().isTargetBeanValid()) { + // clear unused spring values + springValueIterator.remove(); + } } } }