Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ECE 428 - Distributed Systems
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
djq2
ECE 428 - Distributed Systems
Commits
87ec6c37
Commit
87ec6c37
authored
3 years ago
by
kevinmv2
Browse files
Options
Downloads
Patches
Plain Diff
did we do it
parent
53f22ac7
Branches
reqvote
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mp2/src/raft/raft.go
+15
-10
15 additions, 10 deletions
mp2/src/raft/raft.go
with
15 additions
and
10 deletions
mp2/src/raft/raft.go
+
15
−
10
View file @
87ec6c37
...
@@ -122,8 +122,8 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
...
@@ -122,8 +122,8 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
// Refresh timeout
// Refresh timeout
rf
.
mu
.
Lock
()
rf
.
mu
.
Lock
()
// fmt.Printf("
Vote from %d to
%d \n", args.CandidateID, rf.me)
// fmt.Printf("
Candidate: %d to Server:
%d \n", args.CandidateID, rf.me)
// fmt.Printf("vote
d for term %d vote term %d\n", rf.votedFor.term,
args.Term)
// fmt.Printf("vote
term %d\n",
args.Term)
// rf.printLog()
// rf.printLog()
logTerm
:=
0
logTerm
:=
0
if
len
(
rf
.
log
)
!=
0
{
if
len
(
rf
.
log
)
!=
0
{
...
@@ -140,8 +140,8 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
...
@@ -140,8 +140,8 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
reply
.
VoteGranted
=
false
reply
.
VoteGranted
=
false
reply
.
Responded
=
true
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
)){
}
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
.
Term
=
rf
.
currentTerm
reply
.
VoteGranted
=
true
reply
.
VoteGranted
=
true
reply
.
Responded
=
true
reply
.
Responded
=
true
...
@@ -156,7 +156,7 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
...
@@ -156,7 +156,7 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
// fmt.Print("\n")
// fmt.Print("\n")
}
else
{
}
else
{
// print("here\n")
reply
.
Term
=
rf
.
currentTerm
reply
.
Term
=
rf
.
currentTerm
reply
.
VoteGranted
=
false
reply
.
VoteGranted
=
false
reply
.
Responded
=
true
reply
.
Responded
=
true
...
@@ -370,7 +370,8 @@ func (rf *Raft) Follower() {
...
@@ -370,7 +370,8 @@ func (rf *Raft) Follower() {
}
}
func
(
rf
*
Raft
)
Candidate
()
{
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
.
mu
.
Lock
()
rf
.
currentTerm
+=
1
rf
.
currentTerm
+=
1
rf
.
state
=
"CANDIDATE"
rf
.
state
=
"CANDIDATE"
...
@@ -402,7 +403,7 @@ func (rf *Raft) Candidate() {
...
@@ -402,7 +403,7 @@ func (rf *Raft) Candidate() {
finished
:=
1
finished
:=
1
timedout
:=
false
timedout
:=
false
isfollower
:=
false
isfollower
:=
false
currTerm
:=
rf
.
currentTerm
//
currTerm := rf.currentTerm
var
mu
sync
.
Mutex
var
mu
sync
.
Mutex
cond
:=
sync
.
NewCond
(
&
mu
)
cond
:=
sync
.
NewCond
(
&
mu
)
for
i
:=
0
;
i
<
len
(
rf
.
peers
);
i
++
{
for
i
:=
0
;
i
<
len
(
rf
.
peers
);
i
++
{
...
@@ -417,8 +418,8 @@ func (rf *Raft) Candidate() {
...
@@ -417,8 +418,8 @@ func (rf *Raft) Candidate() {
if
resp
[
x
]
.
VoteGranted
{
if
resp
[
x
]
.
VoteGranted
{
count
++
count
++
// fmt.Print("Count: " + strconv.Itoa(count) +" \n")
// fmt.Print("Count: " + strconv.Itoa(count) +" \n")
}
else
if
currTerm
<
resp
[
x
]
.
Term
{
//
} else if currTerm < resp[x].Term{
isfollower
=
true
//
isfollower = true
}
}
finished
++
finished
++
cond
.
Broadcast
()
cond
.
Broadcast
()
...
@@ -436,14 +437,18 @@ func (rf *Raft) Candidate() {
...
@@ -436,14 +437,18 @@ func (rf *Raft) Candidate() {
}
()
}
()
go
func
()
{
go
func
()
{
for
{
for
{
time
.
Sleep
(
20
*
time
.
Millisecond
)
//from 20 to 10
time
.
Sleep
(
10
*
time
.
Millisecond
)
rf
.
mu
.
Lock
()
if
rf
.
state
!=
"CANDIDATE"
{
if
rf
.
state
!=
"CANDIDATE"
{
mu
.
Lock
()
mu
.
Lock
()
isfollower
=
true
;
isfollower
=
true
;
defer
mu
.
Unlock
()
defer
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
cond
.
Broadcast
()
cond
.
Broadcast
()
return
return
}
}
rf
.
mu
.
Unlock
()
}
}
}
()
}
()
...
@@ -499,7 +504,7 @@ func (rf *Raft) Candidate() {
...
@@ -499,7 +504,7 @@ func (rf *Raft) Candidate() {
rf
.
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
return
return
}
}
if
timedout
{
if
timedout
||
(
finished
==
len
(
rf
.
peers
)
&&
count
<=
majority
)
{
go
rf
.
Candidate
()
go
rf
.
Candidate
()
mu
.
Unlock
()
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
rf
.
mu
.
Unlock
()
...
...
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