Ejemplo n.º 1
0
  @Override
  public int setState(ApplicationState recvState) {
    int lastCID = -1;
    if (recvState instanceof CSTState) {
      CSTState state = (CSTState) recvState;

      int lastCheckpointCID = state.getCheckpointCID();
      lastCID = state.getLastCID();

      bftsmart.tom.util.Logger.println(
          "(DurabilityCoordinator.setState) I'm going to update myself from CID "
              + lastCheckpointCID
              + " to CID "
              + lastCID);

      stateLock.lock();
      if (state.getSerializedState() != null) {
        System.out.println("The state is not null. Will install it");
        log.update(state);
        installSnapshot(state.getSerializedState());
      }

      System.out.print("--- Installing log from " + (lastCheckpointCID + 1) + " to " + lastCID);

      for (int cid = lastCheckpointCID + 1; cid <= lastCID; cid++) {
        try {
          bftsmart.tom.util.Logger.println(
              "(DurabilityCoordinator.setState) interpreting and verifying batched requests for CID "
                  + cid);
          CommandsInfo cmdInfo = state.getMessageBatch(cid);
          byte[][] commands = cmdInfo.commands;
          MessageContext[] msgCtx = cmdInfo.msgCtx;

          if (commands == null || msgCtx == null || msgCtx[0].isNoOp()) {
            continue;
          }

          appExecuteBatch(commands, msgCtx);
        } catch (Exception e) {
          e.printStackTrace(System.err);
        }
      }
      System.out.println("--- Installed");
      stateLock.unlock();
    }

    return lastCID;
  }