Skip to content

Commit

Permalink
transplant pull request 5138 & 5198
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Feb 7, 2025
1 parent 7fe8f4e commit a070aa1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class GrayReleaseRulesHolder implements ReleaseMessageListener, Initializ
private Multimap<String, GrayReleaseRuleCache> grayReleaseRuleCache;
//store clientAppId+clientNamespace+ip -> ruleId map
private Multimap<String, Long> reversedGrayReleaseRuleCache;
//store clientAppId+clientNamespace+label -> ruleId map
private Multimap<String, Long> reversedGrayReleaseRuleLabelCache;
//an auto increment version to indicate the age of rules
private AtomicLong loadVersion;

Expand All @@ -80,6 +82,8 @@ public GrayReleaseRulesHolder(final GrayReleaseRuleRepository grayReleaseRuleRep
TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, Ordering.natural()));
reversedGrayReleaseRuleCache = Multimaps.synchronizedSetMultimap(
TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, Ordering.natural()));
reversedGrayReleaseRuleLabelCache = Multimaps.synchronizedSetMultimap(
TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, Ordering.natural()));
executorService = Executors.newScheduledThreadPool(1, ApolloThreadFactory
.create("GrayReleaseRulesHolder", true));
}
Expand Down Expand Up @@ -152,15 +156,29 @@ public Long findReleaseIdFromGrayReleaseRule(String clientAppId, String clientIp
}

/**
* Check whether there are gray release rules for the clientAppId, clientIp, namespace
* combination. Please note that even there are gray release rules, it doesn't mean it will always
* load gray releases. Because gray release rules actually apply to one more dimension - cluster.
* Check whether there are gray release rules for the clientAppId, clientIp, clientLabel, namespace combination.
* Please note that even there are gray release rules, it doesn't mean it will always load gray
* releases. Because gray release rules actually apply to one more dimension - cluster.
*/
public boolean hasGrayReleaseRule(String clientAppId, String clientIp, String namespaceName) {
return reversedGrayReleaseRuleCache.containsKey(assembleReversedGrayReleaseRuleKey(clientAppId,
public boolean hasGrayReleaseRule(String clientAppId, String clientIp, String clientLabel,
String namespaceName) {
// check ip gray rule
if (reversedGrayReleaseRuleCache.containsKey(assembleReversedGrayReleaseRuleKey(clientAppId,
namespaceName, clientIp)) || reversedGrayReleaseRuleCache.containsKey
(assembleReversedGrayReleaseRuleKey(clientAppId, namespaceName, GrayReleaseRuleItemDTO
.ALL_IP));
.ALL_IP))) {
return true;
}
// check label gray rule
if (!Strings.isNullOrEmpty(clientLabel) &&
(reversedGrayReleaseRuleLabelCache.containsKey(
assembleReversedGrayReleaseRuleKey(clientAppId, namespaceName, clientLabel)) ||
reversedGrayReleaseRuleLabelCache.containsKey(
assembleReversedGrayReleaseRuleKey(clientAppId, namespaceName,
GrayReleaseRuleItemDTO.ALL_Label)))) {
return true;
}
return false;
}

private void scanGrayReleaseRules() {
Expand Down Expand Up @@ -232,6 +250,10 @@ private void addCache(String key, GrayReleaseRuleCache ruleCache) {
reversedGrayReleaseRuleCache.put(assembleReversedGrayReleaseRuleKey(ruleItemDTO
.getClientAppId(), ruleCache.getNamespaceName(), clientIp), ruleCache.getRuleId());
}
for (String label : ruleItemDTO.getClientLabelList()) {
reversedGrayReleaseRuleLabelCache.put(assembleReversedGrayReleaseRuleKey(ruleItemDTO
.getClientAppId(), ruleCache.getNamespaceName(), label), ruleCache.getRuleId());
}
}
}
grayReleaseRuleCache.put(key, ruleCache);
Expand All @@ -244,6 +266,10 @@ private void removeCache(String key, GrayReleaseRuleCache ruleCache) {
reversedGrayReleaseRuleCache.remove(assembleReversedGrayReleaseRuleKey(ruleItemDTO
.getClientAppId(), ruleCache.getNamespaceName(), clientIp), ruleCache.getRuleId());
}
for (String label : ruleItemDTO.getClientLabelList()) {
reversedGrayReleaseRuleLabelCache.remove(assembleReversedGrayReleaseRuleKey(ruleItemDTO
.getClientAppId(), ruleCache.getNamespaceName(), label), ruleCache.getRuleId());
}
}
}

