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
37f9d442
Commit
37f9d442
authored
3 months ago
by
AhaanKanaujia
Browse files
Options
Downloads
Patches
Plain Diff
fixed append entries
parent
1c12fda2
No related branches found
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
+35
-13
35 additions, 13 deletions
raft/raft.go
with
35 additions
and
13 deletions
raft/raft.go
+
35
−
13
View file @
37f9d442
...
@@ -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
,
}
}
...
...
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