Creating ThreadLocal kryo instances results in exceptions during serialization

309 views
Skip to first unread message

jta...@ebay.com

unread,
Mar 30, 2016, 6:17:24 PM3/30/16
to kryo-users
Hello,

We are using Kryo 2.24.0. We want to create a Kryo instance per thread using ThreadLocal recommended in the github site, but it had lots of exceptions when serialization, Is ThreadLocal instance supported in 2.24.0, currently we can't upgrade to 3.0.x, because it is not back compatible with data serialized using 2.24.0.  The deserialization works fine without using ThreadLocal.  Thanks in advance for any help on this!

Here is the code:
private static final ThreadLocal<Kryo> kryos = new ThreadLocal<Kryo>() {
        protected Kryo initialValue() {
            
            return createSerializer();
        };
    };

private static Kryo createSerializer()
    {
        Kryo kryo = new Kryo();
        kryo.register(xxx.class);
        kryo.register(yyy.class);
        // more reigsters

        return kryo;
    }

public static Kryo getSerializer()
{
      return kryos.get();
}

Exceptions:
java.lang.ArrayIndexOutOfBoundsException: 113
        at com.esotericsoftware.kryo.util.IdentityObjectIntMap.put(IdentityObjectIntMap.java:81)
        at com.esotericsoftware.kryo.util.MapReferenceResolver.addWrittenObject(MapReferenceResolver.java:23)
        at com.esotericsoftware.kryo.Kryo.writeReferenceOrNull(Kryo.java:629)
        at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:500)
        
        
java.lang.ArrayIndexOutOfBoundsException: 274
        at com.esotericsoftware.kryo.util.IdentityObjectIntMap.get(IdentityObjectIntMap.java:247)
        at com.esotericsoftware.kryo.util.MapReferenceResolver.getWrittenId(MapReferenceResolver.java:28)
        at com.esotericsoftware.kryo.Kryo.writeReferenceOrNull(Kryo.java:619)
        at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:500)
        
        
com.esotericsoftware.kryo.KryoException: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Float
Serialization trace:
Map (com.ebay.traffic.targetengine.Utilties$DatedLCScoreMap)
        at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:82)
        at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:495)
        at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:505)
        



Martin Grotzke

unread,
Mar 30, 2016, 6:29:09 PM3/30/16
to kryo-users

Are you sharing anything related to kryo between threads? Can you show code that (de)serializes using kryo? Ideally you would provide a self contained test case that allows to reproduce the issue (e.g. build upon existing kryo tests).

Cheers,
Martin


--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jta...@ebay.com

unread,
Mar 30, 2016, 6:35:29 PM3/30/16
to kryo-users
Hey Martin,

Thanks for your reply. We don't share anything related to Kryo across thread.
This is the code we use to deserialize. The exception throws in writeObject call. Thanks for the help.

public static ByteBuffer JavaObjectToByteBuffer(Kryo kryo, Object javaObject, String javaClassName)
            throws Exception
    {
        if (javaObject == null)
            return null;
        
        Class<?> classToSerialize = Class.forName(javaClassName);
     
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Output output = new Output(stream);
        kryo.writeObject(output, javaObject);
        output.close();
        return ByteBuffer.wrap(stream.toByteArray());
    }

Martin Grotzke

unread,
Apr 1, 2016, 4:24:12 AM4/1/16
to kryo-users

Ok, I don't see anything wrong here. If you provide a self contained test case that allows to reproduce the issue (e.g. build upon existing kryo tests) I can have a look at it.

Cheers,
Martin

Reply all
Reply to author
Forward
0 new messages