Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-coming committed Dec 21, 2024
1 parent b454698 commit 89321ac
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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());
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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<String> pathParams =
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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<ConfigurationChange> configurationChanges=new ArrayList<>();
List<ConfigurationChange> 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);
Expand All @@ -204,22 +210,25 @@ public void testMergeConfigurations() throws Exception {

String key3 = "key3";
String value3 = "value3";
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1, key3, value3);

List<ConfigurationChange> configurationChanges=new ArrayList<>();
List<ConfigurationChange> 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<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, configurationChanges);
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,
someNamespace);
Map<String, String> 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";
Expand All @@ -228,11 +237,12 @@ public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exce
String key3 = "key3";
String value3 = "value3";

Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1, key3, value3);


RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace);
Map<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, null);
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,
someNamespace);
Map<String, String> result = remoteConfigRepository.mergeConfigurations(previousConfigurations,
null);

assertEquals(2, result.size());
assertEquals(value1, result.get(key1));
Expand All @@ -248,15 +258,17 @@ public void testMergeConfigurationWithChangesIsNULL() throws Exception {
String key3 = "key3";
String value3 = "value3";

List<ConfigurationChange> configurationChanges=new ArrayList<>();
List<ConfigurationChange> 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<String, String> result=remoteConfigRepository.mergeConfigurations(null, configurationChanges);
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,
someNamespace);
Map<String, String> result = remoteConfigRepository.mergeConfigurations(null,
configurationChanges);

assertEquals(2, result.size());
assertEquals(anotherValue1, result.get(key1));
Expand All @@ -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();

Expand Down Expand Up @@ -318,7 +331,8 @@ public HttpResponse<ApolloConfig> 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();

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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<Properties> captor = ArgumentCaptor.forClass(Properties.class);

Expand All @@ -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());
}
Expand All @@ -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<Properties> captor = ArgumentCaptor.forClass(Properties.class);

Expand All @@ -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<HttpRequest> 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();

Expand All @@ -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);

Expand Down Expand Up @@ -479,12 +502,14 @@ private ApolloConfig assembleApolloConfig(Map<String, String> configurations) {

return apolloConfig;
}
private ApolloConfig assembleApolloConfigWithIncrementalSync(List<ConfigurationChange> configurationChanges) {

private ApolloConfig assembleApolloConfigWithIncrementalSync(
List<ConfigurationChange> 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);
Expand Down Expand Up @@ -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;
Expand All @@ -555,8 +580,8 @@ public <T> HttpResponse<T> doGet(HttpRequest httpRequest, Class<T> responseType)
return (HttpResponse<T>) 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 89321ac

Please # to comment.