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 ; }
Applies the given commit to the state machine .