From 1ec5f3ba1c8c65310d2322b62172a5510a5a72a8 Mon Sep 17 00:00:00 2001 From: Austen McClernon Date: Mon, 19 Aug 2024 17:35:11 +0000 Subject: [PATCH 1/2] kvcoord: safe format replica slice Introduce a `SafeFormat` method on `ReplicaSlice`, which can be used to print out the replica slice in logging. Informs: #129031 Release note: None --- pkg/kv/kvclient/kvcoord/BUILD.bazel | 1 + pkg/kv/kvclient/kvcoord/replica_slice.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/kv/kvclient/kvcoord/BUILD.bazel b/pkg/kv/kvclient/kvcoord/BUILD.bazel index ca63215d9630..12ce56996eb5 100644 --- a/pkg/kv/kvclient/kvcoord/BUILD.bazel +++ b/pkg/kv/kvclient/kvcoord/BUILD.bazel @@ -71,6 +71,7 @@ go_library( "//pkg/util/future", "//pkg/util/grpcutil", "//pkg/util/hlc", + "//pkg/util/humanizeutil", "//pkg/util/iterutil", "//pkg/util/limit", "//pkg/util/log", diff --git a/pkg/kv/kvclient/kvcoord/replica_slice.go b/pkg/kv/kvclient/kvcoord/replica_slice.go index dcbfda6ec6d1..8fc8445f2a13 100644 --- a/pkg/kv/kvclient/kvcoord/replica_slice.go +++ b/pkg/kv/kvclient/kvcoord/replica_slice.go @@ -19,9 +19,11 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv/kvclient" "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/settings/cluster" + "github.com/cockroachdb/cockroach/pkg/util/humanizeutil" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/shuffle" "github.com/cockroachdb/errors" + "github.com/cockroachdb/redact" ) // ReplicaInfo extends the Replica structure with the associated node @@ -41,6 +43,25 @@ type ReplicaInfo struct { // A ReplicaSlice is a slice of ReplicaInfo. type ReplicaSlice []ReplicaInfo +func (rs ReplicaSlice) String() string { + return redact.StringWithoutMarkers(rs) +} + +// SafeFormat implements the redact.SafeFormatter interface. +func (rs ReplicaSlice) SafeFormat(w redact.SafePrinter, _ rune) { + var buf redact.StringBuilder + buf.Print("[") + for i, r := range rs { + if i > 0 { + buf.Print(",") + } + buf.Printf("%v(health=%v match=%d latency=%v)", + r, r.healthy, r.tierMatchLength, humanizeutil.Duration(r.latency)) + } + buf.Print("]") + w.Print(buf) +} + // ReplicaSliceFilter controls which kinds of replicas are to be included in // the slice for routing BatchRequests to. type ReplicaSliceFilter int From d2817637ea68010e5e744533195b2001f5910b9e Mon Sep 17 00:00:00 2001 From: Austen McClernon Date: Mon, 19 Aug 2024 17:42:25 +0000 Subject: [PATCH 2/2] kvcoord: log replica order on nearest routing policy Previously, we would log an event when routing to the nearest replica, which would then show up in traces and could be used to determine the routing policy of a query. It is also useful to know how the nearest replica was selected i.e., latency, locality or by health. Log the event after sorting by the replicas' distance to the sender and include the replica order. Informs: #129031 Release note: None --- pkg/kv/kvclient/kvcoord/dist_sender.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kv/kvclient/kvcoord/dist_sender.go b/pkg/kv/kvclient/kvcoord/dist_sender.go index 35526165c8a4..59242b1bc12c 100644 --- a/pkg/kv/kvclient/kvcoord/dist_sender.go +++ b/pkg/kv/kvclient/kvcoord/dist_sender.go @@ -2571,8 +2571,8 @@ func (ds *DistSender) sendToReplicas( case kvpb.RoutingPolicy_NEAREST: // Order by latency. - log.VEvent(ctx, 2, "routing to nearest replica; leaseholder not required") replicas.OptimizeReplicaOrder(ds.st, ds.nodeIDGetter(), ds.healthFunc, ds.latencyFunc, ds.locality) + log.VEventf(ctx, 2, "routing to nearest replica; leaseholder not required order=%v", replicas) default: log.Fatalf(ctx, "unknown routing policy: %s", ba.RoutingPolicy)