Skip to content

internal/keyspan: replace calls to Span.Visible#1747

Merged
jbowens merged 1 commit intocockroachdb:masterfrom
jbowens:keyspan-visibility
Jun 9, 2022
Merged

internal/keyspan: replace calls to Span.Visible#1747
jbowens merged 1 commit intocockroachdb:masterfrom
jbowens:keyspan-visibility

Conversation

@jbowens
Copy link
Copy Markdown
Contributor

@jbowens jbowens commented Jun 8, 2022

The Span.Visible method returns a Span with the subset of keys visible at the
provided sequence number. Batch sequence numbers may force this method to
allocate under some circumstances, so it's preferrable to use narrower
methods that do not need to allocate.

Add two such narrower methods, VisibleAt and CoversAt, and use them to remove
calls to Span.Visible for range deletions during point iteration.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@jbowens jbowens requested a review from a team June 8, 2022 22:00
Copy link
Copy Markdown
Contributor

@nicktrav nicktrav left a comment

Choose a reason for hiding this comment

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

Nice. :lgtm: - just some documentation nits.

Reviewed 11 of 11 files at r1, all commit messages.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @jbowens)


-- commits line 5 at r1:

Batch sequence numbers may force this method to allocate under some circumstances

I didn't quite follow where / how this allocation was happening in the batch case. Can you elaborate for my own benefit?


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

	} else {
		// Otherwise we check the last key. Since keys are ordered decreasing in
		// sequence number, the last key has the lowest sequence number. If any

I read the last sentence a couple times before realizing that what I was kind of expecting was the contrapositive tacked on the end. Something like: "Or put differently: if the last key is not visible, then no key is visible".

I'm just proving this to myself, so feel free to ignore if you think it's clear.


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

}

// CoversAt returns true if the span contains a key that is visible at the

nit: Possible to add a clarifying parenthetical for "visibility" along the lines of: "(i.e. the key's sequence number is strictly less than the snapshot sequence number)"?

.. or maybe I just need to burn that concept into my brain (> for snapshot visibility rather than >=).


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

	for i := range s.Keys {
		if kseq := s.Keys[i].SeqNum(); kseq&base.InternalKeySeqNumBatch != 0 {
			// Only visible batch keys are included when an Iterator's batch spans

What do you think about mentioning the behavior for batches the docstring too? Same for VisibleAt.


internal/keyspan/testdata/visible_at line 21 at r1 (raw file):

1 : false

define

nit: Reading this file in isolation, I'm unlikely to recognize the relevance of the seemingly arbitrary sequence numbers. Worth an additional assertion / "proof" in here that the seqnums are from a batch, and therefore snapshot sequnum is irrelevant?

The Span.Visible method returns a Span with the subset of keys visible at the
provided sequence number. Batch sequence numbers may force this method to
allocate under some circumstances, so it's preferrable to use narrower
methods that do not need to allocate.

Add two such narrower methods, VisibleAt and CoversAt, and use them to remove
calls to Span.Visible for range deletions during point iteration.
@jbowens jbowens force-pushed the keyspan-visibility branch from c1c9580 to e3c9106 Compare June 9, 2022 14:58
Copy link
Copy Markdown
Contributor 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: 9 of 11 files reviewed, 3 unresolved discussions (waiting on @nicktrav)


-- commits line 5 at r1:

Previously, nicktrav (Nick Travers) wrote…

Batch sequence numbers may force this method to allocate under some circumstances

I didn't quite follow where / how this allocation was happening in the batch case. Can you elaborate for my own benefit?

Here's where that allocation happens:

// This is the problematic sandwich case. Allocate a new slice, copying
// the batch keys and the visible keys into it.
//
// [b b b] h h h [v v v]
ret.Keys = make([]Key, (lastBatchIdx+1)+(len(s.Keys)-lastNonVisibleIdx-1))
copy(ret.Keys, s.Keys[:lastBatchIdx+1])
copy(ret.Keys[lastBatchIdx+1:], s.Keys[lastNonVisibleIdx+1:])

There's more in depth discussion about why this allocation is necessary higher up in Visible:

// Keys from indexed batches may force an allocation. The Keys slice is
// ordered by sequence number, so ordinarily we can return the trailing
// subslice containing keys with sequence numbers less than `seqNum`.
//
// However, batch keys are special. Only visible batch keys are included
// when an Iterator's batch spans are fragmented. They must always be
// visible.
//
// Batch keys can create a sandwich of visible batch keys at the beginning
// of the slice and visible committed keys at the end of the slice, forcing
// us to allocate a new slice and copy the contents.
//
// Care is taking to only incur an allocation only when batch keys and
// visible keys actually sandwich non-visible keys.


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

Previously, nicktrav (Nick Travers) wrote…

nit: Possible to add a clarifying parenthetical for "visibility" along the lines of: "(i.e. the key's sequence number is strictly less than the snapshot sequence number)"?

.. or maybe I just need to burn that concept into my brain (> for snapshot visibility rather than >=).

I'll add a comment to the body of the function. I think ideally clients don't need to worry about the numerical relationship (eg, > vs >=), and rely on utilities like this that abstract the concept of visibility.


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

Previously, nicktrav (Nick Travers) wrote…

What do you think about mentioning the behavior for batches the docstring too? Same for VisibleAt.

done


internal/keyspan/testdata/visible_at line 21 at r1 (raw file):

Previously, nicktrav (Nick Travers) wrote…

nit: Reading this file in isolation, I'm unlikely to recognize the relevance of the seemingly arbitrary sequence numbers. Worth an additional assertion / "proof" in here that the seqnums are from a batch, and therefore snapshot sequnum is irrelevant?

added a comment documenting these as batch sequence numbers

@jbowens jbowens merged commit 86dd6fd into cockroachdb:master Jun 9, 2022
@jbowens jbowens deleted the keyspan-visibility branch June 9, 2022 15:12
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