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
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))
}