-
Notifications
You must be signed in to change notification settings - Fork 542
/
tcp.go
489 lines (459 loc) · 12.5 KB
/
tcp.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transport
import (
"bytes"
"context"
"crypto/tls"
"encoding/binary"
"errors"
"hash/crc32"
"io"
"net"
"sync"
"time"
"github.com/lni/dragonboat/config"
"github.com/lni/dragonboat/internal/settings"
"github.com/lni/dragonboat/internal/utils/netutil"
"github.com/lni/dragonboat/internal/utils/syncutil"
"github.com/lni/dragonboat/raftio"
"github.com/lni/dragonboat/raftpb"
)
var (
// ErrBadMessage is the error returned to indicate the incoming message is
// corrupted.
ErrBadMessage = errors.New("invalid message")
magicNumber = [2]byte{0xAE, 0x7D}
payloadBufferSize = settings.SnapshotChunkSize + 1024*128
tlsHandshackTimeout = 10 * time.Second
magicNumberDuration = 1 * time.Second
headerDuration = 2 * time.Second
readDuration = 5 * time.Second
writeDuration = 5 * time.Second
keepAlivePeriod = 30 * time.Second
perConnBufSize = settings.Soft.PerConnectionBufferSize
recvBufSize = settings.Soft.PerCpnnectionRecvBufSize
)
const (
// TCPRaftRPCName is the name of the tcp RPC module.
TCPRaftRPCName = "go-tcp-transport"
requestHeaderSize = 14
raftType uint16 = 100
snapshotType uint16 = 200
)
type requestHeader struct {
method uint16
size uint32
crc uint32
}
func (h *requestHeader) encode(buf []byte) []byte {
if len(buf) < requestHeaderSize {
panic("input buf too small")
}
binary.BigEndian.PutUint16(buf, h.method)
binary.BigEndian.PutUint32(buf[2:], h.size)
binary.BigEndian.PutUint32(buf[6:], 0)
binary.BigEndian.PutUint32(buf[10:], h.crc)
v := crc32.ChecksumIEEE(buf[:requestHeaderSize])
binary.BigEndian.PutUint32(buf[6:], v)
return buf[:requestHeaderSize]
}
func (h *requestHeader) decode(buf []byte) bool {
if len(buf) < requestHeaderSize {
return false
}
incoming := binary.BigEndian.Uint32(buf[6:])
binary.BigEndian.PutUint32(buf[6:], 0)
expected := crc32.ChecksumIEEE(buf[:requestHeaderSize])
if incoming != expected {
plog.Errorf("header crc check failed")
return false
}
binary.BigEndian.PutUint32(buf[6:], incoming)
method := binary.BigEndian.Uint16(buf)
if method != raftType && method != snapshotType {
plog.Errorf("invalid method type")
return false
}
h.method = method
h.size = binary.BigEndian.Uint32(buf[2:])
h.crc = binary.BigEndian.Uint32(buf[10:])
return true
}
// Marshaler is the interface for types that can be Marshaled.
type Marshaler interface {
MarshalTo([]byte) (int, error)
Size() int
}
func writeMessage(conn net.Conn,
header requestHeader, buf []byte, headerBuf []byte) error {
crc := crc32.ChecksumIEEE(buf)
header.size = uint32(len(buf))
header.crc = crc
headerBuf = header.encode(headerBuf)
tt := time.Now().Add(magicNumberDuration).Add(headerDuration)
if err := conn.SetWriteDeadline(tt); err != nil {
return err
}
if _, err := conn.Write(magicNumber[:]); err != nil {
return err
}
if _, err := conn.Write(headerBuf); err != nil {
return err
}
sent := 0
bufSize := int(recvBufSize)
for sent < len(buf) {
if sent+bufSize > len(buf) {
bufSize = len(buf) - sent
}
tt = time.Now().Add(writeDuration)
if err := conn.SetWriteDeadline(tt); err != nil {
return err
}
if _, err := conn.Write(buf[sent : sent+bufSize]); err != nil {
return err
}
sent += bufSize
}
if sent != len(buf) {
plog.Panicf("sent %d, buf len %d", sent, len(buf))
}
return nil
}
func readMessage(conn net.Conn,
header []byte, rbuf []byte) (requestHeader, []byte, error) {
tt := time.Now().Add(headerDuration)
if err := conn.SetReadDeadline(tt); err != nil {
return requestHeader{}, nil, err
}
if _, err := io.ReadFull(conn, header); err != nil {
plog.Errorf("failed to get the header")
return requestHeader{}, nil, err
}
rheader := requestHeader{}
if !rheader.decode(header) {
plog.Errorf("invalid header")
return requestHeader{}, nil, ErrBadMessage
}
if rheader.size == 0 {
plog.Errorf("invalid payload length")
return requestHeader{}, nil, ErrBadMessage
}
var buf []byte
if rheader.size > uint32(len(rbuf)) {
buf = make([]byte, rheader.size)
} else {
buf = rbuf[:rheader.size]
}
received := 0
var recvBuf []byte
if rheader.size < uint32(recvBufSize) {
recvBuf = buf[:rheader.size]
} else {
recvBuf = buf[:recvBufSize]
}
toRead := rheader.size
for toRead > 0 {
tt = time.Now().Add(readDuration)
if err := conn.SetReadDeadline(tt); err != nil {
return requestHeader{}, nil, err
}
if _, err := io.ReadFull(conn, recvBuf); err != nil {
return requestHeader{}, nil, err
}
toRead -= uint32(len(recvBuf))
received += len(recvBuf)
if toRead < uint32(recvBufSize) {
recvBuf = buf[received : uint32(received)+toRead]
} else {
recvBuf = buf[received : received+int(recvBufSize)]
}
}
if uint32(received) != rheader.size {
panic("unexpected size")
}
if crc32.ChecksumIEEE(buf) != rheader.crc {
plog.Errorf("invalid payload checksum")
return requestHeader{}, nil, ErrBadMessage
}
return rheader, buf, nil
}
func readMagicNumber(conn net.Conn, magicNum []byte) error {
tt := time.Now().Add(magicNumberDuration)
if err := conn.SetReadDeadline(tt); err != nil {
return err
}
if _, err := io.ReadFull(conn, magicNum); err != nil {
return err
}
if !bytes.Equal(magicNum, magicNumber[:]) {
plog.Errorf("invalid magic number")
return ErrBadMessage
}
return nil
}
// TCPConnection is the connection used for sending raft messages to remote
// nodes.
type TCPConnection struct {
conn net.Conn
header []byte
payload []byte
}
// NewTCPConnection creates and returns a new TCPConnection instance.
func NewTCPConnection(conn net.Conn) *TCPConnection {
return &TCPConnection{
conn: conn,
header: make([]byte, requestHeaderSize),
payload: make([]byte, perConnBufSize),
}
}
// Close closes the TCPConnection instance.
func (c *TCPConnection) Close() {
if err := c.conn.Close(); err != nil {
plog.Errorf("failed to close the connection %v", err)
}
}
// SendMessageBatch sends a raft message batch to remote node.
func (c *TCPConnection) SendMessageBatch(batch raftpb.MessageBatch) error {
header := requestHeader{method: raftType}
sz := batch.SizeUpperLimit()
var buf []byte
if len(c.payload) < sz {
buf = make([]byte, sz)
} else {
buf = c.payload
}
n, err := batch.MarshalTo(buf)
if err != nil {
panic(err)
}
return writeMessage(c.conn, header, buf[:n], c.header)
}
// TCPSnapshotConnection is the connection for sending raft snapshot chunks to
// remote nodes.
type TCPSnapshotConnection struct {
conn net.Conn
header []byte
}
// NewTCPSnapshotConnection creates and returns a new snapshot connection.
func NewTCPSnapshotConnection(conn net.Conn) *TCPSnapshotConnection {
return &TCPSnapshotConnection{
conn: conn,
header: make([]byte, requestHeaderSize),
}
}
// Close closes the snapshot connection.
func (c *TCPSnapshotConnection) Close() {
if err := c.conn.Close(); err != nil {
plog.Errorf("failed to close the snapshot connection %v", err)
}
}
// SendSnapshotChunk sends the specified snapshot chunk to remote node.
func (c *TCPSnapshotConnection) SendSnapshotChunk(chunk raftpb.SnapshotChunk) error {
header := requestHeader{method: snapshotType}
sz := chunk.Size()
buf := make([]byte, sz)
n, err := chunk.MarshalTo(buf)
if err != nil {
panic(err)
}
return writeMessage(c.conn, header, buf[:n], c.header)
}
// TCPTransport is a TCP based RPC module for exchanging raft messages and
// snapshots between NodeHost instances.
type TCPTransport struct {
nhConfig config.NodeHostConfig
stopper *syncutil.Stopper
requestHandler raftio.RequestHandler
sinkFactory raftio.ChunkSinkFactory
}
// NewTCPTransport creates and returns a new TCP transport module.
func NewTCPTransport(nhConfig config.NodeHostConfig,
requestHandler raftio.RequestHandler,
sinkFactory raftio.ChunkSinkFactory) raftio.IRaftRPC {
plog.Infof("Using the default TCP RPC, switch to gRPC based RPC module " +
"(github.com/lni/dragonboat/plugin/rpc) for HTTP2 based transport")
return &TCPTransport{
nhConfig: nhConfig,
stopper: syncutil.NewStopper(),
requestHandler: requestHandler,
sinkFactory: sinkFactory,
}
}
// Start starts the TCP transport module.
func (g *TCPTransport) Start() error {
address := g.nhConfig.RaftAddress
tlsConfig, err := g.nhConfig.GetServerTLSConfig()
if err != nil {
return err
}
listener, err := netutil.NewStoppableListener(address,
tlsConfig, g.stopper.ShouldStop())
if err != nil {
plog.Panicf("failed to new a stoppable listener, %v", err)
}
g.stopper.RunWorker(func() {
for {
conn, err := listener.Accept()
if err != nil {
if err == netutil.ErrListenerStopped {
return
}
panic(err)
}
var once sync.Once
closeFn := func() {
once.Do(func() {
if err := conn.Close(); err != nil {
plog.Errorf("failed to close the connection %v", err)
}
})
}
g.stopper.RunWorker(func() {
<-g.stopper.ShouldStop()
closeFn()
})
g.stopper.RunWorker(func() {
g.serveConn(conn)
closeFn()
})
}
})
return nil
}
// Stop stops the TCP transport module.
func (g *TCPTransport) Stop() {
g.stopper.Stop()
}
// GetConnection returns a new raftio.IConnection for sending raft messages.
func (g *TCPTransport) GetConnection(ctx context.Context,
target string) (raftio.IConnection, error) {
conn, err := g.getConnection(ctx, target)
if err != nil {
return nil, err
}
return NewTCPConnection(conn), nil
}
// GetSnapshotConnection returns a new raftio.IConnection for sending raft
// snapshots.
func (g *TCPTransport) GetSnapshotConnection(ctx context.Context,
target string) (raftio.ISnapshotConnection, error) {
conn, err := g.getConnection(ctx, target)
if err != nil {
return nil, err
}
return NewTCPSnapshotConnection(conn), nil
}
// Name returns a human readable name of the TCP transport module.
func (g *TCPTransport) Name() string {
return TCPRaftRPCName
}
func (g *TCPTransport) serveConn(conn net.Conn) {
magicNum := make([]byte, len(magicNumber))
header := make([]byte, requestHeaderSize)
tbuf := make([]byte, payloadBufferSize)
var chunks raftio.IChunkSink
stopper := syncutil.NewStopper()
defer func() {
if chunks != nil {
chunks.Close()
}
}()
defer stopper.Stop()
for {
err := readMagicNumber(conn, magicNum)
if err != nil {
if err == ErrBadMessage {
return
}
operr, ok := err.(net.Error)
if ok && operr.Timeout() {
continue
} else {
return
}
}
rheader, buf, err := readMessage(conn, header, tbuf)
if err != nil {
return
}
if rheader.method == raftType {
batch := raftpb.MessageBatch{}
if err := batch.Unmarshal(buf); err != nil {
return
}
g.requestHandler(batch)
} else {
chunk := raftpb.SnapshotChunk{}
if err := chunk.Unmarshal(buf); err != nil {
return
}
if chunks == nil {
chunks = g.sinkFactory()
stopper.RunWorker(func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
chunks.Tick()
case <-stopper.ShouldStop():
return
}
}
})
}
chunks.AddChunk(chunk)
}
}
}
func setTCPConn(conn *net.TCPConn) error {
if err := conn.SetLinger(0); err != nil {
return err
}
if err := conn.SetKeepAlive(true); err != nil {
return err
}
return conn.SetKeepAlivePeriod(keepAlivePeriod)
}
func (g *TCPTransport) getConnection(ctx context.Context,
target string) (net.Conn, error) {
timeout := time.Duration(getDialTimeoutSecond()) * time.Second
conn, err := net.DialTimeout("tcp", target, timeout)
if err != nil {
return nil, err
}
tcpconn, ok := conn.(*net.TCPConn)
if ok {
if err := setTCPConn(tcpconn); err != nil {
return nil, err
}
}
tlsConfig, err := g.nhConfig.GetClientTLSConfig(target)
if err != nil {
return nil, err
}
if tlsConfig != nil {
conn = tls.Client(conn, tlsConfig)
tt := time.Now().Add(tlsHandshackTimeout)
if err := conn.SetDeadline(tt); err != nil {
return nil, err
}
if err := conn.(*tls.Conn).Handshake(); err != nil {
return nil, err
}
}
return conn, nil
}