Search in sources :

Example 1 with DefaultCommit

use of io.atomix.primitive.service.impl.DefaultCommit in project atomix by atomix.

the class RaftServiceContext method applyCommand.

/**
 * Applies the given commit to the state machine.
 */
private OperationResult applyCommand(long index, long sequence, long timestamp, PrimitiveOperation operation, RaftSession session) {
    Commit<byte[]> commit = new DefaultCommit<>(index, operation.id(), operation.value(), session, timestamp);
    long eventIndex = session.getEventIndex();
    OperationResult result;
    try {
        currentSession = session;
        // Execute the state machine operation and get the result.
        byte[] output = service.apply(commit);
        // Store the result for linearizability and complete the command.
        result = OperationResult.succeeded(index, eventIndex, output);
    } catch (Exception e) {
        // If an exception occurs during execution of the command, store the exception.
        result = OperationResult.failed(index, eventIndex, e);
    } finally {
        currentSession = null;
    }
    // Once the operation has been applied to the state machine, commit events published by the command.
    // The state machine context will build a composite future for events published to all sessions.
    commit();
    // Register the result in the session to ensure retries receive the same output for the command.
    session.registerResult(sequence, result);
    // Update the session timestamp and command sequence number.
    session.setCommandSequence(sequence);
    // Complete the command.
    return result;
}
Also used : OperationResult(io.atomix.protocols.raft.impl.OperationResult) DefaultCommit(io.atomix.primitive.service.impl.DefaultCommit) RaftException(io.atomix.protocols.raft.RaftException)

Example 2 with DefaultCommit

use of io.atomix.primitive.service.impl.DefaultCommit in project atomix by atomix.

the class RaftServiceContext method applyQuery.

/**
 * Applies a query to the state machine.
 */
private void applyQuery(long timestamp, RaftSession session, PrimitiveOperation operation, CompletableFuture<OperationResult> future) {
    // If the session is not open, fail the request.
    if (!session.getState().active()) {
        log.warn("Inactive session: " + session.sessionId());
        future.completeExceptionally(new RaftException.UnknownSession("Unknown session: " + session.sessionId()));
        return;
    }
    // Set the current operation type to QUERY to prevent events from being sent to clients.
    setOperation(OperationType.QUERY);
    Commit<byte[]> commit = new DefaultCommit<>(currentIndex, operation.id(), operation.value(), session, timestamp);
    long eventIndex = session.getEventIndex();
    OperationResult result;
    try {
        currentSession = session;
        result = OperationResult.succeeded(currentIndex, eventIndex, service.apply(commit));
    } catch (Exception e) {
        result = OperationResult.failed(currentIndex, eventIndex, e);
    } finally {
        currentSession = null;
    }
    future.complete(result);
}
Also used : RaftException(io.atomix.protocols.raft.RaftException) OperationResult(io.atomix.protocols.raft.impl.OperationResult) DefaultCommit(io.atomix.primitive.service.impl.DefaultCommit) RaftException(io.atomix.protocols.raft.RaftException)

Aggregations

DefaultCommit (io.atomix.primitive.service.impl.DefaultCommit)2 RaftException (io.atomix.protocols.raft.RaftException)2 OperationResult (io.atomix.protocols.raft.impl.OperationResult)2