public AppendEntriesResponse Handle(AppendEntries appendEntries) { var response = _rules.AppendEntriesTermIsLessThanCurrentTerm(appendEntries, CurrentState); if (response.shouldReturn) { return(response.appendEntriesResponse); } response = _rules.LogDoesntContainEntryAtPreviousLogIndexWhoseTermMatchesPreviousLogTerm(appendEntries, _log, CurrentState); if (response.shouldReturn) { return(response.appendEntriesResponse); } _rules.DeleteAnyConflictsInLog(appendEntries, _log); _rules.ApplyEntriesToLog(appendEntries, _log); var commitIndexAndLastApplied = _rules.CommitIndexAndLastApplied(appendEntries, _log, CurrentState); ApplyToStateMachine(commitIndexAndLastApplied.commitIndex, commitIndexAndLastApplied.lastApplied); SetLeaderId(appendEntries); AppendEntriesTermIsGreaterThanCurrentTerm(appendEntries); return(new AppendEntriesResponse(CurrentState.CurrentTerm, true)); }
public async Task <AppendEntriesResponse> Handle(AppendEntries appendEntries) { var response = _rules.AppendEntriesTermIsLessThanCurrentTerm(appendEntries, CurrentState); if (response.shouldReturn) { return(response.appendEntriesResponse); } response = await _rules.LogDoesntContainEntryAtPreviousLogIndexWhoseTermMatchesPreviousLogTerm(appendEntries, _log, CurrentState); if (response.shouldReturn) { return(response.appendEntriesResponse); } await _rules.DeleteAnyConflictsInLog(appendEntries, _log); await _rules.ApplyEntriesToLog(appendEntries, _log); var commitIndexAndLastApplied = await _rules.CommitIndexAndLastApplied(appendEntries, _log, CurrentState); await ApplyToStateMachine(commitIndexAndLastApplied.commitIndex, commitIndexAndLastApplied.lastApplied, appendEntries); SetLeaderId(appendEntries); _messagesSinceLastElectionExpiry++; return(new AppendEntriesResponse(CurrentState.CurrentTerm, true)); }