Skip to content
Permalink
Browse files
feat: add serviceName field to matchRule
  • Loading branch information
plusmancn committed Sep 15, 2021
1 parent f34b777 commit 2613840bbd061a72dfc063696e67df93dd457547
Showing 2 changed files with 69 additions and 35 deletions.
@@ -19,7 +19,8 @@
# Supported component versions of the test case

# Spring app
dubbo.version=3.*
spring.version=4.*, 5.*

dubbo.dubbo-samples-migration-default.version=3.*
dubbo.dubbo-samples-migration-default-test.version=2.7.*
# dubbo.dubbo-samples-migration-default.version=3.*
# dubbo.dubbo-samples-migration-default-test.version=2.7.*
@@ -29,11 +29,13 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Multi-version matcher
@@ -95,33 +97,53 @@ private void doMatch(String caseVersionsFile, String candidateVersionListStr, St
Map<String, List<String>> matchedVersionMap = new LinkedHashMap<>();

candidateVersionMap.forEach((component, candidateVersionList) -> {
List<MatchRule> matchRules = caseVersionMatchRules.get(component);
if (matchRules == null || matchRules.isEmpty()) {
Map<String, List<MatchRule>> matchRulesMap;
if (Constants.DUBBO_VERSION_KEY.equals(component) &&
!caseVersionMatchRules.containsKey(Constants.DUBBO_VERSION_KEY)
) {
matchRulesMap = caseVersionMatchRules.keySet().stream()
.filter(key -> !extractDubboServiceName(key).isEmpty())
.collect(Collectors.toMap(
key -> key,
key -> caseVersionMatchRules.get(key)
));
} else {
matchRulesMap = new HashMap<>();
if (caseVersionMatchRules.get(component) != null) {
matchRulesMap.put(component, caseVersionMatchRules.get(component));
}
}

if (matchRulesMap.isEmpty()) {
return;
}

// matching rules
List<String> matchedVersionList = new ArrayList<>();
for (String version : candidateVersionList) {
if (hasIncludeVersion(matchRules, version)) {
matchedVersionList.add(version);
for (String matchRulesKey : matchRulesMap.keySet()) {
List<MatchRule> matchRules = matchRulesMap.get(matchRulesKey);

// matching rules
List<String> matchedVersionList = new ArrayList<>();
for (String version : candidateVersionList) {
if (hasIncludeVersion(matchRules, version)) {
matchedVersionList.add(version);
}
}
}

//add case specific version
if (matchedVersionList.isEmpty() && includeCaseSpecificVersion) {
for (MatchRule matchRule : matchRules) {
if (!matchRule.isExcluded() && matchRule instanceof PlainMatchRule) {
matchedVersionList.add(((PlainMatchRule) matchRule).getVersion());
//add case specific version
if (matchedVersionList.isEmpty() && includeCaseSpecificVersion) {
for (MatchRule matchRule : matchRules) {
if (!matchRule.isExcluded() && matchRule instanceof PlainMatchRule) {
matchedVersionList.add(((PlainMatchRule) matchRule).getVersion());
}
}
}
}
if (matchedVersionList.size() > 0) {
matchedVersionMap.put(component, matchedVersionList);
if (matchedVersionList.size() > 0) {
matchedVersionMap.put(matchRulesKey, matchedVersionList);
}
}
});

// check if all component has matched version
// Check whether all components have matched version
if (caseVersionMatchRules.size() != matchedVersionMap.size()) {
List<String> components = new ArrayList<>(caseVersionMatchRules.keySet());
components.removeAll(matchedVersionMap.keySet());
@@ -169,21 +191,23 @@ private void doMatch(String caseVersionsFile, String candidateVersionListStr, St
*/
private static void chooseActiveDubboVersionRule(Map<String, List<MatchRule>> caseVersionMatchRules) {
for (String key : caseVersionMatchRules.keySet()) {
if (Constants.PATTERN_DUBBO_VERSION.matcher(key).matches() && caseVersionMatchRules.get(Constants.DUBBO_VERSION_KEY) != null) {
if (!extractDubboServiceName(key).isEmpty() && caseVersionMatchRules.get(Constants.DUBBO_VERSION_KEY) != null) {
errorAndExit(Constants.EXIT_FAILED, "The config item dubbo.version and dubbo.{service}.version can't appear simultaneously");
}
}
for (String key : caseVersionMatchRules.keySet()) {
Matcher matcher = Constants.PATTERN_DUBBO_VERSION.matcher(key);
if (matcher.matches()) {
String service = matcher.group(1);
List<MatchRule> matchRules = caseVersionMatchRules.computeIfAbsent(
Constants.DUBBO_VERSION_KEY,
item -> new ArrayList<>()
);
}
}

/**
* Extract service name from the given key
* @return the key or empty string when not matched.
*/
public static String extractDubboServiceName(String key) {
Matcher matcher = Constants.PATTERN_DUBBO_VERSION.matcher(key);
if (matcher.matches()) {
return matcher.group(1);
}

return "";
}

private static boolean hasIncludeVersion(List<MatchRule> matchRules, String version) {
@@ -253,6 +277,7 @@ private Map<String, List<MatchRule>> parseCaseVersionMatchRules(String caseVersi
}
int p = line.indexOf('=');
String component = line.substring(0, p);
String serviceName = extractDubboServiceName(component);
String patternStr = line.substring(p + 1);
patternStr = trimRule(patternStr, "[", "]");
String[] patterns = patternStr.split(",");
@@ -269,9 +294,9 @@ private Map<String, List<MatchRule>> parseCaseVersionMatchRules(String caseVersi
pattern = pattern.substring(1).trim();
}
if (pattern.contains("*")) {
ruleList.add(new WildcardMatchRule(excluded, pattern));
ruleList.add(new WildcardMatchRule(excluded, pattern, serviceName));
} else {
ruleList.add(new PlainMatchRule(excluded, pattern));
ruleList.add(new PlainMatchRule(excluded, pattern, serviceName));
}
}
}
@@ -338,7 +363,7 @@ private interface MatchRule {


/**
* Which service the MatchRule bind to
* Get service the MatchRule bind to
* @return
*/
default String getServiceName() {
@@ -348,23 +373,30 @@ default String getServiceName() {

private static abstract class ExcludableMatchRule implements MatchRule {
boolean excluded;
protected String serviceName;

public ExcludableMatchRule(boolean excluded) {
this.excluded = excluded;
}

@Override
public boolean isExcluded() {
return excluded;
}

@Override
public String getServiceName() {
return serviceName;
}
}

private static class PlainMatchRule extends ExcludableMatchRule {
private String version;

public PlainMatchRule(boolean excluded, String version) {
public PlainMatchRule(boolean excluded, String version, String serviceName) {
super(excluded);
this.version = version;
this.serviceName = serviceName;
}

@Override
@@ -386,10 +418,11 @@ private static class WildcardMatchRule extends ExcludableMatchRule {
private Pattern versionPattern;
private String versionWildcard;

public WildcardMatchRule(boolean excluded, String versionWildcard) {
public WildcardMatchRule(boolean excluded, String versionWildcard, String serviceName) {
super(excluded);
this.versionPattern = getWildcardPattern(versionWildcard);
this.versionWildcard = versionWildcard;
this.serviceName = serviceName;
}

@Override

0 comments on commit 2613840

Please sign in to comment.