Expand Down Expand Up @@ -282,8 +308,8 @@ private String assembleGrayReleaseRuleKey(String configAppId, String configClust
}

private String assembleReversedGrayReleaseRuleKey(String clientAppId, String
clientNamespaceName, String clientIp) {
return STRING_JOINER.join(clientAppId, clientNamespaceName, clientIp);
clientNamespaceName, String clientIpOrLabel) {
return STRING_JOINER.join(clientAppId, clientNamespaceName, clientIpOrLabel);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package com.ctrip.framework.apollo.biz.registry.configuration.support;

import com.ctrip.framework.apollo.biz.registry.ServiceInstance;
import com.google.common.base.Strings;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
Expand Down Expand Up @@ -72,6 +74,9 @@ public class ApolloServiceRegistryProperties implements ServiceInstance {
@Autowired
private InetUtils inetUtils;

@Autowired
private ServletContext servletContext;

/**
* if user doesn't config, then resolve them on the runtime.
*/
Expand All @@ -84,7 +89,9 @@ public void postConstruct() {
if (this.uri == null) {
String host = this.inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
Integer port = propertyResolver.getRequiredProperty("server.port", Integer.class);
String uriString = "http://" + host + ":" + port + "/";
String contextPath = Strings.isNullOrEmpty(this.servletContext.getContextPath()) ? "/"
: this.servletContext.getContextPath();
String uriString = "http://" + host + ":" + port + contextPath;
this.uri = URI.create(uriString);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,29 @@ public void testScanGrayReleaseRules() throws Exception {
assertNull(grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule(anotherClientAppId,
anotherClientIp, anotherClientLabel, someAppId, someClusterName, someNamespaceName));

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,
anotherNamespaceName));
assertTrue(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp, someClientLabel,
someNamespaceName));
assertTrue(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId.toUpperCase(), someClientIp,
someClientLabel, someNamespaceName.toUpperCase()));
assertTrue(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, anotherClientIp, someClientLabel,
someNamespaceName));
assertTrue(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId.toUpperCase(), anotherClientIp,
someClientLabel, someNamespaceName.toUpperCase()));
assertFalse(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, anotherClientIp,
anotherClientLabel, someNamespaceName));
assertFalse(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp, someClientLabel,
anotherNamespaceName));

assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, anotherClientIp,
someNamespaceName));
anotherClientLabel, someNamespaceName));
assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, anotherClientIp,
anotherNamespaceName));
anotherClientLabel, anotherNamespaceName));

GrayReleaseRule anotherRule = assembleGrayReleaseRule(someAppId, someClusterName,
someNamespaceName, Lists.newArrayList(assembleRuleItem(anotherClientAppId, Sets.newHashSet
Expand All @@ -143,18 +153,22 @@ public void testScanGrayReleaseRules() throws Exception {
assertEquals(someReleaseId, grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule
(anotherClientAppId, anotherClientIp, anotherClientLabel, someAppId, someClusterName, someNamespaceName));

assertFalse(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp, someClientLabel, someNamespaceName));
assertFalse(
grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp, someClientLabel, anotherNamespaceName));

assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp,
someNamespaceName));
assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(someClientAppId, someClientIp,
anotherNamespaceName));

assertTrue(grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, anotherClientIp,
someNamespaceName));
assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, someClientIp,
someNamespaceName));
anotherClientLabel, someNamespaceName));
assertTrue(grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, anotherClientIp,
someClientLabel, someNamespaceName));
assertTrue(grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, someClientIp,
anotherClientLabel, someNamespaceName));
assertFalse(
grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, someClientIp, someClientLabel,
someNamespaceName));
assertFalse(grayReleaseRulesHolder.hasGrayReleaseRule(anotherClientAppId, anotherClientIp,
anotherNamespaceName));
anotherClientLabel, anotherNamespaceName));
}

