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 152455e Reduce case convert, reduce iterator creation, add attribute
to MethodDescriptor (#7750)
152455e is described below
commit 152455e99e9be5c01f02b66eaaa97ac475f44909
Author: Albumen Kevin <[email protected]>
AuthorDate: Fri May 14 10:31:31 2021 +0800
Reduce case convert, reduce iterator creation, add attribute to
MethodDescriptor (#7750)
---
.../dubbo/common/constants/CommonConstants.java | 2 ++
.../apache/dubbo/rpc/model/MethodDescriptor.java | 12 +++++++++
.../main/java/org/apache/dubbo/rpc/Invocation.java | 5 ++++
.../java/org/apache/dubbo/rpc/RpcInvocation.java | 30 ++++++++++++++--------
.../org/apache/dubbo/rpc/support/RpcUtils.java | 6 ++++-
.../protocol/dubbo/DecodeableRpcInvocation.java | 4 +--
.../dubbo/rpc/protocol/tri/AbstractStream.java | 4 +--
7 files changed, 47 insertions(+), 16 deletions(-)
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 02a9770..e510847 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
@@ -126,6 +126,8 @@ public interface CommonConstants {
// works as a replacement of TIMEOUT_KEY on wire, which seems to be
totally useless in previous releases).
String TIMEOUT_ATTACHMENT_KEY = "_TO";
+ String TIMEOUT_ATTACHMENT_KEY_LOWER = "_to";
+
String TIME_COUNTDOWN_KEY = "timeout-countdown";
String ENABLE_TIMEOUT_COUNTDOWN_KEY = "enable-timeout-countdown";
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
index 241ab30..6ada135 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
@@ -23,6 +23,8 @@ import org.apache.dubbo.common.utils.ReflectUtils;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
import java.util.stream.Stream;
import static org.apache.dubbo.common.constants.CommonConstants.$ECHO;
@@ -47,6 +49,8 @@ public class MethodDescriptor {
private final boolean generic;
private final RpcType rpcType;
+ private final ConcurrentMap<String, Object> attributeMap = new
ConcurrentHashMap<>();
+
public MethodDescriptor(Method method) {
this.method = method;
Class<?>[] parameterTypes = method.getParameterTypes();
@@ -158,6 +162,14 @@ public class MethodDescriptor {
return generic;
}
+ public void addAttribute(String key, Object value) {
+ this.attributeMap.put(key, value);
+ }
+
+ public Object getAttribute(String key) {
+ return this.attributeMap.get(key);
+ }
+
public enum RpcType {
UNARY_WRAP,
UNARY_UNWRAP,
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java
index 8422baf..5da0a73 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java
@@ -114,6 +114,11 @@ public interface Invocation {
@Experimental("Experiment api for supporting Object transmission")
Object getObjectAttachment(String key);
+ @Experimental("Experiment api for supporting Object transmission")
+ default Object getObjectAttachmentWithoutConvert(String key) {
+ return getObjectAttachment(key);
+ }
+
/**
* get attachment by key with default 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 36ddee0..e5b9997 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
@@ -29,7 +29,7 @@ import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Stream;
@@ -71,7 +71,7 @@ public class RpcInvocation implements Invocation,
Serializable {
/**
* Only used on the caller side, will not appear on the wire.
*/
- private transient Map<Object, Object> attributes = new HashMap<Object,
Object>();
+ private transient Map<Object, Object> attributes = new
LinkedHashMap<Object, Object>();
private transient Invoker<?> invoker;
@@ -86,7 +86,7 @@ public class RpcInvocation implements Invocation,
Serializable {
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
this(invocation.getMethodName(), invocation.getServiceName(),
invocation.getProtocolServiceKey(),
- invocation.getParameterTypes(), invocation.getArguments(), new
HashMap<>(invocation.getObjectAttachments()),
+ invocation.getParameterTypes(), invocation.getArguments(), new
LinkedHashMap<>(invocation.getObjectAttachments()),
invocation.getInvoker(), invocation.getAttributes());
if (invoker != null) {
URL url = invoker.getUrl();
@@ -144,8 +144,8 @@ public class RpcInvocation implements Invocation,
Serializable {
this.protocolServiceKey = protocolServiceKey;
this.parameterTypes = parameterTypes == null ? new Class<?>[0] :
parameterTypes;
this.arguments = arguments == null ? new Object[0] : arguments;
- this.attachments = attachments == null ? new HashMap<>() : attachments;
- this.attributes = attributes == null ? new HashMap<>() : attributes;
+ this.attachments = attachments == null ? new LinkedHashMap<>() :
attachments;
+ this.attributes = attributes == null ? new LinkedHashMap<>() :
attributes;
this.invoker = invoker;
initParameterDesc();
}
@@ -267,7 +267,7 @@ public class RpcInvocation implements Invocation,
Serializable {
}
public void setObjectAttachments(Map<String, Object> attachments) {
- this.attachments = attachments == null ? new HashMap<>() : attachments;
+ this.attachments = attachments == null ? new LinkedHashMap<>() :
attachments;
}
@Override
@@ -283,7 +283,7 @@ public class RpcInvocation implements Invocation,
Serializable {
@Deprecated
public void setAttachments(Map<String, String> attachments) {
- this.attachments = attachments == null ? new HashMap<>() : new
HashMap<>(attachments);
+ this.attachments = attachments == null ? new LinkedHashMap<>() : new
LinkedHashMap<>(attachments);
}
public void setAttachment(String key, Object value) {
@@ -293,7 +293,7 @@ public class RpcInvocation implements Invocation,
Serializable {
@Override
public void setObjectAttachment(String key, Object value) {
if (attachments == null) {
- attachments = new HashMap<>();
+ attachments = new LinkedHashMap<>();
}
attachments.put(key, value);
}
@@ -310,7 +310,7 @@ public class RpcInvocation implements Invocation,
Serializable {
@Override
public void setObjectAttachmentIfAbsent(String key, Object value) {
if (attachments == null) {
- attachments = new HashMap<>();
+ attachments = new LinkedHashMap<>();
}
if (!attachments.containsKey(key)) {
attachments.put(key, value);
@@ -323,7 +323,7 @@ public class RpcInvocation implements Invocation,
Serializable {
return;
}
if (this.attachments == null) {
- this.attachments = new HashMap<>();
+ this.attachments = new LinkedHashMap<>();
}
this.attachments.putAll(attachments);
}
@@ -333,7 +333,7 @@ public class RpcInvocation implements Invocation,
Serializable {
return;
}
if (this.attachments == null) {
- this.attachments = new HashMap<>();
+ this.attachments = new LinkedHashMap<>();
}
this.attachments.putAll(attachments);
}
@@ -412,6 +412,14 @@ public class RpcInvocation implements Invocation,
Serializable {
return value;
}
+ @Override
+ public Object getObjectAttachmentWithoutConvert(String key) {
+ if (attachments == null) {
+ return null;
+ }
+ return attachments.get(key);
+ }
+
public Class<?> getReturnType() {
return returnType;
}
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java
index 5e9e74a..5df3e63 100644
---
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java
+++
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java
@@ -36,6 +36,7 @@ import static
org.apache.dubbo.common.constants.CommonConstants.$INVOKE;
import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE_ASYNC;
import static
org.apache.dubbo.common.constants.CommonConstants.GENERIC_PARAMETER_DESC;
import static
org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_ATTACHMENT_KEY;
+import static
org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_ATTACHMENT_KEY_LOWER;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
import static org.apache.dubbo.rpc.Constants.$ECHO;
import static org.apache.dubbo.rpc.Constants.$ECHO_PARAMETER_DESC;
@@ -237,7 +238,10 @@ public class RpcUtils {
public static long getTimeout(Invocation invocation, long defaultTimeout) {
long timeout = defaultTimeout;
- Object genericTimeout =
invocation.getObjectAttachment(TIMEOUT_ATTACHMENT_KEY);
+ Object genericTimeout =
invocation.getObjectAttachmentWithoutConvert(TIMEOUT_ATTACHMENT_KEY);
+ if(genericTimeout == null) {
+ genericTimeout =
invocation.getObjectAttachmentWithoutConvert(TIMEOUT_ATTACHMENT_KEY_LOWER);
+ }
if (genericTimeout != null) {
timeout = convertToNumber(genericTimeout, defaultTimeout);
}
diff --git
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
index f286ddb..fdd3ed6 100644
---
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
+++
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
@@ -39,7 +39,7 @@ import org.apache.dubbo.rpc.support.RpcUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
import static org.apache.dubbo.common.URL.buildKey;
@@ -154,7 +154,7 @@ public class DecodeableRpcInvocation extends RpcInvocation
implements Codec, Dec
if (map != null && map.size() > 0) {
Map<String, Object> attachment = getObjectAttachments();
if (attachment == null) {
- attachment = new HashMap<>();
+ attachment = new LinkedHashMap<>();
}
attachment.putAll(map);
setObjectAttachments(attachment);
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/AbstractStream.java
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/AbstractStream.java
index 2fd9524..eb685c9 100644
---
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/AbstractStream.java
+++
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/AbstractStream.java
@@ -34,7 +34,7 @@ import io.netty.handler.codec.http2.Http2Headers;
import java.io.IOException;
import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -210,7 +210,7 @@ public abstract class AbstractStream implements Stream {
}
protected Map<String, Object> parseMetadataToMap(Metadata metadata) {
- Map<String, Object> attachments = new HashMap<>();
+ Map<String, Object> attachments = new LinkedHashMap<>();
for (Map.Entry<CharSequence, CharSequence> header : metadata) {
String key = header.getKey().toString();
if (Http2Headers.PseudoHeaderName.isPseudoHeader(key)) {