Skip to content

Commit 56c07d0

Browse files
authored
executor: optimize executor runtime stats by avoid unnecessary clone (#54004)
ref #56649
1 parent 1521bf7 commit 56c07d0

File tree

6 files changed

+19
-27
lines changed

6 files changed

+19
-27
lines changed

pkg/distsql/distsql_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ func TestSelectResultRuntimeStats(t *testing.T) {
119119
s1.procKeys.Add(100)
120120
s1.procKeys.Add(200)
121121

122-
s2 := *s1
123-
stmtStats.RegisterStats(1, s1)
124-
stmtStats.RegisterStats(1, &s2)
122+
s2 := s1.Clone()
123+
stmtStats.RegisterStats(1, s1.Clone())
124+
stmtStats.RegisterStats(1, s2)
125125
stats := stmtStats.GetRootStats(1)
126126
expect := "time:1s, open:0s, close:0s, loops:1, cop_task: {num: 4, max: 1s, min: 1ms, avg: 500.5ms, p95: 1s, max_proc_keys: 200, p95_proc_keys: 200, tot_proc: 2s, tot_wait: 2s, copr_cache_hit_ratio: 0.00, max_distsql_concurrency: 15}, backoff{RegionMiss: 2ms}"
127127
require.Equal(t, expect, stats.String())
@@ -134,7 +134,7 @@ func TestSelectResultRuntimeStats(t *testing.T) {
134134
}
135135
s1.reqStat.RecordRPCErrorStats("server_is_busy")
136136
s1.reqStat.RecordRPCErrorStats("server_is_busy")
137-
stmtStats.RegisterStats(2, s1)
137+
stmtStats.RegisterStats(2, s1.Clone())
138138
stats = stmtStats.GetRootStats(2)
139139
expect = "cop_task: {num: 2, max: 1s, min: 1ms, avg: 500.5ms, p95: 1s, max_proc_keys: 200, p95_proc_keys: 200, tot_proc: 1s, tot_wait: 1s, copr_cache_hit_ratio: 0.00, max_distsql_concurrency: 15}, rpc_info:{Cop:{num_rpc:1, total_time:1s}, rpc_errors:{server_is_busy:2}}, backoff{RegionMiss: 1ms}"
140140
require.Equal(t, expect, stats.String())

pkg/executor/batch_point_get.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,15 @@ func (e *BatchPointGetExec) Close() error {
177177
if e.RuntimeStats() != nil && e.snapshot != nil {
178178
e.snapshot.SetOption(kv.CollectRuntimeStats, nil)
179179
}
180-
if e.indexUsageReporter != nil {
180+
if e.indexUsageReporter != nil && e.stats != nil {
181181
kvReqTotal := e.stats.GetCmdRPCCount(tikvrpc.CmdBatchGet)
182182
// We cannot distinguish how many rows are coming from each partition. Here, we calculate all index usages
183183
// percentage according to the row counts for the whole table.
184+
rows := e.RuntimeStats().GetActRows()
184185
if e.idxInfo != nil {
185-
e.indexUsageReporter.ReportPointGetIndexUsage(e.tblInfo.ID, e.tblInfo.ID, e.idxInfo.ID, e.ID(), kvReqTotal)
186+
e.indexUsageReporter.ReportPointGetIndexUsage(e.tblInfo.ID, e.tblInfo.ID, e.idxInfo.ID, kvReqTotal, rows)
186187
} else {
187-
e.indexUsageReporter.ReportPointGetIndexUsageForHandle(e.tblInfo, e.tblInfo.ID, e.ID(), kvReqTotal)
188+
e.indexUsageReporter.ReportPointGetIndexUsageForHandle(e.tblInfo, e.tblInfo.ID, kvReqTotal, rows)
188189
}
189190
}
190191
e.inited = 0

pkg/executor/internal/exec/indexusage.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,24 @@ func (e *IndexUsageReporter) ReportCopIndexUsage(tableID int64, physicalTableID
8989

9090
// ReportPointGetIndexUsageForHandle wraps around `ReportPointGetIndexUsage` to get the `indexID` automatically
9191
// from the `table.Table` if the table has a clustered index or integer primary key.
92-
func (e *IndexUsageReporter) ReportPointGetIndexUsageForHandle(tblInfo *model.TableInfo, physicalTableID int64, planID int, kvRequestTotal int64) {
92+
func (e *IndexUsageReporter) ReportPointGetIndexUsageForHandle(tblInfo *model.TableInfo, physicalTableID int64, kvRequestTotal, rows int64) {
9393
idxID, ok := getClusterIndexID(tblInfo)
9494
if !ok {
9595
return
9696
}
9797

98-
e.ReportPointGetIndexUsage(tblInfo.ID, physicalTableID, idxID, planID, kvRequestTotal)
98+
e.ReportPointGetIndexUsage(tblInfo.ID, physicalTableID, idxID, kvRequestTotal, rows)
9999
}
100100

101101
// ReportPointGetIndexUsage reports the index usage of a point get or batch point get
102-
func (e *IndexUsageReporter) ReportPointGetIndexUsage(tableID int64, physicalTableID int64, indexID int64, planID int, kvRequestTotal int64) {
102+
func (e *IndexUsageReporter) ReportPointGetIndexUsage(tableID int64, physicalTableID int64, indexID int64, kvRequestTotal, rows int64) {
103103
tableRowCount, ok := e.getTableRowCount(physicalTableID)
104104
if !ok {
105105
// skip if the table is empty or the stats is not valid
106106
return
107107
}
108108

109-
basic := e.runtimeStatsColl.GetBasicRuntimeStats(planID, false)
110-
if basic == nil {
111-
return
112-
}
113-
accessRows := basic.GetActRows()
114-
115-
sample := indexusage.NewSample(0, uint64(kvRequestTotal), uint64(accessRows), uint64(tableRowCount))
109+
sample := indexusage.NewSample(0, uint64(kvRequestTotal), uint64(rows), uint64(tableRowCount))
116110
e.reporter.Update(tableID, indexID, sample)
117111
}
118112

pkg/executor/internal/exec/indexusage_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ func TestIndexUsageReporter(t *testing.T) {
5252
runtimeStatsColl := sc.RuntimeStatsColl
5353

5454
// For PointGet and BatchPointGet
55-
planID := 3
56-
runtimeStatsColl.GetBasicRuntimeStats(planID, true).Record(time.Second, 2024)
57-
reporter.ReportPointGetIndexUsage(tableID, tableID, indexID, planID, 1)
55+
reporter.ReportPointGetIndexUsage(tableID, tableID, indexID, 1, 2024)
5856

5957
require.Eventually(t, func() bool {
6058
tk.Session().ReportUsageStats()
@@ -63,7 +61,7 @@ func TestIndexUsageReporter(t *testing.T) {
6361
}, time.Second*5, time.Millisecond)
6462

6563
// For Index Scan
66-
planID = 4
64+
planID := 4
6765
rows := uint64(2024)
6866
zero := uint64(0)
6967
executorID := "test-executor"
@@ -87,9 +85,7 @@ func TestIndexUsageReporter(t *testing.T) {
8785
Version: statistics.PseudoVersion,
8886
RealtimeCount: 100,
8987
})
90-
planID = 4
91-
runtimeStatsColl.GetBasicRuntimeStats(planID, true).Record(time.Second, 2024)
92-
reporter.ReportPointGetIndexUsage(tableID, tableID, indexID, planID, 1)
88+
reporter.ReportPointGetIndexUsage(tableID, tableID, indexID, 1, 2024)
9389

9490
require.Eventually(t, func() bool {
9591
tk.Session().ReportUsageStats()

pkg/executor/point_get.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,11 @@ func (e *PointGetExecutor) Close() error {
273273
tableID := e.tblInfo.ID
274274
physicalTableID := GetPhysID(e.tblInfo, e.partitionDefIdx)
275275
kvReqTotal := e.stats.SnapshotRuntimeStats.GetCmdRPCCount(tikvrpc.CmdGet)
276+
rows := e.RuntimeStats().GetActRows()
276277
if e.idxInfo != nil {
277-
e.indexUsageReporter.ReportPointGetIndexUsage(tableID, physicalTableID, e.idxInfo.ID, e.ID(), kvReqTotal)
278+
e.indexUsageReporter.ReportPointGetIndexUsage(tableID, physicalTableID, e.idxInfo.ID, kvReqTotal, rows)
278279
} else {
279-
e.indexUsageReporter.ReportPointGetIndexUsageForHandle(e.tblInfo, physicalTableID, e.ID(), kvReqTotal)
280+
e.indexUsageReporter.ReportPointGetIndexUsageForHandle(e.tblInfo, physicalTableID, kvReqTotal, rows)
280281
}
281282
}
282283
e.done = false

pkg/util/execdetails/execdetails.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ func (e *RuntimeStatsColl) RegisterStats(planID int, info RuntimeStats) {
14981498
}
14991499
}
15001500
if !found {
1501-
stats.groupRss = append(stats.groupRss, info.Clone())
1501+
stats.groupRss = append(stats.groupRss, info)
15021502
}
15031503
}
15041504

0 commit comments

Comments
 (0)