@@ -536,7 +536,7 @@ func (d *DB) writeFormatVersionMarker(formatVers FormatMajorVersion) error {
536
536
// waiting for compactions to complete (or for slots to free up).
537
537
func (d * DB ) compactMarkedFilesLocked () error {
538
538
curr := d .mu .versions .currentVersion ()
539
- if curr .Stats . MarkedForCompaction == 0 {
539
+ if curr .MarkedForCompaction . Count () == 0 {
540
540
return nil
541
541
}
542
542
// Attempt to schedule a compaction to rewrite a file marked for compaction.
@@ -552,7 +552,7 @@ func (d *DB) compactMarkedFilesLocked() error {
552
552
// compaction. Or compaction of the file might have already been in
553
553
// progress. In any scenario, wait until there's some change in the
554
554
// state of active compactions.
555
- for curr .Stats . MarkedForCompaction > 0 {
555
+ for curr .MarkedForCompaction . Count () > 0 {
556
556
// Before waiting, check that the database hasn't been closed. Trying to
557
557
// schedule the compaction may have dropped d.mu while waiting for a
558
558
// manifest write to complete. In that dropped interim, the database may
@@ -568,7 +568,7 @@ func (d *DB) compactMarkedFilesLocked() error {
568
568
569
569
// Only wait on compactions if there are files still marked for compaction.
570
570
// NB: Waiting on this condition variable drops d.mu while blocked.
571
- if curr .Stats . MarkedForCompaction > 0 {
571
+ if curr .MarkedForCompaction . Count () > 0 {
572
572
// NB: we cannot assert that d.mu.compact.compactingCount > 0, since
573
573
// with a CompactionScheduler a DB may not have even one ongoing
574
574
// compaction (if other competing activities are being preferred by the
@@ -586,13 +586,9 @@ func (d *DB) compactMarkedFilesLocked() error {
586
586
// level.
587
587
type findFilesFunc func (v * manifest.Version ) (found bool , files [numLevels ][]* manifest.TableMetadata , _ error )
588
588
589
- // This method is not used currently, but it will be useful the next time we need
590
- // to mark files for compaction.
591
- var _ = (* DB )(nil ).markFilesLocked
592
-
593
- // markFilesLocked durably marks the files that match the given findFilesFunc for
589
+ // markFilesForCompactionLocked durably marks the files that match the given findFilesFunc for
594
590
// compaction.
595
- func (d * DB ) markFilesLocked (findFn findFilesFunc ) error {
591
+ func (d * DB ) markFilesForCompactionLocked (findFn findFilesFunc ) error {
596
592
jobID := d .newJobIDLocked ()
597
593
598
594
// Acquire a read state to have a view of the LSM and a guarantee that none
@@ -634,32 +630,22 @@ func (d *DB) markFilesLocked(findFn findFilesFunc) error {
634
630
// been re-acquired by the defer within the above anonymous function.
635
631
_ , err = d .mu .versions .UpdateVersionLocked (func () (versionUpdate , error ) {
636
632
vers := d .mu .versions .currentVersion ()
637
- for l , filesToMark := range files {
638
- if len (filesToMark ) == 0 {
639
- continue
640
- }
633
+ for level , filesToMark := range files {
641
634
for _ , f := range filesToMark {
642
635
// Ignore files to be marked that have already been compacted or marked.
643
636
if f .CompactionState == manifest .CompactionStateCompacted ||
644
637
f .MarkedForCompaction {
645
638
continue
646
639
}
647
640
// Else, mark the file for compaction in this version.
648
- vers .Stats .MarkedForCompaction ++
649
641
f .MarkedForCompaction = true
642
+ // We are modifying the current version in-place (so that the updated
643
+ // set is reflected in the "base" version in the new manifest). This is
644
+ // ok because we are holding the DB lock and all code that uses the
645
+ // MarkedForCompaction set runs under the DB lock.
646
+ // TODO(radu): find a less sketchy way to do this.
647
+ vers .MarkedForCompaction .Insert (f , level )
650
648
}
651
- // The compaction picker uses the markedForCompactionAnnotator to
652
- // quickly find files marked for compaction, or to quickly determine
653
- // that there are no such files marked for compaction within a level.
654
- // A b-tree node may be annotated with an annotation recording that
655
- // there are no files marked for compaction within the node's subtree,
656
- // based on the assumption that it's static.
657
- //
658
- // Since we're marking files for compaction, these b-tree nodes'
659
- // annotations will be out of date. Clear the compaction-picking
660
- // annotation, so that it's recomputed the next time the compaction
661
- // picker looks for a file marked for compaction.
662
- markedForCompactionAnnotator .InvalidateLevelAnnotation (vers .Levels [l ])
663
649
}
664
650
// The 'marked-for-compaction' bit is persisted in the MANIFEST file
665
651
// metadata. We've already modified the in-memory table metadata, but the
0 commit comments