Skip to content
Snippets Groups Projects
Commit 87ec6c37 authored by kevinmv2's avatar kevinmv2
Browse files

did we do it

parent 53f22ac7
Branches reqvote
No related tags found
No related merge requests found
...@@ -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("voted 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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment