Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ public class AppScriptStateRouter<T> extends AbstractStateRouter<T> implements C
LoggerFactory.getErrorTypeAwareLogger(AppScriptStateRouter.class);
private static final String RULE_SUFFIX = ".script-router";

private ScriptRule scriptRule;
private ScriptStateRouter<T> scriptRouter;
private volatile RouterState<T> routerState;
private String application;

private static final class RouterState<T> {
private final ScriptRule rule;
private final ScriptStateRouter<T> router;

private RouterState(ScriptRule rule, ScriptStateRouter<T> router) {
this.rule = rule;
this.router = router;
}
}

public AppScriptStateRouter(URL url) {
super(url);
}
Expand All @@ -66,16 +75,17 @@ protected BitList<Invoker<T>> doRoute(
Holder<RouterSnapshotNode<T>> routerSnapshotNodeHolder,
Holder<String> messageHolder)
throws RpcException {
if (scriptRouter == null || !scriptRule.isValid() || !scriptRule.isEnabled()) {
RouterState<T> state = routerState;
if (state == null || state.router == null || !state.rule.isValid() || !state.rule.isEnabled()) {
if (needToPrintMessage) {
messageHolder.set(
"Directly return from script router. Reason: Invokers from previous router is empty or script is not enabled. Script rule is: "
+ (scriptRule == null ? "null" : scriptRule.getRawRule()));
+ (state == null ? "null" : state.rule.getRawRule()));
}
return invokers;
}

invokers = scriptRouter.route(invokers, url, invocation, needToPrintMessage, routerSnapshotNodeHolder);
invokers = state.router.route(invokers, url, invocation, needToPrintMessage, routerSnapshotNodeHolder);

if (needToPrintMessage) {
messageHolder.set(messageHolder.get());
Expand All @@ -93,16 +103,16 @@ public synchronized void process(ConfigChangedEvent event) {

try {
if (event.getChangeType().equals(ConfigChangeType.DELETED)) {
this.scriptRule = null;
routerState = null;
} else {
this.scriptRule = ScriptRule.parse(event.getContent());
ScriptRule scriptRule = ScriptRule.parse(event.getContent());
URL scriptUrl = getUrl().addParameter(
TYPE_KEY,
isEmpty(scriptRule.getType()) ? DEFAULT_SCRIPT_TYPE_KEY : scriptRule.getType())
.addParameterAndEncoded(RULE_KEY, scriptRule.getScript())
.addParameter(FORCE_KEY, scriptRule.isForce())
.addParameter(RUNTIME_KEY, scriptRule.isRuntime());
scriptRouter = new ScriptStateRouter<>(scriptUrl);
routerState = new RouterState<>(scriptRule, new ScriptStateRouter<>(scriptUrl));
}
} catch (Exception e) {
logger.error(
Expand Down Expand Up @@ -160,6 +170,7 @@ public void stop() {

// for testing purpose
public void setScriptRule(ScriptRule scriptRule) {
this.scriptRule = scriptRule;
RouterState<T> state = routerState;
routerState = scriptRule == null ? null : new RouterState<>(scriptRule, state == null ? null : state.router);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.apache.dubbo.rpc.cluster.router.script.config;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.utils.Holder;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -82,6 +84,36 @@ void testConfigScriptRoute() {
Assertions.assertEquals(1, result.size());
}

@Test
void deletingRulePassesThroughAndRepublishingRestoresRouting() {
AppScriptStateRouter<String> router = new AppScriptStateRouter<>(url);
BitList<Invoker<String>> invokers = getInvokers();
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("sayHello");
String ruleKey = "demo-provider" + RULE_SUFFIX;

router.process(
new ConfigChangedEvent(ruleKey, DynamicConfiguration.DEFAULT_GROUP, rawRule, ConfigChangeType.ADDED));
Assertions.assertEquals(
1,
router.route(invokers.clone(), url, invocation, false, new Holder<>())
.size());

router.process(
new ConfigChangedEvent(ruleKey, DynamicConfiguration.DEFAULT_GROUP, "", ConfigChangeType.DELETED));
Assertions.assertEquals(
invokers.size(),
router.route(invokers.clone(), url, invocation, false, new Holder<>())
.size());

router.process(
new ConfigChangedEvent(ruleKey, DynamicConfiguration.DEFAULT_GROUP, rawRule, ConfigChangeType.ADDED));
Assertions.assertEquals(
1,
router.route(invokers.clone(), url, invocation, false, new Holder<>())
.size());
}

private BitList<Invoker<String>> getInvokers() {
List<Invoker<String>> originInvokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(
Expand Down
Loading