Skip to content
Snippets Groups Projects
Commit 4c2e9d98 authored by AhaanKanaujia's avatar AhaanKanaujia
Browse files

added optimization

parent 37f9d442
Branches main
No related tags found
No related merge requests found
...@@ -240,6 +240,9 @@ type AppendEntriesArgs struct { ...@@ -240,6 +240,9 @@ type AppendEntriesArgs struct {
type AppendEntriesReply struct { type AppendEntriesReply struct {
Term int // current term (for leader to update itself) Term int // current term (for leader to update itself)
Success bool // true if follower contained entry matching prevLogIndex and prevLogTerm Success bool // true if follower contained entry matching prevLogIndex and prevLogTerm
ConflictTerm int // term of conflicting entry
ConflictIndex int // index of first entry in log that conflicts with new entries
} }
// AppendEntries RPC handler // AppendEntries RPC handler
...@@ -252,7 +255,11 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply ...@@ -252,7 +255,11 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply
defer rf.mu.Unlock() defer rf.mu.Unlock()
rf.resetElectionTimer() rf.resetElectionTimer()
reply.Term = rf.currentTerm
reply.Success = false reply.Success = false
reply.ConflictTerm = -1
reply.ConflictIndex = -1
// candidate term is less than current term // candidate term is less than current term
if args.Term < rf.currentTerm { if args.Term < rf.currentTerm {
...@@ -274,6 +281,23 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply ...@@ -274,6 +281,23 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply
return return
} }
// check if log contains entry within log
if args.PrevLogIndex >= len(rf.log) {
reply.ConflictIndex = len(rf.log)
return
}
// check if log entry at precLogIndex does not match prevLogTerm
if rf.log[args.PrevLogIndex].Term != args.PrevLogTerm {
reply.ConflictTerm = rf.log[args.PrevLogIndex].Term
conflictIndex := args.PrevLogIndex
for conflictIndex >= 0 && rf.log[conflictIndex].Term == reply.ConflictTerm {
conflictIndex--
}
reply.ConflictIndex = conflictIndex + 1
return
}
// // check if existing entry conflicts with new entries // // check if existing entry conflicts with new entries
// // delete any conflicting entries // // delete any conflicting entries
// for j, entry := range args.Entries { // for j, entry := range args.Entries {
...@@ -318,7 +342,6 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply ...@@ -318,7 +342,6 @@ func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply
} }
} }
reply.Term = rf.currentTerm
reply.Success = true reply.Success = true
// fmt.Println(rf.me, rf.log) // fmt.Println(rf.me, rf.log)
...@@ -425,8 +448,31 @@ func (rf *Raft) sendHeartbeats() { ...@@ -425,8 +448,31 @@ func (rf *Raft) sendHeartbeats() {
rf.matchIndex[server] = newMatchIndex rf.matchIndex[server] = newMatchIndex
rf.nextIndex[server] = newMatchIndex + 1 rf.nextIndex[server] = newMatchIndex + 1
} else { } else {
if rf.nextIndex[server] > 1 { // if rf.nextIndex[server] > 1 {
rf.nextIndex[server]-- // rf.nextIndex[server]--
// }
// optimized nextIndex calculation
if reply.ConflictTerm != -1 {
conflictTerm := reply.ConflictTerm
lastIndexOfTerm := -1
for i := len(rf.log) - 1; i >= 0; i-- {
if rf.log[i].Term == conflictTerm {
lastIndexOfTerm = i
break
}
}
if lastIndexOfTerm >= 0 {
rf.nextIndex[server] = lastIndexOfTerm + 1
} else {
rf.nextIndex[server] = reply.ConflictIndex
}
} else {
rf.nextIndex[server] = reply.ConflictIndex
}
if rf.nextIndex[server] < 1 {
rf.nextIndex[server] = 1
} }
} }
......
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