Skip to content

Commit

Permalink
metamorphic: add CockroachKeyFormat
Browse files Browse the repository at this point in the history
Add a new KeyFormat that uses the cockroach encoding of KV pairs in the
metamorphic test, expanding test coverage to cover the cockroachkvs package.

Fixes cockroachdb#4167.
  • Loading branch information
jbowens committed Jan 30, 2025
1 parent 310fac7 commit e5aa184
Show file tree
Hide file tree
Showing 6 changed files with 554 additions and 10 deletions.
25 changes: 25 additions & 0 deletions cockroachkvs/cockroachkvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ func EncodeTimestamp(key []byte, walltime uint64, logical uint32) []byte {
return key
}

// NewTimestampSuffix allocates and encodes a new suffix byte slice encoding the
// provided timestamp.
func NewTimestampSuffix(wallTime uint64, logical uint32) []byte {
switch {
case wallTime == 0 && logical == 0:
return nil
case logical == 0:
v := make([]byte, suffixLenWithWall)
binary.BigEndian.PutUint64(v, wallTime)
v[suffixLenWithWall-1] = suffixLenWithWall
return v
default:
v := make([]byte, suffixLenWithLogical)
binary.BigEndian.PutUint64(v, wallTime)
binary.BigEndian.PutUint32(v[8:], logical)
v[suffixLenWithLogical-1] = suffixLenWithLogical
return v
}
}

// DecodeMVCCTimestampSuffix decodes an MVCC timestamp from its Pebble representation,
// including the length suffix.
func DecodeMVCCTimestampSuffix(encodedTS []byte) (wallTime uint64, logical uint32, err error) {
Expand Down Expand Up @@ -1059,6 +1079,11 @@ func (fk formatKey) Format(f fmt.State, c rune) {
formatKeySuffix(fk[i:]).Format(f, c)
}

// FormatKeySuffix returns a formatter for the user key suffix.
func FormatKeySuffix(suffix []byte) fmt.Formatter {
return formatKeySuffix(suffix)
}

type formatKeySuffix []byte

func (fk formatKeySuffix) Format(f fmt.State, c rune) {
Expand Down
27 changes: 18 additions & 9 deletions internal/metamorphic/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,28 @@ var runOnceFlags, runFlags = metaflags.InitAllFlags()
// be customized via the --inner-binary flag (used for code coverage
// instrumentation).
func TestMeta(t *testing.T) {
runTestMeta(t, false /* multiInstance */)
runTestMeta(t)
}

func TestMetaTwoInstance(t *testing.T) {
runTestMeta(t, true /* multiInstance */)
runTestMeta(t, metamorphic.MultiInstance(2))
}

func runTestMeta(t *testing.T, multiInstance bool) {
func TestMetaCockroachKVs(t *testing.T) {
runTestMeta(t, metamorphic.CockroachKeyFormat)
}

type option interface {
metamorphic.RunOnceOption
metamorphic.RunOption
}

func runTestMeta(t *testing.T, addtlOptions ...option) {
switch {
case runOnceFlags.Compare != "":
onceOpts := runOnceFlags.MakeRunOnceOptions()
if multiInstance {
onceOpts = append(onceOpts, metamorphic.MultiInstance(2))
for _, opt := range addtlOptions {
onceOpts = append(onceOpts, opt)
}
testRootDir, runSubdirs := runOnceFlags.ParseCompare()
if runOnceFlags.TryToReduce {
Expand All @@ -78,8 +87,8 @@ func runTestMeta(t *testing.T, multiInstance bool) {
// runOptions() below) or the user specified it manually in order to re-run
// a test.
onceOpts := runOnceFlags.MakeRunOnceOptions()
if multiInstance {
onceOpts = append(onceOpts, metamorphic.MultiInstance(2))
for _, opt := range addtlOptions {
onceOpts = append(onceOpts, opt)
}
if runOnceFlags.TryToReduce {
tryToReduce(t, runOnceFlags.Dir, runOnceFlags.RunDir, runOnceFlags.ReduceAttempts)
Expand All @@ -90,8 +99,8 @@ func runTestMeta(t *testing.T, multiInstance bool) {

default:
opts := runFlags.MakeRunOptions()
if multiInstance {
opts = append(opts, metamorphic.MultiInstance(2))
for _, opt := range addtlOptions {
opts = append(opts, opt)
}
metamorphic.RunAndCompare(t, runFlags.Dir, opts...)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/metamorphic/metaflags/meta_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func (c *CommonFlags) KeyFormat() metamorphic.KeyFormat {

// KeyFormats is a map of available key formats.
var KeyFormats = map[string]metamorphic.KeyFormat{
"testkeys": metamorphic.TestkeysKeyFormat,
"testkeys": metamorphic.TestkeysKeyFormat,
"cockroachkvs": metamorphic.CockroachKeyFormat,
}

func initCommonFlags() *CommonFlags {
Expand Down
Loading

0 comments on commit e5aa184

Please sign in to comment.