forked from Jille/raft-grpc-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raftapi.go
303 lines (276 loc) · 8.05 KB
/
raftapi.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
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
package transport
import (
"context"
pb "github.com/Jille/raft-grpc-transport/proto"
"github.com/hashicorp/raft"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io"
"log"
"sync"
"time"
)
// These are calls from the Raft engine that we need to send out over gRPC.
type raftAPI struct {
manager *Manager
}
type conn struct {
clientConn *grpc.ClientConn
client pb.RaftTransportClient
mtx sync.Mutex
}
// Consumer returns a channel that can be used to consume and respond to RPC requests.
func (r raftAPI) Consumer() <-chan raft.RPC {
return r.manager.rpcChan
}
// LocalAddr is used to return our local address to distinguish from our peers.
func (r raftAPI) LocalAddr() raft.ServerAddress {
return r.manager.localAddress
}
func (r raftAPI) getPeer(id raft.ServerID, target raft.ServerAddress) (pb.RaftTransportClient, error) {
r.manager.connectionsMtx.Lock()
c, ok := r.manager.connections[id]
if !ok {
c = &conn{}
c.mtx.Lock()
r.manager.connections[id] = c
}
r.manager.connectionsMtx.Unlock()
if ok {
c.mtx.Lock()
}
defer c.mtx.Unlock()
if c.clientConn == nil {
conn, err := grpc.Dial(string(target), r.manager.dialOptions...)
if err != nil {
return nil, err
}
c.clientConn = conn
c.client = pb.NewRaftTransportClient(conn)
}
return c.client, nil
}
// AppendEntries sends the appropriate RPC to the target node.
func (r raftAPI) AppendEntries(id raft.ServerID, target raft.ServerAddress, args *raft.AppendEntriesRequest, resp *raft.AppendEntriesResponse) error {
c, err := r.getPeer(id, target)
if err != nil {
return err
}
ctx := context.TODO()
if r.manager.heartbeatTimeout > 0 && isHeartbeat(args) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.manager.heartbeatTimeout)
defer cancel()
}
ret, err := c.AppendEntries(ctx, encodeAppendEntriesRequest(args))
if err != nil {
r.maybeCloseConn(id, err)
return err
}
*resp = *decodeAppendEntriesResponse(ret)
return nil
}
// RequestVote sends the appropriate RPC to the target node.
func (r raftAPI) RequestVote(id raft.ServerID, target raft.ServerAddress, args *raft.RequestVoteRequest, resp *raft.RequestVoteResponse) error {
c, err := r.getPeer(id, target)
if err != nil {
return err
}
ret, err := c.RequestVote(context.TODO(), encodeRequestVoteRequest(args))
if err != nil {
r.maybeCloseConn(id, err)
return err
}
*resp = *decodeRequestVoteResponse(ret)
return nil
}
// TimeoutNow is used to start a leadership transfer to the target node.
func (r raftAPI) TimeoutNow(id raft.ServerID, target raft.ServerAddress, args *raft.TimeoutNowRequest, resp *raft.TimeoutNowResponse) error {
c, err := r.getPeer(id, target)
if err != nil {
return err
}
ret, err := c.TimeoutNow(context.TODO(), encodeTimeoutNowRequest(args))
if err != nil {
r.maybeCloseConn(id, err)
return err
}
*resp = *decodeTimeoutNowResponse(ret)
return nil
}
// InstallSnapshot is used to push a snapshot down to a follower. The data is read from
// the ReadCloser and streamed to the client.
func (r raftAPI) InstallSnapshot(id raft.ServerID, target raft.ServerAddress, req *raft.InstallSnapshotRequest, resp *raft.InstallSnapshotResponse, data io.Reader) error {
c, err := r.getPeer(id, target)
if err != nil {
return err
}
stream, err := c.InstallSnapshot(context.TODO())
if err != nil {
r.maybeCloseConn(id, err)
return err
}
if err := stream.Send(encodeInstallSnapshotRequest(req)); err != nil {
r.maybeCloseConn(id, err)
return err
}
var buf [16384]byte
for {
n, err := data.Read(buf[:])
if err == io.EOF || (err == nil && n == 0) {
break
}
if err != nil {
r.maybeCloseConn(id, err)
return err
}
if err := stream.Send(&pb.InstallSnapshotRequest{
Data: buf[:n],
}); err != nil {
r.maybeCloseConn(id, err)
return err
}
}
ret, err := stream.CloseAndRecv()
if err != nil {
r.maybeCloseConn(id, err)
return err
}
*resp = *decodeInstallSnapshotResponse(ret)
return nil
}
// AppendEntriesPipeline returns an interface that can be used to pipeline
// AppendEntries requests.
func (r raftAPI) AppendEntriesPipeline(id raft.ServerID, target raft.ServerAddress) (raft.AppendPipeline, error) {
c, err := r.getPeer(id, target)
if err != nil {
return nil, err
}
ctx := context.TODO()
ctx, cancel := context.WithCancel(ctx)
stream, err := c.AppendEntriesPipeline(ctx)
if err != nil {
cancel()
r.maybeCloseConn(id, err)
return nil, err
}
rpa := raftPipelineAPI{
stream: stream,
cancel: cancel,
inflightCh: make(chan *appendFuture, 20),
doneCh: make(chan raft.AppendFuture, 20),
}
go rpa.receiver()
return rpa, nil
}
type raftPipelineAPI struct {
stream pb.RaftTransport_AppendEntriesPipelineClient
cancel func()
inflightChMtx sync.Mutex
inflightCh chan *appendFuture
doneCh chan raft.AppendFuture
}
// AppendEntries is used to add another request to the pipeline.
// The send may block which is an effective form of back-pressure.
func (r raftPipelineAPI) AppendEntries(req *raft.AppendEntriesRequest, resp *raft.AppendEntriesResponse) (raft.AppendFuture, error) {
af := &appendFuture{
start: time.Now(),
request: req,
done: make(chan struct{}),
}
if err := r.stream.Send(encodeAppendEntriesRequest(req)); err != nil {
return nil, err
}
r.inflightChMtx.Lock()
select {
case <-r.stream.Context().Done():
default:
r.inflightCh <- af
}
r.inflightChMtx.Unlock()
return af, nil
}
// Consumer returns a channel that can be used to consume
// response futures when they are ready.
func (r raftPipelineAPI) Consumer() <-chan raft.AppendFuture {
return r.doneCh
}
// Close closes the pipeline and cancels all inflight RPCs
func (r raftPipelineAPI) Close() error {
r.cancel()
r.inflightChMtx.Lock()
close(r.inflightCh)
r.inflightChMtx.Unlock()
return nil
}
func (r raftPipelineAPI) receiver() {
for af := range r.inflightCh {
msg, err := r.stream.Recv()
if err != nil {
af.err = err
} else {
af.response = *decodeAppendEntriesResponse(msg)
}
close(af.done)
r.doneCh <- af
}
}
type appendFuture struct {
raft.AppendFuture
start time.Time
request *raft.AppendEntriesRequest
response raft.AppendEntriesResponse
err error
done chan struct{}
}
// Error blocks until the future arrives and then
// returns the error status of the future.
// This may be called any number of times - all
// calls will return the same value.
// Note that it is not OK to call this method
// twice concurrently on the same Future instance.
func (f *appendFuture) Error() error {
<-f.done
return f.err
}
// Start returns the time that the append request was started.
// It is always OK to call this method.
func (f *appendFuture) Start() time.Time {
return f.start
}
// Request holds the parameters of the AppendEntries call.
// It is always OK to call this method.
func (f *appendFuture) Request() *raft.AppendEntriesRequest {
return f.request
}
// Response holds the results of the AppendEntries call.
// This method must only be called after the Error
// method returns, and will only be valid on success.
func (f *appendFuture) Response() *raft.AppendEntriesResponse {
return &f.response
}
// EncodePeer is used to serialize a peer's address.
func (r raftAPI) EncodePeer(id raft.ServerID, addr raft.ServerAddress) []byte {
return []byte(addr)
}
// DecodePeer is used to deserialize a peer's address.
func (r raftAPI) DecodePeer(p []byte) raft.ServerAddress {
return raft.ServerAddress(p)
}
// SetHeartbeatHandler is used to setup a heartbeat handler
// as a fast-pass. This is to avoid head-of-line blocking from
// disk IO. If a Transport does not support this, it can simply
// ignore the call, and push the heartbeat onto the Consumer channel.
func (r raftAPI) SetHeartbeatHandler(cb func(rpc raft.RPC)) {
r.manager.heartbeatFuncMtx.Lock()
r.manager.heartbeatFunc = cb
r.manager.heartbeatFuncMtx.Unlock()
}
func (r raftAPI) maybeCloseConn(serverID raft.ServerID, err error) {
st, _ := status.FromError(err)
if st.Code() == codes.Unavailable {
log.Printf("raft peer connection unavailable, close [%s] ", serverID)
r.manager.CloseConn(serverID)
}
}