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 2019/12/13 07:24:53 UTC

[dubbo] 12/14: make asyncinfo not rely on method object

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

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

commit a619be64d00bb85096c81f9a97c2acffb8922d1c
Author: beiwei.ly <be...@alibaba-inc.com>
AuthorDate: Fri Nov 29 18:04:08 2019 +0800

    make asyncinfo not rely on method object
---
 .../dubbo/common/extension/LoadingStrategy.java    | 16 ++++
 .../org/apache/dubbo/config/MethodConfigTest.java  |  4 +-
 .../org/apache/dubbo/config/AbstractConfig.java    |  6 +-
 .../org/apache/dubbo/config/ReferenceConfig.java   |  8 +-
 .../apache/dubbo/rpc/model/AsyncMethodInfo.java    | 87 ++++++++++++++++++++++
 .../dubbo/rpc/model/ConsumerMethodModel.java       | 82 +-------------------
 .../org/apache/dubbo/rpc/model/ConsumerModel.java  | 13 +++-
 .../rpc/protocol/dubbo/filter/FutureFilter.java    | 22 ++----
 8 files changed, 130 insertions(+), 108 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java
index 6c98795..2de3f2e 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java
@@ -1,3 +1,19 @@
+/*
+ * 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.common.extension;
 
 public interface LoadingStrategy {
diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
index f43cda8..aa6dd4e 100644
--- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
+++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
@@ -17,7 +17,7 @@
 
 package org.apache.dubbo.config;
 
-import org.apache.dubbo.rpc.model.ConsumerMethodModel;
+import org.apache.dubbo.rpc.model.AsyncMethodInfo;
 import org.apache.dubbo.service.Person;
 
 import com.alibaba.dubbo.config.ArgumentConfig;
@@ -112,7 +112,7 @@ public class MethodConfigTest {
         methodConfig.setOninvokeMethod("setName");
         methodConfig.setOninvoke(new Person());
 
-        ConsumerMethodModel.AsyncMethodInfo methodInfo = org.apache.dubbo.config.MethodConfig.convertMethodConfig2AsyncInfo(methodConfig);
+        AsyncMethodInfo methodInfo = org.apache.dubbo.config.MethodConfig.convertMethodConfig2AsyncInfo(methodConfig);
 
         assertEquals(methodInfo.getOninvokeMethod(), Person.class.getMethod("setName", String.class));
     }
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java
index d557624..c2ad34c 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java
@@ -31,7 +31,7 @@ import org.apache.dubbo.common.utils.ReflectUtils;
 import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.config.context.ConfigConfigurationAdapter;
 import org.apache.dubbo.config.support.Parameter;
-import org.apache.dubbo.rpc.model.ConsumerMethodModel;
+import org.apache.dubbo.rpc.model.AsyncMethodInfo;
 
 import java.io.Serializable;
 import java.lang.reflect.Method;
@@ -252,7 +252,7 @@ public abstract class AbstractConfig implements Serializable {
         }
     }
 
-    protected static ConsumerMethodModel.AsyncMethodInfo convertMethodConfig2AsyncInfo(MethodConfig methodConfig) {
+    protected static AsyncMethodInfo convertMethodConfig2AsyncInfo(MethodConfig methodConfig) {
         if (methodConfig == null || (methodConfig.getOninvoke() == null && methodConfig.getOnreturn() == null && methodConfig.getOnthrow() == null)) {
             return null;
         }
@@ -262,7 +262,7 @@ public abstract class AbstractConfig implements Serializable {
             throw new IllegalStateException("method config error : return attribute must be set true when onreturn or onthrow has been set.");
         }
 
-        ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo();
+        AsyncMethodInfo asyncMethodInfo = new AsyncMethodInfo();
 
         asyncMethodInfo.setOninvokeInstance(methodConfig.getOninvoke());
         asyncMethodInfo.setOnreturnInstance(methodConfig.getOnreturn());
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
index dcdb9be..8fecb10 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
@@ -38,7 +38,7 @@ import org.apache.dubbo.rpc.cluster.directory.StaticDirectory;
 import org.apache.dubbo.rpc.cluster.support.ClusterUtils;
 import org.apache.dubbo.rpc.cluster.support.RegistryAwareCluster;
 import org.apache.dubbo.rpc.model.ApplicationModel;
-import org.apache.dubbo.rpc.model.ConsumerMethodModel;
+import org.apache.dubbo.rpc.model.AsyncMethodInfo;
 import org.apache.dubbo.rpc.model.ConsumerModel;
 import org.apache.dubbo.rpc.model.ServiceMetadata;
 import org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol;
@@ -328,9 +328,9 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
         // appendParameters(map, consumer, Constants.DEFAULT_KEY);
         appendParameters(map, consumer);
         appendParameters(map, this);
-        Map<String, Object> attributes = null;
+        Map<String, AsyncMethodInfo> attributes = null;
         if (CollectionUtils.isNotEmpty(methods)) {
-            attributes = new HashMap<String, Object>();
+            attributes = new HashMap<>();
             for (MethodConfig methodConfig : methods) {
                 appendParameters(map, methodConfig, methodConfig.getName());
                 String retryKey = methodConfig.getName() + ".retry";
@@ -340,7 +340,7 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
                         map.put(methodConfig.getName() + ".retries", "0");
                     }
                 }
-                ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = convertMethodConfig2AsyncInfo(methodConfig);
+                AsyncMethodInfo asyncMethodInfo = convertMethodConfig2AsyncInfo(methodConfig);
                 if (asyncMethodInfo != null) {
 //                    consumerModel.getMethodModel(methodConfig.getName()).addAttribute(ASYNC_KEY, asyncMethodInfo);
                     attributes.put(methodConfig.getName(), asyncMethodInfo);
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/AsyncMethodInfo.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/AsyncMethodInfo.java
new file mode 100644
index 0000000..574607d
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/AsyncMethodInfo.java
@@ -0,0 +1,87 @@
+/*
+ * 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.model;
+
+import java.lang.reflect.Method;
+
+public class AsyncMethodInfo {
+    // callback instance when async-call is invoked
+    private Object oninvokeInstance;
+
+    // callback method when async-call is invoked
+    private Method oninvokeMethod;
+
+    // callback instance when async-call is returned
+    private Object onreturnInstance;
+
+    // callback method when async-call is returned
+    private Method onreturnMethod;
+
+    // callback instance when async-call has exception thrown
+    private Object onthrowInstance;
+
+    // callback method when async-call has exception thrown
+    private Method onthrowMethod;
+
+    public Object getOninvokeInstance() {
+        return oninvokeInstance;
+    }
+
+    public void setOninvokeInstance(Object oninvokeInstance) {
+        this.oninvokeInstance = oninvokeInstance;
+    }
+
+    public Method getOninvokeMethod() {
+        return oninvokeMethod;
+    }
+
+    public void setOninvokeMethod(Method oninvokeMethod) {
+        this.oninvokeMethod = oninvokeMethod;
+    }
+
+    public Object getOnreturnInstance() {
+        return onreturnInstance;
+    }
+
+    public void setOnreturnInstance(Object onreturnInstance) {
+        this.onreturnInstance = onreturnInstance;
+    }
+
+    public Method getOnreturnMethod() {
+        return onreturnMethod;
+    }
+
+    public void setOnreturnMethod(Method onreturnMethod) {
+        this.onreturnMethod = onreturnMethod;
+    }
+
+    public Object getOnthrowInstance() {
+        return onthrowInstance;
+    }
+
+    public void setOnthrowInstance(Object onthrowInstance) {
+        this.onthrowInstance = onthrowInstance;
+    }
+
+    public Method getOnthrowMethod() {
+        return onthrowMethod;
+    }
+
+    public void setOnthrowMethod(Method onthrowMethod) {
+        this.onthrowMethod = onthrowMethod;
+    }
+}
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java
index be60014..6183759 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java
@@ -18,7 +18,6 @@ package org.apache.dubbo.rpc.model;
 
 
 import java.lang.reflect.Method;
-import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -34,24 +33,16 @@ public class ConsumerMethodModel {
     private final String methodName;
     private final boolean generic;
 
-    private final AsyncMethodInfo asyncInfo;
     private final ConcurrentMap<String, Object> attributeMap = new ConcurrentHashMap<>();
 
 
-    public ConsumerMethodModel(Method method, Map<String, Object> attributes) {
+    public ConsumerMethodModel(Method method) {
         this.method = method;
         this.parameterClasses = method.getParameterTypes();
         this.returnClass = method.getReturnType();
         this.parameterTypes = this.createParamSignature(parameterClasses);
         this.methodName = method.getName();
         this.generic = methodName.equals($INVOKE) && parameterTypes != null && parameterTypes.length == 3;
-
-        if (attributes != null) {
-            asyncInfo = (AsyncMethodInfo) attributes.get(methodName);
-        } else {
-            asyncInfo = null;
-        }
-
     }
 
     public Method getMethod() {
@@ -75,10 +66,6 @@ public class ConsumerMethodModel {
         return returnClass;
     }
 
-    public AsyncMethodInfo getAsyncInfo() {
-        return asyncInfo;
-    }
-
     public String getMethodName() {
         return methodName;
     }
@@ -108,71 +95,4 @@ public class ConsumerMethodModel {
     }
 
 
-    public static class AsyncMethodInfo {
-        // callback instance when async-call is invoked
-        private Object oninvokeInstance;
-
-        // callback method when async-call is invoked
-        private Method oninvokeMethod;
-
-        // callback instance when async-call is returned
-        private Object onreturnInstance;
-
-        // callback method when async-call is returned
-        private Method onreturnMethod;
-
-        // callback instance when async-call has exception thrown
-        private Object onthrowInstance;
-
-        // callback method when async-call has exception thrown
-        private Method onthrowMethod;
-
-        public Object getOninvokeInstance() {
-            return oninvokeInstance;
-        }
-
-        public void setOninvokeInstance(Object oninvokeInstance) {
-            this.oninvokeInstance = oninvokeInstance;
-        }
-
-        public Method getOninvokeMethod() {
-            return oninvokeMethod;
-        }
-
-        public void setOninvokeMethod(Method oninvokeMethod) {
-            this.oninvokeMethod = oninvokeMethod;
-        }
-
-        public Object getOnreturnInstance() {
-            return onreturnInstance;
-        }
-
-        public void setOnreturnInstance(Object onreturnInstance) {
-            this.onreturnInstance = onreturnInstance;
-        }
-
-        public Method getOnreturnMethod() {
-            return onreturnMethod;
-        }
-
-        public void setOnreturnMethod(Method onreturnMethod) {
-            this.onreturnMethod = onreturnMethod;
-        }
-
-        public Object getOnthrowInstance() {
-            return onthrowInstance;
-        }
-
-        public void setOnthrowInstance(Object onthrowInstance) {
-            this.onthrowInstance = onthrowInstance;
-        }
-
-        public Method getOnthrowMethod() {
-            return onthrowMethod;
-        }
-
-        public void setOnthrowMethod(Method onthrowMethod) {
-            this.onthrowMethod = onthrowMethod;
-        }
-    }
 }
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java
index fa6f6e5..82ebf4a 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java
@@ -19,6 +19,7 @@ package org.apache.dubbo.rpc.model;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.IdentityHashMap;
 import java.util.List;
 import java.util.Map;
@@ -30,6 +31,7 @@ import java.util.Optional;
 public class ConsumerModel {
     private ServiceMetadata serviceMetadata;
     private Map<Method, ConsumerMethodModel> methodModels = new IdentityHashMap<Method, ConsumerMethodModel>();
+    private Map<String, AsyncMethodInfo> asyncMethodInfos = Collections.emptyMap();
 
     /**
      * This constructor create an instance of ConsumerModel and passed objects should not be null.
@@ -42,11 +44,15 @@ public class ConsumerModel {
         this.serviceMetadata = serviceMetadata;
     }
 
-    public void init(Map<String, Object> attributes) {
+    public void init(Map<String, AsyncMethodInfo> attributes) {
+        if (attributes != null) {
+            this.asyncMethodInfos = attributes;
+        }
+
         Class[] interfaceList = serviceMetadata.getTarget().getClass().getInterfaces();
         for (Class interfaceClass : interfaceList) {
             for (Method method : interfaceClass.getMethods()) {
-                methodModels.put(method, new ConsumerMethodModel(method, attributes));
+                methodModels.put(method, new ConsumerMethodModel(method));
             }
         }
     }
@@ -96,6 +102,9 @@ public class ConsumerModel {
         return consumerMethodModel.orElse(null);
     }
 
+    public AsyncMethodInfo getAsyncInfo(String methodName) {
+        return asyncMethodInfos.get(methodName);
+    }
 
     /**
      * @return
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java
index dcf732e..ffbdcec 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java
@@ -26,7 +26,7 @@ import org.apache.dubbo.rpc.ListenableFilter;
 import org.apache.dubbo.rpc.Result;
 import org.apache.dubbo.rpc.RpcException;
 import org.apache.dubbo.rpc.model.ApplicationModel;
-import org.apache.dubbo.rpc.model.ConsumerMethodModel;
+import org.apache.dubbo.rpc.model.AsyncMethodInfo;
 import org.apache.dubbo.rpc.model.ConsumerModel;
 
 import java.lang.reflect.InvocationTargetException;
@@ -55,7 +55,7 @@ public class FutureFilter extends ListenableFilter {
     }
 
     private void fireInvokeCallback(final Invoker<?> invoker, final Invocation invocation) {
-        final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
+        final AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
         if (asyncMethodInfo == null) {
             return;
         }
@@ -83,7 +83,7 @@ public class FutureFilter extends ListenableFilter {
     }
 
     private void fireReturnCallback(final Invoker<?> invoker, final Invocation invocation, final Object result) {
-        final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
+        final AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
         if (asyncMethodInfo == null) {
             return;
         }
@@ -129,7 +129,7 @@ public class FutureFilter extends ListenableFilter {
     }
 
     private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
-        final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
+        final AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
         if (asyncMethodInfo == null) {
             return;
         }
@@ -175,7 +175,7 @@ public class FutureFilter extends ListenableFilter {
         }
     }
 
-    private ConsumerMethodModel.AsyncMethodInfo getAsyncMethodInfo(Invoker<?> invoker, Invocation invocation) {
+    private AsyncMethodInfo getAsyncMethodInfo(Invoker<?> invoker, Invocation invocation) {
         final ConsumerModel consumerModel = ApplicationModel.getConsumerModel(invoker.getUrl().getServiceKey());
         if (consumerModel == null) {
             return null;
@@ -186,17 +186,7 @@ public class FutureFilter extends ListenableFilter {
             methodName = (String) invocation.getArguments()[0];
         }
 
-        ConsumerMethodModel methodModel = consumerModel.getMethodModel(methodName);
-        if (methodModel == null) {
-            return null;
-        }
-
-        final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = methodModel.getAsyncInfo();
-        if (asyncMethodInfo == null) {
-            return null;
-        }
-
-        return asyncMethodInfo;
+        return consumerModel.getAsyncInfo(methodName);
     }
 
     class FutureListener implements Listener {