Round.java 12.2 KB
Newer Older
P
pjsousa@gmail.com 已提交
1 2
/**
 * Copyright (c) 2007-2009 Alysson Bessani, Eduardo Alchieri, Paulo Sousa, and the authors indicated in the @author tags
3
 *
P
pjsousa@gmail.com 已提交
4
 * This file is part of SMaRt.
5
 *
P
pjsousa@gmail.com 已提交
6 7 8 9
 * 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.
10
 *
P
pjsousa@gmail.com 已提交
11 12
 * SMaRt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
P
pjsousa@gmail.com 已提交
14
 * GNU General Public License for more details.
15
 *
P
pjsousa@gmail.com 已提交
16 17 18 19 20 21 22 23 24 25
 * 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.paxosatwar.executionmanager;

import java.io.Serializable;
import java.security.SignedObject;
import java.util.Arrays;
import java.util.Collection;
import java.util.TreeSet;
26 27
import navigators.smart.reconfiguration.ReconfigurationManager;
import navigators.smart.tom.core.messages.TOMMessage;
P
pjsousa@gmail.com 已提交
28 29 30 31 32 33 34

/**
 * This class stands for a round of an execution of a consensus
 */
public class Round implements Serializable {

    private transient Execution execution; // Execution where the round belongs to
35
    
P
pjsousa@gmail.com 已提交
36 37 38 39 40 41 42 43 44
    private int number; // Round's number
    private int me; // Process ID
    private boolean[] weakSetted;
    private boolean[] strongSetted;
    private byte[][] weak; // weakling accepted values from other processes
    private byte[][] strong; // strongly accepted values from other processes
    private Collection<Integer> freeze = null; // processes where this round was freezed
    private boolean frozen = false; // is this round frozen?
    private boolean collected = false; // indicates if a collect message for this round was already sent
45
    
P
pjsousa@gmail.com 已提交
46 47 48
    private boolean alreadyRemoved = false; // indicates if this round was removed from its execution

    public byte[] propValue = null; // proposed value
49
    public TOMMessage[] deserializedPropValue = null; //utility var
P
pjsousa@gmail.com 已提交
50 51
    public byte[] propValueHash = null; // proposed value hash
    public SignedObject[] proofs; // proof from other processes
52 53 54 55


    private ReconfigurationManager manager;

P
pjsousa@gmail.com 已提交
56 57 58 59 60 61
    /**
     * Creates a new instance of Round for acceptors
     * @param parent Execution to which this round belongs
     * @param number Number of the round
     * @param timeout Timeout duration for this round
     */
62
    protected Round(ReconfigurationManager manager, Execution parent, int number) {
P
pjsousa@gmail.com 已提交
63 64
        this.execution = parent;
        this.number = number;
65 66
        this.manager = manager;
        //ExecutionManager manager = execution.getManager();
P
pjsousa@gmail.com 已提交
67

68
        this.me = manager.getStaticConf().getProcessId();
P
pjsousa@gmail.com 已提交
69

70 71
        //int[] acceptors = manager.getAcceptors();
        int n = manager.getCurrentViewN();
P
pjsousa@gmail.com 已提交
72 73 74

        weakSetted = new boolean[n];
        strongSetted = new boolean[n];
75

P
pjsousa@gmail.com 已提交
76 77 78 79 80 81 82 83 84 85
        Arrays.fill(weakSetted, false);
        Arrays.fill(strongSetted, false);

        if (number == 0) {
            this.weak = new byte[n][];
            this.strong = new byte[n][];

            Arrays.fill((Object[]) weak, null);
            Arrays.fill((Object[]) strong, null);
        } else {
86
            Round previousRound = execution.getRound(number - 1, manager);
P
pjsousa@gmail.com 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

            this.weak = previousRound.getWeak();
            this.strong = previousRound.getStrong();
        }
    }

    /**
     * Set this round as removed from its execution
     */
    public void setRemoved() {
        this.alreadyRemoved = true;
    }

    /**
     * Informs if this round was removed from its execution
     * @return True if it is removed, false otherwise
     */
    public boolean isRemoved() {
        return this.alreadyRemoved;
    }

