Skip to content

Commit

Permalink
log: Show disk usage of a query in slow query and statement summary p…
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyj committed Apr 28, 2020
1 parent 51dabaa commit 7382714
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
copTaskInfo := sessVars.StmtCtx.CopTasksDetails()
statsInfos := plannercore.GetStatsInfo(a.Plan)
memMax := sessVars.StmtCtx.MemTracker.MaxConsumed()
diskMax := sessVars.StmtCtx.DiskTracker.MaxConsumed()
_, digest := sessVars.StmtCtx.SQLDigest()
_, planDigest := getPlanDigest(a.Ctx, a.Plan)
slowItems := &variable.SlowQueryLogItems{
Expand All @@ -838,6 +839,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
CopTasks: copTaskInfo,
ExecDetail: execDetail,
MemMax: memMax,
DiskMax: diskMax,
Succ: succ,
Plan: getPlanTree(a.Plan),
PlanDigest: planDigest,
Expand Down Expand Up @@ -944,6 +946,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
execDetail := stmtCtx.GetExecDetails()
copTaskInfo := stmtCtx.CopTasksDetails()
memMax := stmtCtx.MemTracker.MaxConsumed()
diskMax := stmtCtx.DiskTracker.MaxConsumed()
var userString string
if sessVars.User != nil {
userString = sessVars.User.Username
Expand All @@ -967,6 +970,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
CopTasks: copTaskInfo,
ExecDetail: &execDetail,
MemMax: memMax,
DiskMax: diskMax,
StartTime: sessVars.StartTime,
IsInternal: sessVars.InRestrictedSQL,
Succeed: succ,
Expand Down
4 changes: 4 additions & 0 deletions executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ type slowQueryTuple struct {
maxWaitTime float64
maxWaitAddress string
memMax int64
diskMax int64
prevStmt string
sql string
isInternal bool
Expand Down Expand Up @@ -452,6 +453,8 @@ func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string,
st.maxWaitAddress = value
case variable.SlowLogMemMax:
st.memMax, err = strconv.ParseInt(value, 10, 64)
case variable.SlowLogDiskMax:
st.diskMax, err = strconv.ParseInt(value, 10, 64)
case variable.SlowLogSucc:
st.succ, err = strconv.ParseBool(value)
case variable.SlowLogPlan:
Expand Down Expand Up @@ -511,6 +514,7 @@ func (st *slowQueryTuple) convertToDatumRow() []types.Datum {
record = append(record, types.NewFloat64Datum(st.maxWaitTime))
record = append(record, types.NewStringDatum(st.maxWaitAddress))
record = append(record, types.NewIntDatum(st.memMax))
record = append(record, types.NewIntDatum(st.diskMax))
if st.succ {
record = append(record, types.NewIntDatum(1))
} else {
Expand Down
3 changes: 3 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ var slowQueryCols = []columnInfo{
{name: variable.SlowLogCopWaitMax, tp: mysql.TypeDouble, size: 22},
{name: variable.SlowLogCopWaitAddr, tp: mysql.TypeVarchar, size: 64},
{name: variable.SlowLogMemMax, tp: mysql.TypeLonglong, size: 20},
{name: variable.SlowLogDiskMax, tp: mysql.TypeLonglong, size: 20},
{name: variable.SlowLogSucc, tp: mysql.TypeTiny, size: 1},
{name: variable.SlowLogPlan, tp: mysql.TypeLongBlob, size: types.UnspecifiedLength},
{name: variable.SlowLogPlanDigest, tp: mysql.TypeVarchar, size: 128},
Expand Down Expand Up @@ -1094,6 +1095,8 @@ var tableStatementsSummaryCols = []columnInfo{
{name: "BACKOFF_TYPES", tp: mysql.TypeVarchar, size: 1024, comment: "Types of errors and the number of retries for each type"},
{name: "AVG_MEM", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Average memory(byte) used"},
{name: "MAX_MEM", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Max memory(byte) used"},
{name: "AVG_DISK", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Average disk(byte) used"},
{name: "MAX_DISK", tp: mysql.TypeLonglong, size: 20, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Max disk(byte) used"},
{name: "AVG_AFFECTED_ROWS", tp: mysql.TypeDouble, size: 22, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "Average number of rows affected"},
{name: "FIRST_SEEN", tp: mysql.TypeTimestamp, size: 26, flag: mysql.NotNullFlag, comment: "The time these statements are seen for the first time"},
{name: "LAST_SEEN", tp: mysql.TypeTimestamp, size: 26, flag: mysql.NotNullFlag, comment: "The time these statements are seen for the last time"},
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,8 @@ const (
SlowLogCopBackoffPrefix = "Cop_backoff_"
// SlowLogMemMax is the max number bytes of memory used in this statement.
SlowLogMemMax = "Mem_max"
// SlowLogDiskMax is the max number bytes of disk used in this statement.
SlowLogDiskMax = "Disk_max"
// SlowLogPrepared is used to indicate whether this sql execute in prepare.
SlowLogPrepared = "Prepared"
// SlowLogHasMoreResults is used to indicate whether this sql has more following results.
Expand Down Expand Up @@ -1513,6 +1515,7 @@ type SlowQueryLogItems struct {
CopTasks *stmtctx.CopTasksDetails
ExecDetail execdetails.ExecDetails
MemMax int64
DiskMax int64
Succ bool
Prepared bool
HasMoreResults bool
Expand Down Expand Up @@ -1642,6 +1645,9 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string {
if logItems.MemMax > 0 {
writeSlowLogItem(&buf, SlowLogMemMax, strconv.FormatInt(logItems.MemMax, 10))
}
if logItems.DiskMax > 0 {
writeSlowLogItem(&buf, SlowLogDiskMax, strconv.FormatInt(logItems.DiskMax, 10))
}

writeSlowLogItem(&buf, SlowLogPrepared, strconv.FormatBool(logItems.Prepared))
writeSlowLogItem(&buf, SlowLogHasMoreResults, strconv.FormatBool(logItems.HasMoreResults))
Expand Down
7 changes: 6 additions & 1 deletion sessionctx/variable/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ func (*testSessionSuite) TestSlowLogFormat(c *C) {
copTasks.TotBackoffTimes[backoff] = 200
}

var memMax int64 = 2333
var (
memMax int64 = 2333
diskMax int64 = 2333
)
resultString := `# Txn_start_ts: 406649736972468225
# User: [email protected]
# Conn_ID: 1
Expand All @@ -190,6 +193,7 @@ func (*testSessionSuite) TestSlowLogFormat(c *C) {
# Cop_backoff_rpcPD_total_times: 200 Cop_backoff_rpcPD_total_time: 0.2 Cop_backoff_rpcPD_max_time: 0.2 Cop_backoff_rpcPD_max_addr: 127.0.0.1 Cop_backoff_rpcPD_avg_time: 0.2 Cop_backoff_rpcPD_p90_time: 0.2
# Cop_backoff_rpcTiKV_total_times: 200 Cop_backoff_rpcTiKV_total_time: 0.2 Cop_backoff_rpcTiKV_max_time: 0.2 Cop_backoff_rpcTiKV_max_addr: 127.0.0.1 Cop_backoff_rpcTiKV_avg_time: 0.2 Cop_backoff_rpcTiKV_p90_time: 0.2
# Mem_max: 2333
# Disk_max: 2333
# Prepared: true
# Has_more_results: true
# Succ: true
Expand All @@ -208,6 +212,7 @@ select * from t;`
CopTasks: copTasks,
ExecDetail: execDetail,
MemMax: memMax,
DiskMax: diskMax,
Prepared: true,
HasMoreResults: true,
Succ: true,
Expand Down
9 changes: 9 additions & 0 deletions util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ type stmtSummaryByDigestElement struct {
// other
sumMem int64
maxMem int64
sumDisk int64
maxDisk int64
sumAffectedRows uint64
// The first time this type of SQL executes.
firstSeen time.Time
Expand All @@ -189,6 +191,7 @@ type StmtExecInfo struct {
CopTasks *stmtctx.CopTasksDetails
ExecDetail *execdetails.ExecDetails
MemMax int64
DiskMax int64
StartTime time.Time
IsInternal bool
Succeed bool
Expand Down Expand Up @@ -727,6 +730,10 @@ func (ssElement *stmtSummaryByDigestElement) add(sei *StmtExecInfo, intervalSeco
if sei.MemMax > ssElement.maxMem {
ssElement.maxMem = sei.MemMax
}
ssElement.sumDisk += sei.DiskMax
if sei.DiskMax > ssElement.maxDisk {
ssElement.maxDisk = sei.DiskMax
}
if sei.StartTime.Before(ssElement.firstSeen) {
ssElement.firstSeen = sei.StartTime
}
Expand Down Expand Up @@ -812,6 +819,8 @@ func (ssElement *stmtSummaryByDigestElement) toDatum(ssbd *stmtSummaryByDigest)
formatBackoffTypes(ssElement.backoffTypes),
avgInt(ssElement.sumMem, ssElement.execCount),
ssElement.maxMem,
avgInt(ssElement.sumDisk, ssElement.execCount),
ssElement.maxDisk,
avgFloat(int64(ssElement.sumAffectedRows), ssElement.execCount),
types.NewTime(types.FromGoTime(ssElement.firstSeen), mysql.TypeTimestamp, 0),
types.NewTime(types.FromGoTime(ssElement.lastSeen), mysql.TypeTimestamp, 0),
Expand Down
10 changes: 9 additions & 1 deletion util/stmtsummary/statement_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func (s *testStmtSummarySuite) TestAddStatement(c *C) {
backoffTypes: make(map[fmt.Stringer]int),
sumMem: stmtExecInfo1.MemMax,
maxMem: stmtExecInfo1.MemMax,
sumDisk: stmtExecInfo1.DiskMax,
maxDisk: stmtExecInfo1.DiskMax,
sumAffectedRows: stmtExecInfo1.StmtCtx.AffectedRows(),
firstSeen: stmtExecInfo1.StartTime,
lastSeen: stmtExecInfo1.StartTime,
Expand Down Expand Up @@ -204,6 +206,7 @@ func (s *testStmtSummarySuite) TestAddStatement(c *C) {
IndexNames: indexes,
},
MemMax: 20000,
DiskMax: 20000,
StartTime: time.Date(2019, 1, 1, 10, 10, 20, 10, time.UTC),
Succeed: true,
}
Expand Down Expand Up @@ -254,6 +257,8 @@ func (s *testStmtSummarySuite) TestAddStatement(c *C) {
expectedSummaryElement.backoffTypes[tikv.BoTxnLock] = 1
expectedSummaryElement.sumMem += stmtExecInfo2.MemMax
expectedSummaryElement.maxMem = stmtExecInfo2.MemMax
expectedSummaryElement.sumDisk += stmtExecInfo2.DiskMax
expectedSummaryElement.maxDisk = stmtExecInfo2.DiskMax
expectedSummaryElement.sumAffectedRows += stmtExecInfo2.StmtCtx.AffectedRows()
expectedSummaryElement.lastSeen = stmtExecInfo2.StartTime

Expand Down Expand Up @@ -469,6 +474,8 @@ func matchStmtSummaryByDigest(first, second *stmtSummaryByDigest) bool {
ssElement1.sumBackoffTimes != ssElement2.sumBackoffTimes ||
ssElement1.sumMem != ssElement2.sumMem ||
ssElement1.maxMem != ssElement2.maxMem ||
ssElement1.sumDisk != ssElement2.sumDisk ||
ssElement1.maxDisk != ssElement2.maxDisk ||
ssElement1.sumAffectedRows != ssElement2.sumAffectedRows ||
ssElement1.firstSeen != ssElement2.firstSeen ||
ssElement1.lastSeen != ssElement2.lastSeen {
Expand Down Expand Up @@ -564,6 +571,7 @@ func generateAnyExecInfo() *StmtExecInfo {
IndexNames: indexes,
},
MemMax: 10000,
DiskMax: 10000,
StartTime: time.Date(2019, 1, 1, 10, 10, 10, 10, time.UTC),
Succeed: true,
}
Expand Down Expand Up @@ -605,7 +613,7 @@ func (s *testStmtSummarySuite) TestToDatum(c *C) {
stmtExecInfo1.ExecDetail.CommitDetail.WriteSize, stmtExecInfo1.ExecDetail.CommitDetail.WriteSize,
stmtExecInfo1.ExecDetail.CommitDetail.PrewriteRegionNum, stmtExecInfo1.ExecDetail.CommitDetail.PrewriteRegionNum,
stmtExecInfo1.ExecDetail.CommitDetail.TxnRetry, stmtExecInfo1.ExecDetail.CommitDetail.TxnRetry, 1,
"txnLock:1", stmtExecInfo1.MemMax, stmtExecInfo1.MemMax, stmtExecInfo1.StmtCtx.AffectedRows(),
"txnLock:1", stmtExecInfo1.MemMax, stmtExecInfo1.MemMax, stmtExecInfo1.DiskMax, stmtExecInfo1.DiskMax, stmtExecInfo1.StmtCtx.AffectedRows(),
t, t, stmtExecInfo1.OriginalSQL, stmtExecInfo1.PrevSQL, "plan_digest", ""}
match(c, datums[0], expectedDatum...)

Expand Down

0 comments on commit 7382714

Please sign in to comment.