private GrayReleaseRule assembleGrayReleaseRule(String appId, String clusterName, String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ String queryConfig(ConfigFileOutputFormat outputFormat, String appId, String clu

//1. check whether this client has gray release rules
boolean hasGrayReleaseRule = grayReleaseRulesHolder.hasGrayReleaseRule(appId, clientIp,
namespace);
clientLabel, namespace);

String cacheKey = assembleCacheKey(outputFormat, appId, clusterName, namespace, dataCenter);

Expand All @@ -200,7 +200,7 @@ String queryConfig(ConfigFileOutputFormat outputFormat, String appId, String clu
}
//5. Double check if this client needs to load gray release, if yes, load from db again
//This step is mainly to avoid cache pollution
if (grayReleaseRulesHolder.hasGrayReleaseRule(appId, clientIp, namespace)) {
if (grayReleaseRulesHolder.hasGrayReleaseRule(appId, clientIp, clientLabel, namespace)) {
Tracer.logEvent("ConfigFile.Cache.GrayReleaseConflict", cacheKey);
return loadConfig(outputFormat, appId, clusterName, namespace, dataCenter, clientIp, clientLabel,
request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void setUp() throws Exception {

when(namespaceUtil.filterNamespaceName(someNamespace)).thenReturn(someNamespace);
when(namespaceUtil.normalizeNamespace(someAppId, someNamespace)).thenReturn(someNamespace);
when(grayReleaseRulesHolder.hasGrayReleaseRule(anyString(), anyString(), anyString()))
.thenReturn(false);
when(grayReleaseRulesHolder.hasGrayReleaseRule(anyString(), anyString(), anyString(),
anyString())).thenReturn(false);

watchedKeys2CacheKey =
(Multimap<String, String>) ReflectionTestUtils
Expand Down Expand Up @@ -199,8 +199,8 @@ public void testQueryConfigWithGrayRelease() throws Exception {
Map<String, String> configurations =
ImmutableMap.of(someKey, someValue);

when(grayReleaseRulesHolder.hasGrayReleaseRule(someAppId, someClientIp, someNamespace))
.thenReturn(true);
when(grayReleaseRulesHolder.hasGrayReleaseRule(someAppId, someClientIp, someClientLabel,
someNamespace)).thenReturn(true);

ApolloConfig someApolloConfig = mock(ApolloConfig.class);
when(someApolloConfig.getConfigurations()).thenReturn(configurations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ config_export_module.controller('ConfigExportController',
}

var selectedEnvStr = selectedEnvs.join(",");
$window.location.href = '/configs/export?envs=' + selectedEnvStr;
$window.location.href = AppUtil.prefixPath() + '/configs/export?envs=' + selectedEnvStr;

toastr.success($translate.instant('ConfigExport.ExportSuccess'));
};
Expand Down Expand Up @@ -93,7 +93,7 @@ config_export_module.controller('ConfigExportController',
form.append('file', file);
$http({
method: 'POST',
url: '/configs/import?envs=' + selectedEnvStr + "&conflictAction="
url: AppUtil.prefixPath() + '/configs/import?envs=' + selectedEnvStr + "&conflictAction="
+ $scope.conflictAction,
data: form,
headers: {'Content-Type': undefined},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function importNamespaceModalDirective($window, $q, $translate, $http, toastr, A
form.append('file', file);
$http({
method: 'POST',
url: '/apps/' + toImportNamespace.baseInfo.appId + '/envs/' + scope.env + '/clusters/'
url: AppUtil.prefixPath() + '/apps/' + toImportNamespace.baseInfo.appId + '/envs/' + scope.env + '/clusters/'
+ toImportNamespace.baseInfo.clusterName
+ '/namespaces/' + toImportNamespace.baseInfo.namespaceName + "/items/import",
data: form,
Expand Down

0 comments on commit a070aa1

Please sign in to comment.