/
node.go
173 lines (152 loc) · 3.8 KB
/
node.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
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
package raft
import (
"context"
"sync"
"go.uber.org/zap"
"github.com/ulysseses/raft/pb"
)
// Node is a Raft node that interacts with an Application state machine and network.
type Node struct {
psm *ProtocolStateMachine
tr Transport
applied uint64
applyFunc func([]pb.Entry) error
stopAppChan chan struct{}
stopAppErrChan chan error
nowUnixNanoFunc func() int64
logger *zap.Logger
_padding0 [64]byte
proposeMu sync.Mutex
_padding1 [64]byte
readMu sync.Mutex
_padding2 [64]byte
}
// Propose should be called by the client/application. This method proposes data to the raft log.
// If error is non-nil, index and term should be used to check if the proposed entry was committed.
func (n *Node) Propose(ctx context.Context, data []byte) (index uint64, term uint64, err error) {
n.proposeMu.Lock()
done := ctx.Done()
// drain
select {
case <-n.psm.propRespChan:
case <-done:
n.proposeMu.Unlock()
return 0, 0, ctx.Err()
default:
}
// send proposal request
select {
case n.psm.propReqChan <- proposalRequest{data: data}:
case <-done:
n.proposeMu.Unlock()
return 0, 0, ctx.Err()
}
// get proposal response
select {
case resp := <-n.psm.propRespChan:
n.proposeMu.Unlock()
return resp.index, resp.term, nil
case <-done:
n.proposeMu.Unlock()
return 0, 0, ctx.Err()
}
}
// Read should be called by the client/application.
func (n *Node) Read(ctx context.Context) error {
if n.psm.state.Consistency == ConsistencyStale {
return nil
}
readIndex, err := n.read(ctx, n.nowUnixNanoFunc())
if err != nil {
return err
}
return n.applyTo(ctx, readIndex)
}
// State returns the latest known state of the Raft node.
func (n *Node) State() State {
n.psm.stateReqChan <- stateReq{}
return <-n.psm.stateRespChan
}
// Members returns the latest member states.
func (n *Node) Members() map[uint64]MemberState {
n.psm.membersReqChan <- membersRequest{}
return <-n.psm.membersRespChan
}
// read sends a read-only request to the raft cluster. It returns the committed index that the
// application needs to catch up to.
// ConsistencySerializable: No read request is sent to the raft cluster. read is immediately
// acknowledged.
// ConsistencyLinearizable: A read request is sent to each raft node. read is acknowledged once
// a quorum has acknowledged the read request.
func (n *Node) read(ctx context.Context, unixNano int64) (uint64, error) {
n.readMu.Lock()
done := ctx.Done()
// drain
select {
case <-n.psm.readRespChan:
case <-done:
n.readMu.Unlock()
return 0, ctx.Err()
default:
}
// send read request
select {
case n.psm.readReqChan <- readRequest{unixNano: unixNano}:
case <-done:
n.readMu.Unlock()
return 0, ctx.Err()
}
// wait for response
select {
case resp := <-n.psm.readRespChan:
n.readMu.Unlock()
return resp.index, resp.err
case <-done:
n.readMu.Unlock()
return 0, ctx.Err()
}
}
func (n *Node) applyTo(ctx context.Context, index uint64) error {
n.psm.log.Lock()
err := n.applyFunc(n.psm.log.entries(n.applied+1, index))
if err == nil {
n.applied = index
}
n.psm.log.Unlock()
return err
}
func (n *Node) runApplication() error {
var commitChan <-chan uint64 = n.psm.commitChan
for {
select {
case <-n.stopAppChan:
return nil
case commit := <-commitChan:
if err := n.applyTo(context.Background(), commit); err != nil {
return err
}
}
}
}
// Start starts the Raft node.
func (n *Node) Start() {
n.tr.start()
n.psm.start()
go func() {
err := n.runApplication()
if err != nil && n.l() {
n.logger.Error("runApplication ended with error", zap.Error(err))
}
n.stopAppErrChan <- err
}()
}
// Stop stops the Raft node.
func (n *Node) Stop() error {
n.psm.stop()
n.tr.stop()
n.stopAppChan <- struct{}{}
return <-n.stopAppErrChan
}
func (n *Node) l() bool {
return n.logger != nil
}