Skip to content

Commit

Permalink
distsql: Fix backoff execution info inaccurate issue (#58707)
Browse files Browse the repository at this point in the history
close #58703
  • Loading branch information
yibin87 authored Jan 6, 2025
1 parent 4f78f12 commit 1756a8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pkg/distsql/select_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"container/heap"
"context"
"fmt"
"maps"
"strconv"
"sync/atomic"
"time"
Expand Down Expand Up @@ -500,7 +499,6 @@ func (r *selectResult) updateCopRuntimeStats(ctx context.Context, copStats *copr
if r.rootPlanID <= 0 || r.ctx.RuntimeStatsColl == nil || (callee == "" && (copStats.ReqStats == nil || copStats.ReqStats.GetRPCStatsCount() == 0)) {
return
}

if copStats.ScanDetail.ProcessedKeys > 0 || copStats.TimeDetail.KvReadWallTime > 0 {
readKeys := copStats.ScanDetail.ProcessedKeys
readTime := copStats.TimeDetail.KvReadWallTime.Seconds()
Expand Down Expand Up @@ -655,7 +653,9 @@ func (s *selectResultRuntimeStats) mergeCopRuntimeStats(copStats *copr.CopRuntim
if s.backoffSleep == nil {
s.backoffSleep = make(map[string]time.Duration)
}
maps.Copy(s.backoffSleep, copStats.BackoffSleep)
for k, v := range copStats.BackoffSleep {
s.backoffSleep[k] += v
}
}
s.totalProcessTime += copStats.TimeDetail.ProcessTime
s.totalWaitTime += copStats.TimeDetail.WaitTime
Expand Down
17 changes: 13 additions & 4 deletions pkg/distsql/select_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package distsql
import (
"context"
"testing"
"time"

"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
Expand All @@ -30,11 +31,15 @@ import (
func TestUpdateCopRuntimeStats(t *testing.T) {
ctx := mock.NewContext()
ctx.GetSessionVars().StmtCtx = stmtctx.NewStmtCtx()
sr := selectResult{ctx: ctx.GetDistSQLCtx(), storeType: kv.TiKV}
sr := selectResult{ctx: ctx.GetDistSQLCtx(), storeType: kv.TiKV, stats: &selectResultRuntimeStats{}}
require.Nil(t, ctx.GetSessionVars().StmtCtx.RuntimeStatsColl)

sr.rootPlanID = 1234
sr.updateCopRuntimeStats(context.Background(), &copr.CopRuntimeStats{CopExecDetails: execdetails.CopExecDetails{CalleeAddress: "a"}}, 0)
backOffSleep := make(map[string]time.Duration, 1)
backOffSleep["RegionMiss"] = time.Duration(100)
sr.updateCopRuntimeStats(context.Background(), &copr.CopRuntimeStats{CopExecDetails: execdetails.CopExecDetails{CalleeAddress: "a", BackoffSleep: backOffSleep}}, 0)
// RuntimeStatsColl is nil, so the update doesn't take efffect
require.Equal(t, sr.stats.backoffSleep["RegionMiss"], time.Duration(0))

ctx.GetSessionVars().StmtCtx.RuntimeStatsColl = execdetails.NewRuntimeStatsColl(nil)
// refresh the ctx after assigning `RuntimeStatsColl`.
Expand All @@ -48,13 +53,17 @@ func TestUpdateCopRuntimeStats(t *testing.T) {

require.NotEqual(t, len(sr.copPlanIDs), len(sr.selectResp.GetExecutionSummaries()))

sr.updateCopRuntimeStats(context.Background(), &copr.CopRuntimeStats{CopExecDetails: execdetails.CopExecDetails{CalleeAddress: "callee"}}, 0)
backOffSleep["RegionMiss"] = time.Duration(200)
sr.updateCopRuntimeStats(context.Background(), &copr.CopRuntimeStats{CopExecDetails: execdetails.CopExecDetails{CalleeAddress: "callee", BackoffSleep: backOffSleep}}, 0)
require.False(t, ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.ExistsCopStats(1234))
require.Equal(t, sr.stats.backoffSleep["RegionMiss"], time.Duration(200))

sr.copPlanIDs = []int{sr.rootPlanID}
require.NotNil(t, ctx.GetSessionVars().StmtCtx.RuntimeStatsColl)
require.Equal(t, len(sr.copPlanIDs), len(sr.selectResp.GetExecutionSummaries()))

sr.updateCopRuntimeStats(context.Background(), &copr.CopRuntimeStats{CopExecDetails: execdetails.CopExecDetails{CalleeAddress: "callee"}}, 0)
backOffSleep["RegionMiss"] = time.Duration(300)
sr.updateCopRuntimeStats(context.Background(), &copr.CopRuntimeStats{CopExecDetails: execdetails.CopExecDetails{CalleeAddress: "callee", BackoffSleep: backOffSleep}}, 0)
require.Equal(t, "tikv_task:{time:1ns, loops:1}", ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.GetCopStats(1234).String())
require.Equal(t, sr.stats.backoffSleep["RegionMiss"], time.Duration(500))
}

0 comments on commit 1756a8d

Please sign in to comment.