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

Jackson serialization for multi classloader #1438

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Changes from 1 commit
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 @@ -18,6 +18,7 @@

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alipay.sofa.rpc.common.utils.ClassUtils;
Expand All @@ -27,6 +28,8 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import static com.alipay.sofa.rpc.common.utils.ClassLoaderUtils.getCurrentClassLoader;

/**
* @author <a href="mailto:[email protected]">zhiyuan.lzy</a>
*/
Expand All @@ -37,12 +40,12 @@ public class JacksonHelper {
/**
* Request service and method cache {service+method:class}
*/
private ConcurrentHashMap<String, JavaType[]> requestClassCache = new ConcurrentHashMap<String, JavaType[]>();
private ConcurrentHashMap<ClassLoader, Map<String, JavaType[]>> requestClassCache = new ConcurrentHashMap<>();

/**
* Response service and method cache {service+method:class}
*/
private ConcurrentHashMap<String, JavaType> responseClassCache = new ConcurrentHashMap<String, JavaType>();
private ConcurrentHashMap<ClassLoader, Map<String, JavaType>> responseClassCache = new ConcurrentHashMap<>();

/**
* Fetch request class for cache according service and method
Expand All @@ -54,14 +57,22 @@ public class JacksonHelper {
public JavaType[] getReqClass(String service, String methodName) {

String key = buildMethodKey(service, methodName);
Type[] reqClassList = requestClassCache.get(key);
Type[] reqClassList = getRequestCache().get(key);
if (reqClassList == null) {
//read interface and method from cache
String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service);
Class clazz = ClassUtils.forName(interfaceClass, true);
loadClassToCache(key, clazz, methodName);
}
return requestClassCache.get(key);
return getRequestCache().get(key);
}

public Map<String, JavaType[]> getRequestCache() {
return requestClassCache.computeIfAbsent(getCurrentClassLoader(), k -> new ConcurrentHashMap<>());
}

public Map<String, JavaType> getResponseCache() {
return responseClassCache.computeIfAbsent(getCurrentClassLoader(), k -> new ConcurrentHashMap<>());
}

/**
Expand All @@ -73,14 +84,14 @@ public JavaType[] getReqClass(String service, String methodName) {
*/
public JavaType getResClass(String service, String methodName) {
String key = service + "#" + methodName;
JavaType reqType = responseClassCache.get(key);
JavaType reqType = getResponseCache().get(key);
if (reqType == null) {
// 读取接口里的方法参数和返回值
String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service);
Class clazz = ClassUtils.forName(interfaceClass, true);
loadClassToCache(key, clazz, methodName);
}
return responseClassCache.get(key);
return getResponseCache().get(key);
}

/**
Expand Down Expand Up @@ -122,14 +133,14 @@ private void loadClassToCache(String key, Class clazz, String methodName) {
JavaType javaType = mapper.getTypeFactory().constructType(parameterTypes[i]);
javaTypes[i] = javaType;
}
requestClassCache.put(key, javaTypes);
getRequestCache().put(key, javaTypes);

// parse response types
Type resType = jsonMethod.getGenericReturnType();
if (resType == void.class) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_VOID_RETURN, "jackson", clazz.getName()));
}
JavaType resJavaType = mapper.getTypeFactory().constructType(resType);
responseClassCache.put(key, resJavaType);
getResponseCache().put(key, resJavaType);
}
}
Loading