Skip to content

Commit

Permalink
statistics: check Killed in the TableStatsFromStorage (#47568) (#47642)
Browse files Browse the repository at this point in the history
close #47570
  • Loading branch information
ti-chi-bot authored Oct 18, 2023
1 parent 288af37 commit 3ad7a56
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ func (h *Handle) fmSketchFromStorage(reader *statsReader, tblID int64, isIndex,
return statistics.DecodeFMSketch(rows[0].GetBytes(0))
}

func (h *Handle) indexStatsFromStorage(reader *statsReader, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool) error {
func (h *Handle) indexStatsFromStorage(reader *statsReader, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool, tracker *memory.Tracker) error {
histID := row.GetInt64(2)
distinct := row.GetInt64(3)
histVer := row.GetUint64(4)
Expand Down Expand Up @@ -1292,14 +1292,17 @@ func (h *Handle) indexStatsFromStorage(reader *statsReader, row chunk.Row, table
break
}
if idx != nil {
if tracker != nil {
tracker.Consume(idx.MemoryUsage().TotalMemoryUsage())
}
table.Indices[histID] = idx
} else {
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))
}
return nil
}

func (h *Handle) columnStatsFromStorage(reader *statsReader, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool) error {
func (h *Handle) columnStatsFromStorage(reader *statsReader, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool, tracker *memory.Tracker) error {
histID := row.GetInt64(2)
distinct := row.GetInt64(3)
histVer := row.GetUint64(4)
Expand Down Expand Up @@ -1402,6 +1405,9 @@ func (h *Handle) columnStatsFromStorage(reader *statsReader, row chunk.Row, tabl
break
}
if col != nil {
if tracker != nil {
tracker.Consume(col.MemoryUsage().TotalMemoryUsage())
}
table.Columns[col.ID] = col
} else {
// If we didn't find a Column or Index in tableInfo, we won't load the histogram for it.
Expand All @@ -1424,6 +1430,9 @@ func (h *Handle) TableStatsFromStorage(tableInfo *model.TableInfo, physicalID in
err = err1
}
}()
tracker := memory.NewTracker(memory.LabelForAnalyzeMemory, -1)
tracker.AttachTo(h.mu.ctx.GetSessionVars().MemTracker)
defer tracker.Detach()
table, ok := h.statsCache.Load().(statsCache).Get(physicalID)
// If table stats is pseudo, we also need to copy it, since we will use the column stats when
// the average error rate of it is small.
Expand Down Expand Up @@ -1456,10 +1465,13 @@ func (h *Handle) TableStatsFromStorage(tableInfo *model.TableInfo, physicalID in
return nil, nil
}
for _, row := range rows {
if atomic.LoadUint32(&h.mu.ctx.GetSessionVars().Killed) == 1 {
return nil, errors.Trace(statistics.ErrQueryInterrupted)
}
if row.GetInt64(1) > 0 {
err = h.indexStatsFromStorage(reader, row, table, tableInfo, loadAll)
err = h.indexStatsFromStorage(reader, row, table, tableInfo, loadAll, tracker)
} else {
err = h.columnStatsFromStorage(reader, row, table, tableInfo, loadAll)
err = h.columnStatsFromStorage(reader, row, table, tableInfo, loadAll, tracker)
}
if err != nil {
return nil, err
Expand Down

0 comments on commit 3ad7a56

Please sign in to comment.