-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
pebble_batch.go
708 lines (629 loc) · 20.8 KB
/
pebble_batch.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
// 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 storage
import (
"context"
"sync"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage/pebbleiter"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/rangekey"
)
// Wrapper struct around a pebble.Batch.
type pebbleBatch struct {
db *pebble.DB
batch *pebble.Batch
buf []byte
// The iterator reuse optimization in pebbleBatch is for servicing a
// BatchRequest, such that the iterators get reused across different
// requests in the batch.
// Reuse iterators for {normal,prefix} x {MVCCKey,EngineKey} iteration. We
// need separate iterators for EngineKey and MVCCKey iteration since
// iterators that make separated locks/intents look as interleaved need to
// use both simultaneously.
// When the first iterator is initialized, or when
// PinEngineStateForIterators is called (whichever happens first), the
// underlying *pebble.Iterator is stashed in iter, so that subsequent
// iterator initialization can use Iterator.Clone to use the same underlying
// engine state. This relies on the fact that all pebbleIterators created
// here are marked as reusable, which causes pebbleIterator.Close to not
// close iter. iter will be closed when pebbleBatch.Close is called.
prefixIter pebbleIterator
normalIter pebbleIterator
prefixEngineIter pebbleIterator
normalEngineIter pebbleIterator
iter pebbleiter.Iterator
iterUsed bool // avoids cloning after PinEngineStateForIterators()
writeOnly bool
closed bool
wrappedIntentWriter intentDemuxWriter
// scratch space for wrappedIntentWriter.
scratch []byte
iterStatsReporter iterStatsReporter
batchStatsReporter batchStatsReporter
settings *cluster.Settings
mayWriteSizedDeletes bool
shouldWriteLocalTimestamps bool
shouldWriteLocalTimestampsCached bool
}
var _ Batch = &pebbleBatch{}
var pebbleBatchPool = sync.Pool{
New: func() interface{} {
return &pebbleBatch{}
},
}
type batchStatsReporter interface {
aggregateBatchCommitStats(stats BatchCommitStats)
}
// Instantiates a new pebbleBatch.
func newPebbleBatch(
db *pebble.DB,
batch *pebble.Batch,
writeOnly bool,
settings *cluster.Settings,
iterStatsReporter iterStatsReporter,
batchStatsReporter batchStatsReporter,
) *pebbleBatch {
pb := pebbleBatchPool.Get().(*pebbleBatch)
*pb = pebbleBatch{
db: db,
batch: batch,
buf: pb.buf,
prefixIter: pebbleIterator{
lowerBoundBuf: pb.prefixIter.lowerBoundBuf,
upperBoundBuf: pb.prefixIter.upperBoundBuf,
reusable: true,
},
normalIter: pebbleIterator{
lowerBoundBuf: pb.normalIter.lowerBoundBuf,
upperBoundBuf: pb.normalIter.upperBoundBuf,
reusable: true,
},
prefixEngineIter: pebbleIterator{
lowerBoundBuf: pb.prefixEngineIter.lowerBoundBuf,
upperBoundBuf: pb.prefixEngineIter.upperBoundBuf,
reusable: true,
},
normalEngineIter: pebbleIterator{
lowerBoundBuf: pb.normalEngineIter.lowerBoundBuf,
upperBoundBuf: pb.normalEngineIter.upperBoundBuf,
reusable: true,
},
writeOnly: writeOnly,
iterStatsReporter: iterStatsReporter,
batchStatsReporter: batchStatsReporter,
settings: settings,
// NB: We do not use settings.Version.IsActive because we do not
// generally have a guarantee that the cluster version has been
// initialized. As a part of initializing a store, we use a Batch to
// write the store identifer key; this is written before any cluster
// version has been initialized.
mayWriteSizedDeletes: settings.Version.ActiveVersionOrEmpty(context.TODO()).
IsActive(clusterversion.V23_2_UseSizedPebblePointTombstones),
}
pb.wrappedIntentWriter = wrapIntentWriter(pb)
return pb
}
// Close implements the Batch interface.
func (p *pebbleBatch) Close() {
if p.closed {
panic("closing an already-closed pebbleBatch")
}
p.closed = true
if p.iter != nil && !p.iterUsed {
if err := p.iter.Close(); err != nil {
panic(err)
}
}
// Setting iter to nil is sufficient since it will be closed by one of the
// subsequent destroy calls.
p.iter = nil
// Destroy the iterators before closing the batch.
p.prefixIter.destroy()
p.normalIter.destroy()
p.prefixEngineIter.destroy()
p.normalEngineIter.destroy()
_ = p.batch.Close()
p.batch = nil
pebbleBatchPool.Put(p)
}
// Closed implements the Batch interface.
func (p *pebbleBatch) Closed() bool {
return p.closed
}
// MVCCIterate implements the Batch interface.
func (p *pebbleBatch) MVCCIterate(
start, end roachpb.Key,
iterKind MVCCIterKind,
keyTypes IterKeyType,
f func(MVCCKeyValue, MVCCRangeKeyStack) error,
) error {
if iterKind == MVCCKeyAndIntentsIterKind {
r := wrapReader(p)
// Doing defer r.Free() does not inline.
err := iterateOnReader(r, start, end, iterKind, keyTypes, f)
r.Free()
return err
}
return iterateOnReader(p, start, end, iterKind, keyTypes, f)
}
// NewMVCCIterator implements the Batch interface.
func (p *pebbleBatch) NewMVCCIterator(iterKind MVCCIterKind, opts IterOptions) MVCCIterator {
if p.writeOnly {
panic("write-only batch")
}
if iterKind == MVCCKeyAndIntentsIterKind {
r := wrapReader(p)
// Doing defer r.Free() does not inline.
iter := r.NewMVCCIterator(iterKind, opts)
r.Free()
return maybeWrapInUnsafeIter(iter)
}
iter := &p.normalIter
if opts.Prefix {
iter = &p.prefixIter
}
handle := pebble.Reader(p.batch)
if !p.batch.Indexed() {
handle = p.db
}
if iter.inuse {
return newPebbleIteratorByCloning(CloneContext{
rawIter: p.iter,
statsReporter: p.iterStatsReporter,
}, opts, StandardDurability)
}
if iter.iter != nil {
iter.setOptions(opts, StandardDurability)
} else {
iter.initReuseOrCreate(handle, p.iter, p.iterUsed, opts, StandardDurability, p.iterStatsReporter)
if p.iter == nil {
// For future cloning.
p.iter = iter.iter
}
p.iterUsed = true
}
iter.inuse = true
return maybeWrapInUnsafeIter(iter)
}
// NewEngineIterator implements the Batch interface.
func (p *pebbleBatch) NewEngineIterator(opts IterOptions) EngineIterator {
if p.writeOnly {
panic("write-only batch")
}
iter := &p.normalEngineIter
if opts.Prefix {
iter = &p.prefixEngineIter
}
handle := pebble.Reader(p.batch)
if !p.batch.Indexed() {
handle = p.db
}
if iter.inuse {
return newPebbleIteratorByCloning(CloneContext{
rawIter: p.iter,
statsReporter: p.iterStatsReporter,
}, opts, StandardDurability)
}
if iter.iter != nil {
iter.setOptions(opts, StandardDurability)
} else {
iter.initReuseOrCreate(handle, p.iter, p.iterUsed, opts, StandardDurability, p.iterStatsReporter)
if p.iter == nil {
// For future cloning.
p.iter = iter.iter
}
p.iterUsed = true
}
iter.inuse = true
return iter
}
// ScanInternal implements the Reader interface.
func (p *pebbleBatch) ScanInternal(
ctx context.Context,
lower, upper roachpb.Key,
visitPointKey func(key *pebble.InternalKey, value pebble.LazyValue) error,
visitRangeDel func(start []byte, end []byte, seqNum uint64) error,
visitRangeKey func(start []byte, end []byte, keys []rangekey.Key) error,
visitSharedFile func(sst *pebble.SharedSSTMeta) error,
) error {
panic("ScanInternal only supported on Engine and Snapshot.")
}
// ClearRawEncodedRange implements the InternalWriter interface.
func (p *pebbleBatch) ClearRawEncodedRange(start, end []byte) error {
return p.batch.DeleteRange(start, end, pebble.Sync)
}
// ConsistentIterators implements the Batch interface.
func (p *pebbleBatch) ConsistentIterators() bool {
return true
}
// PinEngineStateForIterators implements the Batch interface.
func (p *pebbleBatch) PinEngineStateForIterators() error {
if p.iter == nil {
if p.batch.Indexed() {
p.iter = pebbleiter.MaybeWrap(p.batch.NewIter(nil))
} else {
p.iter = pebbleiter.MaybeWrap(p.db.NewIter(nil))
}
// NB: p.iterUsed == false avoids cloning this in NewMVCCIterator(). We've
// just created it, so cloning it would just be overhead.
}
return nil
}
// NewMVCCIterator implements the Batch interface.
func (p *pebbleBatch) ApplyBatchRepr(repr []byte, sync bool) error {
var batch pebble.Batch
if err := batch.SetRepr(repr); err != nil {
return err
}
return p.batch.Apply(&batch, nil)
}
// ClearMVCC implements the Batch interface.
func (p *pebbleBatch) ClearMVCC(key MVCCKey, opts ClearOptions) error {
if key.Timestamp.IsEmpty() {
panic("ClearMVCC timestamp is empty")
}
return p.clear(key, opts)
}
// ClearUnversioned implements the Batch interface.
func (p *pebbleBatch) ClearUnversioned(key roachpb.Key, opts ClearOptions) error {
return p.clear(MVCCKey{Key: key}, opts)
}
// ClearIntent implements the Batch interface.
func (p *pebbleBatch) ClearIntent(
key roachpb.Key, txnDidNotUpdateMeta bool, txnUUID uuid.UUID, opts ClearOptions,
) error {
var err error
p.scratch, err = p.wrappedIntentWriter.ClearIntent(key, txnDidNotUpdateMeta, txnUUID, p.scratch, opts)
return err
}
// ClearEngineKey implements the Batch interface.
func (p *pebbleBatch) ClearEngineKey(key EngineKey, opts ClearOptions) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
p.buf = key.EncodeToBuf(p.buf[:0])
if !opts.ValueSizeKnown || !p.mayWriteSizedDeletes {
return p.batch.Delete(p.buf, nil)
}
return p.batch.DeleteSized(p.buf, opts.ValueSize, nil)
}
func (p *pebbleBatch) clear(key MVCCKey, opts ClearOptions) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
p.buf = EncodeMVCCKeyToBuf(p.buf[:0], key)
if !opts.ValueSizeKnown || !p.mayWriteSizedDeletes {
return p.batch.Delete(p.buf, nil)
}
return p.batch.DeleteSized(p.buf, opts.ValueSize, nil)
}
// SingleClearEngineKey implements the Batch interface.
func (p *pebbleBatch) SingleClearEngineKey(key EngineKey) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
p.buf = key.EncodeToBuf(p.buf[:0])
return p.batch.SingleDelete(p.buf, nil)
}
// ClearRawRange implements the Batch interface.
func (p *pebbleBatch) ClearRawRange(start, end roachpb.Key, pointKeys, rangeKeys bool) error {
p.buf = EngineKey{Key: start}.EncodeToBuf(p.buf[:0])
endRaw := EngineKey{Key: end}.Encode()
if pointKeys {
if err := p.batch.DeleteRange(p.buf, endRaw, pebble.Sync); err != nil {
return err
}
}
if rangeKeys {
if err := p.batch.RangeKeyDelete(p.buf, endRaw, pebble.Sync); err != nil {
return err
}
}
return nil
}
// ClearMVCCRange implements the Batch interface.
func (p *pebbleBatch) ClearMVCCRange(start, end roachpb.Key, pointKeys, rangeKeys bool) error {
var err error
p.scratch, err = p.wrappedIntentWriter.ClearMVCCRange(start, end, pointKeys, rangeKeys, p.scratch)
return err
}
// ClearMVCCVersions implements the Batch interface.
func (p *pebbleBatch) ClearMVCCVersions(start, end MVCCKey) error {
p.buf = EncodeMVCCKeyToBuf(p.buf[:0], start)
return p.batch.DeleteRange(p.buf, EncodeMVCCKey(end), nil)
}
// ClearMVCCIteratorRange implements the Batch interface.
func (p *pebbleBatch) ClearMVCCIteratorRange(
start, end roachpb.Key, pointKeys, rangeKeys bool,
) error {
clearPointKeys := func(start, end roachpb.Key) error {
iter := p.NewMVCCIterator(MVCCKeyAndIntentsIterKind, IterOptions{
KeyTypes: IterKeyTypePointsOnly,
LowerBound: start,
UpperBound: end,
})
defer iter.Close()
for iter.SeekGE(MVCCKey{Key: start}); ; iter.Next() {
if valid, err := iter.Valid(); err != nil {
return err
} else if !valid {
break
}
// NB: UnsafeRawKey could be a serialized lock table key, and not just an
// MVCCKey.
if err := p.batch.Delete(iter.UnsafeRawKey(), nil); err != nil {
return err
}
}
return nil
}
if pointKeys {
if err := clearPointKeys(start, end); err != nil {
return err
}
}
clearRangeKeys := func(start, end roachpb.Key) error {
iter := p.NewMVCCIterator(MVCCKeyIterKind, IterOptions{
KeyTypes: IterKeyTypeRangesOnly,
LowerBound: start,
UpperBound: end,
})
defer iter.Close()
for iter.SeekGE(MVCCKey{Key: start}); ; iter.Next() {
if valid, err := iter.Valid(); err != nil {
return err
} else if !valid {
break
}
// TODO(erikgrinaker): We should consider reusing a buffer for the
// encoding here, but we don't expect to see many range keys.
rangeKeys := iter.RangeKeys()
startRaw := EncodeMVCCKey(MVCCKey{Key: rangeKeys.Bounds.Key})
endRaw := EncodeMVCCKey(MVCCKey{Key: rangeKeys.Bounds.EndKey})
for _, v := range rangeKeys.Versions {
if err := p.batch.RangeKeyUnset(startRaw, endRaw,
EncodeMVCCTimestampSuffix(v.Timestamp), nil); err != nil {
return err
}
}
}
return nil
}
if rangeKeys {
if err := clearRangeKeys(start, end); err != nil {
return err
}
}
return nil
}
// ClearMVCCRangeKey implements the Engine interface.
func (p *pebbleBatch) ClearMVCCRangeKey(rangeKey MVCCRangeKey) error {
if err := rangeKey.Validate(); err != nil {
return err
}
return p.ClearEngineRangeKey(
rangeKey.StartKey, rangeKey.EndKey, EncodeMVCCTimestampSuffix(rangeKey.Timestamp))
}
// BufferedSize implements the Engine interface.
func (p *pebbleBatch) BufferedSize() int {
return p.Len()
}
// PutMVCCRangeKey implements the Batch interface.
func (p *pebbleBatch) PutMVCCRangeKey(rangeKey MVCCRangeKey, value MVCCValue) error {
// NB: all MVCC APIs currently assume all range keys are range tombstones.
if !value.IsTombstone() {
return errors.New("range keys can only be MVCC range tombstones")
}
valueRaw, err := EncodeMVCCValue(value)
if err != nil {
return errors.Wrapf(err, "failed to encode MVCC value for range key %s", rangeKey)
}
return p.PutRawMVCCRangeKey(rangeKey, valueRaw)
}
// PutRawMVCCRangeKey implements the Batch interface.
func (p *pebbleBatch) PutRawMVCCRangeKey(rangeKey MVCCRangeKey, value []byte) error {
if err := rangeKey.Validate(); err != nil {
return err
}
return p.PutEngineRangeKey(
rangeKey.StartKey, rangeKey.EndKey, EncodeMVCCTimestampSuffix(rangeKey.Timestamp), value)
}
// PutEngineRangeKey implements the Engine interface.
func (p *pebbleBatch) PutEngineRangeKey(start, end roachpb.Key, suffix, value []byte) error {
return p.batch.RangeKeySet(
EngineKey{Key: start}.Encode(), EngineKey{Key: end}.Encode(), suffix, value, nil)
}
// PutInternalRangeKey implements the InternalWriter interface.
func (p *pebbleBatch) PutInternalRangeKey(start, end []byte, key rangekey.Key) error {
switch key.Kind() {
case pebble.InternalKeyKindRangeKeyUnset:
return p.batch.RangeKeyUnset(start, end, key.Suffix, nil /* writeOptions */)
case pebble.InternalKeyKindRangeKeySet:
return p.batch.RangeKeySet(start, end, key.Suffix, key.Value, nil /* writeOptions */)
case pebble.InternalKeyKindRangeKeyDelete:
return p.batch.RangeKeyDelete(start, end, nil /* writeOptions */)
default:
panic("unexpected range key kind")
}
}
// ClearEngineRangeKey implements the Engine interface.
func (p *pebbleBatch) ClearEngineRangeKey(start, end roachpb.Key, suffix []byte) error {
return p.batch.RangeKeyUnset(
EngineKey{Key: start}.Encode(), EngineKey{Key: end}.Encode(), suffix, nil)
}
// Merge implements the Batch interface.
func (p *pebbleBatch) Merge(key MVCCKey, value []byte) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
p.buf = EncodeMVCCKeyToBuf(p.buf[:0], key)
return p.batch.Merge(p.buf, value, nil)
}
// PutMVCC implements the Batch interface.
func (p *pebbleBatch) PutMVCC(key MVCCKey, value MVCCValue) error {
if key.Timestamp.IsEmpty() {
panic("PutMVCC timestamp is empty")
}
encValue, err := EncodeMVCCValue(value)
if err != nil {
return err
}
return p.put(key, encValue)
}
// PutRawMVCC implements the Batch interface.
func (p *pebbleBatch) PutRawMVCC(key MVCCKey, value []byte) error {
if key.Timestamp.IsEmpty() {
panic("PutRawMVCC timestamp is empty")
}
return p.put(key, value)
}
// PutUnversioned implements the Batch interface.
func (p *pebbleBatch) PutUnversioned(key roachpb.Key, value []byte) error {
return p.put(MVCCKey{Key: key}, value)
}
// PutIntent implements the Batch interface.
func (p *pebbleBatch) PutIntent(
ctx context.Context, key roachpb.Key, value []byte, txnUUID uuid.UUID,
) error {
var err error
p.scratch, err = p.wrappedIntentWriter.PutIntent(ctx, key, value, txnUUID, p.scratch)
return err
}
// PutEngineKey implements the Batch interface.
func (p *pebbleBatch) PutEngineKey(key EngineKey, value []byte) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
p.buf = key.EncodeToBuf(p.buf[:0])
return p.batch.Set(p.buf, value, nil)
}
// PutInternalKey implements the WriteBatch interface.
func (p *pebbleBatch) PutInternalKey(key *pebble.InternalKey, value []byte) error {
if len(key.UserKey) == 0 {
return emptyKeyError()
}
return p.batch.AddInternalKey(key, value, nil /* writeOptions */)
}
func (p *pebbleBatch) put(key MVCCKey, value []byte) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
p.buf = EncodeMVCCKeyToBuf(p.buf[:0], key)
return p.batch.Set(p.buf, value, nil)
}
// LogData implements the Batch interface.
func (p *pebbleBatch) LogData(data []byte) error {
return p.batch.LogData(data, nil)
}
func (p *pebbleBatch) LogLogicalOp(op MVCCLogicalOpType, details MVCCLogicalOpDetails) {
// No-op.
}
// Commit implements the Batch interface.
func (p *pebbleBatch) Commit(sync bool) error {
opts := pebble.NoSync
if sync {
opts = pebble.Sync
}
if p.batch == nil {
panic("called with nil batch")
}
err := p.batch.Commit(opts)
if err != nil {
// TODO(storage): ensure that these errors are only ever due to invariant
// violations and never due to unrecoverable Pebble states. Then switch to
// returning the error instead of panicking.
//
// Once we do that, document on the storage.Batch interface the meaning of
// an error returned from this method and the guarantees that callers have
// or don't have after they receive an error from this method.
panic(err)
}
p.batchStatsReporter.aggregateBatchCommitStats(
BatchCommitStats{p.batch.CommitStats()})
return err
}
// CommitNoSyncWait implements the Batch interface.
func (p *pebbleBatch) CommitNoSyncWait() error {
if p.batch == nil {
panic("called with nil batch")
}
err := p.db.ApplyNoSyncWait(p.batch, pebble.Sync)
if err != nil {
// TODO(storage): ensure that these errors are only ever due to invariant
// violations and never due to unrecoverable Pebble states. Then switch to
// returning the error instead of panicking.
//
// Once we do that, document on the storage.Batch interface the meaning of
// an error returned from this method and the guarantees that callers have
// or don't have after they receive an error from this method.
panic(err)
}
return err
}
// SyncWait implements the Batch interface.
func (p *pebbleBatch) SyncWait() error {
if p.batch == nil {
panic("called with nil batch")
}
err := p.batch.SyncWait()
if err != nil {
// TODO(storage): ensure that these errors are only ever due to invariant
// violations and never due to unrecoverable Pebble states. Then switch to
// returning the error instead of panicking.
//
// Once we do that, document on the storage.Batch interface the meaning of
// an error returned from this method and the guarantees that callers have
// or don't have after they receive an error from this method.
panic(err)
}
p.batchStatsReporter.aggregateBatchCommitStats(
BatchCommitStats{p.batch.CommitStats()})
return err
}
// Empty implements the Batch interface.
func (p *pebbleBatch) Empty() bool {
return p.batch.Count() == 0
}
// Count implements the Batch interface.
func (p *pebbleBatch) Count() uint32 {
return p.batch.Count()
}
// Len implements the Batch interface.
func (p *pebbleBatch) Len() int {
return len(p.batch.Repr())
}
// Repr implements the Batch interface.
func (p *pebbleBatch) Repr() []byte {
// Repr expects a "safe" byte slice as its output. The return value of
// p.batch.Repr() is an unsafe byte slice owned by p.batch. Since we could be
// sending this slice over the wire, we need to make a copy.
repr := p.batch.Repr()
reprCopy := make([]byte, len(repr))
copy(reprCopy, repr)
return reprCopy
}
// CommitStats implements the Batch interface.
func (p *pebbleBatch) CommitStats() BatchCommitStats {
return BatchCommitStats{BatchCommitStats: p.batch.CommitStats()}
}
// ShouldWriteLocalTimestamps implements the Writer interface.
func (p *pebbleBatch) ShouldWriteLocalTimestamps(ctx context.Context) bool {
// pebbleBatch is short-lived, so cache the value for performance.
if !p.shouldWriteLocalTimestampsCached {
p.shouldWriteLocalTimestamps = shouldWriteLocalTimestamps(ctx, p.settings)
p.shouldWriteLocalTimestampsCached = true
}
return p.shouldWriteLocalTimestamps
}