Search in sources :

Example 1 with RaftPeerRole

use of org.apache.ratis.proto.RaftProtos.RaftPeerRole in project incubator-ratis by apache.

the class ArithmeticStateMachine method applyTransaction.

@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;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Message(org.apache.ratis.protocol.Message) Expression(org.apache.ratis.examples.arithmetic.expression.Expression) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) RaftPeerRole(org.apache.ratis.proto.RaftProtos.RaftPeerRole)

Aggregations

Expression (org.apache.ratis.examples.arithmetic.expression.Expression)1 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)1 RaftPeerRole (org.apache.ratis.proto.RaftProtos.RaftPeerRole)1 Message (org.apache.ratis.protocol.Message)1 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)1