LCOV - code coverage report
Current view: top level - pebble - format_major_version.go (source / functions) Hit Total Coverage
Test: 2024-07-08 08:16Z fad89cfb - meta test only.lcov Lines: 82 186 44.1 %
Date: 2024-07-08 08:17:18 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use
       2             : // of this source code is governed by a BSD-style license that can be found in
       3             : // the LICENSE file.
       4             : 
       5             : package pebble
       6             : 
       7             : import (
       8             :         "fmt"
       9             :         "strconv"
      10             : 
      11             :         "github.com/cockroachdb/errors"
      12             :         "github.com/cockroachdb/pebble/internal/manifest"
      13             :         "github.com/cockroachdb/pebble/sstable"
      14             :         "github.com/cockroachdb/pebble/vfs"
      15             :         "github.com/cockroachdb/pebble/vfs/atomicfs"
      16             : )
      17             : 
      18             : // FormatMajorVersion is a constant controlling the format of persisted
      19             : // data. Backwards incompatible changes to durable formats are gated
      20             : // behind new format major versions.
      21             : //
      22             : // At any point, a database's format major version may be bumped.
      23             : // However, once a database's format major version is increased,
      24             : // previous versions of Pebble will refuse to open the database.
      25             : //
      26             : // The zero value format is the FormatDefault constant. The exact
      27             : // FormatVersion that the default corresponds to may change with time.
      28             : type FormatMajorVersion uint64
      29             : 
      30             : // SafeValue implements redact.SafeValue.
      31           0 : func (v FormatMajorVersion) SafeValue() {}
      32             : 
      33             : // String implements fmt.Stringer.
      34           1 : func (v FormatMajorVersion) String() string {
      35           1 :         // NB: This must not change. It's used as the value for the on-disk
      36           1 :         // version marker file.
      37           1 :         //
      38           1 :         // Specifically, this value must always parse as a base 10 integer
      39           1 :         // that fits in a uint64. We format it as zero-padded, 3-digit
      40           1 :         // number today, but the padding may change.
      41           1 :         return fmt.Sprintf("%03d", v)
      42           1 : }
      43             : 
      44             : const (
      45             :         // FormatDefault leaves the format version unspecified. When used to create a
      46             :         // new store, Pebble will choose the earliest format version it supports.
      47             :         FormatDefault FormatMajorVersion = iota
      48             : 
      49             :         // 21.2 versions.
      50             : 
      51             :         // FormatMostCompatible maintains the most backwards compatibility,
      52             :         // maintaining bi-directional compatibility with RocksDB 6.2.1 in
      53             :         // the particular configuration described in the Pebble README.
      54             :         // Deprecated.
      55             :         _ // FormatMostCompatible
      56             : 
      57             :         // formatVersionedManifestMarker is the first
      58             :         // backwards-incompatible change made to Pebble, introducing the
      59             :         // format-version marker file for handling backwards-incompatible
      60             :         // changes more broadly, and replacing the `CURRENT` file with a
      61             :         // marker file.
      62             :         //
      63             :         // This format version is intended as an intermediary version state.
      64             :         // It is deliberately unexported to discourage direct use of this
      65             :         // format major version.  Clients should use FormatVersioned which
      66             :         // also ensures earlier versions of Pebble fail to open a database
      67             :         // written in a future format major version.
      68             :         // Deprecated.
      69             :         _ // formatVersionedManifestMarker
      70             : 
      71             :         // FormatVersioned is a new format major version that replaces the
      72             :         // old `CURRENT` file with a new 'marker' file scheme.  Previous
      73             :         // Pebble versions will be unable to open the database unless
      74             :         // they're aware of format versions.
      75             :         // Deprecated.
      76             :         _ // FormatVersioned
      77             : 
      78             :         // FormatSetWithDelete is a format major version that introduces a new key
      79             :         // kind, base.InternalKeyKindSetWithDelete. Previous Pebble versions will be
      80             :         // unable to open this database.
      81             :         // Deprecated.
      82             :         _ // FormatSetWithDelete
      83             : 
      84             :         // 22.1 versions.
      85             : 
      86             :         // FormatBlockPropertyCollector is a format major version that introduces
      87             :         // BlockPropertyCollectors.
      88             :         // Deprecated.
      89             :         _ // FormatBlockPropertyCollector
      90             : 
      91             :         // FormatSplitUserKeysMarked is a format major version that guarantees that
      92             :         // all files that share user keys with neighbors are marked for compaction
      93             :         // in the manifest. Ratcheting to FormatSplitUserKeysMarked will block
      94             :         // (without holding mutexes) until the scan of the LSM is complete and the
      95             :         // manifest has been rotated.
      96             :         // Deprecated.
      97             :         _ // FormatSplitUserKeysMarked
      98             : 
      99             :         // 22.2 versions.
     100             : 
     101             :         // FormatSplitUserKeysMarkedCompacted is a format major version that
     102             :         // guarantees that all files explicitly marked for compaction in the manifest
     103             :         // have been compacted. Combined with the FormatSplitUserKeysMarked format
     104             :         // major version, this version guarantees that there are no user keys split
     105             :         // across multiple files within a level L1+. Ratcheting to this format version
     106             :         // will block (without holding mutexes) until all necessary compactions for
     107             :         // files marked for compaction are complete.
     108             :         // Deprecated.
     109             :         _ // FormatSplitUserKeysMarkedCompacted
     110             : 
     111             :         // FormatRangeKeys is a format major version that introduces range keys.
     112             :         // Deprecated.
     113             :         _ // FormatRangeKeys
     114             : 
     115             :         // FormatMinTableFormatPebblev1 is a format major version that guarantees that
     116             :         // tables created by or ingested into the DB at or above this format major
     117             :         // version will have a table format version of at least Pebblev1 (Block
     118             :         // Properties).
     119             :         // Deprecated.
     120             :         _ // FormatMinTableFormatPebblev1
     121             : 
     122             :         // FormatPrePebblev1Marked is a format major version that guarantees that all
     123             :         // sstables with a table format version pre-Pebblev1 (i.e. those that are
     124             :         // guaranteed to not contain block properties) are marked for compaction in
     125             :         // the manifest. Ratcheting to FormatPrePebblev1Marked will block (without
     126             :         // holding mutexes) until the scan of the LSM is complete and the manifest has
     127             :         // been rotated.
     128             :         // Deprecated.
     129             :         _ // FormatPrePebblev1Marked
     130             : 
     131             :         // 23.1 versions.
     132             : 
     133             :         // formatUnusedPrePebblev1MarkedCompacted is an unused format major version.
     134             :         // This format major version was originally intended to ship in the 23.1
     135             :         // release. It was later decided that this should be deferred until a
     136             :         // subsequent release. The original ordering is preserved so as not to
     137             :         // introduce breaking changes in Cockroach.
     138             :         _ // formatUnusedPrePebblev1MarkedCompacted
     139             : 
     140             :         // FormatSSTableValueBlocks is a format major version that adds support for
     141             :         // storing values in value blocks in the sstable. Value block support is not
     142             :         // necessarily enabled when writing sstables, when running with this format
     143             :         // major version.
     144             :         _ // FormatSSTableValueBlocks
     145             : 
     146             :         // FormatFlushableIngest is a format major version that enables lazy
     147             :         // addition of ingested sstables into the LSM structure. When an ingest
     148             :         // overlaps with a memtable, a record of the ingest is written to the WAL
     149             :         // without waiting for a flush. Subsequent reads treat the ingested files as
     150             :         // a level above the overlapping memtable. Once the memtable is flushed, the
     151             :         // ingested files are moved into the lowest possible levels.
     152             :         //
     153             :         // This feature is behind a format major version because it required
     154             :         // breaking changes to the WAL format.
     155             :         FormatFlushableIngest
     156             : 
     157             :         // 23.2 versions.
     158             : 
     159             :         // FormatPrePebblev1MarkedCompacted is a format major version that guarantees
     160             :         // that all sstables explicitly marked for compaction in the manifest (see
     161             :         // FormatPrePebblev1Marked) have been compacted. Ratcheting to this format
     162             :         // version will block (without holding mutexes) until all necessary
     163             :         // compactions for files marked for compaction are complete.
     164             :         FormatPrePebblev1MarkedCompacted
     165             : 
     166             :         // FormatDeleteSizedAndObsolete is a format major version that adds support
     167             :         // for deletion tombstones that encode the size of the value they're
     168             :         // expected to delete. This format major version is required before the
     169             :         // associated key kind may be committed through batch applications or
     170             :         // ingests. It also adds support for keys that are marked obsolete (see
     171             :         // sstable/format.go for details).
     172             :         FormatDeleteSizedAndObsolete
     173             : 
     174             :         // FormatVirtualSSTables is a format major version that adds support for
     175             :         // virtual sstables that can reference a sub-range of keys in an underlying
     176             :         // physical sstable. This information is persisted through new,
     177             :         // backward-incompatible fields in the Manifest, and therefore requires
     178             :         // a format major version.
     179             :         FormatVirtualSSTables
     180             : 
     181             :         // FormatSyntheticPrefixSuffix is a format major version that adds support for
     182             :         // sstables to have their content exposed in a different prefix or suffix of
     183             :         // keyspace than the actual prefix/suffix persisted in the keys in such
     184             :         // sstables. The prefix and suffix replacement information is stored in new
     185             :         // fields in the Manifest and thus requires a format major version.
     186             :         FormatSyntheticPrefixSuffix
     187             : 
     188             :         // TODO(msbutler): add major version for synthetic suffixes
     189             : 
     190             :         // -- Add new versions here --
     191             : 
     192             :         // FormatNewest is the most recent format major version.
     193             :         FormatNewest FormatMajorVersion = iota - 1
     194             : 
     195             :         // Experimental versions, which are excluded by FormatNewest (but can be used
     196             :         // in tests) can be defined here.
     197             : 
     198             :         // -- Add experimental versions here --
     199             : 
     200             :         // internalFormatNewest is the most recent, possibly experimental format major
     201             :         // version.
     202             :         internalFormatNewest FormatMajorVersion = iota - 2
     203             : )
     204             : 
     205             : // FormatMinSupported is the minimum format version that is supported by this
     206             : // Pebble version.
     207             : const FormatMinSupported = FormatFlushableIngest
     208             : 
     209             : // FormatMinForSharedObjects it the minimum format version that supports shared
     210             : // objects (see CreateOnShared option).
     211             : const FormatMinForSharedObjects = FormatVirtualSSTables
     212             : 
     213             : // IsSupported returns true if the version is supported by the current Pebble
     214             : // version.
     215           0 : func (v FormatMajorVersion) IsSupported() bool {
     216           0 :         return v == FormatDefault && v >= FormatMinSupported && v <= internalFormatNewest
     217           0 : }
     218             : 
     219             : // MaxTableFormat returns the maximum sstable.TableFormat that can be used at
     220             : // this FormatMajorVersion.
     221           1 : func (v FormatMajorVersion) MaxTableFormat() sstable.TableFormat {
     222           1 :         switch v {
     223           1 :         case FormatDefault, FormatFlushableIngest, FormatPrePebblev1MarkedCompacted:
     224           1 :                 return sstable.TableFormatPebblev3
     225           1 :         case FormatDeleteSizedAndObsolete, FormatVirtualSSTables, FormatSyntheticPrefixSuffix:
     226           1 :                 return sstable.TableFormatPebblev4
     227           0 :         default:
     228           0 :                 panic(fmt.Sprintf("pebble: unsupported format major version: %s", v))
     229             :         }
     230             : }
     231             : 
     232             : // MinTableFormat returns the minimum sstable.TableFormat that can be used at
     233             : // this FormatMajorVersion.
     234           1 : func (v FormatMajorVersion) MinTableFormat() sstable.TableFormat {
     235           1 :         switch v {
     236             :         case FormatDefault, FormatFlushableIngest, FormatPrePebblev1MarkedCompacted,
     237           1 :                 FormatDeleteSizedAndObsolete, FormatVirtualSSTables, FormatSyntheticPrefixSuffix:
     238           1 :                 return sstable.TableFormatPebblev1
     239           0 :         default:
     240           0 :                 panic(fmt.Sprintf("pebble: unsupported format major version: %s", v))
     241             :         }
     242             : }
     243             : 
     244             : // formatMajorVersionMigrations defines the migrations from one format
     245             : // major version to the next. Each migration is defined as a closure
     246             : // which will be invoked on the database before the new format major
     247             : // version is committed. Migrations must be idempotent. Migrations are
     248             : // invoked with d.mu locked.
     249             : //
     250             : // Each migration is responsible for invoking finalizeFormatVersUpgrade
     251             : // to set the new format major version.  RatchetFormatMajorVersion will
     252             : // panic if a migration returns a nil error but fails to finalize the
     253             : // new format major version.
     254             : var formatMajorVersionMigrations = map[FormatMajorVersion]func(*DB) error{
     255           0 :         FormatFlushableIngest: func(d *DB) error { return nil },
     256           1 :         FormatPrePebblev1MarkedCompacted: func(d *DB) error {
     257           1 :                 // Before finalizing the format major version, rewrite any sstables
     258           1 :                 // still marked for compaction. Note all format major versions
     259           1 :                 // migrations are invoked with DB.mu locked.
     260           1 :                 if err := d.compactMarkedFilesLocked(); err != nil {
     261           0 :                         return err
     262           0 :                 }
     263           1 :                 return d.finalizeFormatVersUpgrade(FormatPrePebblev1MarkedCompacted)
     264             :         },
     265           1 :         FormatDeleteSizedAndObsolete: func(d *DB) error {
     266           1 :                 return d.finalizeFormatVersUpgrade(FormatDeleteSizedAndObsolete)
     267           1 :         },
     268           1 :         FormatVirtualSSTables: func(d *DB) error {
     269           1 :                 return d.finalizeFormatVersUpgrade(FormatVirtualSSTables)
     270           1 :         },
     271           1 :         FormatSyntheticPrefixSuffix: func(d *DB) error {
     272           1 :                 return d.finalizeFormatVersUpgrade(FormatSyntheticPrefixSuffix)
     273           1 :         },
     274             : }
     275             : 
     276             : const formatVersionMarkerName = `format-version`
     277             : 
     278             : // lookupFormatMajorVersion retrieves the format version from the format version
     279             : // marker file.
     280             : //
     281             : // If such a file does not exist, returns FormatDefault. Note that this case is
     282             : // only acceptable if we are creating a new store (we no longer support
     283             : // FormatMostCompatible which is the only one with no version marker file).
     284             : func lookupFormatMajorVersion(
     285             :         fs vfs.FS, dirname string, ls []string,
     286           1 : ) (FormatMajorVersion, *atomicfs.Marker, error) {
     287           1 :         m, versString, err := atomicfs.LocateMarkerInListing(fs, dirname, formatVersionMarkerName, ls)
     288           1 :         if err != nil {
     289           0 :                 return 0, nil, err
     290           0 :         }
     291           1 :         if versString == "" {
     292           1 :                 return FormatDefault, m, nil
     293           1 :         }
     294           1 :         v, err := strconv.ParseUint(versString, 10, 64)
     295           1 :         if err != nil {
     296           0 :                 return 0, nil, errors.Wrap(err, "parsing format major version")
     297           0 :         }
     298           1 :         vers := FormatMajorVersion(v)
     299           1 :         if vers == FormatDefault {
     300           0 :                 return 0, nil, errors.Newf("pebble: default format major version should not persisted", vers)
     301           0 :         }
     302           1 :         if vers > internalFormatNewest {
     303           0 :                 return 0, nil, errors.Newf("pebble: database %q written in unknown format major version %d", dirname, vers)
     304           0 :         }
     305           1 :         if vers < FormatMinSupported {
     306           0 :                 return 0, nil, errors.Newf("pebble: database %q written in format major version %d which is no longer supported", dirname, vers)
     307           0 :         }
     308           1 :         return vers, m, nil
     309             : }
     310             : 
     311             : // FormatMajorVersion returns the database's active format major
     312             : // version. The format major version may be higher than the one
     313             : // provided in Options when the database was opened if the existing
     314             : // database was written with a higher format version.
     315           1 : func (d *DB) FormatMajorVersion() FormatMajorVersion {
     316           1 :         return FormatMajorVersion(d.mu.formatVers.vers.Load())
     317           1 : }
     318             : 
     319             : // RatchetFormatMajorVersion ratchets the opened database's format major
     320             : // version to the provided version. It errors if the provided format
     321             : // major version is below the database's current version. Once a
     322             : // database's format major version is upgraded, previous Pebble versions
     323             : // that do not know of the format version will be unable to open the
     324             : // database.
     325           1 : func (d *DB) RatchetFormatMajorVersion(fmv FormatMajorVersion) error {
     326           1 :         if err := d.closed.Load(); err != nil {
     327           0 :                 panic(err)
     328             :         }
     329             : 
     330           1 :         d.mu.Lock()
     331           1 :         defer d.mu.Unlock()
     332           1 :         return d.ratchetFormatMajorVersionLocked(fmv)
     333             : }
     334             : 
     335           1 : func (d *DB) ratchetFormatMajorVersionLocked(formatVers FormatMajorVersion) error {
     336           1 :         if d.opts.ReadOnly {
     337           0 :                 return ErrReadOnly
     338           0 :         }
     339           1 :         if formatVers > internalFormatNewest {
     340           0 :                 // Guard against accidentally forgetting to update internalFormatNewest.
     341           0 :                 return errors.Errorf("pebble: unknown format version %d", formatVers)
     342           0 :         }
     343           1 :         if currentVers := d.FormatMajorVersion(); currentVers > formatVers {
     344           0 :                 return errors.Newf("pebble: database already at format major version %d; cannot reduce to %d",
     345           0 :                         currentVers, formatVers)
     346           0 :         }
     347           1 :         if d.mu.formatVers.ratcheting {
     348           0 :                 return errors.Newf("pebble: database format major version upgrade is in-progress")
     349           0 :         }
     350           1 :         d.mu.formatVers.ratcheting = true
     351           1 :         defer func() { d.mu.formatVers.ratcheting = false }()
     352             : 
     353           1 :         for nextVers := d.FormatMajorVersion() + 1; nextVers <= formatVers; nextVers++ {
     354           1 :                 if err := formatMajorVersionMigrations[nextVers](d); err != nil {
     355           0 :                         return errors.Wrapf(err, "migrating to version %d", nextVers)
     356           0 :                 }
     357             : 
     358             :                 // NB: The migration is responsible for calling
     359             :                 // finalizeFormatVersUpgrade to finalize the upgrade. This
     360             :                 // structure is necessary because some migrations may need to
     361             :                 // update in-memory state (without ever dropping locks) after
     362             :                 // the upgrade is finalized. Here we assert that the upgrade
     363             :                 // did occur.
     364           1 :                 if d.FormatMajorVersion() != nextVers {
     365           0 :                         d.opts.Logger.Fatalf("pebble: successful migration to format version %d never finalized the upgrade", nextVers)
     366           0 :                 }
     367             :         }
     368           1 :         return nil
     369             : }
     370             : 
     371             : // finalizeFormatVersUpgrade is typically only be called from within a
     372             : // format major version migration.
     373             : //
     374             : // See formatMajorVersionMigrations.
     375           1 : func (d *DB) finalizeFormatVersUpgrade(formatVers FormatMajorVersion) error {
     376           1 :         if err := d.writeFormatVersionMarker(formatVers); err != nil {
     377           0 :                 return err
     378           0 :         }
     379           1 :         d.mu.formatVers.vers.Store(uint64(formatVers))
     380           1 :         d.opts.EventListener.FormatUpgrade(formatVers)
     381           1 :         return nil
     382             : }
     383             : 
     384           1 : func (d *DB) writeFormatVersionMarker(formatVers FormatMajorVersion) error {
     385           1 :         // We use the marker to encode the active format version in the
     386           1 :         // marker filename. Unlike other uses of the atomic marker, there is
     387           1 :         // no file with the filename `formatVers.String()` on the
     388           1 :         // filesystem.
     389           1 :         return d.mu.formatVers.marker.Move(formatVers.String())
     390           1 : }
     391             : 
     392             : // compactMarkedFilesLocked performs a migration that schedules rewrite
     393             : // compactions to compact away any sstables marked for compaction.
     394             : // compactMarkedFilesLocked is run while ratcheting the database's format major
     395             : // version to FormatSplitUserKeysMarkedCompacted.
     396             : //
     397             : // Note that while this method is called with the DB.mu held, and will not
     398             : // return until all marked files have been compacted, the mutex is dropped while
     399             : // waiting for compactions to complete (or for slots to free up).
     400           1 : func (d *DB) compactMarkedFilesLocked() error {
     401           1 :         curr := d.mu.versions.currentVersion()
     402           1 :         for curr.Stats.MarkedForCompaction > 0 {
     403           0 :                 // Attempt to schedule a compaction to rewrite a file marked for
     404           0 :                 // compaction.
     405           0 :                 d.maybeScheduleCompactionPicker(func(picker compactionPicker, env compactionEnv) *pickedCompaction {
     406           0 :                         return picker.pickRewriteCompaction(env)
     407           0 :                 })
     408             : 
     409             :                 // The above attempt might succeed and schedule a rewrite compaction. Or
     410             :                 // there might not be available compaction concurrency to schedule the
     411             :                 // compaction.  Or compaction of the file might have already been in
     412             :                 // progress. In any scenario, wait until there's some change in the
     413             :                 // state of active compactions.
     414             : 
     415             :                 // Before waiting, check that the database hasn't been closed. Trying to
     416             :                 // schedule the compaction may have dropped d.mu while waiting for a
     417             :                 // manifest write to complete. In that dropped interim, the database may
     418             :                 // have been closed.
     419           0 :                 if err := d.closed.Load(); err != nil {
     420           0 :                         return err.(error)
     421           0 :                 }
     422             : 
     423             :                 // Some flush or compaction may have scheduled or completed while we waited
     424             :                 // for the manifest lock in maybeScheduleCompactionPicker. Get the latest
     425             :                 // Version before waiting on a compaction.
     426           0 :                 curr = d.mu.versions.currentVersion()
     427           0 : 
     428           0 :                 // Only wait on compactions if there are files still marked for compaction.
     429           0 :                 // NB: Waiting on this condition variable drops d.mu while blocked.
     430           0 :                 if curr.Stats.MarkedForCompaction > 0 {
     431           0 :                         if d.mu.compact.compactingCount == 0 {
     432           0 :                                 panic("expected a compaction of marked files in progress")
     433             :                         }
     434           0 :                         d.mu.compact.cond.Wait()
     435           0 :                         // Refresh the current version again.
     436           0 :                         curr = d.mu.versions.currentVersion()
     437             :                 }
     438             :         }
     439           1 :         return nil
     440             : }
     441             : 
     442             : // findFilesFunc scans the LSM for files, returning true if at least one
     443             : // file was found. The returned array contains the matched files, if any, per
     444             : // level.
     445             : type findFilesFunc func(v *version) (found bool, files [numLevels][]*fileMetadata, _ error)
     446             : 
     447             : // This method is not used currently, but it will be useful the next time we need
     448             : // to mark files for compaction.
     449             : var _ = (*DB)(nil).markFilesLocked
     450             : 
     451             : // markFilesLocked durably marks the files that match the given findFilesFunc for
     452             : // compaction.
     453           0 : func (d *DB) markFilesLocked(findFn findFilesFunc) error {
     454           0 :         jobID := d.newJobIDLocked()
     455           0 : 
     456           0 :         // Acquire a read state to have a view of the LSM and a guarantee that none
     457           0 :         // of the referenced files will be deleted until we've unreferenced the read
     458           0 :         // state. Some findFilesFuncs may read the files, requiring they not be
     459           0 :         // deleted.
     460           0 :         rs := d.loadReadState()
     461           0 :         var (
     462           0 :                 found bool
     463           0 :                 files [numLevels][]*fileMetadata
     464           0 :                 err   error
     465           0 :         )
     466           0 :         func() {
     467           0 :                 defer rs.unrefLocked()
     468           0 :                 // Note the unusual locking: unlock, defer Lock(). The scan of the files in
     469           0 :                 // the version does not need to block other operations that require the
     470           0 :                 // DB.mu. Drop it for the scan, before re-acquiring it.
     471           0 :                 d.mu.Unlock()
     472           0 :                 defer d.mu.Lock()
     473           0 :                 found, files, err = findFn(rs.current)
     474           0 :         }()
     475           0 :         if err != nil {
     476           0 :                 return err
     477           0 :         }
     478             : 
     479             :         // The database lock has been acquired again by the defer within the above
     480             :         // anonymous function.
     481           0 :         if !found {
     482           0 :                 // Nothing to do.
     483           0 :                 return nil
     484           0 :         }
     485             : 
     486             :         // After scanning, if we found files to mark, we fetch the current state of
     487             :         // the LSM (which may have changed) and set MarkedForCompaction on the files,
     488             :         // and update the version's Stats.MarkedForCompaction count, which are both
     489             :         // protected by d.mu.
     490             : 
     491             :         // Lock the manifest for a coherent view of the LSM. The database lock has
     492             :         // been re-acquired by the defer within the above anonymous function.
     493           0 :         d.mu.versions.logLock()
     494           0 :         vers := d.mu.versions.currentVersion()
     495           0 :         for l, filesToMark := range files {
     496           0 :                 if len(filesToMark) == 0 {
     497           0 :                         continue
     498             :                 }
     499           0 :                 for _, f := range filesToMark {
     500           0 :                         // Ignore files to be marked that have already been compacted or marked.
     501           0 :                         if f.CompactionState == manifest.CompactionStateCompacted ||
     502           0 :                                 f.MarkedForCompaction {
     503           0 :                                 continue
     504             :                         }
     505             :                         // Else, mark the file for compaction in this version.
     506           0 :                         vers.Stats.MarkedForCompaction++
     507           0 :                         f.MarkedForCompaction = true
     508             :                 }
     509             :                 // The compaction picker uses the markedForCompactionAnnotator to
     510             :                 // quickly find files marked for compaction, or to quickly determine
     511             :                 // that there are no such files marked for compaction within a level.
     512             :                 // A b-tree node may be annotated with an annotation recording that
     513             :                 // there are no files marked for compaction within the node's subtree,
     514             :                 // based on the assumption that it's static.
     515             :                 //
     516             :                 // Since we're marking files for compaction, these b-tree nodes'
     517             :                 // annotations will be out of date. Clear the compaction-picking
     518             :                 // annotation, so that it's recomputed the next time the compaction
     519             :                 // picker looks for a file marked for compaction.
     520           0 :                 vers.Levels[l].InvalidateAnnotation(markedForCompactionAnnotator{})
     521             :         }
     522             : 
     523             :         // The 'marked-for-compaction' bit is persisted in the MANIFEST file
     524             :         // metadata. We've already modified the in-memory file metadata, but the
     525             :         // manifest hasn't been updated. Force rotation to a new MANIFEST file,
     526             :         // which will write every file metadata to the new manifest file and ensure
     527             :         // that the now marked-for-compaction file metadata are persisted as marked.
     528             :         // NB: This call to logAndApply will unlockthe MANIFEST, which we locked up
     529             :         // above before obtaining `vers`.
     530           0 :         return d.mu.versions.logAndApply(
     531           0 :                 jobID,
     532           0 :                 &manifest.VersionEdit{},
     533           0 :                 map[int]*LevelMetrics{},
     534           0 :                 true, /* forceRotation */
     535           0 :                 func() []compactionInfo { return d.getInProgressCompactionInfoLocked(nil) })
     536             : }

Generated by: LCOV version 1.14