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

backupccl: create stripped crdb_internal.fingerprint overload #98759

Merged
merged 2 commits into from
Mar 25, 2023

Conversation

msbutler
Copy link
Collaborator

This patch adds a variant of crdb_internal.fingerprint(), which creates a "stripped" fingerprint of the target span. Namely, crdb_internal.fingerping(span,true) will return a fingerprint that is agnostic to the mvcc timestamps and the index prefix of the key, and considers only the latest mvcc history of the key span.

For example, suppose the user fingerprinted a table at some system time, then backed up and restored it to that same system time. The restored table should have the same fingerprint!

This fingerprint variant is signicantly more scalable than SHOW EXPERIMENTAL FINGERPRINT, as it uses export requests compared to a simple scan.

Fixes #98570

Release note: None

@msbutler msbutler self-assigned this Mar 16, 2023
@cockroach-teamcity
Copy link
Member

This change is Reviewable

pkg/sql/sem/builtins/builtins.go Outdated Show resolved Hide resolved
Comment on lines 57 to 60
_, tenantPrefix, err := keys.DecodeTenantPrefix(startKey)
if err != nil {
return fingerprintWriter{}, err
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe we are decoding this so that we can use StripTenantPrefix below which asserts that the prefix is the same. But what if I want to use this from the system tenant on a span that crosses tenant boundaries. Are we saying that isn't a valid use case. If so, we should include some reasoning for why not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah, this is a good point. Though I wonder if using a codec's StipTenantPrefix is more performant than DecodeTenantPrefix. I can run some benchmarks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

there's no noticeable perf difference between StripTenantPrefix vs DecodeTenantPrefix, according to a macro benchmark (fingerpinting a 15 gb table). So I suppose I shouldn't add this restriction to limit the cmd to a single tenant key span -- i'll need to create a slightly more complicated helper to strip the index prefix then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a bunch key stripping utilities. If you like this approach, I'll some unit tests to the first commit and rebase.

@msbutler msbutler force-pushed the butler-finger-print-logic branch 3 times, most recently from 808d613 to 5fc09b4 Compare March 23, 2023 11:44
@msbutler
Copy link
Collaborator Author

unrelated CI flake. RFAL!

@msbutler msbutler marked this pull request as ready for review March 23, 2023 14:17
@msbutler msbutler requested review from a team as code owners March 23, 2023 14:17
@msbutler msbutler requested review from a team, msirek, RaduBerinde, rhu713 and adityamaru and removed request for a team, rhu713, msirek and RaduBerinde March 23, 2023 14:17
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

I have some drive-by nits, hope you don't mind 😄

Reviewed 13 of 13 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @msbutler, and @stevendanna)


-- commits line 2 at r2:
nit: missing Release note: None.


pkg/keys/sql.go line 70 at r2 (raw file):

