Skip to content

Commit f1670ba

Browse files
authored
statistics: check Killed in the TableStatsFromStorage (#47568)
close #47570
1 parent 14d3cb5 commit f1670ba

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

statistics/handle/storage/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ go_library(
3030
"//util/compress",
3131
"//util/logutil",
3232
"//util/mathutil",
33+
"//util/memory",
3334
"//util/sqlexec",
3435
"@com_github_klauspost_compress//gzip",
3536
"@com_github_pingcap_errors//:errors",

statistics/handle/storage/read.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"strconv"
21+
"sync/atomic"
2122
"time"
2223

2324
"github.com/pingcap/errors"
@@ -35,6 +36,7 @@ import (
3536
"github.com/pingcap/tidb/util/chunk"
3637
"github.com/pingcap/tidb/util/logutil"
3738
"github.com/pingcap/tidb/util/mathutil"
39+
"github.com/pingcap/tidb/util/memory"
3840
"github.com/pingcap/tidb/util/sqlexec"
3941
"go.uber.org/zap"
4042
)
@@ -220,7 +222,7 @@ func ExtendedStatsFromStorage(sctx sessionctx.Context, table *statistics.Table,
220222
return table, nil
221223
}
222224

223-
func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool, lease time.Duration) error {
225+
func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool, lease time.Duration, tracker *memory.Tracker) error {
224226
histID := row.GetInt64(2)
225227
distinct := row.GetInt64(3)
226228
histVer := row.GetUint64(4)
@@ -293,14 +295,17 @@ func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statis
293295
break
294296
}
295297
if idx != nil {
298+
if tracker != nil {
299+
tracker.Consume(idx.MemoryUsage().TotalMemoryUsage())
300+
}
296301
table.Indices[histID] = idx
297302
} else {
298303
logutil.BgLogger().Debug("we cannot find index id in table info. It may be deleted.", zap.Int64("indexID", histID), zap.String("table", tableInfo.Name.O))
299304
}
300305
return nil
301306
}
302307

303-
func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool, lease time.Duration) error {
308+
func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool, lease time.Duration, tracker *memory.Tracker) error {
304309
histID := row.GetInt64(2)
305310
distinct := row.GetInt64(3)
306311
histVer := row.GetUint64(4)
@@ -396,6 +401,9 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
396401
break
397402
}
398403
if col != nil {
404+
if tracker != nil {
405+
tracker.Consume(col.MemoryUsage().TotalMemoryUsage())
406+
}
399407
table.Columns[col.ID] = col
400408
} else {
401409
// If we didn't find a Column or Index in tableInfo, we won't load the histogram for it.
@@ -408,6 +416,9 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
408416

409417
// TableStatsFromStorage loads table stats info from storage.
410418
func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *model.TableInfo, tableID int64, loadAll bool, lease time.Duration, table *statistics.Table) (_ *statistics.Table, err error) {
419+
tracker := memory.NewTracker(memory.LabelForAnalyzeMemory, -1)
420+
tracker.AttachTo(sctx.GetSessionVars().MemTracker)
421+
defer tracker.Detach()
411422
// If table stats is pseudo, we also need to copy it, since we will use the column stats when
412423
// the average error rate of it is small.
413424
if table == nil || snapshot > 0 {
@@ -439,10 +450,13 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
439450
return nil, nil
440451
}
441452
for _, row := range rows {
453+
if atomic.LoadUint32(&sctx.GetSessionVars().Killed) == 1 {
454+
return nil, errors.Trace(statistics.ErrQueryInterrupted)
455+
}
442456
if row.GetInt64(1) > 0 {
443-
err = indexStatsFromStorage(sctx, row, table, tableInfo, loadAll, lease)
457+
err = indexStatsFromStorage(sctx, row, table, tableInfo, loadAll, lease, tracker)
444458
} else {
445-
err = columnStatsFromStorage(sctx, row, table, tableInfo, loadAll, lease)
459+
err = columnStatsFromStorage(sctx, row, table, tableInfo, loadAll, lease, tracker)
446460
}
447461
if err != nil {
448462
return nil, err

0 commit comments

Comments
 (0)