LCOV - code coverage report
Current view: top level - pebble/internal/metamorphic/metaflags - meta_flags.go (source / functions) Hit Total Coverage
Test: 2024-05-28 08:16Z 542915b2 - meta test only.lcov Lines: 70 173 40.5 %
Date: 2024-05-28 08:17:42 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2023 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 metaflags defines command-line flags for the metamorphic tests and
       6             : // provides functionality to construct the respective
       7             : // metamorphic.RunOptions/RunOnceOptions.
       8             : package metaflags
       9             : 
      10             : import (
      11             :         "flag"
      12             :         "fmt"
      13             :         "math"
      14             :         "os"
      15             :         "regexp"
      16             :         "strings"
      17             :         "time"
      18             : 
      19             :         "github.com/cockroachdb/pebble/internal/invariants"
      20             :         "github.com/cockroachdb/pebble/internal/randvar"
      21             :         "github.com/cockroachdb/pebble/metamorphic"
      22             : )
      23             : 
      24             : // CommonFlags contains flags that apply to both metamorphic.Run and
      25             : // metamorphic.RunOnce/Compare.
      26             : type CommonFlags struct {
      27             :         // Dir is the directory storing test state. See "dir" flag below.
      28             :         Dir string
      29             :         // Seed for generation of random operations. See "seed" flag below.
      30             :         Seed uint64
      31             :         // ErrorRate is the rate of injected filesystem errors. See "error-rate" flag
      32             :         // below.
      33             :         ErrorRate float64
      34             :         // FailRE causes the test to fail if the output matches this regex. See "fail"
      35             :         // flag below.
      36             :         FailRE string
      37             :         // Keep determines if the DB directory is kept on successful runs. See "keep"
      38             :         // flag below.
      39             :         Keep bool
      40             :         // MaxThreads used by a single run. See "max-threads" flag below.
      41             :         MaxThreads int
      42             :         // NumInstances is the number of Pebble instances to create in one run. See
      43             :         // "num-instances" flag below.
      44             :         NumInstances int
      45             :         // OpTimeout is a per-operation timeout.
      46             :         OpTimeout time.Duration
      47             : }
      48             : 
      49           1 : func initCommonFlags() *CommonFlags {
      50           1 :         c := &CommonFlags{}
      51           1 :         flag.StringVar(&c.Dir, "dir", "_meta",
      52           1 :                 "the directory storing test state")
      53           1 : 
      54           1 :         flag.Uint64Var(&c.Seed, "seed", 0,
      55           1 :                 "a pseudorandom number generator seed")
      56           1 : 
      57           1 :         // TODO: default error rate to a non-zero value. Currently, retrying is
      58           1 :         // non-deterministic because of the Ierator.*WithLimit() methods since
      59           1 :         // they may say that the Iterator is not valid, but be positioned at a
      60           1 :         // certain key that can be returned in the future if the limit is changed.
      61           1 :         // Since that key is hidden from clients of Iterator, the retryableIter
      62           1 :         // using SeekGE will not necessarily position the Iterator that saw an
      63           1 :         // injected error at the same place as an Iterator that did not see that
      64           1 :         // error.
      65           1 :         flag.Float64Var(&c.ErrorRate, "error-rate", 0.0,
      66           1 :                 "rate of errors injected into filesystem operations (0 ≤ r < 1)")
      67           1 : 
      68           1 :         flag.StringVar(&c.FailRE, "fail", "",
      69           1 :                 "fail the test if the supplied regular expression matches the output")
      70           1 : 
      71           1 :         flag.BoolVar(&c.Keep, "keep", false,
      72           1 :                 "keep the DB directory even on successful runs")
      73           1 : 
      74           1 :         flag.IntVar(&c.MaxThreads, "max-threads", math.MaxInt,
      75           1 :                 "limit execution of a single run to the provided number of threads; must be ≥ 1")
      76           1 : 
      77           1 :         flag.IntVar(&c.NumInstances, "num-instances", 1, "number of pebble instances to create (default: 1)")
      78           1 : 
      79           1 :         defaultOpTimeout := 1 * time.Minute
      80           1 :         if invariants.RaceEnabled {
      81           0 :                 defaultOpTimeout *= 5
      82           0 :         }
      83           1 :         flag.DurationVar(&c.OpTimeout, "op-timeout", defaultOpTimeout, "per-op timeout")
      84           1 : 
      85           1 :         return c
      86             : }
      87             : 
      88             : // RunOnceFlags contains flags that apply only to metamorphic.RunOnce/Compare.
      89             : type RunOnceFlags struct {
      90             :         *CommonFlags
      91             :         // RunDir applies to metamorphic.RunOnce and contains the specific
      92             :         // configuration of the run. See "run-dir" flag below.
      93             :         RunDir string
      94             :         // Compare applies to metamorphic.Compare. See "compare" flag below.
      95             :         Compare string
      96             :         // TryToReduce enables a mode where we try to find a minimal subset of
      97             :         // operations that reproduce a problem during a test run (e.g. panic or
      98             :         // internal error).
      99             :         TryToReduce bool
     100             :         // ReduceAttempts is the number of attempts to reduce (for each op removal
     101             :         // probability).
     102             :         ReduceAttempts int
     103             : }
     104             : 
     105           1 : func initRunOnceFlags(c *CommonFlags) *RunOnceFlags {
     106           1 :         ro := &RunOnceFlags{CommonFlags: c}
     107           1 :         flag.StringVar(&ro.RunDir, "run-dir", "",
     108           1 :                 `directory containing the specific configuration to (re-)run (used for post-mortem debugging).
     109           1 : Example: --run-dir _meta/231220-164251.3552792807512/random-003`)
     110           1 : 
     111           1 :         flag.StringVar(&ro.Compare, "compare", "",
     112           1 :                 `runs to compare, in the format _meta/test-root-dir/{run1,run2,...}. The result
     113           1 : of each run is compared with the result of the first run.
     114           1 : Example, --compare '_meta/231220-164251.3552792807512/{standard-000,random-025}'`)
     115           1 : 
     116           1 :         flag.BoolVar(&ro.TryToReduce, "try-to-reduce", false,
     117           1 :                 `if set, we will try to reduce the number of operations that cause a failure. The
     118           1 : verbose flag should be used with this flag.`)
     119           1 : 
     120           1 :         flag.IntVar(&ro.ReduceAttempts, "reduce-attempts", 100,
     121           1 :                 `the number of attempts to reduce, for each probability; only used with --try-to-reduce.`)
     122           1 : 
     123           1 :         return ro
     124           1 : }
     125             : 
     126             : // RunFlags contains flags that apply only to metamorphic.Run.
     127             : type RunFlags struct {
     128             :         *CommonFlags
     129             :         // FS controls the type of filesystems to use. See "fs" flag below.
     130             :         FS string
     131             :         // TraceFile for execution tracing. See "trace-file" flag below.
     132             :         TraceFile string
     133             :         // Ops describes how the total number of operations is generated. See "ops" flags below.
     134             :         Ops randvar.Flag
     135             :         // InnerBinary is the binary to invoke for a single run. See "inner-binary"
     136             :         // flag below.
     137             :         InnerBinary string
     138             :         // PreviousOps is the path to the ops file of a previous run. See the
     139             :         // "previous-ops" flag below.
     140             :         PreviousOps string
     141             :         // InitialStatePath is the path to a database data directory from a previous
     142             :         // run. See the "initial-state" flag below.
     143             :         InitialStatePath string
     144             :         // InitialStateDesc is a human-readable description of the initial database
     145             :         // state. See "initial-state-desc" flag below.
     146             :         InitialStateDesc string
     147             : }
     148             : 
     149           0 : func initRunFlags(c *CommonFlags) *RunFlags {
     150           0 :         r := &RunFlags{CommonFlags: c}
     151           0 :         flag.StringVar(&r.FS, "fs", "rand",
     152           0 :                 `force the tests to use either memory or disk-backed filesystems (valid: "mem", "disk", "rand")`)
     153           0 : 
     154           0 :         flag.StringVar(&r.TraceFile, "trace-file", "",
     155           0 :                 "write an execution trace to `<run-dir>/file`")
     156           0 : 
     157           0 :         if err := r.Ops.Set("uniform:5000-10000"); err != nil {
     158           0 :                 panic(err)
     159             :         }
     160           0 :         flag.Var(&r.Ops, "ops", "uniform:5000-10000")
     161           0 : 
     162           0 :         flag.StringVar(&r.InnerBinary, "inner-binary", "",
     163           0 :                 `binary to run for each instance of the test (this same binary by default); cannot be used
     164           0 : with --run-dir or --compare`)
     165           0 : 
     166           0 :         // The following options may be used for split-version metamorphic testing.
     167           0 :         // To perform split-version testing, the client runs the metamorphic tests
     168           0 :         // on an earlier Pebble SHA passing the `--keep` flag. The client then
     169           0 :         // switches to the later Pebble SHA, setting the below options to point to
     170           0 :         // the `ops` file and one of the previous run's data directories.
     171           0 : 
     172           0 :         flag.StringVar(&r.PreviousOps, "previous-ops", "",
     173           0 :                 "path to an ops file, used to prepopulate the set of keys operations draw from")
     174           0 : 
     175           0 :         flag.StringVar(&r.InitialStatePath, "initial-state", "",
     176           0 :                 "path to a database's data directory, used to prepopulate the test run's databases")
     177           0 : 
     178           0 :         flag.StringVar(&r.InitialStateDesc, "initial-state-desc", "",
     179           0 :                 `a human-readable description of the initial database state.
     180           0 :                 If set this parameter is written to the OPTIONS to aid in
     181           0 :                 debugging. It's intended to describe the lineage of a
     182           0 :                 database's state, including sufficient information for
     183           0 :                 reproduction (eg, SHA, prng seed, etc).`)
     184           0 :         return r
     185             : }
     186             : 
     187             : // InitRunOnceFlags initializes the flags that are used for a single run of the
     188             : // metamorphic test.
     189           1 : func InitRunOnceFlags() *RunOnceFlags {
     190           1 :         return initRunOnceFlags(initCommonFlags())
     191           1 : }
     192             : 
     193             : // InitAllFlags initializes all metamorphic test flags: those used for a
     194             : // single run, and those used for a "top level" run.
     195           0 : func InitAllFlags() (*RunOnceFlags, *RunFlags) {
     196           0 :         c := initCommonFlags()
     197           0 :         return initRunOnceFlags(c), initRunFlags(c)
     198           0 : }
     199             : 
     200             : // MakeRunOnceOptions constructs RunOnceOptions based on the flags.
     201           1 : func (ro *RunOnceFlags) MakeRunOnceOptions() []metamorphic.RunOnceOption {
     202           1 :         onceOpts := []metamorphic.RunOnceOption{
     203           1 :                 metamorphic.MaxThreads(ro.MaxThreads),
     204           1 :                 metamorphic.OpTimeout(ro.OpTimeout),
     205           1 :         }
     206           1 :         if ro.Keep {
     207           0 :                 onceOpts = append(onceOpts, metamorphic.KeepData{})
     208           0 :         }
     209           1 :         if ro.FailRE != "" {
     210           0 :                 onceOpts = append(onceOpts, metamorphic.FailOnMatch{Regexp: regexp.MustCompile(ro.FailRE)})
     211           0 :         }
     212           1 :         if ro.ErrorRate > 0 {
     213           0 :                 onceOpts = append(onceOpts, metamorphic.InjectErrorsRate(ro.ErrorRate))
     214           0 :         }
     215           1 :         if ro.NumInstances > 1 {
     216           1 :                 onceOpts = append(onceOpts, metamorphic.MultiInstance(ro.NumInstances))
     217           1 :         }
     218           1 :         return onceOpts
     219             : }
     220             : 
     221             : // ParseCompare parses the value of the compare flag, in format
     222             : // "test-root-dir/{run1,run2,...}". Exits if the value is not valid.
     223             : //
     224             : // Returns the common test root dir (e.g. "test-root-dir") and a list of
     225             : // subdirectories (e.g. {"run1", "run2"}).
     226           0 : func (ro *RunOnceFlags) ParseCompare() (testRootDir string, runSubdirs []string) {
     227           0 :         testRootDir, runSubdirs, ok := ro.tryParseCompare()
     228           0 :         if !ok {
     229           0 :                 fmt.Fprintf(os.Stderr, `cannot parse compare flag value %q; format is "test-root-dir/{run1,run2,..}"`, ro.Compare)
     230           0 :                 os.Exit(1)
     231           0 :         }
     232           0 :         return testRootDir, runSubdirs
     233             : }
     234             : 
     235           0 : func (ro *RunOnceFlags) tryParseCompare() (testRootDir string, runSubdirs []string, ok bool) {
     236           0 :         value := ro.Compare
     237           0 :         brace := strings.Index(value, "{")
     238           0 :         if brace == -1 || !strings.HasSuffix(value, "}") {
     239           0 :                 return "", nil, false
     240           0 :         }
     241             : 
     242           0 :         testRootDir = value[:brace]
     243           0 :         runSubdirs = strings.Split(value[brace+1:len(value)-1], ",")
     244           0 :         if len(runSubdirs) < 2 {
     245           0 :                 return "", nil, false
     246           0 :         }
     247           0 :         return testRootDir, runSubdirs, true
     248             : }
     249             : 
     250             : // MakeRunOptions constructs RunOptions based on the flags.
     251           0 : func (r *RunFlags) MakeRunOptions() []metamorphic.RunOption {
     252           0 :         opts := []metamorphic.RunOption{
     253           0 :                 metamorphic.Seed(r.Seed),
     254           0 :                 metamorphic.OpCount(r.Ops.Static),
     255           0 :                 metamorphic.MaxThreads(r.MaxThreads),
     256           0 :                 metamorphic.OpTimeout(r.OpTimeout),
     257           0 :         }
     258           0 :         if r.Keep {
     259           0 :                 opts = append(opts, metamorphic.KeepData{})
     260           0 :         }
     261           0 :         if r.FailRE != "" {
     262           0 :                 opts = append(opts, metamorphic.FailOnMatch{Regexp: regexp.MustCompile(r.FailRE)})
     263           0 :         }
     264           0 :         if r.ErrorRate > 0 {
     265           0 :                 opts = append(opts, metamorphic.InjectErrorsRate(r.ErrorRate))
     266           0 :         }
     267           0 :         if r.TraceFile != "" {
     268           0 :                 opts = append(opts, metamorphic.RuntimeTrace(r.TraceFile))
     269           0 :         }
     270           0 :         if r.PreviousOps != "" {
     271           0 :                 opts = append(opts, metamorphic.ExtendPreviousRun(r.PreviousOps, r.InitialStatePath, r.InitialStateDesc))
     272           0 :         }
     273           0 :         if r.NumInstances > 1 {
     274           0 :                 opts = append(opts, metamorphic.MultiInstance(r.NumInstances))
     275           0 :         }
     276             : 
     277             :         // If the filesystem type was forced, all tests will use that value.
     278           0 :         switch r.FS {
     279           0 :         case "", "rand", "default":
     280             :                 // No-op. Use the generated value for the filesystem.
     281           0 :         case "disk":
     282           0 :                 opts = append(opts, metamorphic.UseDisk)
     283           0 :         case "mem":
     284           0 :                 opts = append(opts, metamorphic.UseInMemory)
     285           0 :         default:
     286           0 :                 panic(fmt.Sprintf("unknown forced filesystem type: %q", r.FS))
     287             :         }
     288           0 :         if r.InnerBinary != "" {
     289           0 :                 opts = append(opts, metamorphic.InnerBinary(r.InnerBinary))
     290           0 :         }
     291           0 :         return opts
     292             : }

Generated by: LCOV version 1.14