public OperationResult executeCommand ( long index , long sequence , long timestamp , RaftSession session , PrimitiveOperation operation ) { // If the service has been deleted then throw an unknown service exception. if ( deleted ) { log . warn ( "Service {} has been deleted by another process" , serviceName ) ; throw new RaftException . UnknownService ( "Service " + serviceName + " has been deleted" ) ; } // If the session is not open, fail the request. if ( ! session . getState ( ) . active ( ) ) { log . warn ( "Session not open: {}" , session ) ; throw new RaftException . UnknownSession ( "Unknown session: " + session . sessionId ( ) ) ; } // Update the session's timestamp to prevent it from being expired. session . setLastUpdated ( timestamp ) ; // Update the state machine index/timestamp. tick ( index , timestamp ) ; // If the command's sequence number is less than the next session sequence number then that indicates that // we've received a command that was previously applied to the state machine. Ensure linearizability by // returning the cached response instead of applying it to the user defined state machine. if ( sequence > 0 && sequence < session . nextCommandSequence ( ) ) { log . trace ( "Returning cached result for command with sequence number {} < {}" , sequence , session . nextCommandSequence ( ) ) ; return sequenceCommand ( index , sequence , session ) ; } // If we've made it this far, the command must have been applied in the proper order as sequenced by the // session. This should be the case for most commands applied to the state machine. else { // Execute the command in the state machine thread. Once complete, the CompletableFuture callback will be completed // in the state machine thread. Register the result in that thread and then complete the future in the caller's thread. return applyCommand ( index , sequence , timestamp , operation , session ) ; } }
Executes the given command on the state machine .