Round.java 12.8 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 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 414 415
/**
 * 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.paxosatwar.executionmanager;

import java.io.Serializable;
import java.security.SignedObject;
import java.util.Arrays;
import java.util.Collection;
import java.util.TreeSet;

/**
 * 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
    private transient TimeoutTask timeoutTask; // Timeout ssociated with this round
    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
    private long timeout; // duration of the timeout
    private boolean alreadyRemoved = false; // indicates if this round was removed from its execution

    public byte[] propValue = null; // proposed value
    public Object deserializedPropValue = null; //utility var
    public byte[] propValueHash = null; // proposed value hash
    public SignedObject[] proofs; // proof from other processes
    
    /**
     * 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
     */
    protected Round(Execution parent, int number, long timeout) {
        this.execution = parent;
        this.number = number;

        ExecutionManager manager = execution.getManager();

        this.me = manager.getProcessId();

        int[] acceptors = manager.getAcceptors();
        int n = acceptors.length;

        weakSetted = new boolean[n];
        strongSetted = new boolean[n];
        
        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 {
            Round previousRound = execution.getRound(number - 1);

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

        //define the timeout for this round
        this.timeout = (int) Math.pow(2, number) * timeout;
        manager.getAcceptor().scheduleTimeout(this);
    }

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

        proofs[acceptor] = proof;
    }

    /**
     * Retrieves the duration for the timeout
     * @return Duration for the timeout
     */
    public long getTimeout() {
        return this.timeout;
    }

    /**
     * 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
     */
    public void setTimeoutTask(TimeoutTask timeoutTask) {
        this.timeoutTask = timeoutTask;
    }

    /**
     * Retrieves the timeout associated with this round
     * @param timeoutTask The timeout associated with this round
     */
    public TimeoutTask getTimeoutTask() {
        return timeoutTask;
    }

    /**
     * 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) {
        return weak[acceptor] != null;
    }

    /**
     * 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) {
        return strong[acceptor] != null;
    }

    /**
     * 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) {
        return decide[acceptor] != null;
    }

    /**
     * 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) {
        return this.weak[acceptor];
    }

    /**
     * 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?
        if (!weakSetted[acceptor] && !isFrozen()) { //it can only be setted once
            weak[acceptor] = value;
            weakSetted[acceptor] = true;
        }
    }

    /**
     * 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) {
        return strong[acceptor];
    }

    /**
     * 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?
        if (!strongSetted[acceptor] && !isFrozen()) { //it can only be setted once
            strong[acceptor] = value;
            strongSetted[acceptor] = true;
        }
    }

    /**
     * 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) {
        return decide[acceptor];
    }

    /**
     * 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) {
        decide[acceptor] = value;
    }

    /**
     * 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++) {
            buffWeak.append(str(weak[i]) + ",");
            buffStrong.append(str(strong[i]) + ",");
            buffDecide.append(str(decide[i]) + ",");
        }

        buffWeak.append(str(weak[weak.length - 1]) + ")");
        buffStrong.append(str(strong[strong.length - 1]) + ")");
        buffDecide.append(str(decide[decide.length - 1]) + ")");

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