This is an automated email from the ASF dual-hosted git repository.

victory pushed a commit to branch 2.7.0-release
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/2.7.0-release by this push:
     new 5acb4bf  [Dubbo-3231]keep TagRouter consistent with 2.6.x (#3233)
5acb4bf is described below

commit 5acb4bf8686a6c315d8f2f3754f3635909208ab4
Author: ken.lj <ken.lj...@gmail.com>
AuthorDate: Tue Jan 15 18:24:51 2019 +0800

    [Dubbo-3231]keep TagRouter consistent with 2.6.x (#3233)
    
    * keep TagRouter consistent with 2.6.x
    
    * refactor filterUsingStaticTag using lambda in tagRouter
---
 .../dubbo/rpc/cluster/router/tag/TagRouter.java    | 37 ++++++++++++++++++++--
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java
 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java
index 6c4e94e..6eac1fc 100644
--- 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java
+++ 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java
@@ -88,7 +88,7 @@ public class TagRouter extends AbstractRouter implements 
Comparable<Router>, Con
         }
 
         if (tagRouterRule == null || !tagRouterRule.isValid() || 
!tagRouterRule.isEnabled()) {
-            return invokers;
+            return filterUsingStaticTag(invokers, url, invocation);
         }
 
         List<Invoker<T>> result = invokers;
@@ -112,7 +112,7 @@ public class TagRouter extends AbstractRouter implements 
Comparable<Router>, Con
             }
             // If there's no tagged providers that can match the current 
tagged request. force.tag is set by default
             // to false, which means it will invoke any providers without a 
tag unless it's explicitly disallowed.
-            if (CollectionUtils.isNotEmpty(result) || isForceUse(invocation)) {
+            if (CollectionUtils.isNotEmpty(result) || 
isForceUseTag(invocation)) {
                 return result;
             }
             // FAILOVER: return all Providers without any tags.
@@ -141,6 +141,37 @@ public class TagRouter extends AbstractRouter implements 
Comparable<Router>, Con
         }
     }
 
+    /**
+     * If there's no dynamic tag rule being set, use static tag in URL.
+     *
+     * A typical scenario is a Consumer using version 2.7.x calls Providers 
using version 2.6.x or lower,
+     * the Consumer should always respect the tag in provider URL regardless 
of whether a dynamic tag rule has been set to it or not.
+     *
+     * TODO, to guarantee consistent behavior of interoperability between 2.6- 
and 2.7+, this method should has the same logic with the TagRouter in 2.6.x.
+     *
+     * @param invokers
+     * @param url
+     * @param invocation
+     * @param <T>
+     * @return
+     */
+    private <T> List<Invoker<T>> filterUsingStaticTag(List<Invoker<T>> 
invokers, URL url, Invocation invocation) {
+        List<Invoker<T>> result = invokers;
+        // Dynamic param
+        String tag = StringUtils.isEmpty(invocation.getAttachment(TAG_KEY)) ? 
url.getParameter(TAG_KEY) :
+                invocation.getAttachment(TAG_KEY);
+        // Tag request
+        if (!StringUtils.isEmpty(tag)) {
+            result = filterInvoker(invokers, invoker -> 
tag.equals(invoker.getUrl().getParameter(Constants.TAG_KEY)));
+            if (CollectionUtils.isEmpty(result) && !isForceUseTag(invocation)) 
{
+                result = filterInvoker(invokers, invoker -> 
StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY)));
+            }
+        } else {
+            result = filterInvoker(invokers, invoker -> 
StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY)));
+        }
+        return result;
+    }
+
     @Override
     public int getPriority() {
         return DEFAULT_PRIORITY;
@@ -157,7 +188,7 @@ public class TagRouter extends AbstractRouter implements 
Comparable<Router>, Con
         return tagRouterRule != null && tagRouterRule.isForce();
     }
 
-    private boolean isForceUse(Invocation invocation) {
+    private boolean isForceUseTag(Invocation invocation) {
         return Boolean.valueOf(invocation.getAttachment(FORCE_USE_TAG, 
url.getParameter(FORCE_USE_TAG, "false")));
     }
 

Reply via email to