Skip to content

Commit

Permalink
Jackson serialization for multi classloader (#1438)
Browse files Browse the repository at this point in the history
* jackson serialization for multi classloader

* clear cache

---------

Co-authored-by: yuanyuan <[email protected]>
  • Loading branch information
yuanyuancin and yuanyuan2021 authored Aug 16, 2024
1 parent 2d8190f commit 46d1dcd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
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,30 @@ 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);
}

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

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

public void clearCache(ClassLoader classLoader) {
if (classLoader == null) {
return;
}
requestClassCache.remove(classLoader);
responseClassCache.remove(classLoader);
}

/**
Expand All @@ -73,14 +92,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 +141,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,7 @@ protected ObjectMapper getMapper() {
return this.mapper;
}

public void clearCache(ClassLoader classLoader) {
jacksonHelper.clearCache(classLoader);
}
}

0 comments on commit 46d1dcd

Please sign in to comment.