99 "context"
1010
1111 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
12+ "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage/snaprecv"
1213 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/rditer"
1314 "github.com/cockroachdb/cockroach/pkg/raft/raftpb"
1415 "github.com/cockroachdb/cockroach/pkg/roachpb"
@@ -43,7 +44,7 @@ type kvBatchSnapshotStrategy struct {
4344 // before flushing to disk. Only used on the receiver side.
4445 sstChunkSize int64
4546 // Only used on the receiver side.
46- scratch * SSTSnapshotStorageScratch
47+ scratch * snaprecv. SSTSnapshotStorageScratch
4748 st * cluster.Settings
4849 clusterID uuid.UUID
4950}
@@ -104,21 +105,24 @@ func (kvSS *kvBatchSnapshotStrategy) Receive(
104105 return noSnap , sendSnapshotError (ctx , s , stream , errors .New ("cannot accept shared sstables" ))
105106 }
106107
107- // We rely on the last keyRange passed into multiSSTWriter being the user key
108+ // We rely on the last keyRange passed into MultiSSTWriter being the user key
108109 // span. If the sender signals that it can no longer do shared replication
109110 // (with a TransitionFromSharedToRegularReplicate = true), we will have to
110- // switch to adding a rangedel for that span. Since multiSSTWriter acts on an
111+ // switch to adding a rangedel for that span. Since MultiSSTWriter acts on an
111112 // opaque slice of keyRanges, we just tell it to add a rangedel for the last
112113 // span. To avoid bugs, assert on the last span in keyRanges actually being
113114 // equal to the user key span.
114115 if ! keyRanges [len (keyRanges )- 1 ].Equal (header .State .Desc .KeySpan ().AsRawSpanWithNoLocals ()) {
115- return noSnap , errors .AssertionFailedf ("last span in multiSSTWriter did not equal the user key span: %s" , keyRanges [len (keyRanges )- 1 ].String ())
116+ return noSnap , errors .AssertionFailedf ("last span in MultiSSTWriter did not equal the user key span: %s" , keyRanges [len (keyRanges )- 1 ].String ())
116117 }
117118
118119 // The last key range is the user key span.
119120 localRanges := keyRanges [:len (keyRanges )- 1 ]
120121 mvccRange := keyRanges [len (keyRanges )- 1 ]
121- msstw , err := newMultiSSTWriter (ctx , kvSS .st , kvSS .scratch , localRanges , mvccRange , kvSS .sstChunkSize )
122+ msstw , err := snaprecv .NewMultiSSTWriter (ctx , kvSS .st , kvSS .scratch , localRanges , mvccRange , snaprecv.MultiSSTWriterOptions {
123+ SSTChunkSize : kvSS .sstChunkSize ,
124+ MaxSSTSize : MaxSnapshotSSTableSize .Get (& kvSS .st .SV ),
125+ })
122126 if err != nil {
123127 return noSnap , err
124128 }
@@ -168,9 +172,9 @@ func (kvSS *kvBatchSnapshotStrategy) Receive(
168172 // All batch operations are guaranteed to be point key or range key puts.
169173 for batchReader .Next () {
170174 // TODO(lyang24): maybe avoid decoding engine key twice.
171- // msstw calls (i.e. PutInternalPointKey ) can use the decoded engine key here as input.
175+ // msstw calls (i.e. putInternalPointKey ) can use the decoded engine key here as input.
172176
173- bytesEstimate := msstw .estimatedDataSize ()
177+ bytesEstimate := msstw .EstimatedDataSize ()
174178 delta := bytesEstimate - prevBytesEstimate
175179 // Calling nil pacer is a noop.
176180 if err := pacer .Pace (ctx , delta , false /* final */ ); err != nil {
@@ -189,7 +193,7 @@ func (kvSS *kvBatchSnapshotStrategy) Receive(
189193 }
190194 }
191195
192- if err := kvSS . readOneToBatch (ctx , ek , header .SharedReplicate , batchReader , msstw ); err != nil {
196+ if err := msstw . ReadOne (ctx , ek , header .SharedReplicate , batchReader ); err != nil {
193197 return noSnap , err
194198 }
195199 }
@@ -238,8 +242,7 @@ func (kvSS *kvBatchSnapshotStrategy) Receive(
238242 // we must still construct SSTs with range deletion tombstones to remove
239243 // the data.
240244 timingTag .start ("sst" )
241- dataSize , err := msstw .Finish (ctx )
242- sstSize := msstw .sstSize
245+ dataSize , sstSize , err := msstw .Finish (ctx )
243246 if err != nil {
244247 return noSnap , errors .Wrapf (err , "finishing sst for raft snapshot" )
245248 }
@@ -286,79 +289,6 @@ func (kvSS *kvBatchSnapshotStrategy) Receive(
286289 }
287290}
288291
289- func (kvSS * kvBatchSnapshotStrategy ) readOneToBatch (
290- ctx context.Context ,
291- ek storage.EngineKey ,
292- shared bool , // may receive shared SSTs
293- batchReader * storage.BatchReader ,
294- msstw * multiSSTWriter ,
295- ) error {
296- switch batchReader .KeyKind () {
297- case pebble .InternalKeyKindSet , pebble .InternalKeyKindSetWithDelete :
298- if err := msstw .Put (ctx , ek , batchReader .Value ()); err != nil {
299- return errors .Wrapf (err , "writing sst for raft snapshot" )
300- }
301- case pebble .InternalKeyKindDelete , pebble .InternalKeyKindDeleteSized :
302- if ! shared {
303- return errors .AssertionFailedf ("unexpected batch entry key kind %d" , batchReader .KeyKind ())
304- }
305- if err := msstw .PutInternalPointKey (ctx , batchReader .Key (), batchReader .KeyKind (), nil ); err != nil {
306- return errors .Wrapf (err , "writing sst for raft snapshot" )
307- }
308- case pebble .InternalKeyKindRangeDelete :
309- if ! shared {
310- return errors .AssertionFailedf ("unexpected batch entry key kind %d" , batchReader .KeyKind ())
311- }
312- start := batchReader .Key ()
313- end , err := batchReader .EndKey ()
314- if err != nil {
315- return err
316- }
317- if err := msstw .PutInternalRangeDelete (ctx , start , end ); err != nil {
318- return errors .Wrapf (err , "writing sst for raft snapshot" )
319- }
320-
321- case pebble .InternalKeyKindRangeKeyUnset , pebble .InternalKeyKindRangeKeyDelete :
322- if ! shared {
323- return errors .AssertionFailedf ("unexpected batch entry key kind %d" , batchReader .KeyKind ())
324- }
325- start := batchReader .Key ()
326- end , err := batchReader .EndKey ()
327- if err != nil {
328- return err
329- }
330- rangeKeys , err := batchReader .RawRangeKeys ()
331- if err != nil {
332- return err
333- }
334- for _ , rkv := range rangeKeys {
335- err := msstw .PutInternalRangeKey (ctx , start , end , rkv )
336- if err != nil {
337- return errors .Wrapf (err , "writing sst for raft snapshot" )
338- }
339- }
340- case pebble .InternalKeyKindRangeKeySet :
341- start := ek
342- end , err := batchReader .EngineEndKey ()
343- if err != nil {
344- return err
345- }
346- rangeKeys , err := batchReader .EngineRangeKeys ()
347- if err != nil {
348- return err
349- }
350- for _ , rkv := range rangeKeys {
351- err := msstw .PutRangeKey (ctx , start .Key , end .Key , rkv .Version , rkv .Value )
352- if err != nil {
353- return errors .Wrapf (err , "writing sst for raft snapshot" )
354- }
355- }
356- default :
357- return errors .AssertionFailedf ("unexpected batch entry key kind %d" , batchReader .KeyKind ())
358- }
359- return nil
360- }
361-
362292// Send implements the snapshotStrategy interface.
363293func (kvSS * kvBatchSnapshotStrategy ) Send (
364294 ctx context.Context ,
0 commit comments