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

domain: fix stats worker cannot recover from panic (#9072) #9085

Merged
merged 2 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ func (do *Domain) newStatsOwner() owner.Manager {
}

func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) {
defer recoverInDomain("updateStatsWorker", false)
lease := do.statsLease
deltaUpdateDuration := lease * 20
loadTicker := time.NewTicker(lease)
Expand All @@ -865,7 +866,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager)
}
defer func() {
do.SetStatsUpdating(false)
recoverInDomain("updateStatsWorker", false)
do.wg.Done()
}()
for {
Expand Down Expand Up @@ -922,11 +922,11 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager)
}

func (do *Domain) autoAnalyzeWorker(owner owner.Manager) {
defer recoverInDomain("autoAnalyzeWorker", false)
statsHandle := do.StatsHandle()
analyzeTicker := time.NewTicker(do.statsLease)
defer analyzeTicker.Stop()
defer func() {
recoverInDomain("autoAnalyzeWorker", false)
analyzeTicker.Stop()
do.wg.Done()
}()
for {
Expand Down
12 changes: 12 additions & 0 deletions domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testleak"
dto "github.com/prometheus/client_model/go"
)

func TestT(t *testing.T) {
Expand Down Expand Up @@ -127,6 +129,16 @@ func (*testSuite) TestT(c *C) {
c.Assert(*res[0], Equals, SlowQueryInfo{SQL: "ccc", Duration: 2 * time.Second})
c.Assert(*res[1], Equals, SlowQueryInfo{SQL: "bbb", Duration: 3 * time.Second})

metrics.PanicCounter.Reset()
// Since the stats lease is 0 now, so create a new ticker will panic.
// Test that they can recover from panic correctly.
dom.updateStatsWorker(ctx, nil)
dom.autoAnalyzeWorker(nil)
counter := metrics.PanicCounter.WithLabelValues(metrics.LabelDomain)
pb := &dto.Metric{}
counter.Write(pb)
c.Assert(pb.GetCounter().GetValue(), Equals, float64(2))

err = store.Close()
c.Assert(err, IsNil)
}