public async Task <AppendEntriesResponse> Handle(AppendEntries appendEntries) { try { await _appendingEntries.WaitAsync(); 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); var terms = appendEntries.Entries.Any() ? string.Join(",", appendEntries.Entries.Select(x => x.Term)) : string.Empty; _logger.LogInformation( $"{CurrentState.Id} as {nameof(Follower)} applying {appendEntries.Entries.Count} to log, term {terms}"); await _rules.ApplyNewEntriesToLog(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)); } finally { _appendingEntries.Release(); } }
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); if (_applied > 1 && appendEntries.Entries.Any()) { Console.WriteLine("WTF?"); } _applied++; _logger.LogInformation($"{CurrentState.Id} as {nameof(Candidate)} applying entry to log"); await _rules.ApplyNewEntriesToLog(appendEntries, _log); var commitIndexAndLastApplied = await _rules.CommitIndexAndLastApplied(appendEntries, _log, CurrentState); await ApplyToStateMachine(commitIndexAndLastApplied.commitIndex, commitIndexAndLastApplied.lastApplied); SetLeaderId(appendEntries); AppendEntriesTermIsGreaterThanCurrentTerm(appendEntries); return(new AppendEntriesResponse(CurrentState.CurrentTerm, true)); }