Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sstable: order range keys consistently in colblk and rowblk encodings #4160

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

jbowens
Copy link
Collaborator

@jbowens jbowens commented Nov 12, 2024

Previously the ordering of range keys within a range key block was a bit muddled and undefined. Before encoding a Span of range keys to the underlying RawWriter, the sstable.Writer type previously sorted the span's keys by suffix. However, the row-based sstable writer always serializes RANGEKEYSETs of a Span first, followed by RANGEKEYUNSETs and then RANGEKEYDELs.

Confusingly, the rowblk fragment iterator also sorted keys by trailer when iterating backwards, but not forwards. The columnar RawWriter preserved the order of keys in the span passed to EncodeSpan, and this order was preserved during iteration.

This commit adapts the sstable.Writer type to sort a range key Span's keys by trailer and then suffix before encoding. This provides determinism and matches the ordering of keys produced by compactions which sort the keys by trailer. Additionally, the rowblk fragment iterator is updated to always sort the returned keys by trailer.

@jbowens jbowens requested a review from a team as a code owner November 12, 2024 20:46
@jbowens jbowens requested a review from itsbilal November 12, 2024 20:46
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator Author

@jbowens jbowens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Motivated by test failures on cockroachdb/cockroach#134671 where iterating over a sstable's range keys directly results in nondeterministic ordering dependent on whether columnar blocks are enabled.

Reviewable status: 0 of 4 files reviewed, all discussions resolved (waiting on @itsbilal)

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: 0 of 4 files reviewed, 1 unresolved discussion (waiting on @itsbilal and @jbowens)


internal/keyspan/span.go line 478 at r1 (raw file):

	slices.SortFunc(keys, func(a, b Key) int {
		// Trailer are ordered in decreasing number order.
		if v := -cmp.Compare(a.Trailer, b.Trailer); v != 0 {

[nit] can be cmp.Compare(b.Trailer, a.Trailer)

Previously the ordering of range keys within a range key block was a bit
muddled and undefined. Before encoding a Span of range keys to the underlying
RawWriter, the sstable.Writer type previously sorted the span's keys by suffix.
However, the row-based sstable writer always serializes RANGEKEYSETs of a Span
first, followed by RANGEKEYUNSETs and then RANGEKEYDELs.

Confusingly, the rowblk fragment iterator also sorted keys by trailer when
iterating backwards, but not forwards. The columnar RawWriter preserved the
order of keys in the span passed to EncodeSpan, and this order was preserved
during iteration.

This commit adapts the sstable.Writer type to sort a range key Span's keys by
trailer and then suffix before encoding. This provides determinism and matches
the ordering of keys produced by compactions which sort the keys by trailer.
Additionally, the rowblk fragment iterator is updated to always sort the
returned keys by trailer.
Copy link
Collaborator Author

@jbowens jbowens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTR!

Reviewable status: 0 of 4 files reviewed, all discussions resolved (waiting on @itsbilal)


internal/keyspan/span.go line 478 at r1 (raw file):

Previously, RaduBerinde wrote…

[nit] can be cmp.Compare(b.Trailer, a.Trailer)

Done

@jbowens jbowens merged commit 8e01ef3 into cockroachdb:master Nov 12, 2024
22 of 23 checks passed
@jbowens jbowens deleted the rangekey-ordering branch November 12, 2024 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants