LCOV - code coverage report
Current view: top level - pebble/metamorphic - options.go (source / functions) Hit Total Coverage
Test: 2023-12-19 08:16Z 5c5ad7ed - meta test only.lcov Lines: 111 536 20.7 %
Date: 2023-12-19 08:16:43 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2019 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 metamorphic
       6             : 
       7             : import (
       8             :         "bytes"
       9             :         "fmt"
      10             :         "os"
      11             :         "path/filepath"
      12             :         "runtime"
      13             :         "strconv"
      14             :         "strings"
      15             :         "time"
      16             : 
      17             :         "github.com/cockroachdb/errors"
      18             :         "github.com/cockroachdb/pebble"
      19             :         "github.com/cockroachdb/pebble/bloom"
      20             :         "github.com/cockroachdb/pebble/internal/cache"
      21             :         "github.com/cockroachdb/pebble/internal/testkeys"
      22             :         "github.com/cockroachdb/pebble/objstorage/remote"
      23             :         "github.com/cockroachdb/pebble/sstable"
      24             :         "github.com/cockroachdb/pebble/vfs"
      25             :         "golang.org/x/exp/rand"
      26             : )
      27             : 
      28             : const (
      29             :         minimumFormatMajorVersion = pebble.FormatMinSupported
      30             :         // The format major version to use in the default options configurations. We
      31             :         // default to the minimum supported format so we exercise the runtime version
      32             :         // ratcheting that a cluster upgrading would experience. The randomized
      33             :         // options may still use format major versions that are less than
      34             :         // defaultFormatMajorVersion but are at least minimumFormatMajorVersion.
      35             :         defaultFormatMajorVersion = pebble.FormatMinSupported
      36             :         // newestFormatMajorVersionToTest is the most recent format major version
      37             :         // the metamorphic tests should use. This may be greater than
      38             :         // pebble.FormatNewest when some format major versions are marked as
      39             :         // experimental.
      40             :         newestFormatMajorVersionToTest = pebble.FormatNewest
      41             : )
      42             : 
      43             : func parseOptions(
      44             :         opts *TestOptions, data string, customOptionParsers map[string]func(string) (CustomOption, bool),
      45           1 : ) error {
      46           1 :         hooks := &pebble.ParseHooks{
      47           1 :                 NewCache:        pebble.NewCache,
      48           1 :                 NewFilterPolicy: filterPolicyFromName,
      49           1 :                 SkipUnknown: func(name, value string) bool {
      50           1 :                         switch name {
      51           0 :                         case "TestOptions":
      52           0 :                                 return true
      53           1 :                         case "TestOptions.strictfs":
      54           1 :                                 opts.strictFS = true
      55           1 :                                 return true
      56           1 :                         case "TestOptions.ingest_using_apply":
      57           1 :                                 opts.ingestUsingApply = true
      58           1 :                                 return true
      59           1 :                         case "TestOptions.delete_sized":
      60           1 :                                 opts.deleteSized = true
      61           1 :                                 return true
      62           1 :                         case "TestOptions.replace_single_delete":
      63           1 :                                 opts.replaceSingleDelete = true
      64           1 :                                 return true
      65           1 :                         case "TestOptions.use_disk":
      66           1 :                                 opts.useDisk = true
      67           1 :                                 return true
      68           0 :                         case "TestOptions.initial_state_desc":
      69           0 :                                 opts.initialStateDesc = value
      70           0 :                                 return true
      71           0 :                         case "TestOptions.initial_state_path":
      72           0 :                                 opts.initialStatePath = value
      73           0 :                                 return true
      74           1 :                         case "TestOptions.threads":
      75           1 :                                 v, err := strconv.Atoi(value)
      76           1 :                                 if err != nil {
      77           0 :                                         panic(err)
      78             :                                 }
      79           1 :                                 opts.threads = v
      80           1 :                                 return true
      81           1 :                         case "TestOptions.disable_block_property_collector":
      82           1 :                                 v, err := strconv.ParseBool(value)
      83           1 :                                 if err != nil {
      84           0 :                                         panic(err)
      85             :                                 }
      86           1 :                                 opts.disableBlockPropertyCollector = v
      87           1 :                                 if v {
      88           1 :                                         opts.Opts.BlockPropertyCollectors = nil
      89           1 :                                 }
      90           1 :                                 return true
      91           1 :                         case "TestOptions.enable_value_blocks":
      92           1 :                                 opts.enableValueBlocks = true
      93           1 :                                 opts.Opts.Experimental.EnableValueBlocks = func() bool { return true }
      94           1 :                                 return true
      95           1 :                         case "TestOptions.async_apply_to_db":
      96           1 :                                 opts.asyncApplyToDB = true
      97           1 :                                 return true
      98           1 :                         case "TestOptions.shared_storage_enabled":
      99           1 :                                 opts.sharedStorageEnabled = true
     100           1 :                                 sharedStorage := remote.NewInMem()
     101           1 :                                 opts.Opts.Experimental.RemoteStorage = remote.MakeSimpleFactory(map[remote.Locator]remote.Storage{
     102           1 :                                         "": sharedStorage,
     103           1 :                                 })
     104           1 :                                 opts.sharedStorageFS = sharedStorage
     105           1 :                                 if opts.Opts.Experimental.CreateOnShared == remote.CreateOnSharedNone {
     106           0 :                                         opts.Opts.Experimental.CreateOnShared = remote.CreateOnSharedAll
     107           0 :                                 }
     108           1 :                                 return true
     109           1 :                         case "TestOptions.secondary_cache_enabled":
     110           1 :                                 opts.secondaryCacheEnabled = true
     111           1 :                                 opts.Opts.Experimental.SecondaryCacheSizeBytes = 1024 * 1024 * 32 // 32 MBs
     112           1 :                                 return true
     113           1 :                         case "TestOptions.seed_efos":
     114           1 :                                 v, err := strconv.ParseUint(value, 10, 64)
     115           1 :                                 if err != nil {
     116           0 :                                         panic(err)
     117             :                                 }
     118           1 :                                 opts.seedEFOS = v
     119           1 :                                 return true
     120           0 :                         case "TestOptions.ingest_split":
     121           0 :                                 opts.ingestSplit = true
     122           0 :                                 opts.Opts.Experimental.IngestSplit = func() bool {
     123           0 :                                         return true
     124           0 :                                 }
     125           0 :                                 return true
     126           1 :                         case "TestOptions.use_shared_replicate":
     127           1 :                                 opts.useSharedReplicate = true
     128           1 :                                 return true
     129           1 :                         case "TestOptions.use_excise":
     130           1 :                                 opts.useExcise = true
     131           1 :                                 return true
     132           1 :                         case "TestOptions.efos_always_creates_iterators":
     133           1 :                                 opts.efosAlwaysCreatesIters = true
     134           1 :                                 opts.Opts.TestingAlwaysCreateEFOSIterators(true /* value */)
     135           1 :                                 return true
     136           0 :                         default:
     137           0 :                                 if customOptionParsers == nil {
     138           0 :                                         return false
     139           0 :                                 }
     140           0 :                                 name = strings.TrimPrefix(name, "TestOptions.")
     141           0 :                                 if p, ok := customOptionParsers[name]; ok {
     142           0 :                                         if customOpt, ok := p(value); ok {
     143           0 :                                                 opts.CustomOpts = append(opts.CustomOpts, customOpt)
     144           0 :                                                 return true
     145           0 :                                         }
     146             :                                 }
     147           0 :                                 return false
     148             :                         }
     149             :                 },
     150             :         }
     151           1 :         err := opts.Opts.Parse(data, hooks)
     152           1 :         opts.Opts.EnsureDefaults()
     153           1 :         return err
     154             : }
     155             : 
     156           0 : func optionsToString(opts *TestOptions) string {
     157           0 :         var buf bytes.Buffer
     158           0 :         if opts.strictFS {
     159           0 :                 fmt.Fprint(&buf, "  strictfs=true\n")
     160           0 :         }
     161           0 :         if opts.ingestUsingApply {
     162           0 :                 fmt.Fprint(&buf, "  ingest_using_apply=true\n")
     163           0 :         }
     164           0 :         if opts.deleteSized {
     165           0 :                 fmt.Fprint(&buf, "  delete_sized=true\n")
     166           0 :         }
     167           0 :         if opts.replaceSingleDelete {
     168           0 :                 fmt.Fprint(&buf, "  replace_single_delete=true\n")
     169           0 :         }
     170           0 :         if opts.useDisk {
     171           0 :                 fmt.Fprint(&buf, "  use_disk=true\n")
     172           0 :         }
     173           0 :         if opts.initialStatePath != "" {
     174           0 :                 fmt.Fprintf(&buf, "  initial_state_path=%s\n", opts.initialStatePath)
     175           0 :         }
     176           0 :         if opts.initialStateDesc != "" {
     177           0 :                 fmt.Fprintf(&buf, "  initial_state_desc=%s\n", opts.initialStateDesc)
     178           0 :         }
     179           0 :         if opts.threads != 0 {
     180           0 :                 fmt.Fprintf(&buf, "  threads=%d\n", opts.threads)
     181           0 :         }
     182           0 :         if opts.disableBlockPropertyCollector {
     183           0 :                 fmt.Fprintf(&buf, "  disable_block_property_collector=%t\n", opts.disableBlockPropertyCollector)
     184           0 :         }
     185           0 :         if opts.enableValueBlocks {
     186           0 :                 fmt.Fprintf(&buf, "  enable_value_blocks=%t\n", opts.enableValueBlocks)
     187           0 :         }
     188           0 :         if opts.asyncApplyToDB {
     189           0 :                 fmt.Fprint(&buf, "  async_apply_to_db=true\n")
     190           0 :         }
     191           0 :         if opts.sharedStorageEnabled {
     192           0 :                 fmt.Fprint(&buf, "  shared_storage_enabled=true\n")
     193           0 :         }
     194           0 :         if opts.secondaryCacheEnabled {
     195           0 :                 fmt.Fprint(&buf, "  secondary_cache_enabled=true\n")
     196           0 :         }
     197           0 :         if opts.seedEFOS != 0 {
     198           0 :                 fmt.Fprintf(&buf, "  seed_efos=%d\n", opts.seedEFOS)
     199           0 :         }
     200           0 :         if opts.ingestSplit {
     201           0 :                 fmt.Fprintf(&buf, "  ingest_split=%v\n", opts.ingestSplit)
     202           0 :         }
     203           0 :         if opts.useSharedReplicate {
     204           0 :                 fmt.Fprintf(&buf, "  use_shared_replicate=%v\n", opts.useSharedReplicate)
     205           0 :         }
     206           0 :         if opts.useExcise {
     207           0 :                 fmt.Fprintf(&buf, "  use_excise=%v\n", opts.useExcise)
     208           0 :         }
     209           0 :         if opts.efosAlwaysCreatesIters {
     210           0 :                 fmt.Fprintf(&buf, "  efos_always_creates_iterators=%v\n", opts.efosAlwaysCreatesIters)
     211           0 :         }
     212           0 :         for _, customOpt := range opts.CustomOpts {
     213           0 :                 fmt.Fprintf(&buf, "  %s=%s\n", customOpt.Name(), customOpt.Value())
     214           0 :         }
     215             : 
     216           0 :         s := opts.Opts.String()
     217           0 :         if buf.Len() == 0 {
     218           0 :                 return s
     219           0 :         }
     220           0 :         return s + "\n[TestOptions]\n" + buf.String()
     221             : }
     222             : 
     223           1 : func defaultTestOptions() *TestOptions {
     224           1 :         return &TestOptions{
     225           1 :                 Opts:    defaultOptions(),
     226           1 :                 threads: 16,
     227           1 :         }
     228           1 : }
     229             : 
     230           1 : func defaultOptions() *pebble.Options {
     231           1 :         opts := &pebble.Options{
     232           1 :                 Comparer:           testkeys.Comparer,
     233           1 :                 FS:                 vfs.NewMem(),
     234           1 :                 FormatMajorVersion: defaultFormatMajorVersion,
     235           1 :                 Levels: []pebble.LevelOptions{{
     236           1 :                         FilterPolicy: bloom.FilterPolicy(10),
     237           1 :                 }},
     238           1 :                 BlockPropertyCollectors: blockPropertyCollectorConstructors,
     239           1 :         }
     240           1 :         // TODO(sumeer): add IneffectualSingleDeleteCallback that panics by
     241           1 :         // supporting a test option that does not generate ineffectual single
     242           1 :         // deletes.
     243           1 :         opts.Experimental.SingleDeleteInvariantViolationCallback = func(
     244           1 :                 userKey []byte) {
     245           0 :                 panic(errors.AssertionFailedf("single del invariant violations on key %q", userKey))
     246             :         }
     247           1 :         return opts
     248             : }
     249             : 
     250             : // TestOptions describes the options configuring an individual run of the
     251             : // metamorphic tests.
     252             : type TestOptions struct {
     253             :         // Opts holds the *pebble.Options for the test.
     254             :         Opts *pebble.Options
     255             :         // CustomOptions holds custom test options that are defined outside of this
     256             :         // package.
     257             :         CustomOpts []CustomOption
     258             :         useDisk    bool
     259             :         strictFS   bool
     260             :         threads    int
     261             :         // Use Batch.Apply rather than DB.Ingest.
     262             :         ingestUsingApply bool
     263             :         // Use Batch.DeleteSized rather than Batch.Delete.
     264             :         deleteSized bool
     265             :         // Replace a SINGLEDEL with a DELETE.
     266             :         replaceSingleDelete bool
     267             :         // The path on the local filesystem where the initial state of the database
     268             :         // exists.  Empty if the test run begins from an empty database state.
     269             :         initialStatePath string
     270             :         // A human-readable string describing the initial state of the database.
     271             :         // Empty if the test run begins from an empty database state.
     272             :         initialStateDesc string
     273             :         // Disable the block property collector, which may be used by block property
     274             :         // filters.
     275             :         disableBlockPropertyCollector bool
     276             :         // Enable the use of value blocks.
     277             :         enableValueBlocks bool
     278             :         // Use DB.ApplyNoSyncWait for applies that want to sync the WAL.
     279             :         asyncApplyToDB bool
     280             :         // Enable the use of shared storage.
     281             :         sharedStorageEnabled bool
     282             :         // sharedStorageFS stores the remote.Storage that is being used with shared
     283             :         // storage.
     284             :         sharedStorageFS remote.Storage
     285             :         // Enables the use of shared replication in TestOptions.
     286             :         useSharedReplicate bool
     287             :         // Enable the secondary cache. Only effective if sharedStorageEnabled is
     288             :         // also true.
     289             :         secondaryCacheEnabled bool
     290             :         // If nonzero, enables the use of EventuallyFileOnlySnapshots for
     291             :         // newSnapshotOps that are keyspan-bounded. The set of which newSnapshotOps
     292             :         // are actually created as EventuallyFileOnlySnapshots is deterministically
     293             :         // derived from the seed and the operation index.
     294             :         seedEFOS uint64
     295             :         // Enables ingest splits. Saved here for serialization as Options does not
     296             :         // serialize this.
     297             :         ingestSplit bool
     298             :         // Enables operations that do excises. Note that a false value for this does
     299             :         // not guarantee the lack of excises, as useSharedReplicate can also cause
     300             :         // excises. However !useExcise && !useSharedReplicate can be used to guarantee
     301             :         // lack of excises.
     302             :         useExcise bool
     303             :         // Enables EFOS to always create iterators, even if a conflicting excise
     304             :         // happens. Used to guarantee EFOS determinism when conflicting excises are
     305             :         // in play. If false, EFOS determinism is maintained by having the DB do a
     306             :         // flush after every new EFOS.
     307             :         efosAlwaysCreatesIters bool
     308             : }
     309             : 
     310             : // CustomOption defines a custom option that configures the behavior of an
     311             : // individual test run. Like all test options, custom options are serialized to
     312             : // the OPTIONS file even if they're not options ordinarily understood by Pebble.
     313             : type CustomOption interface {
     314             :         // Name returns the name of the custom option. This is the key under which
     315             :         // the option appears in the OPTIONS file, within the [TestOptions] stanza.
     316             :         Name() string
     317             :         // Value returns the value of the custom option, serialized as it should
     318             :         // appear within the OPTIONS file.
     319             :         Value() string
     320             :         // Close is run after the test database has been closed at the end of the
     321             :         // test as well as during restart operations within the test sequence. It's
     322             :         // passed a copy of the *pebble.Options. If the custom options hold on to
     323             :         // any resources outside, Close should release them.
     324             :         Close(*pebble.Options) error
     325             :         // Open is run before the test runs and during a restart operation after the
     326             :         // test database has been closed and Close has been called. It's passed a
     327             :         // copy of the *pebble.Options. If the custom options must acquire any
     328             :         // resources before the test continues, it should reacquire them.
     329             :         Open(*pebble.Options) error
     330             : 
     331             :         // TODO(jackson): provide additional hooks for custom options changing the
     332             :         // behavior of a run.
     333             : }
     334             : 
     335           0 : func standardOptions() []*TestOptions {
     336           0 :         // The index labels are not strictly necessary, but they make it easier to
     337           0 :         // find which options correspond to a failure.
     338           0 :         stdOpts := []string{
     339           0 :                 0: "", // default options
     340           0 :                 1: `
     341           0 : [Options]
     342           0 :   cache_size=1
     343           0 : `,
     344           0 :                 2: `
     345           0 : [Options]
     346           0 :   disable_wal=true
     347           0 : `,
     348           0 :                 3: `
     349           0 : [Options]
     350           0 :   l0_compaction_threshold=1
     351           0 : `,
     352           0 :                 4: `
     353           0 : [Options]
     354           0 :   l0_compaction_threshold=1
     355           0 :   l0_stop_writes_threshold=1
     356           0 : `,
     357           0 :                 5: `
     358           0 : [Options]
     359           0 :   lbase_max_bytes=1
     360           0 : `,
     361           0 :                 6: `
     362           0 : [Options]
     363           0 :   max_manifest_file_size=1
     364           0 : `,
     365           0 :                 7: `
     366           0 : [Options]
     367           0 :   max_open_files=1
     368           0 : `,
     369           0 :                 8: `
     370           0 : [Options]
     371           0 :   mem_table_size=2000
     372           0 : `,
     373           0 :                 9: `
     374           0 : [Options]
     375           0 :   mem_table_stop_writes_threshold=2
     376           0 : `,
     377           0 :                 10: `
     378           0 : [Options]
     379           0 :   wal_dir=data/wal
     380           0 : `,
     381           0 :                 11: `
     382           0 : [Level "0"]
     383           0 :   block_restart_interval=1
     384           0 : `,
     385           0 :                 12: `
     386           0 : [Level "0"]
     387           0 :   block_size=1
     388           0 : `,
     389           0 :                 13: `
     390           0 : [Level "0"]
     391           0 :   compression=NoCompression
     392           0 : `,
     393           0 :                 14: `
     394           0 : [Level "0"]
     395           0 :   index_block_size=1
     396           0 : `,
     397           0 :                 15: `
     398           0 : [Level "0"]
     399           0 :   target_file_size=1
     400           0 : `,
     401           0 :                 16: `
     402           0 : [Level "0"]
     403           0 :   filter_policy=none
     404           0 : `,
     405           0 :                 // 1GB
     406           0 :                 17: `
     407           0 : [Options]
     408           0 :   bytes_per_sync=1073741824
     409           0 : [TestOptions]
     410           0 :   strictfs=true
     411           0 : `,
     412           0 :                 18: `
     413           0 : [Options]
     414           0 :   max_concurrent_compactions=2
     415           0 : `,
     416           0 :                 19: `
     417           0 : [TestOptions]
     418           0 :   ingest_using_apply=true
     419           0 : `,
     420           0 :                 20: `
     421           0 : [TestOptions]
     422           0 :   replace_single_delete=true
     423           0 : `,
     424           0 :                 21: `
     425           0 : [TestOptions]
     426           0 :  use_disk=true
     427           0 : `,
     428           0 :                 22: `
     429           0 : [Options]
     430           0 :   max_writer_concurrency=2
     431           0 :   force_writer_parallelism=true
     432           0 : `,
     433           0 :                 23: `
     434           0 : [TestOptions]
     435           0 :   disable_block_property_collector=true
     436           0 : `,
     437           0 :                 24: `
     438           0 : [TestOptions]
     439           0 :   threads=1
     440           0 : `,
     441           0 :                 25: `
     442           0 : [TestOptions]
     443           0 :   enable_value_blocks=true
     444           0 : `,
     445           0 :                 26: fmt.Sprintf(`
     446           0 : [Options]
     447           0 :   format_major_version=%s
     448           0 : `, newestFormatMajorVersionToTest),
     449           0 :                 27: `
     450           0 : [TestOptions]
     451           0 :   shared_storage_enabled=true
     452           0 :   secondary_cache_enabled=true
     453           0 : `,
     454           0 :         }
     455           0 : 
     456           0 :         opts := make([]*TestOptions, len(stdOpts))
     457           0 :         for i := range opts {
     458           0 :                 opts[i] = defaultTestOptions()
     459           0 :                 // NB: The standard options by definition can never include custom
     460           0 :                 // options, so no need to propagate custom option parsers.
     461           0 :                 if err := parseOptions(opts[i], stdOpts[i], nil /* custom option parsers */); err != nil {
     462           0 :                         panic(err)
     463             :                 }
     464             :         }
     465           0 :         return opts
     466             : }
     467             : 
     468             : func randomOptions(
     469             :         rng *rand.Rand, customOptionParsers map[string]func(string) (CustomOption, bool),
     470           0 : ) *TestOptions {
     471           0 :         testOpts := defaultTestOptions()
     472           0 :         opts := testOpts.Opts
     473           0 : 
     474           0 :         // There are some private options, which we don't want users to fiddle with.
     475           0 :         // There's no way to set it through the public interface. The only method is
     476           0 :         // through Parse.
     477           0 :         {
     478           0 :                 var privateOpts bytes.Buffer
     479           0 :                 fmt.Fprintln(&privateOpts, `[Options]`)
     480           0 :                 if rng.Intn(3) == 0 /* 33% */ {
     481           0 :                         fmt.Fprintln(&privateOpts, `  disable_delete_only_compactions=true`)
     482           0 :                 }
     483           0 :                 if rng.Intn(3) == 0 /* 33% */ {
     484           0 :                         fmt.Fprintln(&privateOpts, `  disable_elision_only_compactions=true`)
     485           0 :                 }
     486           0 :                 if rng.Intn(5) == 0 /* 20% */ {
     487           0 :                         fmt.Fprintln(&privateOpts, `  disable_lazy_combined_iteration=true`)
     488           0 :                 }
     489           0 :                 if privateOptsStr := privateOpts.String(); privateOptsStr != `[Options]\n` {
     490           0 :                         parseOptions(testOpts, privateOptsStr, customOptionParsers)
     491           0 :                 }
     492             :         }
     493             : 
     494           0 :         opts.BytesPerSync = 1 << uint(rng.Intn(28))     // 1B - 256MB
     495           0 :         opts.Cache = cache.New(1 << uint(rng.Intn(30))) // 1B - 1GB
     496           0 :         opts.DisableWAL = rng.Intn(2) == 0
     497           0 :         opts.FlushDelayDeleteRange = time.Millisecond * time.Duration(5*rng.Intn(245)) // 5-250ms
     498           0 :         opts.FlushDelayRangeKey = time.Millisecond * time.Duration(5*rng.Intn(245))    // 5-250ms
     499           0 :         opts.FlushSplitBytes = 1 << rng.Intn(20)                                       // 1B - 1MB
     500           0 :         opts.FormatMajorVersion = minimumFormatMajorVersion
     501           0 :         n := int(newestFormatMajorVersionToTest - opts.FormatMajorVersion)
     502           0 :         opts.FormatMajorVersion += pebble.FormatMajorVersion(rng.Intn(n + 1))
     503           0 :         opts.Experimental.L0CompactionConcurrency = 1 + rng.Intn(4) // 1-4
     504           0 :         opts.Experimental.LevelMultiplier = 5 << rng.Intn(7)        // 5 - 320
     505           0 :         opts.TargetByteDeletionRate = 1 << uint(20+rng.Intn(10))    // 1MB - 1GB
     506           0 :         opts.Experimental.ValidateOnIngest = rng.Intn(2) != 0
     507           0 :         opts.L0CompactionThreshold = 1 + rng.Intn(100)     // 1 - 100
     508           0 :         opts.L0CompactionFileThreshold = 1 << rng.Intn(11) // 1 - 1024
     509           0 :         opts.L0StopWritesThreshold = 1 + rng.Intn(100)     // 1 - 100
     510           0 :         if opts.L0StopWritesThreshold < opts.L0CompactionThreshold {
     511           0 :                 opts.L0StopWritesThreshold = opts.L0CompactionThreshold
     512           0 :         }
     513           0 :         opts.LBaseMaxBytes = 1 << uint(rng.Intn(30)) // 1B - 1GB
     514           0 :         maxConcurrentCompactions := rng.Intn(3) + 1  // 1-3
     515           0 :         opts.MaxConcurrentCompactions = func() int {
     516           0 :                 return maxConcurrentCompactions
     517           0 :         }
     518           0 :         opts.MaxManifestFileSize = 1 << uint(rng.Intn(30)) // 1B  - 1GB
     519           0 :         opts.MemTableSize = 2 << (10 + uint(rng.Intn(16))) // 2KB - 256MB
     520           0 :         opts.MemTableStopWritesThreshold = 2 + rng.Intn(5) // 2 - 5
     521           0 :         if rng.Intn(2) == 0 {
     522           0 :                 opts.WALDir = "data/wal"
     523           0 :         }
     524           0 :         if rng.Intn(4) == 0 {
     525           0 :                 // Enable Writer parallelism for 25% of the random options. Setting
     526           0 :                 // MaxWriterConcurrency to any value greater than or equal to 1 has the
     527           0 :                 // same effect currently.
     528           0 :                 opts.Experimental.MaxWriterConcurrency = 2
     529           0 :                 opts.Experimental.ForceWriterParallelism = true
     530           0 :         }
     531           0 :         if rng.Intn(2) == 0 {
     532           0 :                 opts.Experimental.DisableIngestAsFlushable = func() bool { return true }
     533             :         }
     534             : 
     535             :         // We either use no multilevel compactions, multilevel compactions with the
     536             :         // default (zero) additional propensity, or multilevel compactions with an
     537             :         // additional propensity to encourage more multilevel compactions than we
     538             :         // ohterwise would.
     539           0 :         switch rng.Intn(3) {
     540           0 :         case 0:
     541           0 :                 opts.Experimental.MultiLevelCompactionHeuristic = pebble.NoMultiLevel{}
     542           0 :         case 1:
     543           0 :                 opts.Experimental.MultiLevelCompactionHeuristic = pebble.WriteAmpHeuristic{}
     544           0 :         default:
     545           0 :                 opts.Experimental.MultiLevelCompactionHeuristic = pebble.WriteAmpHeuristic{
     546           0 :                         AddPropensity: rng.Float64() * float64(rng.Intn(3)), // [0,3.0)
     547           0 :                         AllowL0:       rng.Intn(4) == 1,                     // 25% of the time
     548           0 :                 }
     549             :         }
     550             : 
     551           0 :         var lopts pebble.LevelOptions
     552           0 :         lopts.BlockRestartInterval = 1 + rng.Intn(64)  // 1 - 64
     553           0 :         lopts.BlockSize = 1 << uint(rng.Intn(24))      // 1 - 16MB
     554           0 :         lopts.BlockSizeThreshold = 50 + rng.Intn(50)   // 50 - 100
     555           0 :         lopts.IndexBlockSize = 1 << uint(rng.Intn(24)) // 1 - 16MB
     556           0 :         lopts.TargetFileSize = 1 << uint(rng.Intn(28)) // 1 - 256MB
     557           0 : 
     558           0 :         // We either use no bloom filter, the default filter, or a filter with
     559           0 :         // randomized bits-per-key setting. We zero out the Filters map. It'll get
     560           0 :         // repopulated on EnsureDefaults accordingly.
     561           0 :         opts.Filters = nil
     562           0 :         switch rng.Intn(3) {
     563           0 :         case 0:
     564           0 :                 lopts.FilterPolicy = nil
     565           0 :         case 1:
     566           0 :                 lopts.FilterPolicy = bloom.FilterPolicy(10)
     567           0 :         default:
     568           0 :                 lopts.FilterPolicy = newTestingFilterPolicy(1 << rng.Intn(5))
     569             :         }
     570             : 
     571             :         // We use either no compression, snappy compression or zstd compression.
     572           0 :         switch rng.Intn(3) {
     573           0 :         case 0:
     574           0 :                 lopts.Compression = pebble.NoCompression
     575           0 :         case 1:
     576           0 :                 lopts.Compression = pebble.ZstdCompression
     577           0 :         default:
     578           0 :                 lopts.Compression = pebble.SnappyCompression
     579             :         }
     580           0 :         opts.Levels = []pebble.LevelOptions{lopts}
     581           0 : 
     582           0 :         // Explicitly disable disk-backed FS's for the random configurations. The
     583           0 :         // single standard test configuration that uses a disk-backed FS is
     584           0 :         // sufficient.
     585           0 :         testOpts.useDisk = false
     586           0 :         testOpts.strictFS = rng.Intn(2) != 0 // Only relevant for MemFS.
     587           0 :         testOpts.threads = rng.Intn(runtime.GOMAXPROCS(0)) + 1
     588           0 :         if testOpts.strictFS {
     589           0 :                 opts.DisableWAL = false
     590           0 :         }
     591           0 :         testOpts.ingestUsingApply = rng.Intn(2) != 0
     592           0 :         testOpts.deleteSized = rng.Intn(2) != 0
     593           0 :         testOpts.replaceSingleDelete = rng.Intn(2) != 0
     594           0 :         testOpts.disableBlockPropertyCollector = rng.Intn(2) == 1
     595           0 :         if testOpts.disableBlockPropertyCollector {
     596           0 :                 testOpts.Opts.BlockPropertyCollectors = nil
     597           0 :         }
     598           0 :         testOpts.enableValueBlocks = rng.Intn(2) != 0
     599           0 :         if testOpts.enableValueBlocks {
     600           0 :                 testOpts.Opts.Experimental.EnableValueBlocks = func() bool { return true }
     601             :         }
     602           0 :         testOpts.asyncApplyToDB = rng.Intn(2) != 0
     603           0 :         // 20% of time, enable shared storage.
     604           0 :         if rng.Intn(5) == 0 {
     605           0 :                 testOpts.sharedStorageEnabled = true
     606           0 :                 inMemShared := remote.NewInMem()
     607           0 :                 testOpts.Opts.Experimental.RemoteStorage = remote.MakeSimpleFactory(map[remote.Locator]remote.Storage{
     608           0 :                         "": inMemShared,
     609           0 :                 })
     610           0 :                 testOpts.sharedStorageFS = inMemShared
     611           0 :                 // If shared storage is enabled, pick between writing all files on shared
     612           0 :                 // vs. lower levels only, 50% of the time.
     613           0 :                 testOpts.Opts.Experimental.CreateOnShared = remote.CreateOnSharedAll
     614           0 :                 if rng.Intn(2) == 0 {
     615           0 :                         testOpts.Opts.Experimental.CreateOnShared = remote.CreateOnSharedLower
     616           0 :                 }
     617             :                 // If shared storage is enabled, enable secondary cache 50% of time.
     618           0 :                 if rng.Intn(2) == 0 {
     619           0 :                         testOpts.secondaryCacheEnabled = true
     620           0 :                         // TODO(josh): Randomize various secondary cache settings.
     621           0 :                         testOpts.Opts.Experimental.SecondaryCacheSizeBytes = 1024 * 1024 * 32 // 32 MBs
     622           0 :                 }
     623             :                 // 50% of the time, enable shared replication.
     624           0 :                 testOpts.useSharedReplicate = rng.Intn(2) == 0
     625             :         }
     626           0 :         testOpts.seedEFOS = rng.Uint64()
     627           0 :         // TODO(bilal): Enable ingestSplit when known bugs with virtual sstables
     628           0 :         // are addressed.
     629           0 :         //
     630           0 :         // testOpts.ingestSplit = rng.Intn(2) == 0
     631           0 :         opts.Experimental.IngestSplit = func() bool { return testOpts.ingestSplit }
     632           0 :         testOpts.useExcise = rng.Intn(2) == 0
     633           0 :         if testOpts.useExcise || testOpts.useSharedReplicate {
     634           0 :                 testOpts.efosAlwaysCreatesIters = rng.Intn(2) == 0
     635           0 :                 opts.TestingAlwaysCreateEFOSIterators(testOpts.efosAlwaysCreatesIters)
     636           0 :                 if testOpts.Opts.FormatMajorVersion < pebble.FormatVirtualSSTables {
     637           0 :                         testOpts.Opts.FormatMajorVersion = pebble.FormatVirtualSSTables
     638           0 :                 }
     639             :         }
     640           0 :         testOpts.Opts.EnsureDefaults()
     641           0 :         return testOpts
     642             : }
     643             : 
     644           0 : func setupInitialState(dataDir string, testOpts *TestOptions) error {
     645           0 :         // Copy (vfs.Default,<initialStatePath>/data) to (testOpts.opts.FS,<dataDir>).
     646           0 :         ok, err := vfs.Clone(
     647           0 :                 vfs.Default,
     648           0 :                 testOpts.Opts.FS,
     649           0 :                 vfs.Default.PathJoin(testOpts.initialStatePath, "data"),
     650           0 :                 dataDir,
     651           0 :                 vfs.CloneSync,
     652           0 :                 vfs.CloneSkip(func(filename string) bool {
     653           0 :                         // Skip the archive of historical files, any checkpoints created by
     654           0 :                         // operations and files staged for ingest in tmp.
     655           0 :                         b := filepath.Base(filename)
     656           0 :                         return b == "archive" || b == "checkpoints" || b == "tmp"
     657           0 :                 }))
     658           0 :         if err != nil {
     659           0 :                 return err
     660           0 :         } else if !ok {
     661           0 :                 return os.ErrNotExist
     662           0 :         }
     663             : 
     664             :         // Tests with wal_dir set store their WALs in a `wal` directory. The source
     665             :         // database (initialStatePath) could've had wal_dir set, or the current test
     666             :         // options (testOpts) could have wal_dir set, or both.
     667           0 :         fs := testOpts.Opts.FS
     668           0 :         walDir := fs.PathJoin(dataDir, "wal")
     669           0 :         if err := fs.MkdirAll(walDir, os.ModePerm); err != nil {
     670           0 :                 return err
     671           0 :         }
     672             : 
     673             :         // Copy <dataDir>/wal/*.log -> <dataDir>.
     674           0 :         src, dst := walDir, dataDir
     675           0 :         if testOpts.Opts.WALDir != "" {
     676           0 :                 // Copy <dataDir>/*.log -> <dataDir>/wal.
     677           0 :                 src, dst = dst, src
     678           0 :         }
     679           0 :         return moveLogs(fs, src, dst)
     680             : }
     681             : 
     682           0 : func moveLogs(fs vfs.FS, srcDir, dstDir string) error {
     683           0 :         ls, err := fs.List(srcDir)
     684           0 :         if err != nil {
     685           0 :                 return err
     686           0 :         }
     687           0 :         for _, f := range ls {
     688           0 :                 if filepath.Ext(f) != ".log" {
     689           0 :                         continue
     690             :                 }
     691           0 :                 src := fs.PathJoin(srcDir, f)
     692           0 :                 dst := fs.PathJoin(dstDir, f)
     693           0 :                 if err := fs.Rename(src, dst); err != nil {
     694           0 :                         return err
     695           0 :                 }
     696             :         }
     697           0 :         return nil
     698             : }
     699             : 
     700             : var blockPropertyCollectorConstructors = []func() pebble.BlockPropertyCollector{
     701             :         sstable.NewTestKeysBlockPropertyCollector,
     702             : }
     703             : 
     704             : // testingFilterPolicy is used to allow bloom filter policies with non-default
     705             : // bits-per-key setting. It is necessary because the name of the production
     706             : // filter policy is fixed (see bloom.FilterPolicy.Name()); we need to output a
     707             : // custom policy name to the OPTIONS file that the test can then parse.
     708             : type testingFilterPolicy struct {
     709             :         bloom.FilterPolicy
     710             : }
     711             : 
     712             : var _ pebble.FilterPolicy = (*testingFilterPolicy)(nil)
     713             : 
     714           1 : func newTestingFilterPolicy(bitsPerKey int) *testingFilterPolicy {
     715           1 :         return &testingFilterPolicy{
     716           1 :                 FilterPolicy: bloom.FilterPolicy(bitsPerKey),
     717           1 :         }
     718           1 : }
     719             : 
     720             : const testingFilterPolicyFmt = "testing_bloom_filter/bits_per_key=%d"
     721             : 
     722             : // Name implements the pebble.FilterPolicy interface.
     723           1 : func (t *testingFilterPolicy) Name() string {
     724           1 :         if t.FilterPolicy == 10 {
     725           0 :                 return "rocksdb.BuiltinBloomFilter"
     726           0 :         }
     727           1 :         return fmt.Sprintf(testingFilterPolicyFmt, t.FilterPolicy)
     728             : }
     729             : 
     730           1 : func filterPolicyFromName(name string) (pebble.FilterPolicy, error) {
     731           1 :         switch name {
     732           1 :         case "none":
     733           1 :                 return nil, nil
     734           1 :         case "rocksdb.BuiltinBloomFilter":
     735           1 :                 return bloom.FilterPolicy(10), nil
     736             :         }
     737           1 :         var bitsPerKey int
     738           1 :         if _, err := fmt.Sscanf(name, testingFilterPolicyFmt, &bitsPerKey); err != nil {
     739           0 :                 return nil, errors.Errorf("Invalid filter policy name '%s'", name)
     740           0 :         }
     741           1 :         return newTestingFilterPolicy(bitsPerKey), nil
     742             : }

Generated by: LCOV version 1.14