From 89321acfc14f1407a259588ba36a542560595f4a Mon Sep 17 00:00:00 2001 From: jason <2353220944@qq.com> Date: Sat, 21 Dec 2024 13:29:12 +0800 Subject: [PATCH] code format --- .../internals/RemoteConfigRepository.java | 42 +++++--- .../internals/RemoteConfigRepositoryTest.java | 97 ++++++++++++------- .../apollo/core/dto/ApolloConfig.java | 6 +- .../apollo/core/dto/ConfigurationChange.java | 9 +- .../apollo/core/enums/ConfigSyncType.java | 9 +- .../enums/ConfigurationChangeTypeUtils.java | 4 +- 6 files changed, 102 insertions(+), 65 deletions(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java index 58519e72..7a028909 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java @@ -51,8 +51,11 @@ import com.google.common.net.UrlEscapers; import com.google.common.util.concurrent.RateLimiter; import com.google.gson.Gson; - -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -64,7 +67,9 @@ * @author Jason Song(song_s@ctrip.com) */ public class RemoteConfigRepository extends AbstractConfigRepository { - private static final Logger logger = DeferredLoggerFactory.getLogger(RemoteConfigRepository.class); + + private static final Logger logger = DeferredLoggerFactory.getLogger( + RemoteConfigRepository.class); private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR); private static final Joiner.MapJoiner MAP_JOINER = Joiner.on("&").withKeyValueSeparator("="); private static final Escaper pathEscaper = UrlEscapers.urlPathSegmentEscaper(); @@ -93,7 +98,7 @@ public class RemoteConfigRepository extends AbstractConfigRepository { /** * Constructor. * - * @param appId the appId + * @param appId the appId * @param namespace the namespace */ public RemoteConfigRepository(String appId, String namespace) { @@ -108,7 +113,8 @@ public RemoteConfigRepository(String appId, String namespace) { m_remoteMessages = new AtomicReference<>(); m_loadConfigRateLimiter = RateLimiter.create(m_configUtil.getLoadConfigQPS()); m_configNeedForceRefresh = new AtomicBoolean(true); - m_loadConfigFailSchedulePolicy = new ExponentialSchedulePolicy(m_configUtil.getOnErrorRetryInterval(), + m_loadConfigFailSchedulePolicy = new ExponentialSchedulePolicy( + m_configUtil.getOnErrorRetryInterval(), m_configUtil.getOnErrorRetryInterval() * 8); this.schedulePeriodicRefresh(); this.scheduleLongPollingRefresh(); @@ -119,7 +125,7 @@ public Properties getConfig() { if (m_configCache.get() == null) { long start = System.currentTimeMillis(); this.sync(); - Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND+":"+m_namespace, + Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND + ":" + m_namespace, String.valueOf(System.currentTimeMillis() - start)); } return transformApolloConfigToProperties(m_configCache.get()); @@ -142,7 +148,8 @@ private void schedulePeriodicRefresh() { new Runnable() { @Override public void run() { - Tracer.logEvent(APOLLO_CONFIGSERVICE, String.format("periodicRefresh: %s", m_namespace)); + Tracer.logEvent(APOLLO_CONFIGSERVICE, + String.format("periodicRefresh: %s", m_namespace)); logger.debug("refresh config for namespace: {}", m_namespace); trySync(); Tracer.logEvent(APOLLO_CLIENT_VERSION, Apollo.VERSION); @@ -167,7 +174,7 @@ protected synchronized void sync() { } if (current != null) { - Tracer.logEvent(String.format(APOLLO_CLIENT_CONFIGS+"%s", current.getNamespaceName()), + Tracer.logEvent(String.format(APOLLO_CLIENT_CONFIGS + "%s", current.getNamespaceName()), current.getReleaseKey()); } @@ -218,7 +225,8 @@ private ApolloConfig loadApolloConfig() { if (onErrorSleepTime > 0) { logger.warn( "Load config failed, will retry in {} {}. appId: {}, cluster: {}, namespaces: {}", - onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, m_namespace); + onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, + m_namespace); try { m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(onErrorSleepTime); @@ -228,7 +236,7 @@ private ApolloConfig loadApolloConfig() { } url = assembleQueryConfigUrl(configService.getHomepageUrl(), appId, cluster, m_namespace, - dataCenter, m_remoteMessages.get(), m_configCache.get()); + dataCenter, m_remoteMessages.get(), m_configCache.get()); logger.debug("Loading config from {}", url); @@ -286,13 +294,14 @@ private ApolloConfig loadApolloConfig() { appId, cluster, m_namespace); statusCodeException = new ApolloConfigStatusCodeException(ex.getStatusCode(), message); - Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_NOT_FOUND,m_namespace); - + Tracer.logEvent(APOLLO_CLIENT_NAMESPACE_NOT_FOUND, m_namespace); + } - Tracer.logEvent(APOLLO_CONFIG_EXCEPTION, ExceptionUtil.getDetailMessage(statusCodeException)); + Tracer.logEvent(APOLLO_CONFIG_EXCEPTION, + ExceptionUtil.getDetailMessage(statusCodeException)); transaction.setStatus(statusCodeException); exception = statusCodeException; - if(ex.getStatusCode() == 404) { + if (ex.getStatusCode() == 404) { break retryLoopLabel; } } catch (Throwable ex) { @@ -316,7 +325,7 @@ private ApolloConfig loadApolloConfig() { } String assembleQueryConfigUrl(String uri, String appId, String cluster, String namespace, - String dataCenter, ApolloNotificationMessages remoteMessages, ApolloConfig previousConfig) { + String dataCenter, ApolloNotificationMessages remoteMessages, ApolloConfig previousConfig) { String path = "configs/%s/%s/%s"; List pathParams = @@ -361,7 +370,8 @@ private void scheduleLongPollingRefresh() { remoteConfigLongPollService.submit(m_appId, m_namespace, this); } - public void onLongPollNotified(ServiceDTO longPollNotifiedServiceDto, ApolloNotificationMessages remoteMessages) { + public void onLongPollNotified(ServiceDTO longPollNotifiedServiceDto, + ApolloNotificationMessages remoteMessages) { m_longPollServiceDto.set(longPollNotifiedServiceDto); m_remoteMessages.set(remoteMessages); m_executorService.submit(new Runnable() { diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java index e5e44920..07a99f48 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java @@ -31,7 +31,11 @@ import static org.mockito.Mockito.when; import com.ctrip.framework.apollo.build.MockInjector; -import com.ctrip.framework.apollo.core.dto.*; +import com.ctrip.framework.apollo.core.dto.ApolloConfig; +import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; +import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages; +import com.ctrip.framework.apollo.core.dto.ConfigurationChange; +import com.ctrip.framework.apollo.core.dto.ServiceDTO; import com.ctrip.framework.apollo.core.enums.ConfigSyncType; import com.ctrip.framework.apollo.core.signature.Signature; import com.ctrip.framework.apollo.enums.ConfigSourceType; @@ -145,7 +149,8 @@ public void testLoadConfig() throws Exception { when(someResponse.getStatusCode()).thenReturn(200); when(someResponse.getBody()).thenReturn(someApolloConfig); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); Properties config = remoteConfigRepository.getConfig(); @@ -168,19 +173,20 @@ public void testLoadConfigWithIncrementalSync() throws Exception { when(someResponse.getStatusCode()).thenReturn(200); when(someResponse.getBody()).thenReturn(someApolloConfig); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); - - remoteConfigRepository.sync(); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); + remoteConfigRepository.sync(); - List configurationChanges=new ArrayList<>(); + List configurationChanges = new ArrayList<>(); String someNewValue = "someNewValue"; configurationChanges.add(new ConfigurationChange(someKey, someNewValue, "MODIFIED")); configurationChanges.add(new ConfigurationChange(someKey1, null, "DELETED")); String someKey2 = "someKey2"; String someValue2 = "someValue2"; - configurationChanges.add(new ConfigurationChange(someKey2, someValue2,"ADDED")); - ApolloConfig someApolloConfigWithIncrementalSync = assembleApolloConfigWithIncrementalSync(configurationChanges); + configurationChanges.add(new ConfigurationChange(someKey2, someValue2, "ADDED")); + ApolloConfig someApolloConfigWithIncrementalSync = assembleApolloConfigWithIncrementalSync( + configurationChanges); when(someResponse.getStatusCode()).thenReturn(200); when(someResponse.getBody()).thenReturn(someApolloConfigWithIncrementalSync); @@ -204,22 +210,25 @@ public void testMergeConfigurations() throws Exception { String key3 = "key3"; String value3 = "value3"; - Map previousConfigurations = ImmutableMap.of(key1, value1,key3,value3); + Map previousConfigurations = ImmutableMap.of(key1, value1, key3, value3); - List configurationChanges=new ArrayList<>(); + List configurationChanges = new ArrayList<>(); configurationChanges.add(new ConfigurationChange(key1, anotherValue1, "MODIFIED")); String key2 = "key2"; String value2 = "value2"; configurationChanges.add(new ConfigurationChange(key2, value2, "ADDED")); configurationChanges.add(new ConfigurationChange(key3, null, "DELETED")); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); - Map result=remoteConfigRepository.mergeConfigurations(previousConfigurations, configurationChanges); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); + Map result = remoteConfigRepository.mergeConfigurations(previousConfigurations, + configurationChanges); assertEquals(2, result.size()); assertEquals(anotherValue1, result.get(key1)); assertEquals(value2, result.get(key2)); } + @Test public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exception { String key1 = "key1"; @@ -228,11 +237,12 @@ public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exce String key3 = "key3"; String value3 = "value3"; - Map previousConfigurations = ImmutableMap.of(key1, value1,key3,value3); + Map previousConfigurations = ImmutableMap.of(key1, value1, key3, value3); - - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); - Map result=remoteConfigRepository.mergeConfigurations(previousConfigurations, null); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); + Map result = remoteConfigRepository.mergeConfigurations(previousConfigurations, + null); assertEquals(2, result.size()); assertEquals(value1, result.get(key1)); @@ -248,15 +258,17 @@ public void testMergeConfigurationWithChangesIsNULL() throws Exception { String key3 = "key3"; String value3 = "value3"; - List configurationChanges=new ArrayList<>(); + List configurationChanges = new ArrayList<>(); configurationChanges.add(new ConfigurationChange(key1, anotherValue1, "MODIFIED")); String key2 = "key2"; String value2 = "value2"; configurationChanges.add(new ConfigurationChange(key2, value2, "ADDED")); configurationChanges.add(new ConfigurationChange(key3, null, "DELETED")); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace); - Map result=remoteConfigRepository.mergeConfigurations(null, configurationChanges); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); + Map result = remoteConfigRepository.mergeConfigurations(null, + configurationChanges); assertEquals(2, result.size()); assertEquals(anotherValue1, result.get(key1)); @@ -281,7 +293,8 @@ public Properties answer(InvocationOnMock invocation) { } }); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); Properties config = remoteConfigRepository.getConfig(); @@ -318,7 +331,8 @@ public HttpResponse answer(InvocationOnMock invocation) throws Thr } }).when(httpClient).doGet(any(HttpRequest.class), any(Class.class)); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); Properties config = remoteConfigRepository.getConfig(); @@ -331,7 +345,8 @@ public void testGetRemoteConfigWithServerError() throws Exception { when(someResponse.getStatusCode()).thenReturn(500); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); //must stop the long polling before exception occurred remoteConfigLongPollService.stopLongPollingRefresh(); @@ -344,7 +359,8 @@ public void testGetRemoteConfigWithNotFount() throws Exception { when(someResponse.getStatusCode()).thenReturn(404); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); //must stop the long polling before exception occurred remoteConfigLongPollService.stopLongPollingRefresh(); @@ -361,7 +377,8 @@ public void testRepositoryChangeListener() throws Exception { when(someResponse.getBody()).thenReturn(someApolloConfig); RepositoryChangeListener someListener = mock(RepositoryChangeListener.class); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); remoteConfigRepository.addChangeListener(someListener); final ArgumentCaptor captor = ArgumentCaptor.forClass(Properties.class); @@ -372,7 +389,8 @@ public void testRepositoryChangeListener() throws Exception { remoteConfigRepository.sync(); - verify(someListener, times(1)).onRepositoryChange(eq(someAppId), eq(someNamespace), captor.capture()); + verify(someListener, times(1)).onRepositoryChange(eq(someAppId), eq(someNamespace), + captor.capture()); assertEquals(newConfigurations, captor.getValue()); } @@ -395,9 +413,11 @@ public Void answer(InvocationOnMock invocation) throws Throwable { return null; } - }).when(someListener).onRepositoryChange(any(String.class), any(String.class), any(Properties.class)); + }).when(someListener) + .onRepositoryChange(any(String.class), any(String.class), any(Properties.class)); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); remoteConfigRepository.addChangeListener(someListener); final ArgumentCaptor captor = ArgumentCaptor.forClass(Properties.class); @@ -421,12 +441,14 @@ public Void answer(InvocationOnMock invocation) throws Throwable { remoteConfigLongPollService.stopLongPollingRefresh(); - verify(someListener, times(1)).onRepositoryChange(eq(someAppId), eq(someNamespace), captor.capture()); + verify(someListener, times(1)).onRepositoryChange(eq(someAppId), eq(someNamespace), + captor.capture()); assertEquals(newConfigurations, captor.getValue()); final ArgumentCaptor httpRequestArgumentCaptor = ArgumentCaptor .forClass(HttpRequest.class); - verify(httpClient, atLeast(1)).doGet(httpRequestArgumentCaptor.capture(), eq(ApolloConfig.class)); + verify(httpClient, atLeast(1)).doGet(httpRequestArgumentCaptor.capture(), + eq(ApolloConfig.class)); HttpRequest request = httpRequestArgumentCaptor.getValue(); @@ -449,7 +471,8 @@ public void testAssembleQueryConfigUrl() throws Exception { notificationMessages.put(someKey, someNotificationId); notificationMessages.put(anotherKey, anotherNotificationId); - RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, someNamespace); + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); ApolloConfig someApolloConfig = mock(ApolloConfig.class); when(someApolloConfig.getReleaseKey()).thenReturn(someReleaseKey); @@ -479,12 +502,14 @@ private ApolloConfig assembleApolloConfig(Map configurations) { return apolloConfig; } - private ApolloConfig assembleApolloConfigWithIncrementalSync(List configurationChanges) { + + private ApolloConfig assembleApolloConfigWithIncrementalSync( + List configurationChanges) { String someAppId = "appId"; String someClusterName = "cluster"; String someReleaseKey = "1"; ApolloConfig apolloConfig = - new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseKey); + new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseKey); apolloConfig.setConfigSyncType(ConfigSyncType.INCREMENTAL_SYNC.getValue()); apolloConfig.setConfigurationChanges(configurationChanges); @@ -539,8 +564,8 @@ public long getLongPollingInitialDelayInMills() { } @Override - public String getAccessKeySecret(String appId){ - if(appId.equals(someAppId)){ + public String getAccessKeySecret(String appId) { + if (appId.equals(someAppId)) { return someSecret; } return null; @@ -555,8 +580,8 @@ public HttpResponse doGet(HttpRequest httpRequest, Class responseType) return (HttpResponse) someResponse; } throw new ApolloConfigStatusCodeException(someResponse.getStatusCode(), - String.format("Http request failed due to status code: %d", - someResponse.getStatusCode())); + String.format("Http request failed due to status code: %d", + someResponse.getStatusCode())); } @Override diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java index b1d296be..ca68a513 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java @@ -43,9 +43,9 @@ public ApolloConfig() { } public ApolloConfig(String appId, - String cluster, - String namespaceName, - String releaseKey) { + String cluster, + String namespaceName, + String releaseKey) { this.appId = appId; this.cluster = cluster; this.namespaceName = namespaceName; diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java index 7296dc4b..d8a1d5a7 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java @@ -17,20 +17,22 @@ package com.ctrip.framework.apollo.core.dto; - /** * Holds the information for a Configuration change. + * * @author jason */ public class ConfigurationChange { + private final String key; private final String newValue; private final String configurationChangeType; /** * Constructor. - * @param key the key whose value is changed - * @param newValue the value after change + * + * @param key the key whose value is changed + * @param newValue the value after change * @param configurationChangeType the change type */ public ConfigurationChange(String key, String newValue, String configurationChangeType) { @@ -42,6 +44,7 @@ public ConfigurationChange(String key, String newValue, String configurationChan public String getKey() { return key; } + public String getNewValue() { return newValue; } diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java index 481a59e8..19b31e42 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java @@ -24,7 +24,7 @@ * @author jason */ public enum ConfigSyncType { - FULL_SYNC("FullSync"), INCREMENTAL_SYNC("IncrementalSync"), UNKNOWN("UnKnown"); + FULL_SYNC("FullSync"), INCREMENTAL_SYNC("IncrementalSync"), UNKNOWN("Unknown"); private final String value; @@ -33,7 +33,6 @@ public enum ConfigSyncType { } - /** * Transforms a given string to its matching {@link ConfigSyncType}. * @@ -48,9 +47,9 @@ public static ConfigSyncType fromString(String value) { } for (ConfigSyncType type : values()) { if (type.value.equals(value)) { - return type; - } - } + return type; + } + } return UNKNOWN; } diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java index 4a5ef93c..57218707 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigurationChangeTypeUtils.java @@ -21,7 +21,8 @@ /** * A utility class for the {@link ConfigurationChangeType} enum. *

- * The class provides simple functionalities that extend the capabilities of {@link ConfigurationChangeType} + * The class provides simple functionalities that extend the capabilities of + * {@link ConfigurationChangeType} * * @author json */ @@ -43,7 +44,6 @@ public static ConfigurationChangeType transformChangeType(String changeType) { try { return ConfigurationChangeType.valueOf(cleanedChangeType); } catch (IllegalArgumentException e) { - return ConfigurationChangeType.UNKNOWN; } }