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

kv: include locking strength and durability in Get/Scan/RevScan SafeFormat #114481

Merged
merged 2 commits into from
Dec 12, 2023

Conversation

lyang24
Copy link
Contributor

@lyang24 lyang24 commented Nov 15, 2023

kv: include locking strength and durability in Get/Scan/RevScan SafeFormat

The goal of this pr is improving observability. We are adding lock strength
and lock durability with Get/Scan/RevScan request if the request is locking.
This is implemented by introducing an optional extension interface to Request
Interface called SafeFormatterRequest. We are also refactoring inside
BatchRequest.SafeFormat with the added interface. Please note the subtle
changed introduced here: if the EndKey is not present we print only Key with
square brackets, and this applies to all the request types.

Fixes: #114475
Release note: None.

Copy link

blathers-crl bot commented Nov 15, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

I have added a few people who may be able to assist in reviewing:

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Nov 15, 2023
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch from 36fd0ca to 66ef71b Compare November 15, 2023 18:18
Copy link

blathers-crl bot commented Nov 15, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch 2 times, most recently from 17a7d25 to 14fb0cb Compare November 15, 2023 19:39
Copy link

blathers-crl bot commented Nov 15, 2023

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch from 14fb0cb to 01618a8 Compare November 17, 2023 05:33
@lyang24 lyang24 marked this pull request as ready for review November 17, 2023 06:45
@lyang24 lyang24 requested a review from a team as a code owner November 17, 2023 06:45
Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r1, 2 of 3 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @lyang24)


-- commits line 2 at r3:
Same two comments about the commit message as in #114636.


pkg/kv/kvpb/batch.go line 813 at r3 (raw file):

		req := arg.GetInner()
		if et, ok := req.(*EndTxnRequest); ok {

I was thinking that this new SafeFormatterRequest interface could be used for all of these special cases. So instead, we'd just have some logic that looked like

if safeFormatterReq, ok := req.(SafeFormatterRequest); ok {
    safeFormatterReq.SafeFormat(s, verb)
} else {
    s.Print(req.Method())
}
if len(h.EndKey) > 0 {
    s.Printf(" [%s,%s)", h.Key, h.EndKey)
} else {
    s.Printf(" [%s]", h.Key)
}

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch 2 times, most recently from 3e75f82 to 46c1d25 Compare November 23, 2023 00:31
@lyang24 lyang24 requested a review from a team as a code owner November 23, 2023 05:19
Copy link

blathers-crl bot commented Nov 23, 2023

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch 2 times, most recently from 51654b9 to 20e256c Compare November 23, 2023 07:03
Copy link
Contributor Author

@lyang24 lyang24 left a comment

Choose a reason for hiding this comment

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

Thanks for the feedback, i have addressed the comments. Please note the subtle change that we are applying this logic at the end of request types now.

if len(h.EndKey) > 0 {
	s.Printf(" [%s,%s)", h.Key, h.EndKey)
} else {
	s.Printf(" [%s]", h.Key)

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @nvanbenschoten)


pkg/kv/kvpb/batch.go line 813 at r3 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

I was thinking that this new SafeFormatterRequest interface could be used for all of these special cases. So instead, we'd just have some logic that looked like

if safeFormatterReq, ok := req.(SafeFormatterRequest); ok {
    safeFormatterReq.SafeFormat(s, verb)
} else {
    s.Print(req.Method())
}
if len(h.EndKey) > 0 {
    s.Printf(" [%s,%s)", h.Key, h.EndKey)
} else {
    s.Printf(" [%s]", h.Key)
}

Thanks it is so much cleaner after the refactor.

Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

Reviewed 6 of 6 files at r4, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @lyang24)


pkg/kv/kvpb/api.go line 197 at r4 (raw file):

}

// SafeFormatterRequest is an optional extension interface used to allow request to do custom formatting.

It would be nice to add some request-specific tests for these requests which are implementing the SafeFormatterRequest interface. We could add those small unit tests in pkg/kv/kvpb/string_test.go, right below TestBatchRequestString.

When doing so, we can test the safe-string and redacted-string forms. TestReplicaStringAndSafeFormat is an example of how to do that.


pkg/kv/kvpb/api.go line 206 at r4 (raw file):

// SafeFormat implements the redact.SafeFormatter interface.
func (gr GetRequest) SafeFormat(s redact.SafePrinter, _ rune) {

These methods should all use pointer receivers.


pkg/kv/kvpb/api.go line 211 at r4 (raw file):

		return
	}
	s.Printf("[lockStrength=%s,lockDurability=%s]", gr.KeyLockingStrength, gr.KeyLockingDurability)

Here and below: we can make this less verbose. How about something like:

s.Printf("(%s,%s)", gr.KeyLockingStrength, gr.KeyLockingDurability)

pkg/kv/kvpb/batch.go line 813 at r3 (raw file):

Previously, lyang24 (Lanqing Yang) wrote…

Thanks it is so much cleaner after the refactor.

Agreed, this did turn out nicely!

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch from 20e256c to f6c57c6 Compare December 2, 2023 00:00
Copy link
Contributor Author

@lyang24 lyang24 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 @nvanbenschoten)


pkg/kv/kvpb/api.go line 197 at r4 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

It would be nice to add some request-specific tests for these requests which are implementing the SafeFormatterRequest interface. We could add those small unit tests in pkg/kv/kvpb/string_test.go, right below TestBatchRequestString.

When doing so, we can test the safe-string and redacted-string forms. TestReplicaStringAndSafeFormat is an example of how to do that.

yep added the TestRequestSafeFormat the keys are printed in the batch request SafeFormat method so it wont display here just fyi.


pkg/kv/kvpb/api.go line 206 at r4 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

