forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
583 lines (499 loc) · 15.7 KB
/
packet.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Copyright 2018 The CubeFS Authors.
//
// 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 repl
import (
"fmt"
"io"
"net"
"strings"
"time"
"github.com/cubefs/cubefs/depends/tiglabs/raft"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/storage"
"github.com/cubefs/cubefs/util"
"github.com/cubefs/cubefs/util/errors"
"github.com/cubefs/cubefs/util/exporter"
"github.com/cubefs/cubefs/util/log"
)
var (
ErrBadNodes = errors.New("BadNodesErr")
ErrArgLenMismatch = errors.New("ArgLenMismatchErr")
)
type PacketInterface interface {
IsErrPacket() bool
WriteToConn(c net.Conn) (err error)
ReadFromConnWithVer(c net.Conn, timeoutSec int) (err error)
GetUniqueLogId() (m string)
GetReqID() int64
GetPartitionID() uint64
GetExtentID() uint64
GetSize() uint32
GetCRC() uint32
GetArg() []byte
GetArgLen() uint32
GetData() []byte
GetResultCode() uint8
GetExtentOffset() int64
GetStartT() int64
SetSize(size uint32)
GetOpcode() uint8
SetResultCode(uint8)
SetCRC(crc uint32)
SetExtentOffset(int64)
GetOpMsg() (m string)
ShallDegrade() bool
SetStartT(StartT int64)
SetData(data []byte)
SetOpCode(uint8)
LogMessage(action, remote string, start int64, err error) (m string)
PackErrorBody(action, msg string)
PacketOkReply()
SetArglen(len uint32)
SetArg(data []byte)
}
type (
NewPacketFunc func() (p PacketInterface)
Packet struct {
proto.Packet
followersAddrs []string
followerPackets []*FollowerPacket
IsReleased int32 // TODO what is released?
Object interface{}
TpObject *exporter.TimePointCount
NeedReply bool
OrgBuffer []byte
// used locally
shallDegrade bool
AfterPre bool
}
)
type FollowerPacket struct {
proto.Packet
respCh chan error
}
func NewFollowerPacket() (fp *FollowerPacket) {
fp = new(FollowerPacket)
fp.respCh = make(chan error, 1)
fp.StartT = time.Now().UnixNano()
return fp
}
func (p *FollowerPacket) PackErrorBody(action, msg string) {
p.identificationErrorResultCode(action, msg)
p.Size = uint32(len([]byte(action + "_" + msg)))
p.Data = make([]byte, p.Size)
copy(p.Data[:int(p.Size)], []byte(action+"_"+msg))
}
func (p *FollowerPacket) IsErrPacket() bool {
return p.ResultCode != proto.OpOk && p.ResultCode != proto.OpInitResultCode
}
func (p *FollowerPacket) identificationErrorResultCode(errLog string, errMsg string) {
if strings.Contains(errMsg, storage.ParameterMismatchError.Error()) ||
strings.Contains(errMsg, ErrorUnknownOp.Error()) {
p.ResultCode = proto.OpArgMismatchErr
} else if strings.Contains(errMsg, proto.ErrDataPartitionNotExists.Error()) {
p.ResultCode = proto.OpTryOtherAddr
} else if strings.Contains(errMsg, storage.ExtentNotFoundError.Error()) ||
strings.Contains(errMsg, storage.ExtentHasBeenDeletedError.Error()) {
p.ResultCode = proto.OpNotExistErr
} else if strings.Contains(errMsg, storage.NoSpaceError.Error()) {
p.ResultCode = proto.OpDiskNoSpaceErr
} else if strings.Contains(errMsg, storage.LimitedIoError.Error()) {
p.ResultCode = proto.OpLimitedIoErr
} else if strings.Contains(errMsg, storage.TryAgainError.Error()) {
p.ResultCode = proto.OpAgain
} else if strings.Contains(errMsg, raft.ErrNotLeader.Error()) {
p.ResultCode = proto.OpTryOtherAddr
} else if strings.Contains(errMsg, raft.ErrStopped.Error()) {
p.ResultCode = proto.OpTryOtherAddr
} else if strings.Contains(errLog, ActionReceiveFromFollower) || strings.Contains(errLog, ActionSendToFollowers) ||
strings.Contains(errLog, ConnIsNullErr) {
p.ResultCode = proto.OpIntraGroupNetErr
log.LogErrorf("action[identificationErrorResultCode] error %v, errmsg %v", errLog, errMsg)
} else {
log.LogErrorf("action[identificationErrorResultCode] error %v, errmsg %v", errLog, errMsg)
p.ResultCode = proto.OpIntraGroupNetErr
}
}
func (p *Packet) AfterTp() (ok bool) {
if p.TpObject != nil {
p.TpObject.Set(nil)
}
return
}
func (p *Packet) clean() {
if p.Data == nil && p.OrgBuffer == nil {
return
}
p.Object = nil
p.TpObject = nil
p.Data = nil
p.Arg = nil
if p.OrgBuffer != nil && len(p.OrgBuffer) == util.BlockSize && p.IsNormalWriteOperation() {
proto.Buffers.Put(p.OrgBuffer)
p.OrgBuffer = nil
}
}
func copyPacket(src *Packet, dst *FollowerPacket) {
dst.Magic = src.Magic
dst.ExtentType = src.ExtentType
dst.Opcode = src.Opcode
dst.ResultCode = src.ResultCode
dst.CRC = src.CRC
dst.Size = src.Size
dst.KernelOffset = src.KernelOffset
dst.PartitionID = src.PartitionID
dst.ExtentID = src.ExtentID
dst.ExtentOffset = src.ExtentOffset
dst.ReqID = src.ReqID
dst.Data = src.OrgBuffer
}
func (p *Packet) BeforeTp(clusterID string) (ok bool) {
if p.IsForwardPkt() && !p.IsRandomWrite() {
p.TpObject = exporter.NewTPCnt(fmt.Sprintf("PrimaryBackUp_%v", p.GetOpMsg()))
} else if p.IsRandomWrite() {
p.TpObject = exporter.NewTPCnt(fmt.Sprintf("Raft_%v", p.GetOpMsg()))
}
return
}
func (p *Packet) resolveFollowersAddr() (err error) {
defer func() {
if err != nil {
p.PackErrorBody(ActionPreparePkt, err.Error())
}
}()
if len(p.Arg) < int(p.ArgLen) {
err = ErrArgLenMismatch
return
}
str := string(p.Arg[:int(p.ArgLen)])
followerAddrs := strings.SplitN(str, proto.AddrSplit, -1)
followerNum := uint8(len(followerAddrs) - 1)
p.followersAddrs = make([]string, followerNum)
p.followerPackets = make([]*FollowerPacket, followerNum)
p.OrgBuffer = p.Data
if followerNum > 0 {
p.followersAddrs = followerAddrs[:int(followerNum)]
log.LogInfof("action[resolveFollowersAddr] %v", p.followersAddrs)
}
return
}
func NewPacketEx() (p PacketInterface) {
pr := new(Packet)
pr.Magic = proto.ProtoMagic
pr.StartT = time.Now().UnixNano()
pr.NeedReply = true
return pr
}
func NewPacket() (p *Packet) {
p = new(Packet)
p.Magic = proto.ProtoMagic
p.StartT = time.Now().UnixNano()
p.NeedReply = true
return
}
func NewPacketToGetAllWatermarks(partitionID uint64, extentType uint8) (p *Packet) {
p = new(Packet)
p.Opcode = proto.OpGetAllWatermarks
p.PartitionID = partitionID
p.Magic = proto.ProtoMagic
p.ReqID = proto.GenerateRequestID()
p.ExtentType = extentType
return
}
func NewPacketToReadTinyDeleteRecord(partitionID uint64, offset int64) (p *Packet) {
p = new(Packet)
p.Opcode = proto.OpReadTinyDeleteRecord
p.PartitionID = partitionID
p.Magic = proto.ProtoMagic
p.ReqID = proto.GenerateRequestID()
p.ExtentOffset = offset
return
}
func NewReadTinyDeleteRecordResponsePacket(requestID int64, partitionID uint64) (p *Packet) {
p = new(Packet)
p.PartitionID = partitionID
p.Magic = proto.ProtoMagic
p.Opcode = proto.OpOk
p.ReqID = requestID
p.ExtentType = proto.NormalExtentType
return
}
type (
MakeStreamReadResponsePacket func(requestID int64, partitionID uint64, extentID uint64) (p PacketInterface)
MakeExtentRepairReadPacket func(partitionID uint64, extentID uint64, offset, size int) (p PacketInterface)
)
func NewExtentRepairReadPacket(partitionID uint64, extentID uint64, offset, size int) (p PacketInterface) {
pr := new(Packet)
pr.ExtentID = extentID
pr.PartitionID = partitionID
pr.Magic = proto.ProtoMagic
pr.ExtentOffset = int64(offset)
pr.Size = uint32(size)
pr.Opcode = proto.OpExtentRepairRead
pr.ExtentType = proto.NormalExtentType
pr.ReqID = proto.GenerateRequestID()
return pr
}
func NewTinyExtentRepairReadPacket(partitionID uint64, extentID uint64, offset, size int) (p PacketInterface) {
pr := new(Packet)
pr.ExtentID = extentID
pr.PartitionID = partitionID
pr.Magic = proto.ProtoMagic
pr.ExtentOffset = int64(offset)
pr.Size = uint32(size)
pr.Opcode = proto.OpTinyExtentRepairRead
pr.ExtentType = proto.TinyExtentType
pr.ReqID = proto.GenerateRequestID()
return pr
}
func NewTinyExtentStreamReadResponsePacket(requestID int64, partitionID uint64, extentID uint64) (p *Packet) {
p = new(Packet)
p.ExtentID = extentID
p.PartitionID = partitionID
p.Magic = proto.ProtoMagic
p.Opcode = proto.OpTinyExtentRepairRead
p.ReqID = requestID
p.ExtentType = proto.TinyExtentType
p.StartT = time.Now().UnixNano()
return
}
func NewNormalExtentWithHoleRepairReadPacket(partitionID uint64, extentID uint64, offset, size int) (p PacketInterface) {
pr := new(Packet)
pr.ExtentID = extentID
pr.PartitionID = partitionID
pr.Magic = proto.ProtoMagic
pr.ExtentOffset = int64(offset)
pr.Size = uint32(size)
pr.Opcode = proto.OpSnapshotExtentRepairRead
pr.ExtentType = proto.TinyExtentType
pr.ReqID = proto.GenerateRequestID()
p = pr
return
}
func NewNormalExtentWithHoleStreamReadResponsePacket(requestID int64, partitionID uint64, extentID uint64) (p *Packet) {
p = new(Packet)
p.ExtentID = extentID
p.PartitionID = partitionID
p.Magic = proto.ProtoMagic
p.Opcode = proto.OpSnapshotExtentRepairRsp
p.ReqID = requestID
p.ExtentType = proto.NormalExtentType
p.StartT = time.Now().UnixNano()
return
}
func NewStreamReadResponsePacket(requestID int64, partitionID uint64, extentID uint64) (p PacketInterface) {
pr := new(Packet)
pr.ExtentID = extentID
pr.PartitionID = partitionID
pr.Magic = proto.ProtoMagic
pr.Opcode = proto.OpOk
pr.ReqID = requestID
pr.ExtentType = proto.NormalExtentType
return pr
}
func NewPacketToNotifyExtentRepair(partitionID uint64) (p *Packet) {
p = new(Packet)
p.Opcode = proto.OpNotifyReplicasToRepair
p.PartitionID = partitionID
p.Magic = proto.ProtoMagic
p.ExtentType = proto.NormalExtentType
p.ReqID = proto.GenerateRequestID()
return
}
func (p *Packet) SetResultCode(code uint8) {
p.ResultCode = code
}
func (p *Packet) SetCRC(crc uint32) {
p.CRC = crc
}
func (p *Packet) SetExtentOffset(offset int64) {
p.ExtentOffset = offset
}
func (p *Packet) GetStartT() int64 {
return p.StartT
}
func (p *Packet) GetPartitionID() uint64 {
return p.PartitionID
}
func (p *Packet) GetExtentID() uint64 {
return p.ExtentID
}
func (p *Packet) GetSize() uint32 {
return p.Size
}
func (p *Packet) SetSize(size uint32) {
p.Size = size
}
func (p *Packet) SetOpCode(op uint8) {
p.Opcode = op
}
func (p *Packet) GetOpcode() uint8 {
return p.Opcode
}
func (p *Packet) GetArg() []byte {
return p.Arg
}
func (p *Packet) GetCRC() uint32 {
return p.CRC
}
func (p *Packet) GetArgLen() uint32 {
return p.ArgLen
}
func (p *Packet) GetData() []byte {
return p.Data
}
func (p *Packet) GetResultCode() uint8 {
return p.ResultCode
}
func (p *Packet) GetExtentOffset() int64 {
return p.ExtentOffset
}
func (p *Packet) IsErrPacket() bool {
return p.ResultCode != proto.OpOk && p.ResultCode != proto.OpInitResultCode
}
var ErrorUnknownOp = errors.New("unknown opcode")
func (p *Packet) identificationErrorResultCode(errLog string, errMsg string) {
log.LogDebugf("action[identificationErrorResultCode] error %v, errmsg %v", errLog, errMsg)
if strings.Contains(errMsg, storage.ParameterMismatchError.Error()) ||
strings.Contains(errMsg, ErrorUnknownOp.Error()) {
p.ResultCode = proto.OpArgMismatchErr
} else if strings.Contains(errMsg, proto.ErrDataPartitionNotExists.Error()) {
p.ResultCode = proto.OpTryOtherAddr
} else if strings.Contains(errMsg, storage.ExtentNotFoundError.Error()) ||
strings.Contains(errMsg, storage.ExtentHasBeenDeletedError.Error()) {
p.ResultCode = proto.OpNotExistErr
} else if strings.Contains(errMsg, storage.NoSpaceError.Error()) {
p.ResultCode = proto.OpDiskNoSpaceErr
} else if strings.Contains(errMsg, storage.BrokenDiskError.Error()) {
p.ResultCode = proto.OpDiskErr
} else if strings.Contains(errMsg, "GetAvailableTinyExtent") {
p.ResultCode = proto.OpDiskNoSpaceErr
} else if strings.Contains(errMsg, storage.LimitedIoError.Error()) {
p.ResultCode = proto.OpLimitedIoErr
} else if strings.Contains(errMsg, storage.TryAgainError.Error()) {
p.ResultCode = proto.OpAgain
} else if strings.Contains(errMsg, raft.ErrNotLeader.Error()) {
p.ResultCode = proto.OpTryOtherAddr
} else if strings.Contains(errMsg, raft.ErrStopped.Error()) {
p.ResultCode = proto.OpTryOtherAddr
} else if strings.Contains(errMsg, storage.VerNotConsistentError.Error()) {
p.ResultCode = proto.ErrCodeVersionOpError
// log.LogDebugf("action[identificationErrorResultCode] not change ver erro code, (%v)", string(debug.Stack()))
} else if strings.Contains(errMsg, storage.NoDiskReadRepairExtentTokenError.Error()) {
p.ResultCode = proto.OpReadRepairExtentAgain
} else {
log.LogErrorf("action[identificationErrorResultCode] error %v, errmsg %v", errLog, errMsg)
p.ResultCode = proto.OpIntraGroupNetErr
}
}
func (p *Packet) PackErrorBody(action, msg string) {
p.identificationErrorResultCode(action, msg)
p.Size = uint32(len([]byte(action + "_" + msg)))
p.Data = make([]byte, p.Size)
copy(p.Data[:int(p.Size)], []byte(action+"_"+msg))
}
func (p *Packet) ReadFull(c net.Conn, opcode uint8, readSize int) (err error) {
if p.IsNormalWriteOperation() && readSize == util.BlockSize {
p.Data, _ = proto.Buffers.Get(readSize)
} else {
p.Data = make([]byte, readSize)
}
_, err = io.ReadFull(c, p.Data[:readSize])
return
}
func (p *Packet) IsMasterCommand() bool {
switch p.Opcode {
case
proto.OpDataNodeHeartbeat,
proto.OpVersionOperation,
proto.OpLoadDataPartition,
proto.OpCreateDataPartition,
proto.OpDeleteDataPartition,
proto.OpDecommissionDataPartition,
proto.OpAddDataPartitionRaftMember,
proto.OpRemoveDataPartitionRaftMember,
proto.OpDataPartitionTryToLeader:
return true
default:
return false
}
}
func (p *Packet) IsForwardPacket() bool {
r := p.RemainingFollowers > 0 && !p.isSpecialReplicaCntPacket()
return r
}
func (p *Packet) isSpecialReplicaCntPacket() bool {
r := p.RemainingFollowers == 127
return r
}
// A leader packet is the packet send to the leader and does not require packet forwarding.
func (p *Packet) IsLeaderPacket() (ok bool) {
if (p.IsForwardPkt() || p.isSpecialReplicaCntPacket()) &&
(p.IsNormalWriteOperation() || p.IsCreateExtentOperation() || p.IsMarkDeleteExtentOperation()) {
ok = true
}
return
}
func (p *Packet) IsTinyExtentType() bool {
return p.ExtentType == proto.TinyExtentType
}
func (p *Packet) IsNormalWriteOperation() bool {
return p.Opcode == proto.OpWrite || p.Opcode == proto.OpSyncWrite
}
func (p *Packet) IsSnapshotModWriteAppendOperation() bool {
return p.Opcode == proto.OpRandomWriteAppend || p.Opcode == proto.OpSyncRandomWriteAppend
}
func (p *Packet) IsCreateExtentOperation() bool {
return p.Opcode == proto.OpCreateExtent
}
func (p *Packet) IsMarkDeleteExtentOperation() bool {
return p.Opcode == proto.OpMarkDelete || p.Opcode == proto.OpSplitMarkDelete
}
func (p *Packet) IsMarkSplitExtentOperation() bool {
return p.Opcode == proto.OpSplitMarkDelete
}
func (p *Packet) IsBatchDeleteExtents() bool {
return p.Opcode == proto.OpBatchDeleteExtent
}
func (p *Packet) IsBroadcastMinAppliedID() bool {
return p.Opcode == proto.OpBroadcastMinAppliedID
}
func (p *Packet) IsRandomWrite() bool {
return p.Opcode == proto.OpRandomWrite || p.Opcode == proto.OpSyncRandomWrite ||
p.Opcode == proto.OpRandomWriteVer || p.Opcode == proto.OpSyncRandomWriteVer
}
func (p *Packet) IsSyncWrite() bool {
return p.Opcode == proto.OpSyncWrite || p.Opcode == proto.OpSyncRandomWrite
}
func (p *Packet) SetDegrade() {
p.shallDegrade = true
}
func (p *Packet) UnsetDegrade() {
p.shallDegrade = false
}
func (p *Packet) ShallDegrade() bool {
return p.shallDegrade
}
func (p *Packet) SetStartT(StartT int64) {
p.StartT = StartT
}
func (p *Packet) SetData(data []byte) {
p.Data = data
}
func (p *Packet) SetArglen(len uint32) {
p.ArgLen = len
}
func (p *Packet) SetArg(data []byte) {
p.Arg = data
}