Skip to content

Commit 9869df0

Browse files
committed
kvserver: annotate Replica.Send with pprof labels
This commit adds pprof labels to Send methods, as a proper replacement of the no longer working (since Go 1.17) unnamed parameters hack. Adding labels is conditional to the CPU profile cluster setting, to avoid the profiling cost and extra allocations in pprof.Do context construction during normal operation. Release note: None
1 parent 8f1b11e commit 9869df0

2 files changed

Lines changed: 12 additions & 31 deletions

File tree

pkg/kv/kvserver/replica_send.go

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package kvserver
1313
import (
1414
"context"
1515
"reflect"
16+
"runtime/pprof"
1617

1718
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval"
1819
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency"
@@ -24,6 +25,7 @@ import (
2425
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/txnwait"
2526
"github.com/cockroachdb/cockroach/pkg/roachpb"
2627
"github.com/cockroachdb/cockroach/pkg/settings"
28+
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
2729
"github.com/cockroachdb/cockroach/pkg/util/circuit"
2830
"github.com/cockroachdb/cockroach/pkg/util/hlc"
2931
"github.com/cockroachdb/cockroach/pkg/util/log"
@@ -109,38 +111,17 @@ func (r *Replica) Send(
109111
// SendWithWriteBytes is the implementation of Send with an additional
110112
// *StoreWriteBytes return value.
111113
func (r *Replica) SendWithWriteBytes(
112-
ctx context.Context, ba roachpb.BatchRequest,
114+
ctx context.Context, req roachpb.BatchRequest,
113115
) (*roachpb.BatchResponse, *StoreWriteBytes, *roachpb.Error) {
114-
return r.sendWithoutRangeID(ctx, &ba)
115-
}
116-
117-
// sendWithoutRangeID used to be called sendWithRangeID, accepted a `_forStacks
118-
// roachpb.RangeID` argument, and had the description below. Ever since Go
119-
// switched to the register-based calling convention though, this stopped
120-
// working, giving essentially random numbers in the goroutine dumps that were
121-
// misleading. It has thus been "disarmed" until Go produces useful values
122-
// again.
123-
//
124-
// See (internal): https://cockroachlabs.slack.com/archives/G01G8LK77DK/p1641478596004700
125-
//
126-
// sendWithRangeID takes an unused rangeID argument so that the range
127-
// ID will be accessible in stack traces (both in panics and when
128-
// sampling goroutines from a live server). This line is subject to
129-
// the whims of the compiler and it can be difficult to find the right
130-
// value, but as of this writing the following example shows a stack
131-
// while processing range 21 (0x15) (the first occurrence of that
132-
// number is the rangeID argument, the second is within the encoded
133-
// BatchRequest, although we don't want to rely on that occurring
134-
// within the portion printed in the stack trace):
135-
//
136-
// github.com/cockroachdb/cockroach/pkg/storage.(*Replica).sendWithRangeID(0xc420d1a000, 0x64bfb80, 0xc421564b10, 0x15, 0x153fd4634aeb0193, 0x0, 0x100000001, 0x1, 0x15, 0x0, ...)
137-
func (r *Replica) sendWithoutRangeID(
138-
ctx context.Context, ba *roachpb.BatchRequest,
139-
) (_ *roachpb.BatchResponse, _ *StoreWriteBytes, rErr *roachpb.Error) {
140-
var br *roachpb.BatchResponse
141-
116+
if r.store.cfg.Settings.CPUProfileType() == cluster.CPUProfileWithLabels {
117+
defer pprof.SetGoroutineLabels(ctx)
118+
// Note: the defer statement captured the previous context.
119+
ctx = pprof.WithLabels(ctx, pprof.Labels("range_str", r.rangeStr.String()))
120+
pprof.SetGoroutineLabels(ctx)
121+
}
142122
// Add the range log tag.
143123
ctx = r.AnnotateCtx(ctx)
124+
ba := &req
144125

145126
// Record summary throughput information about the batch request for
146127
// accounting.
@@ -177,6 +158,7 @@ func (r *Replica) sendWithoutRangeID(
177158
}
178159

179160
// Differentiate between read-write, read-only, and admin.
161+
var br *roachpb.BatchResponse
180162
var pErr *roachpb.Error
181163
var writeBytes *StoreWriteBytes
182164
if isReadOnly {

pkg/kv/kvserver/replica_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13452,8 +13452,7 @@ func TestProposalNotAcknowledgedOrReproposedAfterApplication(t *testing.T) {
1345213452

1345313453
// Round trip another proposal through the replica to ensure that previously
1345413454
// committed entries have been applied.
13455-
_, _, pErr = tc.repl.sendWithoutRangeID(ctx, &ba)
13456-
if pErr != nil {
13455+
if _, pErr := tc.repl.Send(ctx, ba); pErr != nil {
1345713456
t.Fatal(pErr)
1345813457
}
1345913458
log.Flush()

0 commit comments

Comments
 (0)