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 pathtypes.go
128 lines (109 loc) · 4.13 KB
/
types.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
121
122
123
124
125
126
127
128
package raftify
// State is a custom type for all valid raftify node states.
type State uint8
// Constants for valid node states.
const (
// Bootstrap is the state a node is in if it's waiting for the expected number of
// nodes to go online before starting the Raft leader election.
Bootstrap State = iota
// Rejoin is the state a node is in if it times out or crashes and restarts.
// In this state, it attempts to rejoin the existing cluster it dropped out of.
Rejoin
// Followers reset their timeout if they receive a heartbeat message from a leader.
// If the timeout elapses, they become a precandidate.
Follower
// PreCandidates start collecting prevotes in order to determine if any other cluster
// member has seen a leader and therefore make sure that there truly isn't one anymore
// and a new one needs to be elected. Once the majority of prevotes have been granted,
// it becomes a candidate.
PreCandidate
// Candidates enter a new election term and start collecting votes in order to be
// promoted to the new cluster leader. A candidate votes for itself and waits for other
// nodes to respond to its vote request. Sometimes a split vote can happen which means
// that there are multiple candidates trying to become leader simultaneously such that
// there are not enough votes left to reach quorum. In that case, the nodes wait for
// the next timeout to start a new term. Once the majority of votes have been granted,
// it becomes a leader.
Candidate
// The leader periodically sends out heartbeats to its followers to signal its availability.
// If it suffers any sort of failure it automatically restarts as a follower. If it's
// partitioned out and doesn't receive the majority of heartbeat responses it steps down.
Leader
// PreShutdown is the state in which a node prepares its own voluntary leave by notifying the
// rest of the cluster of an immediate quorum change.
PreShutdown
// Shutdown is the state in which a node initiates a shutdown and gracefully allows the
// runLoop goroutine to be exited and killed.
Shutdown
)
// toString returns the string representation of a node state.
func (s *State) toString() string {
switch *s {
case Bootstrap:
return "Bootstrap"
case Rejoin:
return "Rejoin"
case Follower:
return "Follower"
case PreCandidate:
return "PreCandidate"
case Candidate:
return "Candidate"
case Leader:
return "Leader"
case PreShutdown:
return "PreShutdown"
case Shutdown:
return "Shutdown"
default:
return "unknown"
}
}
// MessageType is a custom type for all valid raftify messages.
type MessageType uint8
// Constants for valid messages.
const (
// A heartbeat message is sent out by a leader to signal availability.
HeartbeatMsg MessageType = iota
// A heartbeat response message is sent by the node who received the
// heartbeat to the leader it originated from.
HeartbeatResponseMsg
// A prevote request message is sent out by aprecandidate in order to
// make sure there truly is no more leader and a new candidacy needs to
// be initiated.
PreVoteRequestMsg
// A prevote response message is sent by the node who received the
// prevote request to the precandidate it originated from.
PreVoteResponseMsg
// A vote request message is sent out by a candidate in order to become
// the new cluster leader.
VoteRequestMsg
// A vote response message is sent by the node who received the vote
// request to the candidate it originated from.
VoteResponseMsg
// A new quorum message is sent out by a voluntarily leaving node. It triggers
// an immediate quorum change instead of having to wait for the cluster to
// detect and kick the dead node eventually.
NewQuorumMsg
)
// toString returns the string representation of a message type.
func (t *MessageType) toString() string {
switch *t {
case HeartbeatMsg:
return "HeartbeatMsg"
case HeartbeatResponseMsg:
return "HeartbeatResponseMsg"
case PreVoteRequestMsg:
return "PreVoteRequestMsg"
case PreVoteResponseMsg:
return "PreVoteResponseMsg"
case VoteRequestMsg:
return "VoteRequestMsg"
case VoteResponseMsg:
return "VoteResponseMsg"
case NewQuorumMsg:
return "NewQuorumMsg"
default:
return "unknown"
}
}