-
Notifications
You must be signed in to change notification settings - Fork 26.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Condition route v3.1 #14356
base: 3.3
Are you sure you want to change the base?
Condition route v3.1 #14356
Changes from 13 commits
a1d6b04
8801437
a6a60d0
aa5c006
d4443a6
25d7125
57b3e6a
0ad636e
6f209f9
01fa191
e35df88
dc15a8d
df02525
4838236
72c7b94
5026497
f28b686
983ac31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,13 @@ | |
import org.apache.dubbo.rpc.Invocation; | ||
import org.apache.dubbo.rpc.Invoker; | ||
import org.apache.dubbo.rpc.RpcException; | ||
import org.apache.dubbo.rpc.cluster.router.AbstractRouterRule; | ||
import org.apache.dubbo.rpc.cluster.router.RouterSnapshotNode; | ||
import org.apache.dubbo.rpc.cluster.router.condition.ConditionStateRouter; | ||
import org.apache.dubbo.rpc.cluster.router.condition.MultiDestConditionRouter; | ||
import org.apache.dubbo.rpc.cluster.router.condition.config.model.ConditionRouterRule; | ||
import org.apache.dubbo.rpc.cluster.router.condition.config.model.ConditionRuleParser; | ||
import org.apache.dubbo.rpc.cluster.router.condition.config.model.MultiDestConditionRouterRule; | ||
import org.apache.dubbo.rpc.cluster.router.state.AbstractStateRouter; | ||
import org.apache.dubbo.rpc.cluster.router.state.BitList; | ||
import org.apache.dubbo.rpc.cluster.router.state.TailStateRouter; | ||
|
@@ -42,6 +45,7 @@ | |
import java.util.stream.Collectors; | ||
|
||
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CLUSTER_FAILED_RULE_PARSING; | ||
import static org.apache.dubbo.rpc.cluster.Constants.RULE_VERSION_V31; | ||
|
||
/** | ||
* Abstract router which listens to dynamic configuration | ||
|
@@ -52,8 +56,11 @@ public abstract class ListenableStateRouter<T> extends AbstractStateRouter<T> im | |
|
||
private static final ErrorTypeAwareLogger logger = | ||
LoggerFactory.getErrorTypeAwareLogger(ListenableStateRouter.class); | ||
private volatile ConditionRouterRule routerRule; | ||
private volatile AbstractRouterRule routerRule; | ||
private volatile List<ConditionStateRouter<T>> conditionRouters = Collections.emptyList(); | ||
|
||
// for v3.1 | ||
private volatile List<MultiDestConditionRouter<T>> multiDestConditionRouters = Collections.emptyList(); | ||
private final String ruleKey; | ||
|
||
public ListenableStateRouter(URL url, String ruleKey) { | ||
|
@@ -73,6 +80,8 @@ public synchronized void process(ConfigChangedEvent event) { | |
if (event.getChangeType().equals(ConfigChangeType.DELETED)) { | ||
routerRule = null; | ||
conditionRouters = Collections.emptyList(); | ||
// for v3.1 | ||
multiDestConditionRouters = Collections.emptyList(); | ||
} else { | ||
try { | ||
routerRule = ConditionRuleParser.parse(event.getContent()); | ||
|
@@ -99,7 +108,8 @@ public BitList<Invoker<T>> doRoute( | |
Holder<RouterSnapshotNode<T>> nodeHolder, | ||
Holder<String> messageHolder) | ||
throws RpcException { | ||
if (CollectionUtils.isEmpty(invokers) || conditionRouters.size() == 0) { | ||
if (CollectionUtils.isEmpty(invokers) | ||
|| (conditionRouters.size() == 0 && multiDestConditionRouters.size() == 0)) { | ||
if (needToPrintMessage) { | ||
messageHolder.set( | ||
"Directly return. Reason: Invokers from previous router is empty or conditionRouters is empty."); | ||
|
@@ -112,18 +122,45 @@ public BitList<Invoker<T>> doRoute( | |
if (needToPrintMessage) { | ||
resultMessage = new StringBuilder(); | ||
} | ||
for (AbstractStateRouter<T> router : conditionRouters) { | ||
invokers = router.route(invokers, url, invocation, needToPrintMessage, nodeHolder); | ||
if (needToPrintMessage) { | ||
resultMessage.append(messageHolder.get()); | ||
|
||
BitList<Invoker<T>> routeResult = invokers; | ||
if (routerRule instanceof MultiDestConditionRouterRule | ||
|| routerRule.getVersion() != null && routerRule.getVersion().startsWith(RULE_VERSION_V31)) { | ||
boolean trafficDisable = false; | ||
for (MultiDestConditionRouter<T> multiDestConditionRouter : multiDestConditionRouters) { | ||
routeResult = multiDestConditionRouter.route(invokers, url, invocation, needToPrintMessage, nodeHolder); | ||
if (invokers == routeResult) { | ||
// not match or disable to continue next multiDestConditionRouter | ||
continue; | ||
} else if (routeResult.size() == 0 | ||
&& !multiDestConditionRouter.isTrafficDisable() | ||
&& !multiDestConditionRouter.isForce()) { | ||
// empty but can continue to next multiDestConditionRouter | ||
continue; | ||
} else { | ||
trafficDisable = multiDestConditionRouter.isTrafficDisable(); | ||
break; | ||
} | ||
} | ||
// if trafficDisable ignore root.force | ||
if (routeResult.size() == 0 && !routerRule.isForce() && !trafficDisable) { | ||
routeResult = invokers; | ||
} | ||
} else { | ||
for (AbstractStateRouter<T> router : conditionRouters) { | ||
routeResult = router.route(routeResult, url, invocation, needToPrintMessage, nodeHolder); | ||
} | ||
} | ||
|
||
if (needToPrintMessage) { | ||
resultMessage.append(messageHolder.get()); | ||
} | ||
|
||
if (needToPrintMessage) { | ||
messageHolder.set(resultMessage.toString()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed it here. |
||
|
||
return invokers; | ||
return routeResult; | ||
} | ||
|
||
@Override | ||
|
@@ -135,15 +172,31 @@ private boolean isRuleRuntime() { | |
return routerRule != null && routerRule.isValid() && routerRule.isRuntime(); | ||
} | ||
|
||
private void generateConditions(ConditionRouterRule rule) { | ||
if (rule != null && rule.isValid()) { | ||
this.conditionRouters = rule.getConditions().stream() | ||
.map(condition -> | ||
new ConditionStateRouter<T>(getUrl(), condition, rule.isForce(), rule.isEnabled())) | ||
.collect(Collectors.toList()); | ||
private void generateConditions(AbstractRouterRule rule) { | ||
if (rule == null || !rule.isValid()) { | ||
return; | ||
} | ||
|
||
if (rule instanceof ConditionRouterRule) { | ||
this.conditionRouters = ((ConditionRouterRule) rule) | ||
.getConditions().stream() | ||
.map(condition -> | ||
new ConditionStateRouter<T>(getUrl(), condition, rule.isForce(), rule.isEnabled())) | ||
.collect(Collectors.toList()); | ||
|
||
for (ConditionStateRouter<T> conditionRouter : this.conditionRouters) { | ||
conditionRouter.setNextRouter(TailStateRouter.getInstance()); | ||
} | ||
} else if (rule instanceof MultiDestConditionRouterRule) { | ||
this.multiDestConditionRouters = ((MultiDestConditionRouterRule) rule) | ||
.getConditions().stream() | ||
.map(condition -> new MultiDestConditionRouter<T>(getUrl(), condition, rule.isEnabled())) | ||
.sorted((a, b) -> a.getPriority() - b.getPriority()) | ||
.collect(Collectors.toList()); | ||
|
||
for (MultiDestConditionRouter<T> conditionRouter : this.multiDestConditionRouters) { | ||
conditionRouter.setNextRouter(TailStateRouter.getInstance()); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,21 @@ | |
*/ | ||
package org.apache.dubbo.rpc.cluster.router.condition.config.model; | ||
|
||
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; | ||
import org.apache.dubbo.common.logger.LoggerFactory; | ||
import org.apache.dubbo.common.utils.CollectionUtils; | ||
import org.apache.dubbo.rpc.cluster.router.AbstractRouterRule; | ||
|
||
import java.util.Map; | ||
|
||
import org.yaml.snakeyaml.LoaderOptions; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.constructor.SafeConstructor; | ||
|
||
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CLUSTER_FAILED_RULE_PARSING; | ||
import static org.apache.dubbo.rpc.cluster.Constants.CONFIG_VERSION_KEY; | ||
import static org.apache.dubbo.rpc.cluster.Constants.RULE_VERSION_V31; | ||
|
||
/** | ||
* %YAML1.2 | ||
* | ||
|
@@ -40,14 +47,35 @@ | |
*/ | ||
public class ConditionRuleParser { | ||
|
||
public static ConditionRouterRule parse(String rawRule) { | ||
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ConditionRuleParser.class); | ||
|
||
public static AbstractRouterRule parse(String rawRule) { | ||
AbstractRouterRule rule; | ||
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions())); | ||
Map<String, Object> map = yaml.load(rawRule); | ||
ConditionRouterRule rule = ConditionRouterRule.parseFromMap(map); | ||
rule.setRawRule(rawRule); | ||
if (CollectionUtils.isEmpty(rule.getConditions())) { | ||
String confVersion = (String) map.get(CONFIG_VERSION_KEY); | ||
|
||
if (confVersion != null && confVersion.toLowerCase().startsWith(RULE_VERSION_V31)) { | ||
rule = MultiDestConditionRouterRule.parseFromMap(map); | ||
if (CollectionUtils.isEmpty(((MultiDestConditionRouterRule) rule).getConditions())) { | ||
rule.setValid(false); | ||
} | ||
} else if (confVersion != null && confVersion.compareToIgnoreCase(RULE_VERSION_V31) > 0) { | ||
logger.warn( | ||
CLUSTER_FAILED_RULE_PARSING, | ||
"Invalid condition config version number.", | ||
"", | ||
"Ignore this configuration. Only " + RULE_VERSION_V31 + " and below are supported in this release"); | ||
rule = ConditionRouterRule.parseFromMap(map); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we continue with parsing when found configVersion not match? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative is to simply create a new ConditionRouterRule object.Otherwise, the rule property is null. Even though there may be a null test, I think it's best to try not to generate null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just set it to null and add a unit test |
||
rule.setValid(false); | ||
} else { | ||
// for under v3.1 | ||
rule = ConditionRouterRule.parseFromMap(map); | ||
if (CollectionUtils.isEmpty(((ConditionRouterRule) rule).getConditions())) { | ||
rule.setValid(false); | ||
} | ||
} | ||
rule.setRawRule(rawRule); | ||
|
||
return rule; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.dubbo.rpc.cluster.router.condition.config.model; | ||
|
||
import org.apache.dubbo.rpc.cluster.router.condition.matcher.ConditionMatcher; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.apache.dubbo.rpc.cluster.Constants.DefaultRouteConditionSubSetWeight; | ||
|
||
public class ConditionSubSet { | ||
private Map<String, ConditionMatcher> condition = new HashMap<>(); | ||
private Integer subSetWeight; | ||
|
||
public ConditionSubSet() {} | ||
|
||
public ConditionSubSet(Map<String, ConditionMatcher> condition, Integer subSetWeight) { | ||
this.condition = condition; | ||
this.subSetWeight = subSetWeight; | ||
if (subSetWeight <= 0) { | ||
this.subSetWeight = DefaultRouteConditionSubSetWeight; | ||
} | ||
} | ||
|
||
public Map<String, ConditionMatcher> getCondition() { | ||
return condition; | ||
} | ||
|
||
public void setCondition(Map<String, ConditionMatcher> condition) { | ||
this.condition = condition; | ||
} | ||
|
||
public Integer getSubSetWeight() { | ||
return subSetWeight; | ||
} | ||
|
||
public void setSubSetWeight(int subSetWeight) { | ||
this.subSetWeight = subSetWeight; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ConditionSubSet{" + "cond=" + condition + ", subSetWeight=" + subSetWeight + '}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.dubbo.rpc.cluster.router.condition.config.model; | ||
|
||
import org.apache.dubbo.rpc.Invoker; | ||
import org.apache.dubbo.rpc.cluster.router.state.BitList; | ||
|
||
public class Destination<T> { | ||
private int weight; | ||
private BitList<Invoker<T>> invokers; | ||
|
||
Destination(int weight, BitList<Invoker<T>> invokers) { | ||
this.weight = weight; | ||
this.invokers = invokers; | ||
} | ||
|
||
public int getWeight() { | ||
return weight; | ||
} | ||
|
||
public void setWeight(int weight) { | ||
this.weight = weight; | ||
} | ||
|
||
public BitList<Invoker<T>> getInvokers() { | ||
return invokers; | ||
} | ||
|
||
public void setInvokers(BitList<Invoker<T>> invokers) { | ||
this.invokers = invokers; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should clone
invokers
here firstThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if cloning is necessary here; the v3.0 logic just passes the filtering on invokers itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the route method, the value in invokers is not modified directly, and there are three cases:
1.Return invokers directly
2. return BItlist.empty
3. The invokers will be cloned and modified then return
so I don't think it is necessary to clone first here, just keep the same with v3.0