You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/10/12 09:07:40 UTC

[GitHub] [dubbo] guohao commented on a diff in pull request #10691: Add dual chain support for state router, prevent update when invoke

guohao commented on code in PR #10691:
URL: https://github.com/apache/dubbo/pull/10691#discussion_r993201655


##########
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/RouterChain.java:
##########
@@ -103,264 +76,95 @@ public static <T> RouterChain<T> buildChain(Class<T> interfaceClass, URL url) {
 
         RouterSnapshotSwitcher routerSnapshotSwitcher = ScopeModelUtil.getFrameworkModel(moduleModel).getBeanFactory().getBean(RouterSnapshotSwitcher.class);
 
-        return new RouterChain<>(routers, stateRouters, shouldFailFast, routerSnapshotSwitcher);
+        return new SingleRouterChain<>(routers, stateRouters, shouldFailFast, routerSnapshotSwitcher);
     }
 
-    public RouterChain(List<Router> routers, List<StateRouter<T>> stateRouters, boolean shouldFailFast, RouterSnapshotSwitcher routerSnapshotSwitcher) {
-        initWithRouters(routers);
-
-        initWithStateRouters(stateRouters);
-
-        this.shouldFailFast = shouldFailFast;
-        this.routerSnapshotSwitcher = routerSnapshotSwitcher;
-    }
-
-    private void initWithStateRouters(List<StateRouter<T>> stateRouters) {
-        StateRouter<T> stateRouter = TailStateRouter.getInstance();
-        for (int i = stateRouters.size() - 1; i >= 0; i--) {
-            StateRouter<T> nextStateRouter = stateRouters.get(i);
-            nextStateRouter.setNextRouter(stateRouter);
-            stateRouter = nextStateRouter;
+    public RouterChain(SingleRouterChain<T>[] chains) {
+        if (chains.length != 2) {
+            throw new IllegalArgumentException("chains' size should be 2.");
         }
-        this.headStateRouter = stateRouter;
-        this.stateRouters = Collections.unmodifiableList(stateRouters);
+        this.mainChain = chains[0];
+        this.backupChain = chains[1];
+        this.currentChain = this.mainChain;
     }
 
-    /**
-     * the resident routers must being initialized before address notification.
-     * only for ut
-     */
-    public void initWithRouters(List<Router> builtinRouters) {
-        this.builtinRouters = builtinRouters;
-        this.routers = new LinkedList<>(builtinRouters);
+    public List<Invoker<T>> route(URL url, BitList<Invoker<T>> availableInvokers, Invocation invocation) {
+        return currentChain.route(url, availableInvokers, invocation);
     }
 
     /**
-     * If we use route:// protocol in version before 2.7.0, each URL will generate a Router instance, so we should
-     * keep the routers up to date, that is, each time router URLs changes, we should update the routers list, only
-     * keep the builtinRouters which are available all the time and the latest notified routers which are generated
-     * from URLs.
-     *
-     * @param routers routers from 'router://' rules in 2.6.x or before.
+     * Notify router chain of the initial addresses from registry at the first time.
+     * Notify whenever addresses in registry change.
      */
-    public void addRouters(List<Router> routers) {
-        List<Router> newRouters = new LinkedList<>();
-        newRouters.addAll(builtinRouters);
-        newRouters.addAll(routers);
-        CollectionUtils.sort(newRouters);
-        this.routers = newRouters;
-    }
-
-    public List<Router> getRouters() {
-        return routers;
-    }
-
-    public StateRouter<T> getHeadStateRouter() {
-        return headStateRouter;
-    }
-
-    public List<Invoker<T>> route(URL url, BitList<Invoker<T>> availableInvokers, Invocation invocation) {
-        if (RpcContext.getServiceContext().isNeedPrintRouterSnapshot()) {
-            return routeAndPrint(url, availableInvokers, invocation);
-        } else {
-            return simpleRoute(url, availableInvokers, invocation);
-        }
-    }
+    public synchronized void setInvokers(BitList<Invoker<T>> invokers) {
+        // 1. switch
+        currentChain = backupChain;
 
-    public List<Invoker<T>> routeAndPrint(URL url, BitList<Invoker<T>> availableInvokers, Invocation invocation) {
-        RouterSnapshotNode<T> snapshot = buildRouterSnapshot(url, availableInvokers, invocation);
-        logRouterSnapshot(url, invocation, snapshot);
-        return snapshot.getChainOutputInvokers();
-    }
-
-    public List<Invoker<T>> simpleRoute(URL url, BitList<Invoker<T>> availableInvokers, Invocation invocation) {
-        BitList<Invoker<T>> resultInvokers = availableInvokers.clone();
+        // 2. wait
+        waitChain(mainChain);
 
-        // 1. route state router
-        resultInvokers = headStateRouter.route(resultInvokers, url, invocation, false, null);
-        if (resultInvokers.isEmpty() && (shouldFailFast || routers.isEmpty())) {
-            printRouterSnapshot(url, availableInvokers, invocation);
-            return BitList.emptyList();
-        }
+        // 3. notify
+        mainChain.setInvokers(invokers);
 
-        if (routers.isEmpty()) {
-            return resultInvokers;
-        }
-        List<Invoker<T>> commonRouterResult = resultInvokers.cloneToArrayList();
-        // 2. route common router
-        for (Router router : routers) {
-            // Copy resultInvokers to a arrayList. BitList not support
-            RouterResult<Invoker<T>> routeResult = router.route(commonRouterResult, url, invocation, false);
-            commonRouterResult = routeResult.getResult();
-            if (CollectionUtils.isEmpty(commonRouterResult) && shouldFailFast) {
-                printRouterSnapshot(url, availableInvokers, invocation);
-                return BitList.emptyList();
-            }
-
-            // stop continue routing
-            if (!routeResult.isNeedContinueRoute()) {
-                return commonRouterResult;
-            }
-        }
+        // 4. switch back
+        currentChain = mainChain;
 
-        if (commonRouterResult.isEmpty()) {
-            printRouterSnapshot(url, availableInvokers, invocation);
-            return BitList.emptyList();
-        }
+        // 5. wait
+        waitChain(backupChain);
 
-        return commonRouterResult;
+        // 6. notify
+        backupChain.setInvokers(invokers);
     }
 
-    /**
-     * store each router's input and output, log out if empty
-     */
-    private void printRouterSnapshot(URL url, BitList<Invoker<T>> availableInvokers, Invocation invocation) {
-        if (logger.isWarnEnabled()) {
-            logRouterSnapshot(url, invocation, buildRouterSnapshot(url, availableInvokers, invocation));
+    private void waitChain(SingleRouterChain<T> oldChain) {
+        try {
+            Thread.sleep(1);

Review Comment:
   No need to wait if concurrency == 0



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org