Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
djq2
ECE 428 - Distributed Systems
Commits
73367bf7
Commit
73367bf7
authored
Apr 05, 2022
by
djq2
Browse files
.we made it
parent
53f22ac7
Changes
1
Hide whitespace changes
Inline
Side-by-side
mp2/src/raft/raft.go
View file @
73367bf7
...
...
@@ -269,17 +269,15 @@ func (rf *Raft) handleCommitChannel(){
entries
=
rf
.
log
[
rf
.
lastApplied
+
1
:
rf
.
commitIndex
+
1
]
rf
.
lastApplied
=
rf
.
commitIndex
}
rf
.
mu
.
Unlock
()
for
i
:=
0
;
i
<
len
(
entries
);
i
++
{
rf
.
applyCh
<-
ApplyMsg
{
CommandValid
:
true
,
Command
:
entries
[
i
]
.
Command
,
CommandIndex
:
lastApplied
+
2
+
i
,
}
}
rf
.
mu
.
Unlock
()
// rf.printLog()
}
...
...
@@ -339,7 +337,7 @@ func (rf *Raft) sendAppendEntries(server int, args *AppendEntriesArgs, reply *Ap
// Follower Protocol
func
(
rf
*
Raft
)
Follower
()
{
// Start listening
timeout
:=
time
.
Duration
((
3
5
0
+
(
rand
.
Int
()
%
15
0
)))
*
time
.
Millisecond
timeout
:=
time
.
Duration
((
3
0
0
+
(
rand
.
Int
()
%
20
0
)))
*
time
.
Millisecond
rf
.
mu
.
Lock
()
rf
.
state
=
"FOLLOWER"
...
...
@@ -370,7 +368,7 @@ func (rf *Raft) Follower() {
}
func
(
rf
*
Raft
)
Candidate
()
{
timeout
:=
time
.
Duration
((
5
00
+
(
rand
.
Int
()
%
2
00
)))
*
time
.
Millisecond
timeout
:=
time
.
Duration
((
4
00
+
(
rand
.
Int
()
%
3
00
)))
*
time
.
Millisecond
rf
.
mu
.
Lock
()
rf
.
currentTerm
+=
1
rf
.
state
=
"CANDIDATE"
...
...
@@ -402,7 +400,6 @@ func (rf *Raft) Candidate() {
finished
:=
1
timedout
:=
false
isfollower
:=
false
currTerm
:=
rf
.
currentTerm
var
mu
sync
.
Mutex
cond
:=
sync
.
NewCond
(
&
mu
)
for
i
:=
0
;
i
<
len
(
rf
.
peers
);
i
++
{
...
...
@@ -417,8 +414,6 @@ func (rf *Raft) Candidate() {
if
resp
[
x
]
.
VoteGranted
{
count
++
// fmt.Print("Count: " + strconv.Itoa(count) +" \n")
}
else
if
currTerm
<
resp
[
x
]
.
Term
{
isfollower
=
true
}
finished
++
cond
.
Broadcast
()
...
...
@@ -437,12 +432,17 @@ func (rf *Raft) Candidate() {
go
func
()
{
for
{
time
.
Sleep
(
20
*
time
.
Millisecond
)
mu
.
Lock
()
rf
.
mu
.
Lock
()
if
rf
.
state
!=
"CANDIDATE"
{
mu
.
Lock
()
isfollower
=
true
;
defer
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
cond
.
Broadcast
()
return
}
else
{
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
}
}
...
...
@@ -476,7 +476,7 @@ func (rf *Raft) Candidate() {
rf
.
mu
.
Unlock
()
return
}
else
if
isfollower
{
}
else
if
isfollower
||
(
finished
==
len
(
rf
.
peers
)
&&
count
<=
majority
)
{
rf
.
state
=
"FOLLOWER"
go
rf
.
Follower
()
mu
.
Unlock
()
...
...
@@ -499,7 +499,7 @@ func (rf *Raft) Candidate() {
rf
.
mu
.
Unlock
()
return
}
if
timedout
{
if
timedout
{
go
rf
.
Candidate
()
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
...
...
@@ -508,8 +508,6 @@ func (rf *Raft) Candidate() {
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
return
}
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment