Skip to content

Commit

Permalink
kv: include locking strength and durability in Get/Scan/RevScan SafeF…
Browse files Browse the repository at this point in the history
…ormat

fixes: #114475
Release note: None.
  • Loading branch information
Eric.Yang committed Nov 17, 2023
1 parent c114b1d commit 01618a8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/kv/kvclient/kvcoord/dist_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangecache"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/isolation"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcostmodel"
"github.com/cockroachdb/cockroach/pkg/roachpb"
Expand Down Expand Up @@ -4530,12 +4531,14 @@ func TestDistSenderSlowLogMessage(t *testing.T) {
ba := &kvpb.BatchRequest{}
get := &kvpb.GetRequest{}
get.Key = roachpb.Key("a")
get.KeyLockingStrength = lock.Shared
get.KeyLockingDurability = lock.Unreplicated
ba.Add(get)
br := &kvpb.BatchResponse{}
br.Error = kvpb.NewError(errors.New("boom"))
desc := &roachpb.RangeDescriptor{RangeID: 9, StartKey: roachpb.RKey("x"), EndKey: roachpb.RKey("z")}
{
exp := `have been waiting 8.16s (120 attempts) for RPC Get [‹"a"›,/Min) to` +
exp := `have been waiting 8.16s (120 attempts) for RPC Get [‹"a"›,/Min)[lockStrength=Shared,lockDurability=Unreplicated] to` +
` r9:‹{x-z}› [<no replicas>, next=0, gen=0]; resp: ‹(err: boom)›`
var s redact.StringBuilder
slowRangeRPCWarningStr(&s, ba, dur, attempts, desc, nil /* err */, br)
Expand Down
36 changes: 36 additions & 0 deletions pkg/kv/kvpb/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,42 @@ type Request interface {
flags() flag
}

// SafeFormatterRequest is an optional extension interface used to allow request to do custom formatting.
type SafeFormatterRequest interface {
Request
redact.SafeFormatter
}

var _ SafeFormatterRequest = (*GetRequest)(nil)

// SafeFormat implements the redact.SafeFormatter interface.
func (gr GetRequest) SafeFormat(s redact.SafePrinter, _ rune) {
if gr.KeyLockingStrength == lock.None {
return
}
s.Printf("[lockStrength=%s,lockDurability=%s]", gr.KeyLockingStrength, gr.KeyLockingDurability)
}

var _ SafeFormatterRequest = (*ScanRequest)(nil)

// SafeFormat implements the redact.SafeFormatter interface.
func (sr ScanRequest) SafeFormat(s redact.SafePrinter, _ rune) {
if sr.KeyLockingStrength == lock.None {
return
}
s.Printf("[lockStrength=%s,lockDurability=%s]", sr.KeyLockingStrength, sr.KeyLockingDurability)
}

var _ SafeFormatterRequest = (*ReverseScanRequest)(nil)

// SafeFormat implements the redact.SafeFormatter interface.
func (rsr ReverseScanRequest) SafeFormat(s redact.SafePrinter, _ rune) {
if rsr.KeyLockingStrength == lock.None {
return
}
s.Printf("[lockStrength=%s,lockDurability=%s]", rsr.KeyLockingStrength, rsr.KeyLockingDurability)
}

// LockingReadRequest is an interface used to expose the key-level locking
// strength of a read-only request.
type LockingReadRequest interface {
Expand Down
5 changes: 4 additions & 1 deletion pkg/kv/kvpb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ func (ba BatchRequest) Split(canSplitET bool) [][]RequestUnion {

// SafeFormat implements redact.SafeFormatter.
// It gives a brief summary of the contained requests and keys in the batch.
func (ba BatchRequest) SafeFormat(s redact.SafePrinter, _ rune) {
func (ba BatchRequest) SafeFormat(s redact.SafePrinter, verb rune) {
for count, arg := range ba.Requests {
// Limit the strings to provide just a summary. Without this limit
// a log message with a BatchRequest can be very long.
Expand Down Expand Up @@ -845,6 +845,9 @@ func (ba BatchRequest) SafeFormat(s redact.SafePrinter, _ rune) {
s.Print(req.Method())
}
s.Printf(" [%s,%s)", h.Key, h.EndKey)
if safeFormatterReq, ok := req.(SafeFormatterRequest); ok {
safeFormatterReq.SafeFormat(s, verb)
}
}
}
{
Expand Down

0 comments on commit 01618a8

Please sign in to comment.