Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
wcy666103 committed Jul 21, 2024
1 parent 3dcf6bf commit 94be54b
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public MultiDestConditionRouter(URL url, MultiDestCondition multiDestCondition,
super(url);
this.setForce(force);
this.enabled = enabled;
matcherFactories = moduleModel.getExtensionLoader(ConditionMatcherFactory.class)
.getActivateExtensions();
matcherFactories =
moduleModel.getExtensionLoader(ConditionMatcherFactory.class).getActivateExtensions();
this.init(multiDestCondition.getFrom(), multiDestCondition.getTo());
}

Expand All @@ -82,12 +82,15 @@ public void init(Map<String, String> from, List<Map<String, String>> to) {
List<ConditionSubSet> thenConditions = new ArrayList<>();
for (Map<String, String> toMap : to) {
String thenRule = toMap.get("match");
Map<String, ConditionMatcher> then = StringUtils.isBlank(thenRule)
|| "false".equals(thenRule) ? new HashMap<>() : parseRule(thenRule);
Map<String, ConditionMatcher> then = StringUtils.isBlank(thenRule) || "false".equals(thenRule)
? new HashMap<>()
: parseRule(thenRule);
// NOTE: It should be determined on the business level whether the `When condition` can be empty or not.

thenConditions.add(new ConditionSubSet(then, Integer.valueOf(toMap.getOrDefault("weight",
String.valueOf(DefaultRouteConditionSubSetWeight)))));
thenConditions.add(new ConditionSubSet(
then,
Integer.valueOf(
toMap.getOrDefault("weight", String.valueOf(DefaultRouteConditionSubSetWeight)))));
}
this.thenCondition = thenConditions;
} catch (ParseException e) {
Expand Down Expand Up @@ -127,7 +130,8 @@ else if ("=".equals(separator)) {
if (matcherPair == null) {
throw new ParseException(
"Illegal route rule \"" + rule + "\", The error char '" + separator + "' at index "
+ matcher.start() + " before \"" + content + "\".", matcher.start());
+ matcher.start() + " before \"" + content + "\".",
matcher.start());
}

values = matcherPair.getMatches();
Expand All @@ -138,7 +142,8 @@ else if ("!=".equals(separator)) {
if (matcherPair == null) {
throw new ParseException(
"Illegal route rule \"" + rule + "\", The error char '" + separator + "' at index "
+ matcher.start() + " before \"" + content + "\".", matcher.start());
+ matcher.start() + " before \"" + content + "\".",
matcher.start());
}

values = matcherPair.getMismatches();
Expand All @@ -149,13 +154,15 @@ else if (",".equals(separator)) { // Should be separated by ','
if (values == null || values.isEmpty()) {
throw new ParseException(
"Illegal route rule \"" + rule + "\", The error char '" + separator + "' at index "
+ matcher.start() + " before \"" + content + "\".", matcher.start());
+ matcher.start() + " before \"" + content + "\".",
matcher.start());
}
values.add(content);
} else {
throw new ParseException(
"Illegal route rule \"" + rule + "\", The error char '" + separator + "' at index "
+ matcher.start() + " before \"" + content + "\".", matcher.start());
+ matcher.start() + " before \"" + content + "\".",
matcher.start());
}
}
return condition;
Expand All @@ -167,7 +174,8 @@ private ConditionMatcher getMatcher(String key) {
return factory.createMatcher(key, moduleModel);
}
}
return moduleModel.getExtensionLoader(ConditionMatcherFactory.class)
return moduleModel
.getExtensionLoader(ConditionMatcherFactory.class)
.getExtension("param")
.createMatcher(key, moduleModel);
}
Expand All @@ -179,7 +187,8 @@ protected BitList<Invoker<T>> doRoute(
Invocation invocation,
boolean needToPrintMessage,
Holder<RouterSnapshotNode<T>> routerSnapshotNodeHolder,
Holder<String> messageHolder) throws RpcException {
Holder<String> messageHolder)
throws RpcException {

if (!enabled) {
if (needToPrintMessage) {
Expand All @@ -203,7 +212,10 @@ protected BitList<Invoker<T>> doRoute(
return invokers;
}
if (thenCondition == null || thenCondition.size() == 0) {
logger.warn(CLUSTER_CONDITIONAL_ROUTE_LIST_EMPTY, "condition state router thenCondition is empty", "",
logger.warn(
CLUSTER_CONDITIONAL_ROUTE_LIST_EMPTY,
"condition state router thenCondition is empty",
"",
"The current consumer in the service blocklist. consumer: " + NetUtils.getLocalHost()
+ ", service: " + url.getServiceKey());
if (needToPrintMessage) {
Expand All @@ -222,18 +234,22 @@ protected BitList<Invoker<T>> doRoute(
}
}
if (!res.isEmpty()) {
destinations.addDestination(condition.getSubSetWeight()
== null ? DefaultRouteConditionSubSetWeight : condition.getSubSetWeight(), res.clone());
destinations.addDestination(
condition.getSubSetWeight() == null
? DefaultRouteConditionSubSetWeight
: condition.getSubSetWeight(),
res.clone());
}
}

if (!destinations.getDestinations()
.isEmpty()) {
if (!destinations.getDestinations().isEmpty()) {
BitList<Invoker<T>> res = destinations.randDestination();
return res;
} else if (this.isForce()) {
logger.warn(CLUSTER_CONDITIONAL_ROUTE_LIST_EMPTY, "execute condition state router result list is "
+ "empty. and force=true", "",
logger.warn(
CLUSTER_CONDITIONAL_ROUTE_LIST_EMPTY,
"execute condition state router result list is " + "empty. and force=true",
"",
"The route result is empty and force execute. consumer: " + NetUtils.getLocalHost()
+ ", service: " + url.getServiceKey() + ", router: "
+ url.getParameterAndDecoded(RULE_KEY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public BitList<Invoker<T>> doRoute(
List<? extends AbstractStateRouter<T>> routers;
if (routerRule instanceof MultiDestConditionRouterRule) {
routers = multiDestConditionRouters;
}else {
} else {
routers = conditionRouters;
}

Expand Down Expand Up @@ -170,7 +170,8 @@ private void generateConditions(AbstractRouterRule rule) {
} else if (rule instanceof MultiDestConditionRouterRule) {
this.multiDestConditionRouters = ((MultiDestConditionRouterRule) rule)
.getConditions().stream()
.map(condition -> new MultiDestConditionRouter<T>(getUrl(), condition,rule.isForce(), rule.isEnabled()))
.map(condition -> new MultiDestConditionRouter<T>(
getUrl(), condition, rule.isForce(), rule.isEnabled()))
.collect(Collectors.toList());

for (MultiDestConditionRouter<T> conditionRouter : this.multiDestConditionRouters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.dubbo.rpc.cluster.router.AbstractRouterRule;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

Expand Down
Loading

0 comments on commit 94be54b

Please sign in to comment.