Commit 87ec6c37 authored by kevinmv2's avatar kevinmv2
Browse files

did we do it

parent 53f22ac7
......@@ -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("voted 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()
......
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