Commit d6a85e18 authored by Zhuohao Li's avatar Zhuohao Li Committed by Jared Tan

fix #2092. (#2696)

* fix #2092.

* fix #2092.

* change to Integer.compare.

* code clean.

* code clean.
parent 76ac64b5
...@@ -21,7 +21,9 @@ import com.google.common.collect.Lists; ...@@ -21,7 +21,9 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.collect.TreeMultimap;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -54,7 +56,7 @@ import java.util.function.Function; ...@@ -54,7 +56,7 @@ import java.util.function.Function;
public class NotificationControllerV2 implements ReleaseMessageListener { public class NotificationControllerV2 implements ReleaseMessageListener {
private static final Logger logger = LoggerFactory.getLogger(NotificationControllerV2.class); private static final Logger logger = LoggerFactory.getLogger(NotificationControllerV2.class);
private final Multimap<String, DeferredResultWrapper> deferredResults = private final Multimap<String, DeferredResultWrapper> deferredResults =
Multimaps.synchronizedSetMultimap(HashMultimap.create()); Multimaps.synchronizedSetMultimap(TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, Ordering.natural()));
private static final Splitter STRING_SPLITTER = private static final Splitter STRING_SPLITTER =
Splitter.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).omitEmptyStrings(); Splitter.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).omitEmptyStrings();
private static final Type notificationsTypeReference = private static final Type notificationsTypeReference =
......
...@@ -7,6 +7,7 @@ import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; ...@@ -7,6 +7,7 @@ import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResult;
import java.util.List; import java.util.List;
...@@ -15,7 +16,7 @@ import java.util.Map; ...@@ -15,7 +16,7 @@ import java.util.Map;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class DeferredResultWrapper { public class DeferredResultWrapper implements Comparable<DeferredResultWrapper> {
private static final ResponseEntity<List<ApolloConfigNotification>> private static final ResponseEntity<List<ApolloConfigNotification>>
NOT_MODIFIED_RESPONSE_LIST = new ResponseEntity<>(HttpStatus.NOT_MODIFIED); NOT_MODIFIED_RESPONSE_LIST = new ResponseEntity<>(HttpStatus.NOT_MODIFIED);
...@@ -64,4 +65,9 @@ public class DeferredResultWrapper { ...@@ -64,4 +65,9 @@ public class DeferredResultWrapper {
public DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> getResult() { public DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> getResult() {
return result; return result;
} }
@Override
public int compareTo(@NonNull DeferredResultWrapper deferredResultWrapper) {
return Integer.compare(this.hashCode(), deferredResultWrapper.hashCode());
}
} }
...@@ -340,6 +340,38 @@ public class NotificationControllerV2Test { ...@@ -340,6 +340,38 @@ public class NotificationControllerV2Test {
assertTrue(deferredResult.hasResult() && anotherDeferredResult.hasResult()); assertTrue(deferredResult.hasResult() && anotherDeferredResult.hasResult());
} }
@Test
public void testPollNotificationWithCaseInsensitiveAppId() throws Exception {
String someWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)
.join(someAppId, someCluster, defaultNamespace);
String anotherWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)
.join("someAPPID", someCluster, defaultNamespace);
Multimap<String, String> watchKeysMap =
assembleMultiMap(defaultNamespace, Lists.newArrayList(someWatchKey));
String notificationAsString =
transformApolloConfigNotificationsToString(defaultNamespace, someNotificationId);
when(watchKeysUtil
.assembleAllWatchKeys(someAppId, someCluster, Sets.newHashSet(defaultNamespace),
someDataCenter)).thenReturn(watchKeysMap);
DeferredResult<ResponseEntity<List<ApolloConfigNotification>>>
deferredResult = controller
.pollNotification(someAppId, someCluster, notificationAsString, someDataCenter,
someClientIp);
long someId = 1;
ReleaseMessage someReleaseMessage = new ReleaseMessage(anotherWatchKey);
someReleaseMessage.setId(someId);
controller.handleMessage(someReleaseMessage, Topics.APOLLO_RELEASE_TOPIC);
//now both of them should have result
assertTrue(deferredResult.hasResult());
}
private String transformApolloConfigNotificationsToString( private String transformApolloConfigNotificationsToString(
String namespace, long notificationId) { String namespace, long notificationId) {
List<ApolloConfigNotification> notifications = List<ApolloConfigNotification> notifications =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment