Commit 73367bf7 authored by djq2's avatar djq2
Browse files

.we made it

parent 53f22ac7
......@@ -269,17 +269,15 @@ func (rf *Raft) handleCommitChannel(){
entries = rf.log[rf.lastApplied + 1: rf.commitIndex + 1]
rf.lastApplied = rf.commitIndex
}
rf.mu.Unlock()
for i := 0; i < len(entries); i ++{
rf.applyCh <- ApplyMsg{
CommandValid: true,
Command: entries[i].Command,
CommandIndex: lastApplied + 2 + i,
}
}
rf.mu.Unlock()
// rf.printLog()
}
......@@ -339,7 +337,7 @@ func (rf *Raft) sendAppendEntries(server int, args *AppendEntriesArgs, reply *Ap
// Follower Protocol
func (rf *Raft) Follower() {
// Start listening
timeout := time.Duration((350 + (rand.Int() % 150))) * time.Millisecond
timeout := time.Duration((300 + (rand.Int() % 200))) * time.Millisecond
rf.mu.Lock()
rf.state = "FOLLOWER"
......@@ -370,7 +368,7 @@ func (rf *Raft) Follower() {
}
func (rf *Raft) Candidate() {
timeout := time.Duration((500 + (rand.Int() % 200))) * time.Millisecond
timeout := time.Duration((400 + (rand.Int() % 300))) * time.Millisecond
rf.mu.Lock()
rf.currentTerm += 1
rf.state = "CANDIDATE"
......@@ -402,7 +400,6 @@ func (rf *Raft) Candidate() {
finished := 1
timedout := false
isfollower := false
currTerm := rf.currentTerm
var mu sync.Mutex
cond := sync.NewCond(&mu)
for i := 0; i < len(rf.peers); i++ {
......@@ -417,8 +414,6 @@ func (rf *Raft) Candidate() {
if resp[x].VoteGranted {
count++
// fmt.Print("Count: " + strconv.Itoa(count) +" \n")
} else if currTerm < resp[x].Term{
isfollower = true
}
finished++
cond.Broadcast()
......@@ -437,12 +432,17 @@ func (rf *Raft) Candidate() {
go func() {
for {
time.Sleep(20 * time.Millisecond)
mu.Lock()
rf.mu.Lock()
if rf.state != "CANDIDATE"{
mu.Lock()
isfollower = true;
defer mu.Unlock()
rf.mu.Unlock()
cond.Broadcast()
return
} else {
mu.Unlock()
rf.mu.Unlock()
}
}
......@@ -476,7 +476,7 @@ func (rf *Raft) Candidate() {
rf.mu.Unlock()
return
} else if isfollower {
} else if isfollower || (finished == len(rf.peers) && count <= majority) {
rf.state = "FOLLOWER"
go rf.Follower()
mu.Unlock()
......@@ -499,7 +499,7 @@ func (rf *Raft) Candidate() {
rf.mu.Unlock()
return
}
if timedout{
if timedout {
go rf.Candidate()
mu.Unlock()
rf.mu.Unlock()
......@@ -508,8 +508,6 @@ func (rf *Raft) Candidate() {
mu.Unlock()
rf.mu.Unlock()
return
}
......
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