StateLog.java 6.4 KB
Newer Older
P
pjsousa@gmail.com 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/**
 * Copyright (c) 2007-2009 Alysson Bessani, Eduardo Alchieri, Paulo Sousa, and the authors indicated in the @author tags
 * 
 * This file is part of SMaRt.
 * 
 * SMaRt is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SMaRt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with SMaRt.  If not, see <http://www.gnu.org/licenses/>.
 */

package navigators.smart.statemanagment;

/**
 * This classes serves as a log for the state associated with the last checkpoint, and the message
 * batches received since the same checkpoint until the present. The state associated with the last
 * checkpoint together with all the batches of messages received so far, comprises this replica
 * current state
 * 
27
 * @author Jo�o Sousa
P
pjsousa@gmail.com 已提交
28 29 30 31
 */
public class StateLog {

    private byte[][] messageBatches; // batches received since the last checkpoint.
32
    private int lastEid; // Execution ID for the last checkpoint
P
pjsousa@gmail.com 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
    private byte[] state; // State associated with the last checkpoint
    private int k; // checkpoint period
    private int position; // next position in the array of batches to be written
    private int execCounter; // number of executions so far

    /**
     * Constructs a State log
     * @param k The chekpoint period
     */
    public StateLog(int k) {

        this.k = k;
        this.messageBatches = new byte[k][];
46
        this.lastEid = 0;
P
pjsousa@gmail.com 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        this.state = null;
        this.position = 0;
        this.execCounter = 0;
    }
    
    /**
     * Sets the state associated with the last checkpoint, and updates the execution ID associated with it
     * @param state State associated with the last checkpoint
     */
    public void newCheckpoint(byte[] state) {

        for (int i = 0; i < k; i++)
            messageBatches[i] = null;

        position = 0;
62
        lastEid += k;
63
        execCounter++;
P
pjsousa@gmail.com 已提交
64
        this.state = state;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
       /************************* TESTE *************************
        System.out.println("###################################");
        System.out.println("posicao: " + position);
        System.out.println("execucoes: " + execCounter);
        System.out.println("tamanho do array de bytes do estado: " + this.state.length);
        
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (this.state[i] & 0x000000FF) << shift;
        }
        System.out.println("Valor do estado: " + value);
        System.out.println("###################################");
        /************************* TESTE *************************/

P
pjsousa@gmail.com 已提交
80 81 82 83 84 85 86 87
    }

    /**
     * Retrieves the execution ID for the last checkpoint
     * @return Execution ID for the last checkpoint, or -1 if no checkpoint was yet executed
     */
    public int getCurrentCheckpointEid() {
        
88
        return lastEid - 1;
P
pjsousa@gmail.com 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    }

    /**
     * Retrieves the state associated with the last checkpoint
     * @return State associated with the last checkpoint
     */
    public byte[] getState() {
        return state;
    }

    /**
     * Adds a message batch to the log. This batches should be added to the log
     * in the same order in which they are delivered to the application. Only
     * the 'k' batches received after the last checkpoint are supposed to be kept
     * @param batch The batch of messages to be kept.
     * @return True if the batch was added to the log, false otherwise
     */
    public boolean addMessageBatch(byte[] batch) {

        if (position < k) {

            messageBatches[position] = batch;
111 112
            position++;
            execCounter++;
P
pjsousa@gmail.com 已提交
113

114
            /************************* TESTE *************************/
115 116 117
            System.out.println("posicao: " + position);
            System.out.println("execucoes: " + execCounter);
            /************************* TESTE *************************/
118
            
P
pjsousa@gmail.com 已提交
119 120 121 122 123 124 125 126 127 128 129 130
            return true;
        }

        return false;
    }

    /**
     * Returns a batch of messages, given its correspondent execution ID
     * @param eid Execution ID associated with the batch to be fetched
     * @return The batch of messages associated with the batch correspondent execution ID
     */
    public byte[] getMessageBatch(int eid) {
131 132
        if (eid >= lastEid && eid <= execCounter) {
            return messageBatches[eid - lastEid];
P
pjsousa@gmail.com 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        }
        else return null;
    }

    /**
     * Retrieves all the stored batches kept since the last checkpoint
     * @return All the stored batches kept since the last checkpoint
     */
    public byte[][] getMessageBatches() {
        return messageBatches;
    }

    /**
     * Constructs a TransferableState using this log information
     * @param eid Execution ID correspondent to desired state
148
     * @return TransferableState Object containing this log information
P
pjsousa@gmail.com 已提交
149 150 151
     */
    public TransferableState getTransferableState(int eid) {

152 153 154 155 156
        //System.out.println("A devolver o estado!");
        //System.out.println("EID pedido: " + eid);
        //System.out.println("Execucoes feitas ate agora: " + execCounter);
        //System.out.println("Ultimo checkpoint: " + lastEid);
        //System.exit(0);
P
pjsousa@gmail.com 已提交
157

158
        if (eid >= lastEid && eid <= execCounter) {
159
         
160 161
            byte[][] batches = new byte[eid - lastEid + 1][];

162
            for (int i = 0; i < (eid - lastEid + 1); i++)
P
pjsousa@gmail.com 已提交
163 164
                batches[i] = messageBatches[i];

165
            return new TransferableState(batches, lastEid, state);
P
pjsousa@gmail.com 已提交
166 167 168 169 170

        }
        else return null;
    }

171 172 173 174 175 176 177 178 179 180
    /**
     * Updates this log, according to the information contained in the TransferableState object
     * @param transState TransferableState object containing the information which is used to updated this log
     */
    public void update(TransferableState transState) {

        for (int i = 0; i < transState.getMessageBatches().length; i++, position = i) {
            this.messageBatches[i] = transState.getMessageBatches()[i];
        }

181
        this.lastEid = transState.getCurrentCheckpointEid() + 1;
182 183 184

        this.state = transState.getState();

185
        this.execCounter = this.lastEid + position;
186
    }
P
pjsousa@gmail.com 已提交
187
}