Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Triple] Fix boxed type methods #11577

Merged
merged 3 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.dubbo.common.serialize.MultipleSerialization;
import org.apache.dubbo.common.serialize.support.DefaultSerializationSelector;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.config.Constants;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.PackableMethod;
Expand All @@ -34,8 +33,7 @@
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

import static org.apache.dubbo.common.constants.CommonConstants.$ECHO;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME;
Expand Down Expand Up @@ -93,7 +91,8 @@ public ReflectionPackableMethod(MethodDescriptor method, URL url, String seriali
.getExtension(url.getParameter(Constants.MULTI_SERIALIZATION_KEY,
CommonConstants.DEFAULT_KEY));

this.requestPack = new WrapRequestPack(serialization, url, serializeName, singleArgument);
this.requestPack = new WrapRequestPack(serialization, url, serializeName, actualRequestTypes,
singleArgument);
this.responsePack = new WrapResponsePack(serialization, url, actualResponseType);
this.requestUnpack = new WrapRequestUnpack(serialization, url, actualRequestTypes);
this.responseUnpack = new WrapResponseUnpack(serialization, url, actualResponseType);
Expand Down Expand Up @@ -323,16 +322,10 @@ private WrapResponsePack(MultipleSerialization multipleSerialization, URL url,
@Override
public byte[] pack(Object obj) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Class<?> clz;
if (obj != null) {
clz = obj.getClass();
} else {
clz = actualResponseType;
}
multipleSerialization.serialize(url, serialize, clz, obj, bos);
multipleSerialization.serialize(url, serialize, actualResponseType, obj, bos);
return TripleCustomerProtocolWapper.TripleResponseWrapper.Builder.newBuilder()
.setSerializeType(serialize)
.setType(clz.getName())
.setType(actualResponseType.getName())
.setData(bos.toByteArray())
.build()
.toByteArray();
Expand All @@ -341,17 +334,15 @@ public byte[] pack(Object obj) throws IOException {

private static class WrapResponseUnpack implements UnPack {

private final Map<String, Class<?>> classCache = new ConcurrentHashMap<>();

private final MultipleSerialization serialization;
private final URL url;
private final Class<?> returnClass;

private final Class<?> actualResponseType;

private WrapResponseUnpack(MultipleSerialization serialization, URL url, Class<?> actualResponseType) {
private WrapResponseUnpack(MultipleSerialization serialization, URL url, Class<?> returnClass) {
this.serialization = serialization;
this.url = url;
this.actualResponseType = actualResponseType;
this.returnClass = returnClass;
}

@Override
Expand All @@ -360,25 +351,27 @@ public Object unpack(byte[] data) throws IOException, ClassNotFoundException {
.parseFrom(data);
final String serializeType = convertHessianFromWrapper(wrapper.getSerializeType());
ByteArrayInputStream bais = new ByteArrayInputStream(wrapper.getData());
Class<?> clz = getClassFromCache(wrapper.getType(), classCache, actualResponseType);
return serialization.deserialize(url, serializeType, clz, bais);
return serialization.deserialize(url, serializeType, returnClass, bais);
}
}

private static class WrapRequestPack implements Pack {

private final String serialize;
private final MultipleSerialization multipleSerialization;
private final String[] argumentsType;
private final URL url;
private final boolean singleArgument;

private WrapRequestPack(MultipleSerialization multipleSerialization,
URL url,
String serialize,
Class<?>[] actualRequestTypes,
boolean singleArgument) {
this.url = url;
this.serialize = convertHessianToWrapper(serialize);
this.multipleSerialization = multipleSerialization;
this.argumentsType = Stream.of(actualRequestTypes).map(Class::getName).toArray(String[]::new);
this.singleArgument = singleArgument;
}

Expand All @@ -392,8 +385,10 @@ public byte[] pack(Object obj) throws IOException {
}
final TripleCustomerProtocolWapper.TripleRequestWrapper.Builder builder = TripleCustomerProtocolWapper.TripleRequestWrapper.Builder.newBuilder();
builder.setSerializeType(serialize);
for (String type : argumentsType) {
builder.addArgTypes(type);
}
for (Object argument : arguments) {
builder.addArgTypes(argument.getClass().getName());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
multipleSerialization.serialize(url, serialize, argument.getClass(), argument, bos);
builder.addArgs(bos.toByteArray());
Expand Down Expand Up @@ -436,8 +431,6 @@ public byte[] pack(Object obj) throws IOException {

private class WrapRequestUnpack implements UnPack {

private final Map<String, Class<?>> classCache = new ConcurrentHashMap<>();

private final MultipleSerialization serialization;
private final URL url;

Expand All @@ -458,32 +451,11 @@ public Object unpack(byte[] data) throws IOException, ClassNotFoundException {
for (int i = 0; i < wrapper.getArgs().size(); i++) {
ByteArrayInputStream bais = new ByteArrayInputStream(
wrapper.getArgs().get(i));
String className = wrapper.getArgTypes().get(i);
Class<?> clz = getClassFromCache(className, classCache, actualRequestTypes[i]);
ret[i] = serialization.deserialize(url, wrapper.getSerializeType(), clz, bais);
ret[i] = serialization.deserialize(url, wrapper.getSerializeType(),
actualRequestTypes[i],
bais);
}
return ret;
}


}


private static Class<?> getClassFromCache(String className, Map<String, Class<?>> classCache, Class<?> expectedClass) {
if (expectedClass.getName().equals(className)) {
return expectedClass;
}

Class<?> clz = classCache.get(className);
if (clz == null) {
try {
clz = ClassUtils.forName(className);
} catch (Exception e) {
// To catch IllegalStateException, LinkageError, ClassNotFoundException
clz = expectedClass;
}
classCache.put(className, clz);
}
return clz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionScope;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.common.utils.ClassUtils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -29,22 +28,8 @@
@SPI(scope = ExtensionScope.FRAMEWORK)
public interface MultipleSerialization {


default void serialize(URL url, String serializeType, String clzStr, Object obj, OutputStream os) throws IOException, ClassNotFoundException {
Class<?> clz = ClassUtils.forName(clzStr);
serialize(url, serializeType, clz, obj, os);
}


void serialize(URL url, String serializeType, Class<?> clz, Object obj, OutputStream os) throws IOException;


default Object deserialize(URL url, String serializeType, String clzStr, InputStream os) throws IOException, ClassNotFoundException {
Class<?> clz = ClassUtils.forName(clzStr);
return deserialize(url, serializeType, clz, os);
}


Object deserialize(URL url, String serializeType, Class<?> clz, InputStream os) throws IOException, ClassNotFoundException;

}