Commit ffc33fb8 authored by Jason Song's avatar Jason Song

make gray release rule case-insensitive

parent 62cdc90d
...@@ -7,7 +7,7 @@ import java.util.Set; ...@@ -7,7 +7,7 @@ import java.util.Set;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class GrayReleaseRuleCache { public class GrayReleaseRuleCache implements Comparable<GrayReleaseRuleCache> {
private long ruleId; private long ruleId;
private String branchName; private String branchName;
private String namespaceName; private String namespaceName;
...@@ -67,4 +67,9 @@ public class GrayReleaseRuleCache { ...@@ -67,4 +67,9 @@ public class GrayReleaseRuleCache {
} }
return false; return false;
} }
@Override
public int compareTo(GrayReleaseRuleCache that) {
return Long.compare(this.ruleId, that.ruleId);
}
} }
...@@ -3,10 +3,10 @@ package com.ctrip.framework.apollo.biz.grayReleaseRule; ...@@ -3,10 +3,10 @@ package com.ctrip.framework.apollo.biz.grayReleaseRule;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
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.ctrip.framework.apollo.biz.config.BizConfig; import com.ctrip.framework.apollo.biz.config.BizConfig;
...@@ -23,6 +23,7 @@ import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; ...@@ -23,6 +23,7 @@ import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.tracer.Tracer; import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.tracer.spi.Transaction; import com.ctrip.framework.apollo.tracer.spi.Transaction;
import com.google.common.collect.TreeMultimap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
...@@ -61,8 +62,10 @@ public class GrayReleaseRulesHolder implements ReleaseMessageListener, Initializ ...@@ -61,8 +62,10 @@ public class GrayReleaseRulesHolder implements ReleaseMessageListener, Initializ
public GrayReleaseRulesHolder() { public GrayReleaseRulesHolder() {
loadVersion = new AtomicLong(); loadVersion = new AtomicLong();
grayReleaseRuleCache = Multimaps.synchronizedSetMultimap(HashMultimap.create()); grayReleaseRuleCache = Multimaps.synchronizedSetMultimap(
reversedGrayReleaseRuleCache = Multimaps.synchronizedSetMultimap(HashMultimap.create()); TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, Ordering.natural()));
reversedGrayReleaseRuleCache = Multimaps.synchronizedSetMultimap(
TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, Ordering.natural()));
executorService = Executors.newScheduledThreadPool(1, ApolloThreadFactory executorService = Executors.newScheduledThreadPool(1, ApolloThreadFactory
.create("GrayReleaseRulesHolder", true)); .create("GrayReleaseRulesHolder", true));
} }
......
...@@ -84,6 +84,8 @@ public class GrayReleaseRulesHolderTest { ...@@ -84,6 +84,8 @@ public class GrayReleaseRulesHolderTest {
assertEquals(someReleaseId, grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule assertEquals(someReleaseId, grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule
(someClientAppId, someClientIp, someAppId, someClusterName, someNamespaceName)); (someClientAppId, someClientIp, someAppId, someClusterName, someNamespaceName));
assertEquals(someReleaseId, grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule
(someClientAppId.toUpperCase(), someClientIp, someAppId.toUpperCase(), someClusterName, someNamespaceName.toUpperCase()));
assertNull(grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule(someClientAppId, assertNull(grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule(someClientAppId,
anotherClientIp, someAppId, someClusterName, someNamespaceName)); anotherClientIp, someAppId, someClusterName, someNamespaceName));
...@@ -94,6 +96,8 @@ public class GrayReleaseRulesHolderTest { ...@@ -94,6 +96,8 @@ public class GrayReleaseRulesHolderTest {
assertTrue(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp, assertTrue(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp,
someNamespaceName)); someNamespaceName));
assertTrue(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId.toUpperCase(), someClientIp,
someNamespaceName.toUpperCase()));
assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, anotherClientIp, assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, anotherClientIp,
someNamespaceName)); someNamespaceName));
assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp, assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp,
......
...@@ -37,7 +37,7 @@ public class GrayReleaseRuleItemDTO { ...@@ -37,7 +37,7 @@ public class GrayReleaseRuleItemDTO {
} }
private boolean appIdMatches(String clientAppId) { private boolean appIdMatches(String clientAppId) {
return this.clientAppId.equals(clientAppId); return this.clientAppId.equalsIgnoreCase(clientAppId);
} }
private boolean ipMatches(String clientIp) { private boolean ipMatches(String clientIp) {
......
...@@ -341,35 +341,52 @@ public class NotificationControllerV2Test { ...@@ -341,35 +341,52 @@ public class NotificationControllerV2Test {
} }
@Test @Test
public void testPollNotificationWithCaseInsensitiveAppId() throws Exception { public void testPollNotificationWithIncorrectCase() throws Exception {
String someWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR) String appIdWithIncorrectCase = someAppId.toUpperCase();
String namespaceWithIncorrectCase = defaultNamespace.toUpperCase();
String someMessage = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)
.join(someAppId, someCluster, defaultNamespace); .join(someAppId, someCluster, defaultNamespace);
String anotherWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR) String someWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)
.join("someAPPID", someCluster, defaultNamespace); .join(appIdWithIncorrectCase, someCluster, defaultNamespace);
Multimap<String, String> watchKeysMap = Multimap<String, String> watchKeysMap =
assembleMultiMap(defaultNamespace, Lists.newArrayList(someWatchKey)); assembleMultiMap(defaultNamespace, Lists.newArrayList(someWatchKey));
String notificationAsString = String notificationAsString =
transformApolloConfigNotificationsToString(defaultNamespace, someNotificationId); transformApolloConfigNotificationsToString(defaultNamespace.toUpperCase(), someNotificationId);
when(namespaceUtil.filterNamespaceName(namespaceWithIncorrectCase)).thenReturn(namespaceWithIncorrectCase);
when(namespaceUtil.normalizeNamespace(appIdWithIncorrectCase, namespaceWithIncorrectCase)).thenReturn(defaultNamespace);
when(watchKeysUtil when(watchKeysUtil
.assembleAllWatchKeys(someAppId, someCluster, Sets.newHashSet(defaultNamespace), .assembleAllWatchKeys(appIdWithIncorrectCase, someCluster, Sets.newHashSet(defaultNamespace),
someDataCenter)).thenReturn(watchKeysMap); someDataCenter)).thenReturn(watchKeysMap);
DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> DeferredResult<ResponseEntity<List<ApolloConfigNotification>>>
deferredResult = controller deferredResult = controller
.pollNotification(someAppId, someCluster, notificationAsString, someDataCenter, .pollNotification(appIdWithIncorrectCase, someCluster, notificationAsString, someDataCenter,
someClientIp); someClientIp);
long someId = 1; long someId = 1;
ReleaseMessage someReleaseMessage = new ReleaseMessage(anotherWatchKey); ReleaseMessage someReleaseMessage = new ReleaseMessage(someMessage);
someReleaseMessage.setId(someId); someReleaseMessage.setId(someId);
controller.handleMessage(someReleaseMessage, Topics.APOLLO_RELEASE_TOPIC); controller.handleMessage(someReleaseMessage, Topics.APOLLO_RELEASE_TOPIC);
//now both of them should have result
assertTrue(deferredResult.hasResult()); assertTrue(deferredResult.hasResult());
ResponseEntity<List<ApolloConfigNotification>> response =
(ResponseEntity<List<ApolloConfigNotification>>) deferredResult.getResult();
assertEquals(1, response.getBody().size());
ApolloConfigNotification notification = response.getBody().get(0);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(namespaceWithIncorrectCase, notification.getNamespaceName());
assertEquals(someId, notification.getNotificationId());
ApolloNotificationMessages notificationMessages = notification.getMessages();
assertEquals(1, notificationMessages.getDetails().size());
assertEquals(someId, notificationMessages.get(someMessage).longValue());
} }
private String transformApolloConfigNotificationsToString( private String transformApolloConfigNotificationsToString(
......
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