Skip to content

Commit 12893cb

Browse files
committed
metamorphic: fix options ranges
Some bitshifts were off by 1 due to using rand.IntN, which returns a half open interval. Fix options ranges to match the one described by comments.
1 parent 727d3e6 commit 12893cb

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

metamorphic/options.go

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -650,28 +650,28 @@ func RandomOptions(rng *rand.Rand, kf KeyFormat, cfg RandomOptionsCfg) *TestOpti
650650
}
651651
}
652652

653-
opts.BytesPerSync = 1 << uint(rng.IntN(28)) // 1B - 256MB
654-
opts.CacheSize = 1 << uint(rng.IntN(30)) // 1B - 1GB
653+
opts.BytesPerSync = int(randPowerOf2(rng, 0, 28)) // 1B - 256MB
654+
opts.CacheSize = int64(randPowerOf2(rng, 0, 30)) // 1B - 1GB
655655
opts.DisableWAL = rng.IntN(2) == 0
656-
opts.FlushDelayDeleteRange = time.Millisecond * time.Duration(5*rng.IntN(245)) // 5-250ms
657-
opts.FlushDelayRangeKey = time.Millisecond * time.Duration(5*rng.IntN(245)) // 5-250ms
658-
opts.FlushSplitBytes = 1 << rng.IntN(20) // 1B - 1MB
656+
opts.FlushDelayDeleteRange = time.Millisecond * time.Duration(randInRange(rng, 5, 250)) // 5-250ms
657+
opts.FlushDelayRangeKey = time.Millisecond * time.Duration(randInRange(rng, 5, 250)) // 5-250ms
658+
opts.FlushSplitBytes = int64(randPowerOf2(rng, 0, 20)) // 1B - 1MB
659659
opts.FormatMajorVersion = minimumFormatMajorVersion
660660
n := int(newestFormatMajorVersionToTest - opts.FormatMajorVersion)
661661
opts.FormatMajorVersion += pebble.FormatMajorVersion(rng.IntN(n + 1))
662-
opts.Experimental.L0CompactionConcurrency = 1 + rng.IntN(4) // 1-4
663-
opts.Experimental.LevelMultiplier = 5 << rng.IntN(7) // 5 - 320
664-
targetByteDeletionRate := uint64(1) << uint(20+rng.IntN(10)) // 1MB - 1GB
662+
opts.Experimental.L0CompactionConcurrency = 1 + rng.IntN(4) // 1-4
663+
opts.Experimental.LevelMultiplier = 5 * int(randPowerOf2(rng, 0, 6)) // 5 - 320
664+
targetByteDeletionRate := randPowerOf2(rng, 20, 30) // 1MB - 1GB
665665
opts.DeletionPacing.BaselineRate = func() uint64 { return targetByteDeletionRate }
666666
opts.Experimental.ValidateOnIngest = rng.IntN(2) != 0
667-
opts.L0CompactionThreshold = 1 + rng.IntN(100) // 1 - 100
668-
opts.L0CompactionFileThreshold = 1 << rng.IntN(11) // 1 - 1024
669-
opts.L0StopWritesThreshold = 50 + rng.IntN(100) // 50 - 150
667+
opts.L0CompactionThreshold = 1 + rng.IntN(100) // 1 - 100
668+
opts.L0CompactionFileThreshold = int(randPowerOf2(rng, 0, 10)) // 1 - 1024
669+
opts.L0StopWritesThreshold = randInRange(rng, 50, 150) // 50 - 150
670670
if opts.L0StopWritesThreshold < 2*opts.L0CompactionThreshold {
671671
opts.L0StopWritesThreshold = 2 * opts.L0CompactionThreshold
672672
}
673-
opts.LBaseMaxBytes = 1 << uint(rng.IntN(30)) // 1B - 1GB
674-
maxConcurrentCompactions := rng.IntN(3) + 1 // 1-3
673+
opts.LBaseMaxBytes = int64(randPowerOf2(rng, 0, 30)) // 1B - 1GB
674+
maxConcurrentCompactions := rng.IntN(3) + 1 // 1-3
675675
minConcurrentCompactions := 1
676676
if rng.IntN(4) == 0 {
677677
minConcurrentCompactions = rng.IntN(maxConcurrentCompactions) + 1
@@ -688,9 +688,9 @@ func RandomOptions(rng *rand.Rand, kf KeyFormat, cfg RandomOptionsCfg) *TestOpti
688688
opts.MaxConcurrentDownloads = func() int {
689689
return maxConcurrentDownloads
690690
}
691-
opts.MaxManifestFileSize = 1 << uint(rng.IntN(30)) // 1B - 1GB
692-
opts.MemTableSize = 2 << (10 + uint(rng.IntN(16))) // 2KB - 256MB
693-
opts.MemTableStopWritesThreshold = 2 + rng.IntN(5) // 2 - 5
691+
opts.MaxManifestFileSize = int64(randPowerOf2(rng, 0, 30)) // 1B - 1GB
692+
opts.MemTableSize = randPowerOf2(rng, 11, 28) // 2KB - 256MB
693+
opts.MemTableStopWritesThreshold = randInRange(rng, 2, 5) // 2 - 5
694694
if rng.IntN(2) == 0 {
695695
opts.WALDir = pebble.MakeStoreRelativePath(opts.FS, "wal")
696696
}
@@ -754,7 +754,7 @@ func RandomOptions(rng *rand.Rand, kf KeyFormat, cfg RandomOptionsCfg) *TestOpti
754754
opts.AllocatorSizeClasses = pebble.JemallocSizeClasses
755755
}
756756

