Java Code Examples for org.apache.ratis.statemachine.TransactionContext#getLogEntry()

The following examples show how to use org.apache.ratis.statemachine.TransactionContext#getLogEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArithmeticStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  final LogEntryProto entry = trx.getLogEntry();
  final AssignmentMessage assignment = new AssignmentMessage(entry.getStateMachineLogEntry().getLogData());

  final long index = entry.getIndex();
  final Double result;
  try(AutoCloseableLock writeLock = writeLock()) {
    result = assignment.evaluate(variables);
    updateLastAppliedTermIndex(entry.getTerm(), index);
  }
  final Expression r = Expression.Utils.double2Expression(result);
  final CompletableFuture<Message> f = CompletableFuture.completedFuture(Expression.Utils.toMessage(r));

  final RaftPeerRole role = trx.getServerRole();
  if (role == RaftPeerRole.LEADER) {
    LOG.info("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
  } else {
    LOG.debug("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
  }
  if (LOG.isTraceEnabled()) {
    LOG.trace("{}-{}: variables={}", getId(), index, variables);
  }
  return f;
}
 
Example 2
Source File: StateMachineShutdownTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  CompletableFuture<Message> future = new CompletableFuture<Message>();
  if (blockOnApply) {
    synchronized (objectToWait) {
      try {
        objectToWait.wait();
      } catch (InterruptedException e) {
        throw new RuntimeException();
      }
    }
  }
  RaftProtos.LogEntryProto entry = trx.getLogEntry();
  updateLastAppliedTermIndex(entry.getTerm(), entry.getIndex());
  future.complete(new RaftTestUtil.SimpleMessage("done"));
  return future;
}
 
Example 3
Source File: ArithmeticStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  final LogEntryProto entry = trx.getLogEntry();
  final AssignmentMessage assignment = new AssignmentMessage(entry.getStateMachineLogEntry().getLogData());

  final long index = entry.getIndex();
  final Double result;
  try(final AutoCloseableLock writeLock = writeLock()) {
    result = assignment.evaluate(variables);
    updateLastAppliedTermIndex(entry.getTerm(), index);
  }
  final Expression r = Expression.Utils.double2Expression(result);
  final CompletableFuture<Message> f = CompletableFuture.completedFuture(Expression.Utils.toMessage(r));

  final RaftPeerRole role = trx.getServerRole();
  if (role == RaftPeerRole.LEADER) {
    LOG.info("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
  } else {
    LOG.debug("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
  }
  if (LOG.isTraceEnabled()) {
    LOG.trace("{}-{}: variables={}", getId(), index, variables);
  }
  return f;
}
 
Example 4
Source File: LogStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  try {
    checkInitialization();
    final LogEntryProto entry = trx.getLogEntry();
    LogServiceRequestProto logServiceRequestProto =
        LogServiceRequestProto.parseFrom(entry.getStateMachineLogEntry().getLogData());
    switch (logServiceRequestProto.getRequestCase()) {
      case CLOSELOG:
        return processCloseLog(logServiceRequestProto);
      case APPENDREQUEST:
        return processAppendRequest(trx, logServiceRequestProto);
      case SYNCREQUEST:
        return processSyncRequest(trx, logServiceRequestProto);
      default:
        //TODO
        return null;
    }
  } catch (IOException e) {
    // TODO exception handling
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: FileStoreStateMachine.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  final LogEntryProto entry = trx.getLogEntry();

  final long index = entry.getIndex();
  updateLastAppliedTermIndex(entry.getTerm(), index);

  final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
  final FileStoreRequestProto request;
  try {
    request = FileStoreRequestProto.parseFrom(smLog.getLogData());
  } catch (InvalidProtocolBufferException e) {
    return FileStoreCommon.completeExceptionally(index,
        "Failed to parse logData in" + smLog, e);
  }

  switch(request.getRequestCase()) {
    case DELETE:
      return delete(index, request.getDelete());
    case WRITEHEADER:
      return writeCommit(index, request.getWriteHeader(), smLog.getStateMachineEntry().getStateMachineData().size());
    case WRITE:
      // WRITE should not happen here since
      // startTransaction converts WRITE requests to WRITEHEADER requests.
    default:
      LOG.error(getId() + ": Unexpected request case " + request.getRequestCase());
      return FileStoreCommon.completeExceptionally(index,
          "Unexpected request case " + request.getRequestCase());
  }
}
 
Example 6
Source File: CounterStateMachine.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Apply the INCREMENT command by incrementing the counter object.
 *
 * @param trx the transaction context
 * @return the message containing the updated counter value
 */
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  final RaftProtos.LogEntryProto entry = trx.getLogEntry();

  //check if the command is valid
  String logData = entry.getStateMachineLogEntry().getLogData()
      .toString(Charset.defaultCharset());
  if (!logData.equals("INCREMENT")) {
    return CompletableFuture.completedFuture(
        Message.valueOf("Invalid Command"));
  }
  //update the last applied term and index
  final long index = entry.getIndex();
  updateLastAppliedTermIndex(entry.getTerm(), index);

  //actual execution of the command: increment the counter
  counter.incrementAndGet();

  //return the new value of the counter to the client
  final CompletableFuture<Message> f =
      CompletableFuture.completedFuture(Message.valueOf(counter.toString()));

  //if leader, log the incremented value and it's log index
  if (trx.getServerRole() == RaftProtos.RaftPeerRole.LEADER) {
    LOG.info("{}: Increment to {}", index, counter.toString());
  }

  return f;
}
 