    /**
     * Adds a collect proof from another replica
     *
     * @param acceptor replica which sent the proof
     * @param proof proof received
     */
    public void setCollectProof(int acceptor, SignedObject proof) {
        if (proofs == null) {
            proofs = new SignedObject[weak.length];
            Arrays.fill((SignedObject[]) proofs, null);
        }
119 120 121
        //******* EDUARDO BEGIN **************//
        proofs[this.manager.getCurrentViewPos(acceptor)] = proof;
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
122 123 124 125 126 127
    }

    /**
     * Retrieves the duration for the timeout
     * @return Duration for the timeout
     */
128
    /*public long getTimeout() {
P
pjsousa@gmail.com 已提交
129
        return this.timeout;
130
    }*/
P
pjsousa@gmail.com 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

    /**
     * Retrieves this round's number
     * @return This round's number
     */
    public int getNumber() {
        return number;
    }

    /**
     * Retrieves this round's execution
     * @return This round's execution
     */
    public Execution getExecution() {
        return execution;
    }

    /**
     * Informs if there is a weakly accepted value from a replica
     * @param acceptor The replica ID
     * @return True if there is a weakly accepted value from a replica, false otherwise
     */
    public boolean isWeakSetted(int acceptor) {
154
        return weak[this.manager.getCurrentViewPos(acceptor)] != null;
P
pjsousa@gmail.com 已提交
155 156 157 158 159 160 161 162
    }

    /**
     * Informs if there is a strongly accepted value from a replica
     * @param acceptor The replica ID
     * @return True if there is a strongly accepted value from a replica, false otherwise
     */
    public boolean isStrongSetted(int acceptor) {
163 164 165
        //******* EDUARDO BEGIN **************//
        return strong[this.manager.getCurrentViewPos(acceptor)] != null;
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
166 167 168 169 170 171 172 173
    }

    /**
     * Retrives the weakly accepted value from the specified replica
     * @param acceptor The replica ID
     * @return The value weakly accepted from the specified replica
     */
    public byte[] getWeak(int acceptor) {
174 175 176
        //******* EDUARDO BEGIN **************//
        return this.weak[this.manager.getCurrentViewPos(acceptor)];
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    }

    /**
     * Retrives all weakly accepted value from all replicas
     * @return The values weakly accepted from all replicas
     */
    public byte[][] getWeak() {
        return this.weak;
    }

    /**
     * Sets the weakly accepted value from the specified replica
     * @param acceptor The replica ID
     * @param value The value weakly accepted from the specified replica
     */
    public void setWeak(int acceptor, byte[] value) { // TODO: Condicao de corrida?
193 194 195 196 197
        //******* EDUARDO BEGIN **************//
        int p = this.manager.getCurrentViewPos(acceptor);
        if (!weakSetted[p] && !isFrozen()) { //it can only be setted once
            weak[p] = value;
            weakSetted[p] = true;
P
pjsousa@gmail.com 已提交
198
        }
199
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
200 201 202 203 204 205 206 207
    }

    /**
     * Retrives the strongly accepted value from the specified replica
     * @param acceptor The replica ID
     * @return The value strongly accepted from the specified replica
     */
    public byte[] getStrong(int acceptor) {
208 209 210
        //******* EDUARDO BEGIN **************//
        return strong[this.manager.getCurrentViewPos(acceptor)];
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    }

    /**
     * Retrives all strongly accepted values from all replicas
     * @return The values strongly accepted from all replicas
     */
    public byte[][] getStrong() {
        return strong;
    }

    /**
     * Sets the strongly accepted value from the specified replica
     * @param acceptor The replica ID
     * @param value The value strongly accepted from the specified replica
     */
    public void setStrong(int acceptor, byte[] value) { // TODO: condicao de corrida?
227 228 229 230 231
        //******* EDUARDO BEGIN **************//
        int p = this.manager.getCurrentViewPos(acceptor);
        if (!strongSetted[p] && !isFrozen()) { //it can only be setted once
            strong[p] = value;
            strongSetted[p] = true;
P
pjsousa@gmail.com 已提交
232
        }
233
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    }

    /**
     * Indicates if a collect message for this round was already sent
     * @return True if done so, false otherwise
     */
    public boolean isCollected() {
        return collected;
    }

