-
Notifications
You must be signed in to change notification settings - Fork 455
/
buffer.go
884 lines (760 loc) · 23.1 KB
/
buffer.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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package series
import (
"errors"
"fmt"
"sync/atomic"
"time"
"github.com/m3db/m3/src/dbnode/clock"
"github.com/m3db/m3/src/dbnode/encoding"
"github.com/m3db/m3/src/dbnode/storage/block"
m3dberrors "github.com/m3db/m3/src/dbnode/storage/errors"
"github.com/m3db/m3/src/dbnode/ts"
"github.com/m3db/m3/src/dbnode/x/xio"
"github.com/m3db/m3x/context"
xtime "github.com/m3db/m3x/time"
)
var (
errMoreThanOneStreamAfterMerge = errors.New("buffer has more than one stream after merge")
errNoAvailableBuckets = errors.New("[invariant violated] buffer has no available buckets")
timeZero time.Time
)
const (
// bucketsLen is three to contain the following buckets:
// 1. Bucket before current window, can be drained or not-yet-drained
// 2. Bucket currently taking writes
// 3. Bucket for the future that can be taking writes that is head of
// the current block if write is for the future within bounds
bucketsLen = 3
)
type computeBucketIdxOp int
const (
computeBucketIdx computeBucketIdxOp = iota
computeAndResetBucketIdx
)
type databaseBuffer interface {
Write(
ctx context.Context,
timestamp time.Time,
value float64,
unit xtime.Unit,
annotation []byte,
) error
Snapshot(ctx context.Context, blockStart time.Time) (xio.SegmentReader, error)
ReadEncoded(
ctx context.Context,
start, end time.Time,
) [][]xio.BlockReader
FetchBlocks(
ctx context.Context,
starts []time.Time,
) []block.FetchBlockResult
FetchBlocksMetadata(
ctx context.Context,
start, end time.Time,
opts FetchBlocksMetadataOptions,
) block.FetchBlockMetadataResults
IsEmpty() bool
Stats() bufferStats
// MinMax returns the minimum and maximum blockstarts for the buckets
// that are contained within the buffer. These ranges exclude buckets
// that have already been drained (as those buckets are no longer in use.)
MinMax() (time.Time, time.Time, error)
Tick() bufferTickResult
NeedsDrain() bool
DrainAndReset() drainAndResetResult
Bootstrap(bl block.DatabaseBlock) error
Reset(opts Options)
}
type bufferStats struct {
openBlocks int
wiredBlocks int
}
type drainAndResetResult struct {
mergedOutOfOrderBlocks int
}
type bufferTickResult struct {
mergedOutOfOrderBlocks int
}
type dbBuffer struct {
opts Options
nowFn clock.NowFn
drainFn databaseBufferDrainFn
pastMostBucketIdx int
buckets [bucketsLen]dbBufferBucket
blockSize time.Duration
bufferPast time.Duration
bufferFuture time.Duration
}
type databaseBufferDrainFn func(b block.DatabaseBlock)
// NB(prateek): databaseBuffer.Reset(...) must be called upon the returned
// object prior to use.
func newDatabaseBuffer(drainFn databaseBufferDrainFn) databaseBuffer {
b := &dbBuffer{
drainFn: drainFn,
}
return b
}
func (b *dbBuffer) Reset(opts Options) {
b.opts = opts
b.nowFn = opts.ClockOptions().NowFn()
ropts := opts.RetentionOptions()
b.blockSize = ropts.BlockSize()
b.bufferPast = ropts.BufferPast()
b.bufferFuture = ropts.BufferFuture()
// Avoid capturing any variables with callback
b.computedForEachBucketAsc(computeAndResetBucketIdx, bucketResetStart)
}
func bucketResetStart(now time.Time, b *dbBuffer, idx int, start time.Time) int {
b.buckets[idx].opts = b.opts
b.buckets[idx].resetTo(start)
return 1
}
func (b *dbBuffer) MinMax() (time.Time, time.Time, error) {
var min, max time.Time
for i := range b.buckets {
if (min.IsZero() || b.buckets[i].start.Before(min)) && !b.buckets[i].drained {
min = b.buckets[i].start
}
if max.IsZero() || b.buckets[i].start.After(max) && !b.buckets[i].drained {
max = b.buckets[i].start
}
}
if min.IsZero() || max.IsZero() {
// Should never happen
return time.Time{}, time.Time{}, errNoAvailableBuckets
}
return min, max, nil
}
func (b *dbBuffer) Write(
ctx context.Context,
timestamp time.Time,
value float64,
unit xtime.Unit,
annotation []byte,
) error {
now := b.nowFn()
futureLimit := now.Add(1 * b.bufferFuture)
pastLimit := now.Add(-1 * b.bufferPast)
if !futureLimit.After(timestamp) {
return m3dberrors.ErrTooFuture
}
if !pastLimit.Before(timestamp) {
return m3dberrors.ErrTooPast
}
bucketStart := timestamp.Truncate(b.blockSize)
idx := b.writableBucketIdx(timestamp)
if b.buckets[idx].needsReset(bucketStart) {
// Needs reset
b.DrainAndReset()
}
return b.buckets[idx].write(timestamp, value, unit, annotation)
}
func (b *dbBuffer) writableBucketIdx(t time.Time) int {
return int(t.Truncate(b.blockSize).UnixNano() / int64(b.blockSize) % bucketsLen)
}
func (b *dbBuffer) IsEmpty() bool {
canReadAny := false
for i := range b.buckets {
canReadAny = canReadAny || b.buckets[i].canRead()
}
return !canReadAny
}
func (b *dbBuffer) Stats() bufferStats {
var stats bufferStats
writableIdx := b.writableBucketIdx(b.nowFn())
for i := range b.buckets {
if !b.buckets[i].canRead() {
continue
}
if i == writableIdx {
stats.openBlocks++
}
stats.wiredBlocks++
}
return stats
}
func (b *dbBuffer) NeedsDrain() bool {
// Avoid capturing any variables with callback
return b.computedForEachBucketAsc(computeBucketIdx, bucketNeedsDrain) > 0
}
func bucketNeedsDrain(now time.Time, b *dbBuffer, idx int, start time.Time) int {
if b.buckets[idx].needsDrain(now, start) {
return 1
}
return 0
}
func (b *dbBuffer) Tick() bufferTickResult {
// Avoid capturing any variables with callback
mergedOutOfOrder := b.computedForEachBucketAsc(computeAndResetBucketIdx, bucketTick)
return bufferTickResult{
mergedOutOfOrderBlocks: mergedOutOfOrder,
}
}
func bucketTick(now time.Time, b *dbBuffer, idx int, start time.Time) int {
// Perform a drain and reset if necessary
mergedOutOfOrderBlocks := bucketDrainAndReset(now, b, idx, start)
// Try to merge any out of order encoders to amortize the cost of a drain
r, err := b.buckets[idx].merge()
if err != nil {
log := b.opts.InstrumentOptions().Logger()
log.Errorf("buffer merge encode error: %v", err)
}
if r.merges > 0 {
mergedOutOfOrderBlocks++
}
return mergedOutOfOrderBlocks
}
func (b *dbBuffer) DrainAndReset() drainAndResetResult {
// Avoid capturing any variables with callback
mergedOutOfOrder := b.computedForEachBucketAsc(computeAndResetBucketIdx, bucketDrainAndReset)
return drainAndResetResult{
mergedOutOfOrderBlocks: mergedOutOfOrder,
}
}
func bucketDrainAndReset(now time.Time, b *dbBuffer, idx int, start time.Time) int {
mergedOutOfOrderBlocks := 0
if b.buckets[idx].needsDrain(now, start) {
// Rotate the buffer to a block, merging if required
result, err := b.buckets[idx].discardMerged()
if err != nil {
log := b.opts.InstrumentOptions().Logger()
log.Errorf("buffer merge encode error: %v", err)
} else {
if result.merges > 0 {
mergedOutOfOrderBlocks++
}
if !(result.block.Len() > 0) {
log := b.opts.InstrumentOptions().Logger()
log.Errorf("buffer drain tried to drain empty stream for bucket: %v",
start.String())
} else {
// If this block was read mark it as such
if lastRead := b.buckets[idx].lastRead(); !lastRead.IsZero() {
result.block.SetLastReadTime(lastRead)
}
b.drainFn(result.block)
}
}
b.buckets[idx].drained = true
}
if b.buckets[idx].needsReset(start) {
// Reset bucket
b.buckets[idx].resetTo(start)
}
return mergedOutOfOrderBlocks
}
func (b *dbBuffer) Bootstrap(bl block.DatabaseBlock) error {
blockStart := bl.StartTime()
bootstrapped := false
for i := range b.buckets {
if b.buckets[i].start.Equal(blockStart) {
if b.buckets[i].drained {
return fmt.Errorf(
"block at %s cannot be bootstrapped by buffer because its already drained",
blockStart.String(),
)
}
b.buckets[i].bootstrap(bl)
bootstrapped = true
break
}
}
if !bootstrapped {
return fmt.Errorf("block at %s not contained by buffer", blockStart.String())
}
return nil
}
// forEachBucketAsc iterates over the buckets in time ascending order
// to read bucket data
func (b *dbBuffer) forEachBucketAsc(fn func(*dbBufferBucket)) {
for i := 0; i < bucketsLen; i++ {
idx := (b.pastMostBucketIdx + i) % bucketsLen
fn(&b.buckets[idx])
}
}
// computedForEachBucketAsc performs a fn on the buckets in time ascending order
// and returns the sum of the number returned by each fn
func (b *dbBuffer) computedForEachBucketAsc(
op computeBucketIdxOp,
fn func(now time.Time, b *dbBuffer, idx int, bucketStart time.Time) int,
) int {
now := b.nowFn()
pastMostBucketStart := now.Truncate(b.blockSize).Add(-1 * b.blockSize)
bucketNum := (pastMostBucketStart.UnixNano() / int64(b.blockSize)) % bucketsLen
result := 0
for i := int64(0); i < bucketsLen; i++ {
idx := int((bucketNum + i) % bucketsLen)
curr := pastMostBucketStart.Add(time.Duration(i) * b.blockSize)
result += fn(now, b, idx, curr)
}
if op == computeAndResetBucketIdx {
b.pastMostBucketIdx = int(bucketNum)
}
return result
}
func (b *dbBuffer) Snapshot(ctx context.Context, blockStart time.Time) (xio.SegmentReader, error) {
var (
res xio.SegmentReader
err error
)
b.forEachBucketAsc(func(bucket *dbBufferBucket) {
if err != nil {
// Something already went wrong and we want to return the error to the caller
// as soon as possible instead of continuing to do work.
return
}
if !bucket.canRead() {
return
}
if !blockStart.Equal(bucket.start) {
return
}
// We need to merge all the bootstrapped blocks / encoders into a single stream for
// the sake of being able to persist it to disk as a single encoded stream.
_, err = bucket.merge()
if err != nil {
return
}
// This operation is safe because all of the underlying resources will respect the
// lifecycle of the context in one way or another. The "bootstrapped blocks" that
// we stream from will mark their internal context as dependent on that of the passed
// context, and the Encoder's that we stream from actually perform a data copy and
// don't share a reference.
streams := bucket.streams(ctx)
if len(streams) != 1 {
// Should never happen as the call to merge above should result in only a single
// stream being present.
err = errMoreThanOneStreamAfterMerge
return
}
// Direct indexing is safe because canRead guarantees us at least one stream
res = streams[0]
})
return res, err
}
func (b *dbBuffer) ReadEncoded(ctx context.Context, start, end time.Time) [][]xio.BlockReader {
// TODO(r): pool these results arrays
var res [][]xio.BlockReader
b.forEachBucketAsc(func(bucket *dbBufferBucket) {
if !bucket.canRead() {
return
}
if !start.Before(bucket.start.Add(b.blockSize)) {
return
}
if !bucket.start.Before(end) {
return
}
res = append(res, bucket.streams(ctx))
// NB(r): Store the last read time, should not set this when
// calling FetchBlocks as a read is differentiated from
// a FetchBlocks call. One is initiated by an external
// entity and the other is used for streaming blocks between
// the storage nodes. This distinction is important as this
// data is important for use with understanding access patterns, etc.
bucket.setLastRead(b.nowFn())
})
return res
}
func (b *dbBuffer) FetchBlocks(ctx context.Context, starts []time.Time) []block.FetchBlockResult {
var res []block.FetchBlockResult
b.forEachBucketAsc(func(bucket *dbBufferBucket) {
if !bucket.canRead() {
return
}
found := false
// starts have only a few items, linear search should be okay time-wise to
// avoid allocating a map here.
for _, start := range starts {
if start.Equal(bucket.start) {
found = true
break
}
}
if !found {
return
}
streams := bucket.streams(ctx)
res = append(res, block.NewFetchBlockResult(bucket.start, streams, nil))
})
return res
}
func (b *dbBuffer) FetchBlocksMetadata(
ctx context.Context,
start, end time.Time,
opts FetchBlocksMetadataOptions,
) block.FetchBlockMetadataResults {
blockSize := b.opts.RetentionOptions().BlockSize()
res := b.opts.FetchBlockMetadataResultsPool().Get()
b.forEachBucketAsc(func(bucket *dbBufferBucket) {
if !bucket.canRead() {
return
}
if !start.Before(bucket.start.Add(blockSize)) || !bucket.start.Before(end) {
return
}
size := int64(bucket.streamsLen())
// If we have no data in this bucket, return early without appending it to the result.
if size == 0 {
return
}
var resultSize int64
if opts.IncludeSizes {
resultSize = size
}
var resultLastRead time.Time
if opts.IncludeLastRead {
resultLastRead = bucket.lastRead()
}
// NB(r): Ignore if opts.IncludeChecksum because we avoid
// calculating checksum since block is open and is being mutated
res.Add(block.FetchBlockMetadataResult{
Start: bucket.start,
Size: resultSize,
LastRead: resultLastRead,
})
})
return res
}
type dbBufferBucket struct {
opts Options
start time.Time
encoders []inOrderEncoder
bootstrapped []block.DatabaseBlock
lastReadUnixNanos int64
drained bool
}
type inOrderEncoder struct {
encoder encoding.Encoder
lastWriteAt time.Time
}
func (b *dbBufferBucket) resetTo(
start time.Time,
) {
// Close the old context if we're resetting for use
b.finalize()
bopts := b.opts.DatabaseBlockOptions()
encoder := bopts.EncoderPool().Get()
encoder.Reset(start, bopts.DatabaseBlockAllocSize())
b.start = start
b.encoders = append(b.encoders, inOrderEncoder{
encoder: encoder,
})
b.bootstrapped = nil
atomic.StoreInt64(&b.lastReadUnixNanos, 0)
b.drained = false
}
func (b *dbBufferBucket) finalize() {
b.resetEncoders()
b.resetBootstrapped()
}
func (b *dbBufferBucket) empty() bool {
for _, block := range b.bootstrapped {
if block.Len() > 0 {
return false
}
}
for _, elem := range b.encoders {
if elem.encoder != nil && elem.encoder.NumEncoded() > 0 {
return false
}
}
return true
}
func (b *dbBufferBucket) canRead() bool {
return !b.drained && !b.empty()
}
func (b *dbBufferBucket) needsReset(
targetStart time.Time,
) bool {
return !b.start.Equal(targetStart)
}
func (b *dbBufferBucket) needsDrain(
now time.Time,
targetStart time.Time,
) bool {
retentionOpts := b.opts.RetentionOptions()
blockSize := retentionOpts.BlockSize()
bufferPast := retentionOpts.BufferPast()
return b.canRead() && (b.needsReset(targetStart) ||
b.start.Add(blockSize).Before(now.Add(-bufferPast)))
}
func (b *dbBufferBucket) bootstrap(
bl block.DatabaseBlock,
) {
b.bootstrapped = append(b.bootstrapped, bl)
}
func (b *dbBufferBucket) write(
timestamp time.Time,
value float64,
unit xtime.Unit,
annotation []byte,
) error {
datapoint := ts.Datapoint{
Timestamp: timestamp,
Value: value,
}
// Find the correct encoder to write to
idx := -1
for i := range b.encoders {
lastWriteAt := b.encoders[i].lastWriteAt
if timestamp.Equal(lastWriteAt) {
last, err := b.encoders[i].encoder.LastEncoded()
if err != nil {
return err
}
if last.Value == value {
// No-op since matches the current value
// TODO(r): in the future we could return some metadata that
// this result was a no-op and hence does not need to be written
// to the commit log, otherwise high frequency write volumes
// that are using M3DB as a cache-like index of things seen
// in a time window will still cause a flood of disk/CPU resource
// usage writing values to the commit log, even if the memory
// profile is lean as a side effect of this write being a no-op.
return nil
}
continue
}
if timestamp.After(lastWriteAt) {
idx = i
break
}
}
// Upsert/last-write-wins semantics.
// NB(r): We push datapoints with the same timestamp but differing
// value into a new encoder later in the stack of in order encoders
// since an encoder is immutable.
// The encoders pushed later will surface their values first.
if idx != -1 {
return b.writeToEncoderIndex(idx, datapoint, unit, annotation)
}
// Need a new encoder, we didn't find an encoder to write to
b.opts.Stats().IncCreatedEncoders()
bopts := b.opts.DatabaseBlockOptions()
blockSize := b.opts.RetentionOptions().BlockSize()
blockAllocSize := bopts.DatabaseBlockAllocSize()
encoder := bopts.EncoderPool().Get()
encoder.Reset(timestamp.Truncate(blockSize), blockAllocSize)
b.encoders = append(b.encoders, inOrderEncoder{
encoder: encoder,
lastWriteAt: timestamp,
})
idx = len(b.encoders) - 1
err := b.writeToEncoderIndex(idx, datapoint, unit, annotation)
if err != nil {
encoder.Close()
b.encoders = b.encoders[:idx]
return err
}
return nil
}
func (b *dbBufferBucket) writeToEncoderIndex(
idx int,
datapoint ts.Datapoint,
unit xtime.Unit,
annotation []byte,
) error {
err := b.encoders[idx].encoder.Encode(datapoint, unit, annotation)
if err != nil {
return err
}
b.encoders[idx].lastWriteAt = datapoint.Timestamp
return nil
}
func (b *dbBufferBucket) streams(ctx context.Context) []xio.BlockReader {
streams := make([]xio.BlockReader, 0, len(b.bootstrapped)+len(b.encoders))
for i := range b.bootstrapped {
if b.bootstrapped[i].Len() == 0 {
continue
}
if s, err := b.bootstrapped[i].Stream(ctx); err == nil && s.IsNotEmpty() {
// NB(r): block stream method will register the stream closer already
streams = append(streams, s)
}
}
for i := range b.encoders {
start := b.start
if s := b.encoders[i].encoder.Stream(); s != nil {
br := xio.BlockReader{
SegmentReader: s,
Start: start,
BlockSize: b.opts.RetentionOptions().BlockSize(),
}
ctx.RegisterFinalizer(s)
streams = append(streams, br)
}
}
return streams
}
func (b *dbBufferBucket) streamsLen() int {
length := 0
for i := range b.bootstrapped {
length += b.bootstrapped[i].Len()
}
for i := range b.encoders {
length += b.encoders[i].encoder.Len()
}
return length
}
func (b *dbBufferBucket) setLastRead(value time.Time) {
atomic.StoreInt64(&b.lastReadUnixNanos, value.UnixNano())
}
func (b *dbBufferBucket) lastRead() time.Time {
return time.Unix(0, atomic.LoadInt64(&b.lastReadUnixNanos))
}
func (b *dbBufferBucket) resetEncoders() {
var zeroed inOrderEncoder
for i := range b.encoders {
// Register when this bucket resets we close the encoder
encoder := b.encoders[i].encoder
encoder.Close()
b.encoders[i] = zeroed
}
b.encoders = b.encoders[:0]
}
func (b *dbBufferBucket) resetBootstrapped() {
for i := range b.bootstrapped {
bl := b.bootstrapped[i]
bl.Close()
}
b.bootstrapped = nil
}
func (b *dbBufferBucket) needsMerge() bool {
return b.canRead() && !(b.hasJustSingleEncoder() || b.hasJustSingleBootstrappedBlock())
}
func (b *dbBufferBucket) hasJustSingleEncoder() bool {
return len(b.encoders) == 1 && len(b.bootstrapped) == 0
}
func (b *dbBufferBucket) hasJustSingleBootstrappedBlock() bool {
encodersEmpty := len(b.encoders) == 0 ||
(len(b.encoders) == 1 &&
b.encoders[0].encoder.Len() == 0)
return encodersEmpty && len(b.bootstrapped) == 1
}
type mergeResult struct {
merges int
}
func (b *dbBufferBucket) merge() (mergeResult, error) {
if !b.needsMerge() {
// Save unnecessary work
return mergeResult{}, nil
}
merges := 0
bopts := b.opts.DatabaseBlockOptions()
encoder := bopts.EncoderPool().Get()
encoder.Reset(b.start, bopts.DatabaseBlockAllocSize())
var (
start = b.start
readers = make([]xio.SegmentReader, 0, len(b.encoders)+len(b.bootstrapped))
streams = make([]xio.SegmentReader, 0, len(b.encoders))
iter = b.opts.MultiReaderIteratorPool().Get()
ctx = b.opts.ContextPool().Get()
)
defer func() {
iter.Close()
ctx.Close()
// NB(r): Only need to close the mutable encoder streams as
// the context we created for reading the bootstrap blocks
// when closed will close those streams.
for _, stream := range streams {
stream.Finalize()
}
}()
// Rank bootstrapped blocks as data that has appeared before data that
// arrived locally in the buffer
for i := range b.bootstrapped {
block, err := b.bootstrapped[i].Stream(ctx)
if err == nil && block.SegmentReader != nil {
merges++
readers = append(readers, block.SegmentReader)
}
}
for i := range b.encoders {
if s := b.encoders[i].encoder.Stream(); s != nil {
merges++
readers = append(readers, s)
streams = append(streams, s)
}
}
var lastWriteAt time.Time
iter.Reset(readers, start, b.opts.RetentionOptions().BlockSize())
for iter.Next() {
dp, unit, annotation := iter.Current()
if err := encoder.Encode(dp, unit, annotation); err != nil {
return mergeResult{}, err
}
lastWriteAt = dp.Timestamp
}
if err := iter.Err(); err != nil {
return mergeResult{}, err
}
b.resetEncoders()
b.resetBootstrapped()
b.encoders = append(b.encoders, inOrderEncoder{
encoder: encoder,
lastWriteAt: lastWriteAt,
})
return mergeResult{merges: merges}, nil
}
type discardMergedResult struct {
block block.DatabaseBlock
merges int
}
func (b *dbBufferBucket) discardMerged() (discardMergedResult, error) {
if b.hasJustSingleEncoder() {
// Already merged as a single encoder
encoder := b.encoders[0].encoder
newBlock := b.opts.DatabaseBlockOptions().DatabaseBlockPool().Get()
blockSize := b.opts.RetentionOptions().BlockSize()
newBlock.Reset(b.start, blockSize, encoder.Discard())
// The single encoder is already discarded, no need to call resetEncoders
// just remove it from the list of encoders
b.encoders = b.encoders[:0]
b.resetBootstrapped()
return discardMergedResult{newBlock, 0}, nil
}
if b.hasJustSingleBootstrappedBlock() {
// Already merged just a single bootstrapped block
existingBlock := b.bootstrapped[0]
// Need to reset encoders but do not want to finalize the block as we
// are passing ownership of it to the caller
b.resetEncoders()
b.bootstrapped = nil
return discardMergedResult{existingBlock, 0}, nil
}
result, err := b.merge()
if err != nil {
b.resetEncoders()
b.resetBootstrapped()
return discardMergedResult{}, err
}
merged := b.encoders[0].encoder
newBlock := b.opts.DatabaseBlockOptions().DatabaseBlockPool().Get()
blockSize := b.opts.RetentionOptions().BlockSize()
newBlock.Reset(b.start, blockSize, merged.Discard())
// The merged encoder is already discarded, no need to call resetEncoders
// just remove it from the list of encoders
b.encoders = b.encoders[:0]
b.resetBootstrapped()
return discardMergedResult{newBlock, result.merges}, nil
}