Round.java 15.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
    //private transient TimeoutTask timeoutTask; // Timeout ssociated with this round
P
pjsousa@gmail.com 已提交
36 37 38 39 40 41 42 43 44 45
    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 byte[][] decide; // values decided by 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
46
    //private long timeout; // duration of the timeout
P
pjsousa@gmail.com 已提交
47 48 49
    private boolean alreadyRemoved = false; // indicates if this round was removed from its execution

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


    private ReconfigurationManager manager;

P
pjsousa@gmail.com 已提交
57 58 59 60 61 62
    /**
     * 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
     */
63
    protected Round(ReconfigurationManager manager, Execution parent, int number, long timeout) {
P
pjsousa@gmail.com 已提交
64 65
        this.execution = parent;
        this.number = number;
66 67
        this.manager = manager;
        //ExecutionManager manager = execution.getManager();
P
pjsousa@gmail.com 已提交
68

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

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

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

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

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

            Arrays.fill((Object[]) weak, null);
            Arrays.fill((Object[]) strong, null);
            Arrays.fill((Object[]) decide, null);
        } else {
89
            Round previousRound = execution.getRound(number - 1, manager);
P
pjsousa@gmail.com 已提交
90 91 92 93 94 95

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

96 97 98
        //define the timeout for this round (not used anymore)
        //this.timeout = (int) Math.pow(2, number) * timeout;
        //execution.getManager().getAcceptor().scheduleTimeout(this);
P
pjsousa@gmail.com 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    }

    /**
     * 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);
        }
127 128 129
        //******* EDUARDO BEGIN **************//
        proofs[this.manager.getCurrentViewPos(acceptor)] = proof;
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
130 131 132 133 134 135
    }

    /**
     * Retrieves the duration for the timeout
     * @return Duration for the timeout
     */
136
    /*public long getTimeout() {
P
pjsousa@gmail.com 已提交
137
        return this.timeout;
138
    }*/
P
pjsousa@gmail.com 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

    /**
     * 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;
    }

    /**
     * Sets the timeout associated with this round
     * @param timeoutTask The timeout associated with this round
     */
160
    /*public void setTimeoutTask(TimeoutTask timeoutTask) {
P
pjsousa@gmail.com 已提交
161
        this.timeoutTask = timeoutTask;
162
    }*/
P
pjsousa@gmail.com 已提交
163 164 165 166 167

    /**
     * Retrieves the timeout associated with this round
     * @param timeoutTask The timeout associated with this round
     */
168
    /*public TimeoutTask getTimeoutTask() {
P
pjsousa@gmail.com 已提交
169
        return timeoutTask;
170
    }*/
P
pjsousa@gmail.com 已提交
171 172 173 174 175 176 177

    /**
     * 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) {
178
        return weak[this.manager.getCurrentViewPos(acceptor)] != null;
P
pjsousa@gmail.com 已提交
179 180 181 182 183 184 185 186
    }

    /**
     * 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) {
187 188 189
        //******* EDUARDO BEGIN **************//
        return strong[this.manager.getCurrentViewPos(acceptor)] != null;
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
190 191 192 193 194 195 196 197
    }

    /**
     * Informs if there is a decided value from a replica
     * @param acceptor The replica ID
     * @return True if there is a decided value from a replica, false otherwise
     */
    public boolean isDecideSetted(int acceptor) {
198 199 200
        //******* EDUARDO BEGIN **************//
        return decide[this.manager.getCurrentViewPos(acceptor)] != null;
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
201 202 203 204 205 206 207 208
    }

    /**
     * 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) {
209 210 211
        //******* EDUARDO BEGIN **************//
        return this.weak[this.manager.getCurrentViewPos(acceptor)];
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    }

    /**
     * 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?
228 229 230 231 232
        //******* 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 已提交
233
        }
234
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
235 236 237 238 239 240 241 242
    }

    /**
     * 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) {
243 244 245
        //******* EDUARDO BEGIN **************//
        return strong[this.manager.getCurrentViewPos(acceptor)];
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    }

    /**
     * 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?
262 263 264 265 266
        //******* 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 已提交
267
        }
268
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
269 270 271 272 273 274 275 276
    }

    /**
     * Retrieves the decided value by the specified replica
     * @param acceptor The replica ID
     * @return The value decided by the specified replica
     */
    public byte[] getDecide(int acceptor) {
277 278 279
        //******* EDUARDO BEGIN **************//
        return decide[this.manager.getCurrentViewPos(acceptor)];
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    }

    /**
     * Retrives all the decided values by all replicas
     * @return The values decided by all replicas
     */
    public byte[][] getDecide() {
        return decide;
    }

    /**
     * Sets the decided value by the specified replica
     * @param acceptor The replica ID
     * @param value The value decided by the specified replica
     */
    public void setDecide(int acceptor, byte[] value) {
296 297 298
        //******* EDUARDO BEGIN **************//
        decide[this.manager.getCurrentViewPos(acceptor)] = value;
        //******* EDUARDO END **************//
P
pjsousa@gmail.com 已提交
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    }

    /**
     * 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);
    }

    /**
     * Retrives the ammount of replicas that decided a specified value
     * @param value The value in question
     * @return Ammount of replicas that decided the specified value
     */
    public int countDecide(byte[] value) {
        return count(null,decide, 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++) {
414 415 416
            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] ,");
            buffDecide.append(str(decide[i]) + " [" + (decide[i] != null ? decide[i].length : 0) + " bytes] ,");
P
pjsousa@gmail.com 已提交
417 418
        }

419 420 421
        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])");
        buffDecide.append(str(decide[decide.length - 1]) + " [" + (decide[decide.length - 1] != null ? decide[decide.length - 1].length : 0) + " bytes])");
P
pjsousa@gmail.com 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

        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;
    }
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    
    /**
     * Limpa toda a informacao que tem sobre o consenso (proposes, weaks, strons e decides)
     */
    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][];
        this.decide = new byte[n][];

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