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 cfbed4b commit 045556e
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.utils.StringUtils;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.config.entity.Route;
import com.huaweicloud.sermant.router.config.entity.Rule;
import com.huaweicloud.sermant.router.config.utils.RuleUtils;
import com.huaweicloud.sermant.router.config.utils.RuleUtils.RouteResult;
Expand Down Expand Up @@ -78,20 +77,28 @@ 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);
if (result.isMatch() && !CollectionUtils.isEmpty(rule.getFallback())) {
// fallback有设置路由规则时,仅返回匹配的实例,如果存在直接返回
List<I> routeInstances = getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName,
instances, false);
if (!CollectionUtils.isEmpty(routeInstances)) {
return routeInstances;
}
}

// route设置的目标标签未匹配实例时,fallback有设置路由,通过fallback路由目标标签实例
if (!CollectionUtils.isEmpty(rule.getFallback())) {
RouteResult<?> fallback = RuleUtils.getTargetTags(rule.getFallback(), isReplaceDash);
List<I> fallbackInstances = getInstances(getStrategy(fallback.isMatch()), fallback.getTags(), serviceName,
instances, false);

// 如果命中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路由规则选中实例,返回对应实例
if (!CollectionUtils.isEmpty(fallbackInstances)) {
return fallbackInstances;
}
}

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

@Override
Expand Down Expand Up @@ -125,7 +132,12 @@ public List<I> getZoneInstances(String serviceName, List<I> instances, String zo

private <T> List<I> getInstances(InstanceStrategy<I, T> instanceStrategy, T tags, String serviceName,
List<I> instances, boolean isReturnAllInstancesWhenMismatch) {
List<I> resultList = getMatchTagInstances(instanceStrategy, tags, instances);
List<I> resultList = new ArrayList<>();
for (I instance : instances) {
if (instanceStrategy.isMatch(instance, tags, mapper)) {
resultList.add(instance);
}
}
boolean mismatch = CollectionUtils.isEmpty(resultList);
if (!mismatch) {
if (LOGGER.isLoggable(Level.FINE)) {
Expand All @@ -144,16 +156,6 @@ private <T> List<I> getInstances(InstanceStrategy<I, T> instanceStrategy, T tags
return isReturnAllInstancesWhenMismatch && mismatch ? instances : resultList;
}

private <T> List<I> getMatchTagInstances(InstanceStrategy<I, T> instanceStrategy, T tags, List<I> instances) {
List<I> resultList = new ArrayList<>();
for (I instance : instances) {
if (instanceStrategy.isMatch(instance, tags, mapper)) {
resultList.add(instance);
}
}
return resultList;
}

private <T> InstanceStrategy<I, T> getStrategy(boolean isMatch) {
return isMatch ? (InstanceStrategy<I, T>) matchInstanceStrategy
: (InstanceStrategy<I, T>) mismatchInstanceStrategy;
Expand Down

0 comments on commit 045556e

Please sign in to comment.