Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: fix panic when getStatsReader fail (#15651) #15711

Merged
merged 3 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
Expand Down Expand Up @@ -284,15 +285,16 @@ func (sc statsCache) update(tables []*statistics.Table, deletedIDs []int64, newV
func (h *Handle) LoadNeededHistograms() (err error) {
cols := statistics.HistogramNeededColumns.AllCols()
reader, err := h.getStatsReader(nil)
if err != nil {
return err
}

defer func() {
err1 := h.releaseStatsReader(reader)
if err1 != nil && err == nil {
err = err1
}
}()
if err != nil {
return err
}

for _, col := range cols {
statsCache := h.statsCache.Load().(statsCache)
Expand Down Expand Up @@ -500,15 +502,15 @@ func (h *Handle) columnStatsFromStorage(reader *statsReader, row chunk.Row, tabl
// tableStatsFromStorage loads table stats info from storage.
func (h *Handle) tableStatsFromStorage(tableInfo *model.TableInfo, physicalID int64, loadAll bool, historyStatsExec sqlexec.RestrictedSQLExecutor) (_ *statistics.Table, err error) {
reader, err := h.getStatsReader(historyStatsExec)
if err != nil {
return nil, err
}
defer func() {
err1 := h.releaseStatsReader(reader)
if err == nil && err1 != nil {
err = err1
}
}()
if err != nil {
return nil, err
}
table, ok := h.statsCache.Load().(statsCache).tables[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 @@ -745,6 +747,11 @@ func (sr *statsReader) isHistory() bool {
}

func (h *Handle) getStatsReader(history sqlexec.RestrictedSQLExecutor) (*statsReader, error) {
failpoint.Inject("mockGetStatsReaderFail", func(val failpoint.Value) {
if val.(bool) {
failpoint.Return(nil, errors.New("gofail genStatsReader error"))
}
})
if history != nil {
return &statsReader{history: history}, nil
}
Expand Down
6 changes: 6 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -455,6 +456,11 @@ func (s *testStatsSuite) TestLoadStats(c *C) {
stat = h.GetTableStats(tableInfo)
hg = stat.Columns[tableInfo.Columns[2].ID].Histogram
c.Assert(hg.Len(), Greater, 0)
// Following test tests whether the LoadNeededHistograms would panic.
c.Assert(failpoint.Enable("github.com/pingcap/tidb/statistics/handle/mockGetStatsReaderFail", `return(true)`), IsNil)
err = h.LoadNeededHistograms()
c.Assert(err, NotNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/statistics/handle/mockGetStatsReaderFail"), IsNil)
}

func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {
Expand Down