Skip to content

Commit 820a41f

Browse files
authored
*: add statement summary for RUv2 and use float64 for calculations (#67223)
ref #67199
1 parent 022ae5b commit 820a41f

17 files changed

Lines changed: 100 additions & 42 deletions

File tree

pkg/executor/adapter.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,10 +1729,22 @@ func (a *ExecStmt) finalizeStatementRUV2Metrics() {
17291729
dctx.RUConsumptionReporter.ReportTiKVRUV2Consumption(dctx.ResourceGroupName, tikvRU)
17301730
}
17311731
if tidbRU > 0 {
1732-
dctx.RUConsumptionReporter.ReportTiDBRUV2Consumption(dctx.ResourceGroupName, float64(tidbRU))
1732+
dctx.RUConsumptionReporter.ReportTiDBRUV2Consumption(dctx.ResourceGroupName, tidbRU)
17331733
}
17341734
}
17351735

1736+
func calculateStatementTotalRUV2(metrics *execdetails.RUV2Metrics, weights execdetails.RUV2Weights, ruDetail *util.RUDetails) float64 {
1737+
var tiKVRU, tiFlashRU float64
1738+
if ruDetail != nil {
1739+
tiKVRU = ruDetail.TiKVRUV2()
1740+
tiFlashRU = ruDetail.TiflashRU()
1741+
}
1742+
if metrics == nil {
1743+
return tiKVRU + tiFlashRU
1744+
}
1745+
return metrics.TotalRU(weights, tiKVRU, tiFlashRU)
1746+
}
1747+
17361748
func (a *ExecStmt) recordLastQueryInfo(err error) {
17371749
sessVars := a.Ctx.GetSessionVars()
17381750
// Record diagnostic information for DML statements
@@ -2176,6 +2188,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
21762188
stmtExecInfo.KeyspaceName = keyspaceName
21772189
stmtExecInfo.KeyspaceID = keyspaceID
21782190
stmtExecInfo.RUDetail = ruDetail
2191+
stmtExecInfo.TotalRUV2 = calculateStatementTotalRUV2(sessVars.RUV2Metrics, sessVars.RUV2Weights(), ruDetail)
21792192
stmtExecInfo.ResourceGroupName = sessVars.StmtCtx.ResourceGroupName
21802193
stmtExecInfo.CPUUsages = sessVars.SQLCPUUsages.GetCPUUsages()
21812194
stmtExecInfo.PlanCacheUnqualified = sessVars.StmtCtx.PlanCacheUnqualified()

pkg/executor/adapter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ func TestFinishExecuteStmtReportsTiDBRUV2WithoutSyncingRUDetails(t *testing.T) {
494494
require.Equal(t, "rg1", reporter.tikvGroup)
495495
require.Equal(t, float64(23456), reporter.tikvRUV2)
496496
require.Equal(t, "rg1", reporter.tidbGroup)
497-
require.Equal(t, float64(expected), reporter.tidbRUV2)
497+
require.Equal(t, expected, reporter.tidbRUV2)
498498
}
499499

500500
func TestSlowLogMaxPerSec(t *testing.T) {

pkg/executor/explain_unit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestExplainAnalyzeInvokeNextAndClose(t *testing.T) {
146146
require.Contains(
147147
t,
148148
ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.GetRootStats(targetPlan.ID()).String(),
149-
fmt.Sprintf("RU:%.2f", float64(metrics.TotalRU(weights, 0, 0))),
149+
fmt.Sprintf("RU:%.2f", metrics.TotalRU(weights, 0, 0)),
150150
)
151151

152152
recordInsertRows2Metrics(ctx.GetSessionVars())

pkg/infoschema/tables.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,8 @@ var tableStatementsSummaryCols = []columnInfo{
14251425
{name: stmtsummary.AvgRequestUnitWriteStr, tp: mysql.TypeDouble, flag: mysql.NotNullFlag | mysql.UnsignedFlag, size: 22, comment: "Average write request-unit cost of these statements"},
14261426
{name: stmtsummary.MaxQueuedRcTimeStr, tp: mysql.TypeLonglong, size: 22, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Max time of waiting for available request-units"},
14271427
{name: stmtsummary.AvgQueuedRcTimeStr, tp: mysql.TypeLonglong, size: 22, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Average time of waiting for available request-units"},
1428+
{name: stmtsummary.MaxRequestUnitV2Str, tp: mysql.TypeDouble, flag: mysql.NotNullFlag | mysql.UnsignedFlag, size: 22, comment: "Max request-unit v2 cost of these statements"},
1429+
{name: stmtsummary.AvgRequestUnitV2Str, tp: mysql.TypeDouble, flag: mysql.NotNullFlag | mysql.UnsignedFlag, size: 22, comment: "Average request-unit v2 cost of these statements"},
14281430
{name: stmtsummary.ResourceGroupName, tp: mysql.TypeVarchar, size: 64, comment: "Bind resource group name"},
14291431
{name: stmtsummary.PlanCacheUnqualifiedStr, tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag, comment: "The number of times that these statements are not supported by the plan cache"},
14301432
{name: stmtsummary.PlanCacheUnqualifiedLastReasonStr, tp: mysql.TypeBlob, size: types.UnspecifiedLength, comment: "The last reason why the statement is not supported by the plan cache"},

pkg/server/conn_stmt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func TestCursorWithParams(t *testing.T) {
200200

201201
require.Equal(t, "rg1", reporter.tidbGroup)
202202
expectedCursorDelta := ruv2Metrics.CalculateRUValues(weights) - baselineTiDBRU
203-
require.Equal(t, float64(expectedCursorDelta), reporter.tidbRUV2)
203+
require.Equal(t, expectedCursorDelta, reporter.tidbRUV2)
204204
require.Equal(t, 0.0, reporter.tikvRUV2)
205205

206206
ruDetails.AddTiKVRUV2(7)

pkg/server/internal/resultset/resultset.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type CursorRUV2Tracker struct {
7373
ruDetails *clientutil.RUDetails
7474
resourceGroupName string
7575
weights execdetails.RUV2Weights
76-
reportedTiDBRU int64
76+
reportedTiDBRU float64
7777
reportedTiKVRUV2 float64
7878
mu sync.Mutex
7979
}
@@ -119,7 +119,7 @@ func (t *CursorRUV2Tracker) reportDelta() {
119119
t.mu.Lock()
120120
defer t.mu.Unlock()
121121

122-
var currentTiDBRU int64
122+
var currentTiDBRU float64
123123
if t.metrics != nil {
124124
currentTiDBRU = t.metrics.CalculateRUValues(t.weights)
125125
}
@@ -133,7 +133,7 @@ func (t *CursorRUV2Tracker) reportDelta() {
133133
t.reporter.ReportTiKVRUV2Consumption(t.resourceGroupName, deltaTiKVRUV2)
134134
}
135135
if deltaTiDBRU := currentTiDBRU - t.reportedTiDBRU; deltaTiDBRU > 0 {
136-
t.reporter.ReportTiDBRUV2Consumption(t.resourceGroupName, float64(deltaTiDBRU))
136+
t.reporter.ReportTiDBRUV2Consumption(t.resourceGroupName, deltaTiDBRU)
137137
}
138138
}
139139

pkg/sessionctx/variable/slow_log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,10 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string {
555555
}
556556
writeSlowLogItem(&buf, SlowLogStorageFromKV, strconv.FormatBool(logItems.StorageKV))
557557
writeSlowLogItem(&buf, SlowLogStorageFromMPP, strconv.FormatBool(logItems.StorageMPP))
558-
var tiKVRU, tiFlashRU int64
558+
var tiKVRU, tiFlashRU float64
559559
if logItems.RUDetails != nil {
560-
tiKVRU = int64(logItems.RUDetails.TiKVRUV2())
561-
tiFlashRU = int64(logItems.RUDetails.TiflashRU())
560+
tiKVRU = logItems.RUDetails.TiKVRUV2()
561+
tiFlashRU = logItems.RUDetails.TiflashRU()
562562
}
563563
if formatted := execdetails.FormatRUV2Metrics(logItems.RUV2Metrics, s.RUV2Weights(), tiKVRU, tiFlashRU); len(formatted) > 0 {
564564
writeSlowLogItem(&buf, SlowLogRUV2Metrics, formatted)

pkg/sessionctx/variable/tests/session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func TestSlowLogFormatIncludesTiFlashRUInRUV2Metrics(t *testing.T) {
417417
logItems.RUDetails.UpdateTiFlash(&rmpb.Consumption{RRU: 20, WRU: 30})
418418

419419
logString := seVar.SlowLogFormat(logItems)
420-
require.Contains(t, logString, "# RUv2_metrics: total_ru:150, tidb_ru:0, tikv_ru:100, tiflash_ru:50")
420+
require.Contains(t, logString, "# RUv2_metrics: total_ru:150.00, tidb_ru:0.00, tikv_ru:100.00, tiflash_ru:50.00")
421421

422422
t.Run("default session weights come from config defaults", func(t *testing.T) {
423423
original := config.GetGlobalConfig()

pkg/util/execdetails/execdetails_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,13 @@ func TestRUV2MetricsSnapshotCalculateRUValues(t *testing.T) {
361361
metrics.AddTiKVStorageProcessedKeysGet(79)
362362

363363
tidbRU := metrics.CalculateRUValues(weights)
364-
tikvRU := int64(157258)
365-
tiflashRU := int64(24680)
364+
tikvRU := float64(157258)
365+
tiflashRU := float64(24680)
366366
totalRU := metrics.TotalRU(weights, tikvRU, tiflashRU)
367-
require.Equal(t, int64(114198), tidbRU)
368-
require.Equal(t, int64(157258), tikvRU)
369-
require.Equal(t, int64(24680), tiflashRU)
370-
require.Equal(t, int64(296136), totalRU)
367+
require.InEpsilon(t, 114198.0, tidbRU, 0.01)
368+
require.InEpsilon(t, 157258.0, tikvRU, 0.01)
369+
require.InEpsilon(t, 24680.0, tiflashRU, 0.01)
370+
require.InEpsilon(t, 296136.0, totalRU, 0.01)
371371

372372
t.Run("zero scale stays zero", func(t *testing.T) {
373373
zeroScaleWeights := weights
@@ -408,10 +408,10 @@ func TestFormatRUV2MetricsIncludesRUValuesFirst(t *testing.T) {
408408

409409
parts := strings.Split(formatted, ", ")
410410
require.Len(t, parts, 7)
411-
require.Equal(t, "total_ru:19983", parts[0])
412-
require.Equal(t, "tidb_ru:8750", parts[1])
413-
require.Equal(t, "tikv_ru:10987", parts[2])
414-
require.Equal(t, "tiflash_ru:246", parts[3])
411+
require.Equal(t, "total_ru:19983.42", parts[0])
412+
require.Equal(t, "tidb_ru:8750.42", parts[1])
413+
require.Equal(t, "tikv_ru:10987.00", parts[2])
414+
require.Equal(t, "tiflash_ru:246.00", parts[3])
415415
}
416416

417417
func TestRURuntimeStatsStringIncludesTiFlashRU(t *testing.T) {

pkg/util/execdetails/runtime_stats.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,18 +1000,18 @@ func (e *RURuntimeStats) String() string {
10001000
return buf.String()
10011001
}
10021002
case "v2":
1003-
var tiKVRU, tiFlashRU int64
1003+
var tiKVRU, tiFlashRU float64
10041004
if e.RUDetails != nil {
1005-
tiKVRU = int64(e.RUDetails.TiKVRUV2())
1006-
tiFlashRU = int64(e.RUDetails.TiflashRU())
1005+
tiKVRU = e.RUDetails.TiKVRUV2()
1006+
tiFlashRU = e.RUDetails.TiflashRU()
10071007
}
10081008
totalRU := e.Metrics.TotalRU(e.Weights, tiKVRU, tiFlashRU)
10091009
if totalRU == 0 {
10101010
return ""
10111011
}
10121012
buf := bytes.NewBuffer(make([]byte, 0, 8))
10131013
buf.WriteString("RU:")
1014-
buf.WriteString(strconv.FormatFloat(float64(totalRU), 'f', 2, 64))
1014+
buf.WriteString(strconv.FormatFloat(totalRU, 'f', 2, 64))
10151015
return buf.String()
10161016
}
10171017
return ""

0 commit comments

Comments
 (0)