@Override public void setReplicaContext(ReplicaContext replicaContext) { this.config = replicaContext.getStaticConfiguration(); if (log == null) { globalCheckpointPeriod = config.getGlobalCheckpointPeriod(); replicaCkpIndex = getCheckpointPortionIndex(); checkpointPortion = globalCheckpointPeriod / config.getN(); // byte[] state = getSnapshot(); if (config.isToLog()) { int replicaId = config.getProcessId(); boolean isToLog = config.isToLog(); boolean syncLog = config.isToWriteSyncLog(); boolean syncCkp = config.isToWriteSyncCkp(); // log = new DurableStateLog(replicaId, state, computeHash(state), isToLog, syncLog, // syncCkp); log = new DurableStateLog(replicaId, null, null, isToLog, syncLog, syncCkp); CSTState storedState = log.loadDurableState(); if (storedState.getLastCID() > -1) { System.out.println("LAST CID RECOVERED FROM LOG: " + storedState.getLastCID()); setState(storedState); getStateManager().setLastCID(storedState.getLastCID()); } else { System.out.println("REPLICA IS IN INITIAL STATE"); } } getStateManager().askCurrentConsensusId(); } }
/** * Write commands to log file * * @param commands array of commands. Each command is an array of bytes * @param msgCtx */ private void saveCommands(byte[][] commands, MessageContext[] msgCtx) { if (!config.isToLog()) return; if (commands.length != msgCtx.length) { System.out.println("----SIZE OF COMMANDS AND MESSAGE CONTEXTS IS DIFFERENT----"); System.out.println( "----COMMANDS: " + commands.length + ", CONTEXTS: " + msgCtx.length + " ----"); } logLock.lock(); int cid = msgCtx[0].getConsensusId(); int batchStart = 0; for (int i = 0; i <= msgCtx.length; i++) { if (i == msgCtx .length) { // the batch command contains only one command or it is the last position // of the array byte[][] batch = Arrays.copyOfRange(commands, batchStart, i); MessageContext[] batchMsgCtx = Arrays.copyOfRange(msgCtx, batchStart, i); log.addMessageBatch(batch, batchMsgCtx, cid); log.setLastCID(cid, globalCheckpointPeriod, checkpointPortion); // if(batchStart > 0) // System.out.println("Last batch: " + commands.length + "," + batchStart + "-" + i + // "," + batch.length); } else { if (msgCtx[i].getConsensusId() > cid) { // saves commands when the CID changes or when it is the last batch byte[][] batch = Arrays.copyOfRange(commands, batchStart, i); MessageContext[] batchMsgCtx = Arrays.copyOfRange(msgCtx, batchStart, i); // System.out.println("THERE IS MORE THAN ONE CID in this batch." + commands.length + // "," + batchStart + "-" + i + "," + batch.length); log.addMessageBatch(batch, batchMsgCtx, cid); log.setLastCID(cid, globalCheckpointPeriod, checkpointPortion); cid = msgCtx[i].getConsensusId(); batchStart = i; } } } logLock.unlock(); }