Fixed - OOM arise during RLiveObjectService.persist() method invocation. #2938

pull/2954/head
Nikita Koksharov 5 years ago
parent a81041f25d
commit 18897a1109

@ -694,9 +694,7 @@ public class RedissonLiveObjectService implements RLiveObjectService {
}
private String getRIdFieldName(Class<?> cls) {
return Introspectior.getFieldsWithAnnotation(cls, RId.class)
.getOnly()
.getName();
return Introspectior.getREntityIdFieldName(cls);
}
private <T> T instantiateLiveObject(Class<T> proxyClass, Object id) {

@ -229,6 +229,7 @@ public class CommandBatchService extends CommandAsyncService {
&& this.options.getSyncSlaves() == 0) {
voidPromise.onComplete((res, e) -> {
executed.set(true);
commands.clear();
nestedServices.clear();
promise.trySuccess(new BatchResult<>(Collections.emptyList(), 0));
});
@ -242,6 +243,7 @@ public class CommandBatchService extends CommandAsyncService {
e.getCommands().forEach(t -> t.tryFailure(ex));
}
commands.clear();
nestedServices.clear();
return;
}
@ -276,7 +278,8 @@ public class CommandBatchService extends CommandAsyncService {
BatchResult<Object> result = new BatchResult<Object>(responses, syncedSlaves);
promise.trySuccess(result);
commands.clear();
nestedServices.clear();
});
}

@ -76,7 +76,7 @@ public class AccessorInterceptor {
return null;
}
String fieldName = getFieldName(method);
String fieldName = getFieldName(me.getClass().getSuperclass(), method);
Field field = ClassUtils.getDeclaredField(me.getClass().getSuperclass(), fieldName);
Class<?> fieldType = field.getType();
@ -236,13 +236,20 @@ public class AccessorInterceptor {
}
}
private String getFieldName(Method method) {
private String getFieldName(Class<?> clazz, Method method) {
String name = method.getName();
int i = 4;
if (name.startsWith("is")) {
i = 3;
}
return name.substring(i - 1, i).toLowerCase() + name.substring(i);
try {
String fieldName = name.substring(i - 1, i).toLowerCase() + name.substring(i);
ClassUtils.getDeclaredField(clazz, fieldName);
return fieldName;
} catch (NoSuchFieldException e) {
return name.substring(i - 1);
}
}
private boolean isGetter(Method method, String fieldName) {
@ -259,10 +266,7 @@ public class AccessorInterceptor {
}
private static String getREntityIdFieldName(Object o) {
return Introspectior
.getFieldsWithAnnotation(o.getClass().getSuperclass(), RId.class)
.getOnly()
.getName();
return Introspectior.getREntityIdFieldName(o.getClass().getSuperclass());
}
}

@ -108,18 +108,31 @@ public class ClassUtils {
}
}
private static final Object NO_FIELD = new Object();
private static final Map<String, Object> FIELD_CACHE = new LRUCacheMap<>(1000, 0, 0);
public static Field getDeclaredField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
for (Class<?> c : getClassHierarchy(clazz)) {
for (Field field : c.getDeclaredFields()) {
if (field.getName().equals(fieldName)) {
return field;
Object field = FIELD_CACHE.get(clazz.getName() + ":" + fieldName);
if (field == null) {
for (Class<?> c : getClassHierarchy(clazz)) {
for (Field f : c.getDeclaredFields()) {
if (f.getName().equals(fieldName)) {
FIELD_CACHE.put(clazz.getName() + ":" + fieldName, f);
return f;
}
}
}
}
if (field instanceof Field) {
return (Field) field;
}
if (field == null) {
FIELD_CACHE.put(clazz.getName() + ":" + fieldName, NO_FIELD);
}
throw new NoSuchFieldException("No such field: " + fieldName);
}
private static final Map<Class<?>, Boolean> ANNOTATED_CLASSES = new LRUCacheMap<Class<?>, Boolean>(500, 0, 0);
private static final Map<Class<?>, Boolean> ANNOTATED_CLASSES = new LRUCacheMap<>(500, 0, 0);
public static boolean isAnnotationPresent(Class<?> clazz, Class<? extends Annotation> annotation) {
if (clazz.getName().startsWith("java.")) {

@ -20,12 +20,14 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatchers;
import org.redisson.api.annotation.RId;
import org.redisson.cache.LRUCacheMap;
/**
*
@ -37,32 +39,6 @@ public class Introspectior {
return new TypeDescription.ForLoadedType(c);
}
public static MethodDescription getMethodDescription(Class<?> c, String method) {
if (method == null || method.isEmpty()) {
return null;
}
return getTypeDescription(c)
.getDeclaredMethods()
.filter(ElementMatchers.hasMethodName(method))
.getOnly();
}
public static FieldList<FieldDescription.InDefinedShape> getFieldsDescription(Class<?> c) {
return getTypeDescription(c)
.getDeclaredFields();
}
public static FieldDescription getFieldDescription(Class<?> c, String field) {
if (field == null || field.isEmpty()) {
return null;
}
return getTypeDescription(c)
.getDeclaredFields()
.filter(ElementMatchers.named(field))
.getOnly();
}
public static FieldList<FieldDescription.InDefinedShape> getFieldsWithAnnotation(Class<?> c, Class<? extends Annotation> a) {
return getAllFields(c)
.filter(ElementMatchers.isAnnotatedWith(a));
@ -75,4 +51,20 @@ public class Introspectior {
}
return new FieldList.ForLoadedFields(fields);
}
private static final Map<Class<?>, String> ID_FIELD_NAME_CACHE = new LRUCacheMap<>(500, 0, 0);
public static String getREntityIdFieldName(Class<?> cls) {
String name = ID_FIELD_NAME_CACHE.get(cls);
if (name == null) {
name = Introspectior
.getFieldsWithAnnotation(cls, RId.class)
.getOnly()
.getName();
ID_FIELD_NAME_CACHE.put(cls, name);
}
return name;
}
}

Loading…
Cancel
Save