-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_raft.go
742 lines (679 loc) · 25.1 KB
/
store_raft.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package kvserver
import (
"context"
"sync/atomic"
"time"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
crdberrors "github.com/cockroachdb/errors"
"go.etcd.io/etcd/raft/raftpb"
)
type raftRequestInfo struct {
req *RaftMessageRequest
respStream RaftMessageResponseStream
}
type raftRequestQueue struct {
syncutil.Mutex
infos []raftRequestInfo
// TODO(nvanbenschoten): consider recycling []raftRequestInfo slices. This
// could be done without any new mutex locking by storing two slices here
// and swapping them under lock in processRequestQueue.
}
// HandleSnapshot reads an incoming streaming snapshot and applies it if
// possible.
func (s *Store) HandleSnapshot(
header *SnapshotRequest_Header, stream SnapshotResponseStream,
) error {
ctx := s.AnnotateCtx(stream.Context())
const name = "storage.Store: handle snapshot"
return s.stopper.RunTaskWithErr(ctx, name, func(ctx context.Context) error {
s.metrics.RaftRcvdMessages[raftpb.MsgSnap].Inc(1)
if s.IsDraining() {
return stream.Send(&SnapshotResponse{
Status: SnapshotResponse_DECLINED,
Message: storeDrainingMsg,
})
}
return s.receiveSnapshot(ctx, header, stream)
})
}
func (s *Store) uncoalesceBeats(
ctx context.Context,
beats []RaftHeartbeat,
fromReplica, toReplica roachpb.ReplicaDescriptor,
msgT raftpb.MessageType,
respStream RaftMessageResponseStream,
) {
if len(beats) == 0 {
return
}
if log.V(4) {
log.Infof(ctx, "uncoalescing %d beats of type %v: %+v", len(beats), msgT, beats)
}
beatReqs := make([]RaftMessageRequest, len(beats))
var toEnqueue []roachpb.RangeID
for i, beat := range beats {
msg := raftpb.Message{
Type: msgT,
From: uint64(beat.FromReplicaID),
To: uint64(beat.ToReplicaID),
Term: beat.Term,
Commit: beat.Commit,
}
beatReqs[i] = RaftMessageRequest{
RangeID: beat.RangeID,
FromReplica: roachpb.ReplicaDescriptor{
NodeID: fromReplica.NodeID,
StoreID: fromReplica.StoreID,
ReplicaID: beat.FromReplicaID,
},
ToReplica: roachpb.ReplicaDescriptor{
NodeID: toReplica.NodeID,
StoreID: toReplica.StoreID,
ReplicaID: beat.ToReplicaID,
},
Message: msg,
Quiesce: beat.Quiesce,
LaggingFollowersOnQuiesce: beat.LaggingFollowersOnQuiesce,
LaggingFollowersOnQuiesceAccurate: beat.LaggingFollowersOnQuiesceAccurate,
}
if log.V(4) {
log.Infof(ctx, "uncoalesced beat: %+v", beatReqs[i])
}
enqueue := s.HandleRaftUncoalescedRequest(ctx, &beatReqs[i], respStream)
if enqueue {
toEnqueue = append(toEnqueue, beat.RangeID)
}
}
s.scheduler.EnqueueRaftRequests(toEnqueue...)
}
// HandleRaftRequest dispatches a raft message to the appropriate Replica. It
// requires that s.mu is not held.
func (s *Store) HandleRaftRequest(
ctx context.Context, req *RaftMessageRequest, respStream RaftMessageResponseStream,
) *roachpb.Error {
// NB: unlike the other two RaftMessageHandler methods implemented by Store,
// this one doesn't need to directly run through a Stopper task because it
// delegates all work through a raftScheduler, whose workers' lifetimes are
// already tied to the Store's Stopper.
if len(req.Heartbeats)+len(req.HeartbeatResps) > 0 {
if req.RangeID != 0 {
log.Fatalf(ctx, "coalesced heartbeats must have rangeID == 0")
}
s.uncoalesceBeats(ctx, req.Heartbeats, req.FromReplica, req.ToReplica, raftpb.MsgHeartbeat, respStream)
s.uncoalesceBeats(ctx, req.HeartbeatResps, req.FromReplica, req.ToReplica, raftpb.MsgHeartbeatResp, respStream)
return nil
}
enqueue := s.HandleRaftUncoalescedRequest(ctx, req, respStream)
if enqueue {
s.scheduler.EnqueueRaftRequest(req.RangeID)
}
return nil
}
// HandleRaftUncoalescedRequest dispatches a raft message to the appropriate
// Replica. The method returns whether the Range needs to be enqueued in the
// Raft scheduler. It requires that s.mu is not held.
func (s *Store) HandleRaftUncoalescedRequest(
ctx context.Context, req *RaftMessageRequest, respStream RaftMessageResponseStream,
) (enqueue bool) {
if len(req.Heartbeats)+len(req.HeartbeatResps) > 0 {
log.Fatalf(ctx, "HandleRaftUncoalescedRequest cannot be given coalesced heartbeats or heartbeat responses, received %s", req)
}
// HandleRaftRequest is called on locally uncoalesced heartbeats (which are
// not sent over the network if the environment variable is set) so do not
// count them.
s.metrics.RaftRcvdMessages[req.Message.Type].Inc(1)
value, ok := s.replicaQueues.Load(int64(req.RangeID))
if !ok {
value, _ = s.replicaQueues.LoadOrStore(int64(req.RangeID), unsafe.Pointer(&raftRequestQueue{}))
}
q := (*raftRequestQueue)(value)
q.Lock()
defer q.Unlock()
if len(q.infos) >= replicaRequestQueueSize {
// TODO(peter): Return an error indicating the request was dropped. Note
// that dropping the request is safe. Raft will retry.
s.metrics.RaftRcvdMsgDropped.Inc(1)
return false
}
q.infos = append(q.infos, raftRequestInfo{
req: req,
respStream: respStream,
})
// processRequestQueue will process all infos in the slice each time it
// runs, so we only need to schedule a Raft request event if we added the
// first info in the slice. Everyone else can rely on the request that added
// the first info already having scheduled a Raft request event.
return len(q.infos) == 1 /* enqueue */
}
// withReplicaForRequest calls the supplied function with the (lazily
// initialized) Replica specified in the request. The replica passed to
// the function will have its Replica.raftMu locked.
func (s *Store) withReplicaForRequest(
ctx context.Context, req *RaftMessageRequest, f func(context.Context, *Replica) *roachpb.Error,
) *roachpb.Error {
// Lazily create the replica.
r, _, err := s.getOrCreateReplica(
ctx,
req.RangeID,
req.ToReplica.ReplicaID,
&req.FromReplica,
)
if err != nil {
return roachpb.NewError(err)
}
defer r.raftMu.Unlock()
ctx = r.AnnotateCtx(ctx)
r.setLastReplicaDescriptors(req)
return f(ctx, r)
}
// processRaftRequestWithReplica processes the (non-snapshot) Raft request on
// the specified replica. Notably, it does not handle updates to the Raft Ready
// state; callers will probably want to handle this themselves at some point.
func (s *Store) processRaftRequestWithReplica(
ctx context.Context, r *Replica, req *RaftMessageRequest,
) *roachpb.Error {
if verboseRaftLoggingEnabled() {
log.Infof(ctx, "incoming raft message:\n%s", raftDescribeMessage(req.Message, raftEntryFormatter))
}
if req.Message.Type == raftpb.MsgSnap {
log.Fatalf(ctx, "unexpected snapshot: %+v", req)
}
if req.Quiesce {
if req.Message.Type != raftpb.MsgHeartbeat {
log.Fatalf(ctx, "unexpected quiesce: %+v", req)
}
if r.maybeQuiesceOnNotify(
ctx,
req.Message,
laggingReplicaSet(req.LaggingFollowersOnQuiesce),
req.LaggingFollowersOnQuiesceAccurate,
) {
return nil
}
}
if req.ToReplica.ReplicaID == 0 {
log.VEventf(ctx, 1, "refusing incoming Raft message %s from %+v to %+v",
req.Message.Type, req.FromReplica, req.ToReplica)
return roachpb.NewErrorf(
"cannot recreate replica that is not a member of its range (StoreID %s not found in r%d)",
r.store.StoreID(), req.RangeID,
)
}
drop := maybeDropMsgApp(ctx, (*replicaMsgAppDropper)(r), &req.Message, req.RangeStartKey)
if !drop {
if err := r.stepRaftGroup(req); err != nil {
return roachpb.NewError(err)
}
}
return nil
}
// processRaftSnapshotRequest processes the incoming non-preemptive snapshot
// Raft request on the request's specified replica. The function makes sure to
// handle any updated Raft Ready state. It also adds and later removes the
// (potentially) necessary placeholder to protect against concurrent access to
// the keyspace encompassed by the snapshot but not yet guarded by the replica.
func (s *Store) processRaftSnapshotRequest(
ctx context.Context, snapHeader *SnapshotRequest_Header, inSnap IncomingSnapshot,
) *roachpb.Error {
if snapHeader.IsPreemptive() {
return roachpb.NewError(crdberrors.AssertionFailedf(`expected a raft or learner snapshot`))
}
return s.withReplicaForRequest(ctx, &snapHeader.RaftMessageRequest, func(
ctx context.Context, r *Replica,
) (pErr *roachpb.Error) {
if snapHeader.RaftMessageRequest.Message.Type != raftpb.MsgSnap {
log.Fatalf(ctx, "expected snapshot: %+v", snapHeader.RaftMessageRequest)
}
// Check to see if a snapshot can be applied. Snapshots can always be applied
// to initialized replicas. Note that if we add a placeholder we need to
// already be holding Replica.raftMu in order to prevent concurrent
// raft-ready processing of uninitialized replicas.
var addedPlaceholder bool
var removePlaceholder bool
if err := func() error {
s.mu.Lock()
defer s.mu.Unlock()
placeholder, err := s.canApplySnapshotLocked(ctx, snapHeader)
if err != nil {
// If the storage cannot accept the snapshot, return an
// error before passing it to RawNode.Step, since our
// error handling options past that point are limited.
log.Infof(ctx, "cannot apply snapshot: %s", err)
return err
}
if placeholder != nil {
// NB: The placeholder added here is either removed below after a
// preemptive snapshot is applied or after the next call to
// Replica.handleRaftReady. Note that we can only get here if the
// replica doesn't exist or is uninitialized.
if err := s.addPlaceholderLocked(placeholder); err != nil {
log.Fatalf(ctx, "could not add vetted placeholder %s: %+v", placeholder, err)
}
addedPlaceholder = true
}
return nil
}(); err != nil {
return roachpb.NewError(err)
}
if addedPlaceholder {
// If we added a placeholder remove it before we return unless some other
// part of the code takes ownership of the removal (indicated by setting
// removePlaceholder to false).
removePlaceholder = true
defer func() {
if removePlaceholder {
if s.removePlaceholder(ctx, snapHeader.RaftMessageRequest.RangeID) {
atomic.AddInt32(&s.counts.removedPlaceholders, 1)
}
}
}()
}
// NB: we cannot get errRemoved here because we're promised by
// withReplicaForRequest that this replica is not currently being removed
// and we've been holding the raftMu the entire time.
if err := r.stepRaftGroup(&snapHeader.RaftMessageRequest); err != nil {
return roachpb.NewError(err)
}
_, expl, err := r.handleRaftReadyRaftMuLocked(ctx, inSnap)
maybeFatalOnRaftReadyErr(ctx, expl, err)
removePlaceholder = false
return nil
})
}
// HandleRaftResponse implements the RaftMessageHandler interface. Per the
// interface specification, an error is returned if and only if the underlying
// Raft connection should be closed.
// It requires that s.mu is not held.
func (s *Store) HandleRaftResponse(ctx context.Context, resp *RaftMessageResponse) error {
ctx = s.AnnotateCtx(ctx)
const name = "storage.Store: handle raft response"
return s.stopper.RunTaskWithErr(ctx, name, func(ctx context.Context) error {
repl, replErr := s.GetReplica(resp.RangeID)
if replErr == nil {
// Best-effort context annotation of replica.
ctx = repl.AnnotateCtx(ctx)
}
switch val := resp.Union.GetValue().(type) {
case *roachpb.Error:
switch tErr := val.GetDetail().(type) {
case *roachpb.ReplicaTooOldError:
if replErr != nil {
// RangeNotFoundErrors are expected here; nothing else is.
if !errors.HasType(replErr, (*roachpb.RangeNotFoundError)(nil)) {
log.Errorf(ctx, "%v", replErr)
}
return nil
}
// Grab the raftMu in addition to the replica mu because
// cancelFailedProposalsLocked below requires it.
repl.raftMu.Lock()
defer repl.raftMu.Unlock()
repl.mu.Lock()
// If the replica ID in the error does not match then we know
// that the replica has been removed and re-added quickly. In
// that case, we don't want to add it to the replicaGCQueue.
// If the replica is not alive then we also should ignore this error.
if tErr.ReplicaID != repl.mu.replicaID ||
!repl.mu.destroyStatus.IsAlive() ||
// Ignore if we want to test the replicaGC queue.
s.TestingKnobs().DisableEagerReplicaRemoval {
repl.mu.Unlock()
return nil
}
// The replica will be garbage collected soon (we are sure
// since our replicaID is definitely too old), but in the meantime we
// already want to bounce all traffic from it. Note that the replica
// could be re-added with a higher replicaID, but we want to clear the
// replica's data before that happens.
if log.V(1) {
log.Infof(ctx, "setting local replica to destroyed due to ReplicaTooOld error")
}
repl.mu.Unlock()
nextReplicaID := tErr.ReplicaID + 1
return s.removeReplicaRaftMuLocked(ctx, repl, nextReplicaID, RemoveOptions{
DestroyData: true,
})
case *roachpb.RaftGroupDeletedError:
if replErr != nil {
// RangeNotFoundErrors are expected here; nothing else is.
if !errors.HasType(replErr, (*roachpb.RangeNotFoundError)(nil)) {
log.Errorf(ctx, "%v", replErr)
}
return nil
}
// If the replica is talking to a replica that's been deleted, it must be
// out of date. While this may just mean it's slightly behind, it can
// also mean that it is so far behind it no longer knows where any of the
// other replicas are (#23994). Add it to the replica GC queue to do a
// proper check.
s.replicaGCQueue.AddAsync(ctx, repl, replicaGCPriorityDefault)
case *roachpb.StoreNotFoundError:
log.Warningf(ctx, "raft error: node %d claims to not contain store %d for replica %s: %s",
resp.FromReplica.NodeID, resp.FromReplica.StoreID, resp.FromReplica, val)
return val.GetDetail() // close Raft connection
default:
log.Warningf(ctx, "got error from r%d, replica %s: %s",
resp.RangeID, resp.FromReplica, val)
}
default:
log.Warningf(ctx, "got unknown raft response type %T from replica %s: %s", val, resp.FromReplica, val)
}
return nil
})
}
// enqueueRaftUpdateCheck asynchronously registers the given range ID to be
// checked for raft updates when the processRaft goroutine is idle.
func (s *Store) enqueueRaftUpdateCheck(rangeID roachpb.RangeID) {
s.scheduler.EnqueueRaftReady(rangeID)
}
func (s *Store) processRequestQueue(ctx context.Context, rangeID roachpb.RangeID) bool {
value, ok := s.replicaQueues.Load(int64(rangeID))
if !ok {
return false
}
q := (*raftRequestQueue)(value)
q.Lock()
infos := q.infos
q.infos = nil
q.Unlock()
if len(infos) == 0 {
return false
}
var hadError bool
for i := range infos {
info := &infos[i]
if pErr := s.withReplicaForRequest(
ctx, info.req, func(ctx context.Context, r *Replica) *roachpb.Error {
return s.processRaftRequestWithReplica(ctx, r, info.req)
},
); pErr != nil {
hadError = true
if err := info.respStream.Send(newRaftMessageResponse(info.req, pErr)); err != nil {
// Seems excessive to log this on every occurrence as the other side
// might have closed.
log.VEventf(ctx, 1, "error sending error: %s", err)
}
}
}
if hadError {
// If we're unable to process a request, consider dropping the request queue
// to free up space in the map.
// This is relevant if requests failed because the target replica could not
// be created (for example due to the Raft tombstone). The particular code
// here takes into account that we don't want to drop the queue if there
// are other messages waiting on it, or if the target replica exists. Raft
// tolerates the occasional dropped message, but our unit tests are less
// forgiving.
//
// See https://github.com/cockroachdb/cockroach/issues/30951#issuecomment-428010411.
if _, exists := s.mu.replicas.Load(int64(rangeID)); !exists {
q.Lock()
if len(q.infos) == 0 {
s.replicaQueues.Delete(int64(rangeID))
}
q.Unlock()
}
}
// NB: Even if we had errors and the corresponding replica no longer
// exists, returning true here won't cause a new, uninitialized replica
// to be created in processReady().
return true // ready
}
func (s *Store) processReady(ctx context.Context, rangeID roachpb.RangeID) {
value, ok := s.mu.replicas.Load(int64(rangeID))
if !ok {
return
}
r := (*Replica)(value)
ctx = r.AnnotateCtx(ctx)
start := timeutil.Now()
stats, expl, err := r.handleRaftReady(ctx, noSnap)
removed := maybeFatalOnRaftReadyErr(ctx, expl, err)
elapsed := timeutil.Since(start)
s.metrics.RaftWorkingDurationNanos.Inc(elapsed.Nanoseconds())
// Warn if Raft processing took too long. We use the same duration as we
// use for warning about excessive raft mutex lock hold times. Long
// processing time means we'll have starved local replicas of ticks and
// remote replicas will likely start campaigning.
if elapsed >= defaultReplicaRaftMuWarnThreshold {
log.Warningf(ctx, "handle raft ready: %.1fs [applied=%d, batches=%d, state_assertions=%d]",
elapsed.Seconds(), stats.entriesProcessed, stats.batchesProcessed, stats.stateAssertions)
}
if !removed && !r.IsInitialized() {
// Only an uninitialized replica can have a placeholder since, by
// definition, an initialized replica will be present in the
// replicasByKey map. While the replica will usually consume the
// placeholder itself, that isn't guaranteed and so this invocation
// here is crucial (i.e. don't remove it).
//
// We need to hold raftMu here to prevent removing a placeholder that is
// actively being used by Store.processRaftRequest.
r.raftMu.Lock()
if s.removePlaceholder(ctx, r.RangeID) {
atomic.AddInt32(&s.counts.droppedPlaceholders, 1)
}
r.raftMu.Unlock()
}
}
func (s *Store) processTick(ctx context.Context, rangeID roachpb.RangeID) bool {
value, ok := s.mu.replicas.Load(int64(rangeID))
if !ok {
return false
}
livenessMap, _ := s.livenessMap.Load().(IsLiveMap)
start := timeutil.Now()
r := (*Replica)(value)
exists, err := r.tick(livenessMap)
if err != nil {
log.Errorf(ctx, "%v", err)
}
s.metrics.RaftTickingDurationNanos.Inc(timeutil.Since(start).Nanoseconds())
return exists // ready
}
// nodeIsLiveCallback is invoked when a node transitions from non-live to live.
// Iterate through all replicas and find any which belong to ranges containing
// the implicated node. Unquiesce if currently quiesced and the node's replica
// is not up-to-date.
//
// See the comment in shouldFollowerQuiesceOnNotify for details on how these two
// functions combine to provide the guarantee that:
//
// If a quorum of replica in a Raft group is alive and at least
// one of these replicas is up-to-date, the Raft group will catch
// up any of the live, lagging replicas.
//
// Note that this mechanism can race with concurrent invocations of processTick,
// which may have a copy of the previous livenessMap where the now-live node is
// down. Those instances should be rare, however, and we expect the newly live
// node to eventually unquiesce the range.
func (s *Store) nodeIsLiveCallback(l kvserverpb.Liveness) {
s.updateLivenessMap()
s.mu.replicas.Range(func(k int64, v unsafe.Pointer) bool {
r := (*Replica)(v)
r.mu.RLock()
quiescent := r.mu.quiescent
lagging := r.mu.laggingFollowersOnQuiesce
laggingAccurate := r.mu.laggingFollowersOnQuiesceAccurate
r.mu.RUnlock()
if quiescent && (lagging.MemberStale(l) || !laggingAccurate) {
r.unquiesce()
}
return true
})
}
func (s *Store) processRaft(ctx context.Context) {
if s.cfg.TestingKnobs.DisableProcessRaft {
return
}
s.scheduler.Start(ctx, s.stopper)
// Wait for the scheduler worker goroutines to finish.
s.stopper.RunWorker(ctx, s.scheduler.Wait)
s.stopper.RunWorker(ctx, s.raftTickLoop)
s.stopper.RunWorker(ctx, s.coalescedHeartbeatsLoop)
s.stopper.AddCloser(stop.CloserFn(func() {
s.cfg.Transport.Stop(s.StoreID())
}))
}
func (s *Store) raftTickLoop(ctx context.Context) {
ticker := time.NewTicker(s.cfg.RaftTickInterval)
defer ticker.Stop()
var rangeIDs []roachpb.RangeID
for {
select {
case <-ticker.C:
rangeIDs = rangeIDs[:0]
// Update the liveness map.
if s.cfg.NodeLiveness != nil {
s.updateLivenessMap()
}
s.unquiescedReplicas.Lock()
// Why do we bother to ever queue a Replica on the Raft scheduler for
// tick processing? Couldn't we just call Replica.tick() here? Yes, but
// then a single bad/slow Replica can disrupt tick processing for every
// Replica on the store which cascades into Raft elections and more
// disruption.
for rangeID := range s.unquiescedReplicas.m {
rangeIDs = append(rangeIDs, rangeID)
}
s.unquiescedReplicas.Unlock()
s.scheduler.EnqueueRaftTicks(rangeIDs...)
s.metrics.RaftTicks.Inc(1)
case <-s.stopper.ShouldStop():
return
}
}
}
func (s *Store) updateLivenessMap() {
nextMap := s.cfg.NodeLiveness.GetIsLiveMap()
for nodeID, entry := range nextMap {
if entry.IsLive {
// Make sure we ask all live nodes for closed timestamp updates.
s.cfg.ClosedTimestamp.Clients.EnsureClient(nodeID)
continue
}
// Liveness claims that this node is down, but ConnHealth gets the last say
// because we'd rather quiesce a range too little than one too often. Note
// that this policy is different from the one governing the releasing of
// proposal quota; see comments over there.
//
// NB: This has false negatives. If a node doesn't have a conn open to it
// when ConnHealth is called, then ConnHealth will return
// rpc.ErrNotHeartbeated regardless of whether the node is up or not. That
// said, for the nodes that matter, we're likely talking to them via the
// Raft transport, so ConnHealth should usually indicate a real problem if
// it gives us an error back. The check can also have false positives if the
// node goes down after populating the map, but that matters even less.
entry.IsLive = (s.cfg.NodeDialer.ConnHealth(nodeID, rpc.SystemClass) == nil)
nextMap[nodeID] = entry
}
s.livenessMap.Store(nextMap)
}
// Since coalesced heartbeats adds latency to heartbeat messages, it is
// beneficial to have it run on a faster cycle than once per tick, so that
// the delay does not impact latency-sensitive features such as quiescence.
func (s *Store) coalescedHeartbeatsLoop(ctx context.Context) {
ticker := time.NewTicker(s.cfg.CoalescedHeartbeatsInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.sendQueuedHeartbeats(ctx)
case <-s.stopper.ShouldStop():
return
}
}
}
// sendQueuedHeartbeatsToNode requires that the s.coalescedMu lock is held. It
// returns the number of heartbeats that were sent.
func (s *Store) sendQueuedHeartbeatsToNode(
ctx context.Context, beats, resps []RaftHeartbeat, to roachpb.StoreIdent,
) int {
var msgType raftpb.MessageType
if len(beats) == 0 && len(resps) == 0 {
return 0
} else if len(resps) == 0 {
msgType = raftpb.MsgHeartbeat
} else if len(beats) == 0 {
msgType = raftpb.MsgHeartbeatResp
} else {
log.Fatal(ctx, "cannot coalesce both heartbeats and responses")
}
chReq := newRaftMessageRequest()
*chReq = RaftMessageRequest{
RangeID: 0,
ToReplica: roachpb.ReplicaDescriptor{
NodeID: to.NodeID,
StoreID: to.StoreID,
ReplicaID: 0,
},
FromReplica: roachpb.ReplicaDescriptor{
NodeID: s.Ident.NodeID,
StoreID: s.Ident.StoreID,
},
Message: raftpb.Message{
Type: msgType,
},
Heartbeats: beats,
HeartbeatResps: resps,
}
if log.V(4) {
log.Infof(ctx, "sending raft request (coalesced) %+v", chReq)
}
if !s.cfg.Transport.SendAsync(chReq, rpc.SystemClass) {
for _, beat := range beats {
if value, ok := s.mu.replicas.Load(int64(beat.RangeID)); ok {
(*Replica)(value).addUnreachableRemoteReplica(beat.ToReplicaID)
}
}
for _, resp := range resps {
if value, ok := s.mu.replicas.Load(int64(resp.RangeID)); ok {
(*Replica)(value).addUnreachableRemoteReplica(resp.ToReplicaID)
}
}
return 0
}
return len(beats) + len(resps)
}
func (s *Store) sendQueuedHeartbeats(ctx context.Context) {
s.coalescedMu.Lock()
heartbeats := s.coalescedMu.heartbeats
heartbeatResponses := s.coalescedMu.heartbeatResponses
s.coalescedMu.heartbeats = map[roachpb.StoreIdent][]RaftHeartbeat{}
s.coalescedMu.heartbeatResponses = map[roachpb.StoreIdent][]RaftHeartbeat{}
s.coalescedMu.Unlock()
var beatsSent int
for to, beats := range heartbeats {
beatsSent += s.sendQueuedHeartbeatsToNode(ctx, beats, nil, to)
}
for to, resps := range heartbeatResponses {
beatsSent += s.sendQueuedHeartbeatsToNode(ctx, nil, resps, to)
}
s.metrics.RaftCoalescedHeartbeatsPending.Update(int64(beatsSent))
}
func (s *Store) updateCapacityGauges(ctx context.Context) error {
desc, err := s.Descriptor(ctx, false /* useCached */)
if err != nil {
return err
}
s.metrics.Capacity.Update(desc.Capacity.Capacity)
s.metrics.Available.Update(desc.Capacity.Available)
s.metrics.Used.Update(desc.Capacity.Used)
return nil
}