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 pathleader.go
120 lines (100 loc) · 3.89 KB
/
leader.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package raftify
import (
"encoding/json"
)
// toLeader initiates the transition into a leader node. Calling toLeader on a node that already is
// in the leader state just resets the data.
func (n *Node) toLeader() {
if n.state == Follower || n.state == PreCandidate {
n.logger.Println("[WARN] raftify: follower and precandidate nodes cannot directly switch to leader")
return
}
n.logger.Printf("[INFO] raftify: Entering leader state for term %v\n", n.currentTerm)
n.timeoutTimer.Stop() // Leaders have no timeout
n.startMessageTicker() // Used to periodically send out heartbeat messages
n.heartbeatIDList.reset()
n.votedFor = ""
n.state = Leader
n.sendHeartbeatToAll()
}
// runLeader runs the leader loop. This function is called within the runLoop function.
func (n *Node) runLeader() {
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 HeartbeatResponseMsg:
var content HeartbeatResponse
if err := json.Unmarshal(msg.Content, &content); err != nil {
n.logger.Printf("[ERR] raftify: error while unmarshaling heartbeat response message: %v\n", err.Error())
break
}
n.handleHeartbeatResponse(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 leader, discarding...\n", msg.Type.toString())
}
case <-n.messageTicker.C:
if !n.quorumReached(n.heartbeatIDList.received) {
n.heartbeatIDList.subQuorumCycles++
n.logger.Printf("[DEBUG] raftify: Not enough heartbeat responses for %v cycles\n", n.heartbeatIDList.subQuorumCycles)
if n.heartbeatIDList.subQuorumCycles >= MaxSubQuorumCycles {
n.logger.Println("[DEBUG] raftify: Too many cycles without reaching leader quorum, stepping down as leader...")
// Reset heartbeat and quorum counter.
n.heartbeatIDList.currentHeartbeatID = 0
n.heartbeatIDList.subQuorumCycles = 0
// Reload the config so that the memberlist from the state.json is loaded into
// the peerlist.
if err := n.loadConfig(true); err != nil {
n.logger.Printf("[ERR] raftify: %v, fall back to raftify.json\n", err.Error())
}
// Step down as a leader if too many cycles have passed without reaching quorum.
// Here, it is safe to assume it has been partitioned out into a smaller sub-cluster.
// It therefore needs to try rejoining the cluster in order to receive the latest
// memberlist in case anything has changed during its absence in the other
// sub-cluster.
n.toRejoin()
break
}
} else {
// If the quorum was reached, reset the count for how many sub quorum cycles have passed.
n.heartbeatIDList.subQuorumCycles = 0
}
n.sendHeartbeatToAll()
case <-n.events.eventCh:
n.saveState()
case <-n.shutdownCh:
n.toPreShutdown()
}
}