[
https://issues.apache.org/jira/browse/SCB-782?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16559641#comment-16559641
]
ASF GitHub Bot commented on SCB-782:
------------------------------------
heyile closed pull request #841: [SCB-782]support revision check when use pull
mode with config center
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/841
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
index a58de4da2..3daca8d64 100644
---
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
+++
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
@@ -136,7 +136,7 @@ public void connectServer() {
}
refreshMembers(memberDiscovery);
ConfigRefresh refreshTask = new ConfigRefresh(parseConfigUtils,
memberDiscovery);
- refreshTask.run(true);
+ refreshTask.run();
executor.scheduleWithFixedDelay(refreshTask,
firstRefreshInterval,
refreshInterval,
@@ -234,16 +234,19 @@ private HttpClientOptions createHttpClientOptions() {
this.memberdis = memberdis;
}
- public void run(boolean wait) {
+ @Override
+ public void run() {
// this will be single threaded, so we don't care about concurrent
// staffs
try {
String configCenter = memberdis.getConfigServer();
if (refreshMode == 1) {
- refreshConfig(configCenter, wait);
+ //make sure that there is only one thread is invoking refreshConfig
method
+ refreshConfig(configCenter);
} else if (!isWatching) {
// 重新监听时需要先加载,避免在断开期间丢失变更
- refreshConfig(configCenter, wait);
+ //make sure that there is only one thread is invoking refreshConfig
method
+ refreshConfig(configCenter);
doWatch(configCenter);
}
} catch (Exception e) {
@@ -251,11 +254,7 @@ public void run(boolean wait) {
}
}
- // 具体动作
- @Override
- public void run() {
- run(false);
- }
+
// create watch and wait for done
public void doWatch(String configCenter)
@@ -302,7 +301,7 @@ public void doWatch(String configCenter)
LOGGER.info("watching config recieved {}", action);
Map<String, Object> mAction = action.toJsonObject().getMap();
if ("CREATE".equals(mAction.get("action"))) {
- refreshConfig(configCenter, false);
+ refreshConfig(configCenter);
} else if ("MEMBER_CHANGE".equals(mAction.get("action"))) {
refreshMembers(memberdis);
} else {
@@ -348,7 +347,7 @@ private void sendHeartbeat(WebSocket ws) {
}
}
- public void refreshConfig(String configcenter, boolean wait) {
+ public void refreshConfig(String configcenter) {
CountDownLatch latch = new CountDownLatch(1);
String encodeServiceName = "";
try {
@@ -357,7 +356,7 @@ public void refreshConfig(String configcenter, boolean
wait) {
LOGGER.error("encode failed. Error message: {}", e.getMessage());
encodeServiceName = StringUtils.deleteWhitespace(serviceName);
}
- String path = uriConst.ITEMS + "?dimensionsInfo=" + encodeServiceName;
+ String path = uriConst.ITEMS + "?dimensionsInfo=" + encodeServiceName +
"&revision="+ParseConfigUtils.CURRENT_VERSION_INFO;
clientMgr.findThreadBindClientPool().runOnContext(client -> {
IpPort ipPort = NetUtils.parseIpPortFromURI(configcenter);
HttpClientRequest request = client.get(ipPort.getPort(),
ipPort.getHostOrIp(), path, rsp -> {
@@ -375,7 +374,9 @@ public void refreshConfig(String configcenter, boolean
wait) {
}
latch.countDown();
});
- } else {
+ }else if (rsp.statusCode() ==
HttpResponseStatus.NOT_MODIFIED.code()){
+ latch.countDown();
+ }else {
rsp.bodyHandler(buf -> {
LOGGER.error("Server error message is [{}].", buf);
latch.countDown();
@@ -383,7 +384,7 @@ public void refreshConfig(String configcenter, boolean
wait) {
EventManager.post(new ConnFailEvent("fetch config fail"));
LOGGER.error("Config refresh from {} failed.", configcenter);
}
- });
+ }).setTimeout((BOOTUP_WAIT_TIME -1 )* 1000);
Map<String, String> headers = new HashMap<>();
headers.put("x-domain-name", tenantName);
if (ConfigCenterConfig.INSTANCE.getToken() != null) {
@@ -403,15 +404,13 @@ public void refreshConfig(String configcenter, boolean
wait) {
});
request.end();
});
- if (wait) {
- LOGGER.info("Refreshing remote config...");
- try {
- latch.await(BOOTUP_WAIT_TIME, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- LOGGER.warn(e.getMessage());
- }
- LOGGER.info("Refreshing remote config is done.");
+ LOGGER.info("Refreshing remote config...");
+ try {
+ latch.await(BOOTUP_WAIT_TIME, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ LOGGER.warn(e.getMessage());
}
+ LOGGER.info("Refreshing remote config is done.");
}
}
diff --git
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
index a891088fe..d043f6e68 100644
---
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
+++
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
@@ -40,6 +40,8 @@
public static final Map<String, Object> flatItems = new HashMap<>();
+ public static String CURRENT_VERSION_INFO = "default";
+
private UpdateHandler updateHandler;
public ParseConfigUtils(UpdateHandler updateHandler) {
@@ -47,6 +49,8 @@ public ParseConfigUtils(UpdateHandler updateHandler) {
}
public void refreshConfigItems(Map<String, Map<String, Object>> remoteItems)
{
+ CURRENT_VERSION_INFO = remoteItems.getOrDefault("revision",new
HashMap<>()).getOrDefault("version","default").toString();
+ remoteItems.remove("revision");//the key revision is not the config setting
multiDimensionItems.clear();
multiDimensionItems.putAll(remoteItems);
doRefreshItems();
diff --git
a/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
b/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
index 83398f107..32a624c23 100644
---
a/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
+++
b/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
@@ -115,7 +115,70 @@ public void testConnectRefreshModeTwo() {
@SuppressWarnings("unchecked")
@Test
- public void testConfigRefresh(@Mocked
ClientPoolManager<HttpClientWithContext> clientMgr,
+ public void testConfigRefreshModeOne(@Mocked
ClientPoolManager<HttpClientWithContext> clientMgr,@Mocked
HttpClientWithContext httpClientWithContext) {
+ String version1 = refreshAndGetCurrentRevision(clientMgr,
httpClientWithContext, 200, "huawei");
+ //test the sdk get and change the latestRevision
+ Assert.assertEquals("huawei", version1);
+ String version2 = refreshAndGetCurrentRevision(clientMgr,
httpClientWithContext, 304, "rkd");
+ //test that when return code is 304, the sdk do not change the
latestRevision
+ Assert.assertNotEquals("rkd", version2);
+ }
+
+ private String
refreshAndGetCurrentRevision(ClientPoolManager<HttpClientWithContext> clientMgr,
+ HttpClientWithContext httpClientWithContext,int statusCode,String
version) {
+
+ ConfigCenterConfigurationSourceImpl impl = new
ConfigCenterConfigurationSourceImpl();
+ UpdateHandler updateHandler = impl.new UpdateHandler();
+ HttpClientRequest request = Mockito.mock(HttpClientRequest.class);
+
Mockito.when(request.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
+ Buffer rsp = Mockito.mock(Buffer.class);
+ Mockito.when(rsp.toString())
+
.thenReturn(String.format("{\"application\":{\"3\":\"2\",\"aa\":\"1\"},\"vmalledge\":{\"aa\":\"3\"},\"revision\":
{ \"version\": \"%s\"} }",version));
+
+ HttpClientResponse httpClientResponse =
Mockito.mock(HttpClientResponse.class);
+
Mockito.when(httpClientResponse.bodyHandler(Mockito.any(Handler.class))).then(invocation
-> {
+ Handler<Buffer> handler = invocation.getArgumentAt(0, Handler.class);
+ handler.handle(rsp);
+ return null;
+ });
+ Mockito.when(httpClientResponse.statusCode()).thenReturn(statusCode);
+
+ HttpClient httpClient = Mockito.mock(HttpClient.class);
+ Mockito.when(
+ httpClient.get(Mockito.anyInt(), Mockito.anyString(),
Mockito.anyString(), Mockito.any(Handler.class)))
+ .then(invocation -> {
+ Handler<HttpClientResponse> handler = invocation.getArgumentAt(3,
Handler.class);
+ handler.handle(httpClientResponse);
+ return request;
+ });
+
+ new MockUp<HttpClientWithContext>() {
+ @Mock
+ public void runOnContext(RunHandler handler) {
+ handler.run(httpClient);
+ }
+ };
+ new Expectations() {
+ {
+ clientMgr.findThreadBindClientPool();
+ result = httpClientWithContext;
+ }
+ };
+
+ ConfigCenterClient cc = new ConfigCenterClient(updateHandler);
+ Deencapsulation.setField(cc, "clientMgr", clientMgr);
+ ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
+ MemberDiscovery memberdis = new
MemberDiscovery(Arrays.asList("http://configcentertest:30103"));
+ ConfigRefresh refresh = cc.new ConfigRefresh(parseConfigUtils, memberdis);
+ Deencapsulation.setField(cc, "refreshMode", 1);
+ refresh.run();
+ String currentVersionInfo = Deencapsulation.getField(parseConfigUtils,
"CURRENT_VERSION_INFO").toString();
+ return currentVersionInfo;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testConfigRefreshModeZero(@Mocked
ClientPoolManager<HttpClientWithContext> clientMgr,
@Mocked HttpClientWithContext httpClientWithContext) {
ConfigCenterConfigurationSourceImpl impl = new
ConfigCenterConfigurationSourceImpl();
UpdateHandler updateHandler = impl.new UpdateHandler();
@@ -186,6 +249,11 @@ public void runOnContext(RunHandler handler) {
refresh.run();
}
+
+
+
+
+
@SuppressWarnings("unchecked")
@Test
public void testConfigRefreshException(@Mocked
ClientPoolManager<HttpClientWithContext> clientMgr,
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> [SCB-782]support revision check when use pull mode with config center
> ---------------------------------------------------------------------
>
> Key: SCB-782
> URL: https://issues.apache.org/jira/browse/SCB-782
> Project: Apache ServiceComb
> Issue Type: Improvement
> Components: Java-Chassis
> Reporter: 何一乐
> Assignee: 何一乐
> Priority: Major
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)