This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollower.go
77 lines (64 loc) · 2.24 KB
/
follower.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package raftify
import (
"encoding/json"
)
// toFollower initiates the transition into a follower node for a given term. Calling toFollower
// on a node that already is in the follower state just resets the data.
func (n *Node) toFollower(term uint64) {
n.logger.Printf("[INFO] raftify: Entering follower state for term %v\n", term)
n.resetTimeout()
n.messageTicker.Stop() // Stop the ticker if the node was a leader or candidate prior to becoming a follower.
n.currentTerm = term
n.votedFor = ""
n.state = Follower
}
// runFollower runs the follower loop. This function is called within the runLoop function.
func (n *Node) runFollower() {
select {
case msgBytes := <-n.messages.messageCh:
var msg Message
if err := json.Unmarshal(msgBytes, &msg); err != nil {
n.logger.Printf("[ERR] raftify: error while unmarshaling wrapper message: %v\n", err.Error())
break
}
switch msg.Type {
case HeartbeatMsg:
var content Heartbeat
if err := json.Unmarshal(msg.Content, &content); err != nil {
n.logger.Printf("[ERR] raftify: error while unmarshaling heartbeat message: %v\n", err.Error())
break
}
n.handleHeartbeat(content)
case PreVoteRequestMsg:
var content PreVoteRequest
if err := json.Unmarshal(msg.Content, &content); err != nil {
n.logger.Printf("[ERR] raftify: error while unmarshaling prevote request message: %v\n", err.Error())
break
}
n.handlePreVoteRequest(content)
case VoteRequestMsg:
var content VoteRequest
if err := json.Unmarshal(msg.Content, &content); err != nil {
n.logger.Printf("[ERR] raftify: error while unmarshaling vote request message: %v\n", err.Error())
break
}
n.handleVoteRequest(content)
case NewQuorumMsg:
var content NewQuorum
if err := json.Unmarshal(msg.Content, &content); err != nil {
n.logger.Printf("[ERR] raftify: error while unmarshaling new quorum message: %v\n", err.Error())
break
}
n.handleNewQuorum(content)
default:
n.logger.Printf("[WARN] raftify: received %v as follower, discarding...\n", msg.Type.toString())
}
case <-n.timeoutTimer.C:
n.logger.Println("[DEBUG] raftify: Heartbeat timeout elapsed")
n.toPreCandidate()
case <-n.events.eventCh:
n.saveState()
case <-n.shutdownCh:
n.toPreShutdown()
}
}