Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5854,13 +5854,13 @@ def go_deps():
name = "com_github_pingcap_tipb",
build_file_proto_mode = "disable_global",
importpath = "github.com/pingcap/tipb",
sha256 = "1b707429b5b938a05b250b5770be2a6aa243d6a4983d23b01bbca164e86b3e3c",
strip_prefix = "github.com/pingcap/tipb@v0.0.0-20241022082558-0607513e7fa4",
sha256 = "a1f1f8523c7a8f788e73fed28c2accee002f0c5c502fdbed257eb6a42810e784",
strip_prefix = "github.com/pingcap/tipb@v0.0.0-20260522023117-6ceadabb61de",
urls = [
"http://bazel-cache.pingcap.net:8080/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241022082558-0607513e7fa4.zip",
"http://ats.apps.svc/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241022082558-0607513e7fa4.zip",
"https://cache.hawkingrei.com/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241022082558-0607513e7fa4.zip",
"https://storage.googleapis.com/pingcapmirror/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20241022082558-0607513e7fa4.zip",
"http://bazel-cache.pingcap.net:8080/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20260522023117-6ceadabb61de.zip",
"http://ats.apps.svc/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20260522023117-6ceadabb61de.zip",
"https://cache.hawkingrei.com/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20260522023117-6ceadabb61de.zip",
"https://storage.googleapis.com/pingcapmirror/gomod/github.com/pingcap/tipb/com_github_pingcap_tipb-v0.0.0-20260522023117-6ceadabb61de.zip",
],
)
go_repository(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ require (
github.com/pingcap/log v1.1.1-0.20250917021125-19901e015dc9
github.com/pingcap/sysutil v1.0.1-0.20240311050922-ae81ee01f3a5
github.com/pingcap/tidb/pkg/parser v0.0.0-20211011031125-9b13dc409c5e
github.com/pingcap/tipb v0.0.0-20241022082558-0607513e7fa4
github.com/pingcap/tipb v0.0.0-20260522023117-6ceadabb61de
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.57.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,8 @@ github.com/pingcap/log v1.1.1-0.20250917021125-19901e015dc9 h1:qG9BSvlWFEE5otQGa
github.com/pingcap/log v1.1.1-0.20250917021125-19901e015dc9/go.mod h1:ORfBOFp1eteu2odzsyaxI+b8TzJwgjwyQcGhI+9SfEA=
github.com/pingcap/sysutil v1.0.1-0.20240311050922-ae81ee01f3a5 h1:T4pXRhBflzDeAhmOQHNPRRogMYxP13V7BkYw3ZsoSfE=
github.com/pingcap/sysutil v1.0.1-0.20240311050922-ae81ee01f3a5/go.mod h1:rlimy0GcTvjiJqvD5mXTRr8O2eNZPBrcUgiWVYp9530=
github.com/pingcap/tipb v0.0.0-20241022082558-0607513e7fa4 h1:wvaUybJT0fUReCDcFtV3CEvMuI9iu+G7IW72tbSlil4=
github.com/pingcap/tipb v0.0.0-20241022082558-0607513e7fa4/go.mod h1:A7mrd7WHBl1o63LE2bIBGEJMTNWXqhgmYiOvMLxozfs=
github.com/pingcap/tipb v0.0.0-20260522023117-6ceadabb61de h1:zZNGJcObLgFvuOF9CPviXomIN9QGAEDj9w9SUGp3s64=
github.com/pingcap/tipb v0.0.0-20260522023117-6ceadabb61de/go.mod h1:A7mrd7WHBl1o63LE2bIBGEJMTNWXqhgmYiOvMLxozfs=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down
6 changes: 3 additions & 3 deletions pkg/executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2191,13 +2191,13 @@ func (a *ExecStmt) observeStmtBeginForTopSQL(ctx context.Context) context.Contex
}
// Always attach the SQL and plan info uses to catch the running SQL when Top SQL is enabled in execution.
if stats != nil {
stats.OnExecutionBegin(sqlDigestByte, planDigestByte)
stats.OnExecutionBegin(sqlDigestByte, planDigestByte, vars.InPacketBytes.Load())
}
return topsql.AttachSQLAndPlanInfo(ctx, sqlDigest, planDigest)
}

if stats != nil {
stats.OnExecutionBegin(sqlDigestByte, planDigestByte)
stats.OnExecutionBegin(sqlDigestByte, planDigestByte, vars.InPacketBytes.Load())
// This is a special logic prepared for TiKV's SQLExecCount.
sc.KvExecCounter = stats.CreateKvExecCounter(sqlDigestByte, planDigestByte)
}
Expand All @@ -2222,7 +2222,7 @@ func (a *ExecStmt) observeStmtFinishedForTopSQL() {
if stats := a.Ctx.GetStmtStats(); stats != nil && topsqlstate.TopSQLEnabled() {
sqlDigest, planDigest := a.getSQLPlanDigest()
execDuration := vars.GetTotalCostDuration()
stats.OnExecutionFinished(sqlDigest, planDigest, execDuration)
stats.OnExecutionFinished(sqlDigest, planDigest, execDuration, vars.OutPacketBytes.Load())
}
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,11 @@ func (cc *clientConn) readPacket() ([]byte, error) {
if cc.getCtx() != nil {
cc.pkt.SetMaxAllowedPacket(cc.ctx.GetSessionVars().MaxAllowedPacket)
}
return cc.pkt.ReadPacket()
data, err := cc.pkt.ReadPacket()
if err == nil && cc.getCtx() != nil {
cc.ctx.GetSessionVars().InPacketBytes.Add(uint64(len(data)))
}
return data, err
}

func (cc *clientConn) writePacket(data []byte) error {
Expand All @@ -495,6 +499,9 @@ func (cc *clientConn) writePacket(data []byte) error {
failpoint.Return(nil)
}
})
if cc.getCtx() != nil {
cc.ctx.GetSessionVars().OutPacketBytes.Add(uint64(len(data)))
}
Comment thread
yibin87 marked this conversation as resolved.
return cc.pkt.WritePacket(data)
}

