/
peer_apply.go
586 lines (488 loc) · 13 KB
/
peer_apply.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
584
585
586
package raftstore
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"time"
"github.com/deepfabric/beehive/metric"
"github.com/deepfabric/beehive/pb"
"github.com/deepfabric/beehive/pb/metapb"
"github.com/deepfabric/beehive/pb/raftcmdpb"
"github.com/deepfabric/beehive/pb/raftpb"
"github.com/deepfabric/beehive/util"
"github.com/fagongzi/goetty"
"github.com/fagongzi/util/protoc"
etcdraftpb "go.etcd.io/etcd/raft/raftpb"
)
func (pr *peerReplica) doRegistrationJob(delegate *applyDelegate) error {
value, loaded := pr.store.delegates.LoadOrStore(delegate.shard.ID, delegate)
if loaded {
old := value.(*applyDelegate)
if old.peerID != delegate.peerID {
logger.Fatalf("shard %d delegate peer id not match, old=<%d> curr=<%d>",
pr.shardID,
old.peerID,
delegate.peerID)
}
old.peerID = delegate.peerID
old.shard = delegate.shard
old.term = delegate.term
old.applyState = delegate.applyState
old.appliedIndexTerm = delegate.appliedIndexTerm
old.clearAllCommandsAsStale()
}
return nil
}
func (s *store) doDestroy(shardID uint64, peer metapb.Peer) error {
if value, ok := s.delegates.Load(shardID); ok {
s.delegates.Delete(shardID)
delegate := value.(*applyDelegate)
delegate.destroy()
}
pr := s.getPR(shardID, false)
if pr != nil {
pr.mustDestroy()
}
return nil
}
func (pr *peerReplica) doCompactRaftLog(shardID, startIndex, endIndex uint64) error {
firstIndex := startIndex
if firstIndex == 0 {
startKey := getRaftLogKey(shardID, 0)
firstIndex = endIndex
key, _, err := pr.store.MetadataStorage().Seek(startKey)
if err != nil {
return err
}
if len(key) > 0 {
firstIndex, err = getRaftLogIndex(key)
if err != nil {
return err
}
}
}
if firstIndex >= endIndex {
logger.Infof("shard %d no need to gc raft log",
shardID)
return nil
}
wb := util.NewWriteBatch()
for index := firstIndex; index < endIndex; index++ {
key := getRaftLogKey(shardID, index)
err := wb.Delete(key)
if err != nil {
return err
}
}
err := pr.store.MetadataStorage().Write(wb, false)
if err == nil {
logger.Debugf("shard %d raft log gc complete, entriesCount=<%d>",
shardID,
(endIndex - startIndex))
}
return err
}
func (pr *peerReplica) doApplyingSnapshotJob() error {
logger.Infof("shard %d begin apply snapshot data", pr.shardID)
localState, err := pr.ps.loadLocalState(pr.ps.applySnapJob)
if err != nil {
logger.Fatalf("shard %d apply snap load local state failed, errors:\n %+v",
pr.shardID,
err)
return err
}
err = pr.ps.deleteAllInRange(encStartKey(&localState.Shard), encEndKey(&localState.Shard), pr.ps.applySnapJob)
if err != nil {
logger.Fatalf("shard %d apply snap delete range data failed, errors:\n %+v",
pr.shardID,
err)
return err
}
err = pr.ps.applySnapshot(pr.ps.applySnapJob)
if err != nil {
logger.Errorf("shard %d apply snap snapshot failed, errors:\n %+v",
pr.shardID,
err)
return err
}
err = pr.ps.updatePeerState(pr.ps.shard, raftpb.PeerNormal, nil)
if err != nil {
logger.Fatalf("shard %d apply snap update peer state failed, errors:\n %+v",
pr.shardID,
err)
return err
}
pr.store.opts.shardStateAware.SnapshotApplied(pr.ps.shard)
pr.stopRaftTick = false
logger.Infof("shard %d apply snapshot data complete, %+v",
pr.shardID,
pr.ps.raftHardState)
return nil
}
func (pr *peerReplica) doApplyCommittedEntries(shardID uint64, term uint64, commitedEntries []etcdraftpb.Entry) error {
logger.Debugf("shard %d async apply raft log with %d entries at term %d",
shardID,
len(commitedEntries),
term)
value, ok := pr.store.delegates.Load(shardID)
if !ok {
return fmt.Errorf("shard %d missing delegate", pr.shardID)
}
delegate := value.(*applyDelegate)
delegate.term = term
delegate.applyCommittedEntries(commitedEntries)
if delegate.isPendingRemove() {
delegate.destroy()
pr.store.delegates.Delete(delegate.shard.ID)
}
return nil
}
type asyncApplyResult struct {
shardID uint64
appliedIndexTerm uint64
applyState raftpb.RaftApplyState
result *execResult
metrics applyMetrics
}
func (res *asyncApplyResult) reset() {
res.shardID = 0
res.appliedIndexTerm = 0
res.applyState = raftpb.RaftApplyState{}
res.result = nil
res.metrics = applyMetrics{}
}
func (res *asyncApplyResult) hasSplitExecResult() bool {
return nil != res.result && res.result.splitResult != nil
}
type execResult struct {
adminType raftcmdpb.AdminCmdType
changePeer *changePeer
splitResult *splitResult
raftGCResult *raftGCResult
}
type changePeer struct {
confChange etcdraftpb.ConfChange
peer metapb.Peer
shard metapb.Shard
}
type splitResult struct {
left metapb.Shard
right metapb.Shard
}
type raftGCResult struct {
state raftpb.RaftTruncatedState
firstIndex uint64
}
type applyContext struct {
raftStateWB *util.WriteBatch
dataWB CommandWriteBatch
applyState raftpb.RaftApplyState
req *raftcmdpb.RaftCMDRequest
index uint64
term uint64
metrics applyMetrics
}
func newApplyContext(id uint64, store *store) *applyContext {
var dataWB CommandWriteBatch
if store.opts.writeBatchFunc != nil {
dataWB = store.opts.writeBatchFunc(id)
}
return &applyContext{
raftStateWB: util.NewWriteBatch(),
dataWB: dataWB,
}
}
func (ctx *applyContext) reset() {
ctx.raftStateWB.Reset()
ctx.applyState = raftpb.RaftApplyState{}
ctx.req = nil
ctx.index = 0
ctx.term = 0
ctx.metrics = applyMetrics{}
if ctx.dataWB != nil {
ctx.dataWB.Reset()
}
}
type applyDelegate struct {
store *store
ps *peerStorage
peerID uint64
shard metapb.Shard
// if we remove ourself in ChangePeer remove, we should set this flag, then
// any following committed logs in same Ready should be applied failed.
pendingRemove bool
applyState raftpb.RaftApplyState
appliedIndexTerm uint64
term uint64
pendingCMDs []cmd
pendingChangePeerCMD cmd
// reuse
wb *util.WriteBatch
ctx *applyContext
// attrs
buf *goetty.ByteBuf
attrs map[string]interface{}
requests []int
}
func (d *applyDelegate) resetAttrs() {
for key := range d.attrs {
delete(d.attrs, key)
}
d.attrs[attrBuf] = d.buf
}
func (d *applyDelegate) clearAllCommandsAsStale() {
for _, c := range d.pendingCMDs {
d.notifyStaleCMD(c)
}
if d.pendingChangePeerCMD.req != nil {
d.notifyStaleCMD(d.pendingChangePeerCMD)
}
d.pendingCMDs = d.pendingCMDs[:0]
d.pendingChangePeerCMD = emptyCMD
}
func (d *applyDelegate) findCB(ctx *applyContext) (cmd, bool) {
if isChangePeerCMD(ctx.req) {
c := d.pendingChangePeerCMD
if c.req == nil {
return emptyCMD, false
} else if bytes.Compare(ctx.req.Header.ID, c.getUUID()) == 0 {
return c, true
}
d.notifyStaleCMD(c)
return emptyCMD, false
}
for {
head, ok := d.popPendingCMD(ctx.term)
if !ok || head.req == nil {
return emptyCMD, false
}
if bytes.Compare(head.getUUID(), ctx.req.Header.ID) == 0 {
return head, true
}
// Because of the lack of original RaftCmdRequest, we skip calling
// coprocessor here.
d.notifyStaleCMD(head)
}
}
func (d *applyDelegate) appendPendingCmd(c cmd) {
d.pendingCMDs = append(d.pendingCMDs, c)
}
func (d *applyDelegate) popPendingCMD(raftLogEntryTerm uint64) (cmd, bool) {
if len(d.pendingCMDs) == 0 {
return emptyCMD, false
}
if d.pendingCMDs[0].term > raftLogEntryTerm {
return emptyCMD, false
}
c := d.pendingCMDs[0]
d.pendingCMDs[0] = emptyCMD
d.pendingCMDs = d.pendingCMDs[1:]
return c, true
}
func (d *applyDelegate) notifyStaleCMD(c cmd) {
c.resp(errorStaleCMDResp(c.getUUID(), d.term))
}
func (d *applyDelegate) notifyShardRemoved(c cmd) {
logger.Infof("shard %d cmd is removed, skip. cmd=<%+v>",
d.shard.ID,
c)
c.respShardNotFound(d.shard.ID)
}
func (d *applyDelegate) applyCommittedEntries(commitedEntries []etcdraftpb.Entry) {
if len(commitedEntries) <= 0 {
return
}
start := time.Now()
req := pb.AcquireRaftCMDRequest()
for idx, entry := range commitedEntries {
if d.isPendingRemove() {
// This peer is about to be destroyed, skip everything.
break
}
expectIndex := d.applyState.AppliedIndex + 1
if expectIndex != entry.Index {
logger.Fatalf("shard %d index not match, expect=<%d> get=<%d> state=<%+v> entry=<%+v>",
d.shard.ID,
expectIndex,
entry.Index,
d.applyState,
entry)
}
if idx > 0 {
d.ctx.reset()
req.Reset()
}
d.ctx.req = req
d.ctx.applyState = d.applyState
d.ctx.index = entry.Index
d.ctx.term = entry.Term
var result *execResult
switch entry.Type {
case etcdraftpb.EntryNormal:
result = d.applyEntry(&entry)
case etcdraftpb.EntryConfChange:
result = d.applyConfChange(&entry)
}
asyncResult := asyncApplyResult{}
asyncResult.shardID = d.shard.ID
asyncResult.appliedIndexTerm = d.appliedIndexTerm
asyncResult.applyState = d.applyState
asyncResult.result = result
if d.ctx != nil {
asyncResult.metrics = d.ctx.metrics
}
pr := d.store.getPR(d.shard.ID, false)
if pr != nil {
pr.addApplyResult(asyncResult)
}
}
// only release RaftCMDRequest. Header and Requests fields is pb created in Unmarshal
pb.ReleaseRaftCMDRequest(req)
metric.ObserveRaftLogApplyDuration(start)
}
func (d *applyDelegate) applyEntry(entry *etcdraftpb.Entry) *execResult {
if len(entry.Data) > 0 {
protoc.MustUnmarshal(d.ctx.req, entry.Data)
return d.doApplyRaftCMD()
}
// when a peer become leader, it will send an empty entry.
state := d.applyState
state.AppliedIndex = entry.Index
err := d.store.MetadataStorage().Set(getApplyStateKey(d.shard.ID), protoc.MustMarshal(&state))
if err != nil {
logger.Fatalf("shard %d apply empty entry failed, entry=<%s> errors:\n %+v",
d.shard.ID,
entry.String(),
err)
}
d.applyState.AppliedIndex = entry.Index
d.appliedIndexTerm = entry.Term
if entry.Term <= 0 {
panic("error empty entry term.")
}
for {
c, ok := d.popPendingCMD(entry.Term - 1)
if !ok {
return nil
}
// apprently, all the callbacks whose term is less than entry's term are stale.
d.notifyStaleCMD(c)
}
}
func (d *applyDelegate) applyConfChange(entry *etcdraftpb.Entry) *execResult {
cc := new(etcdraftpb.ConfChange)
protoc.MustUnmarshal(cc, entry.Data)
protoc.MustUnmarshal(d.ctx.req, cc.Context)
result := d.doApplyRaftCMD()
if nil == result {
return &execResult{
adminType: raftcmdpb.ChangePeer,
changePeer: &changePeer{},
}
}
result.changePeer.confChange = *cc
return result
}
func (d *applyDelegate) doApplyRaftCMD() *execResult {
if d.ctx.index == 0 {
logger.Fatalf("shard %d apply raft command needs a none zero index",
d.shard.ID)
}
c, ok := d.findCB(d.ctx)
if d.isPendingRemove() {
logger.Fatalf("shard %d apply raft comand can not pending remove",
d.shard.ID)
}
var err error
var resp *raftcmdpb.RaftCMDResponse
var result *execResult
var writeBytes uint64
var diffBytes int64
if !d.checkEpoch(d.ctx.req) {
resp = errorStaleEpochResp(d.ctx.req.Header.ID, d.term, d.shard)
} else {
if d.ctx.req.AdminRequest != nil {
resp, result, err = d.execAdminRequest(d.ctx)
if err != nil {
resp = errorStaleEpochResp(d.ctx.req.Header.ID, d.term, d.shard)
}
} else {
writeBytes, diffBytes, resp = d.execWriteRequest(d.ctx)
}
}
if d.ctx.dataWB != nil {
writeBytes, diffBytes, err = d.ctx.dataWB.Execute(d.shard)
if err != nil {
logger.Fatalf("shard %d execute batch failed with %+v",
d.shard.ID,
err)
}
d.ctx.dataWB.Reset()
}
if logger.DebugEnabled() {
for _, req := range d.ctx.req.Requests {
logger.Debugf("%s exec completed", hex.EncodeToString(req.ID))
}
}
d.ctx.metrics.writtenBytes += writeBytes
if diffBytes < 0 {
v := uint64(math.Abs(float64(diffBytes)))
if v >= d.ctx.metrics.sizeDiffHint {
d.ctx.metrics.sizeDiffHint = 0
} else {
d.ctx.metrics.sizeDiffHint -= v
}
} else {
d.ctx.metrics.sizeDiffHint += uint64(diffBytes)
}
d.ctx.applyState.AppliedIndex = d.ctx.index
if !d.isPendingRemove() {
err := d.ctx.raftStateWB.Set(getApplyStateKey(d.shard.ID), protoc.MustMarshal(&d.ctx.applyState))
if err != nil {
logger.Fatalf("shard %d save apply context failed, errors:\n %+v",
d.shard.ID,
err)
}
}
err = d.store.MetadataStorage().Write(d.ctx.raftStateWB, false)
if err != nil {
logger.Fatalf("shard %d commit apply result failed, errors:\n %+v",
d.shard.ID,
err)
}
d.applyState = d.ctx.applyState
d.term = d.ctx.term
if ok {
if resp != nil {
buildTerm(d.term, resp)
buildUUID(d.ctx.req.Header.ID, resp)
// resp client
c.resp(resp)
}
}
return result
}
func (d *applyDelegate) destroy() {
for _, c := range d.pendingCMDs {
d.notifyShardRemoved(c)
}
if d.pendingChangePeerCMD.req != nil {
d.notifyShardRemoved(d.pendingChangePeerCMD)
}
d.pendingCMDs = nil
d.pendingChangePeerCMD = emptyCMD
}
func (d *applyDelegate) setPendingRemove() {
d.pendingRemove = true
}
func (d *applyDelegate) isPendingRemove() bool {
return d.pendingRemove
}
func (d *applyDelegate) checkEpoch(req *raftcmdpb.RaftCMDRequest) bool {
return checkEpoch(d.shard, req)
}
func isChangePeerCMD(req *raftcmdpb.RaftCMDRequest) bool {
return nil != req.AdminRequest &&
req.AdminRequest.CmdType == raftcmdpb.ChangePeer
}