Skip to content
Snippets Groups Projects
Commit 37f9d442 authored by AhaanKanaujia's avatar AhaanKanaujia
Browse files

fixed append entries

parent 1c12fda2
No related branches found
No related tags found
No related merge requests found
...@@ -274,20 +274,39 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply ...@@ -274,20 +274,39 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply
return return
} }
// // check if existing entry conflicts with new entries
// // delete any conflicting entries
// for j, entry := range args.Entries {
// if args.PrevLogIndex + j + 1 < len(rf.log) {
// if rf.log[args.PrevLogIndex + j + 1].Term != entry.Term {
// rf.log = rf.log[:args.PrevLogIndex + j + 1]
// break
// }
// }
// }
// // append any new entries
// if len(args.Entries) > 0 {
// rf.log = append(rf.log, args.Entries...)
// }
// check if existing entry conflicts with new entries // check if existing entry conflicts with new entries
// delete any conflicting entries // delete any conflicting entries, then append new entries
for j, entry := range args.Entries { conflictIndex := args.PrevLogIndex + 1
if args.PrevLogIndex + j + 1 < len(rf.log) { for i, newEntry := range args.Entries {
if rf.log[args.PrevLogIndex + j + 1].Term != entry.Term { if conflictIndex < len(rf.log) {
rf.log = rf.log[:args.PrevLogIndex + j + 1] // conflict index found within existing log
if rf.log[conflictIndex].Term != newEntry.Term {
rf.log = rf.log[:conflictIndex]
rf.log = append(rf.log, args.Entries[i:]...)
break break
} }
} else {
// no conflict found, append new entries after existing log
rf.log = append(rf.log, args.Entries[i:]...)
break
} }
} conflictIndex++
// append any new entries
if len(args.Entries) > 0 {
rf.log = append(rf.log, args.Entries...)
} }
// update commit index // update commit index
...@@ -352,12 +371,15 @@ func (rf *Raft) sendHeartbeats() { ...@@ -352,12 +371,15 @@ func (rf *Raft) sendHeartbeats() {
} }
// check if new entries need to be sent // check if new entries need to be sent
if len(rf.log) >= rf.nextIndex[server] { if len(rf.log) >= rf.nextIndex[server] {
entriesCopy := make([]LogEntry, len(rf.log[rf.nextIndex[server]:]))
copy(entriesCopy, rf.log[rf.nextIndex[server]:])
argsServer[server] = AppendEntriesArgs{ argsServer[server] = AppendEntriesArgs{
Term: rf.currentTerm, Term: rf.currentTerm,
LeaderId: rf.me, LeaderId: rf.me,
PrevLogIndex: rf.nextIndex[server] - 1, PrevLogIndex: rf.nextIndex[server] - 1,
PrevLogTerm: rf.log[rf.nextIndex[server]-1].Term, PrevLogTerm: rf.log[rf.nextIndex[server] - 1].Term,
Entries: rf.log[rf.nextIndex[server]:], Entries: entriesCopy,
LeaderCommit: rf.commitIndex, LeaderCommit: rf.commitIndex,
} }
} else { } else {
...@@ -365,7 +387,7 @@ func (rf *Raft) sendHeartbeats() { ...@@ -365,7 +387,7 @@ func (rf *Raft) sendHeartbeats() {
Term: rf.currentTerm, Term: rf.currentTerm,
LeaderId: rf.me, LeaderId: rf.me,
PrevLogIndex: rf.nextIndex[server] - 1, PrevLogIndex: rf.nextIndex[server] - 1,
PrevLogTerm: rf.log[rf.nextIndex[server]-1].Term, PrevLogTerm: rf.log[rf.nextIndex[server] - 1].Term,
Entries: []LogEntry{}, Entries: []LogEntry{},
LeaderCommit: rf.commitIndex, LeaderCommit: rf.commitIndex,
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment