Skip to content

Commit 6113624

Browse files
craig[bot]stevendanna
andcommitted
Merge #146432
146432: kvclient: add request record summary to write buffer tracing r=yuzefovich a=stevendanna This might help someone understand why the batch ultimately sent to KV is incongruous with trace entries added by the SQL layer. Epic: none Release note: None Co-authored-by: Steven Danna <[email protected]>
2 parents 05cc34e + 054a9d3 commit 6113624

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

pkg/cli/clisqlshell/sql.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2169,17 +2169,21 @@ func (c *cliState) doRunStatements(nextState cliStateEnum) cliStateEnum {
21692169
// shell.
21702170
} else {
21712171
traceType := ""
2172+
compact := ""
21722173
if strings.Contains(c.iCtx.autoTrace, "kv") {
21732174
traceType = "kv"
21742175
}
2176+
if strings.Contains(c.iCtx.autoTrace, "compact") {
2177+
compact = "COMPACT"
2178+
}
21752179
if err := c.runWithInterruptableCtx(func(ctx context.Context) error {
21762180
defer c.maybeFlushOutput()
21772181
return c.sqlExecCtx.RunQueryAndFormatResults(ctx,
21782182
c.conn,
21792183
c.iCtx.queryOutput, // query output
21802184
c.iCtx.stdout, // timings
21812185
c.iCtx.stderr, // errors
2182-
clisqlclient.MakeQuery(fmt.Sprintf("SHOW %s TRACE FOR SESSION", traceType)))
2186+
clisqlclient.MakeQuery(fmt.Sprintf("SHOW %s %s TRACE FOR SESSION", compact, traceType)))
21832187
}); err != nil {
21842188
clierror.OutputError(c.iCtx.stderr, err, true /*showSeverity*/, false /*verbose*/)
21852189
if c.exitErr == nil {

pkg/kv/kvclient/kvcoord/txn_interceptor_write_buffer.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ package kvcoord
88
import (
99
"context"
1010
"encoding/binary"
11+
"fmt"
1112
"slices"
1213
"sort"
14+
"strings"
1315
"unsafe"
1416

1517
"github.com/cockroachdb/cockroach/pkg/kv/kvnemesis/kvnemesisutil"
@@ -249,6 +251,12 @@ func (twb *txnWriteBuffer) SendLocked(
249251
return nil, pErr
250252
}
251253

254+
if log.ExpensiveLogEnabled(ctx, 2) {
255+
if summary := rr.Summary(); summary != "" {
256+
log.VEventf(ctx, 2, "txn write buffer modified the batch; %s", summary)
257+
}
258+
}
259+
252260
if len(transformedBa.Requests) == 0 {
253261
// Lower layers (the DistSender and the KVServer) do not expect/handle empty
254262
// batches. If all requests in the batch can be handled locally, and we're
@@ -1195,6 +1203,40 @@ func (rr requestRecords) Empty() bool {
11951203
return len(rr) == 0
11961204
}
11971205

1206+
// Summary returns a string summarizing the modifications made to the original
1207+
// batch that generated this set of request records. An empty string indicates
1208+
// that the original batch was not modified.
1209+
func (rr requestRecords) Summary() string {
1210+
fullyBuffered := make(map[kvpb.Method]int)
1211+
transformed := make(map[kvpb.Method]int)
1212+
for _, rec := range rr {
1213+
if rec.stripped {
1214+
fullyBuffered[rec.origRequest.Method()]++
1215+
} else if rec.transformed {
1216+
transformed[rec.origRequest.Method()]++
1217+
}
1218+
}
1219+
if len(fullyBuffered) == 0 && len(transformed) == 0 {
1220+
return ""
1221+
}
1222+
b := &strings.Builder{}
1223+
sep := ""
1224+
if len(fullyBuffered) > 0 {
1225+
b.WriteString("fully buffered:")
1226+
for method, count := range fullyBuffered {
1227+
fmt.Fprintf(b, " %s:%d", method, count)
1228+
}
1229+
sep = "; "
1230+
}
1231+
if len(transformed) > 0 {
1232+
fmt.Fprintf(b, "%stransformed:", sep)
1233+
for method, count := range transformed {
1234+
fmt.Fprintf(b, " %s:%d", method, count)
1235+
}
1236+
}
1237+
return b.String()
1238+
}
1239+
11981240
// addToBuffer adds a write to the given key to the buffer.
11991241
func (twb *txnWriteBuffer) addToBuffer(
12001242
key roachpb.Key, val roachpb.Value, seq enginepb.TxnSeq, kvNemSeq kvnemesisutil.Container,

pkg/sql/conn_executor_exec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4101,6 +4101,8 @@ func (ex *connExecutor) enableTracing(modes []string) error {
41014101
traceKV = true
41024102
case "cluster":
41034103
recordingType = tracingpb.RecordingVerbose
4104+
case "compact":
4105+
// compact modifies the output format.
41044106
default:
41054107
return pgerror.Newf(pgcode.Syntax,
41064108
"set tracing: unknown mode %q", s)

pkg/sql/logictest/testdata/logic_test/buffered_writes

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,32 @@ query II
479479
SELECT pk,v FROM t5 WHERE pk = 1
480480
----
481481
1 1
482+
483+
subtest tracing
484+
485+
statement ok
486+
CREATE TABLE t6 (pk INT PRIMARY KEY, v INT, FAMILY (pk, v), INDEX (v), INDEX ((v+5)))
487+
488+
statement ok
489+
BEGIN
490+
491+
statement ok
492+
SET TRACING = "on"
493+
494+
statement ok
495+
INSERT INTO t6 VALUES (1,1)
496+
497+
# This assertion depends on write buffering not being disabled because
498+
# of the version or transaction isolation level. Skip configurations
499+
# that result in buffered writes being disabled.
500+
skipif config local-read-committed local-repeatable-read local-mixed-24.3 local-mixed-25.1
501+
query T
502+
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE location LIKE '%write_buffer.go%'
503+
----
504+
txn write buffer modified the batch; fully buffered: Put:2; transformed: ConditionalPut:1
505+
506+
statement ok
507+
SET TRACING = "off"
508+
509+
statement ok
510+
COMMIT

0 commit comments

Comments
 (0)