Skip to content

Commit

Permalink
fix class init
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Sep 22, 2024
1 parent c0e96b0 commit 33df929
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ public void register(Class<?> cls, boolean createSerializer) {
}
buildMetaSharedClassInfo(Tuple2.of(classDef, null), classDef);
}
if (GraalvmSupport.isGraalBuildtime()) {
// Instance for generated class should be hold at graalvm runtime only.
getGraalvmClassRegistry().serializerClassMap.put(cls, classInfo.serializer.getClass());
classInfo.serializer = null;
}
}
}

Expand Down Expand Up @@ -1915,33 +1920,52 @@ public Fury getFury() {
return fury;
}

private static final ConcurrentMap<Integer, List<ClassResolver>> GRAALVM_REGISTRY =
private static final ConcurrentMap<Integer, GraalvmClassRegistry> GRAALVM_REGISTRY =
new ConcurrentHashMap<>();

// CHECKSTYLE.OFF:MethodName
public static void _addGraalvmClassRegistry(int furyConfigHash, ClassResolver classResolver) {
// CHECKSTYLE.ON:MethodName
if (GraalvmSupport.isGraalBuildtime()) {
List<ClassResolver> resolvers =
GRAALVM_REGISTRY.computeIfAbsent(
furyConfigHash, k -> Collections.synchronizedList(new ArrayList<>()));
resolvers.add(classResolver);
GraalvmClassRegistry registry =
GRAALVM_REGISTRY.computeIfAbsent(furyConfigHash, k -> new GraalvmClassRegistry());
registry.resolvers.add(classResolver);
}
}

private static class GraalvmClassRegistry {
private final List<ClassResolver> resolvers;
private final Map<Class<?>, Class<? extends Serializer>> serializerClassMap;

private GraalvmClassRegistry() {
resolvers = Collections.synchronizedList(new ArrayList<>());
serializerClassMap = new ConcurrentHashMap<>();
}
}

private GraalvmClassRegistry getGraalvmClassRegistry() {
return GRAALVM_REGISTRY.computeIfAbsent(
fury.getConfig().getConfigHash(), k -> new GraalvmClassRegistry());
}

private Class<? extends Serializer> getSerializerClassFromGraalvmRegistry(Class<?> cls) {
List<ClassResolver> classResolvers = GRAALVM_REGISTRY.get(fury.getConfig().getConfigHash());
GraalvmClassRegistry registry = getGraalvmClassRegistry();
List<ClassResolver> classResolvers = registry.resolvers;
if (classResolvers == null || classResolvers.isEmpty()) {
return null;
}
for (ClassResolver classResolver : classResolvers) {
if (classResolver != this) {
ClassInfo classInfo = classResolver.classInfoMap.get(cls);
if (classInfo != null) {
if (classInfo != null && classInfo.serializer != null) {
return classInfo.serializer.getClass();
}
}
}
Class<? extends Serializer> serializerClass = registry.serializerClassMap.get(cls);
if (serializerClass != null) {
return serializerClass;
}
if (GraalvmSupport.isGraalRuntime()) {
if (Functions.isLambda(cls) || ReflectionUtils.isJdkProxy(cls)) {
return null;
Expand All @@ -1953,7 +1977,8 @@ private Class<? extends Serializer> getSerializerClassFromGraalvmRegistry(Class<

private Class<? extends Serializer> getMetaSharedDeserializerClassFromGraalvmRegistry(
Class<?> cls, ClassDef classDef) {
List<ClassResolver> classResolvers = GRAALVM_REGISTRY.get(fury.getConfig().getConfigHash());
GraalvmClassRegistry registry = getGraalvmClassRegistry();
List<ClassResolver> classResolvers = registry.resolvers;
if (classResolvers == null || classResolvers.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,22 @@ Args=--initialize-at-build-time=org.apache.fury.memory.MemoryBuffer,\
com.google.common.cache.CacheBuilder$NullListener,\
com.google.common.cache.CacheBuilder$OneWeigher,\
com.google.common.base.Ticker$1,\
org.apache.fury.serializer.collection.FuryArrayAsListSerializer$ArrayAsList,\
org.apache.fury.serializer.collection.GuavaCollectionSerializers$GuavaCollectionSerializer,\
org.apache.fury.serializer.collection.GuavaCollectionSerializers$ImmutableSortedSetSerializer,\
org.apache.fury.serializer.collection.GuavaCollectionSerializers$GuavaMapSerializer,\
org.apache.fury.serializer.collection.GuavaCollectionSerializers$ImmutableMapSerializer,\
org.apache.fury.serializer.collection.GuavaCollectionSerializers$ImmutableBiMapSerializer,\
org.apache.fury.serializer.collection.GuavaCollectionSerializers$ImmutableSortedMapSerializer,\
org.apache.fury.serializer.collection.GuavaCollectionSerializers$1,\
org.apache.fury.reflect.FieldAccessor$GeneratedAccessor,\
org.apache.fury.serializer.ReplaceResolveSerializer$1,\
org.apache.fury.reflect.ReflectionUtils$1,\
org.apache.fury.serializer.JavaSerializer$1,\
org.apache.fury.serializer.JavaSerializer$2,\
org.apache.fury.serializer.JavaSerializer$3,\
org.apache.fury.serializer.JavaSerializer$4,\
org.apache.fury.serializer.NonexistentClassSerializers$ClassFieldsInfo,\
org.apache.fury.serializer.NonexistentClassSerializers$NonexistentEnumClassSerializer,\
org.apache.fury.resolver.ClassResolver.GraalvmClassRegistry,\
org.apache.fury.resolver.ClassChecker

0 comments on commit 33df929

Please sign in to comment.