Skip to content

Commit bd64861

Browse files
committed
metamorphic: add random number to external object names
External object names can collide with existing objects when starting with an initial state. This change adds a random unique number to the filenames.
1 parent 07c9d31 commit bd64861

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

metamorphic/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ func openExternalObj(
258258
rangeDelIter keyspan.FragmentIterator,
259259
rangeKeyIter keyspan.FragmentIterator,
260260
) {
261-
objReader, objSize, err := t.externalStorage.ReadObject(context.Background(), externalObjName(externalObjID))
261+
objMeta := t.getExternalObj(externalObjID)
262+
objReader, objSize, err := t.externalStorage.ReadObject(context.Background(), objMeta.objName)
262263
panicIfErr(err)
263264
opts := t.opts.MakeReaderOptions()
264265
reader, err = sstable.NewReader(

metamorphic/ops.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ package metamorphic
77
import (
88
"bytes"
99
"context"
10-
"crypto/rand"
10+
cryptorand "crypto/rand"
1111
"encoding/binary"
1212
"fmt"
1313
"io"
14+
"math/rand/v2"
1415
"path"
1516
"path/filepath"
1617
"slices"
@@ -1053,7 +1054,7 @@ func (o *ingestExternalFilesOp) run(t *Test, h historyRecorder) {
10531054
meta := t.getExternalObj(obj.externalObjID)
10541055
external[i] = pebble.ExternalFile{
10551056
Locator: "external",
1056-
ObjName: externalObjName(obj.externalObjID),
1057+
ObjName: meta.objName,
10571058
Size: meta.sstMeta.Size,
10581059
StartKey: obj.bounds.Start,
10591060
EndKey: obj.bounds.End,
@@ -1199,8 +1200,8 @@ func (o *newIterOp) run(t *Test, h historyRecorder) {
11991200
// Trash the bounds to ensure that Pebble doesn't rely on the stability of
12001201
// the user-provided bounds.
12011202
if opts != nil {
1202-
rand.Read(opts.LowerBound[:])
1203-
rand.Read(opts.UpperBound[:])
1203+
cryptorand.Read(opts.LowerBound[:])
1204+
cryptorand.Read(opts.UpperBound[:])
12041205
}
12051206
h.Recordf("%s // %v", o.formattedString(t.testOpts.KeyFormat), i.Error())
12061207
}
@@ -1318,8 +1319,8 @@ func (o *iterSetBoundsOp) run(t *Test, h historyRecorder) {
13181319

13191320
// Trash the bounds to ensure that Pebble doesn't rely on the stability of
13201321
// the user-provided bounds.
1321-
rand.Read(lower[:])
1322-
rand.Read(upper[:])
1322+
cryptorand.Read(lower[:])
1323+
cryptorand.Read(upper[:])
13231324

13241325
h.Recordf("%s // %v", o.formattedString(t.testOpts.KeyFormat), i.Error())
13251326
}
@@ -1363,8 +1364,8 @@ func (o *iterSetOptionsOp) run(t *Test, h historyRecorder) {
13631364

13641365
// Trash the bounds to ensure that Pebble doesn't rely on the stability of
13651366
// the user-provided bounds.
1366-
rand.Read(opts.LowerBound[:])
1367-
rand.Read(opts.UpperBound[:])
1367+
cryptorand.Read(opts.LowerBound[:])
1368+
cryptorand.Read(opts.UpperBound[:])
13681369

13691370
h.Recordf("%s // %v", o.formattedString(t.testOpts.KeyFormat), i.Error())
13701371
}
@@ -1872,18 +1873,20 @@ type newExternalObjOp struct {
18721873
externalObjID objID
18731874
}
18741875

1875-
func externalObjName(externalObjID objID) string {
1876-
if externalObjID.tag() != externalObjTag {
1877-
panic(fmt.Sprintf("invalid externalObjID %s", externalObjID))
1878-
}
1879-
return fmt.Sprintf("external-for-ingest-%d.sst", externalObjID.slot())
1880-
}
1881-
18821876
func (o *newExternalObjOp) run(t *Test, h historyRecorder) {
18831877
b := t.getBatch(o.batchID)
18841878
t.clearObj(o.batchID)
18851879

1886-
writeCloser, err := t.externalStorage.CreateObject(externalObjName(o.externalObjID))
1880+
if o.externalObjID.tag() != externalObjTag {
1881+
panic(fmt.Sprintf("invalid externalObjID %s", o.externalObjID))
1882+
}
1883+
// We add a unique number to the object name to avoid collisions with existing
1884+
// external objects (when using an initial starting state).
1885+
//
1886+
// Note that the number is not based on the seed, in case we run using the
1887+
// same seed that was used in a previous run with the same store.
1888+
objName := fmt.Sprintf("external-for-ingest-%d-%d.sst", o.externalObjID.slot(), rand.Uint64())
1889+
writeCloser, err := t.externalStorage.CreateObject(objName)
18871890
if err != nil {
18881891
panic(err)
18891892
}
@@ -1908,6 +1911,7 @@ func (o *newExternalObjOp) run(t *Test, h historyRecorder) {
19081911
panic("metamorphic test internal error: external object empty")
19091912
}
19101913
t.setExternalObj(o.externalObjID, externalObjMeta{
1914+
objName: objName,
19111915
sstMeta: sstMeta,
19121916
})
19131917
h.Recordf("%s", o.formattedString(t.testOpts.KeyFormat))

metamorphic/test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type Test struct {
7272

7373
type externalObjMeta struct {
7474
sstMeta *sstable.WriterMetadata
75+
objName string
7576
}
7677

7778
func newTest(ops []op) *Test {

0 commit comments

Comments
 (0)