From 20db4ad363fc7bae134d73d2002ed5b5d72106ed Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Mon, 9 Jul 2018 15:54:37 -0400 Subject: [PATCH] storage: Include range IDs in stack traces Use dummy wrapper functions at key points (withRaftGroupLocked and Replica.Send) to ensure that the range ID is visible in the stack trace. Fixes #11684 Release note: None --- pkg/storage/replica.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/storage/replica.go b/pkg/storage/replica.go index 0ec378013a0b..ac0f65a3f2b8 100644 --- a/pkg/storage/replica.go +++ b/pkg/storage/replica.go @@ -548,7 +548,11 @@ func (r *Replica) withRaftGroupLocked( } } - unquiesce, err := f(r.mu.internalRaftGroup) + // This wrapper function is a hack to add range IDs to stack traces + // using the same pattern as Replica.sendWithRangeID. + unquiesce, err := func(rangeID roachpb.RangeID, raftGroup *raft.RawNode) (bool, error) { + return f(raftGroup) + }(r.RangeID, r.mu.internalRaftGroup) if unquiesce { r.unquiesceAndWakeLeaderLocked() } @@ -1911,6 +1915,23 @@ func (r *Replica) maybeInitializeRaftGroup(ctx context.Context) { // ctx should contain the log tags from the store (and up). func (r *Replica) Send( ctx context.Context, ba roachpb.BatchRequest, +) (*roachpb.BatchResponse, *roachpb.Error) { + return r.sendWithRangeID(ctx, r.RangeID, ba) +} + +// sendWithRangeID takes an unused rangeID argument so that the range +// ID will be accessible in stack traces (both in panics and when +// sampling goroutines from a live server). This line is subject to +// the whims of the compiler and it can be difficult to find the right +// value, but as of this writing the following example shows a stack +// while processing range 21 (0x15) (the first occurrence of that +// number is the rangeID argument, the second is within the encoded +// BatchRequest, although we don't want to rely on that occurring +// within the portion printed in the stack trace): +// +// github.com/cockroachdb/cockroach/pkg/storage.(*Replica).sendWithRangeID(0xc420d1a000, 0x64bfb80, 0xc421564b10, 0x15, 0x153fd4634aeb0193, 0x0, 0x100000001, 0x1, 0x15, 0x0, ...) +func (r *Replica) sendWithRangeID( + ctx context.Context, rangeID roachpb.RangeID, ba roachpb.BatchRequest, ) (*roachpb.BatchResponse, *roachpb.Error) { var br *roachpb.BatchResponse if r.leaseholderStats != nil && ba.Header.GatewayNodeID != 0 {