-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_snapshot.go
1046 lines (963 loc) · 36.8 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 storage
import (
"context"
"fmt"
"io"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"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/engine"
"github.com/cockroachdb/cockroach/pkg/storage/rditer"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
crdberrors "github.com/cockroachdb/errors"
"github.com/pkg/errors"
"go.etcd.io/etcd/raft/raftpb"
"golang.org/x/time/rate"
)
const (
// Messages that provide detail about why a preemptive snapshot was rejected.
snapshotStoreTooFullMsg = "store almost out of disk space"
snapshotApplySemBusyMsg = "store busy applying snapshots"
storeDrainingMsg = "store is draining"
// IntersectingSnapshotMsg is part of the error message returned from
// canApplySnapshotLocked 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(*SnapshotResponse) error
Recv() (*SnapshotRequest, error)
}
// outgoingSnapshotStream is the minimal interface on a GRPC stream required
// to send a snapshot over the network.
type outgoingSnapshotStream interface {
Send(*SnapshotRequest) error
Recv() (*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, SnapshotRequest_Header,
) (IncomingSnapshot, error)
// Send streams SnapshotRequests created from the OutgoingSnapshot in to the
// provided stream.
Send(
context.Context, outgoingSnapshotStream, SnapshotRequest_Header, *OutgoingSnapshot,
) error
// Status provides a status report on the work performed during the
// snapshot. Only valid if the strategy succeeded.
Status() string
// Close cleans up any resources associated with the snapshot strategy.
Close(context.Context)
}
func assertStrategy(
ctx context.Context, header SnapshotRequest_Header, expect SnapshotRequest_Strategy,
) {
if header.Strategy != expect {
log.Fatalf(ctx, "expected strategy %s, found strategy %s", expect, header.Strategy)
}
}
// kvBatchSnapshotStrategy is an implementation of snapshotStrategy that streams
// batches of KV pairs in the BatchRepr format.
type kvBatchSnapshotStrategy struct {
raftCfg *base.RaftConfig
status string
// 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() engine.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.
ssss *SSTSnapshotStorageScratch
}
// multiSSTWriter is a wrapper around RocksDBSstFileWriter and
// SSTSnapshotStorageScratch that handles chunking SSTs and persisting them to
// disk.
type multiSSTWriter struct {
ssss *SSTSnapshotStorageScratch
currSST engine.RocksDBSstFileWriter
currSSTFile *SSTSnapshotStorageFile
keyRanges []rditer.KeyRange
currRange int
// The size of the SST the last time the SST file writer was truncated. This
// size is used to determine the size of the SST chunk buffered in-memory.
truncatedSize int64
// The approximate size of the SST chunk to buffer in memory on the receiver
// before flushing to disk.
sstChunkSize int64
}
func newMultiSSTWriter(
ssss *SSTSnapshotStorageScratch, keyRanges []rditer.KeyRange, sstChunkSize int64,
) (multiSSTWriter, error) {
msstw := multiSSTWriter{
ssss: ssss,
keyRanges: keyRanges,
sstChunkSize: sstChunkSize,
}
if err := msstw.initSST(); err != nil {
return msstw, err
}
return msstw, nil
}
func (msstw *multiSSTWriter) initSST() error {
newSSTFile, err := msstw.ssss.NewFile()
if err != nil {
return errors.Wrap(err, "failed to create new sst file")
}
msstw.currSSTFile = newSSTFile
newSST, err := engine.MakeRocksDBSstFileWriter()
if err != nil {
return errors.Wrap(err, "failed to create sst file writer")
}
msstw.currSST = newSST
if err := msstw.currSST.ClearRange(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")
}
msstw.truncatedSize = 0
return nil
}
func (msstw *multiSSTWriter) finalizeSST(ctx context.Context) error {
chunk, err := msstw.currSST.Finish()
if err != nil {
return errors.Wrap(err, "failed to finish sst")
}
if err := msstw.currSSTFile.Write(ctx, chunk); err != nil {
return errors.Wrap(err, "failed to write to sst file")
}
if err := msstw.currSSTFile.Close(); err != nil {
return errors.Wrap(err, "failed to close sst file")
}
msstw.currRange++
msstw.currSST.Close()
return nil
}
func (msstw *multiSSTWriter) Put(ctx context.Context, key engine.MVCCKey, value []byte) error {
for msstw.keyRanges[msstw.currRange].End.Key.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(); err != nil {
return err
}
}
if msstw.keyRanges[msstw.currRange].Start.Key.Compare(key.Key) > 0 {
return crdberrors.AssertionFailedf("client error: expected %s to fall in one of %s", key.Key, msstw.keyRanges)
}
if err := msstw.currSST.Put(key, value); err != nil {
return errors.Wrap(err, "failed to put in sst")
}
if msstw.currSST.DataSize-msstw.truncatedSize > msstw.sstChunkSize {
msstw.truncatedSize = msstw.currSST.DataSize
chunk, err := msstw.currSST.Truncate()
if err != nil {
return errors.Wrap(err, "failed to truncate sst")
}
// NOTE: Chunk may be empty due to the semantics of Truncate(), but Write()
// handles an empty chunk as a noop.
if err := msstw.currSSTFile.Write(ctx, chunk); err != nil {
return errors.Wrap(err, "failed to write to sst file")
}
}
return nil
}
func (msstw *multiSSTWriter) Finish(ctx context.Context) error {
if msstw.currRange < len(msstw.keyRanges) {
for {
if err := msstw.finalizeSST(ctx); err != nil {
return err
}
if msstw.currRange >= len(msstw.keyRanges) {
break
}
if err := msstw.initSST(); err != nil {
return err
}
}
}
return nil
}
func (msstw *multiSSTWriter) Close() error {
msstw.currSST.Close()
return msstw.currSSTFile.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. User key range
func (kvSS *kvBatchSnapshotStrategy) Receive(
ctx context.Context, stream incomingSnapshotStream, header SnapshotRequest_Header,
) (IncomingSnapshot, error) {
assertStrategy(ctx, header, SnapshotRequest_KV_BATCH)
// At the moment we'll write at most three SSTs.
// TODO(jeffreyxiao): Re-evaluate as the default range size grows.
keyRanges := rditer.MakeReplicatedKeyRanges(header.State.Desc)
msstw, err := newMultiSSTWriter(kvSS.ssss, keyRanges, kvSS.sstChunkSize)
if err != nil {
return noSnap, err
}
defer func() {
// Nothing actionable if closing multiSSTWriter. Closing the same SST and
// SST file multiple times is idempotent.
if err := msstw.Close(); err != nil {
log.Warningf(ctx, "failed to close multiSSTWriter: %v", err)
}
}()
var logEntries [][]byte
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 := engine.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() != engine.BatchTypeValue {
return noSnap, crdberrors.AssertionFailedf("expected type %d, found type %d", engine.BatchTypeValue, batchReader.BatchType())
}
key, err := batchReader.MVCCKey()
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, err
}
}
}
if req.LogEntries != nil {
logEntries = append(logEntries, req.LogEntries...)
}
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.
if err := msstw.Finish(ctx); err != nil {
return noSnap, err
}
if err := msstw.Close(); err != nil {
return noSnap, err
}
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{
UsesUnreplicatedTruncatedState: header.UnreplicatedTruncatedState,
SnapUUID: snapUUID,
SSSS: kvSS.ssss,
LogEntries: logEntries,
State: &header.State,
snapType: header.Type,
}
// 19.1 nodes don't set the Type field on the SnapshotRequest_Header proto
// when sending this RPC, so in a mixed cluster setting we may have gotten
// the zero value of RAFT. Since the RPC didn't have type information
// previously, a 19.1 node receiving a snapshot distinguished between RAFT
// and PREEMPTIVE (19.1 nodes never sent LEARNER snapshots) by checking
// whether the replica was a placeholder (ReplicaID == 0).
//
// This adjustment can be removed after 19.2.
if inSnap.snapType == SnapshotRequest_RAFT &&
header.RaftMessageRequest.ToReplica.ReplicaID == 0 {
inSnap.snapType = SnapshotRequest_PREEMPTIVE
}
kvSS.status = fmt.Sprintf("log entries: %d, ssts: %d", len(logEntries), len(kvSS.ssss.SSTs()))
return inSnap, nil
}
}
}
// Send implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Send(
ctx context.Context,
stream outgoingSnapshotStream,
header SnapshotRequest_Header,
snap *OutgoingSnapshot,
) error {
assertStrategy(ctx, header, SnapshotRequest_KV_BATCH)
// Iterate over all keys using the provided iterator and stream out batches
// of key-values.
n := 0
var b engine.Batch
for iter := snap.Iter; ; iter.Next() {
if ok, err := iter.Valid(); err != nil {
return err
} else if !ok {
break
}
key := iter.Key()
value := iter.Value()
n++
if b == nil {
b = kvSS.newBatch()
}
if err := b.Put(key, value); err != nil {
b.Close()
return err
}
if int64(b.Len()) >= kvSS.batchSize {
if err := kvSS.sendBatch(ctx, stream, b); err != nil {
return err
}
b = nil
// We no longer need the keys and values in the batch we just sent,
// so reset ReplicaDataIterator's allocator and allow its data to
// be garbage collected.
iter.ResetAllocator()
}
}
if b != nil {
if err := kvSS.sendBatch(ctx, stream, b); err != nil {
return err
}
}
// Iterate over the specified range of Raft entries and send them all out
// together.
firstIndex := header.State.TruncatedState.Index + 1
endIndex := snap.RaftSnap.Metadata.Index + 1
preallocSize := endIndex - firstIndex
const maxPreallocSize = 1000
if preallocSize > maxPreallocSize {
// It's possible for the raft log to become enormous in certain
// sustained failure conditions. We may bail out of the snapshot
// process early in scanFunc, but in the worst case this
// preallocation is enough to run the server out of memory. Limit
// the size of the buffer we will preallocate.
preallocSize = maxPreallocSize
}
logEntries := make([][]byte, 0, preallocSize)
var raftLogBytes int64
scanFunc := func(kv roachpb.KeyValue) (bool, error) {
bytes, err := kv.Value.GetBytes()
if err == nil {
logEntries = append(logEntries, bytes)
raftLogBytes += int64(len(bytes))
if snap.snapType == SnapshotRequest_PREEMPTIVE &&
raftLogBytes > 4*kvSS.raftCfg.RaftLogTruncationThreshold {
// If the raft log is too large, abort the snapshot instead of
// potentially running out of memory. However, if this is a
// raft-initiated snapshot (instead of a preemptive one), we
// have a dilemma. It may be impossible to truncate the raft
// log until we have caught up a peer with a snapshot. Since
// we don't know the exact size at which we will run out of
// memory, we err on the size of allowing the snapshot if it
// is raft-initiated, while aborting preemptive snapshots at a
// reasonable threshold. (Empirically, this is good enough:
// the situations that result in large raft logs have not been
// observed to result in raft-initiated snapshots).
//
// By aborting preemptive snapshots here, we disallow replica
// changes until the current replicas have caught up and
// truncated the log (either the range is available, in which
// case this will eventually happen, or it's not,in which case
// the preemptive snapshot would be wasted anyway because the
// change replicas transaction would be unable to commit).
return false, errors.Errorf(
"aborting snapshot because raft log is too large "+
"(%d bytes after processing %d of %d entries)",
raftLogBytes, len(logEntries), endIndex-firstIndex)
}
}
return false, err
}
rangeID := header.State.Desc.RangeID
if err := iterateEntries(ctx, snap.EngineSnap, rangeID, firstIndex, endIndex, scanFunc); err != nil {
return err
}
// Inline the payloads for all sideloaded proposals.
//
// TODO(tschottdorf): could also send slim proposals and attach sideloaded
// SSTables directly to the snapshot. Probably the better long-term
// solution, but let's see if it ever becomes relevant. Snapshots with
// inlined proposals are hopefully the exception.
{
var ent raftpb.Entry
for i := range logEntries {
if err := protoutil.Unmarshal(logEntries[i], &ent); err != nil {
return err
}
if !sniffSideloadedRaftCommand(ent.Data) {
continue
}
if err := snap.WithSideloaded(func(ss SideloadStorage) error {
newEnt, err := maybeInlineSideloadedRaftCommand(
ctx, rangeID, ent, ss, snap.RaftEntryCache,
)
if err != nil {
return err
}
if newEnt != nil {
ent = *newEnt
}
return nil
}); err != nil {
if errors.Cause(err) == errSideloadedFileNotFound {
// We're creating the Raft snapshot based on a snapshot of
// the engine, but the Raft log may since have been
// truncated and corresponding on-disk sideloaded payloads
// unlinked. Luckily, we can just abort this snapshot; the
// caller can retry.
//
// TODO(tschottdorf): check how callers handle this. They
// should simply retry. In some scenarios, perhaps this can
// happen repeatedly and prevent a snapshot; not sending the
// log entries wouldn't help, though, and so we'd really
// need to make sure the entries are always here, for
// instance by pre-loading them into memory. Or we can make
// log truncation less aggressive about removing sideloaded
// files, by delaying trailing file deletion for a bit.
return &errMustRetrySnapshotDueToTruncation{
index: ent.Index,
term: ent.Term,
}
}
return err
}
// TODO(tschottdorf): it should be possible to reuse `logEntries[i]` here.
var err error
if logEntries[i], err = protoutil.Marshal(&ent); err != nil {
return err
}
}
}
kvSS.status = fmt.Sprintf("kv pairs: %d, log entries: %d", n, len(logEntries))
return stream.Send(&SnapshotRequest{LogEntries: logEntries})
}
func (kvSS *kvBatchSnapshotStrategy) sendBatch(
ctx context.Context, stream outgoingSnapshotStream, batch engine.Batch,
) error {
if err := kvSS.limiter.WaitN(ctx, 1); err != nil {
return err
}
repr := batch.Repr()
batch.Close()
return stream.Send(&SnapshotRequest{KVBatch: repr})
}
// Status implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Status() string { return kvSS.status }
// Close implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Close(ctx context.Context) {
if kvSS.ssss != 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.ssss.Clear(); err != nil {
log.Warningf(ctx, "error closing kvBatchSnapshotStrategy: %v", err)
}
}
}
// reserveSnapshot throttles incoming snapshots. The returned closure is used
// to cleanup the reservation and release its resources. A nil cleanup function
// and a non-empty rejectionMessage indicates the reservation was declined.
func (s *Store) reserveSnapshot(
ctx context.Context, header *SnapshotRequest_Header,
) (_cleanup func(), _rejectionMsg string, _err error) {
tBegin := timeutil.Now()
if header.RangeSize == 0 {
// 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.
} else if header.CanDecline {
storeDesc, ok := s.cfg.StorePool.getStoreDescriptor(s.StoreID())
if ok && (!maxCapacityCheck(storeDesc) || header.RangeSize > storeDesc.Capacity.Available) {
return nil, snapshotStoreTooFullMsg, nil
}
select {
case s.snapshotApplySem <- struct{}{}:
case <-ctx.Done():
return nil, "", ctx.Err()
case <-s.stopper.ShouldStop():
return nil, "", errors.Errorf("stopped")
default:
return nil, snapshotApplySemBusyMsg, nil
}
} else {
select {
case s.snapshotApplySem <- struct{}{}:
case <-ctx.Done():
return nil, "", ctx.Err()
case <-s.stopper.ShouldStop():
return nil, "", errors.Errorf("stopped")
}
}
// The choice here is essentially arbitrary, but with a default range size of 64mb and the
// Raft snapshot rate limiting of 8mb/s, we expect to spend less than 8s per snapshot.
// Preemptive snapshots are limited to 2mb/s (by default), so they can take up to 4x longer,
// but an average range is closer to 32mb, so we expect ~16s for larger preemptive snapshots,
// which is what we want to log.
const snapshotReservationWaitWarnThreshold = 13 * time.Second
if elapsed := timeutil.Since(tBegin); elapsed > snapshotReservationWaitWarnThreshold {
replDesc, _ := header.State.Desc.GetReplicaDescriptor(s.StoreID())
log.Infof(
ctx,
"waited for %.1fs to acquire snapshot reservation to r%d/%d",
elapsed.Seconds(),
header.State.Desc.RangeID,
replDesc.ReplicaID,
)
}
s.metrics.ReservedReplicaCount.Inc(1)
s.metrics.Reserved.Inc(header.RangeSize)
return func() {
s.metrics.ReservedReplicaCount.Dec(1)
s.metrics.Reserved.Dec(header.RangeSize)
if header.RangeSize != 0 {
<-s.snapshotApplySem
}
}, "", nil
}
// canApplySnapshotLocked 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 can be added to the replicasByKey map (if
// necessary). If a placeholder is required, it is returned as the first value.
//
// Both the store mu (and the raft mu for an existing replica if there is one)
// must be held.
func (s *Store) canApplySnapshotLocked(
ctx context.Context, snapHeader *SnapshotRequest_Header,
) (*ReplicaPlaceholder, error) {
if snapHeader.IsPreemptive() {
return nil, crdberrors.AssertionFailedf(`expected a raft or learner snapshot`)
}
// 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.
v, ok := s.mu.replicas.Load(
int64(desc.RangeID),
)
if !ok {
return nil, errors.Errorf("canApplySnapshotLocked requires a replica present")
}
existingRepl := (*Replica)(v)
// 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.
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. Given this is not a
// preemptive snapshot we know that its id must be non-zero.
return nil, nil
}
// If we are not alive then we should not apply a snapshot as our removal
// is imminent.
if existingDestroyStatus.RemovingOrRemoved() {
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 GC queue .
func (s *Store) checkSnapshotOverlapLocked(
ctx context.Context, snapHeader *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: canApplySnapshotLocked: 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 exRange := s.getOverlappingKeyRangeLocked(&desc); exRange != 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(exRange.Desc().RangeID)
msg := IntersectingSnapshotMsg
if err != nil {
log.Warning(ctx, errors.Wrapf(
err, "unable to look up overlapping replica on %s", exReplica))
} else {
inactive := func(r *Replica) bool {
if r.RaftStatus() == nil {
return true
}
// TODO(benesch): this check does 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.
lease, pendingLease := r.GetLease()
now := s.Clock().Now()
return !r.IsLeaseValid(lease, now) &&
(pendingLease == (roachpb.Lease{}) || !r.IsLeaseValid(pendingLease, now))
}
// We unconditionally send this replica through the GC queue. It's
// reasonably likely that the 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 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 = replicaGCPriorityCandidate
}
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
}
// shouldAcceptSnapshotData is an optimization to check whether we should even
// bother to read the data for an incoming snapshot. If the snapshot overlaps an
// existing replica or placeholder, we'd error during application anyway, so do
// it before transferring all the data. This method is a guess and may have
// false positives. If the snapshot should be rejected, an error is returned
// with a description of why. Otherwise, nil means we should accept the
// snapshot.
func (s *Store) shouldAcceptSnapshotData(
ctx context.Context, snapHeader *SnapshotRequest_Header,
) error {
if snapHeader.IsPreemptive() {
return crdberrors.AssertionFailedf(`expected a raft or learner snapshot`)
}
pErr := s.withReplicaForRequest(ctx, &snapHeader.RaftMessageRequest,
func(ctx context.Context, r *Replica) *roachpb.Error {
// If the current replica is not initialized then we should accept this
// snapshot if it doesn't overlap existing ranges.
if !r.IsInitialized() {
s.mu.Lock()
defer s.mu.Unlock()
return roachpb.NewError(s.checkSnapshotOverlapLocked(ctx, snapHeader))
}
// If the current range is initialized then we need to accept this
// this snapshot. We also know that this initialized range must be
// initialized as this replica as we'll clear out any preemptive
// snapshots when detecting a learner snapshot in getOrCreateReplica.
return nil
})
return pErr.GoError()
}
// receiveSnapshot receives an incoming snapshot via a pre-opened GRPC stream.
func (s *Store) receiveSnapshot(
ctx context.Context, header *SnapshotRequest_Header, stream incomingSnapshotStream,
) error {
if fn := s.cfg.TestingKnobs.ReceiveSnapshot; fn != nil {
if err := fn(header); err != nil {
return sendSnapshotError(stream, err)
}
}
// Defensive check that any non-preemptive snapshot contains this store in the
// descriptor.
if !header.IsPreemptive() {
storeID := s.StoreID()
if _, ok := header.State.Desc.GetReplicaDescriptor(storeID); !ok {
return crdberrors.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, rejectionMsg, err := s.reserveSnapshot(ctx, header)
if err != nil {
return err
}
if cleanup == nil {
return stream.Send(&SnapshotResponse{
Status: SnapshotResponse_DECLINED,
Message: rejectionMsg,
})
}
defer cleanup()
// Check to see if the snapshot can be applied but don't attempt to add
// a placeholder here, because we're not holding the replica's raftMu.
// We'll perform this check again later after receiving the rest of the
// snapshot data - this is purely an optimization to prevent downloading
// a snapshot that we know we won't be able to apply.
if header.IsPreemptive() {
if _, err := s.canApplyPreemptiveSnapshot(ctx, header, false /* authoritative */); err != nil {
return sendSnapshotError(stream,
errors.Wrapf(err, "%s,r%d: cannot apply snapshot", s, header.State.Desc.RangeID),
)
}
} else {
if err := s.shouldAcceptSnapshotData(ctx, header); err != nil {
return sendSnapshotError(stream,
errors.Wrapf(err, "%s,r%d: cannot apply snapshot", s, header.State.Desc.RangeID),
)
}
}
// 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 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{
raftCfg: &s.cfg.RaftConfig,
ssss: s.sss.NewSSTSnapshotStorageScratch(header.State.Desc.RangeID, snapUUID),
sstChunkSize: snapshotSSTWriteSyncRate.Get(&s.cfg.Settings.SV),
}
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(&SnapshotResponse{Status: 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
}
if header.IsPreemptive() {
if err := s.processPreemptiveSnapshotRequest(ctx, header, inSnap); err != nil {
return sendSnapshotError(stream, errors.Wrap(err.GoError(), "failed to apply snapshot"))
}
} else {
if err := s.processRaftSnapshotRequest(ctx, header, inSnap); err != nil {
return sendSnapshotError(stream, errors.Wrap(err.GoError(), "failed to apply snapshot"))
}
}
return stream.Send(&SnapshotResponse{Status: SnapshotResponse_APPLIED})
}
func sendSnapshotError(stream incomingSnapshotStream, err error) error {
return stream.Send(&SnapshotResponse{
Status: 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)
}
// rebalanceSnapshotRate is the rate at which preemptive snapshots can be sent.
// This includes snapshots generated for upreplication or for rebalancing.
var rebalanceSnapshotRate = settings.RegisterByteSizeSetting(
"kv.snapshot_rebalance.max_rate",
"the rate limit (bytes/sec) to use for rebalance and upreplication snapshots",
envutil.EnvOrDefaultBytes("COCKROACH_PREEMPTIVE_SNAPSHOT_RATE", 8<<20),
)
// recoverySnapshotRate is the rate at which Raft-initiated spanshots can be
// sent. Ideally, one would never see a Raft-initiated snapshot; we'd like all
// the snapshots to be preemptive. 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.
var recoverySnapshotRate = settings.RegisterByteSizeSetting(
"kv.snapshot_recovery.max_rate",
"the rate limit (bytes/sec) to use for recovery snapshots",
envutil.EnvOrDefaultBytes("COCKROACH_RAFT_SNAPSHOT_RATE", 8<<20),
)
// 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(
"kv.snapshot_sst.sync_size",
"threshold after which snapshot SST writes must fsync",
2<<20, /* 2 MiB */
)
func snapshotRateLimit(
st *cluster.Settings, priority SnapshotRequest_Priority,
) (rate.Limit, error) {
switch priority {
case SnapshotRequest_RECOVERY:
return rate.Limit(recoverySnapshotRate.Get(&st.SV)), nil
case SnapshotRequest_REBALANCE:
return rate.Limit(rebalanceSnapshotRate.Get(&st.SV)), nil
default:
return 0, errors.Errorf("unknown snapshot priority: %s", priority)
}
}
type errMustRetrySnapshotDueToTruncation struct {
index, term uint64
}
func (e *errMustRetrySnapshotDueToTruncation) Error() string {
return fmt.Sprintf(
"log truncation during snapshot removed sideloaded SSTable at index %d, term %d",
e.index, e.term,
)
}
// sendSnapshot sends an outgoing snapshot via a pre-opened GRPC stream.
func sendSnapshot(
ctx context.Context,
raftCfg *base.RaftConfig,
st *cluster.Settings,
stream outgoingSnapshotStream,
storePool SnapshotStorePool,
header SnapshotRequest_Header,
snap *OutgoingSnapshot,
newBatch func() engine.Batch,
sent func(),
) error {
start := timeutil.Now()
to := header.RaftMessageRequest.ToReplica
if err := stream.Send(&SnapshotRequest{Header: &header}); err != nil {
return err
}
// Wait until we get a response from the server.
resp, err := stream.Recv()
if err != nil {
storePool.throttle(throttleFailed, err.Error(), to.StoreID)
return err
}
switch resp.Status {
case SnapshotResponse_DECLINED:
if header.CanDecline {
declinedMsg := "reservation rejected"
if len(resp.Message) > 0 {
declinedMsg = resp.Message
}
err := &benignError{errors.Errorf("%s: remote declined %s: %s", to, snap, declinedMsg)}
storePool.throttle(throttleDeclined, err.Error(), to.StoreID)
return err
}
err := errors.Errorf("%s: programming error: remote declined required %s: %s",
to, snap, resp.Message)
storePool.throttle(throttleFailed, err.Error(), to.StoreID)
return err
case SnapshotResponse_ERROR:
storePool.throttle(throttleFailed, resp.Message, to.StoreID)
return errors.Errorf("%s: remote couldn't accept %s with error: %s",
to, snap, resp.Message)
case SnapshotResponse_ACCEPTED:
// This is the response we're expecting. Continue with snapshot sending.
default:
err := errors.Errorf("%s: server sent an invalid status while negotiating %s: %s",
to, snap, resp.Status)
storePool.throttle(throttleFailed, err.Error(), to.StoreID)
return err
}
log.Infof(ctx, "sending %s", snap)
// The size of batches to send. This is the granularity of rate limiting.
const batchSize = 256 << 10 // 256 KB
targetRate, err := snapshotRateLimit(st, header.Priority)
if err != nil {
return errors.Wrapf(err, "%s", to)
}
// Convert the bytes/sec rate limit to batches/sec.
//
// TODO(peter): Using bytes/sec for rate limiting seems more natural but has
// practical difficulties. We either need to use a very large burst size
// which seems to disable the rate limiting, or call WaitN in smaller than
// burst size chunks which caused excessive slowness in testing. Would be
// nice to figure this out, but the batches/sec rate limit works for now.
limiter := rate.NewLimiter(targetRate/batchSize, 1 /* burst size */)
// Create a snapshotStrategy based on the desired snapshot strategy.
var ss snapshotStrategy
switch header.Strategy {