Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyouling committed Dec 18, 2023
1 parent 94a399f commit 9ad4394
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ private List<Object> getTargetInvokersByRules(List<Object> invokers, Object invo
+ DubboReflectUtils.getMethodName(invocation) + ":" + getVersion(queryMap);
List<Rule> rules = RuleUtils
.getRules(configuration, targetService, interfaceName, DubboCache.INSTANCE.getAppName());
Rule matchRule = RouteUtils.getRule(rules, DubboReflectUtils.getArguments(invocation),
Optional<Rule> matchRuleOptional = RouteUtils.getRule(rules, DubboReflectUtils.getArguments(invocation),
parseAttachments(invocation));
if (matchRule != null) {
return RuleStrategyHandler.INSTANCE.getMatchInvokers(targetService, invokers, matchRule);
if (matchRuleOptional.isPresent()) {
return RuleStrategyHandler.INSTANCE.getMatchInvokers(targetService, invokers, matchRuleOptional.get());
}
return RuleStrategyHandler.INSTANCE
.getMismatchInvokers(targetService, invokers, RuleUtils.getTags(rules, true), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ private RouteUtils() {
* @param attachments dubbo的attachments参数
* @return 匹配的路由
*/
public static Rule getRule(List<Rule> list, Object[] arguments, Map<String, Object> attachments) {
public static Optional<Rule> getRule(List<Rule> list, Object[] arguments, Map<String, Object> attachments) {
for (Rule rule : list) {
Match match = rule.getMatch();
if (match == null) {
return rule;
return Optional.of(rule);
}
List<Route> routeList;
if (!CollectionUtils.isEmpty(match.getAttachments()) && !CollectionUtils.isEmpty(attachments)) {
Expand All @@ -65,10 +65,10 @@ public static Rule getRule(List<Rule> list, Object[] arguments, Map<String, Obje
routeList = Collections.emptyList();
}
if (!CollectionUtils.isEmpty(routeList)) {
return rule;
return Optional.of(rule);
}
}
return null;
return Optional.empty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,20 @@ public AbstractRuleStrategy(String source, InstanceStrategy<I, Map<String, Strin
public List<I> getMatchInstances(String serviceName, List<I> instances, Rule rule, boolean isReplaceDash) {
// match set routes
RouteResult<?> result = RuleUtils.getTargetTags(rule.getRoute(), isReplaceDash);

// 如果命中routeTag且没有符合条件的实例,降级去fallback路由策略匹配实例
RouteResult<?> fallback = new RouteResult<>(false, new ArrayList<>());
if (result.isMatch() && !CollectionUtils.isEmpty(rule.getFallback())
&& CollectionUtils.isEmpty(getMatchTagInstances(getStrategy(result.isMatch()), result.getTags(),
instances))) {
fallback = RuleUtils.getTargetTags(rule.getFallback(), isReplaceDash);
}

// fallback中设置了路由规则命中routeTag,且有对应的实例匹配则按fallback路由规则选中实例;否则按未匹配route规则选中实例
return fallback.isMatch() && !CollectionUtils.isEmpty(getMatchTagInstances(getStrategy(fallback.isMatch()),

Check failure on line 91 in sermant-plugins/sermant-router/router-config-common/src/main/java/com/huaweicloud/sermant/router/config/strategy/AbstractRuleStrategy.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Use a single space to separate non-whitespace characters. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-plugins/sermant-router/router-config-common/src/main/java/com/huaweicloud/sermant/router/config/strategy/AbstractRuleStrategy.java:91:17: error: Use a single space to separate non-whitespace characters. (com.puppycrawl.tools.checkstyle.checks.whitespace.SingleSpaceSeparatorCheck)
fallback.getTags(), instances)) ?
getInstances(getStrategy(fallback.isMatch()), fallback.getTags(), serviceName, instances, true) :
getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName, instances, true);
fallback.getTags(), instances))
? getInstances(getStrategy(fallback.isMatch()), fallback.getTags(), serviceName, instances, true)
: getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName, instances, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.config.cache.ConfigCache;
import com.huaweicloud.sermant.router.config.entity.EnabledStrategy;
import com.huaweicloud.sermant.router.config.entity.Route;
import com.huaweicloud.sermant.router.config.entity.RouterConfiguration;
import com.huaweicloud.sermant.router.config.entity.Rule;
import com.huaweicloud.sermant.router.config.utils.RuleUtils;
Expand All @@ -35,6 +34,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* BaseLoadBalancerInterceptor服务
Expand Down Expand Up @@ -96,9 +96,9 @@ private List<Object> getTargetInstancesByRules(String targetName, List<Object> i
return instances;
}
List<Rule> rules = RuleUtils.getRules(configuration, targetName, path, AppCache.INSTANCE.getAppName());
Rule rule = RouteUtils.getRoutes(rules, header);
if (rule != null) {
return RuleStrategyHandler.INSTANCE.getMatchInstances(targetName, instances, rule);
Optional<Rule> ruleOptional = RouteUtils.getRoutes(rules, header);
if (ruleOptional.isPresent()) {
return RuleStrategyHandler.INSTANCE.getMatchInstances(targetName, instances, ruleOptional.get());
}
return RuleStrategyHandler.INSTANCE
.getMismatchInstances(targetName, instances, RuleUtils.getTags(rules, false), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

/**
* 路由插件工具类
Expand All @@ -46,14 +47,14 @@ private RouteUtils() {
* @param header header
* @return 匹配的路由
*/
public static Rule getRoutes(List<Rule> list, Map<String, List<String>> header) {
public static Optional<Rule> getRoutes(List<Rule> list, Map<String, List<String>> header) {
for (Rule rule : list) {
List<Route> routeList = getRoutes(header, rule);
if (!CollectionUtils.isEmpty(routeList)) {
return rule;
return Optional.of(rule);
}
}
return null;
return Optional.empty();
}

private static List<Route> getRoutes(Map<String, List<String>> header, Rule rule) {
Expand Down

0 comments on commit 9ad4394

Please sign in to comment.