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/06/04 09:29:14 UTC

[dubbo] branch performance-tuning-2.7.x updated: performance tuning: getMethodparameter refactor (#4244)

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

liujun pushed a commit to branch performance-tuning-2.7.x
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/performance-tuning-2.7.x by this push:
     new 086949c  performance tuning: getMethodparameter refactor (#4244)
086949c is described below

commit 086949c8fb2204bfa51e989b1fb7b8ccc6a3bc39
Author: ken.lj <ke...@gmail.com>
AuthorDate: Tue Jun 4 17:29:03 2019 +0800

    performance tuning: getMethodparameter refactor (#4244)
---
 .../src/main/java/org/apache/dubbo/common/URL.java | 81 +++++++++++++++++-----
 .../java/org/apache/dubbo/common/URLBuilder.java   | 57 ++++++++++++---
 .../java/org/apache/dubbo/rpc/RpcInvocation.java   | 16 +++--
 3 files changed, 121 insertions(+), 33 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
index 5dac550..741b7cb 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
@@ -19,8 +19,6 @@ package org.apache.dubbo.common;
 import org.apache.dubbo.common.config.Configuration;
 import org.apache.dubbo.common.config.InmemoryConfiguration;
 import org.apache.dubbo.common.constants.RemotingConstants;
-import org.apache.dubbo.common.model.ApplicationModel;
-import org.apache.dubbo.common.model.MethodModel;
 import org.apache.dubbo.common.utils.ArrayUtils;
 import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.common.utils.NetUtils;
@@ -39,7 +37,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.TreeMap;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -50,6 +47,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY;
@@ -180,7 +178,7 @@ class URL implements Serializable {
                int port,
                String path,
                Map<String, String> parameters) {
-        this (protocol, username, password, host, port, path, parameters, toMethodParameters(parameters, path));
+        this (protocol, username, password, host, port, path, parameters, toMethodParameters(parameters));
     }
 
     public URL(String protocol,
@@ -302,22 +300,31 @@ class URL implements Serializable {
         return new URL(protocol, username, password, host, port, path, parameters);
     }
 
-    public static Map<String, Map<String, String>> toMethodParameters(Map<String, String> parameters, String path) {
+    public static Map<String, Map<String, String>> toMethodParameters(Map<String, String> parameters) {
         Map<String, Map<String, String>> methodParameters = new HashMap<>();
         if (parameters != null) {
-            for (Map.Entry<String, String> entry : parameters.entrySet()) {
-                String key = entry.getKey();
-                int methodSeparator = key.indexOf(".");
-                if (methodSeparator > 0) {
-                    String method = key.substring(0, methodSeparator);
-                    String realKey = key.substring(methodSeparator + 1);
-                    ApplicationModel.getServiceModel(path).ifPresent(serviceModel -> {
-                        Set<MethodModel> methodModels = serviceModel.getMethods(method);
-                        if (CollectionUtils.isNotEmpty(methodModels)) {
-                            Map<String, String> subParameter = methodParameters.computeIfAbsent(method, k -> new HashMap<>());
-                            subParameter.put(realKey, entry.getValue());
+            String methodsString = parameters.get(METHODS_KEY);
+            if (StringUtils.isNotEmpty(methodsString)) {
+                String[] methods = methodsString.split(",");
+                for (Map.Entry<String, String> entry : parameters.entrySet()) {
+                    String key = entry.getKey();
+                    for (String method : methods) {
+                        String methodPrefix = method + ".";
+                        if (key.startsWith(methodPrefix)) {
+                            String realKey = key.substring(methodPrefix.length());
+                            URL.putMethodParameter(method, realKey, entry.getValue(), methodParameters);
                         }
-                    });
+                    }
+                }
+            } else {
+                for (Map.Entry<String, String> entry : parameters.entrySet()) {
+                    String key = entry.getKey();
+                    int methodSeparator = key.indexOf(".");
+                    if (methodSeparator > 0) {
+                        String method = key.substring(0, methodSeparator);
+                        String realKey = key.substring(methodSeparator + 1);
+                        URL.putMethodParameter(method, realKey, entry.getValue(), methodParameters);
+                    }
                 }
             }
         }
@@ -1088,6 +1095,7 @@ class URL implements Serializable {
 
         Map<String, String> map = new HashMap<>(getParameters());
         map.put(key, value);
+
         return new URL(protocol, username, password, host, port, path, map);
     }
 
@@ -1101,9 +1109,43 @@ class URL implements Serializable {
         }
         Map<String, String> map = new HashMap<>(getParameters());
         map.put(key, value);
+
         return new URL(protocol, username, password, host, port, path, map);
     }
 
+    public URL addMethodParameter(String method, String key, String value) {
+        if (StringUtils.isEmpty(method)
+                || StringUtils.isEmpty(key)
+                || StringUtils.isEmpty(value)) {
+            return this;
+        }
+
+        Map<String, String> map = new HashMap<>(getParameters());
+        map.put(method + "." + key, value);
+        Map<String, Map<String, String>> methodMap = toMethodParameters(map);
+        URL.putMethodParameter(method, key, value, methodMap);
+
+        return new URL(protocol, username, password, host, port, path, map, methodMap);
+    }
+
+    public URL addMethodParameterIfAbsent(String method, String key, String value) {
+        if (StringUtils.isEmpty(method)
+                || StringUtils.isEmpty(key)
+                || StringUtils.isEmpty(value)) {
+            return this;
+        }
+        if (hasMethodParameter(method, key)) {
+            return this;
+        }
+
+        Map<String, String> map = new HashMap<>(getParameters());
+        map.put(method + "." + key, value);
+        Map<String, Map<String, String>> methodMap = toMethodParameters(map);
+        URL.putMethodParameter(method, key, value, methodMap);
+
+        return new URL(protocol, username, password, host, port, path, map, methodMap);
+    }
+
     /**
      * Add parameters to a new url.
      *
@@ -1616,4 +1658,9 @@ class URL implements Serializable {
         return true;
     }
 
+    public static void putMethodParameter(String method, String key, String value, Map<String, Map<String, String>> methodParameters) {
+        Map<String, String> subParameter = methodParameters.computeIfAbsent(method, k -> new HashMap<>());
+        subParameter.put(key, value);
+    }
+
 }
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java
index b1c1a3d..3097c28 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java
@@ -84,7 +84,7 @@ public final class URLBuilder {
                       String host,
                       int port,
                       String path, Map<String, String> parameters) {
-        this(protocol, username, password, host, port, path, parameters, URL.toMethodParameters(parameters, path));
+        this(protocol, username, password, host, port, path, parameters, URL.toMethodParameters(parameters));
     }
 
     public URLBuilder(String protocol,
@@ -112,6 +112,7 @@ public final class URLBuilder {
         int port = url.getPort();
         String path = url.getPath();
         Map<String, String> parameters = new HashMap<>(url.getParameters());
+        Map<String, Map<String, String>> methodParameters = new HashMap<>(url.getMethodParameters());
         return new URLBuilder(
                 protocol,
                 username,
@@ -119,7 +120,8 @@ public final class URLBuilder {
                 host,
                 port,
                 path,
-                parameters);
+                parameters,
+                methodParameters);
     }
 
     public URL build() {
@@ -252,10 +254,6 @@ public final class URLBuilder {
         if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
             return this;
         }
-        // if value doesn't change, return immediately
-        if (value.equals(parameters.get(key))) { // value != null
-            return this;
-        }
 
         parameters.put(key, value);
         return this;
@@ -265,9 +263,7 @@ public final class URLBuilder {
         if (StringUtils.isEmpty(method) || StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
             return this;
         }
-        Map<String, String> keyParameter = methodParameters.computeIfAbsent(method, m -> new HashMap<>());
-
-        keyParameter.put(key, value);
+        URL.putMethodParameter(method, key, value, methodParameters);
         return this;
     }
 
@@ -282,6 +278,17 @@ public final class URLBuilder {
         return this;
     }
 
+    public URLBuilder addMethodParameterIfAbsent(String method, String key, String value) {
+        if (StringUtils.isEmpty(method) || StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
+            return this;
+        }
+        if (hasMethodParameter(method, key)) {
+            return this;
+        }
+        URL.putMethodParameter(method, key, value, methodParameters);
+        return this;
+    }
+
     public URLBuilder addParameters(Map<String, String> parameters) {
         if (CollectionUtils.isEmptyMap(parameters)) {
             return this;
@@ -378,7 +385,39 @@ public final class URLBuilder {
         return value != null && value.length() > 0;
     }
 
+    public boolean hasMethodParameter(String method, String key) {
+        if (method == null) {
+            String suffix = "." + key;
+            for (String fullKey : parameters.keySet()) {
+                if (fullKey.endsWith(suffix)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+        if (key == null) {
+            String prefix = method + ".";
+            for (String fullKey : parameters.keySet()) {
+                if (fullKey.startsWith(prefix)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+        String value = getMethodParameter(method, key);
+        return value != null && value.length() > 0;
+    }
+
     public String getParameter(String key) {
         return parameters.get(key);
     }
+
+    public String getMethodParameter(String method, String key) {
+        Map<String, String> keyMap = methodParameters.get(method);
+        String value = null;
+        if (keyMap != null) {
+            value =  keyMap.get(key);
+        }
+        return value;
+    }
 }
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java
index 7299acb..ad46cd7 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java
@@ -121,13 +121,15 @@ public class RpcInvocation implements Invocation, Serializable {
         this.arguments = arguments == null ? new Object[0] : arguments;
         this.attachments = attachments == null ? new HashMap<String, String>() : attachments;
         this.invoker = invoker;
-        ApplicationModel.getServiceModel(serviceName).ifPresent(serviceModel ->
-             serviceModel.getMethod(methodName, parameterTypes)
-                    .ifPresent(methodModel -> {
-                        this.parameterTypesDesc = methodModel.getParamDesc();
-                        this.returnTypes = methodModel.getReturnTypes();
-                    })
-        );
+        if (StringUtils.isNotEmpty(serviceName)) {
+            ApplicationModel.getServiceModel(serviceName).ifPresent(serviceModel ->
+                    serviceModel.getMethod(methodName, parameterTypes)
+                            .ifPresent(methodModel -> {
+                                this.parameterTypesDesc = methodModel.getParamDesc();
+                                this.returnTypes = methodModel.getReturnTypes();
+                            })
+            );
+        }
     }
 
     @Override