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

Serializing and Deserializing $$Lambda$ Anonymous Classes #714

Closed
bmistry13 opened this issue Mar 31, 2020 · 2 comments
Closed

Serializing and Deserializing $$Lambda$ Anonymous Classes #714

bmistry13 opened this issue Mar 31, 2020 · 2 comments

Comments

@bmistry13
Copy link

bmistry13 commented Mar 31, 2020

Hi All,

I am trying to Serialize/Deserialise JGraphT Object SimpleDirectedGraph into Hazelcast via SubZero Library and it has Supplier which of Lamda type DefaultEdge.class.

    protected org.jgrapht.Graph<Node, DefaultEdge> graph = new SimpleDirectedGraph<Node, DefaultEdge>(null,
            (Supplier<DefaultEdge> & **Serializable**) () ->new DefaultEdge(),false);

The serialization is happening but serialization has issue with Anonymous class not found.

Caused by: com.esotericsoftware.kryo.KryoException: Unable to find class: x.x.x.x.x$$Lambda$1306/0x0000000800ea5840
Serialization trace:
edgeSupplier (org.jgrapht.graph.SimpleDirectedGraph)
graph (x.x.x.x.x)
	at com.esotericsoftware.kryo.util.DefaultClassResolver.readName(DefaultClassResolver.java:160)
	at com.esotericsoftware.kryo.util.DefaultClassResolver.readClass(DefaultClassResolver.java:133)
Caused by: java.lang.ClassNotFoundException: com.versa.impl.graph.AbstractGraph$$Lambda$1306/0x0000000800ea5840
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:398)
	at com.esotericsoftware.kryo.util.DefaultClassResolver.readName(DefaultClassResolver.java:154)
	... 72 common frames omitted

Can you please suggest how I can overcome this issue? I tried kryo.getFieldSerializerConfig().setOptimizedGenerics(false); but it did not work for me. Any help is greatly appreciated.

I have looked at a similar issue here: #469 where the recommendation is to use Serializable but it still did not work.

For reference, here is the internal structure of Graph :
https://github.com/jgrapht/jgrapht/blob/master/jgrapht-core/src/main/java/org/jgrapht/graph/AbstractBaseGraph.java#L74

Thanks,

Bhavesh

@bmistry13
Copy link
Author

bmistry13 commented Apr 1, 2020

Hi All,

I was using SubZero and by default, it won't register the following serializers:

    kryo.register(SerializedLambda.class);
    kryo.register(Closure.class, new ClosureSerializer()); 

You must add your own Serilizable:


clientConfig = SubZero.useAsGlobalSerializer(clientConfig, MyGlobalConfig.class);

public class MyGlobalConfig extends AbstractGlobalUserSerializer {
    public MyGlobalConfig() {
        super(UserSerializerConfig.delegate(new KryoConfigurer() {
            @Override
           public void configure(Kryo kryo) {
                kryo.register( Arrays.asList( "" ).getClass(), new ArraysAsListSerializer() );
                kryo.register( Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer() );
                kryo.register( Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer() );
                kryo.register( Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer() );
                kryo.register( Collections.singletonList( "" ).getClass(), new CollectionsSingletonListSerializer() );
                kryo.register( Collections.singleton( "" ).getClass(), new CollectionsSingletonSetSerializer() );
                kryo.register( Collections.singletonMap( "", "" ).getClass(), new CollectionsSingletonMapSerializer() );
                kryo.register( GregorianCalendar.class, new GregorianCalendarSerializer() );
                kryo.register( InvocationHandler.class, new JdkProxySerializer() );
                UnmodifiableCollectionsSerializer.registerSerializers( kryo );
                SynchronizedCollectionsSerializer.registerSerializers( kryo );
// custom serializers for non-jdk libs

// register CGLibProxySerializer, works in combination with the appropriate action in handleUnregisteredClass (see below)
                kryo.register( CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer( ) );
// guava ImmutableList, ImmutableSet, ImmutableMap, ImmutableMultimap, ImmutableTable, ReverseList, UnmodifiableNavigableSet
                ImmutableListSerializer.registerSerializers( kryo );
                ImmutableSetSerializer.registerSerializers( kryo );
                ImmutableMapSerializer.registerSerializers( kryo );
                ImmutableMultimapSerializer.registerSerializers( kryo );
                ImmutableTableSerializer.registerSerializers( kryo );
                ReverseListSerializer.registerSerializers( kryo );
                UnmodifiableNavigableSetSerializer.registerSerializers( kryo );
// guava ArrayListMultimap, HashMultimap, LinkedHashMultimap, LinkedListMultimap, TreeMultimap, ArrayTable, HashBasedTable, TreeBasedTable
                ArrayListMultimapSerializer.registerSerializers( kryo );
                HashMultimapSerializer.registerSerializers( kryo );
                LinkedHashMultimapSerializer.registerSerializers( kryo );
                LinkedListMultimapSerializer.registerSerializers( kryo );
                TreeMultimapSerializer.registerSerializers( kryo );
                ArrayTableSerializer.registerSerializers( kryo );
                HashBasedTableSerializer.registerSerializers( kryo );
                TreeBasedTableSerializer.registerSerializers( kryo );
                kryo.register(FolderEntity.class);
                kryo.register(AllowedType.class);
                kryo.register(EntityGraph.class);
                **kryo.register(SerializedLambda.class);**
               ** kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer());**
            }
        }));
    }

I need bunch of custom sterilizer so I had to add :

            <dependency>
                <groupId>de.javakaffee</groupId>
                <artifactId>kryo-serializers</artifactId>
                <version>0.45</version>
            </dependency>

I hope this helps someone.

Thanks,
Bhavesh

@dmestetskiy
Copy link

dmestetskiy commented Dec 11, 2020

@bmistry13 thank you!!! This fixed our issue. We migrated from scala 2.11 to 2.12, kryo 4.0.2 and kept having serialization issues with lambdas. Adding these two lines fixed it

kryo.register(classOf[SerializedLambda]);
kryo.register(classOf[Closure], new ClosureSerializer());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants