使用Kryo反序列化包含一些不可反序列化的对象的数组(抢救可反序列化的部分)

时间:2014-01-06 11:42:13

标签: java arrays serialization kryo

背景

我试图以这样的方式编写Kryo反序列化:如果一个对象数组包含某些对象(由于代码更改)无法反序列化,那么数组中的那些引用将变为空而不是抛出异常;允许对象的其余部分进行抢救。我之前一直在使用Java的内置序列化,并且我已经能够通过在数组中的每个项目之间编写“已知良好”整数,然后在流中查找,如果发现错误以找到下一个对象。详细问题Deserializing an array that contains some non-deserializable objects (salvaging the deserializable parts)

由于效率原因,我现在已经转移到Kryo序列化,并尝试重新创建此方法,但是在Kryo中,此错误恢复似乎只能工作一次,但之后它无法正常恢复。

我尝试了什么

我尝试在序列化期间在END_OF_APPLE_MAGIC数组中的每个对象之间编写一个已知的良好整数(Apple)。在反序列化期间,如果发现BadApple无法反序列化,则会被ErrorApple替换(类比越来越弱),搜索END_OF_APPLE_MAGIC以查找下一个苹果的查找位置。如果数组中只有BadAppleBadApple不是第一个条目,则此方法有效。但是,如果数组中有多个BadApple或第一个AppleBadApple

,则会以各种方式失败(请参阅详细分析)
public class AppleHolder implements Serializable,KryoSerializable{
    static int END_OF_APPLE_MAGIC=1234467895; //if this just "turns up" in the stream we will have a bad day; however this is only the case in recovery mode, so is an acceptable risk

    int numberOfApples=6;
    Apple[] apples=new Apple[numberOfApples];
    double otherData=15;

    //these are just for debug
    int dividers=0; //counts number of times END_OF_APPLE_MAGIC is found
    int problems=0; //counts number of times an apple fails to load
    int badIntegers=0; //counts number of times END_OF_APPLE_MAGIC is looked for and a different Integer is found (I have never seen this happen)

    public AppleHolder(){
        Apple goodApple=new Apple("GoodApple","tastyGood");
        BadApple badApple=new BadApple("BadApple","untastyBad");

        apples[0]=goodApple;
        apples[1]=badApple;
        apples[2]=goodApple;
        apples[3]=goodApple; // multiple references to same object intentional
        apples[4]=goodApple;
        apples[5]=goodApple;


    }


    public void write (Kryo kryo, Output output) {
        for(int i=0;i<apples.length;i++){

            //kryo.writeObject(output, apples[i]);
            kryo.writeClassAndObject(output, apples[i]);
            kryo.writeClassAndObject(output, END_OF_APPLE_MAGIC);
        }

        kryo.writeObject(output,otherData);
    }


    public void read (Kryo kryo, Input input) {
        try{

            apples =new Apple[numberOfApples];
            for(int i=0;i<apples.length;i++){

                try{
                    Object ob=kryo.readClassAndObject(input);
                    apples[i]=(Apple)ob;
                }catch(Exception e){
                    apples[i]=new ErrorApple();
                    problems++;
                }

                //Search for next Apple Boundary (except in recovery mode 
                //it will be the next entry)
                boolean atBoundary=false;
                while (atBoundary==false){ //should probably put a limit on this just in case
                    try{
                        int appleMagic =(Integer)kryo.readClassAndObject(input);
                        if (appleMagic == END_OF_APPLE_MAGIC){
                            atBoundary=true;
                            dividers++;
                        }else{
                            badIntegers++;
                        }
                    }catch(Exception e){
                        //painful byte reading mode only entered in recovery mode; under good 
                        //situations it does not represent an efficiency problem
                        input.skip(1); //consume byte of bad input
                        //Where buffer underflow exceptions occur they occur here
                    }
                }


            }
            otherData = kryo.readObject(input, Double.class);

        }catch(Exception e){
            //something when wrong (always a Buffer underflow so far), print what we have


            for(int i=0;i<apples.length;i++){
                System.out.println(apples[i]);

            }

            throw e;
        }

    }


    public static void main(String[] args)
            throws Exception {

        /*
         * (1) First run serialize()
         * (2) Rename/delete badApple such that it cannot be found for deserialization
         * (3) Run deSerialize(()
         */


        serialize();


        //deSerialize();

    }

    public static void serialize() throws Exception{
        AppleHolder testWrite = new AppleHolder();
        /*FileOutputStream fos = new FileOutputStream("testfile");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(testWrite);
        oos.flush();
        oos.close();
        */

        Kryo kryo = new Kryo();
        Output output = new Output(new FileOutputStream("testfile"));
        kryo.writeObject(output, testWrite);
        output.close();

    }

    public static void deSerialize() throws Exception{
        /*AppleHolder testRead;
        FileInputStream fis = new FileInputStream("testfile");
        ObjectInputStream ois = new ObjectInputStream(fis);
        testRead = (AppleHolder) ois.readObject();
        ois.close();
        */

        Kryo kryo = new Kryo();
        Input input = new Input(new FileInputStream("testfile"));
        AppleHolder testRead = kryo.readObject(input, AppleHolder.class);
        input.close();

        for(int i=0;i<testRead.apples.length;i++){
            System.out.println(testRead.apples[i]);

        }

        System.out.println("otherData: " + testRead.otherData);

    }

}

public class Apple implements Serializable {
    private String propertyOne;
    private String propertyTwo;

    public Apple(){}

    public Apple(String propertyOne, String propertyTwo) {
        this.propertyOne = propertyOne;
        this.propertyTwo = propertyTwo;
        validate();
    }

    private void writeObject(ObjectOutputStream o)
            throws IOException {

        o.writeObject(propertyOne);
        o.writeObject(propertyTwo);
    }

    private void readObject(ObjectInputStream o)
            throws IOException, ClassNotFoundException {

        propertyOne = (String) o.readObject();
        propertyTwo = (String) o.readObject();
        validate();
    }

    private void validate(){
        if(propertyOne == null ||
                propertyOne.length() == 0 ||
                propertyTwo == null ||
                propertyTwo.length() == 0){

            throw new IllegalArgumentException();
        }
    }

    public String getPropertyOne() {
        return propertyOne;
    }

    public String getPropertyTwo() {
        return propertyTwo;
    }

    @Override
    public String toString() {
        return "goodApple";
    }



}

public class BadApple extends Apple {


    public BadApple(){}

    public BadApple(String propertyOne, String propertyTwo) {
        super(propertyOne, propertyTwo);
    }

    @Override
    public String toString() {
        return "badApple";
    }
}

public class ErrorApple extends Apple {


    public ErrorApple(){}

    public ErrorApple(String propertyOne, String propertyTwo) {
        super(propertyOne, propertyTwo);
    }

    @Override
    public String toString() {
        return "errorApple";
    }

}

问题

我如何打捞Kyro序列化数组,其中只有一些对象可以反序列化?从而获得具有ErrorApple条目的数组,用于不可反序列化的部分。在我的数组中,我在一个数组中有几个对同一个对象的引用,在反序列化过程中保留它是至关重要的。

所以进入序列化我有

[GoodApple]
[GoodApple]  
[GoodApple]  
[BadApple]
[BadApple]
[GoodApple] 

我想要反序列化(因为badApple已经改变,无法反序列化

[GoodApple]
[GoodApple]  
[GoodApple]  
[ErrorApple]
[ErrorApple]
[GoodApple]  

我希望这能提供一个后备,在这种情况下无法实现向后兼容,或者删除了之前安装的程序的第三方修改

详细分析

本节概述了现有程序失败的方式。

一般

  • 除阵列中第一个位置以外的某个BadApple将正常运行
  • 数组中第一个位置的BadApple将导致下一个Apple读数正确,然后ErrorApples从那时开始(即使是好Apple s)
  • 如果第二个BadApple之后的第一个商品Apple有多个BadApple正确读取但可能会在数组中向前移动,然后从那时起ErrorApples(即使是好Apple s)。将有一个KryoException: Buffer underflow,并且数组末尾可能有空条目。

我使用的输入和输出如下所示:

    In          Out
    [goodApple] [goodApple]  
    [goodApple] [goodApple]  
    [badApple]  [badApple]  
    [goodApple] [goodApple]  
    [goodApple] [goodApple]  
    [goodApple] [goodApple]  

    In          Out
    [badApple]  [errorApple]  
    [goodApple] [goodApple]  
    [goodApple] [errorApple]  
    [goodApple] [errorApple]  
    [goodApple] [errorApple]  
    [goodApple] [errorApple]  

    In          Out
    [goodApple] [goodApple]  
    [badApple]  [errorApple]  
    [goodApple] [goodApple]  
    [badApple]  [errorApple]  
    [goodApple] [goodApple]  
    [goodApple] [errorApple]  
    KryoException: Buffer underflow. (occures at input.skip(1);)  

    In          Out
    [goodApple] [goodApple]  
    [goodApple] [goodApple]  
    [badApple]  [errorApple]  
    [badApple]  [errorApple]  
    [goodApple] [goodApple]  
    [goodApple] [errorApple]  
    KryoException: Buffer underflow (occures at input.skip(1);)  

    In          Out
    [goodApple] [goodApple]  
    [badApple]  [errorApple]  
    [badApple]  [errorApple]  
    [badApple]  [goodApple]  
    [goodApple] [errorApple]  
    [goodApple] [null]  
    KryoException: Buffer underflow. (occures at input.skip(1);)  

1 个答案:

答案 0 :(得分:1)

我发现的解决方案是编写第二个恢复序列化文件,该文件只在每个项目添加到文件后保留文件长度。然后,如果反序列化数据存在问题,程序就知道它可以找到“下一个好”值的位置。

然而有一个问题,一旦你读了一个字节,你就无法重新读取它而不重新开始(.reset()抛出一个UnsupportedOperationException),有时Kryo开始读入下一个好的对象在它意识到它在一个坏对象上窒息之前。我的解决方案是使用单独文件中的数据来检测每个对象要读取的字节数,将它们作为字节读取,然后将其传递给Kryo进行反序列化。

所有这些都有一些开销,但对于我的测试,整个过程仍然比标准的Java反序列化快得多,它只能用于“恢复模式”。

以下程序证明了这一点,它将坏值留空(但可以做任何想做的事情)。

public class AppleHolder implements Serializable,KryoSerializable{
    int numberOfApples=10;
    Apple[] apples=new Apple[numberOfApples];
    double otherData=15;


    public AppleHolder(){

        BadApple sharedBad=new BadApple(0);

        for(int i=0;i<numberOfApples;i++){
            if (i>3 && i<=6){
                apples[i]=new Apple(i);
            }else{
                apples[i]=new BadApple();
            }
        }

    }


    public void write (Kryo kryo, Output output) {
        int[] recoveryTrack=new int[apples.length+1]; //last one for after the last entry

        for(int i=0;i<apples.length;i++){
            recoveryTrack[i]=output.total();
            kryo.writeClassAndObject(output, apples[i]);
        }
        recoveryTrack[recoveryTrack.length-1]=output.total();

        kryo.writeObject(output,otherData);

        Output outputRecovery;
        try {
            outputRecovery = new Output(new FileOutputStream("testfile.recovery"));
            kryo.writeObject(outputRecovery, recoveryTrack);
            outputRecovery.close();
        } catch (FileNotFoundException ex) {
            //I guess hopefully we won't need the recovery track
            Logger.getLogger(AppleHolder.class.getName()).log(Level.SEVERE, null, ex);
        }


    }


    public void read (Kryo kryo, Input input) {



        int[] readRecoveryTrack=null;
        try {
            Kryo kryoRecovery = new Kryo();
            Input inputRecovery = new Input(new FileInputStream("testfile.recovery"));
            readRecoveryTrack =kryoRecovery.readObject(inputRecovery, int[].class);
            inputRecovery.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AppleHolder.class.getName()).log(Level.SEVERE, null, ex);
        }





        apples=new Apple[numberOfApples];

        for(int j=0;j<apples.length;j++){

            int actualPos=input.total();
            int desiredPos=readRecoveryTrack[j];
            int desiredBytes=readRecoveryTrack[j+1]-readRecoveryTrack[j];

            byte[] bytes=input.readBytes(desiredBytes);

            ByteArrayInputStream byteStream =new  ByteArrayInputStream(bytes);

            try{
                apples[j]=(Apple)kryo.readClassAndObject(new Input(byteStream));
            }catch(Exception e){
                //don't care, leave null
            }




        }

    }

    public static void main(String[] args)
            throws Exception {

        /*
         * (1) First run serialize()
         * (2) Rename/delete badApple such that it cannot be found for deserialization
         * (3) Run deSerialize(()
         */


        serialize();


        //deSerialize();

    }

    public static void serialize() throws Exception{
        AppleHolder testWrite = new AppleHolder();


        Kryo kryo = new Kryo();
        Output output = new Output(new FileOutputStream("testfile"));
        kryo.writeObject(output, testWrite);
        output.close();
    }

    public static void deSerialize() throws Exception{

        Kryo kryo = new Kryo();
        Input input = new Input(new FileInputStream("testfile"));
        AppleHolder testRead = kryo.readObject(input, AppleHolder.class);
        input.close();


        for(int i=0;i<testRead.apples.length;i++){
            System.out.println(testRead.apples[i]);

        }

        System.out.println(testRead.otherData);

    }
}

public class Apple implements Serializable {
    protected int index;

    public Apple(){}

    public Apple(int index) {
        this.index = index;
    }


    @Override
    public String toString() {
        return "goodApple " + index;
    }



}

public class BadApple extends Apple {

    private static final long serialVersionUID = 7;

    public BadApple(){}

    public BadApple(int index){
        super(index);
    }


    @Override
    public String toString() {
        return "badApple " + index;
    }
}