757-
opts.TargetFileSizes[0] = 1 << uint(rng.IntN(28)) // 1 - 256MB
757+
opts.TargetFileSizes[0] = int64(randPowerOf2(rng, 0, 28)) // 1B - 256MB
758758
if opts.TargetFileSizes[0] < 1<<12 {
759759
// We will generate a lot of files, which will slow down compactions.
760760
// Increase L0StopWritesThreshold to reduce the number of write stalls
@@ -766,10 +766,10 @@ func RandomOptions(rng *rand.Rand, kf KeyFormat, cfg RandomOptionsCfg) *TestOpti
766766
opts.TargetFileSizes[0] = max(opts.TargetFileSizes[0], 12)
767767

768768
var lopts pebble.LevelOptions
769-
lopts.BlockRestartInterval = 1 + rng.IntN(64) // 1 - 64
770-
lopts.BlockSize = 1 << uint(rng.IntN(24)) // 1 - 16MB
771-
lopts.BlockSizeThreshold = 50 + rng.IntN(50) // 50 - 100
772-
lopts.IndexBlockSize = 1 << uint(rng.IntN(24)) // 1 - 16MB
769+
lopts.BlockRestartInterval = 1 + rng.IntN(64) // 1 - 64
770+
lopts.BlockSize = int(randPowerOf2(rng, 0, 24)) // 1B - 16MB
771+
lopts.BlockSizeThreshold = 50 + rng.IntN(51) // 50 - 100
772+
lopts.IndexBlockSize = int(randPowerOf2(rng, 0, 24)) // 1B - 16MB
773773

774774
// We either use no bloom filter, the default filter, or a filter with
775775
// randomized bits-per-key setting. We zero out the Filters map. It'll get
@@ -781,7 +781,7 @@ func RandomOptions(rng *rand.Rand, kf KeyFormat, cfg RandomOptionsCfg) *TestOpti
781781
case 1:
782782
lopts.FilterPolicy = bloom.FilterPolicy(10)
783783
default:
784-
lopts.FilterPolicy = newTestingFilterPolicy(1 << rng.IntN(5))
784+
lopts.FilterPolicy = newTestingFilterPolicy(int(randPowerOf2(rng, 0, 4)))
785785
}
786786

787787
switch rng.IntN(4) {
@@ -1079,3 +1079,22 @@ func filterPolicyFromName(name string) (pebble.FilterPolicy, error) {
10791079
}
10801080
return newTestingFilterPolicy(bitsPerKey), nil
10811081
}
1082+
1083+
// randInRange returns an integer in the range [minRange,maxRange].
1084+
func randInRange(rng *rand.Rand, minRange, maxRange int) int {
1085+
if minRange > maxRange {
1086+
panic("minRange must be <= maxRange")
1087+
}
1088+
return minRange + rng.IntN(maxRange-minRange+1)
1089+
}
1090+
1091+
// randPowerOf2 returns a power of 2 in the range [2^minExp,2^maxExp].
1092+
func randPowerOf2(rng *rand.Rand, minExp, maxExp int) uint64 {
1093+
if minExp < 0 {
1094+
panic("exponents must be non-negative")
1095+
}
1096+
if minExp > maxExp {
1097+
panic("minExp must be <= maxExp")
1098+
}
1099+
return 1 << randInRange(rng, minExp, maxExp)
1100+
}

0 commit comments

Comments
 (0)