You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2022/03/15 09:33:25 UTC
[dubbo] branch 3.0 updated: [3.0] Fix Zone Aware Filter not work (#9680)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new ec6b53f [3.0] Fix Zone Aware Filter not work (#9680)
ec6b53f is described below
commit ec6b53f07bde071437e3daa5443f6c66e925f9d3
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Tue Mar 15 17:32:38 2022 +0800
[3.0] Fix Zone Aware Filter not work (#9680)
---
.../cluster/filter/support/ZoneAwareFilter.java | 70 ----------------------
.../support/registry/ZoneAwareClusterInvoker.java | 19 +++++-
...g.apache.dubbo.rpc.cluster.filter.ClusterFilter | 1 -
.../registry/ZoneAwareClusterInvokerTest.java | 7 ++-
4 files changed, 21 insertions(+), 76 deletions(-)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/support/ZoneAwareFilter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/support/ZoneAwareFilter.java
deleted file mode 100644
index 9465af5..0000000
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/support/ZoneAwareFilter.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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.filter.support;
-
-import org.apache.dubbo.common.constants.CommonConstants;
-import org.apache.dubbo.common.extension.Activate;
-import org.apache.dubbo.common.extension.ExtensionLoader;
-import org.apache.dubbo.common.utils.StringUtils;
-import org.apache.dubbo.rpc.Invocation;
-import org.apache.dubbo.rpc.Invoker;
-import org.apache.dubbo.rpc.Result;
-import org.apache.dubbo.rpc.RpcContext;
-import org.apache.dubbo.rpc.RpcException;
-import org.apache.dubbo.rpc.ZoneDetector;
-import org.apache.dubbo.rpc.cluster.filter.ClusterFilter;
-import org.apache.dubbo.rpc.model.ApplicationModel;
-
-import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE;
-import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE_FORCE;
-
-/**
- * Determines the zone information of current request.
- *
- * active only when url has key 'cluster=zone-aware'
- */
-@Activate(group = CommonConstants.CONSUMER, value = "cluster:zone-aware")
-public class ZoneAwareFilter implements ClusterFilter {
-
- private ApplicationModel applicationModel;
-
- public ZoneAwareFilter(ApplicationModel applicationModel) {
- this.applicationModel = applicationModel;
- }
-
- @Override
- public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
- RpcContext rpcContext = RpcContext.getClientAttachment();
- String zone = (String) rpcContext.getAttachment(REGISTRY_ZONE);
- String force = (String) rpcContext.getAttachment(REGISTRY_ZONE_FORCE);
- ExtensionLoader<ZoneDetector> loader = applicationModel.getExtensionLoader(ZoneDetector.class);
- if (StringUtils.isEmpty(zone) && loader.hasExtension("default")) {
- ZoneDetector detector = loader.getExtension("default");
- zone = detector.getZoneOfCurrentRequest(invocation);
- force = detector.isZoneForcingEnabled(invocation, zone);
- }
-
- if (StringUtils.isNotEmpty(zone)) {
- invocation.setAttachment(REGISTRY_ZONE, zone);
- }
- if (StringUtils.isNotEmpty(force)) {
- invocation.setAttachment(REGISTRY_ZONE_FORCE, force);
- }
-
- return invoker.invoke(invocation);
- }
-}
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java
index d2f1f10..f4b307b 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java
@@ -16,13 +16,16 @@
*/
package org.apache.dubbo.rpc.cluster.support.registry;
+import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
+import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.ZoneDetector;
import org.apache.dubbo.rpc.cluster.ClusterInvoker;
import org.apache.dubbo.rpc.cluster.Directory;
import org.apache.dubbo.rpc.cluster.LoadBalance;
@@ -49,8 +52,14 @@ public class ZoneAwareClusterInvoker<T> extends AbstractClusterInvoker<T> {
private static final Logger logger = LoggerFactory.getLogger(ZoneAwareClusterInvoker.class);
+ private ZoneDetector zoneDetector;
+
public ZoneAwareClusterInvoker(Directory<T> directory) {
super(directory);
+ ExtensionLoader<ZoneDetector> loader = directory.getConsumerUrl().getOrDefaultApplicationModel().getExtensionLoader(ZoneDetector.class);
+ if (loader.hasExtension("default")) {
+ zoneDetector = loader.getExtension("default");
+ }
}
@Override
@@ -65,8 +74,15 @@ public class ZoneAwareClusterInvoker<T> extends AbstractClusterInvoker<T> {
}
}
+ RpcContext rpcContext = RpcContext.getClientAttachment();
+ String zone = rpcContext.getAttachment(REGISTRY_ZONE);
+ String force = rpcContext.getAttachment(REGISTRY_ZONE_FORCE);
+ if (StringUtils.isEmpty(zone) && zoneDetector != null) {
+ zone = zoneDetector.getZoneOfCurrentRequest(invocation);
+ force = zoneDetector.isZoneForcingEnabled(invocation, zone);
+ }
+
// providers in the registry with the same zone
- String zone = invocation.getAttachment(REGISTRY_ZONE);
if (StringUtils.isNotEmpty(zone)) {
for (Invoker<T> invoker : invokers) {
ClusterInvoker<T> clusterInvoker = (ClusterInvoker<T>) invoker;
@@ -74,7 +90,6 @@ public class ZoneAwareClusterInvoker<T> extends AbstractClusterInvoker<T> {
return clusterInvoker.invoke(invocation);
}
}
- String force = invocation.getAttachment(REGISTRY_ZONE_FORCE);
if (StringUtils.isNotEmpty(force) && "true".equalsIgnoreCase(force)) {
throw new IllegalStateException("No registry instance in zone or no available providers in the registry, zone: "
+ zone
diff --git a/dubbo-cluster/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.filter.ClusterFilter b/dubbo-cluster/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.filter.ClusterFilter
index bd85b15..77cf2ac 100644
--- a/dubbo-cluster/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.filter.ClusterFilter
+++ b/dubbo-cluster/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.filter.ClusterFilter
@@ -1,3 +1,2 @@
-zone-aware=org.apache.dubbo.rpc.cluster.filter.support.ZoneAwareFilter
consumercontext=org.apache.dubbo.rpc.cluster.filter.support.ConsumerContextFilter
router-snapshot=org.apache.dubbo.rpc.cluster.router.RouterSnapshotFilter
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java
index 381cfdf..848fbf2 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java
@@ -19,6 +19,7 @@ package org.apache.dubbo.rpc.cluster.support.registry;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.cluster.ClusterInvoker;
import org.apache.dubbo.rpc.cluster.Directory;
@@ -100,7 +101,7 @@ public class ZoneAwareClusterInvokerTest {
given(invocation.getParameterTypes()).willReturn(new Class<?>[]{});
given(invocation.getArguments()).willReturn(new Object[]{});
given(invocation.getObjectAttachments()).willReturn(new HashMap<>());
- given(invocation.getAttachment(REGISTRY_ZONE)).willReturn(zoneKey);
+ RpcContext.getClientAttachment().setAttachment(REGISTRY_ZONE, zoneKey);
firstInvoker = newUnexpectedInvoker();
thirdInvoker = newUnexpectedInvoker();
@@ -144,8 +145,8 @@ public class ZoneAwareClusterInvokerTest {
given(invocation.getParameterTypes()).willReturn(new Class<?>[]{});
given(invocation.getArguments()).willReturn(new Object[]{});
given(invocation.getObjectAttachments()).willReturn(new HashMap<>());
- given(invocation.getAttachment(REGISTRY_ZONE)).willReturn(zoneKey);
- given(invocation.getAttachment(REGISTRY_ZONE_FORCE)).willReturn("true");
+ RpcContext.getClientAttachment().setAttachment(REGISTRY_ZONE, zoneKey);
+ RpcContext.getClientAttachment().setAttachment(REGISTRY_ZONE_FORCE, "true");
firstInvoker = newUnexpectedInvoker();
secondInvoker = newUnexpectedInvoker();