Commit fea6e587 authored by kevinmv2's avatar kevinmv2
Browse files

changing hella

parent ab6a4497
......@@ -22,8 +22,8 @@ import "sync/atomic"
import "../labrpc"
import "math/rand"
import "time"
import "fmt"
import "strconv"
// import "fmt"
// import "strconv"
//
// as each Raft peer becomes aware that successive log entries are
// committed, the peer should send an ApplyMsg to the service (or
......@@ -142,11 +142,11 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
rf.mu.Lock()
rf.votedFor.candidate = args.CandidateID
rf.mu.Unlock()
fmt.Print("id: " + strconv.Itoa(rf.me) + " term: " + strconv.Itoa(rf.currentTerm))
fmt.Print(" ")
fmt.Print(rf.state)
fmt.Print(" Vote Term: " + strconv.Itoa(rf.votedFor.term) + " Voted For: " + strconv.Itoa(rf.votedFor.candidate))
fmt.Print("\n")
// fmt.Print("id: " + strconv.Itoa(rf.me) + " term: " + strconv.Itoa(rf.currentTerm))
// fmt.Print(" ")
// fmt.Print(rf.state)
// fmt.Print(" Vote Term: " + strconv.Itoa(rf.votedFor.term) + " Voted For: " + strconv.Itoa(rf.votedFor.candidate))
// fmt.Print("\n")
return
}else if rf.votedFor.term < args.Term{
......@@ -158,11 +158,11 @@ func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) {
rf.votedFor.candidate = args.CandidateID
rf.votedFor.term = args.Term
rf.mu.Unlock()
fmt.Print("id: " + strconv.Itoa(rf.me) + " term: " + strconv.Itoa(rf.currentTerm))
fmt.Print(" ")
fmt.Print(rf.state)
fmt.Print(" Vote Term: " + strconv.Itoa(rf.votedFor.term) + " Voted For: " + strconv.Itoa(rf.votedFor.candidate) + " Bool val " + strconv.FormatBool(reply.VoteGranted))
fmt.Print("\n")
// fmt.Print("id: " + strconv.Itoa(rf.me) + " term: " + strconv.Itoa(rf.currentTerm))
// fmt.Print(" ")
// fmt.Print(rf.state)
// fmt.Print(" Vote Term: " + strconv.Itoa(rf.votedFor.term) + " Voted For: " + strconv.Itoa(rf.votedFor.candidate) + " Bool val " + strconv.FormatBool(reply.VoteGranted))
// fmt.Print("\n")
return
}
return
......@@ -277,7 +277,6 @@ func (rf *Raft) Candidate() {
rf.currentTerm += 1
rf.state = "CANDIDATE"
rf.mu.Unlock()
candStart := time.Now()
majority := len(rf.peers)/2
//set up vote args
......@@ -304,6 +303,7 @@ func (rf *Raft) Candidate() {
count := 1
finished := 1
timedout := false
isfollower := false
var mu sync.Mutex
cond := sync.NewCond(&mu)
for i := 0; i < len(rf.peers); i++ {
......@@ -311,19 +311,20 @@ func (rf *Raft) Candidate() {
if rf.me != i {
go func(x int) {
ok := rf.sendRequestVote(x, &args, &resp[x])
fmt.Print(strconv.FormatBool(ok))
ok = ok
mu.Lock()
defer mu.Unlock()
fmt.Print("vote is: " + strconv.FormatBool(resp[x].VoteGranted) + " \n")
// fmt.Print("vote is: " + strconv.FormatBool(resp[x].VoteGranted) + " \n")
if resp[x].VoteGranted {
count++
fmt.Print("Count: " + strconv.Itoa(count) +" \n")
// fmt.Print("Count: " + strconv.Itoa(count) +" \n")
}
finished++
cond.Broadcast()
}(i)
}
}
//waits until the timeout value is hit then broadcasts
go func() {
time.Sleep(timeout)
mu.Lock()
......@@ -334,15 +335,16 @@ func (rf *Raft) Candidate() {
} ()
go func() {
for {
if rf.killed() || rf.state != "CANDIDATE"{
time.Sleep(20 * time.Millisecond)
if rf.state != "CANDIDATE"{
mu.Lock()
isfollower = true;
defer mu.Unlock()
cond.Broadcast()
return
}
}
} ()
resp[rf.me].Term = rf.currentTerm - 1
......@@ -353,12 +355,12 @@ func (rf *Raft) Candidate() {
rf.mu.Unlock()
mu.Lock()
for !(count > majority) && (finished != len(rf.peers)) && !timedout {
for !(count > majority) && (finished != len(rf.peers)) && !timedout && !isfollower {
cond.Wait()
fmt.Print("Countfdasfasdfa: " + strconv.Itoa(count) +" \n")
// fmt.Print("Countfdasfasdfa: " + strconv.Itoa(count) +" \n")
}
fmt.Print("Case Hit \n" + "Count: " + strconv.Itoa(count) + "finished " + strconv.Itoa(finished) + " \n")
// fmt.Print("Case Hit \n" + "Count: " + strconv.Itoa(count) + "finished " + strconv.Itoa(finished) + " \n")
if rf.killed() {
mu.Unlock()
......@@ -375,7 +377,7 @@ func (rf *Raft) Candidate() {
mu.Unlock()
return
}
if time.Now().Sub(candStart) >= timeout{
if timedout{
go rf.Candidate()
mu.Unlock()
return
......@@ -391,7 +393,7 @@ func (rf *Raft) Leader() {
rf.mu.Lock()
rf.state = "LEADER"
rf.mu.Unlock()
fmt.Print(strconv.Itoa(rf.me) + "is new leader \n")
// fmt.Print(strconv.Itoa(rf.me) + "is new leader \n")
heartbeat := time.Duration(100) * time.Millisecond
//send heartbeat to take tell others you are leader
rf.sendHeartbeat()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment