From 08348de602a7e5784ec3384c8a586f2dcac695d2 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Mon, 25 Dec 2023 15:04:35 +0800 Subject: [PATCH 01/15] New deepCopyUtil --- .../rpc/protocol/injvm/ParamDeepCopyUtil.java | 3 + .../injvm/TripleParamDeepCopyUtil.java | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+) rename dubbo-rpc/{dubbo-rpc-injvm => dubbo-rpc-api}/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java (94%) create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java similarity index 94% rename from dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java index 1e76789b91b..8c6ed6bc730 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java @@ -17,17 +17,20 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.Adaptive; import org.apache.dubbo.common.extension.ExtensionScope; import org.apache.dubbo.common.extension.SPI; import java.lang.reflect.Type; @SPI(scope = ExtensionScope.FRAMEWORK) +@Adaptive public interface ParamDeepCopyUtil { default T copy(URL url, Object src, Class targetClass) { return copy(url, src, targetClass, null); } + @Adaptive T copy(URL url, Object src, Class targetClass, Type type); } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java new file mode 100644 index 00000000000..2a223458757 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java @@ -0,0 +1,80 @@ +package org.apache.dubbo.rpc.protocol.injvm; + +import com.google.protobuf.Message; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.protocol.tri.TripleInvoker; +import org.apache.dubbo.rpc.protocol.tri.compressor.Compressor; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.reflect.Type; + +import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; + + +public class TripleParamDeepCopyUtil implements ParamDeepCopyUtil{ + + @Override + public T copy(URL url, Object src, Class targetClass, Type type) { + + if(src instanceof Message) { + try { + OutputStream outputStream = new ByteArrayOutputStream(); + int compressed = 0; + outputStream.write(compressed); + int serializedSize = ((Message) src).getSerializedSize(); + + + Compressor compressor = TripleInvoker.getCompressorFromEnv(); + + }catch (Exception e) { + return null; + } + }else { + + } + } + + private static void write(HttpMessageCodec codec, OutputStream outputStream, Object data) { + int serializedSize = ((Message) data).getSerializedSize(); + // write length + writeLength(outputStream, serializedSize); + codec.encode(outputStream, data); + } + + private static void writeLength(OutputStream outputStream, int length) { + try { + outputStream.write(((length >> 24) & 0xFF)); + outputStream.write(((length >> 16) & 0xFF)); + outputStream.write(((length >> 8) & 0xFF)); + outputStream.write((length & 0xFF)); + } catch (IOException e) { + + } + } + + private static boolean isProtobuf(Object data) { + if (data == null) { + return false; + } + return isProtoClass(data.getClass()); + } + + private static boolean isProtoClass(Class clazz) { + while (clazz != Object.class && clazz != null) { + Class[] interfaces = clazz.getInterfaces(); + if (interfaces.length > 0) { + for (Class clazzInterface : interfaces) { + if (PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) { + return true; + } + } + } + clazz = clazz.getSuperclass(); + } + return false; + } + + +} From 634ac7c6089101aaa8ef5fa320dc602d46159a96 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Tue, 26 Dec 2023 13:17:16 +0800 Subject: [PATCH 02/15] Add CompositeParamDeepCopyUtil --- .../injvm/DefaultParamDeepCopyUtil.java | 2 + .../rpc/protocol/injvm/ParamDeepCopyUtil.java | 3 - .../rpc/protocol/injvm/InjvmInvoker.java | 3 +- .../injvm/CompositeParamDeepCopyUtil.java | 81 +++++++++++++++++++ .../injvm/TripleParamDeepCopyUtil.java | 80 ------------------ ...dubbo.rpc.protocol.injvm.ParamDeepCopyUtil | 1 + 6 files changed, 86 insertions(+), 84 deletions(-) rename dubbo-rpc/{dubbo-rpc-injvm => dubbo-rpc-api}/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java (98%) create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java delete mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java similarity index 98% rename from dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index 4f5243e6557..605b548e39a 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -17,6 +17,7 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.ObjectInput; @@ -31,6 +32,7 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE; +@Activate public class DefaultParamDeepCopyUtil implements ParamDeepCopyUtil { private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DefaultParamDeepCopyUtil.class); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java index 8c6ed6bc730..1e76789b91b 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtil.java @@ -17,20 +17,17 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.Adaptive; import org.apache.dubbo.common.extension.ExtensionScope; import org.apache.dubbo.common.extension.SPI; import java.lang.reflect.Type; @SPI(scope = ExtensionScope.FRAMEWORK) -@Adaptive public interface ParamDeepCopyUtil { default T copy(URL url, Object src, Class targetClass) { return copy(url, src, targetClass, null); } - @Adaptive T copy(URL url, Object src, Class targetClass, Type type); } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java index 68054d7ddb7..b31967b4fd5 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java @@ -82,7 +82,8 @@ public class InjvmInvoker extends AbstractInvoker { this.executorRepository = ExecutorRepository.getInstance(url.getOrDefaultApplicationModel()); this.paramDeepCopyUtil = url.getOrDefaultFrameworkModel() .getExtensionLoader(ParamDeepCopyUtil.class) - .getExtension(url.getParameter(CommonConstants.INJVM_COPY_UTIL_KEY, DefaultParamDeepCopyUtil.NAME)); + .getActivateExtensions() + .get(0); this.shouldIgnoreSameModule = url.getParameter(CommonConstants.INJVM_IGNORE_SAME_MODULE_KEY, false); } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java new file mode 100644 index 00000000000..b2a27b9dc62 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java @@ -0,0 +1,81 @@ +/* + * 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.protocol.injvm; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.rpc.protocol.tri.SingleProtobufUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.lang.reflect.Type; + +import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; +import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE; + +@Activate(order = -100) +public class CompositeParamDeepCopyUtil extends DefaultParamDeepCopyUtil { + + private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(CompositeParamDeepCopyUtil.class); + + @Override + public T copy(URL url, Object src, Class targetClass, Type type) { + if (isProtobuf(src)) { + try { + // encode + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + SingleProtobufUtils.serialize(src, outputStream); + // decode + ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + return SingleProtobufUtils.deserialize(inputStream, targetClass); + } catch (Exception e) { + logger.error(PROTOCOL_ERROR_DESERIALIZE, "", "", "Unable to deep copy parameter to target class.", e); + } + if (src.getClass().equals(targetClass)) { + return (T) src; + } else { + return null; + } + } else { + return super.copy(url, src, targetClass, type); + } + } + + private static boolean isProtobuf(Object data) { + if (data == null) { + return false; + } + return isProtoClass(data.getClass()); + } + + private static boolean isProtoClass(Class clazz) { + while (clazz != Object.class && clazz != null) { + Class[] interfaces = clazz.getInterfaces(); + if (interfaces.length > 0) { + for (Class clazzInterface : interfaces) { + if (PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) { + return true; + } + } + } + clazz = clazz.getSuperclass(); + } + return false; + } +} diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java deleted file mode 100644 index 2a223458757..00000000000 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/TripleParamDeepCopyUtil.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.apache.dubbo.rpc.protocol.injvm; - -import com.google.protobuf.Message; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.rpc.protocol.tri.TripleInvoker; -import org.apache.dubbo.rpc.protocol.tri.compressor.Compressor; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.lang.reflect.Type; - -import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; - - -public class TripleParamDeepCopyUtil implements ParamDeepCopyUtil{ - - @Override - public T copy(URL url, Object src, Class targetClass, Type type) { - - if(src instanceof Message) { - try { - OutputStream outputStream = new ByteArrayOutputStream(); - int compressed = 0; - outputStream.write(compressed); - int serializedSize = ((Message) src).getSerializedSize(); - - - Compressor compressor = TripleInvoker.getCompressorFromEnv(); - - }catch (Exception e) { - return null; - } - }else { - - } - } - - private static void write(HttpMessageCodec codec, OutputStream outputStream, Object data) { - int serializedSize = ((Message) data).getSerializedSize(); - // write length - writeLength(outputStream, serializedSize); - codec.encode(outputStream, data); - } - - private static void writeLength(OutputStream outputStream, int length) { - try { - outputStream.write(((length >> 24) & 0xFF)); - outputStream.write(((length >> 16) & 0xFF)); - outputStream.write(((length >> 8) & 0xFF)); - outputStream.write((length & 0xFF)); - } catch (IOException e) { - - } - } - - private static boolean isProtobuf(Object data) { - if (data == null) { - return false; - } - return isProtoClass(data.getClass()); - } - - private static boolean isProtoClass(Class clazz) { - while (clazz != Object.class && clazz != null) { - Class[] interfaces = clazz.getInterfaces(); - if (interfaces.length > 0) { - for (Class clazzInterface : interfaces) { - if (PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) { - return true; - } - } - } - clazz = clazz.getSuperclass(); - } - return false; - } - - -} diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil new file mode 100644 index 00000000000..9bbe2dfc582 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil @@ -0,0 +1 @@ +composite=org.apache.dubbo.rpc.protocol.injvm.CompositeParamDeepCopyUtil From de92d7ad78bf33bb1f9892cd281457de80c084d6 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Tue, 26 Dec 2023 16:50:27 +0800 Subject: [PATCH 03/15] Fix wrong @Parameter place --- .../java/org/apache/dubbo/config/AbstractServiceConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java index c1bfe1c59ba..41913e812be 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java @@ -397,11 +397,11 @@ public void setPayload(Integer payload) { this.payload = payload; } + @Parameter(excluded = true) public Boolean getUseJavaPackageAsPath() { return useJavaPackageAsPath; } - @Parameter(excluded = true) public void setUseJavaPackageAsPath(Boolean useJavaPackageAsPath) { this.useJavaPackageAsPath = useJavaPackageAsPath; } From f193c8898d58b677932d0d54d437a88895b63860 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Tue, 26 Dec 2023 19:33:19 +0800 Subject: [PATCH 04/15] Refactor CompositeParamDeepCopyUtil --- .../rpc/protocol/injvm/CompositeParamDeepCopyUtil.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java index b2a27b9dc62..009d179bc72 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java @@ -29,11 +29,16 @@ import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE; +/** + * Provides protobuf class deep copy capacity + */ @Activate(order = -100) -public class CompositeParamDeepCopyUtil extends DefaultParamDeepCopyUtil { +public class CompositeParamDeepCopyUtil implements ParamDeepCopyUtil { private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(CompositeParamDeepCopyUtil.class); + private final ParamDeepCopyUtil defaultParamDeepCopyUtil = new DefaultParamDeepCopyUtil(); + @Override public T copy(URL url, Object src, Class targetClass, Type type) { if (isProtobuf(src)) { @@ -53,7 +58,7 @@ public T copy(URL url, Object src, Class targetClass, Type type) { return null; } } else { - return super.copy(url, src, targetClass, type); + return defaultParamDeepCopyUtil.copy(url, src, targetClass, type); } } From 0931bbfafac3c2d3c4773bff2a0e2f9e97822f8e Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Wed, 27 Dec 2023 12:49:27 +0800 Subject: [PATCH 05/15] Simplify CompositeParamDeepCopyUtil --- .../injvm/CompositeParamDeepCopyUtil.java | 25 +------------------ .../rpc/protocol/tri/SingleProtobufUtils.java | 2 +- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java index 009d179bc72..b29cf6d4ff7 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java @@ -26,7 +26,6 @@ import java.io.ByteArrayOutputStream; import java.lang.reflect.Type; -import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE; /** @@ -41,7 +40,7 @@ public class CompositeParamDeepCopyUtil implements ParamDeepCopyUtil { @Override public T copy(URL url, Object src, Class targetClass, Type type) { - if (isProtobuf(src)) { + if (SingleProtobufUtils.isSupported(src.getClass())) { try { // encode ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -61,26 +60,4 @@ public T copy(URL url, Object src, Class targetClass, Type type) { return defaultParamDeepCopyUtil.copy(url, src, targetClass, type); } } - - private static boolean isProtobuf(Object data) { - if (data == null) { - return false; - } - return isProtoClass(data.getClass()); - } - - private static boolean isProtoClass(Class clazz) { - while (clazz != Object.class && clazz != null) { - Class[] interfaces = clazz.getInterfaces(); - if (interfaces.length > 0) { - for (Class clazzInterface : interfaces) { - if (PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) { - return true; - } - } - } - clazz = clazz.getSuperclass(); - } - return false; - } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java index ec51dacc7ae..298624e5ff4 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java @@ -61,7 +61,7 @@ public class SingleProtobufUtils { marshaller(ListValue.getDefaultInstance()); } - static boolean isSupported(Class clazz) { + public static boolean isSupported(Class clazz) { if (clazz == null) { return false; } From 545833a1af250dadb783e47c0c248b1fd8c48b58 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Wed, 27 Dec 2023 16:43:24 +0800 Subject: [PATCH 06/15] Simplify CompositeParamDeepCopyUtil --- .../dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java index b29cf6d4ff7..92b5497cb94 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.ProtobufUtils; import org.apache.dubbo.rpc.protocol.tri.SingleProtobufUtils; import java.io.ByteArrayInputStream; @@ -40,7 +41,7 @@ public class CompositeParamDeepCopyUtil implements ParamDeepCopyUtil { @Override public T copy(URL url, Object src, Class targetClass, Type type) { - if (SingleProtobufUtils.isSupported(src.getClass())) { + if (src != null && ProtobufUtils.isProtobufClass(src.getClass())) { try { // encode ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); From 70bb6c5240cbbdd486562406f9d272ff1c8cd458 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <100946116+namelessssssssssss@users.noreply.github.com> Date: Wed, 27 Dec 2023 16:51:17 +0800 Subject: [PATCH 07/15] Update SingleProtobufUtils.java --- .../org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java index 298624e5ff4..ec51dacc7ae 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/SingleProtobufUtils.java @@ -61,7 +61,7 @@ public class SingleProtobufUtils { marshaller(ListValue.getDefaultInstance()); } - public static boolean isSupported(Class clazz) { + static boolean isSupported(Class clazz) { if (clazz == null) { return false; } From 0d8589cbe5c1457003ee657a8f6d4046bfefce16 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Thu, 28 Dec 2023 20:32:40 +0800 Subject: [PATCH 08/15] Simplify CompositeParamDeepCopyUtil --- .../protocol/injvm/DefaultParamDeepCopyUtil.java | 2 -- .../dubbo/rpc/protocol/injvm/InjvmInvoker.java | 3 +-- ...pyUtil.java => ParamDeepCopyUtilWrapper.java} | 16 ++++++++++------ ...he.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) rename dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/{CompositeParamDeepCopyUtil.java => ParamDeepCopyUtilWrapper.java} (84%) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index 605b548e39a..4f5243e6557 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -17,7 +17,6 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.ObjectInput; @@ -32,7 +31,6 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE; -@Activate public class DefaultParamDeepCopyUtil implements ParamDeepCopyUtil { private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DefaultParamDeepCopyUtil.class); diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java index b31967b4fd5..049d7e4e0b5 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java @@ -82,8 +82,7 @@ public class InjvmInvoker extends AbstractInvoker { this.executorRepository = ExecutorRepository.getInstance(url.getOrDefaultApplicationModel()); this.paramDeepCopyUtil = url.getOrDefaultFrameworkModel() .getExtensionLoader(ParamDeepCopyUtil.class) - .getActivateExtensions() - .get(0); + .getExtension(DefaultParamDeepCopyUtil.NAME, true); this.shouldIgnoreSameModule = url.getParameter(CommonConstants.INJVM_IGNORE_SAME_MODULE_KEY, false); } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtilWrapper.java similarity index 84% rename from dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java rename to dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtilWrapper.java index 92b5497cb94..e6a0c83442c 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/CompositeParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtilWrapper.java @@ -17,7 +17,7 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.extension.Wrapper; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ProtobufUtils; @@ -32,12 +32,16 @@ /** * Provides protobuf class deep copy capacity */ -@Activate(order = -100) -public class CompositeParamDeepCopyUtil implements ParamDeepCopyUtil { +@Wrapper +public class ParamDeepCopyUtilWrapper implements ParamDeepCopyUtil { - private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(CompositeParamDeepCopyUtil.class); + private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ParamDeepCopyUtilWrapper.class); - private final ParamDeepCopyUtil defaultParamDeepCopyUtil = new DefaultParamDeepCopyUtil(); + private final ParamDeepCopyUtil paramDeepCopyUtil; + + public ParamDeepCopyUtilWrapper(ParamDeepCopyUtil paramDeepCopyUtil) { + this.paramDeepCopyUtil = paramDeepCopyUtil; + } @Override public T copy(URL url, Object src, Class targetClass, Type type) { @@ -58,7 +62,7 @@ public T copy(URL url, Object src, Class targetClass, Type type) { return null; } } else { - return defaultParamDeepCopyUtil.copy(url, src, targetClass, type); + return paramDeepCopyUtil.copy(url, src, targetClass, type); } } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil index 9bbe2dfc582..65f6aaf5577 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil @@ -1 +1 @@ -composite=org.apache.dubbo.rpc.protocol.injvm.CompositeParamDeepCopyUtil +composite=org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtilWrapper From f17f3c4aaad2b1bf48f8532b84a76ba3305a10df Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Sat, 30 Dec 2023 16:36:30 +0800 Subject: [PATCH 09/15] Refactor CompositeParamDeepCopyUtil --- .../injvm/DefaultParamDeepCopyUtil.java | 21 ++++++++++++++++++ .../rpc/protocol/injvm/InjvmInvoker.java | 2 +- ...r.java => DefaultPbParamDeepCopyUtil.java} | 22 ++++++------------- ...dubbo.rpc.protocol.injvm.ParamDeepCopyUtil | 2 +- 4 files changed, 30 insertions(+), 17 deletions(-) rename dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/{ParamDeepCopyUtilWrapper.java => DefaultPbParamDeepCopyUtil.java} (78%) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index 4f5243e6557..41470215f04 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -22,7 +22,9 @@ import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; +import org.apache.dubbo.common.utils.ProtobufUtils; import org.apache.dubbo.remoting.utils.UrlUtils; +import org.apache.dubbo.rpc.model.FrameworkModel; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -37,9 +39,28 @@ public class DefaultParamDeepCopyUtil implements ParamDeepCopyUtil { public static final String NAME = "default"; + private static final String PB_UTIL_NAME = "protobuf"; + + private ParamDeepCopyUtil protobufDeepCopyUtil; + + public DefaultParamDeepCopyUtil() {} + + public void setFrameworkModel(FrameworkModel frameworkModel) { + try { + this.protobufDeepCopyUtil = + frameworkModel.getExtensionLoader(ParamDeepCopyUtil.class).getExtension(PB_UTIL_NAME); + } catch (IllegalStateException illegalStateException) { + this.protobufDeepCopyUtil = null; + } + } + @Override @SuppressWarnings({"unchecked"}) public T copy(URL url, Object src, Class targetClass, Type type) { + if (src != null && ProtobufUtils.isProtobufClass(src.getClass()) && protobufDeepCopyUtil != null) { + return protobufDeepCopyUtil.copy(url, src, targetClass); + } + Serialization serialization = url.getOrDefaultFrameworkModel() .getExtensionLoader(Serialization.class) .getExtension(UrlUtils.serializationOrDefault(url)); diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java index 049d7e4e0b5..68054d7ddb7 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java @@ -82,7 +82,7 @@ public class InjvmInvoker extends AbstractInvoker { this.executorRepository = ExecutorRepository.getInstance(url.getOrDefaultApplicationModel()); this.paramDeepCopyUtil = url.getOrDefaultFrameworkModel() .getExtensionLoader(ParamDeepCopyUtil.class) - .getExtension(DefaultParamDeepCopyUtil.NAME, true); + .getExtension(url.getParameter(CommonConstants.INJVM_COPY_UTIL_KEY, DefaultParamDeepCopyUtil.NAME)); this.shouldIgnoreSameModule = url.getParameter(CommonConstants.INJVM_IGNORE_SAME_MODULE_KEY, false); } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtilWrapper.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultPbParamDeepCopyUtil.java similarity index 78% rename from dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtilWrapper.java rename to dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultPbParamDeepCopyUtil.java index e6a0c83442c..84f7bd46301 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/ParamDeepCopyUtilWrapper.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultPbParamDeepCopyUtil.java @@ -17,7 +17,6 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.Wrapper; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ProtobufUtils; @@ -32,16 +31,11 @@ /** * Provides protobuf class deep copy capacity */ -@Wrapper -public class ParamDeepCopyUtilWrapper implements ParamDeepCopyUtil { +public class DefaultPbParamDeepCopyUtil extends DefaultParamDeepCopyUtil { - private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ParamDeepCopyUtilWrapper.class); + private static final String NAME = "protobuf"; - private final ParamDeepCopyUtil paramDeepCopyUtil; - - public ParamDeepCopyUtilWrapper(ParamDeepCopyUtil paramDeepCopyUtil) { - this.paramDeepCopyUtil = paramDeepCopyUtil; - } + private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DefaultPbParamDeepCopyUtil.class); @Override public T copy(URL url, Object src, Class targetClass, Type type) { @@ -56,13 +50,11 @@ public T copy(URL url, Object src, Class targetClass, Type type) { } catch (Exception e) { logger.error(PROTOCOL_ERROR_DESERIALIZE, "", "", "Unable to deep copy parameter to target class.", e); } - if (src.getClass().equals(targetClass)) { - return (T) src; - } else { - return null; - } + } + if (src != null && src.getClass().equals(targetClass)) { + return (T) src; } else { - return paramDeepCopyUtil.copy(url, src, targetClass, type); + return null; } } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil index 65f6aaf5577..7623d8a4e0b 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil @@ -1 +1 @@ -composite=org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtilWrapper +protobuf=org.apache.dubbo.rpc.protocol.injvm.DefaultPbParamDeepCopyUtil From 861ce1b3ac1f9a6de141fc4e72a348874ee6d268 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Sun, 31 Dec 2023 00:12:34 +0800 Subject: [PATCH 10/15] Refactor DefaultParamDeepCopyUtil --- .../common/constants/CommonConstants.java | 2 + .../injvm/DefaultParamDeepCopyUtil.java | 23 ++-- .../injvm/DefaultPbParamDeepCopyUtil.java | 60 ---------- .../tri/serialize/ProtobufObjectInput.java | 101 +++++++++++++++++ .../tri/serialize/ProtobufObjectOutput.java | 105 ++++++++++++++++++ .../tri/serialize/ProtobufSerialization.java | 52 +++++++++ ...pache.dubbo.common.serialize.Serialization | 1 + ...dubbo.rpc.protocol.injvm.ParamDeepCopyUtil | 1 - 8 files changed, 276 insertions(+), 69 deletions(-) delete mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultPbParamDeepCopyUtil.java create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectOutput.java create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufSerialization.java create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization delete mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil 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 0178a5382c0..ebb747edfb3 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 @@ -280,6 +280,8 @@ public interface CommonConstants { String PROTOBUF_MESSAGE_CLASS_NAME = "com.google.protobuf.Message"; + String PROTOBUF_SERIALIZATION_NAME = "protobuf"; + int MAX_PROXY_COUNT = 65535; String MONITOR_KEY = "monitor"; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index 41470215f04..399fa7dc0f3 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -17,6 +17,8 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.constants.CommonConstants; +import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.ObjectInput; @@ -57,14 +59,7 @@ public void setFrameworkModel(FrameworkModel frameworkModel) { @Override @SuppressWarnings({"unchecked"}) public T copy(URL url, Object src, Class targetClass, Type type) { - if (src != null && ProtobufUtils.isProtobufClass(src.getClass()) && protobufDeepCopyUtil != null) { - return protobufDeepCopyUtil.copy(url, src, targetClass); - } - - Serialization serialization = url.getOrDefaultFrameworkModel() - .getExtensionLoader(Serialization.class) - .getExtension(UrlUtils.serializationOrDefault(url)); - + Serialization serialization = findSerialization(url,src); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ObjectOutput objectOutput = serialization.serialize(url, outputStream); objectOutput.writeObject(src); @@ -91,4 +86,16 @@ public T copy(URL url, Object src, Class targetClass, Type type) { return null; } } + + private Serialization findSerialization(URL url,Object src){ + ExtensionLoader extensionLoader = url.getOrDefaultFrameworkModel().getExtensionLoader(Serialization.class); + if (src != null && ProtobufUtils.isProtobufClass(src.getClass()) && protobufDeepCopyUtil != null) { + try { + return extensionLoader.getExtension(CommonConstants.PROTOBUF_SERIALIZATION_NAME); + }catch (IllegalStateException ignored){} + } + return url.getOrDefaultFrameworkModel() + .getExtensionLoader(Serialization.class) + .getExtension(UrlUtils.serializationOrDefault(url)); + } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultPbParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultPbParamDeepCopyUtil.java deleted file mode 100644 index 84f7bd46301..00000000000 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultPbParamDeepCopyUtil.java +++ /dev/null @@ -1,60 +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.protocol.injvm; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ProtobufUtils; -import org.apache.dubbo.rpc.protocol.tri.SingleProtobufUtils; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.lang.reflect.Type; - -import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE; - -/** - * Provides protobuf class deep copy capacity - */ -public class DefaultPbParamDeepCopyUtil extends DefaultParamDeepCopyUtil { - - private static final String NAME = "protobuf"; - - private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DefaultPbParamDeepCopyUtil.class); - - @Override - public T copy(URL url, Object src, Class targetClass, Type type) { - if (src != null && ProtobufUtils.isProtobufClass(src.getClass())) { - try { - // encode - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - SingleProtobufUtils.serialize(src, outputStream); - // decode - ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); - return SingleProtobufUtils.deserialize(inputStream, targetClass); - } catch (Exception e) { - logger.error(PROTOCOL_ERROR_DESERIALIZE, "", "", "Unable to deep copy parameter to target class.", e); - } - } - if (src != null && src.getClass().equals(targetClass)) { - return (T) src; - } else { - return null; - } - } -} diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java new file mode 100644 index 00000000000..8f926d91a0d --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java @@ -0,0 +1,101 @@ +/* + * 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.protocol.tri.serialize; + +import org.apache.dubbo.common.serialize.ObjectInput; +import org.apache.dubbo.rpc.protocol.tri.SingleProtobufUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Type; + +import com.google.protobuf.BoolValue; +import com.google.protobuf.BytesValue; +import com.google.protobuf.DoubleValue; +import com.google.protobuf.FloatValue; +import com.google.protobuf.Int32Value; +import com.google.protobuf.Int64Value; +import com.google.protobuf.StringValue; + +public class ProtobufObjectInput implements ObjectInput { + + private final InputStream input; + + public ProtobufObjectInput(InputStream input) { + this.input = input; + } + + @Override + public Object readObject() { + return null; + } + + @Override + public T readObject(Class cls) throws IOException { + return SingleProtobufUtils.deserialize(input, cls); + } + + @Override + public T readObject(Class cls, Type type) throws IOException { + return SingleProtobufUtils.deserialize(input, cls); + } + + @Override + public boolean readBool() throws IOException { + return BoolValue.parseFrom(input).getValue(); + } + + @Override + public byte readByte() throws IOException { + return (byte) input.read(); + } + + @Override + public short readShort() throws IOException { + return (short) Int32Value.parseFrom(input).getValue(); + } + + @Override + public int readInt() throws IOException { + return Int32Value.parseFrom(input).getValue(); + } + + @Override + public long readLong() throws IOException { + return Int64Value.parseFrom(input).getValue(); + } + + @Override + public float readFloat() throws IOException { + return FloatValue.parseFrom(input).getValue(); + } + + @Override + public double readDouble() throws IOException { + return DoubleValue.parseFrom(input).getValue(); + } + + @Override + public String readUTF() throws IOException { + return StringValue.parseFrom(input).getValue(); + } + + @Override + public byte[] readBytes() throws IOException { + return BytesValue.parseFrom(input).getValue().toByteArray(); + } +} diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectOutput.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectOutput.java new file mode 100644 index 00000000000..01844c85986 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectOutput.java @@ -0,0 +1,105 @@ +/* + * 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.protocol.tri.serialize; + +import org.apache.dubbo.common.serialize.ObjectOutput; +import org.apache.dubbo.rpc.protocol.tri.SingleProtobufUtils; + +import java.io.IOException; +import java.io.OutputStream; + +import com.google.protobuf.BoolValue; +import com.google.protobuf.ByteString; +import com.google.protobuf.BytesValue; +import com.google.protobuf.DoubleValue; +import com.google.protobuf.FloatValue; +import com.google.protobuf.Int32Value; +import com.google.protobuf.Int64Value; +import com.google.protobuf.StringValue; + +public class ProtobufObjectOutput implements ObjectOutput { + + private final OutputStream output; + + public ProtobufObjectOutput(OutputStream output) { + this.output = output; + } + + @Override + public void writeObject(Object obj) throws IOException { + SingleProtobufUtils.serialize(obj, output); + } + + @Override + public void writeBool(boolean v) throws IOException { + output.write(BoolValue.newBuilder().setValue(v).build().toByteArray()); + } + + @Override + public void writeByte(byte v) throws IOException { + output.write(v); + } + + @Override + public void writeShort(short v) throws IOException { + output.write(Int32Value.newBuilder().setValue(v).build().toByteArray()); + } + + @Override + public void writeInt(int v) throws IOException { + output.write(Int32Value.newBuilder().setValue(v).build().toByteArray()); + } + + @Override + public void writeLong(long v) throws IOException { + output.write(Int64Value.newBuilder().setValue(v).build().toByteArray()); + } + + @Override + public void writeFloat(float v) throws IOException { + output.write(FloatValue.newBuilder().setValue(v).build().toByteArray()); + } + + @Override + public void writeDouble(double v) throws IOException { + output.write(DoubleValue.newBuilder().setValue(v).build().toByteArray()); + } + + @Override + public void writeUTF(String v) throws IOException { + output.write(StringValue.newBuilder().setValue(v).build().toByteArray()); + } + + @Override + public void writeBytes(byte[] v) throws IOException { + output.write( + BytesValue.newBuilder().setValue(ByteString.copyFrom(v)).build().toByteArray()); + } + + @Override + public void writeBytes(byte[] v, int off, int len) throws IOException { + output.write(BytesValue.newBuilder() + .setValue(ByteString.copyFrom(v, off, len)) + .build() + .toByteArray()); + } + + @Override + public void flushBuffer() throws IOException { + output.flush(); + } +} diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufSerialization.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufSerialization.java new file mode 100644 index 00000000000..9dd79975740 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufSerialization.java @@ -0,0 +1,52 @@ +/* + * 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.protocol.tri.serialize; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.serialize.Constants; +import org.apache.dubbo.common.serialize.ObjectInput; +import org.apache.dubbo.common.serialize.ObjectOutput; +import org.apache.dubbo.common.serialize.Serialization; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class ProtobufSerialization implements Serialization { + + private static final String NAME = "protobuf"; + + @Override + public byte getContentTypeId() { + return Constants.PROTOBUF_SERIALIZATION_ID; + } + + @Override + public String getContentType() { + return "application/protobuf"; + } + + @Override + public ObjectOutput serialize(URL url, OutputStream output) throws IOException { + return new ProtobufObjectOutput(output); + } + + @Override + public ObjectInput deserialize(URL url, InputStream input) { + return new ProtobufObjectInput(input); + } +} diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization new file mode 100644 index 00000000000..0384cb79821 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization @@ -0,0 +1 @@ +protobuf=org.apache.dubbo.rpc.protocol.tri.serialize.ProtobufSerialization diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil deleted file mode 100644 index 7623d8a4e0b..00000000000 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil +++ /dev/null @@ -1 +0,0 @@ -protobuf=org.apache.dubbo.rpc.protocol.injvm.DefaultPbParamDeepCopyUtil From 65f4063002f9243ad13dd2ff5fa99d534338b6ae Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Sun, 31 Dec 2023 00:14:31 +0800 Subject: [PATCH 11/15] Refactor DefaultParamDeepCopyUtil --- .../protocol/injvm/DefaultParamDeepCopyUtil.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index 399fa7dc0f3..b7f04d5aff2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -59,7 +59,7 @@ public void setFrameworkModel(FrameworkModel frameworkModel) { @Override @SuppressWarnings({"unchecked"}) public T copy(URL url, Object src, Class targetClass, Type type) { - Serialization serialization = findSerialization(url,src); + Serialization serialization = findSerialization(url, src); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ObjectOutput objectOutput = serialization.serialize(url, outputStream); objectOutput.writeObject(src); @@ -87,15 +87,15 @@ public T copy(URL url, Object src, Class targetClass, Type type) { } } - private Serialization findSerialization(URL url,Object src){ - ExtensionLoader extensionLoader = url.getOrDefaultFrameworkModel().getExtensionLoader(Serialization.class); + private Serialization findSerialization(URL url, Object src) { + ExtensionLoader extensionLoader = + url.getOrDefaultFrameworkModel().getExtensionLoader(Serialization.class); if (src != null && ProtobufUtils.isProtobufClass(src.getClass()) && protobufDeepCopyUtil != null) { try { return extensionLoader.getExtension(CommonConstants.PROTOBUF_SERIALIZATION_NAME); - }catch (IllegalStateException ignored){} + } catch (IllegalStateException ignored) { + } } - return url.getOrDefaultFrameworkModel() - .getExtensionLoader(Serialization.class) - .getExtension(UrlUtils.serializationOrDefault(url)); + return extensionLoader.getExtension(UrlUtils.serializationOrDefault(url)); } } From 8523410ca8f25058306dcfebc640cacbaa159cc0 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Sun, 31 Dec 2023 16:17:32 +0800 Subject: [PATCH 12/15] Update ProtobufObjectInput --- .../dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java index 8f926d91a0d..99da89d983b 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/serialize/ProtobufObjectInput.java @@ -41,7 +41,7 @@ public ProtobufObjectInput(InputStream input) { @Override public Object readObject() { - return null; + throw new UnsupportedOperationException("Protobuf serialization does not support readObject()"); } @Override From 0cd3ba63d018acc4c5d62b3b6e87375196074bce Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Mon, 1 Jan 2024 14:50:19 +0800 Subject: [PATCH 13/15] Bug fix --- .../dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index b7f04d5aff2..c122f08c502 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -90,7 +90,7 @@ public T copy(URL url, Object src, Class targetClass, Type type) { private Serialization findSerialization(URL url, Object src) { ExtensionLoader extensionLoader = url.getOrDefaultFrameworkModel().getExtensionLoader(Serialization.class); - if (src != null && ProtobufUtils.isProtobufClass(src.getClass()) && protobufDeepCopyUtil != null) { + if (src != null && ProtobufUtils.isProtobufClass(src.getClass())) { try { return extensionLoader.getExtension(CommonConstants.PROTOBUF_SERIALIZATION_NAME); } catch (IllegalStateException ignored) { From 85efe677f90812562db0df931f75570f51e82660 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Tue, 2 Jan 2024 13:50:12 +0800 Subject: [PATCH 14/15] Refactor --- .../common/constants/CommonConstants.java | 3 --- .../apache/dubbo/remoting/utils/UrlUtils.java | 8 ++++++++ .../injvm/DefaultParamDeepCopyUtil.java | 19 +++---------------- ...pache.dubbo.common.serialize.Serialization | 2 +- 4 files changed, 12 insertions(+), 20 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 ebb747edfb3..ba35cd69322 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 @@ -279,9 +279,6 @@ public interface CommonConstants { String RELEASE_KEY = "release"; String PROTOBUF_MESSAGE_CLASS_NAME = "com.google.protobuf.Message"; - - String PROTOBUF_SERIALIZATION_NAME = "protobuf"; - int MAX_PROXY_COUNT = 65535; String MONITOR_KEY = "monitor"; diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/utils/UrlUtils.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/utils/UrlUtils.java index 2ba32fc4eba..23aa3c76661 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/utils/UrlUtils.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/utils/UrlUtils.java @@ -17,6 +17,7 @@ package org.apache.dubbo.remoting.utils; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.serialize.support.DefaultSerializationSelector; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Constants; @@ -111,11 +112,18 @@ public static Byte serializationId(URL url) { * @return {@link String} */ public static String serializationOrDefault(URL url) { + if (useNativeStubSerialization(url)) { + return CommonConstants.NATIVE_STUB; + } //noinspection OptionalGetWithoutIsPresent Optional serializations = allSerializations(url).stream().findFirst(); return serializations.orElseGet(DefaultSerializationSelector::getDefaultRemotingSerialization); } + public static boolean useNativeStubSerialization(URL url) { + return CommonConstants.NATIVE_STUB.equals(url.getParameter("proxy", CommonConstants.DEFAULT_PROXY)); + } + /** * Get the all serializations,ensure insertion order * diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index c122f08c502..5a5786a1c45 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -17,14 +17,11 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.constants.CommonConstants; -import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; -import org.apache.dubbo.common.utils.ProtobufUtils; import org.apache.dubbo.remoting.utils.UrlUtils; import org.apache.dubbo.rpc.model.FrameworkModel; @@ -59,7 +56,9 @@ public void setFrameworkModel(FrameworkModel frameworkModel) { @Override @SuppressWarnings({"unchecked"}) public T copy(URL url, Object src, Class targetClass, Type type) { - Serialization serialization = findSerialization(url, src); + Serialization serialization = url.getOrDefaultFrameworkModel() + .getExtensionLoader(Serialization.class) + .getExtension(UrlUtils.serializationOrDefault(url)); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ObjectOutput objectOutput = serialization.serialize(url, outputStream); objectOutput.writeObject(src); @@ -86,16 +85,4 @@ public T copy(URL url, Object src, Class targetClass, Type type) { return null; } } - - private Serialization findSerialization(URL url, Object src) { - ExtensionLoader extensionLoader = - url.getOrDefaultFrameworkModel().getExtensionLoader(Serialization.class); - if (src != null && ProtobufUtils.isProtobufClass(src.getClass())) { - try { - return extensionLoader.getExtension(CommonConstants.PROTOBUF_SERIALIZATION_NAME); - } catch (IllegalStateException ignored) { - } - } - return extensionLoader.getExtension(UrlUtils.serializationOrDefault(url)); - } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization index 0384cb79821..20fa093ff15 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization @@ -1 +1 @@ -protobuf=org.apache.dubbo.rpc.protocol.tri.serialize.ProtobufSerialization +nativestub=org.apache.dubbo.rpc.protocol.tri.serialize.ProtobufSerialization From 62f5deed7e6d6c802d6666e82304e53f79041994 Mon Sep 17 00:00:00 2001 From: namelessssssssssss <1544669126@qq.com> Date: Tue, 2 Jan 2024 17:12:24 +0800 Subject: [PATCH 15/15] Refactor --- .../dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java index 5a5786a1c45..83aad4128c3 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/injvm/DefaultParamDeepCopyUtil.java @@ -22,6 +22,7 @@ import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; +import org.apache.dubbo.common.utils.ProtobufUtils; import org.apache.dubbo.remoting.utils.UrlUtils; import org.apache.dubbo.rpc.model.FrameworkModel; @@ -56,6 +57,10 @@ public void setFrameworkModel(FrameworkModel frameworkModel) { @Override @SuppressWarnings({"unchecked"}) public T copy(URL url, Object src, Class targetClass, Type type) { + //TODO: maybe we have better way to do this + if(src != null && ProtobufUtils.isProtobufClass(src.getClass())){ + return (T)src; + } Serialization serialization = url.getOrDefaultFrameworkModel() .getExtensionLoader(Serialization.class) .getExtension(UrlUtils.serializationOrDefault(url));