From 4b750a6940fc132f56e1ee8c77c056bb241f826a Mon Sep 17 00:00:00 2001 From: Bilal Akhtar Date: Wed, 17 Jan 2024 17:17:19 -0500 Subject: [PATCH] db: fix sstable bound inversion in ingestSynthesizeShared Previously, for shared file ingestions, we could have seqnum substitution result in invalid bounds (eg. smallest > largest). This change detects this case as an artifact of seqnum substitution and addresses it. Fixes #3225. --- ingest.go | 8 ++++++ testdata/ingest_shared | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/ingest.go b/ingest.go index 957dc5454f..98ef20a47e 100644 --- a/ingest.go +++ b/ingest.go @@ -147,6 +147,14 @@ func ingestSynthesizeShared( if sm.LargestPointKey.IsExclusiveSentinel() { largestPointKey = base.MakeRangeDeleteSentinelKey(sm.LargestPointKey.UserKey) } + if opts.Comparer.Equal(smallestPointKey.UserKey, largestPointKey.UserKey) && + smallestPointKey.Trailer < largestPointKey.Trailer { + // We get kinds from the sender, however we substitute our own sequence + // numbers. This can result in cases where an sstable [b#5,SET-b#4,DELSIZED] + // becomes [b#0,SET-b#0,DELSIZED] when we synthesize it here, but the + // kinds need to be reversed now because DelSized > Set. + smallestPointKey, largestPointKey = largestPointKey, smallestPointKey + } meta.ExtendPointKeyBounds(opts.Comparer.Compare, smallestPointKey, largestPointKey) } if err := meta.Validate(opts.Comparer.Compare, opts.Comparer.FormatKey); err != nil { diff --git a/testdata/ingest_shared b/testdata/ingest_shared index 2c60c29aca..b7f659eef0 100644 --- a/testdata/ingest_shared +++ b/testdata/ingest_shared @@ -1240,3 +1240,60 @@ c: (., [c-cc) @7=foo UPDATED) dd: (., [dd-e) @7=foo UPDATED) . . + +# Regression test for https://github.com/cockroachdb/pebble/issues/3225 +# Handle cases where an sstable starts and ends at the same user key but different +# internal key, and the ordering of internal key kinds changes due to seqnum +# substitution. + +reset +---- + +switch 1 +---- +ok + +batch +set a foo +---- + +file-only-snapshot s11 + a z +---- +ok + +batch +del a +---- + +flush +---- + +compact a-z +---- +ok + +lsm +---- +6: + 000005:[a#11,DEL-a#10,SET] + +replicate 1 2 a z +---- +replicated 1 shared SSTs + +switch 2 +---- +ok + +lsm +---- +6: + 000005:[a#10,SET-a#10,DEL] + +iter +first +next +---- +. +.