These methods should all use pointer receivers.

great catch!

Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 3 files at r5, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @lyang24)


pkg/kv/kvpb/string_test.go line 103 at r5 (raw file):

// Unit test the requests that implemented SafeFormatterRequest interface.
func TestRequestSafeFormat(t *testing.T) {
	gr := &kvpb.GetRequest{

Can we turn this into a table-driven test to make it easier to add new request types? Something like:

testCases := []struct {
	req        Request
	redactable string
	redacted   string
}{
	{
		req: &kvpb.GetRequest{
			RequestHeader: kvpb.RequestHeader{
				Key: roachpb.Key("a"),
			},
			KeyLockingStrength:   lock.Shared,
			KeyLockingDurability: lock.Unreplicated,
		},
		redactable: "Get(Shared,Unreplicated)",	
		redacted: "Get(Shared,Unreplicated)",	
	},
	// ...
}
for _, c := range testCases {
	t.Run(c.Method(), func(t *testing.T) {
		// ...
	})
}

pkg/kv/kvpb/string_test.go line 170 at r5 (raw file):

		PusheeTxn: pusheeTxn.TxnMeta,
	}
	require.EqualValues(t, "PushTxn(‹PUSH_TIMESTAMP›,00fbff58->00fbff59)", redact.Sprint(ptr))

The redaction of PUSH_TIMESTAMP is interesting here.

We should add a new commit which makes PushTxnType implement redact.SafeValue.

To do that, we'll want a line like the following to api.go:

// SafeValue implements the redact.SafeValue interface.
func (PushTxnType) SafeValue() {}

And then we'll have to add an entry to pkg/testutils/lint/passes/redactcheck/redactcheck.go.

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch from f6c57c6 to 12f06f8 Compare December 5, 2023 23:26
Copy link

blathers-crl bot commented Dec 5, 2023

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • When CI has completed, please ensure no errors have appeared.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Copy link
Contributor Author

@lyang24 lyang24 left a comment

Choose a reason for hiding this comment

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

Thank you for reviewing.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @nvanbenschoten)


pkg/kv/kvpb/string_test.go line 103 at r5 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

Can we turn this into a table-driven test to make it easier to add new request types? Something like:

testCases := []struct {
	req        Request
	redactable string
	redacted   string
}{
	{
		req: &kvpb.GetRequest{
			RequestHeader: kvpb.RequestHeader{
				Key: roachpb.Key("a"),
			},
			KeyLockingStrength:   lock.Shared,
			KeyLockingDurability: lock.Unreplicated,
		},
		redactable: "Get(Shared,Unreplicated)",	
		redacted: "Get(Shared,Unreplicated)",	
	},
	// ...
}
for _, c := range testCases {
	t.Run(c.Method(), func(t *testing.T) {
		// ...
	})
}

Thank you this is a nice pattern looking forward to incorporate in my future developments.


pkg/kv/kvpb/string_test.go line 170 at r5 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

The redaction of PUSH_TIMESTAMP is interesting here.

We should add a new commit which makes PushTxnType implement redact.SafeValue.

To do that, we'll want a line like the following to api.go:

// SafeValue implements the redact.SafeValue interface.
func (PushTxnType) SafeValue() {}

And then we'll have to add an entry to pkg/testutils/lint/passes/redactcheck/redactcheck.go.

Thank you for catching this. I fixed it.

Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

:lgtm:

bors r=nvanbenschoten

Reviewed 1 of 1 files at r6, 3 of 3 files at r7, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @lyang24)

@craig
Copy link
Contributor

craig bot commented Dec 7, 2023

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Dec 7, 2023

Build failed:

@nvanbenschoten
Copy link
Member

bors r-

@nvanbenschoten
Copy link
Member

@lyang24 this PR skewed with e53cc10. Could you rebase it on master?

@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch 2 times, most recently from 8a8a671 to c1cb237 Compare December 8, 2023 08:06
@lyang24
Copy link
Contributor Author

lyang24 commented Dec 8, 2023

Hi Nathan, This pr is rebased on master now.

Eric.Yang and others added 2 commits December 10, 2023 20:01
…ormat

The goal of this pr is improving observability. We are adding lock strength
and lock durability with Get/Scan/RevScan request if the request is locking.
This is implemented by introducing an optional extension interface to Request
Interface called SafeFormatterRequest. We are also refactoring inside
BatchRequest.SafeFormat with the added interface. Please note the subtle
changed introduced here: if the EndKey is not present we print only Key with
square brackets, and this applies to all the request types.

Fixes: cockroachdb#114475

Release note: None.
This commit implements redact.SafeValue for PushTxnType with the goal of
displaying the PushTxnType on PushTxnRequest with the redacted format.

Fixes: cockroachdb#114475
Release note: None.
@lyang24 lyang24 force-pushed the add_lock_str_dur_to_ba_format branch from c1cb237 to 17eaea1 Compare December 11, 2023 04:01
@nvanbenschoten
Copy link
Member

bors r=nvanbenschoten

1 similar comment
@nvanbenschoten
Copy link
Member

bors r=nvanbenschoten

@craig
Copy link
Contributor

craig bot commented Dec 12, 2023

Build succeeded:

@craig craig bot merged commit a15eeba into cockroachdb:master Dec 12, 2023
4 checks passed
@michae2
Copy link
Collaborator

michae2 commented Dec 13, 2023

This is really helpful! Thank you!

@michae2
Copy link
Collaborator

michae2 commented Dec 13, 2023

@lyang24 @nvanbenschoten I'm going to backport this to 23.2. I think it would be really helpful to have in v23.2.1+.

blathers backport release-23.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-community Originated from the community X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kv: include locking strength and durability in Get/Scan/RevScan SafeFormat
4 participants