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