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 an unsafe lock operation and adjust some logging levels (#20381) #20424

Merged
merged 7 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 15 additions & 6 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (h *Handle) Update(is infoschema.InfoSchema) error {
tbl, err := h.tableStatsFromStorage(tableInfo, physicalID, false, nil)
// Error is not nil may mean that there are some ddl changes on this table, we will not update it.
if err != nil {
logutil.Logger(context.Background()).Debug("error occurred when read table stats", zap.String("table", tableInfo.Name.O), zap.Error(err))
logutil.Logger(context.Background()).Error("[stats] error occurred when read table stats", zap.String("table", tableInfo.Name.O), zap.Error(err))
continue
}
if tbl == nil {
Expand Down Expand Up @@ -342,14 +342,14 @@ func (h *Handle) FlushStats() {
for len(h.ddlEventCh) > 0 {
e := <-h.ddlEventCh
if err := h.HandleDDLEvent(e); err != nil {
logutil.Logger(context.Background()).Debug("[stats] handle ddl event fail", zap.Error(err))
logutil.Logger(context.Background()).Error("[stats] handle ddl event fail", zap.Error(err))
}
}
if err := h.DumpStatsDeltaToKV(DumpAll); err != nil {
logutil.Logger(context.Background()).Debug("[stats] dump stats delta fail", zap.Error(err))
logutil.Logger(context.Background()).Error("[stats] dump stats delta fail", zap.Error(err))
}
if err := h.DumpStatsFeedbackToKV(); err != nil {
logutil.Logger(context.Background()).Debug("[stats] dump stats feedback fail", zap.Error(err))
logutil.Logger(context.Background()).Error("[stats] dump stats feedback fail", zap.Error(err))
}
}

Expand Down Expand Up @@ -769,7 +769,7 @@ func (sr *statsReader) isHistory() bool {
return sr.history != nil
}

func (h *Handle) getStatsReader(history sqlexec.RestrictedSQLExecutor) (*statsReader, error) {
func (h *Handle) getStatsReader(history sqlexec.RestrictedSQLExecutor) (reader *statsReader, err error) {
failpoint.Inject("mockGetStatsReaderFail", func(val failpoint.Value) {
if val.(bool) {
failpoint.Return(nil, errors.New("gofail genStatsReader error"))
Expand All @@ -779,7 +779,16 @@ func (h *Handle) getStatsReader(history sqlexec.RestrictedSQLExecutor) (*statsRe
return &statsReader{history: history}, nil
}
h.mu.Lock()
_, err := h.mu.ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), "begin")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("getStatsReader panic %v", r)
}
if err != nil {
h.mu.Unlock()
}
}()
failpoint.Inject("mockGetStatsReaderPanic", nil)
_, err = h.mu.ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), "begin")
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,13 @@ func (s *testStatsSuite) TestLoadStats(c *C) {
err = h.LoadNeededHistograms()
c.Assert(err, NotNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/statistics/handle/mockGetStatsReaderFail"), IsNil)

c.Assert(failpoint.Enable("github.com/pingcap/tidb/statistics/handle/mockGetStatsReaderPanic", "panic"), IsNil)
err = h.LoadNeededHistograms()
c.Assert(err, ErrorMatches, ".*getStatsReader panic.*")
c.Assert(failpoint.Disable("github.com/pingcap/tidb/statistics/handle/mockGetStatsReaderPanic"), IsNil)
err = h.LoadNeededHistograms()
c.Assert(err, IsNil)
}

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