From dcb62bb1215dbbf0bfbcbfae9108aeaabd930ce4 Mon Sep 17 00:00:00 2001 From: ekexium Date: Thu, 23 Oct 2025 22:33:37 +0800 Subject: [PATCH] Trace events (#1776) ref pingcap/tidb#64008 Signed-off-by: ekexium --- examples/gcworker/go.mod | 6 +- examples/rawkv/go.mod | 6 +- examples/txnkv/1pc_txn/go.mod | 6 +- examples/txnkv/async_commit/go.mod | 6 +- examples/txnkv/delete_range/go.mod | 6 +- examples/txnkv/go.mod | 6 +- examples/txnkv/pessimistic_txn/go.mod | 6 +- examples/txnkv/unsafedestoryrange/go.mod | 6 +- go.mod | 2 +- go.sum | 4 +- integration_tests/go.mod | 2 +- integration_tests/go.sum | 4 +- internal/locate/region_request.go | 74 +++++++++++++++ trace/trace.go | 113 +++++++++++++++++++++++ txnkv/transaction/commit.go | 14 +++ txnkv/transaction/prewrite.go | 10 ++ txnkv/txnlock/lock_resolver.go | 26 +++++- 17 files changed, 266 insertions(+), 31 deletions(-) create mode 100644 trace/trace.go diff --git a/examples/gcworker/go.mod b/examples/gcworker/go.mod index cc3f2b52..f66e166e 100644 --- a/examples/gcworker/go.mod +++ b/examples/gcworker/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../ diff --git a/examples/rawkv/go.mod b/examples/rawkv/go.mod index 303ff0d2..ce3a7731 100644 --- a/examples/rawkv/go.mod +++ b/examples/rawkv/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../ diff --git a/examples/txnkv/1pc_txn/go.mod b/examples/txnkv/1pc_txn/go.mod index b18b7ca5..e1a73b61 100644 --- a/examples/txnkv/1pc_txn/go.mod +++ b/examples/txnkv/1pc_txn/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../../ diff --git a/examples/txnkv/async_commit/go.mod b/examples/txnkv/async_commit/go.mod index 54874a3c..68e5b6ca 100644 --- a/examples/txnkv/async_commit/go.mod +++ b/examples/txnkv/async_commit/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../../ diff --git a/examples/txnkv/delete_range/go.mod b/examples/txnkv/delete_range/go.mod index 8d153dbf..f003102b 100644 --- a/examples/txnkv/delete_range/go.mod +++ b/examples/txnkv/delete_range/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../../ diff --git a/examples/txnkv/go.mod b/examples/txnkv/go.mod index 951d9bfa..b3bdbd7d 100644 --- a/examples/txnkv/go.mod +++ b/examples/txnkv/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../ diff --git a/examples/txnkv/pessimistic_txn/go.mod b/examples/txnkv/pessimistic_txn/go.mod index b0fca490..54125eec 100644 --- a/examples/txnkv/pessimistic_txn/go.mod +++ b/examples/txnkv/pessimistic_txn/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../../ diff --git a/examples/txnkv/unsafedestoryrange/go.mod b/examples/txnkv/unsafedestoryrange/go.mod index 45ff8ca5..563949c6 100644 --- a/examples/txnkv/unsafedestoryrange/go.mod +++ b/examples/txnkv/unsafedestoryrange/go.mod @@ -11,7 +11,6 @@ require ( github.com/cloudfoundry/gosigar v1.3.6 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -23,14 +22,14 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect github.com/twmb/murmur3 v1.1.3 // indirect @@ -49,6 +48,7 @@ require ( google.golang.org/grpc v1.64.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + modernc.org/mathutil v1.7.1 // indirect ) replace github.com/tikv/client-go/v2 => ../../../ diff --git a/go.mod b/go.mod index 6625d915..e8c84ef4 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.20.5 diff --git a/go.sum b/go.sum index 1cc96e61..9852b81e 100644 --- a/go.sum +++ b/go.sum @@ -77,8 +77,8 @@ github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 h1:tdMsjOqUR7YXH github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86/go.mod h1:exzhVYca3WRtd6gclGNErRWb1qEgff3LYta0LvRmON4= github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E= github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw= -github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 h1:Heh92YLNrK2KTXiLj+cXbCTKG1TmZVSNagyvsGtoqHk= -github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8= +github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 h1:wAL6q8MTf0kL6EGFribwUhMnJW3YovdDUslFgcQ1iWU= +github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8= github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 h1:HR/ylkkLmGdSSDaD8IDP+SZrdhV1Kibl9KrHxJ9eciw= github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/integration_tests/go.mod b/integration_tests/go.mod index 9c8ace9b..257f579a 100644 --- a/integration_tests/go.mod +++ b/integration_tests/go.mod @@ -7,7 +7,7 @@ require ( github.com/ninedraft/israce v0.0.3 github.com/pingcap/errors v0.11.5-0.20250523034308-74f78ae071ee github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 - github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 + github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 github.com/pingcap/tidb v1.1.0-beta.0.20250609033843-a165d9fd7c01 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.10.0 diff --git a/integration_tests/go.sum b/integration_tests/go.sum index ba8b4963..7870a901 100644 --- a/integration_tests/go.sum +++ b/integration_tests/go.sum @@ -1351,8 +1351,8 @@ github.com/pingcap/fn v1.0.0/go.mod h1:u9WZ1ZiOD1RpNhcI42RucFh/lBuzTu6rw88a+oF2Z github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E= github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw= github.com/pingcap/kvproto v0.0.0-20241113043844-e1fa7ea8c302/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8= -github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 h1:Heh92YLNrK2KTXiLj+cXbCTKG1TmZVSNagyvsGtoqHk= -github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8= +github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 h1:wAL6q8MTf0kL6EGFribwUhMnJW3YovdDUslFgcQ1iWU= +github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8= github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM= github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= github.com/pingcap/log v1.1.1-0.20250514022801-14f3b4ca066e h1:8AZZRv1Ox9FVGATVZBBgr6y1MrNBQFABEdovNJt1QIc= diff --git a/internal/locate/region_request.go b/internal/locate/region_request.go index b84789da..d7f1c937 100644 --- a/internal/locate/region_request.go +++ b/internal/locate/region_request.go @@ -64,8 +64,10 @@ import ( "github.com/tikv/client-go/v2/metrics" "github.com/tikv/client-go/v2/oracle" "github.com/tikv/client-go/v2/tikvrpc" + "github.com/tikv/client-go/v2/trace" "github.com/tikv/client-go/v2/util" "github.com/tikv/client-go/v2/util/async" + "github.com/tikv/client-go/v2/util/redact" "github.com/tikv/pd/client/errs" pderr "github.com/tikv/pd/client/errs" ) @@ -993,6 +995,10 @@ func (s *sendReqState) next() (done bool) { logutil.Eventf(bo.GetCtx(), "send %s request to region %d at %s", req.Type, s.args.regionID.id, s.vars.rpcCtx.Addr) s.storeAddr = s.vars.rpcCtx.Addr + // Extract trace ID from context and propagate to TiKV + if traceID := trace.TraceIDFromContext(bo.GetCtx()); len(traceID) > 0 { + req.Context.TraceId = traceID + } req.Context.ClusterId = s.vars.rpcCtx.ClusterID if req.InputRequestSource != "" && s.replicaSelector != nil { patchRequestSource(req, s.replicaSelector.replicaType()) @@ -1122,9 +1128,73 @@ func (s *sendReqState) send() (canceled bool) { } if !injectFailOnSend { + // Emit kv.request.send trace event before sending + if trace.IsCategoryEnabled(trace.CategoryKVRequest) { + var storeID uint64 + var storeAddr string + if rpcCtx.Store != nil { + storeID = rpcCtx.Store.StoreID() + storeAddr = rpcCtx.Store.GetAddr() + } + fields := []zap.Field{ + zap.Stringer("cmd", req.Type), + zap.Uint64("region_id", rpcCtx.Region.id), + zap.Uint64("region_ver", rpcCtx.Region.ver), + zap.Uint64("region_confVer", rpcCtx.Region.confVer), + zap.Uint64("store_id", storeID), + zap.String("store_addr", storeAddr), + zap.Duration("timeout", s.args.timeout), + } + if rpcCtx.Meta != nil { + fields = append(fields, + zap.String("region_start_key", redact.Key(rpcCtx.Meta.GetStartKey())), + zap.String("region_end_key", redact.Key(rpcCtx.Meta.GetEndKey())), + ) + } + trace.TraceEvent(ctx, trace.CategoryKVRequest, "kv.request.send", fields...) + } + start := time.Now() s.vars.resp, s.vars.err = s.client.SendRequest(ctx, sendToAddr, req, s.args.timeout) rpcDuration := time.Since(start) + + // Emit kv.request.result trace event after receiving response + if trace.IsCategoryEnabled(trace.CategoryKVRequest) { + fields := []zap.Field{ + zap.Stringer("cmd", req.Type), + zap.Duration("latency", rpcDuration), + zap.Bool("success", s.vars.err == nil && s.vars.resp != nil), + } + if s.vars.err != nil { + fields = append(fields, zap.Error(s.vars.err)) + } + if s.vars.resp != nil && s.vars.resp.Resp != nil { + if regionErr, _ := s.vars.resp.GetRegionError(); regionErr != nil { + fields = append(fields, zap.String("region_error", regionErr.String())) + } + } + trace.TraceEvent(ctx, trace.CategoryKVRequest, "kv.request.result", fields...) + } + + if trace.IsCategoryEnabled(trace.CategoryKVRequest) && s.vars.resp != nil && s.vars.resp.Resp != nil { + if copResp, ok := s.vars.resp.Resp.(*coprocessor.Response); ok && copResp.OtherError != "" { + var storeID uint64 + var storeAddr string + if rpcCtx.Store != nil { + storeID = rpcCtx.Store.StoreID() + storeAddr = rpcCtx.Store.GetAddr() + } + trace.TraceEvent(ctx, trace.CategoryKVRequest, "cop.other_error", + zap.String("other_error", copResp.OtherError), + zap.Uint64("region_id", rpcCtx.Region.id), + zap.Uint64("region_ver", rpcCtx.Region.ver), + zap.Uint64("region_confVer", rpcCtx.Region.confVer), + zap.Uint64("store_id", storeID), + zap.String("store_addr", storeAddr), + ) + } + } + if s.replicaSelector != nil { recordAttemptedTime(s.replicaSelector, rpcDuration) } @@ -1246,6 +1316,10 @@ func (s *sendReqState) initForAsyncRequest() (ok bool) { // set access location based on source and target "zone" label. s.setReqAccessLocation(req) + // Extract trace ID from context and propagate to TiKV + if traceID := trace.TraceIDFromContext(bo.GetCtx()); len(traceID) > 0 { + req.Context.TraceId = traceID + } req.Context.ClusterId = s.vars.rpcCtx.ClusterID if req.InputRequestSource != "" && s.replicaSelector != nil { patchRequestSource(req, s.replicaSelector.replicaType()) diff --git a/trace/trace.go b/trace/trace.go new file mode 100644 index 00000000..a7253e57 --- /dev/null +++ b/trace/trace.go @@ -0,0 +1,113 @@ +// Copyright 2025 TiKV Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package trace + +import ( + "context" + "sync/atomic" + + "go.uber.org/zap" +) + +// Category identifies a trace event family emitted from client-go. +type Category uint32 + +const ( + // CategoryTxn2PC traces two-phase commit prewrite and commit phases. + CategoryTxn2PC Category = iota + // CategoryTxnLockResolve traces lock resolution and conflict handling. + CategoryTxnLockResolve + // CategoryKVRequest traces individual KV request send and result events. + CategoryKVRequest +) + +// TraceEventFunc is the function signature for recording trace events. +type TraceEventFunc func(ctx context.Context, category Category, name string, fields ...zap.Field) + +// IsCategoryEnabledFunc is the function signature for checking if a category is enabled. +type IsCategoryEnabledFunc func(category Category) bool + +// Default no-op implementations +func noopTraceEvent(context.Context, Category, string, ...zap.Field) {} +func noopIsCategoryEnabled(Category) bool { return false } + +// Global function pointers stored independently +var ( + globalTraceEventFunc atomic.Pointer[TraceEventFunc] + globalIsCategoryEnabledFunc atomic.Pointer[IsCategoryEnabledFunc] +) + +func init() { + // Set default no-op implementations + defaultTraceEvent := TraceEventFunc(noopTraceEvent) + defaultIsCategoryEnabled := IsCategoryEnabledFunc(noopIsCategoryEnabled) + globalTraceEventFunc.Store(&defaultTraceEvent) + globalIsCategoryEnabledFunc.Store(&defaultIsCategoryEnabled) +} + +// SetTraceEventFunc registers the trace event handler function. +// This is typically called once during application initialization (e.g., by TiDB). +// Passing nil will use a no-op implementation. +func SetTraceEventFunc(fn TraceEventFunc) { + if fn == nil { + fn = noopTraceEvent + } + globalTraceEventFunc.Store(&fn) +} + +// SetIsCategoryEnabledFunc registers the category enablement check function. +// This can be updated independently of the trace event function. +// Passing nil will use a no-op implementation that returns false. +func SetIsCategoryEnabledFunc(fn IsCategoryEnabledFunc) { + if fn == nil { + fn = noopIsCategoryEnabled + } + globalIsCategoryEnabledFunc.Store(&fn) +} + +// TraceEvent records a trace event using the registered function. +func TraceEvent(ctx context.Context, category Category, name string, fields ...zap.Field) { + fn := globalTraceEventFunc.Load() + (*fn)(ctx, category, name, fields...) +} + +// IsCategoryEnabled checks if a category is enabled for tracing using the registered function. +func IsCategoryEnabled(category Category) bool { + fn := globalIsCategoryEnabledFunc.Load() + return (*fn)(category) +} + +// Trace ID context management + +// traceIDKey is the context key for storing trace IDs. +// This key is shared between TiDB and client-go for trace ID propagation. +type traceIDKey struct{} + +// ContextWithTraceID returns a new context with the given trace ID attached. +func ContextWithTraceID(ctx context.Context, traceID []byte) context.Context { + return context.WithValue(ctx, traceIDKey{}, traceID) +} + +// TraceIDFromContext extracts the trace ID from the context. +// Returns nil if no trace ID is present. +func TraceIDFromContext(ctx context.Context) []byte { + if ctx == nil { + return nil + } + if traceID, ok := ctx.Value(traceIDKey{}).([]byte); ok { + return traceID + } + return nil +} diff --git a/txnkv/transaction/commit.go b/txnkv/transaction/commit.go index b2eb59f3..9e39ad0a 100644 --- a/txnkv/transaction/commit.go +++ b/txnkv/transaction/commit.go @@ -49,6 +49,7 @@ import ( "github.com/tikv/client-go/v2/internal/logutil" "github.com/tikv/client-go/v2/metrics" "github.com/tikv/client-go/v2/tikvrpc" + "github.com/tikv/client-go/v2/trace" "github.com/tikv/client-go/v2/util/redact" "go.uber.org/zap" ) @@ -102,6 +103,13 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac c.resourceGroupTagger(req) } + trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.start", + zap.Uint64("startTS", c.startTS), + zap.Uint64("commitTS", c.commitTS), + zap.Uint64("regionID", batch.region.GetID()), + zap.Bool("isPrimary", batch.isPrimary), + zap.Int("keyCount", len(keys))) + tBegin := time.Now() attempts := 0 @@ -126,6 +134,9 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac // Unexpected error occurs, return it. if err != nil { + trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result", + zap.Uint64("regionID", batch.region.GetID()), + zap.Bool("success", false)) return err } @@ -245,6 +256,9 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac // Group that contains primary key is always the first. // We mark transaction's status committed when we receive the first success response. c.mu.committed = true + trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result", + zap.Uint64("regionID", batch.region.GetID()), + zap.Bool("success", true)) return nil } diff --git a/txnkv/transaction/prewrite.go b/txnkv/transaction/prewrite.go index a8cf739b..44c19e3f 100644 --- a/txnkv/transaction/prewrite.go +++ b/txnkv/transaction/prewrite.go @@ -54,6 +54,7 @@ import ( "github.com/tikv/client-go/v2/metrics" "github.com/tikv/client-go/v2/oracle" "github.com/tikv/client-go/v2/tikvrpc" + "github.com/tikv/client-go/v2/trace" "github.com/tikv/client-go/v2/txnkv/txnlock" "github.com/tikv/client-go/v2/util" "github.com/tikv/client-go/v2/util/redact" @@ -238,12 +239,21 @@ func (action actionPrewrite) handleSingleBatch( handler := action.newSingleBatchPrewriteReqHandler(c, batch, bo) + trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.start", + zap.Uint64("startTS", c.startTS), + zap.Uint64("regionID", batch.region.GetID()), + zap.Bool("isPrimary", batch.isPrimary), + zap.Int("keyCount", batch.mutations.Len())) + var retryable bool for { // It will return false if the request is success or meet an unretryable error. // otherwise if the error is retryable, it will return true. retryable, err = handler.sendReqAndCheck() if !retryable { + trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.result", + zap.Uint64("regionID", batch.region.GetID()), + zap.Bool("success", err == nil)) handler.drop(err) return err } diff --git a/txnkv/txnlock/lock_resolver.go b/txnkv/txnlock/lock_resolver.go index e414b438..9657e5bf 100644 --- a/txnkv/txnlock/lock_resolver.go +++ b/txnkv/txnlock/lock_resolver.go @@ -35,6 +35,7 @@ import ( "github.com/tikv/client-go/v2/metrics" "github.com/tikv/client-go/v2/oracle" "github.com/tikv/client-go/v2/tikvrpc" + "github.com/tikv/client-go/v2/trace" "github.com/tikv/client-go/v2/util" "github.com/tikv/client-go/v2/util/redact" "go.uber.org/zap" @@ -485,7 +486,7 @@ func (lr *LockResolver) ResolveLocksDone(callerStartTS uint64, token int) { lr.mu.Unlock() } -func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, opts ResolveLocksOptions) (ResolveLockResult, error) { +func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, opts ResolveLocksOptions) (result ResolveLockResult, err error) { callerStartTS, locks, forRead, lite, detail, pessimisticRegionResolve := opts.CallerStartTS, opts.Locks, opts.ForRead, opts.Lite, opts.Detail, opts.PessimisticRegionResolve util.EvalFailpoint("tryResolveLock") if lr.testingKnobs.meetLock != nil { @@ -497,6 +498,29 @@ func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, opts ResolveLocksOptio TTL: msBeforeTxnExpired.value(), }, nil } + trace.TraceEvent(bo.GetCtx(), trace.CategoryTxnLockResolve, "resolve_locks.start", + zap.Uint64("callerStartTS", callerStartTS), + zap.Int("lockCount", len(locks)), + zap.Bool("forRead", forRead), + zap.Bool("lite", lite)) + + // trace the results + defer func() { + if trace.IsCategoryEnabled(trace.CategoryTxnLockResolve) { + fields := []zap.Field{ + zap.Uint64("callerStartTS", callerStartTS), + zap.Int("lockCount", len(locks)), + zap.Int64("ttl", result.TTL), + zap.Int("ignoredLocks", len(result.IgnoreLocks)), + zap.Int("accessibleLocks", len(result.AccessLocks)), + } + if err != nil { + fields = append(fields, zap.Error(err)) + } + trace.TraceEvent(bo.GetCtx(), trace.CategoryTxnLockResolve, "resolve_locks.finish", fields...) + } + }() + metrics.LockResolverCountWithResolve.Inc() // This is the origin resolve lock time. // TODO(you06): record the more details and calculate the total time by calculating the sum of details.