youxinren / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: 12819 #137

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Used kryo 2.21 version to serialize deserialize
2. Update to version 2.22 and deserialization fails

 I was using kryo version 2.21 to serialize / deserialize objects to db.
It worked perfectly until I decided to download the latest version 2.22. 
Deserialization fails with the following message:

com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: 
12819

methods to read/ write:

    public  MyObject readKryo(byte[] out)
            throws IOException {
                Kryo kryo = new Kryo();
        Input kryoIn = null;
        MyObject surveyObject = null;
        try {
            kryoIn = new Input(out);
         synchronized (this) {
             surveyObject = kryo.readObject(kryoIn, MyObject.class);
        }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return surveyObject;

    }

    public  ByteArrayOutputStream writeKryo(MyObject test,
            ) throws IOException {
        Output output = null;
                Kryo kryo = new Kryo();
        ByteArrayOutputStream out = null;
        try {
            out = new ByteArrayOutputStream();
            output = new Output(out);
            kryo.writeObject(output, test);
        } finally {
            if (output != null) {
                output.close();
            }
        }
        return out;
    }

Original issue reported on code.google.com by jlia...@gmail.com on 8 Oct 2013 at 8:08

GoogleCodeExporter commented 9 years ago
Would it be possible for you to provide a complete reproducible test-case? The 
code above does not include the MyObject class and the way how object is 
initialized before you serialize/deserialize it.

Original comment by romixlev on 16 Oct 2013 at 7:36

GoogleCodeExporter commented 9 years ago
The class MyObject has a List of ReviewNodes. An example which MyObject is 
initialized is the following:

List<ReviewNode> reviewNode = new ArrayList<ReviewNode>();
ReviewNode node = new ReviewNode();
node.setNumberofquestions("4");
reviewNode.add(node);

MyObject obj = new MyObject();
obj.setReviewNode(reviewNode);

public class MyObject implements java.io.Serializable{
public List<ReviewNode> reviewNode = new ArrayList<ReviewNode>();
public List<ReviewNode> getReviewNode() {
        return reviewNode;
    }
    public void setReviewNode(List<ReviewNode> reviewNode) {
        this.reviewNode = reviewNode;
    }

}

public class ReviewNode  implements java.io.Serializable{

    String name;
    String language;
    String type;
    String numberofquestions;
    String numberofanswers;
    String textfont;
    String pretextfont;
    String posttextfont;
    String answersfont;
    String backgroundcolor;
    String agentfont;
    String txtfont;
    String logoname;
    String logo;
    String agentcolor;
    String txtcolor;
    String  answerscolor;
    String posttextcolor;
    String pretextcolor;
    String textcolor;

    protected String oddcolor;
    protected String evencolor;
    String views;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getNumberofquestions() {
        return numberofquestions;
    }
    public void setNumberofquestions(String numberofquestions) {
        this.numberofquestions = numberofquestions;
    }
    public String getNumberofanswers() {
        return numberofanswers;
    }
    public void setNumberofanswers(String numberofanswers) {
        this.numberofanswers = numberofanswers;
    }
    public String getTextfont() {
        return textfont;
    }
    public void setTextfont(String textfont) {
        this.textfont = textfont;
    }
    public String getPretextfont() {
        return pretextfont;
    }
    public void setPretextfont(String pretextfont) {
        this.pretextfont = pretextfont;
    }
    public String getPosttextfont() {
        return posttextfont;
    }
    public void setPosttextfont(String posttextfont) {
        this.posttextfont = posttextfont;
    }
    public String getAnswersfont() {
        return answersfont;
    }
    public void setAnswersfont(String answersfont) {
        this.answersfont = answersfont;
    }
    public String getBackgroundcolor() {
        return backgroundcolor;
    }
    public void setBackgroundcolor(String backgroundcolor) {
        this.backgroundcolor = backgroundcolor;
    }
    public String getOddcolor() {
        return oddcolor;
    }
    public void setOddcolor(String oddcolor) {
        this.oddcolor = oddcolor;
    }
    public String getEvencolor() {
        return evencolor;
    }
    public void setEvencolor(String evencolor) {
        this.evencolor = evencolor;
    }
    public String getViews() {
        return views;
    }
    public void setViews(String views) {
        this.views = views;
    }
    public String getAgentfont() {
        return agentfont;
    }
    public void setAgentfont(String agentfont) {
        this.agentfont = agentfont;
    }

    public String getTxtfont() {
        return txtfont;
    }
    public void setTxtfont(String txtfont) {
        this.txtfont = txtfont;
    }
    public String getLogoname() {
        return logoname;
    }
    public void setLogoname(String logoname) {
        this.logoname = logoname;
    }
    public String getLogo() {
        return logo;
    }
    public void setLogo(String logo) {
        this.logo = logo;
    }
    public String getAgentcolor() {
        return agentcolor;
    }
    public void setAgentcolor(String agentcolor) {
        this.agentcolor = agentcolor;
    }
    public String getTxtcolor() {
        return txtcolor;
    }
    public void setTxtcolor(String txtcolor) {
        this.txtcolor = txtcolor;
    }
    public String getAnswerscolor() {
        return answerscolor;
    }
    public void setAnswerscolor(String answerscolor) {
        this.answerscolor = answerscolor;
    }
    public String getPosttextcolor() {
        return posttextcolor;
    }
    public void setPosttextcolor(String posttextcolor) {
        this.posttextcolor = posttextcolor;
    }
    public String getPretextcolor() {
        return pretextcolor;
    }
    public void setPretextcolor(String pretextcolor) {
        this.pretextcolor = pretextcolor;
    }
    public String getTextcolor() {
        return textcolor;
    }
    public void setTextcolor(String textcolor) {
        this.textcolor = textcolor;
    }

}

Original comment by jlia...@gmail.com on 16 Oct 2013 at 8:57

GoogleCodeExporter commented 9 years ago
Hi,
I tried to reproduce a problem by using your code, but I don't see any 
exceptions. Please provide a complete self-contained reproducible test-case.

The code I've written based on your comments is below:

package com.esotericsoftware.kryo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.minlog.Log;

public class Issue137Test extends KryoTestCase {

    public MyObject readKryo (byte[] out) throws IOException {
        Kryo kryo = new Kryo();
        Input kryoIn = null;
        MyObject surveyObject = null;
        try {
            kryoIn = new Input(out);
            synchronized (this) {
                surveyObject = kryo.readObject(kryoIn, MyObject.class);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return surveyObject;

    }

    public ByteArrayOutputStream writeKryo (MyObject test) throws IOException {
        Output output = null;
        Kryo kryo = new Kryo();
        ByteArrayOutputStream out = null;
        try {
            out = new ByteArrayOutputStream();
            output = new Output(out);
            kryo.writeObject(output, test);
        } finally {
            if (output != null) {
                output.close();
            }
        }
        return out;
    }

    static public class MyObject implements java.io.Serializable {
        public List<ReviewNode> reviewNode = new ArrayList<ReviewNode>();

        public List<ReviewNode> getReviewNode () {
            return reviewNode;
        }

        public void setReviewNode (List<ReviewNode> reviewNode) {
            this.reviewNode = reviewNode;
        }

    }

    static public class ReviewNode implements java.io.Serializable {

        String name;
        String language;
        String type;
        String numberofquestions;
        String numberofanswers;
        String textfont;
        String pretextfont;
        String posttextfont;
        String answersfont;
        String backgroundcolor;
        String agentfont;
        String txtfont;
        String logoname;
        String logo;
        String agentcolor;
        String txtcolor;
        String answerscolor;
        String posttextcolor;
        String pretextcolor;
        String textcolor;

        protected String oddcolor;
        protected String evencolor;
        String views;

        public String getName () {
            return name;
        }

        public void setName (String name) {
            this.name = name;
        }

        public String getLanguage () {
            return language;
        }

        public void setLanguage (String language) {
            this.language = language;
        }

        public String getType () {
            return type;
        }

        public void setType (String type) {
            this.type = type;
        }

        public String getNumberofquestions () {
            return numberofquestions;
        }

        public void setNumberofquestions (String numberofquestions) {
            this.numberofquestions = numberofquestions;
        }

        public String getNumberofanswers () {
            return numberofanswers;
        }

        public void setNumberofanswers (String numberofanswers) {
            this.numberofanswers = numberofanswers;
        }

        public String getTextfont () {
            return textfont;
        }

        public void setTextfont (String textfont) {
            this.textfont = textfont;
        }

        public String getPretextfont () {
            return pretextfont;
        }

        public void setPretextfont (String pretextfont) {
            this.pretextfont = pretextfont;
        }

        public String getPosttextfont () {
            return posttextfont;
        }

        public void setPosttextfont (String posttextfont) {
            this.posttextfont = posttextfont;
        }

        public String getAnswersfont () {
            return answersfont;
        }

        public void setAnswersfont (String answersfont) {
            this.answersfont = answersfont;
        }

        public String getBackgroundcolor () {
            return backgroundcolor;
        }

        public void setBackgroundcolor (String backgroundcolor) {
            this.backgroundcolor = backgroundcolor;
        }

        public String getOddcolor () {
            return oddcolor;
        }

        public void setOddcolor (String oddcolor) {
            this.oddcolor = oddcolor;
        }

        public String getEvencolor () {
            return evencolor;
        }

        public void setEvencolor (String evencolor) {
            this.evencolor = evencolor;
        }

        public String getViews () {
            return views;
        }

        public void setViews (String views) {
            this.views = views;
        }

        public String getAgentfont () {
            return agentfont;
        }

        public void setAgentfont (String agentfont) {
            this.agentfont = agentfont;
        }

        public String getTxtfont () {
            return txtfont;
        }

        public void setTxtfont (String txtfont) {
            this.txtfont = txtfont;
        }

        public String getLogoname () {
            return logoname;
        }

        public void setLogoname (String logoname) {
            this.logoname = logoname;
        }

        public String getLogo () {
            return logo;
        }

        public void setLogo (String logo) {
            this.logo = logo;
        }

        public String getAgentcolor () {
            return agentcolor;
        }

        public void setAgentcolor (String agentcolor) {
            this.agentcolor = agentcolor;
        }

        public String getTxtcolor () {
            return txtcolor;
        }

        public void setTxtcolor (String txtcolor) {
            this.txtcolor = txtcolor;
        }

        public String getAnswerscolor () {
            return answerscolor;
        }

        public void setAnswerscolor (String answerscolor) {
            this.answerscolor = answerscolor;
        }

        public String getPosttextcolor () {
            return posttextcolor;
        }

        public void setPosttextcolor (String posttextcolor) {
            this.posttextcolor = posttextcolor;
        }

        public String getPretextcolor () {
            return pretextcolor;
        }

        public void setPretextcolor (String pretextcolor) {
            this.pretextcolor = pretextcolor;
        }

        public String getTextcolor () {
            return textcolor;
        }

        public void setTextcolor (String textcolor) {
            this.textcolor = textcolor;
        }

    }

    public void testCase () throws Exception {
        List<ReviewNode> reviewNode = new ArrayList<ReviewNode>();
        ReviewNode node = new ReviewNode();
        node.setNumberofquestions("4");
        reviewNode.add(node);

        MyObject obj = new MyObject();
        obj.setReviewNode(reviewNode);

        ByteArrayOutputStream bos = writeKryo(obj);
        readKryo(bos.toByteArray());
    }
}

Original comment by romixlev on 17 Oct 2013 at 7:14

GoogleCodeExporter commented 9 years ago
The main difference is that I serialize the object with version 2.21, store it 
in db and then I am trying to desirialize it with the version 2.22.

Original comment by jlia...@gmail.com on 18 Oct 2013 at 7:05

GoogleCodeExporter commented 9 years ago
There were some changes and fixes in 2.22 which may make it 
backwards-incompatible with 2.21. Therefore it is not such a good idea to store 
using 2.21 and read using 2.22. 

Are you forced to use different versions of Kryo for serialization and 
deserialization? If not, please try to use the same version. If yes, try to the 
following:
- Read the 2.21 serialized objects using Kryo 2.21. You get your Java objects 
in-memory as a result.
- Now use 2.22 to serialize your Java objects and store it into your DB, i.e. 
you upgrade the serialized representation in the DB this way.

You may have problems running both versions of Kryo at the same time in the 
same application. To overcome this, I'd suggest using shade Maven plugin or 
something similar for one (or even both) of the Kryo versions. Using it you may 
rename the packages, which would eliminate conflicts arising from using two 
JARs with the same classes and packages.

Let me know, if this approach solves your problems.

Original comment by romixlev on 18 Oct 2013 at 7:26

GoogleCodeExporter commented 9 years ago
Thanks for your suggestion. I do not use both versions at the same time. The 
project was using kryo v1 for some time, so all serialized objects are in v1. I 
tried to replace the v1 with v2 and the error occured. I do not want to mix the 
versions so I suppose it is safer to use the previous version. 

I would try your suggestion if kryo v2 is faster than kryo v1. Is this true?

Original comment by jlia...@gmail.com on 18 Oct 2013 at 7:41

GoogleCodeExporter commented 9 years ago
Depending on what you do and what kind of data you serialize, Kryo 2.22 is much 
faster than 2.21 and previous versions. It can be even faster if you use Unsafe 
IO streams (please read the updated home page for more information). If you use 
a lot of primitive arrays in your objects (e.g. int[]), it may be an order of 
magnitude faster. Using Unsafe IO streams makes it often 2-3 times faster. But 
you really need to test, because it very much depends on your data.

Original comment by romixlev on 18 Oct 2013 at 7:47

GoogleCodeExporter commented 9 years ago
Thanks a lot for your help. I updated all the stored seriliazed objects to the 
current version. I also used the Unsafe stream to read/ write the object. It is 
faster indeed!

Original comment by jlia...@gmail.com on 18 Oct 2013 at 12:04

GoogleCodeExporter commented 9 years ago
Glad to hear that it helped. Don't forget that Unsafe-streams are CPU 
architecture dependent. So, make sure you serialize and deserialize on the same 
kind of machines.

OK, I close this issue, as it seems to be resolved now.

Original comment by romixlev on 18 Oct 2013 at 2:56