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 08e2705 commit 69f4677
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
* @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 @@ -98,7 +96,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 @@ -113,8 +111,7 @@ 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 @@ -125,7 +122,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 @@ -148,8 +145,7 @@ 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 @@ -174,7 +170,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 @@ -225,8 +221,7 @@ 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 @@ -236,7 +231,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 @@ -274,7 +269,7 @@ private ApolloConfig loadApolloConfig() {
result.setConfigurations(
mergeConfigurations(previousConfigurations, result.getConfigurationChanges()));
}

}

logger.debug("Loaded config for {}: {}", m_namespace, result);
Expand All @@ -290,14 +285,13 @@ 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 @@ -321,7 +315,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 @@ -366,8 +360,7 @@ 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 @@ -293,8 +293,7 @@ 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 @@ -331,8 +330,7 @@ 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 @@ -345,8 +343,7 @@ 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 @@ -359,8 +356,7 @@ 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 @@ -377,8 +373,7 @@ 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 @@ -389,8 +384,7 @@ 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 @@ -413,11 +407,9 @@ 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 @@ -441,14 +433,12 @@ 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 @@ -471,8 +461,7 @@ 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 @@ -564,8 +553,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 @@ -580,8 +569,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

0 comments on commit 69f4677

Please sign in to comment.