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

liujun pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new 78a4dbad6e Fix #12773 Callback mode rpc context loss. (#12866)
78a4dbad6e is described below

commit 78a4dbad6e5ed195acebc4d2eb18588c79563367
Author: wuwen <[email protected]>
AuthorDate: Wed Aug 9 16:05:27 2023 +0800

    Fix #12773 Callback mode rpc context loss. (#12866)
---
 .../dubbo/common/constants/CommonConstants.java    |  2 ++
 .../support/CallbackConsumerContextFilter.java     | 37 ++++++++++++++++++++++
 .../dubbo/internal/org.apache.dubbo.rpc.Filter     |  1 +
 .../rpc/protocol/dubbo/CallbackServiceCodec.java   | 17 +++++++++-
 4 files changed, 56 insertions(+), 1 deletion(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
index 88d687c504..c303c3c3ab 100644
--- 
a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
@@ -32,6 +32,8 @@ public interface CommonConstants {
     String PROVIDER = "provider";
 
     String CONSUMER = "consumer";
+    
+    String CALLBACK = "callback";
 
     String APPLICATION_KEY = "application";
 
diff --git 
a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/CallbackConsumerContextFilter.java
 
b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/CallbackConsumerContextFilter.java
new file mode 100644
index 0000000000..ab76f628d5
--- /dev/null
+++ 
b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/CallbackConsumerContextFilter.java
@@ -0,0 +1,37 @@
+/*
+ * 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.monitor.support;
+
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.rpc.Filter;
+import org.apache.dubbo.rpc.cluster.filter.support.ConsumerContextFilter;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+import static org.apache.dubbo.common.constants.CommonConstants.CALLBACK;
+
+/**
+ * CallbackConsumerContextFilter set current RpcContext with 
invoker,invocation, local host, remote host and port
+ * for consumer callback invoker.It does it to make the requires info 
available to execution thread's RpcContext.
+ * @see ConsumerContextFilter
+ */
+@Activate(group = CALLBACK, order = Integer.MIN_VALUE)
+public class CallbackConsumerContextFilter extends ConsumerContextFilter 
implements Filter {
+    
+    public CallbackConsumerContextFilter(ApplicationModel applicationModel) {
+        super(applicationModel);
+    }
+}
diff --git 
a/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter
 
b/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter
index fbeff59d1d..4fe3200e52 100644
--- 
a/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter
+++ 
b/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter
@@ -1 +1,2 @@
 monitor=org.apache.dubbo.monitor.support.MonitorFilter
+callback-consumer-context=org.apache.dubbo.monitor.support.CallbackConsumerContextFilter
diff --git 
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java
 
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java
index 2bc90887fb..7c15e8a26e 100644
--- 
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java
+++ 
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.rpc.protocol.dubbo;
 
 import org.apache.dubbo.common.BaseServiceMetadata;
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.constants.CommonConstants;
 import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.url.component.ServiceConfigURL;
@@ -33,10 +34,12 @@ import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.Protocol;
 import org.apache.dubbo.rpc.ProxyFactory;
 import org.apache.dubbo.rpc.RpcInvocation;
+import org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 import org.apache.dubbo.rpc.model.FrameworkModel;
 import org.apache.dubbo.rpc.model.ModuleModel;
 import org.apache.dubbo.rpc.model.ProviderModel;
+import org.apache.dubbo.rpc.model.ScopeModelUtil;
 import org.apache.dubbo.rpc.model.ServiceDescriptor;
 import org.apache.dubbo.rpc.model.ServiceMetadata;
 import org.apache.dubbo.rpc.support.RpcUtils;
@@ -47,11 +50,14 @@ import java.util.Map;
 import java.util.Set;
 
 import static 
org.apache.dubbo.common.constants.CommonConstants.CALLBACK_INSTANCES_LIMIT_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
 import static 
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_CALLBACK_INSTANCES;
 import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL;
 import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
+import static 
org.apache.dubbo.common.constants.CommonConstants.REFERENCE_FILTER_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_PROPERTY_TYPE_MISMATCH;
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_FAILED_DESTROY_INVOKER;
@@ -202,11 +208,16 @@ public class CallbackServiceCodec {
         if (isRefer) {
             if (proxy == null) {
                 URL referurl = URL.valueOf("callback://" + url.getAddress() + 
"/" + clazz.getName() + "?" + INTERFACE_KEY + "=" + clazz.getName());
-                referurl = 
referurl.addParametersIfAbsent(url.getParameters()).removeParameter(METHODS_KEY);
+                referurl = 
referurl.addParametersIfAbsent(url.getParameters()).removeParameter(METHODS_KEY).addParameter(SIDE_KEY,
 CONSUMER_SIDE);
                 if (!isInstancesOverLimit(channel, referurl, clazz.getName(), 
instid, true)) {
                     
url.getOrDefaultApplicationModel().getDefaultModule().getServiceRepository().registerService(clazz);
                     @SuppressWarnings("rawtypes")
                     Invoker<?> invoker = new ChannelWrappedInvoker(clazz, 
channel, referurl, String.valueOf(instid));
+
+                    FilterChainBuilder builder = getFilterChainBuilder(url);
+                    invoker = builder.buildInvokerChain(invoker, 
REFERENCE_FILTER_KEY, CommonConstants.CONSUMER);
+                    invoker = builder.buildInvokerChain(invoker, 
REFERENCE_FILTER_KEY, CommonConstants.CALLBACK);
+                    
                     proxy = proxyFactory.getProxy(invoker);
                     channel.setAttribute(proxyCacheKey, proxy);
                     channel.setAttribute(invokerCacheKey, invoker);
@@ -244,6 +255,10 @@ public class CallbackServiceCodec {
         return proxy;
     }
 
+    private FilterChainBuilder getFilterChainBuilder(URL url) {
+        return ScopeModelUtil.getExtensionLoader(FilterChainBuilder.class, 
url.getScopeModel()).getDefaultExtension();
+    }
+
     private static String getClientSideCallbackServiceCacheKey(int instid) {
         return CALLBACK_SERVICE_KEY + "." + instid;
     }

Reply via email to