Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs425-mp2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ahaank2
cs425-mp2
Commits
4c2e9d98
Commit
4c2e9d98
authored
4 months ago
by
AhaanKanaujia
Browse files
Options
Downloads
Patches
Plain Diff
added optimization
parent
37f9d442
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
raft/raft.go
+49
-3
49 additions, 3 deletions
raft/raft.go
with
49 additions
and
3 deletions
raft/raft.go
+
49
−
3
View file @
4c2e9d98
...
@@ -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
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment