-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelection.go
More file actions
410 lines (350 loc) · 9.08 KB
/
Copy pathelection.go
File metadata and controls
410 lines (350 loc) · 9.08 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
package dmt implements leader election functionality for the distributed radix tree.
It uses a Raft-like consensus algorithm to maintain a consistent leader across the
network and handle leader failures gracefully.
*/
package dmt
import (
"math/rand"
"sync"
"time"
)
/*
NodeState represents the current state of a node in the election process.
A node can be in one of three states: Follower, Candidate, or Leader,
following the Raft consensus algorithm.
*/
type NodeState int
const (
Follower NodeState = iota
Candidate
Leader
)
/*
ElectionConfig holds configuration for leader election.
It defines timeouts, intervals, and quorum requirements that control
the behavior of the election process.
*/
type ElectionConfig struct {
// Base timeout for elections (will be randomized)
ElectionTimeout time.Duration
// How often to send heartbeats when leader
HeartbeatInterval time.Duration
// Minimum number of nodes needed for election
QuorumSize int
}
/*
Election manages the leader election process.
It implements a Raft-like consensus algorithm to maintain a consistent
leader across the distributed system, handling state transitions,
vote counting, and heartbeat mechanisms.
*/
type Election struct {
config ElectionConfig
node *NetworkNode
// Election state
state NodeState
term uint64
votedFor string
stateLock sync.RWMutex
// Log tracking
lastLogTerm uint64
lastLogIndex uint64
logLock sync.RWMutex
// Election timers
electionTimer *time.Timer
heartbeatTimer *time.Timer
// Control channels
votes chan string
shutdown chan struct{}
}
/*
NewElection creates a new election manager.
It initializes the election state machine with the provided configuration
and network node, starting in the Follower state.
*/
func NewElection(config ElectionConfig, node *NetworkNode) *Election {
e := &Election{
config: config,
node: node,
state: Follower,
votes: make(chan string, 100),
shutdown: make(chan struct{}),
}
// Start election management
go e.run()
return e
}
/*
run manages the election state machine.
It handles timer events, vote processing, and heartbeat sending
in a continuous loop until shutdown.
*/
func (e *Election) run() {
e.resetElectionTimer()
for {
select {
case <-e.shutdown:
return
case <-e.electionTimer.C:
e.startElection()
case voter := <-e.votes:
e.handleVote(voter)
case <-e.heartbeatTimer.C:
if e.getState() == Leader {
e.sendHeartbeats()
}
}
}
}
/*
startElection initiates a new election.
It transitions the node to Candidate state, increments the term,
and requests votes from all peers. If sufficient votes are received,
the node becomes the leader.
*/
func (e *Election) startElection() {
e.stateLock.Lock()
e.state = Candidate
e.term++
e.votedFor = e.node.config.NodeID
currentTerm := e.term
e.stateLock.Unlock()
// Update metrics
e.node.metrics.SetLeader(false)
// Request votes from all peers
e.node.peersMutex.RLock()
peers := make([]*peer, 0, len(e.node.peers))
for _, p := range e.node.peers {
peers = append(peers, p)
}
e.node.peersMutex.RUnlock()
// Track votes received (including self-vote)
votesReceived := 1
votesNeeded := (len(peers) / 2) + 1
// Request votes from all peers
for _, p := range peers {
go func(peer *peer) {
future, release := peer.client.RequestVote(e.node.ctx, func(p RadixRPC_requestVote_Params) error {
p.SetTerm(currentTerm)
p.SetCandidateId(e.node.config.NodeID)
p.SetLastLogIndex(uint64(len(e.node.merkleTree.Root.Hash)))
return nil
})
defer release()
result, err := future.Struct()
if err != nil {
return
}
if result.VoteGranted() {
e.votes <- peer.addr
}
}(p)
}
// Wait for votes or timeout
timeout := time.After(e.config.ElectionTimeout)
for votesReceived < votesNeeded {
select {
case <-e.votes:
votesReceived++
case <-timeout:
return
case <-e.shutdown:
return
}
}
// Won election
if votesReceived >= votesNeeded {
e.becomeLeader()
}
}
/*
becomeLeader transitions the node to leader state.
It updates the node's state to Leader, starts the heartbeat timer,
and updates relevant metrics.
*/
func (e *Election) becomeLeader() {
e.stateLock.Lock()
e.state = Leader
e.stateLock.Unlock()
// Update metrics
e.node.metrics.SetLeader(true)
// Start heartbeat timer
e.heartbeatTimer = time.NewTimer(e.config.HeartbeatInterval)
}
/*
sendHeartbeats sends heartbeat messages to all peers.
Leaders periodically send heartbeats to maintain their authority
and prevent new elections from being started.
*/
func (e *Election) sendHeartbeats() {
e.node.peersMutex.RLock()
peers := make([]*peer, 0, len(e.node.peers))
for _, p := range e.node.peers {
peers = append(peers, p)
}
e.node.peersMutex.RUnlock()
for _, p := range peers {
go func(peer *peer) {
future, release := peer.client.Heartbeat(e.node.ctx, func(p RadixRPC_heartbeat_Params) error {
p.SetTerm(e.term)
p.SetLeaderId(e.node.config.NodeID)
return nil
})
defer release()
result, err := future.Struct()
if err != nil {
return
}
// Step down if peer has higher term
if result.Term() > e.term {
e.stepDown(result.Term())
}
}(p)
}
// Reset heartbeat timer
e.heartbeatTimer.Reset(e.config.HeartbeatInterval)
}
/*
stepDown steps down from leader/candidate to follower.
This occurs when a node discovers a higher term or when it needs
to relinquish leadership for other reasons.
*/
func (e *Election) stepDown(newTerm uint64) {
e.stateLock.Lock()
e.state = Follower
e.term = newTerm
e.votedFor = ""
e.stateLock.Unlock()
// Update metrics
e.node.metrics.SetLeader(false)
// Reset election timer
e.resetElectionTimer()
}
/*
resetElectionTimer resets the election timeout with random jitter.
The randomization helps prevent split votes by ensuring nodes don't
all timeout at the same time.
*/
func (e *Election) resetElectionTimer() {
if e.electionTimer != nil {
e.electionTimer.Stop()
}
// Add random jitter to election timeout
jitter := time.Duration(rand.Int63n(int64(e.config.ElectionTimeout)))
timeout := e.config.ElectionTimeout + jitter
e.electionTimer = time.NewTimer(timeout)
}
/*
getState returns the current node state.
It provides thread-safe access to the node's current role in the
election process.
*/
func (e *Election) getState() NodeState {
e.stateLock.RLock()
defer e.stateLock.RUnlock()
return e.state
}
/*
handleVote processes a vote received from a peer.
It updates voting metrics and counts votes during an election,
but only if the node is still a candidate.
*/
func (e *Election) handleVote(voter string) {
e.stateLock.Lock()
defer e.stateLock.Unlock()
// Only count votes if still a candidate
if e.state != Candidate {
return
}
// Record vote in metrics
e.node.metrics.RecordVote(voter)
}
/*
handleVoteRequest processes a vote request from a candidate.
It implements the Raft voting rules, checking term numbers and log
indices to decide whether to grant the vote.
*/
func (e *Election) handleVoteRequest(term uint64, candidateId string, lastLogIndex uint64, lastLogTerm uint64) bool {
e.stateLock.Lock()
defer e.stateLock.Unlock()
// Step down if term is newer
if term > e.term {
e.stepDown(term)
}
// Check term and whether we've voted this term
if term < e.term || (e.votedFor != "" && e.votedFor != candidateId) {
return false
}
// Check if candidate's log is at least as up-to-date as ours
e.logLock.RLock()
logOK := lastLogTerm > e.lastLogTerm ||
(lastLogTerm == e.lastLogTerm && lastLogIndex >= e.lastLogIndex)
e.logLock.RUnlock()
if !logOK {
return false
}
// Grant vote
e.votedFor = candidateId
e.resetElectionTimer()
return true
}
/*
handleHeartbeat processes a heartbeat from the leader.
It updates terms if necessary, resets election timeouts, and
maintains the leader-follower relationship in the cluster.
*/
func (e *Election) handleHeartbeat(term uint64, leaderId string) bool {
e.stateLock.Lock()
defer e.stateLock.Unlock()
// Step down if term is higher
if term > e.term {
e.stepDown(term)
// Update metrics with new leader
e.node.metrics.SetNodeRole("follower", 0.0)
return true
}
// Reject if term is lower
if term < e.term {
return false
}
// Accept heartbeat if term matches and from valid leader
if e.state != Leader && leaderId != "" {
e.resetElectionTimer()
// Update metrics
e.node.metrics.termNumber.Store(term)
e.node.metrics.SetNodeRole("follower", 0.0)
return true
}
return false
}
/*
Close shuts down the election manager.
It signals the run loop to stop and cleans up resources.
*/
func (e *Election) Close() {
close(e.shutdown)
}
/*
Update log state when applying new entries
*/
func (e *Election) updateLogState(index uint64, term uint64) {
e.logLock.Lock()
defer e.logLock.Unlock()
if index > e.lastLogIndex {
e.lastLogIndex = index
e.lastLogTerm = term
}
}
// getCurrentTerm returns the current term number
func (e *Election) getCurrentTerm() uint64 {
e.stateLock.RLock()
defer e.stateLock.RUnlock()
return e.term
}
// getLastLogIndex returns the index of the last log entry
func (e *Election) getLastLogIndex() uint64 {
e.logLock.RLock()
defer e.logLock.RUnlock()
return e.lastLogIndex
}