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
87ec6c37
Commit
87ec6c37
authored
Apr 05, 2022
by
kevinmv2
Browse files
did we do it
parent
53f22ac7
Changes
1
Hide whitespace changes
Inline
Side-by-side
mp2/src/raft/raft.go
View file @
87ec6c37
...
...
@@ -122,8 +122,8 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
// Refresh timeout
rf
.
mu
.
Lock
()
// fmt.Printf("
Vote from %d to
%d \n", args.CandidateID, rf.me)
// fmt.Printf("vote
d for term %d vote term %d\n", rf.votedFor.term,
args.Term)
// fmt.Printf("
Candidate: %d to Server:
%d \n", args.CandidateID, rf.me)
// fmt.Printf("vote
term %d\n",
args.Term)
// rf.printLog()
logTerm
:=
0
if
len
(
rf
.
log
)
!=
0
{
...
...
@@ -140,8 +140,8 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
reply
.
VoteGranted
=
false
reply
.
Responded
=
true
}
else
if
rf
.
currentTerm
==
args
.
Term
&&
(
rf
.
votedFor
.
candidate
==
-
1
||
rf
.
votedFor
.
candidate
==
args
.
CandidateID
)
&&
(
args
.
LastLogTerm
>
logTerm
||
(
args
.
LastLogTerm
==
logTerm
&&
args
.
LastLogIndex
>=
len
(
rf
.
log
)
-
1
)){
rf
.
startTime
=
time
.
Now
()
rf
.
startTime
=
time
.
Now
()
reply
.
Term
=
rf
.
currentTerm
reply
.
VoteGranted
=
true
reply
.
Responded
=
true
...
...
@@ -156,7 +156,7 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
// fmt.Print("\n")
}
else
{
// print("here\n")
reply
.
Term
=
rf
.
currentTerm
reply
.
VoteGranted
=
false
reply
.
Responded
=
true
...
...
@@ -370,7 +370,8 @@ func (rf *Raft) Follower() {
}
func
(
rf
*
Raft
)
Candidate
()
{
timeout
:=
time
.
Duration
((
500
+
(
rand
.
Int
()
%
200
)))
*
time
.
Millisecond
//changed from 500 to 300
timeout
:=
time
.
Duration
((
300
+
(
rand
.
Int
()
%
200
)))
*
time
.
Millisecond
rf
.
mu
.
Lock
()
rf
.
currentTerm
+=
1
rf
.
state
=
"CANDIDATE"
...
...
@@ -402,7 +403,7 @@ func (rf *Raft) Candidate() {
finished
:=
1
timedout
:=
false
isfollower
:=
false
currTerm
:=
rf
.
currentTerm
//
currTerm := rf.currentTerm
var
mu
sync
.
Mutex
cond
:=
sync
.
NewCond
(
&
mu
)
for
i
:=
0
;
i
<
len
(
rf
.
peers
);
i
++
{
...
...
@@ -417,8 +418,8 @@ func (rf *Raft) Candidate() {
if
resp
[
x
]
.
VoteGranted
{
count
++
// fmt.Print("Count: " + strconv.Itoa(count) +" \n")
}
else
if
currTerm
<
resp
[
x
]
.
Term
{
isfollower
=
true
//
} else if currTerm < resp[x].Term{
//
isfollower = true
}
finished
++
cond
.
Broadcast
()
...
...
@@ -436,14 +437,18 @@ func (rf *Raft) Candidate() {
}
()
go
func
()
{
for
{
time
.
Sleep
(
20
*
time
.
Millisecond
)
//from 20 to 10
time
.
Sleep
(
10
*
time
.
Millisecond
)
rf
.
mu
.
Lock
()
if
rf
.
state
!=
"CANDIDATE"
{
mu
.
Lock
()
isfollower
=
true
;
defer
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
cond
.
Broadcast
()
return
}
rf
.
mu
.
Unlock
()
}
}
()
...
...
@@ -499,7 +504,7 @@ func (rf *Raft) Candidate() {
rf
.
mu
.
Unlock
()
return
}
if
timedout
{
if
timedout
||
(
finished
==
len
(
rf
.
peers
)
&&
count
<=
majority
)
{
go
rf
.
Candidate
()
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
...
...
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