-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_leader.go
114 lines (103 loc) · 2.66 KB
/
state_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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
import (
"runtime"
"sync/atomic"
"time"
)
type leaderState struct {
node *node
closed int32
done chan bool
heartbeatTicker *time.Ticker
}
func newLeaderState(n *node) state {
//logger.Tracef("%s newLeaderState",node.address)
s := &leaderState{
node: n,
done: make(chan bool, 1),
heartbeatTicker: time.NewTicker(n.heartbeatTick),
}
s.Start()
go s.run()
return s
}
func (s *leaderState) Start() {
//logger.Tracef("%s leaderState.Start %s nextIndex:%d", s.node.address, s.node.address, s.node.nextIndex)
if len(s.node.peers) > 0 {
for _, peer := range s.node.peers {
peer.init()
}
}
s.node.ready = false
s.node.pipe.init(s.node.lastLogIndex)
s.node.log.cache.Reset()
s.node.leader.Store(s.node.address)
s.node.lease = true
s.node.election.Random(false)
s.node.election.Reset()
s.node.logger.Tracef("%s leaderState.Start Term:%d", s.node.address, s.node.currentTerm.Load())
go func(n *node, term uint64) {
if ok, _ := n.do(noOperationCommand, defaultCommandTimeout); ok != nil {
if n.currentTerm.Load() == term {
n.ready = true
if s.node.leaderChange != nil {
go s.node.leaderChange()
}
return
}
}
}(s.node, s.node.currentTerm.Load())
}
func (s *leaderState) Update() bool {
s.node.check()
return s.node.commit()
}
func (s *leaderState) FixedUpdate() {
if !s.node.voting() {
s.node.lease = false
s.node.stepDown(false)
s.node.logger.Tracef("%s leaderState.FixedUpdate Non-Voting", s.node.address)
} else if s.node.election.Timeout() {
s.node.lease = false
s.node.stepDown(false)
s.node.logger.Tracef("%s leaderState.FixedUpdate ElectionTimeout", s.node.address)
}
}
func (s *leaderState) String() string {
return leader
}
func (s *leaderState) StepDown() state {
s.node.logger.Tracef("%s leaderState.StepDown", s.node.address)
if atomic.CompareAndSwapInt32(&s.closed, 0, 1) {
close(s.done)
}
return newFollowerState(s.node)
}
func (s *leaderState) NextState() state {
s.node.logger.Tracef("%s leaderState.NextState", s.node.address)
if atomic.CompareAndSwapInt32(&s.closed, 0, 1) {
close(s.done)
}
return newFollowerState(s.node)
}
func (s *leaderState) run() {
for {
runtime.Gosched()
select {
case <-s.heartbeatTicker.C:
s.node.nodesMut.Lock()
votingsCount := s.node.votingsCount()
s.node.nodesMut.Unlock()
if votingsCount == 1 {
s.node.election.Reset()
} else if s.node.heartbeats() {
s.node.election.Reset()
}
case <-s.done:
s.heartbeatTicker.Stop()
return
}
}
}