-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathblock.go
1139 lines (980 loc) · 31.9 KB
/
block.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 (c) 2018 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 index
import (
"errors"
"fmt"
"sync"
"time"
"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
"github.com/m3db/m3/src/dbnode/storage/index/compaction"
"github.com/m3db/m3/src/dbnode/storage/index/segments"
"github.com/m3db/m3/src/dbnode/storage/namespace"
"github.com/m3db/m3/src/m3ninx/doc"
m3ninxindex "github.com/m3db/m3/src/m3ninx/index"
"github.com/m3db/m3/src/m3ninx/index/segment"
"github.com/m3db/m3/src/m3ninx/index/segment/builder"
"github.com/m3db/m3/src/m3ninx/index/segment/fst"
"github.com/m3db/m3/src/m3ninx/search"
"github.com/m3db/m3/src/m3ninx/search/executor"
"github.com/m3db/m3x/context"
xerrors "github.com/m3db/m3x/errors"
"github.com/m3db/m3x/instrument"
xlog "github.com/m3db/m3x/log"
xtime "github.com/m3db/m3x/time"
"github.com/uber-go/tally"
)
var (
// ErrUnableToQueryBlockClosed is returned when querying closed block.
ErrUnableToQueryBlockClosed = errors.New("unable to query, index block is closed")
// ErrUnableReportStatsBlockClosed is returned from Stats when the block is closed.
ErrUnableReportStatsBlockClosed = errors.New("unable to report stats, block is closed")
errUnableToWriteBlockClosed = errors.New("unable to write, index block is closed")
errUnableToWriteBlockSealed = errors.New("unable to write, index block is sealed")
errUnableToWriteBlockConcurrent = errors.New("unable to write, index block is being written to already")
errUnableToBootstrapBlockClosed = errors.New("unable to bootstrap, block is closed")
errUnableToTickBlockClosed = errors.New("unable to tick, block is closed")
errBlockAlreadyClosed = errors.New("unable to close, block already closed")
errForegroundCompactorNoBuilderDocs = errors.New("index foreground compactor has no builder documents to compact")
errForegroundCompactorNoPlan = errors.New("index foreground compactor failed to generate a plan")
errForegroundCompactorBadPlanFirstTask = errors.New("index foreground compactor generated plan without mutable segment in first task")
errForegroundCompactorBadPlanSecondaryTask = errors.New("index foreground compactor generated plan with mutable segment a secondary task")
errUnableToSealBlockIllegalStateFmtString = "unable to seal, index block state: %v"
errUnableToWriteBlockUnknownStateFmtString = "unable to write, unknown index block state: %v"
)
type blockState uint
const (
blockStateOpen blockState = iota
blockStateSealed
blockStateClosed
compactDebugLogEvery = 1 // Emit debug log for every compaction
)
func (s blockState) String() string {
switch s {
case blockStateOpen:
return "open"
case blockStateSealed:
return "sealed"
case blockStateClosed:
return "closed"
}
return "unknown"
}
type newExecutorFn func() (search.Executor, error)
type block struct {
sync.RWMutex
state blockState
hasEvictedMutableSegmentsAnyTimes bool
segmentBuilder segment.DocumentsBuilder
foregroundSegments []*readableSeg
backgroundSegments []*readableSeg
shardRangesSegments []blockShardRangesSegments
newExecutorFn newExecutorFn
blockStart time.Time
blockEnd time.Time
blockSize time.Duration
opts Options
iopts instrument.Options
nsMD namespace.Metadata
docsPool doc.DocumentArrayPool
compactingForeground bool
compactingBackground bool
compactionsForeground int
compactionsBackground int
foregroundCompactor *compaction.Compactor
backgroundCompactor *compaction.Compactor
metrics blockMetrics
logger xlog.Logger
}
type blockMetrics struct {
rotateActiveSegment tally.Counter
rotateActiveSegmentAge tally.Timer
rotateActiveSegmentSize tally.Histogram
foregroundCompactionPlanRunLatency tally.Timer
foregroundCompactionTaskRunLatency tally.Timer
backgroundCompactionPlanRunLatency tally.Timer
backgroundCompactionTaskRunLatency tally.Timer
}
func newBlockMetrics(s tally.Scope) blockMetrics {
s = s.SubScope("index").SubScope("block")
foregroundScope := s.Tagged(map[string]string{"compaction-type": "foreground"})
backgroundScope := s.Tagged(map[string]string{"compaction-type": "background"})
return blockMetrics{
rotateActiveSegment: s.Counter("rotate-active-segment"),
rotateActiveSegmentAge: s.Timer("rotate-active-segment-age"),
rotateActiveSegmentSize: s.Histogram("rotate-active-segment-size",
append(tally.ValueBuckets{0}, tally.MustMakeExponentialValueBuckets(100, 2, 16)...)),
foregroundCompactionPlanRunLatency: foregroundScope.Timer("compaction-plan-run-latency"),
foregroundCompactionTaskRunLatency: foregroundScope.Timer("compaction-task-run-latency"),
backgroundCompactionPlanRunLatency: backgroundScope.Timer("compaction-plan-run-latency"),
backgroundCompactionTaskRunLatency: backgroundScope.Timer("compaction-task-run-latency"),
}
}
// blockShardsSegments is a collection of segments that has a mapping of what shards
// and time ranges they completely cover, this can only ever come from computing
// from data that has come from shards, either on an index flush or a bootstrap.
type blockShardRangesSegments struct {
shardTimeRanges result.ShardTimeRanges
segments []segment.Segment
}
// NewBlock returns a new Block, representing a complete reverse index for the
// duration of time specified. It is backed by one or more segments.
func NewBlock(
blockStart time.Time,
md namespace.Metadata,
opts Options,
) (Block, error) {
docsPool := opts.DocumentArrayPool()
foregroundCompactor := compaction.NewCompactor(docsPool,
documentArrayPoolCapacity,
opts.SegmentBuilderOptions(),
opts.FSTSegmentOptions(),
compaction.CompactorOptions{
FSTWriterOptions: &fst.WriterOptions{
// DisableRegistry is set to true to trade a larger FST size
// for a faster FST compaction since we want to reduce the end
// to end latency for time to first index a metric.
DisableRegistry: true,
},
})
backgroundCompactor := compaction.NewCompactor(docsPool,
documentArrayPoolCapacity,
opts.SegmentBuilderOptions(),
opts.FSTSegmentOptions(),
compaction.CompactorOptions{})
segmentBuilder, err := builder.NewBuilderFromDocuments(opts.SegmentBuilderOptions())
if err != nil {
return nil, err
}
blockSize := md.Options().IndexOptions().BlockSize()
iopts := opts.InstrumentOptions()
b := &block{
state: blockStateOpen,
segmentBuilder: segmentBuilder,
blockStart: blockStart,
blockEnd: blockStart.Add(blockSize),
blockSize: blockSize,
opts: opts,
iopts: iopts,
nsMD: md,
docsPool: docsPool,
foregroundCompactor: foregroundCompactor,
backgroundCompactor: backgroundCompactor,
metrics: newBlockMetrics(iopts.MetricsScope()),
logger: iopts.Logger(),
}
b.newExecutorFn = b.executorWithRLock
return b, nil
}
func (b *block) StartTime() time.Time {
return b.blockStart
}
func (b *block) EndTime() time.Time {
return b.blockEnd
}
func (b *block) maybeBackgroundCompactWithLock() {
if b.compactingBackground || b.state != blockStateOpen {
return
}
// Create a logical plan.
segs := make([]compaction.Segment, 0, len(b.backgroundSegments))
for _, seg := range b.backgroundSegments {
segs = append(segs, compaction.Segment{
Age: seg.Age(),
Size: seg.Segment().Size(),
Type: segments.FSTType,
Segment: seg.Segment(),
})
}
plan, err := compaction.NewPlan(segs, b.opts.BackgroundCompactionPlannerOptions())
if err != nil {
instrument.EmitAndLogInvariantViolation(b.iopts, func(l xlog.Logger) {
l.Errorf("index background compaction plan error: %v", err)
})
return
}
if len(plan.Tasks) == 0 {
return
}
// Kick off compaction.
b.compactingBackground = true
go func() {
b.backgroundCompactWithPlan(plan)
b.Lock()
b.compactingBackground = false
b.cleanupBackgroundCompactWithLock()
b.Unlock()
}()
}
func (b *block) shouldEvictCompactedSegmentsWithLock() bool {
// NB(r): The frozen/compacted segments are derived segments of the
// active mutable segment, if we ever evict that segment then
// we don't need the frozen/compacted segments either and should
// shed them from memory.
return b.state == blockStateClosed ||
b.hasEvictedMutableSegmentsAnyTimes
}
func (b *block) cleanupBackgroundCompactWithLock() {
if b.state == blockStateOpen {
// See if we need to trigger another compaction.
b.maybeBackgroundCompactWithLock()
return
}
// Check if need to close all the compacted segments due to
// having evicted mutable segments or the block being closed.
if !b.shouldEvictCompactedSegmentsWithLock() {
return
}
// Evict compacted segments.
b.closeCompactedSegments(b.backgroundSegments)
b.backgroundSegments = nil
// Free compactor resources.
if b.backgroundCompactor == nil {
return
}
if err := b.backgroundCompactor.Close(); err != nil {
instrument.EmitAndLogInvariantViolation(b.iopts, func(l xlog.Logger) {
l.Errorf("error closing index block background compactor: %v", err)
})
}
b.backgroundCompactor = nil
}
func (b *block) closeCompactedSegments(segments []*readableSeg) {
for _, seg := range segments {
err := seg.Segment().Close()
if err != nil {
instrument.EmitAndLogInvariantViolation(b.iopts, func(l xlog.Logger) {
l.Errorf("could not close compacted segment: %v", err)
})
}
}
}
func (b *block) backgroundCompactWithPlan(plan *compaction.Plan) {
sw := b.metrics.backgroundCompactionPlanRunLatency.Start()
defer sw.Stop()
n := b.compactionsBackground
b.compactionsBackground++
logger := b.logger.WithFields(
xlog.NewField("block", b.blockStart.String()),
xlog.NewField("numBackgroundCompaction", n),
)
log := n%compactDebugLogEvery == 0
if log {
for i, task := range plan.Tasks {
summary := task.Summary()
logger.WithFields(
xlog.NewField("task", i),
xlog.NewField("numMutable", summary.NumMutable),
xlog.NewField("numFST", summary.NumFST),
xlog.NewField("cumulativeMutableAge", summary.CumulativeMutableAge.String()),
xlog.NewField("cumulativeSize", summary.CumulativeSize),
).Debug("planned background compaction task")
}
}
for i, task := range plan.Tasks {
err := b.backgroundCompactWithTask(task, log,
logger.WithFields(xlog.NewField("task", i)))
if err != nil {
instrument.EmitAndLogInvariantViolation(b.iopts, func(l xlog.Logger) {
l.Errorf("error compacting segments: %v", err)
})
return
}
}
}
func (b *block) backgroundCompactWithTask(
task compaction.Task,
log bool,
logger xlog.Logger,
) error {
if log {
logger.Debug("start compaction task")
}
segments := make([]segment.Segment, 0, len(task.Segments))
for _, seg := range task.Segments {
segments = append(segments, seg.Segment)
}
start := time.Now()
compacted, err := b.backgroundCompactor.Compact(segments)
took := time.Since(start)
b.metrics.backgroundCompactionTaskRunLatency.Record(took)
if log {
logger.WithFields(xlog.NewField("took", took.String())).
Debug("done compaction task")
}
if err != nil {
return err
}
// Rotate out the replaced frozen segments and add the compacted one.
b.Lock()
defer b.Unlock()
result := b.addCompactedSegmentFromSegments(b.backgroundSegments,
segments, compacted)
b.backgroundSegments = result
return nil
}
func (b *block) addCompactedSegmentFromSegments(
current []*readableSeg,
segmentsJustCompacted []segment.Segment,
compacted segment.Segment,
) []*readableSeg {
result := make([]*readableSeg, 0, len(current))
for _, existing := range current {
keepCurr := true
for _, seg := range segmentsJustCompacted {
if existing.Segment() == seg {
// Do not keep this one, it was compacted just then.
keepCurr = false
break
}
}
if keepCurr {
result = append(result, existing)
continue
}
err := existing.Segment().Close()
if err != nil {
// Already compacted, not much we can do about not closing it.
instrument.EmitAndLogInvariantViolation(b.iopts, func(l xlog.Logger) {
l.Errorf("unable to close compacted block: %v", err)
})
}
}
// Return all the ones we kept plus the new compacted segment
return append(result, newReadableSeg(compacted, b.opts))
}
func (b *block) WriteBatch(inserts *WriteBatch) (WriteBatchResult, error) {
b.Lock()
if b.state != blockStateOpen {
b.Unlock()
return b.writeBatchResult(inserts, b.writeBatchErrorInvalidState(b.state))
}
if b.compactingForeground {
b.Unlock()
return b.writeBatchResult(inserts, errUnableToWriteBlockConcurrent)
}
b.compactingForeground = true
builder := b.segmentBuilder
b.Unlock()
defer func() {
b.Lock()
b.compactingForeground = false
b.cleanupForegroundCompactWithLock()
b.Unlock()
}()
builder.Reset(0)
insertResultErr := builder.InsertBatch(m3ninxindex.Batch{
Docs: inserts.PendingDocs(),
AllowPartialUpdates: true,
})
if len(builder.Docs()) == 0 {
// No inserts, no need to compact.
return b.writeBatchResult(inserts, insertResultErr)
}
// We inserted some documents, need to compact immediately into a
// foreground segment from the segment builder before we can serve reads
// from an FST segment.
err := b.foregroundCompactWithBuilder(builder)
if err != nil {
return b.writeBatchResult(inserts, err)
}
// Return result from the original insertion since compaction was successful.
return b.writeBatchResult(inserts, insertResultErr)
}
func (b *block) writeBatchResult(
inserts *WriteBatch,
err error,
) (WriteBatchResult, error) {
if err == nil {
inserts.MarkUnmarkedEntriesSuccess()
return WriteBatchResult{
NumSuccess: int64(inserts.Len()),
}, nil
}
partialErr, ok := err.(*m3ninxindex.BatchPartialError)
if !ok {
// NB: marking all the inserts as failure, cause we don't know which ones failed.
inserts.MarkUnmarkedEntriesError(err)
return WriteBatchResult{NumError: int64(inserts.Len())}, err
}
numErr := len(partialErr.Errs())
for _, err := range partialErr.Errs() {
// Avoid marking these as success.
inserts.MarkUnmarkedEntryError(err.Err, err.Idx)
}
// Mark all non-error inserts success, so we don't repeatedly index them.
inserts.MarkUnmarkedEntriesSuccess()
return WriteBatchResult{
NumSuccess: int64(inserts.Len() - numErr),
NumError: int64(numErr),
}, partialErr
}
func (b *block) foregroundCompactWithBuilder(builder segment.DocumentsBuilder) error {
// We inserted some documents, need to compact immediately into a
// foreground segment.
b.Lock()
foregroundSegments := b.foregroundSegments
b.Unlock()
segs := make([]compaction.Segment, 0, len(foregroundSegments)+1)
segs = append(segs, compaction.Segment{
Age: 0,
Size: int64(len(builder.Docs())),
Type: segments.MutableType,
Builder: builder,
})
for _, seg := range foregroundSegments {
segs = append(segs, compaction.Segment{
Age: seg.Age(),
Size: seg.Segment().Size(),
Type: segments.FSTType,
Segment: seg.Segment(),
})
}
plan, err := compaction.NewPlan(segs, b.opts.ForegroundCompactionPlannerOptions())
if err != nil {
return err
}
// Check plan
if len(plan.Tasks) == 0 {
// Should always generate a task when a mutable builder is passed to planner
return errForegroundCompactorNoPlan
}
if taskNumBuilders(plan.Tasks[0]) != 1 {
// First task of plan must include the builder, so we can avoid resetting it
// for the first task, but then safely reset it in consequent tasks
return errForegroundCompactorBadPlanFirstTask
}
// Move any unused segments to the background.
b.Lock()
b.maybeMoveForegroundSegmentsToBackgroundWithLock(plan.UnusedSegments)
b.Unlock()
n := b.compactionsForeground
b.compactionsForeground++
logger := b.logger.WithFields(
xlog.NewField("block", b.blockStart.String()),
xlog.NewField("numForegroundCompaction", n),
)
log := n%compactDebugLogEvery == 0
if log {
for i, task := range plan.Tasks {
summary := task.Summary()
logger.WithFields(
xlog.NewField("task", i),
xlog.NewField("numMutable", summary.NumMutable),
xlog.NewField("numFST", summary.NumFST),
xlog.NewField("cumulativeMutableAge", summary.CumulativeMutableAge.String()),
xlog.NewField("cumulativeSize", summary.CumulativeSize),
).Debug("planned foreground compaction task")
}
}
// Run the plan.
sw := b.metrics.foregroundCompactionPlanRunLatency.Start()
defer sw.Stop()
// Run the first task, without resetting the builder.
if err := b.foregroundCompactWithTask(
builder, plan.Tasks[0],
log, logger.WithFields(xlog.NewField("task", 0)),
); err != nil {
return err
}
// Now run each consequent task, resetting the builder each time since
// the results from the builder have already been compacted in the first
// task.
for i := 1; i < len(plan.Tasks); i++ {
task := plan.Tasks[i]
if taskNumBuilders(task) > 0 {
// Only the first task should compact the builder
return errForegroundCompactorBadPlanSecondaryTask
}
// Now use the builder after resetting it.
builder.Reset(0)
if err := b.foregroundCompactWithTask(
builder, task,
log, logger.WithFields(xlog.NewField("task", i)),
); err != nil {
return err
}
}
return nil
}
func (b *block) maybeMoveForegroundSegmentsToBackgroundWithLock(
segments []compaction.Segment,
) {
if len(segments) == 0 {
return
}
if b.backgroundCompactor == nil {
// No longer performing background compaction due to evict/close.
return
}
b.logger.Debugf("moving %d segments from foreground to background",
len(segments))
// If background compaction is still active, then we move any unused
// foreground segments into the background so that they might be
// compacted by the background compactor at some point.
i := 0
for _, currForeground := range b.foregroundSegments {
movedToBackground := false
for _, seg := range segments {
if currForeground.Segment() == seg.Segment {
b.backgroundSegments = append(b.backgroundSegments, currForeground)
movedToBackground = true
break
}
}
if movedToBackground {
continue // No need to keep this segment, we moved it.
}
b.foregroundSegments[i] = currForeground
i++
}
b.foregroundSegments = b.foregroundSegments[:i]
// Potentially kick off a background compaction.
b.maybeBackgroundCompactWithLock()
}
func (b *block) foregroundCompactWithTask(
builder segment.DocumentsBuilder,
task compaction.Task,
log bool,
logger xlog.Logger,
) error {
if log {
logger.Debug("start compaction task")
}
segments := make([]segment.Segment, 0, len(task.Segments))
for _, seg := range task.Segments {
if seg.Segment == nil {
continue // This means the builder is being used.
}
segments = append(segments, seg.Segment)
}
start := time.Now()
compacted, err := b.foregroundCompactor.CompactUsingBuilder(builder, segments)
took := time.Since(start)
b.metrics.foregroundCompactionTaskRunLatency.Record(took)
if log {
logger.WithFields(xlog.NewField("took", took.String())).
Debug("done compaction task")
}
if err != nil {
return err
}
// Rotate in the ones we just compacted.
b.Lock()
defer b.Unlock()
result := b.addCompactedSegmentFromSegments(b.foregroundSegments,
segments, compacted)
b.foregroundSegments = result
return nil
}
func (b *block) cleanupForegroundCompactWithLock() {
// Check if we need to close all the compacted segments due to
// having evicted mutable segments or the block being closed.
if !b.shouldEvictCompactedSegmentsWithLock() {
return
}
// Evict compacted segments.
b.closeCompactedSegments(b.foregroundSegments)
b.foregroundSegments = nil
// Free compactor resources.
if b.foregroundCompactor == nil {
return
}
if err := b.foregroundCompactor.Close(); err != nil {
instrument.EmitAndLogInvariantViolation(b.iopts, func(l xlog.Logger) {
l.Errorf("error closing index block foreground compactor: %v", err)
})
}
b.foregroundCompactor = nil
b.segmentBuilder = nil
}
func (b *block) executorWithRLock() (search.Executor, error) {
expectedReaders := len(b.foregroundSegments) + len(b.backgroundSegments)
for _, group := range b.shardRangesSegments {
expectedReaders += len(group.segments)
}
var (
readers = make([]m3ninxindex.Reader, 0, expectedReaders)
success = false
)
defer func() {
// Cleanup in case any of the readers below fail.
if !success {
for _, reader := range readers {
reader.Close()
}
}
}()
// Add foreground and background segments.
var foregroundErr, backgroundErr error
readers, foregroundErr = addReadersFromReadableSegments(readers,
b.foregroundSegments)
readers, backgroundErr = addReadersFromReadableSegments(readers,
b.backgroundSegments)
if err := xerrors.FirstError(foregroundErr, backgroundErr); err != nil {
return nil, err
}
// Loop over the segments associated to shard time ranges.
for _, group := range b.shardRangesSegments {
for _, seg := range group.segments {
reader, err := seg.Reader()
if err != nil {
return nil, err
}
readers = append(readers, reader)
}
}
success = true
return executor.NewExecutor(readers), nil
}
func (b *block) Query(
query Query,
opts QueryOptions,
results Results,
) (bool, error) {
b.RLock()
defer b.RUnlock()
if b.state == blockStateClosed {
return false, ErrUnableToQueryBlockClosed
}
exec, err := b.newExecutorFn()
if err != nil {
return false, err
}
// FOLLOWUP(prateek): push down QueryOptions to restrict results
iter, err := exec.Execute(query.Query.SearchQuery())
if err != nil {
exec.Close()
return false, err
}
size := results.Size()
limitedResults := false
iterCloser := safeCloser{closable: iter}
execCloser := safeCloser{closable: exec}
defer func() {
iterCloser.Close()
execCloser.Close()
}()
for iter.Next() {
if opts.LimitExceeded(size) {
limitedResults = true
break
}
d := iter.Current()
_, size, err = results.AddDocument(d)
if err != nil {
return false, err
}
}
if err := iter.Err(); err != nil {
return false, err
}
if err := iterCloser.Close(); err != nil {
return false, err
}
if err := execCloser.Close(); err != nil {
return false, err
}
exhaustive := !limitedResults
return exhaustive, nil
}
func (b *block) AddResults(
results result.IndexBlock,
) error {
b.Lock()
defer b.Unlock()
// NB(prateek): we have to allow bootstrap to succeed even if we're Sealed because
// of topology changes. i.e. if the current m3db process is assigned new shards,
// we need to include their data in the index.
// i.e. the only state we do not accept bootstrapped data is if we are closed.
if b.state == blockStateClosed {
return errUnableToBootstrapBlockClosed
}
// First check fulfilled is correct
min, max := results.Fulfilled().MinMax()
if min.Before(b.blockStart) || max.After(b.blockEnd) {
blockRange := xtime.Range{Start: b.blockStart, End: b.blockEnd}
return fmt.Errorf("fulfilled range %s is outside of index block range: %s",
results.Fulfilled().SummaryString(), blockRange.String())
}
entry := blockShardRangesSegments{
shardTimeRanges: results.Fulfilled(),
segments: results.Segments(),
}
// First see if this block can cover all our current blocks covering shard
// time ranges.
currFulfilled := make(result.ShardTimeRanges)
for _, existing := range b.shardRangesSegments {
currFulfilled.AddRanges(existing.shardTimeRanges)
}
unfulfilledBySegments := currFulfilled.Copy()
unfulfilledBySegments.Subtract(results.Fulfilled())
if !unfulfilledBySegments.IsEmpty() {
// This is the case where it cannot wholly replace the current set of blocks
// so simply append the segments in this case.
b.shardRangesSegments = append(b.shardRangesSegments, entry)
return nil
}
// This is the case where the new segments can wholly replace the
// current set of blocks since unfullfilled by the new segments is zero.
multiErr := xerrors.NewMultiError()
for i, group := range b.shardRangesSegments {
for _, seg := range group.segments {
// Make sure to close the existing segments.
multiErr = multiErr.Add(seg.Close())
}
b.shardRangesSegments[i] = blockShardRangesSegments{}
}
b.shardRangesSegments = append(b.shardRangesSegments[:0], entry)
return multiErr.FinalError()
}
func (b *block) Tick(c context.Cancellable, tickStart time.Time) (BlockTickResult, error) {
b.RLock()
defer b.RUnlock()
result := BlockTickResult{}
if b.state == blockStateClosed {
return result, errUnableToTickBlockClosed
}
// Add foreground/background segments.
for _, seg := range b.foregroundSegments {
result.NumSegments++
result.NumDocs += seg.Segment().Size()
}
for _, seg := range b.backgroundSegments {
result.NumSegments++
result.NumDocs += seg.Segment().Size()
}
// Any segments covering persisted shard ranges.
for _, group := range b.shardRangesSegments {
for _, seg := range group.segments {
result.NumSegments++
result.NumDocs += seg.Size()
}
}
return result, nil
}
func (b *block) Seal() error {
b.Lock()
defer b.Unlock()
// Ensure we only Seal if we're marked Open.
if b.state != blockStateOpen {
return fmt.Errorf(errUnableToSealBlockIllegalStateFmtString, b.state)
}
b.state = blockStateSealed
// All foreground/background segments and added mutable segments can't
// be written to and they don't need to be sealed since we don't flush
// these segments.
return nil
}
func (b *block) Stats(reporter BlockStatsReporter) error {
b.RLock()
defer b.RUnlock()
if b.state != blockStateOpen {
return ErrUnableReportStatsBlockClosed
}
for _, seg := range b.foregroundSegments {
_, mutable := seg.Segment().(segment.MutableSegment)
reporter.ReportSegmentStats(BlockSegmentStats{
Type: ActiveForegroundSegment,
Mutable: mutable,
Age: seg.Age(),
Size: seg.Segment().Size(),
})
}
for _, seg := range b.backgroundSegments {
_, mutable := seg.Segment().(segment.MutableSegment)
reporter.ReportSegmentStats(BlockSegmentStats{
Type: ActiveBackgroundSegment,
Mutable: mutable,
Age: seg.Age(),
Size: seg.Segment().Size(),
})
}
for _, shardRangeSegments := range b.shardRangesSegments {
for _, seg := range shardRangeSegments.segments {
_, mutable := seg.(segment.MutableSegment)
reporter.ReportSegmentStats(BlockSegmentStats{
Type: FlushedSegment,
Mutable: mutable,
Size: seg.Size(),
})
}
}
return nil
}
func (b *block) IsSealedWithRLock() bool {
return b.state == blockStateSealed
}
func (b *block) IsSealed() bool {
b.RLock()
defer b.RUnlock()
return b.IsSealedWithRLock()
}
func (b *block) NeedsMutableSegmentsEvicted() bool {
b.RLock()
defer b.RUnlock()
// Check any foreground/background segments that can be evicted after a flush.
var anyMutableSegmentNeedsEviction bool
for _, seg := range b.foregroundSegments {
anyMutableSegmentNeedsEviction = anyMutableSegmentNeedsEviction || seg.Segment().Size() > 0
}
for _, seg := range b.backgroundSegments {
anyMutableSegmentNeedsEviction = anyMutableSegmentNeedsEviction || seg.Segment().Size() > 0
}
// Check boostrapped segments and to see if any of them need an eviction.
for _, shardRangeSegments := range b.shardRangesSegments {
for _, seg := range shardRangeSegments.segments {
if mutableSeg, ok := seg.(segment.MutableSegment); ok {
anyMutableSegmentNeedsEviction = anyMutableSegmentNeedsEviction || mutableSeg.Size() > 0
}
}
}
return anyMutableSegmentNeedsEviction
}
func (b *block) EvictMutableSegments() error {
b.Lock()
defer b.Unlock()
if b.state != blockStateSealed {
return fmt.Errorf("unable to evict mutable segments, block must be sealed, found: %v", b.state)
}
b.hasEvictedMutableSegmentsAnyTimes = true