internal async Task <IPeerResponse> HandleAppendEntriesRequest(AppendEntriesRequest <TWriteOp> entry) { var ceva = entry.Term; var r = entry.PrevLogIndex; var rr = entry.Entries; var t = entry.LeaderId; var s = entry.PrevLogTerm; if (log.LogEntries.Count < entry.Entries.Count) { log.LogEntries = entry.Entries; Console.WriteLine($" append new log {log.LogEntries.Last()?.Operation}"); } return(new RequestVoteResponse() { Sender = Id, Term = _currentTerm, VoteGranted = false, }); }
/// <summary> /// Processes the given append entries request. /// </summary> /// <param name="request">AppendEntriesRequest</param> void AppendEntries(AppendEntriesRequest request) { if (request.Term < this.CurrentTerm) { Console.WriteLine("\n [Server] " + this.ServerId + " | term " + this.CurrentTerm + " | log " + this.Logs.Count + " | last applied: " + this.LastApplied + " | append false (< term)\n"); this.Send(request.LeaderId, new AppendEntriesResponse(this.CurrentTerm, false, this.Id, request.ReceiverEndpoint)); } else { if (request.PrevLogIndex > 0 && (this.Logs.Count < request.PrevLogIndex || this.Logs[request.PrevLogIndex - 1].Term != request.PrevLogTerm)) { Console.WriteLine("\n [Server] " + this.ServerId + " | term " + this.CurrentTerm + " | log " + this.Logs.Count + " | last applied: " + this.LastApplied + " | append false (not in log)\n"); this.Send(request.LeaderId, new AppendEntriesResponse(this.CurrentTerm, false, this.Id, request.ReceiverEndpoint)); } else { if (request.Entries.Count > 0) { var currentIndex = request.PrevLogIndex + 1; foreach (var entry in request.Entries) { if (this.Logs.Count < currentIndex) { this.Logs.Add(entry); } else if (this.Logs[currentIndex - 1].Term != entry.Term) { this.Logs.RemoveRange(currentIndex - 1, this.Logs.Count - (currentIndex - 1)); this.Logs.Add(entry); } currentIndex++; } } if (request.LeaderCommit > this.CommitIndex && this.Logs.Count < request.LeaderCommit) { this.CommitIndex = this.Logs.Count; } else if (request.LeaderCommit > this.CommitIndex) { this.CommitIndex = request.LeaderCommit; } if (this.CommitIndex > this.LastApplied) { this.LastApplied++; } Console.WriteLine("\n [Server] " + this.ServerId + " | term " + this.CurrentTerm + " | log " + this.Logs.Count + " | entries received " + request.Entries.Count + " | last applied " + this.LastApplied + " | append true\n"); this.LeaderId = request.LeaderId; this.Send(request.LeaderId, new AppendEntriesResponse(this.CurrentTerm, true, this.Id, request.ReceiverEndpoint)); } } }