Skip to content

Commit f6d4da9

Browse files
authored
Merge pull request apolloconfig#688 from nobodyiam/misc-change
Misc change
2 parents cfe3ad6 + ba4b551 commit f6d4da9

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ private synchronized void updateConfigServices() {
106106
transaction.setStatus(Transaction.SUCCESS);
107107
List<ServiceDTO> services = response.getBody();
108108
if (services == null || services.isEmpty()) {
109-
logConfigServiceToCat("Empty response!");
109+
logConfigService("Empty response!");
110110
continue;
111111
}
112112
m_configServices.set(services);
113-
logConfigServicesToCat(services);
113+
logConfigServices(services);
114114
return;
115115
} catch (Throwable ex) {
116116
Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
@@ -145,13 +145,13 @@ private String assembleMetaServiceUrl() {
145145
return domainName + "/services/config?" + MAP_JOINER.join(queryParams);
146146
}
147147

148-
private void logConfigServicesToCat(List<ServiceDTO> serviceDtos) {
148+
private void logConfigServices(List<ServiceDTO> serviceDtos) {
149149
for (ServiceDTO serviceDto : serviceDtos) {
150-
logConfigServiceToCat(serviceDto.getHomepageUrl());
150+
logConfigService(serviceDto.getHomepageUrl());
151151
}
152152
}
153153

154-
private void logConfigServiceToCat(String serviceUrl) {
154+
private void logConfigService(String serviceUrl) {
155155
Tracer.logEvent("Apollo.Config.Services", serviceUrl);
156156
}
157157
}

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.concurrent.Executors;
88
import java.util.concurrent.ScheduledExecutorService;
99
import java.util.concurrent.TimeUnit;
10+
import java.util.concurrent.atomic.AtomicBoolean;
1011
import java.util.concurrent.atomic.AtomicReference;
1112

1213
import org.slf4j.Logger;
@@ -17,6 +18,8 @@
1718
import com.ctrip.framework.apollo.core.ConfigConsts;
1819
import com.ctrip.framework.apollo.core.dto.ApolloConfig;
1920
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
21+
import com.ctrip.framework.apollo.core.schedule.ExponentialSchedulePolicy;
22+
import com.ctrip.framework.apollo.core.schedule.SchedulePolicy;
2023
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
2124
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
2225
import com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException;
@@ -51,6 +54,8 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
5154
private final static ScheduledExecutorService m_executorService;
5255
private AtomicReference<ServiceDTO> m_longPollServiceDto;
5356
private RateLimiter m_loadConfigRateLimiter;
57+
private AtomicBoolean m_configNeedForceRefresh;
58+
private SchedulePolicy m_loadConfigFailSchedulePolicy;
5459
private static final Escaper pathEscaper = UrlEscapers.urlPathSegmentEscaper();
5560
private static final Escaper queryParamEscaper = UrlEscapers.urlFormParameterEscaper();
5661

@@ -73,6 +78,9 @@ public RemoteConfigRepository(String namespace) {
7378
remoteConfigLongPollService = ApolloInjector.getInstance(RemoteConfigLongPollService.class);
7479
m_longPollServiceDto = new AtomicReference<>();
7580
m_loadConfigRateLimiter = RateLimiter.create(m_configUtil.getLoadConfigQPS());
81+
m_configNeedForceRefresh = new AtomicBoolean(true);
82+
m_loadConfigFailSchedulePolicy = new ExponentialSchedulePolicy(m_configUtil.getOnErrorRetryInterval(),
83+
m_configUtil.getOnErrorRetryInterval() * 8);
7684
this.trySync();
7785
this.schedulePeriodicRefresh();
7886
this.scheduleLongPollingRefresh();
@@ -154,7 +162,8 @@ private ApolloConfig loadApolloConfig() {
154162
String cluster = m_configUtil.getCluster();
155163
String dataCenter = m_configUtil.getDataCenter();
156164
Tracer.logEvent("Apollo.Client.ConfigMeta", STRING_JOINER.join(appId, cluster, m_namespace));
157-
int maxRetries = 2;
165+
int maxRetries = m_configNeedForceRefresh.get() ? 2 : 1;
166+
long onErrorSleepTime = 0; // 0 means no sleep
158167
Throwable exception = null;
159168

160169
List<ServiceDTO> configServices = getConfigServices();
@@ -167,6 +176,18 @@ private ApolloConfig loadApolloConfig() {
167176
}
168177

169178
for (ServiceDTO configService : randomConfigServices) {
179+
if (onErrorSleepTime > 0) {
180+
logger.warn(
181+
"Load config failed, will retry in {} {}. appId: {}, cluster: {}, namespaces: {}",
182+
onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, m_namespace);
183+
184+
try {
185+
m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(onErrorSleepTime);
186+
} catch (InterruptedException e) {
187+
//ignore
188+
}
189+
}
190+
170191
String url =
171192
assembleQueryConfigUrl(configService.getHomepageUrl(), appId, cluster, m_namespace,
172193
dataCenter, m_configCache.get());
@@ -179,6 +200,8 @@ private ApolloConfig loadApolloConfig() {
179200
try {
180201

181202
HttpResponse<ApolloConfig> response = m_httpUtil.doGet(request, ApolloConfig.class);
203+
m_configNeedForceRefresh.set(false);
204+
m_loadConfigFailSchedulePolicy.success();
182205

183206
transaction.addData("StatusCode", response.getStatusCode());
184207
transaction.setStatus(Transaction.SUCCESS);
@@ -215,13 +238,11 @@ private ApolloConfig loadApolloConfig() {
215238
transaction.complete();
216239
}
217240

241+
// if force refresh, do normal sleep, if normal config load, do exponential sleep
242+
onErrorSleepTime = m_configNeedForceRefresh.get() ? m_configUtil.getOnErrorRetryInterval() :
243+
m_loadConfigFailSchedulePolicy.fail();
218244
}
219245

220-
try {
221-
m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
222-
} catch (InterruptedException ex) {
223-
//ignore
224-
}
225246
}
226247
String message = String.format(
227248
"Load Apollo Config failed - appId: %s, cluster: %s, namespace: %s",
@@ -271,6 +292,7 @@ public void onLongPollNotified(ServiceDTO longPollNotifiedServiceDto) {
271292
m_executorService.submit(new Runnable() {
272293
@Override
273294
public void run() {
295+
m_configNeedForceRefresh.set(true);
274296
trySync();
275297
}
276298
});

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.ctrip.framework.apollo.spring.config;
22

3-
import java.util.Collection;
4-
import java.util.Iterator;
3+
import com.google.common.collect.HashMultimap;
4+
import com.google.common.collect.ImmutableSortedSet;
5+
import com.google.common.collect.Multimap;
6+
7+
import com.ctrip.framework.apollo.Config;
8+
import com.ctrip.framework.apollo.ConfigService;
59

610
import org.springframework.beans.BeansException;
711
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
@@ -13,14 +17,16 @@
1317
import org.springframework.core.env.ConfigurableEnvironment;
1418
import org.springframework.core.env.Environment;
1519

16-
import com.ctrip.framework.apollo.Config;
17-
import com.ctrip.framework.apollo.ConfigService;
18-
import com.google.common.collect.HashMultimap;
19-
import com.google.common.collect.ImmutableSortedSet;
20-
import com.google.common.collect.Multimap;
20+
import java.util.Collection;
21+
import java.util.Iterator;
2122

2223
/**
23-
* Apollo Property Sources processor for Spring Annotation Based Application
24+
* Apollo Property Sources processor for Spring Annotation Based Application. <br /> <br />
25+
*
26+
* The reason why PropertySourcesProcessor implements {@link BeanFactoryPostProcessor} instead of
27+
* {@link org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor} is that lower versions of
28+
* Spring (e.g. 3.1.1) doesn't support registering BeanDefinitionRegistryPostProcessor in ImportBeanDefinitionRegistrar
29+
* - {@link com.ctrip.framework.apollo.spring.annotation.ApolloConfigRegistrar}
2430
*
2531
* @author Jason Song([email protected])
2632
*/
@@ -68,7 +74,7 @@ public void setEnvironment(Environment environment) {
6874
}
6975

7076
//only for test
71-
private static void reset() {
77+
private static void reset() {
7278
NAMESPACE_NAMES.clear();
7379
}
7480

apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/NotificationController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ public DeferredResult<ResponseEntity<ApolloConfigNotification>> pollNotification
107107
}
108108

109109
deferredResult
110-
.onTimeout(() -> logWatchedKeysToCat(watchedKeys, "Apollo.LongPoll.TimeOutKeys"));
110+
.onTimeout(() -> logWatchedKeys(watchedKeys, "Apollo.LongPoll.TimeOutKeys"));
111111

112112
deferredResult.onCompletion(() -> {
113113
//unregister all keys
114114
for (String key : watchedKeys) {
115115
deferredResults.remove(key, deferredResult);
116116
}
117-
logWatchedKeysToCat(watchedKeys, "Apollo.LongPoll.CompletedKeys");
117+
logWatchedKeys(watchedKeys, "Apollo.LongPoll.CompletedKeys");
118118
});
119119

120-
logWatchedKeysToCat(watchedKeys, "Apollo.LongPoll.RegisteredKeys");
120+
logWatchedKeys(watchedKeys, "Apollo.LongPoll.RegisteredKeys");
121121
logger.debug("Listening {} from appId: {}, cluster: {}, namespace: {}, datacenter: {}",
122122
watchedKeys, appId, cluster, namespace, dataCenter);
123123
}
@@ -159,7 +159,7 @@ public void handleMessage(ReleaseMessage message, String channel) {
159159
logger.debug("Notification completed");
160160
}
161161

162-
private void logWatchedKeysToCat(Set<String> watchedKeys, String eventName) {
162+
private void logWatchedKeys(Set<String> watchedKeys, String eventName) {
163163
for (String watchedKey : watchedKeys) {
164164
Tracer.logEvent(eventName, watchedKey);
165165
}

0 commit comments

Comments
 (0)