Expand Down Expand Up @@ -1275,6 +1282,8 @@ func (cc *clientConn) dispatch(ctx context.Context, data []byte) error {
defer func() {
// reset killed for each request
cc.ctx.GetSessionVars().SQLKiller.Reset()
cc.ctx.GetSessionVars().InPacketBytes.Store(0)
cc.ctx.GetSessionVars().OutPacketBytes.Store(0)
Comment thread
yibin87 marked this conversation as resolved.
}()
t := time.Now()
if (cc.ctx.Status() & mysql.ServerStatusInTrans) > 0 {
Expand Down
43 changes: 33 additions & 10 deletions pkg/server/tests/commontest/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3517,6 +3517,24 @@ func TestAuditPluginRetrying(t *testing.T) {
retrying bool
}
testResults := make([]normalTest, 0)
var testResultsMu sync.Mutex
appendResult := func(audit normalTest) {
testResultsMu.Lock()
defer testResultsMu.Unlock()
testResults = append(testResults, audit)
}
resetResults := func() {
testResultsMu.Lock()
defer testResultsMu.Unlock()
testResults = testResults[:0]
}
getResults := func() []normalTest {
testResultsMu.Lock()
defer testResultsMu.Unlock()
results := make([]normalTest, len(testResults))
copy(results, testResults)
return results
}

onGeneralEvent := func(ctx context.Context, sctx *variable.SessionVars, event plugin.GeneralEvent, cmd string) {
// Only consider the Completed event
Expand All @@ -3529,7 +3547,7 @@ func TestAuditPluginRetrying(t *testing.T) {
audit.retrying = retrying.(bool)
}
audit.sql = sctx.StmtCtx.OriginalSQL
testResults = append(testResults, audit)
appendResult(audit)
}
plugin.LoadPluginForTest(t, onGeneralEvent)
defer plugin.Shutdown(context.Background())
Expand All @@ -3546,25 +3564,29 @@ func TestAuditPluginRetrying(t *testing.T) {
_, err = db.Exec("INSERT INTO auto_retry_test VALUES (1, 0)")
require.NoError(t, err)

testResults = testResults[:0]
resetResults()
// Inject deterministic write conflicts to make statement retries observable in auto-commit mode.
testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/store/mockstore/unistore/tikv/pessimisticLockReturnWriteConflict", "10*return(true)")
// a big enough concurrency to trigger retries
concurrency := 500
var wg sync.WaitGroup
for range concurrency {
wg.Add(1)
conn, err := db.Conn(context.Background())
require.NoError(t, err)
go func() {
go func(conn *sql.Conn) {
defer wg.Done()
_, err := conn.QueryContext(context.Background(), "UPDATE auto_retry_test SET val = val + 1 WHERE id = 1")
defer conn.Close()
_, err := conn.ExecContext(context.Background(), "UPDATE auto_retry_test SET val = val + 1 WHERE id = 1")
require.NoError(t, err)
}()
}(conn)
}
wg.Wait()

require.Greater(t, len(testResults), concurrency)
results := getResults()
require.Greater(t, len(results), concurrency)
nonRetryingCount := 0
for _, res := range testResults {
for _, res := range results {
if !res.retrying {
nonRetryingCount++
}
Expand Down Expand Up @@ -3597,7 +3619,7 @@ func TestAuditPluginRetrying(t *testing.T) {
return conn
}

testResults = testResults[:0]
resetResults()
var wg sync.WaitGroup
wg.Add(2)
// Transaction 1
Expand Down Expand Up @@ -3633,9 +3655,10 @@ func TestAuditPluginRetrying(t *testing.T) {
}()
wg.Wait()

results := getResults()
retryingCount := 0
nonRetryingCount := 0
for _, res := range testResults {
for _, res := range results {
if res.retrying {
retryingCount++
} else {
Expand Down Expand Up @@ -3665,7 +3688,7 @@ func TestAuditPluginRetrying(t *testing.T) {
ts.RunTests(t, nil, func(dbt *testkit.DBTestKit) {
db := dbt.GetDB()

testResults = testResults[:0]
resetResults()
runExplicitTransactionRetry(db, true)
})
}
6 changes: 6 additions & 0 deletions pkg/sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,12 @@ type SessionVars struct {

// InternalSQLScanUserTable indicates whether to use user table for internal SQL. it will be used by TTL scan
InternalSQLScanUserTable bool

// InPacketBytes records the total incoming packet bytes from clients for current session.
InPacketBytes atomic.Uint64

// OutPacketBytes records the total outcoming packet bytes to clients for current session.
OutPacketBytes atomic.Uint64
}

// GetSessionVars implements the `SessionVarsProvider` interface.
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/topsql/reporter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ go_test(
],
embed = [":reporter"],
flaky = True,
shard_count = 36,
shard_count = 37,
deps = [
"//pkg/config",
"//pkg/testkit/testsetup",
Expand Down
Loading