    /**
     * Establishes that a collect message for this round was already sent
     */
    public void collect() {
        collected = true;
    }

    /**
     * Indicates if this round is frozen
     * @return True if so, false otherwise
     */
    public boolean isFrozen() {
        return frozen;
    }

    /**
     * Establishes that this round is frozen
     */
    public void freeze() {
        frozen = true;
        addFreeze(me);
    }

    /**
     * Establishes that a replica locally freezed this round
     * @param acceptor replica that locally freezed this round
     */
    public void addFreeze(int acceptor) {
        if (freeze == null) {
            freeze = new TreeSet<Integer>();
        }
        freeze.add(acceptor);
    }

    /**
     * Retrieves the ammount of replicas that locally freezed this round
     * @return Ammount of replicas that locally freezed this round
     */
    public int countFreeze() {
        return freeze.size();
    }

    /**
     * Retrives the ammount of replicas from which this process weakly accepted a specified value
     * @param value The value in question
     * @return Ammount of replicas from which this process weakly accepted the specified value
     */
    public int countWeak(byte[] value) {
        return count(weakSetted,weak, value);
    }

    /**
     * Retrives the ammount of replicas from which this process strongly accepted a specified value
     * @param value The value in question
     * @return Ammount of replicas from which this process strongly accepted the specified value
     */
    public int countStrong(byte[] value) {
        return count(strongSetted,strong, value);
    }

    /**
     * Counts how many times 'value' occurs in 'array'
     * @param array Array where to count
     * @param value Value to count
     * @return Ammount of times that 'value' was find in 'array'
     */
    private int count(boolean[] arraySetted,byte[][] array, byte[] value) {
        if (value != null) {
            int counter = 0;
            for (int i = 0; i < array.length; i++) {
                if (arraySetted != null && arraySetted[i] && Arrays.equals(value, array[i])) {
                    counter++;
                }
            }
            return counter;
        }
        return 0;
    }

    /*************************** DEBUG METHODS *******************************/
    /**
     * Print round information.
     */
    @Override
    public String toString() {
        StringBuffer buffWeak = new StringBuffer(1024);
        StringBuffer buffStrong = new StringBuffer(1024);
        StringBuffer buffDecide = new StringBuffer(1024);

        buffWeak.append("W=(");
        buffStrong.append("S=(");
        buffDecide.append("D=(");

        //recall that weak.length = strong.length = decide.length

        for (int i = 0; i < weak.length - 1; i++) {
340 341
            buffWeak.append(str(weak[i]) + " [" + (weak[i] != null ? weak[i].length : 0) + " bytes] ,");
            buffStrong.append(str(strong[i]) + " [" + (strong[i] != null ? strong[i].length : 0) + " bytes] ,");
P
pjsousa@gmail.com 已提交
342 343
        }

344 345
        buffWeak.append(str(weak[weak.length - 1]) + " [" + (weak[weak.length - 1] != null ? weak[weak.length - 1].length : 0) + " bytes])");
        buffStrong.append(str(strong[strong.length - 1]) + " [" + (strong[strong.length - 1] != null ? strong[strong.length - 1].length : 0) + " bytes])");
P
pjsousa@gmail.com 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

        return "eid=" + execution.getId() + " r=" + getNumber() + " " + buffWeak + " " + buffStrong + " " + buffDecide;
    }

    private String str(byte[] obj) {
        if(obj == null) {
            return "*";
        } else {
            sun.misc.BASE64Encoder b64 = new sun.misc.BASE64Encoder();
            return b64.encode(obj);
        }
    }

    @Override
    public boolean equals(Object o) {
        return this == o;
    }
363 364
    
    /**
365
     * Clear all round info.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
     */
    public void clear() {

        int n = manager.getCurrentViewN();
        
        weakSetted = new boolean[n];
        strongSetted = new boolean[n];

        Arrays.fill(weakSetted, false);
        Arrays.fill(strongSetted, false);

        this.weak = new byte[n][];
        this.strong = new byte[n][];

        Arrays.fill((Object[]) weak, null);
        Arrays.fill((Object[]) strong, null);
    }
P
pjsousa@gmail.com 已提交
383
}