@Override public Commit<Void> mapToNull() { return new DefaultCommit<>(index, operation, null, session, timestamp); }
@Override public <U> Commit<U> map(Function<T, U> transcoder) { return new DefaultCommit<>(index, operation, transcoder.apply(value), session, timestamp); }
Commit<byte[]> commit = new DefaultCommit<>(currentIndex, operation.id(), operation.value(), session, timestamp);
byte[] output = service.apply(new DefaultCommit<>( currentIndex, operation.operationId(), currentOperation = OperationType.QUERY; try { output = service.apply(new DefaultCommit<>( currentIndex, pendingRead.operationId,
/** * Applies the given commit to the state machine. */ private OperationResult applyCommand(long index, long sequence, long timestamp, PrimitiveOperation operation, RaftSession session) { long eventIndex = session.getEventIndex(); Commit<byte[]> commit = new DefaultCommit<>(index, operation.id(), operation.value(), session, timestamp); 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; }
private ExecuteResponse applyQuery(ExecuteRequest request, Session session) { try { byte[] result = context.service().apply(new DefaultCommit<>( context.getIndex(), request.operation().id(), request.operation().value(), context.setSession(session), context.currentTimestamp())); return ExecuteResponse.ok(result); } catch (Exception e) { return ExecuteResponse.error(); } finally { context.setSession(null); } }
/** * Applies an execute operation to the service. */ private void applyExecute(ExecuteOperation operation) { Session session = context.getOrCreateSession(operation.session(), operation.node()); if (operation.operation() != null) { try { context.service().apply(new DefaultCommit<>( context.setIndex(operation.index()), operation.operation().id(), operation.operation().value(), context.setSession(session), context.setTimestamp(operation.timestamp()))); } catch (Exception e) { log.warn("Failed to apply operation: {}", e); } finally { context.setSession(null); } } }
private CompletableFuture<ExecuteResponse> executeCommand(ExecuteRequest request) { PrimaryBackupSession session = context.getOrCreateSession(request.session(), request.node()); long index = context.nextIndex(); long timestamp = System.currentTimeMillis(); return replicator.replicate(new ExecuteOperation( index, timestamp, session.sessionId().id(), session.memberId(), request.operation())) .thenApply(v -> { try { byte[] result = context.service().apply(new DefaultCommit<>( context.setIndex(index), request.operation().id(), request.operation().value(), context.setSession(session), context.setTimestamp(timestamp))); return ExecuteResponse.ok(result); } catch (Exception e) { return ExecuteResponse.error(); } finally { context.setSession(null); } }); }