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;
}
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);
}
Aggregations