-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_snapshot.go
1330 lines (1227 loc) · 47 KB
/
store_snapshot.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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 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"
"io"
"time"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/raftentry"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/rditer"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/stateloader"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
"go.etcd.io/etcd/raft/v3/raftpb"
"golang.org/x/time/rate"
"google.golang.org/grpc"
)
const (
// Messages that provide detail about why a snapshot was rejected.
storeDrainingMsg = "store is draining"
// IntersectingSnapshotMsg is part of the error message returned from
// canAcceptSnapshotLocked and is exposed here so testing can rely on it.
IntersectingSnapshotMsg = "snapshot intersects existing range"
)
// incomingSnapshotStream is the minimal interface on a GRPC stream required
// to receive a snapshot over the network.
type incomingSnapshotStream interface {
Send(*kvserverpb.SnapshotResponse) error
Recv() (*kvserverpb.SnapshotRequest, error)
}
// outgoingSnapshotStream is the minimal interface on a GRPC stream required
// to send a snapshot over the network.
type outgoingSnapshotStream interface {
Send(*kvserverpb.SnapshotRequest) error
Recv() (*kvserverpb.SnapshotResponse, error)
}
// incomingSnapshotStream is the minimal interface on a GRPC stream required
// to receive a snapshot over the network.
type incomingDelegatedStream interface {
Send(*kvserverpb.SnapshotResponse) error
Recv() (*kvserverpb.DelegateSnapshotRequest, error)
}
// outgoingSnapshotStream is the minimal interface on a GRPC stream required
// to send a snapshot over the network.
type outgoingDelegatedStream interface {
Send(*kvserverpb.DelegateSnapshotRequest) error
Recv() (*kvserverpb.SnapshotResponse, error)
}
// snapshotStrategy is an approach to sending and receiving Range snapshots.
// Each implementation corresponds to a SnapshotRequest_Strategy, and it is
// expected that the implementation that matches the Strategy specified in the
// snapshot header will always be used.
type snapshotStrategy interface {
// Receive streams SnapshotRequests in from the provided stream and
// constructs an IncomingSnapshot.
Receive(context.Context, incomingSnapshotStream, kvserverpb.SnapshotRequest_Header) (IncomingSnapshot, error)
// Send streams SnapshotRequests created from the OutgoingSnapshot in to the
// provided stream. On nil error, the number of bytes sent is returned.
Send(context.Context, outgoingSnapshotStream, kvserverpb.SnapshotRequest_Header, *OutgoingSnapshot) (int64, error)
// Status provides a status report on the work performed during the
// snapshot. Only valid if the strategy succeeded.
Status() redact.RedactableString
// Close cleans up any resources associated with the snapshot strategy.
Close(context.Context)
}
func assertStrategy(
ctx context.Context,
header kvserverpb.SnapshotRequest_Header,
expect kvserverpb.SnapshotRequest_Strategy,
) {
if header.Strategy != expect {
log.Fatalf(ctx, "expected strategy %s, found strategy %s", expect, header.Strategy)
}
}
// Separated locks and snapshots send/receive:
// When running in a mixed version cluster with 21.1 and 20.2, snapshots sent
// by 21.1 nodes will attempt to read the lock table key space and send any
// keys in it. But there will be none, so 20.2 nodes receiving such snapshots
// are fine. A 21.1 node receiving a snapshot will construct SSTs for the lock
// table key range which will only contain ClearRange for those ranges.
//
// When the cluster transitions to clusterversion.SeparatedLocks, the nodes
// that see that transition can immediately start writing separated
// intents/locks. Since the 21.1 nodes that have not seen that transition are
// always ready to handle separated intents, including receiving them in
// snapshots, the cluster will behave correctly despite nodes seeing this
// state transition at different times.
// kvBatchSnapshotStrategy is an implementation of snapshotStrategy that streams
// batches of KV pairs in the BatchRepr format.
type kvBatchSnapshotStrategy struct {
status redact.RedactableString
// The size of the batches of PUT operations to send to the receiver of the
// snapshot. Only used on the sender side.
batchSize int64
// Limiter for sending KV batches. Only used on the sender side.
limiter *rate.Limiter
// Only used on the sender side.
newBatch func() storage.Batch
// The approximate size of the SST chunk to buffer in memory on the receiver
// before flushing to disk. Only used on the receiver side.
sstChunkSize int64
// Only used on the receiver side.
scratch *SSTSnapshotStorageScratch
st *cluster.Settings
}
// multiSSTWriter is a wrapper around RocksDBSstFileWriter and
// SSTSnapshotStorageScratch that handles chunking SSTs and persisting them to
// disk.
type multiSSTWriter struct {
st *cluster.Settings
scratch *SSTSnapshotStorageScratch
currSST storage.SSTWriter
keyRanges []rditer.KeyRange
currRange int
// The approximate size of the SST chunk to buffer in memory on the receiver
// before flushing to disk.
sstChunkSize int64
// The total size of SST data. Updated on SST finalization.
dataSize int64
}
func newMultiSSTWriter(
ctx context.Context,
st *cluster.Settings,
scratch *SSTSnapshotStorageScratch,
keyRanges []rditer.KeyRange,
sstChunkSize int64,
) (multiSSTWriter, error) {
msstw := multiSSTWriter{
st: st,
scratch: scratch,
keyRanges: keyRanges,
sstChunkSize: sstChunkSize,
}
if err := msstw.initSST(ctx); err != nil {
return msstw, err
}
return msstw, nil
}
func (msstw *multiSSTWriter) initSST(ctx context.Context) error {
newSSTFile, err := msstw.scratch.NewFile(ctx, msstw.sstChunkSize)
if err != nil {
return errors.Wrap(err, "failed to create new sst file")
}
newSST := storage.MakeIngestionSSTWriter(ctx, msstw.st, newSSTFile)
msstw.currSST = newSST
if err := msstw.currSST.ClearRawRange(
msstw.keyRanges[msstw.currRange].Start, msstw.keyRanges[msstw.currRange].End); err != nil {
msstw.currSST.Close()
return errors.Wrap(err, "failed to clear range on sst file writer")
}
return nil
}
func (msstw *multiSSTWriter) finalizeSST(ctx context.Context) error {
err := msstw.currSST.Finish()
if err != nil {
return errors.Wrap(err, "failed to finish sst")
}
msstw.dataSize += msstw.currSST.DataSize
msstw.currRange++
msstw.currSST.Close()
return nil
}
func (msstw *multiSSTWriter) Put(ctx context.Context, key storage.EngineKey, value []byte) error {
for msstw.keyRanges[msstw.currRange].End.Compare(key.Key) <= 0 {
// Finish the current SST, write to the file, and move to the next key
// range.
if err := msstw.finalizeSST(ctx); err != nil {
return err
}
if err := msstw.initSST(ctx); err != nil {
return err
}
}
if msstw.keyRanges[msstw.currRange].Start.Compare(key.Key) > 0 {
return errors.AssertionFailedf("client error: expected %s to fall in one of %s", key.Key, msstw.keyRanges)
}
if err := msstw.currSST.PutEngineKey(key, value); err != nil {
return errors.Wrap(err, "failed to put in sst")
}
return nil
}
func (msstw *multiSSTWriter) Finish(ctx context.Context) (int64, error) {
if msstw.currRange < len(msstw.keyRanges) {
for {
if err := msstw.finalizeSST(ctx); err != nil {
return 0, err
}
if msstw.currRange >= len(msstw.keyRanges) {
break
}
if err := msstw.initSST(ctx); err != nil {
return 0, err
}
}
}
return msstw.dataSize, nil
}
func (msstw *multiSSTWriter) Close() {
msstw.currSST.Close()
}
// Receive implements the snapshotStrategy interface.
//
// NOTE: This function assumes that the key-value pairs are sent in sorted
// order. The key-value pairs are sent in the following sorted order:
//
// 1. Replicated range-id local key range
// 2. Range-local key range
// 3. Two lock-table key ranges (optional)
// 4. User key range
func (kvSS *kvBatchSnapshotStrategy) Receive(
ctx context.Context, stream incomingSnapshotStream, header kvserverpb.SnapshotRequest_Header,
) (IncomingSnapshot, error) {
assertStrategy(ctx, header, kvserverpb.SnapshotRequest_KV_BATCH)
// At the moment we'll write at most five SSTs.
// TODO(jeffreyxiao): Re-evaluate as the default range size grows.
keyRanges := rditer.MakeReplicatedKeyRanges(header.State.Desc)
msstw, err := newMultiSSTWriter(ctx, kvSS.st, kvSS.scratch, keyRanges, kvSS.sstChunkSize)
if err != nil {
return noSnap, err
}
defer msstw.Close()
for {
req, err := stream.Recv()
if err != nil {
return noSnap, err
}
if req.Header != nil {
err := errors.New("client error: provided a header mid-stream")
return noSnap, sendSnapshotError(stream, err)
}
if req.KVBatch != nil {
batchReader, err := storage.NewRocksDBBatchReader(req.KVBatch)
if err != nil {
return noSnap, errors.Wrap(err, "failed to decode batch")
}
// All operations in the batch are guaranteed to be puts.
for batchReader.Next() {
if batchReader.BatchType() != storage.BatchTypeValue {
return noSnap, errors.AssertionFailedf("expected type %d, found type %d", storage.BatchTypeValue, batchReader.BatchType())
}
key, err := batchReader.EngineKey()
if err != nil {
return noSnap, errors.Wrap(err, "failed to decode mvcc key")
}
if err := msstw.Put(ctx, key, batchReader.Value()); err != nil {
return noSnap, errors.Wrapf(err, "writing sst for raft snapshot")
}
}
}
if req.Final {
// We finished receiving all batches and log entries. It's possible that
// we did not receive any key-value pairs for some of the key ranges, but
// we must still construct SSTs with range deletion tombstones to remove
// the data.
dataSize, err := msstw.Finish(ctx)
if err != nil {
return noSnap, errors.Wrapf(err, "finishing sst for raft snapshot")
}
msstw.Close()
snapUUID, err := uuid.FromBytes(header.RaftMessageRequest.Message.Snapshot.Data)
if err != nil {
err = errors.Wrap(err, "client error: invalid snapshot")
return noSnap, sendSnapshotError(stream, err)
}
inSnap := IncomingSnapshot{
SnapUUID: snapUUID,
SSTStorageScratch: kvSS.scratch,
FromReplica: header.RaftMessageRequest.FromReplica,
Desc: header.State.Desc,
DataSize: dataSize,
snapType: header.Type,
raftAppliedIndex: header.State.RaftAppliedIndex,
}
kvSS.status = redact.Sprintf("ssts: %d", len(kvSS.scratch.SSTs()))
return inSnap, nil
}
}
}
// errMalformedSnapshot indicates that the snapshot in question is malformed,
// for e.g. missing raft log entries.
var errMalformedSnapshot = errors.New("malformed snapshot generated")
// Send implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Send(
ctx context.Context,
stream outgoingSnapshotStream,
header kvserverpb.SnapshotRequest_Header,
snap *OutgoingSnapshot,
) (int64, error) {
assertStrategy(ctx, header, kvserverpb.SnapshotRequest_KV_BATCH)
// bytesSent is updated as key-value batches are sent with sendBatch. It
// does not reflect the log entries sent (which are never sent in newer
// versions of CRDB, as of VersionUnreplicatedTruncatedState).
bytesSent := int64(0)
// Iterate over all keys using the provided iterator and stream out batches
// of key-values.
kvs := 0
var b storage.Batch
defer func() {
if b != nil {
b.Close()
}
}()
for iter := snap.Iter; ; iter.Next() {
if ok, err := iter.Valid(); err != nil {
return 0, err
} else if !ok {
break
}
kvs++
unsafeKey := iter.UnsafeKey()
unsafeValue := iter.UnsafeValue()
if b == nil {
b = kvSS.newBatch()
}
if err := b.PutEngineKey(unsafeKey, unsafeValue); err != nil {
return 0, err
}
if bLen := int64(b.Len()); bLen >= kvSS.batchSize {
if err := kvSS.sendBatch(ctx, stream, b); err != nil {
return 0, err
}
bytesSent += bLen
b.Close()
b = nil
}
}
if b != nil {
if err := kvSS.sendBatch(ctx, stream, b); err != nil {
return 0, err
}
bytesSent += int64(b.Len())
}
kvSS.status = redact.Sprintf("kv pairs: %d", kvs)
return bytesSent, nil
}
func (kvSS *kvBatchSnapshotStrategy) sendBatch(
ctx context.Context, stream outgoingSnapshotStream, batch storage.Batch,
) error {
if err := kvSS.limiter.WaitN(ctx, 1); err != nil {
return err
}
return stream.Send(&kvserverpb.SnapshotRequest{KVBatch: batch.Repr()})
}
// Status implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Status() redact.RedactableString {
return kvSS.status
}
// Close implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Close(ctx context.Context) {
if kvSS.scratch != nil {
// A failure to clean up the storage is benign except that it will leak
// disk space (which is reclaimed on node restart). It is unexpected
// though, so log a warning.
if err := kvSS.scratch.Clear(); err != nil {
log.Warningf(ctx, "error closing kvBatchSnapshotStrategy: %v", err)
}
}
}
// reserveSnapshot throttles incoming snapshots.
func (s *Store) reserveSnapshot(
ctx context.Context, header *kvserverpb.SnapshotRequest_Header,
) (_cleanup func(), _err error) {
return s.throttleSnapshot(
ctx, s.snapshotApplySem, header.RangeSize,
header.RaftMessageRequest.RangeID, header.RaftMessageRequest.ToReplica.ReplicaID,
)
}
// reserveSendSnapshot throttles outgoing snapshots.
func (s *Store) reserveSendSnapshot(
ctx context.Context, req *kvserverpb.DelegateSnapshotRequest,
) (_cleanup func(), _err error) {
return s.throttleSnapshot(
ctx, s.snapshotSendSem, req.Header.RangeSize,
req.Header.RangeID, req.DelegatedSender.ReplicaID,
)
}
// throttleSnapshot is a helper function to throttle snapshot sending and
// receiving. The returned closure is used to cleanup the reservation and
// release its resources.
func (s *Store) throttleSnapshot(
ctx context.Context,
snapshotSem chan struct{},
rangeSize int64,
rangeID roachpb.RangeID,
replicaID roachpb.ReplicaID,
) (_cleanup func(), _err error) {
tBegin := timeutil.Now()
// Empty snapshots are exempt from rate limits because they're so cheap to
// apply. This vastly speeds up rebalancing any empty ranges created by a
// RESTORE or manual SPLIT AT, since it prevents these empty snapshots from
// getting stuck behind large snapshots managed by the replicate queue.
if rangeSize != 0 {
queueCtx := ctx
if deadline, ok := queueCtx.Deadline(); ok {
// Enforce a more strict timeout for acquiring the snapshot reservation to
// ensure that if the reservation is acquired, the snapshot has sufficient
// time to complete. See the comment on snapshotReservationQueueTimeoutFraction
// and TestReserveSnapshotQueueTimeout.
timeoutFrac := snapshotReservationQueueTimeoutFraction.Get(&s.ClusterSettings().SV)
timeout := time.Duration(timeoutFrac * float64(timeutil.Until(deadline)))
var cancel func()
queueCtx, cancel = context.WithTimeout(queueCtx, timeout) // nolint:context
defer cancel()
}
select {
case snapshotSem <- struct{}{}:
case <-queueCtx.Done():
if err := ctx.Err(); err != nil {
return nil, errors.Wrap(err, "acquiring snapshot reservation")
}
return nil, errors.Wrapf(
queueCtx.Err(),
"giving up during snapshot reservation due to %q",
snapshotReservationQueueTimeoutFraction.Key(),
)
case <-s.stopper.ShouldQuiesce():
return nil, errors.Errorf("stopped")
}
}
// The choice here is essentially arbitrary, but with a default range size of 128mb-512mb and the
// Raft snapshot rate limiting of 32mb/s, we expect to spend less than 16s per snapshot.
// which is what we want to log.
const snapshotReservationWaitWarnThreshold = 32 * time.Second
elapsed := timeutil.Since(tBegin)
if elapsed > snapshotReservationWaitWarnThreshold && !buildutil.CrdbTestBuild {
log.Infof(
ctx,
"waited for %.1fs to acquire snapshot reservation to r%d/%d",
elapsed.Seconds(),
rangeID,
replicaID,
)
}
s.metrics.ReservedReplicaCount.Inc(1)
s.metrics.Reserved.Inc(rangeSize)
return func() {
s.metrics.ReservedReplicaCount.Dec(1)
s.metrics.Reserved.Dec(rangeSize)
if rangeSize != 0 {
<-snapshotSem
}
}, nil
}
// canAcceptSnapshotLocked returns (_, nil) if the snapshot can be applied to
// this store's replica (i.e. the snapshot is not from an older incarnation of
// the replica) and a placeholder that can be (but is not yet) added to the
// replicasByKey map (if necessary).
//
// Both the store mu and the raft mu for the existing replica (which must exist)
// must be held.
func (s *Store) canAcceptSnapshotLocked(
ctx context.Context, snapHeader *kvserverpb.SnapshotRequest_Header,
) (*ReplicaPlaceholder, error) {
// TODO(tbg): see the comment on desc.Generation for what seems to be a much
// saner way to handle overlap via generational semantics.
desc := *snapHeader.State.Desc
// First, check for an existing Replica.
existingRepl, ok := s.mu.replicasByRangeID.Load(desc.RangeID)
if !ok {
return nil, errors.Errorf("canAcceptSnapshotLocked requires a replica present")
}
// The raftMu is held which allows us to use the existing replica as a
// placeholder when we decide that the snapshot can be applied. As long as the
// caller releases the raftMu only after feeding the snapshot into the
// replica, this is safe. This is true even when the snapshot spans a merge,
// because we will be guaranteed to have the subsumed (initialized) Replicas
// in place as well. This is because they are present when the merge first
// commits, and cannot have been replicaGC'ed yet (see replicaGCQueue.process).
existingRepl.raftMu.AssertHeld()
existingRepl.mu.RLock()
existingDesc := existingRepl.mu.state.Desc
existingIsInitialized := existingDesc.IsInitialized()
existingDestroyStatus := existingRepl.mu.destroyStatus
existingRepl.mu.RUnlock()
if existingIsInitialized {
// Regular Raft snapshots can't be refused at this point,
// even if they widen the existing replica. See the comments
// in Replica.maybeAcquireSnapshotMergeLock for how this is
// made safe.
//
// NB: The snapshot must be intended for this replica as
// withReplicaForRequest ensures that requests with a non-zero replica
// id are passed to a replica with a matching id.
return nil, nil
}
// If we are not alive then we should not apply a snapshot as our removal
// is imminent.
if existingDestroyStatus.Removed() {
return nil, existingDestroyStatus.err
}
// We have a key range [desc.StartKey,desc.EndKey) which we want to apply a
// snapshot for. Is there a conflicting existing placeholder or an
// overlapping range?
if err := s.checkSnapshotOverlapLocked(ctx, snapHeader); err != nil {
return nil, err
}
placeholder := &ReplicaPlaceholder{
rangeDesc: desc,
}
return placeholder, nil
}
// checkSnapshotOverlapLocked returns an error if the snapshot overlaps an
// existing replica or placeholder. Any replicas that do overlap have a good
// chance of being abandoned, so they're proactively handed to the replica GC
// queue.
func (s *Store) checkSnapshotOverlapLocked(
ctx context.Context, snapHeader *kvserverpb.SnapshotRequest_Header,
) error {
desc := *snapHeader.State.Desc
// NB: this check seems redundant since placeholders are also represented in
// replicasByKey (and thus returned in getOverlappingKeyRangeLocked).
if exRng, ok := s.mu.replicaPlaceholders[desc.RangeID]; ok {
return errors.Errorf("%s: canAcceptSnapshotLocked: cannot add placeholder, have an existing placeholder %s %v", s, exRng, snapHeader.RaftMessageRequest.FromReplica)
}
// TODO(benesch): consider discovering and GC'ing *all* overlapping ranges,
// not just the first one that getOverlappingKeyRangeLocked happens to return.
if it := s.getOverlappingKeyRangeLocked(&desc); it.item != nil {
// We have a conflicting range, so we must block the snapshot.
// When such a conflict exists, it will be resolved by one range
// either being split or garbage collected.
exReplica, err := s.GetReplica(it.Desc().RangeID)
msg := IntersectingSnapshotMsg
if err != nil {
log.Warningf(ctx, "unable to look up overlapping replica on %s: %v", exReplica, err)
} else {
inactive := func(r *Replica) bool {
if r.RaftStatus() == nil {
return true
}
// TODO(benesch): this check does not detect inactivity on
// replicas with epoch-based leases. Since the validity of an
// epoch-based lease is tied to the owning node's liveness, the
// lease can be valid well after the leader of the range has cut
// off communication with this replica. Expiration based leases,
// by contrast, will expire quickly if the leader of the range
// stops sending this replica heartbeats.
return !r.CurrentLeaseStatus(ctx).IsValid()
}
// We unconditionally send this replica through the replica GC queue. It's
// reasonably likely that the replica GC queue will do nothing because the
// replica needs to split instead, but better to err on the side of
// queueing too frequently. Blocking Raft snapshots for too long can wedge
// a cluster, and if the replica does need to be GC'd, this might be the
// only code path that notices in a timely fashion.
//
// We're careful to avoid starving out other replicas in the replica GC
// queue by queueing at a low priority unless we can prove that the range
// is inactive and thus unlikely to be about to process a split.
gcPriority := replicaGCPriorityDefault
if inactive(exReplica) {
gcPriority = replicaGCPrioritySuspect
}
msg += "; initiated GC:"
s.replicaGCQueue.AddAsync(ctx, exReplica, gcPriority)
}
return errors.Errorf("%s %v (incoming %v)", msg, exReplica, snapHeader.State.Desc.RSpan()) // exReplica can be nil
}
return nil
}
// receiveSnapshot receives an incoming snapshot via a pre-opened GRPC stream.
func (s *Store) receiveSnapshot(
ctx context.Context, header *kvserverpb.SnapshotRequest_Header, stream incomingSnapshotStream,
) error {
// Draining nodes will generally not be rebalanced to (see the filtering that
// happens in getStoreListFromIDsLocked()), but in case they are, they should
// reject the incoming rebalancing snapshots.
if s.IsDraining() {
switch t := header.Priority; t {
case kvserverpb.SnapshotRequest_RECOVERY:
// We can not reject Raft snapshots because draining nodes may have
// replicas in `StateSnapshot` that need to catch up.
//
// TODO(aayush): We also do not reject snapshots sent to replace dead
// replicas here, but draining stores are still filtered out in
// getStoreListFromIDsLocked(). Is that sound? Don't we want to
// upreplicate to draining nodes if there are no other candidates?
case kvserverpb.SnapshotRequest_REBALANCE:
return sendSnapshotError(stream, errors.New(storeDrainingMsg))
default:
// If this a new snapshot type that this cockroach version does not know
// about, we let it through.
}
}
if fn := s.cfg.TestingKnobs.ReceiveSnapshot; fn != nil {
if err := fn(header); err != nil {
return sendSnapshotError(stream, err)
}
}
// Defensive check that any snapshot contains this store in the descriptor.
storeID := s.StoreID()
if _, ok := header.State.Desc.GetReplicaDescriptor(storeID); !ok {
return errors.AssertionFailedf(
`snapshot of type %s was sent to s%d which did not contain it as a replica: %s`,
header.Type, storeID, header.State.Desc.Replicas())
}
cleanup, err := s.reserveSnapshot(ctx, header)
if err != nil {
return err
}
defer cleanup()
// The comment on ReplicaPlaceholder motivates and documents
// ReplicaPlaceholder semantics. Please be familiar with them
// before making any changes.
var placeholder *ReplicaPlaceholder
if pErr := s.withReplicaForRequest(
ctx, &header.RaftMessageRequest, func(ctx context.Context, r *Replica,
) *roachpb.Error {
var err error
s.mu.Lock()
defer s.mu.Unlock()
placeholder, err = s.canAcceptSnapshotLocked(ctx, header)
if err != nil {
return roachpb.NewError(err)
}
if placeholder != nil {
if err := s.addPlaceholderLocked(placeholder); err != nil {
return roachpb.NewError(err)
}
}
return nil
}); pErr != nil {
log.Infof(ctx, "cannot accept snapshot: %s", pErr)
return pErr.GoError()
}
defer func() {
if placeholder != nil {
// Remove the placeholder, if it's still there. Most of the time it will
// have been filled and this is a no-op.
if _, err := s.removePlaceholder(ctx, placeholder, removePlaceholderFailed); err != nil {
log.Fatalf(ctx, "unable to remove placeholder: %s", err)
}
}
}()
// Determine which snapshot strategy the sender is using to send this
// snapshot. If we don't know how to handle the specified strategy, return
// an error.
var ss snapshotStrategy
switch header.Strategy {
case kvserverpb.SnapshotRequest_KV_BATCH:
snapUUID, err := uuid.FromBytes(header.RaftMessageRequest.Message.Snapshot.Data)
if err != nil {
err = errors.Wrap(err, "invalid snapshot")
return sendSnapshotError(stream, err)
}
ss = &kvBatchSnapshotStrategy{
scratch: s.sstSnapshotStorage.NewScratchSpace(header.State.Desc.RangeID, snapUUID),
sstChunkSize: snapshotSSTWriteSyncRate.Get(&s.cfg.Settings.SV),
st: s.ClusterSettings(),
}
defer ss.Close(ctx)
default:
return sendSnapshotError(stream,
errors.Errorf("%s,r%d: unknown snapshot strategy: %s",
s, header.State.Desc.RangeID, header.Strategy),
)
}
if err := stream.Send(&kvserverpb.SnapshotResponse{Status: kvserverpb.SnapshotResponse_ACCEPTED}); err != nil {
return err
}
if log.V(2) {
log.Infof(ctx, "accepted snapshot reservation for r%d", header.State.Desc.RangeID)
}
inSnap, err := ss.Receive(ctx, stream, *header)
if err != nil {
return err
}
inSnap.placeholder = placeholder
// Use a background context for applying the snapshot, as handleRaftReady is
// not prepared to deal with arbitrary context cancellation. Also, we've
// already received the entire snapshot here, so there's no point in
// abandoning application half-way through if the caller goes away.
applyCtx := s.AnnotateCtx(context.Background())
if err := s.processRaftSnapshotRequest(applyCtx, header, inSnap); err != nil {
return sendSnapshotError(stream, errors.Wrap(err.GoError(), "failed to apply snapshot"))
}
return stream.Send(&kvserverpb.SnapshotResponse{Status: kvserverpb.SnapshotResponse_APPLIED})
}
func sendSnapshotError(stream incomingSnapshotStream, err error) error {
return stream.Send(&kvserverpb.SnapshotResponse{
Status: kvserverpb.SnapshotResponse_ERROR,
Message: err.Error(),
})
}
// SnapshotStorePool narrows StorePool to make sendSnapshot easier to test.
type SnapshotStorePool interface {
throttle(reason throttleReason, why string, toStoreID roachpb.StoreID)
}
// minSnapshotRate defines the minimum value that the rate limit for rebalance
// and recovery snapshots can be configured to. Any value below this lower bound
// is considered unsafe for use, as it can lead to excessively long-running
// snapshots. The sender of Raft snapshots holds resources (e.g. LSM snapshots,
// LSM iterators until #75824 is addressed) and blocks Raft log truncation, so
// it is not safe to let a single snapshot run for an unlimited period of time.
//
// The value was chosen based on a maximum range size of 512mb and a desire to
// prevent a single snapshot for running for more than 10 minutes. With a rate
// limit of 1mb/s, a 512mb snapshot will take just under 9 minutes to send.
const minSnapshotRate = 1 << 20 // 1mb/s
// rebalanceSnapshotRate is the rate at which snapshots can be sent in the
// context of up-replication or rebalancing (i.e. any snapshot that was not
// requested by raft itself, to which `kv.snapshot_recovery.max_rate` applies).
var rebalanceSnapshotRate = settings.RegisterByteSizeSetting(
settings.SystemOnly,
"kv.snapshot_rebalance.max_rate",
"the rate limit (bytes/sec) to use for rebalance and upreplication snapshots",
32<<20, // 32mb/s
func(v int64) error {
if v < minSnapshotRate {
return errors.Errorf("snapshot rate cannot be set to a value below %s: %s",
humanizeutil.IBytes(minSnapshotRate), humanizeutil.IBytes(v))
}
return nil
},
).WithPublic()
// recoverySnapshotRate is the rate at which Raft-initiated snapshot can be
// sent. Ideally, one would never see a Raft-initiated snapshot; we'd like all
// replicas to start out as learners or via splits, and to never be cut off from
// the log. However, it has proved unfeasible to completely get rid of them.
//
// TODO(tbg): The existence of this rate, separate from rebalanceSnapshotRate,
// does not make a whole lot of sense. Both sources of snapshots compete thanks
// to a semaphore at the receiver, and so the slower one ultimately determines
// the pace at which things can move along.
var recoverySnapshotRate = settings.RegisterByteSizeSetting(
settings.SystemOnly,
"kv.snapshot_recovery.max_rate",
"the rate limit (bytes/sec) to use for recovery snapshots",
32<<20, // 32mb/s
func(v int64) error {
if v < minSnapshotRate {
return errors.Errorf("snapshot rate cannot be set to a value below %s: %s",
humanizeutil.IBytes(minSnapshotRate), humanizeutil.IBytes(v))
}
return nil
},
).WithPublic()
// snapshotSenderBatchSize is the size that key-value batches are allowed to
// grow to during Range snapshots before being sent to the receiver. This limit
// places an upper-bound on the memory footprint of the sender of a Range
// snapshot. It is also the granularity of rate limiting.
var snapshotSenderBatchSize = settings.RegisterByteSizeSetting(
settings.SystemOnly,
"kv.snapshot_sender.batch_size",
"size of key-value batches sent over the network during snapshots",
256<<10, // 256 KB
settings.PositiveInt,
)
// snapshotReservationQueueTimeoutFraction is the maximum fraction of a Range
// snapshot's total timeout that it is allowed to spend queued on the receiver
// waiting for a reservation.
//
// Enforcement of this snapshotApplySem-scoped timeout is intended to prevent
// starvation of snapshots in cases where a queue of snapshots waiting for
// reservations builds and no single snapshot acquires the semaphore with
// sufficient time to complete, but each holds the semaphore long enough to
// ensure that later snapshots in the queue encounter this same situation. This
// is a case of FIFO queuing + timeouts leading to starvation. By rejecting
// snapshot attempts earlier, we ensure that those that do acquire the semaphore
// have sufficient time to complete.
//
// Consider the following motivating example:
//
// With a 60s timeout set by the snapshotQueue/replicateQueue for each snapshot,
// 45s needed to actually stream the data, and a willingness to wait for as long
// as it takes to get the reservation (i.e. this fraction = 1.0) there can be
// starvation. Each snapshot spends so much time waiting for the reservation
// that it will itself fail during sending, while the next snapshot wastes
// enough time waiting for us that it will itself fail, ad infinitum:
//
// t | snap1 snap2 snap3 snap4 snap5 ...
// ----+------------------------------------
// 0 | send
// 15 | queue queue
// 30 | queue
// 45 | ok send
// 60 | queue
// 75 | fail fail send
// 90 | fail send
// 105 |
// 120 | fail
// 135 |
//
// If we limit the amount of time we are willing to wait for a reservation to
// something that is small enough to, on success, give us enough time to
// actually stream the data, no starvation can occur. For example, with a 60s
// timeout, 45s needed to stream the data, we can wait at most 15s for a
// reservation and still avoid starvation:
//
// t | snap1 snap2 snap3 snap4 snap5 ...
// ----+------------------------------------
// 0 | send
// 15 | queue queue
// 30 | fail fail send
// 45 |
// 60 | ok queue
// 75 | ok send
// 90 |
// 105 |
// 120 | ok
// 135 |
//
// In practice, the snapshot reservation logic (reserveSnapshot) doesn't know
// how long sending the snapshot will actually take. But it knows the timeout it
// has been given by the snapshotQueue/replicateQueue, which serves as an upper
// bound, under the assumption that snapshots can make progress in the absence
// of starvation.
//
// Without the reservation timeout fraction, if the product of the number of
// concurrent snapshots and the average streaming time exceeded this timeout,
// the starvation scenario could occur, since the average queuing time would
// exceed the timeout. With the reservation limit, progress will be made as long
// as the average streaming time is less than the guaranteed processing time for
// any snapshot that succeeds in acquiring a reservation:
//
// guaranteed_processing_time = (1 - reservation_queue_timeout_fraction) x timeout
//
// The timeout for the snapshot and replicate queues bottoms out at 60s (by
// default, see kv.queue.process.guaranteed_time_budget). Given a default
// reservation queue timeout fraction of 0.4, this translates to a guaranteed
// processing time of 36s for any snapshot attempt that manages to acquire a
// reservation. This means that a 512MiB snapshot will succeed if sent at a rate
// of 14MiB/s or above.
//
// Lower configured snapshot rate limits quickly lead to a much higher timeout
// since we apply a liberal multiplier (permittedRangeScanSlowdown). Concretely,
// we move past the 1-minute timeout once the rate limit is set to anything less
// than 10*range_size/guaranteed_budget(in MiB/s), which comes out to ~85MiB/s
// for a 512MiB range and the default 1m budget. In other words, the queue uses
// sumptuous timeouts, and so we'll also be excessively lenient with how long
// we're willing to wait for a reservation (but not to the point of allowing the
// starvation scenario). As long as the nodes between the cluster can transfer
// at around ~14MiB/s, even a misconfiguration of the rate limit won't cause
// issues and where it does, the setting can be set to 1.0, effectively
// reverting to the old behavior.
var snapshotReservationQueueTimeoutFraction = settings.RegisterFloatSetting(
settings.SystemOnly,
"kv.snapshot_receiver.reservation_queue_timeout_fraction",
"the fraction of a snapshot's total timeout that it is allowed to spend "+
"queued on the receiver waiting for a reservation",
0.4,
func(v float64) error {
const min, max = 0.25, 1.0
if v < min {
return errors.Errorf("cannot set to a value less than %f: %f", min, v)
} else if v > max {
return errors.Errorf("cannot set to a value greater than %f: %f", max, v)
}
return nil
},
)
// snapshotSSTWriteSyncRate is the size of chunks to write before fsync-ing.
// The default of 2 MiB was chosen to be in line with the behavior in bulk-io.
// See sstWriteSyncRate.
var snapshotSSTWriteSyncRate = settings.RegisterByteSizeSetting(
settings.SystemOnly,
"kv.snapshot_sst.sync_size",
"threshold after which snapshot SST writes must fsync",
bulkIOWriteBurst,
settings.PositiveInt,
)
func snapshotRateLimit(
st *cluster.Settings, priority kvserverpb.SnapshotRequest_Priority,
) (rate.Limit, error) {
switch priority {
case kvserverpb.SnapshotRequest_RECOVERY:
return rate.Limit(recoverySnapshotRate.Get(&st.SV)), nil
case kvserverpb.SnapshotRequest_REBALANCE:
return rate.Limit(rebalanceSnapshotRate.Get(&st.SV)), nil
default:
return 0, errors.Errorf("unknown snapshot priority: %s", priority)
}
}
// SendEmptySnapshot creates an OutgoingSnapshot for the input range
// descriptor and seeds it with an empty range. Then, it sends this
// snapshot to the replica specified in the input.
func SendEmptySnapshot(
ctx context.Context,
st *cluster.Settings,
cc *grpc.ClientConn,
now hlc.Timestamp,
desc roachpb.RangeDescriptor,
to roachpb.ReplicaDescriptor,
) error {
// Create an engine to use as a buffer for the empty snapshot.
eng, err := storage.Open(
context.Background(),
storage.InMemory(),
storage.CacheSize(1<<20 /* 1 MiB */),
storage.MaxSize(512<<20 /* 512 MiB */))
if err != nil {
return err
}
defer eng.Close()
var ms enginepb.MVCCStats
// Seed an empty range into the new engine.
if err := storage.MVCCPutProto(
ctx, eng, &ms, keys.RangeDescriptorKey(desc.StartKey), now, nil /* txn */, &desc,
); err != nil {
return err
}