Example 7
Source File: LogStateMachine.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Message> processAppendRequest(TransactionContext trx,
    LogServiceRequestProto logProto) {

  final LogEntryProto entry = trx.getLogEntry();
  AppendLogEntryRequestProto proto = logProto.getAppendRequest();
  final long index = entry.getIndex();
  long newSize = 0;
  Throwable t = verifyState(State.OPEN);
  final List<Long> ids = new ArrayList<Long>();
  if (t == null) {
    try (AutoCloseableLock writeLock = writeLock()) {
        List<byte[]> entries = LogServiceProtoUtil.toListByteArray(proto.getDataList());
        for (byte[] bb : entries) {
          ids.add(this.length);
          newSize += bb.length;
          this.length++;
        }
        this.dataRecordsSize += newSize;
        // TODO do we need this for other write request (close, sync)
        updateLastAppliedTermIndex(entry.getTerm(), index);
    }
  }
  final CompletableFuture<Message> f =
      CompletableFuture.completedFuture(
        Message.valueOf(LogServiceProtoUtil.toAppendLogReplyProto(ids, t).toByteString()));
  final RaftProtos.RaftPeerRole role = trx.getServerRole();
  if (LOG.isTraceEnabled()) {
    LOG.trace("{}:{}-{}: {} new length {}", role, getId(), index,
        TextFormat.shortDebugString(proto), dataRecordsSize);
  }
  return f;
}
 
Example 8
Source File: LogStateMachine.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  try {
    checkInitialization();
    final LogEntryProto entry = trx.getLogEntry();
    LogServiceRequestProto logServiceRequestProto =
        LogServiceRequestProto.parseFrom(entry.getStateMachineLogEntry().getLogData());
    switch (logServiceRequestProto.getRequestCase()) {
    case CHANGESTATE:
        return recordTime(getCloseLogTimer, new Task(){
          @Override public CompletableFuture<Message> run() {
            return processChangeState(logServiceRequestProto);
          }});
      case APPENDREQUEST:
        return recordTime(appendRequestTimer, new Task(){
            @Override public CompletableFuture<Message> run() {
              return processAppendRequest(trx, logServiceRequestProto);
            }});
      case SYNCREQUEST:
        return recordTime(syncRequesTimer, new Task(){
          @Override public CompletableFuture<Message> run() {
            return processSyncRequest(trx, logServiceRequestProto);
          }});
      case ARCHIVELOG:
        return updateArchiveLogInfo(logServiceRequestProto);
      default:
        //TODO
        return null;
    }
  } catch (IOException e) {
    // TODO exception handling
    throw new RuntimeException(e);
  }
}
 
Example 9
Source File: FileStoreStateMachine.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
  final LogEntryProto entry = trx.getLogEntry();

  final long index = entry.getIndex();
  updateLastAppliedTermIndex(entry.getTerm(), index);

  final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
  final FileStoreRequestProto request;
  try {
    request = FileStoreRequestProto.parseFrom(smLog.getLogData());
  } catch (InvalidProtocolBufferException e) {
    return FileStoreCommon.completeExceptionally(index,
        "Failed to parse logData in" + smLog, e);
  }

  switch(request.getRequestCase()) {
    case DELETE:
      return delete(index, request.getDelete());
    case WRITEHEADER:
      return writeCommit(index, request.getWriteHeader(), smLog.getStateMachineEntry().getStateMachineData().size());
    case WRITE:
      // WRITE should not happen here since
      // startTransaction converts WRITE requests to WRITEHEADER requests.
    default:
      LOG.error(getId() + ": Unexpected request case " + request.getRequestCase());
      return FileStoreCommon.completeExceptionally(index,
          "Unexpected request case " + request.getRequestCase());
  }
}
 
