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

Serialization and deserialization use classes in the wrapper #11302

Merged
merged 9 commits into from
Jan 17, 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 @@ -113,6 +113,7 @@ public class ClassUtils {
PRIMITIVE_WRAPPER_TYPE_MAP.put(Integer.class, int.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Long.class, long.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Short.class, short.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Void.class, void.class);

Set<Class<?>> primitiveTypeNames = new HashSet<>(32);
primitiveTypeNames.addAll(PRIMITIVE_WRAPPER_TYPE_MAP.values());
Expand Down Expand Up @@ -163,7 +164,7 @@ public static ClassLoader getClassLoader(Class<?> clazz) {
if (cl == null) {
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (Throwable ex) {
} catch (Exception ignored) {
// Cannot access thread context ClassLoader - falling back to system class loader...
}
if (cl == null) {
Expand All @@ -173,7 +174,7 @@ public static ClassLoader getClassLoader(Class<?> clazz) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
} catch (Throwable ex) {
} catch (Exception ignored) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
Expand Down Expand Up @@ -475,7 +476,7 @@ public static boolean isAssignableFrom(Class<?> superType, Class<?> targetType)
public static boolean isPresent(String className, ClassLoader classLoader) {
try {
forName(className, classLoader);
} catch (Throwable ignored) { // Ignored
} catch (Exception ignored) { // Ignored
return false;
}
return true;
Expand All @@ -493,7 +494,7 @@ public static Class<?> resolveClass(String className, ClassLoader classLoader) {
Class<?> targetClass = null;
try {
targetClass = forName(className, classLoader);
} catch (Throwable ignored) { // Ignored
} catch (Exception ignored) { // Ignored
}
return targetClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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 @@ -33,7 +34,8 @@
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Iterator;
import java.util.stream.Stream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

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

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

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

Expand All @@ -387,10 +392,8 @@ 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 @@ -433,6 +436,8 @@ 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 @@ -453,11 +458,27 @@ 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));
ret[i] = serialization.deserialize(url, wrapper.getSerializeType(),
actualRequestTypes[i],
bais);
String className = wrapper.getArgTypes().get(i);
Class<?> clz = getClassFromCache(className, classCache, actualRequestTypes[i]);
ret[i] = serialization.deserialize(url, wrapper.getSerializeType(), clz, bais);
}
return ret;
}


}


private static Class<?> getClassFromCache(String className, Map<String, Class<?>> classCache, Class<?> expectedClass) {
Class<?> clz = classCache.get(className);
if (clz == null) {
try {
clz = ClassUtils.forName(className);
} catch (ClassNotFoundException e) {
clz = expectedClass;
}
classCache.put(className, clz);
}
return clz;
}
}