func StripTenantPrefixE(key roachpb.Key) ([]byte, error) {
	var err error
	// Strip the tenant prefix

nit: missing period.


pkg/sql/sem/builtins/builtins.go line 7500 at r3 (raw file):

			ReturnType: tree.FixedReturnType(types.Int),
			Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
				skipTimestamp := *args[1].(*tree.DBool)

nit: it would be good to add an explicit length check on args to be 2 here (and 3 in the other overload) as well as to check the types of each arg (i.e. return an error if args[1] is not a DBool).


pkg/sql/sem/builtins/builtins.go line 7501 at r3 (raw file):

			Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
				skipTimestamp := *args[1].(*tree.DBool)
				return fingerprint(ctx, evalCtx, args, hlc.Timestamp{}, false, bool(skipTimestamp))

nit: add inlined comment for what false stands here for.


pkg/sql/sem/builtins/builtins.go line 7520 at r3 (raw file):

				startTimestamp := hlc.Timestamp{WallTime: startTime.UnixNano()}
				allRevisions := *args[2].(*tree.DBool)
				return fingerprint(ctx, evalCtx, args, startTimestamp, bool(allRevisions), false)

nit: ditto for inlined comment for false.


pkg/sql/sem/builtins/fingerprint_builtins.go line 37 at r3 (raw file):

	ctx context.Context,
	evalCtx *eval.Context,
	args tree.Datums,

nit: since we only use args[0] and we assume it's a DArray, should we just take in DArray explicitly here?

Copy link
Collaborator

@stevendanna stevendanna left a comment

Choose a reason for hiding this comment

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

Overall I think will be useful. I've left some nitpicks.

On the various Strip functions: I could go either way. They introduce a bit of duplication, but it is nice to be able to operate on a roachpb.Key without specific codec. If you like them, I'd say keep them.

pkg/storage/mvcc.go Outdated Show resolved Hide resolved
pkg/keys/sql.go Outdated Show resolved Hide resolved
pkg/storage/fingerprint_writer.go Outdated Show resolved Hide resolved


run ok
put k=a ts=2 v=a2
Copy link
Collaborator

Choose a reason for hiding this comment

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

NB: If you want to test the table prefix stripping as well, if you do something like put k=/b ts=2 v=b tenant-prefix=10 init-checksum you'll get a key that looks like a table key.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought about this, but I think I've got plenty of coverage for the index prefix stripping stuff in other tests. This unit test specifically exercises the timestamp stripping.

@msbutler msbutler force-pushed the butler-finger-print-logic branch from 5fc09b4 to 45643a7 Compare March 24, 2023 17:20
Copy link
Collaborator Author

@msbutler msbutler left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @stevendanna, and @yuzefovich)


pkg/sql/sem/builtins/builtins.go line 7500 at r3 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: it would be good to add an explicit length check on args to be 2 here (and 3 in the other overload) as well as to check the types of each arg (i.e. return an error if args[1] is not a DBool).

done.


pkg/sql/sem/builtins/builtins.go line 7501 at r3 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: add inlined comment for what false stands here for.

done


pkg/sql/sem/builtins/builtins.go line 7520 at r3 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: ditto for inlined comment for false.

done


pkg/sql/sem/builtins/fingerprint_builtins.go line 37 at r3 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: since we only use args[0] and we assume it's a DArray, should we just take in DArray explicitly here?

i refactored this so we parse the span outside fingerprint().

Copy link
Collaborator Author

@msbutler msbutler left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @stevendanna, and @yuzefovich)


-- commits line 2 at r2:

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: missing Release note: None.

will plan to clean up the commit message once I squash everything!

@msbutler msbutler added the backport-23.1.x Flags PRs that need to be backported to 23.1 label Mar 24, 2023
The codec normally used to decode and encode crdb keys requires a tenant prefix
upon creation. For callers that want to manipulate keys across tenants, the per
tenant codec provides a poor experience. This patch provides a few helper
functions to strip key prefixes without a codec.

Informs cockroachdb#98570
Release note: None
This patch adds a variant of crdb_internal.fingerprint(), which creates a
"stripped" fingerprint of the target span. Namely,
`crdb_internal.fingerpint(span,true)` will return a fingerprint that is
agnostic to the mvcc timestamps and the index prefix of the key, and considers
only the latest mvcc history of the key span. This cmd should only get used
over user table space, i.e. on keys with an index prefix.

For example, suppose the user fingerprinted a table at some system time, then
backed up and restored it to that same system time. The restored table should
have the same fingerprint!

crdb_internal.fingerpint(span,false) is equivalent to
crdb_internal.fingerpint(span,NULL,LATEST).

This fingerprint variant is signicantly more scalable than SHOW EXPERIMENTAL
FINGERPRINT, as it uses export requests compared to a simple scan.

Fixes cockroachdb#98570

Release note: None
@msbutler msbutler force-pushed the butler-finger-print-logic branch from 45643a7 to cf91202 Compare March 24, 2023 21:01
@msbutler
Copy link
Collaborator Author

TFTRs!!!

bors r=stevendanna

@craig
Copy link
Contributor

craig bot commented Mar 25, 2023

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

backupccl: add ignoreTimestamps flag to crdb_internal.fingerprint
4 participants