Example 10
Source File: MetaStateMachine.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionContext applyTransactionSerial(TransactionContext trx) {
    RaftProtos.LogEntryProto x = trx.getLogEntry();
    MetaSMRequestProto req = null;
    try {
        req = MetaSMRequestProto.parseFrom(x.getStateMachineLogEntry().getLogData());
    } catch (InvalidProtocolBufferException e) {
        e.printStackTrace();
    }
    switch (req.getTypeCase()) {
        case REGISTERREQUEST:
            LogServiceRegisterLogRequestProto r = req.getRegisterRequest();
            LogName logname = LogServiceProtoUtil.toLogName(r.getLogname());
            RaftGroup rg = MetaServiceProtoUtil.toRaftGroup(r.getRaftGroup());
            map.put(logname, rg);
            LOG.info("Log {} registered at {} with group {} ", logname, getId(), rg );
            break;
        case UNREGISTERREQUEST:
            LogServiceUnregisterLogRequestProto unregReq = req.getUnregisterRequest();
            logname = LogServiceProtoUtil.toLogName(unregReq.getLogname());
            map.remove(logname);
            break;
        case PINGREQUEST:
            LogServicePingRequestProto pingRequest = req.getPingRequest();
            RaftPeer peer = MetaServiceProtoUtil.toRaftPeer(pingRequest.getPeer());
            if (peers.contains(peer)) {
                //Do Nothing, that's just heartbeat
            } else {
                peers.add(peer);
                avail.add(new PeerGroups(peer));
            }
            break;

        default:
    }
    return super.applyTransactionSerial(trx);
}
 
Example 11
Source File: LogStateMachine.java    From ratis with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Message> processAppendRequest(TransactionContext trx,
    LogServiceRequestProto logProto) {

  final LogEntryProto entry = trx.getLogEntry();
  AppendLogEntryRequestProto proto = logProto.getAppendRequest();
  final long index = entry.getIndex();
  long total = 0;
  Throwable t = verifyState(State.OPEN);
  if (t == null) {
    try (final AutoCloseableLock writeLock = writeLock()) {
        List<byte[]> entries = LogServiceProtoUtil.toListByteArray(proto.getDataList());
        for (byte[] bb : entries) {
          total += bb.length;
        }
        this.length += total;
        // TODO do we need this for other write request (close, sync)
        updateLastAppliedTermIndex(entry.getTerm(), index);
    }
  }
  List<Long> ids = new ArrayList<Long>();
  ids.add(index);
  final CompletableFuture<Message> f =
      CompletableFuture.completedFuture(
        Message.valueOf(LogServiceProtoUtil.toAppendLogReplyProto(ids, t).toByteString()));
  final RaftProtos.RaftPeerRole role = trx.getServerRole();
  LOG.debug("{}:{}-{}: {} new length {}", role, getId(), index, proto, length);
  if (LOG.isTraceEnabled()) {
    LOG.trace("{}-{}: variables={}", getId(), index, length);
  }
  return f;
}
 
Example 12
Source File: MetaStateMachine.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionContext applyTransactionSerial(TransactionContext trx) {
    RaftProtos.LogEntryProto x = trx.getLogEntry();
    MetaSMRequestProto req = null;
    try {
        req = MetaSMRequestProto.parseFrom(x.getStateMachineLogEntry().getLogData());
    } catch (InvalidProtocolBufferException e) {
        e.printStackTrace();
    }
    switch (req.getTypeCase()) {
        case REGISTERREQUEST:
            LogServiceRegisterLogRequestProto r = req.getRegisterRequest();
            LogName logname = LogServiceProtoUtil.toLogName(r.getLogname());
            RaftGroup rg = MetaServiceProtoUtil.toRaftGroup(r.getRaftGroup());
            rg.getPeers().stream().forEach(raftPeer -> {
                Set<LogName> logNames;
                if(!peerLogs.containsKey(raftPeer)) {
                    logNames = new HashSet<>();
                    peerLogs.put(raftPeer, logNames);
                } else {
                    logNames = peerLogs.get(raftPeer);
                }
                logNames.add(logname);

            });
            map.put(logname, rg);

            LOG.info("Log {} registered at {} with group {} ", logname, getId(), rg );
            break;
        case UNREGISTERREQUEST:
            LogServiceUnregisterLogRequestProto unregReq = req.getUnregisterRequest();
            logname = LogServiceProtoUtil.toLogName(unregReq.getLogname());
            map.remove(logname);
            break;
        case PINGREQUEST:
            LogServicePingRequestProto pingRequest = req.getPingRequest();
            RaftPeer peer = MetaServiceProtoUtil.toRaftPeer(pingRequest.getPeer());
            //If Set<RaftPeer> contains peer then do nothing as that's just heartbeat else add the peer to the set.
            if (!peers.contains(peer)) {
                peers.add(peer);
                avail.add(new PeerGroups(peer));
                heartbeatInfo.put(peer,  System.currentTimeMillis());
            }
            break;
        case HEARTBEATREQUEST:
            MetaServiceProtos.LogServiceHeartbeatRequestProto heartbeatRequest = req.getHeartbeatRequest();
            RaftPeer heartbeatPeer = MetaServiceProtoUtil.toRaftPeer(heartbeatRequest.getPeer());
            heartbeatInfo.put(heartbeatPeer,  System.currentTimeMillis());
            break;
        default:
    }
    return super.applyTransactionSerial(trx);
}