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)); }
private void AppendEntriesTermIsGreaterThanCurrentTerm(AppendEntries appendEntries) { if (appendEntries.Term > CurrentState.CurrentTerm) { CurrentState = new CurrentState(CurrentState.Id, appendEntries.Term, CurrentState.VotedFor, CurrentState.CommitIndex, CurrentState.LastApplied, CurrentState.LeaderId); BecomeFollower(); } }
private void ApplyToStateMachine(int commitIndex, int lastApplied, AppendEntries appendEntries) { while (commitIndex > lastApplied) { lastApplied++; var log = _log.Get(lastApplied); _fsm.Handle(log); } CurrentState = new CurrentState(CurrentState.Id, appendEntries.Term, CurrentState.VotedFor, commitIndex, lastApplied, CurrentState.LeaderId); }
public async Task <AppendEntriesResponse> Handle(AppendEntries appendEntries) { if (appendEntries.Term > CurrentState.CurrentTerm) { var response = await _rules.CommitIndexAndLastApplied(appendEntries, _log, CurrentState); await ApplyToStateMachine(appendEntries, response.commitIndex, response.lastApplied); SetLeaderId(appendEntries); _node.BecomeFollower(CurrentState); return(new AppendEntriesResponse(CurrentState.CurrentTerm, true)); } return(new AppendEntriesResponse(CurrentState.CurrentTerm, false)); }
public AppendEntriesResponse Handle(AppendEntries appendEntries) { return(State.Handle(appendEntries)); }
private void SetLeaderId(AppendEntries appendEntries) { CurrentState = new CurrentState(CurrentState.Id, CurrentState.CurrentTerm, CurrentState.VotedFor, CurrentState.CommitIndex, CurrentState.LastApplied, appendEntries.LeaderId); }
public async Task <AppendEntriesResponse> Handle(AppendEntries appendEntries) { return(await State.Handle(appendEntries)); }
public AppendEntriesResponse Request(AppendEntries appendEntries) { throw new NotImplementedException(); }