StateLog.java 8.3 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
 */
public class StateLog {

31
    private BatchInfo[] messageBatches; // batches received since the last checkpoint.
32
    private int lastCheckpointEid; // Execution ID for the last checkpoint
33 34
    private int lastCheckpointRound; // Decision round for the last checkpoint
    private int lastCheckpointLeader; // Leader for the last checkpoint
P
pjsousa@gmail.com 已提交
35
    private byte[] state; // State associated with the last checkpoint
36
    private byte[] stateHash; // Hash of the state associated with the last checkpoint
P
pjsousa@gmail.com 已提交
37
    private int position; // next position in the array of batches to be written
38
    private int lastEid; // Execution ID for the last messages batch delivered to the application
P
pjsousa@gmail.com 已提交
39 40 41 42 43 44 45

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

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

62
        for (int i = 0; i < this.messageBatches.length; i++)
P
pjsousa@gmail.com 已提交
63 64 65 66
            messageBatches[i] = null;

        position = 0;
        this.state = state;
67
        this.stateHash = stateHash;
68

P
pjsousa@gmail.com 已提交
69 70 71
    }

    /**
72 73
     * Sets the execution ID for the last checkpoint
     * @param lastCheckpointEid Execution ID for the last checkpoint
P
pjsousa@gmail.com 已提交
74
     */
75 76 77 78 79 80
    public void setLastCheckpointEid(int lastCheckpointEid) {

        this.lastCheckpointEid = lastCheckpointEid;
    }

    /**
81 82
     * Retrieves the execution ID for the last checkpoint
     * @return Execution ID for the last checkpoint, or -1 if none was obtained
83 84
     */
    public int getLastCheckpointEid() {
P
pjsousa@gmail.com 已提交
85
        
86 87 88
        return lastCheckpointEid ;
    }

89 90
    /**
     * Sets the decision round for the last checkpoint
91
     * @param lastCheckpointRound Decision round for the last checkpoint
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
     */
    public void setLastCheckpointRound(int lastCheckpointRound) {

        this.lastCheckpointRound = lastCheckpointRound;
    }

    /**
     * Retrieves the decision round for the last checkpoint
     * @return Decision round for the last checkpoint, or -1 if none was obtained
     */
    public int getLastCheckpointRound() {

        return lastCheckpointRound ;
    }

    /**
     * Sets the leader for the last checkpoint
109
     * @param lastCheckpointLeader Leader for the last checkpoint
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
     */
    public void setLastCheckpointLeader(int lastCheckpointLeader) {

        this.lastCheckpointLeader = lastCheckpointLeader;
    }

    /**
     * Retrieves the leader for the last checkpoint
     * @return Leader for the last checkpoint, or -1 if none was obtained
     */
    public int getLastCheckpointLeader() {

        return lastCheckpointLeader;
    }

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    /**
     * Sets the execution ID for the last messages batch delivered to the application
     * @param lastEid the execution ID for the last messages batch delivered to the application
     */
    public void setLastEid(int lastEid) {

        this.lastEid = lastEid;
    }

    /**
     * Retrieves the execution ID for the last messages batch delivered to the application
     * @return Execution ID for the last messages batch delivered to the application
     */
    public int getLastEid() {

        return lastEid;
P
pjsousa@gmail.com 已提交
141 142 143 144 145 146 147 148 149 150
    }

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

151 152 153 154 155 156 157 158
    /**
     * Retrieves the hash of the state associated with the last checkpoint
     * @return Hash of the state associated with the last checkpoint
     */
    public byte[] getStateHash() {
        return stateHash;
    }

P
pjsousa@gmail.com 已提交
159 160 161 162 163 164
    /**
     * 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.
     */
165
    public void addMessageBatch(byte[] batch, int round, int leader) {
P
pjsousa@gmail.com 已提交
166

167
        if (position < messageBatches.length) {
P
pjsousa@gmail.com 已提交
168

169
            messageBatches[position] = new BatchInfo(batch, round, leader);
170
            position++;
171
        }
P
pjsousa@gmail.com 已提交
172 173 174 175 176 177 178
    }

    /**
     * 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
     */
179
    public BatchInfo getMessageBatch(int eid) {
180 181
        if (eid > lastCheckpointEid && eid <= lastEid) {
            return messageBatches[eid - lastCheckpointEid - 1];
P
pjsousa@gmail.com 已提交
182 183 184 185 186 187 188 189
        }
        else return null;
    }

    /**
     * Retrieves all the stored batches kept since the last checkpoint
     * @return All the stored batches kept since the last checkpoint
     */
190
    public BatchInfo[] getMessageBatches() {
P
pjsousa@gmail.com 已提交
191 192 193
        return messageBatches;
    }

194 195 196 197 198 199 200
    /**
     * Retrieves the total number of stored batches kept since the last checkpoint
     * @return The total number of stored batches kept since the last checkpoint
     */
    public int getNumBatches() {
        return position;
    }
P
pjsousa@gmail.com 已提交
201 202 203
    /**
     * Constructs a TransferableState using this log information
     * @param eid Execution ID correspondent to desired state
204
     * @return TransferableState Object containing this log information
P
pjsousa@gmail.com 已提交
205
     */
206
    public TransferableState getTransferableState(int eid, boolean setState) {
P
pjsousa@gmail.com 已提交
207

208
        if (lastCheckpointEid > -1 && eid >= lastCheckpointEid) {
209

210
            BatchInfo[] batches = null;
P
pjsousa@gmail.com 已提交
211

212 213 214 215
             if  (eid <= lastEid) {
                int size = eid - lastCheckpointEid ;
            
                if (size > 0) {
216
                    batches = new BatchInfo[size];
217 218 219 220

                    for (int i = 0; i < size; i++)
                        batches[i] = messageBatches[i];
                }
221
             } else if (lastEid > -1) {
222

223
                    batches = messageBatches;
224
             }
225
            return new TransferableState(batches, lastCheckpointEid, lastCheckpointRound, lastCheckpointLeader, eid, (setState ? state : null), stateHash);
P
pjsousa@gmail.com 已提交
226 227 228 229 230

        }
        else return null;
    }

231 232 233 234 235 236
    /**
     * 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) {

237 238 239 240 241
        position = 0;
        if (transState.getMessageBatches() != null) {
            for (int i = 0; i < transState.getMessageBatches().length; i++, position = i) {
                this.messageBatches[i] = transState.getMessageBatches()[i];
            }
242 243
        }

244
        this.lastCheckpointEid = transState.getLastCheckpointEid();
245 246 247

        this.state = transState.getState();

248 249
        this.stateHash = transState.getStateHash();

250
        this.lastEid = transState.getLastEid();
251
    }
252

P
pjsousa@gmail.com 已提交
253
}