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 memory leak for stats (#7864) #7873

Merged
merged 2 commits into from
Oct 11, 2018
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
18 changes: 18 additions & 0 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/coreos/etcd/clientv3"
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/ngaut/pools"
"github.com/ngaut/sync2"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
Expand Down Expand Up @@ -54,6 +55,7 @@ type Domain struct {
privHandle *privileges.Handle
statsHandle unsafe.Pointer
statsLease time.Duration
statsUpdating sync2.AtomicInt32
ddl ddl.DDL
info *InfoSyncer
m sync.Mutex
Expand Down Expand Up @@ -664,6 +666,20 @@ func (do *Domain) CreateStatsHandle(ctx sessionctx.Context) {
atomic.StorePointer(&do.statsHandle, unsafe.Pointer(statistics.NewHandle(ctx, do.statsLease)))
}

// StatsUpdating checks if the stats worker is updating.
func (do *Domain) StatsUpdating() bool {
return do.statsUpdating.Get() > 0
}

// SetStatsUpdating sets the value of stats updating.
func (do *Domain) SetStatsUpdating(val bool) {
if val {
do.statsUpdating.Set(1)
} else {
do.statsUpdating.Set(0)
}
}

// RunAutoAnalyze indicates if this TiDB server starts auto analyze worker and can run auto analyze job.
var RunAutoAnalyze = true

Expand All @@ -680,6 +696,7 @@ func (do *Domain) UpdateTableStatsLoop(ctx sessionctx.Context) error {
}
owner := do.newStatsOwner()
do.wg.Add(1)
do.SetStatsUpdating(true)
go do.updateStatsWorker(ctx, owner)
if RunAutoAnalyze {
do.wg.Add(1)
Expand Down Expand Up @@ -729,6 +746,7 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager)
log.Info("[stats] init stats info takes ", time.Now().Sub(t))
}
defer func() {
do.SetStatsUpdating(false)
recoverInDomain("updateStatsWorker", false)
do.wg.Done()
}()
Expand Down
1 change: 1 addition & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (s *testSuite) SetUpSuite(c *C) {
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
d.SetStatsUpdating(true)
s.domain = d
}

Expand Down
1 change: 1 addition & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (s *testSuite) TestDataForTableStatsField(c *C) {
session.SetStatsLease(0)
do, err := session.BootstrapSession(store)
c.Assert(err, IsNil)
do.SetStatsUpdating(true)
defer do.Close()
oldExpiryTime := infoschema.TableStatsCacheExpiry
infoschema.TableStatsCacheExpiry = 0
Expand Down
1 change: 1 addition & 0 deletions planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {
session.SetSchemaLease(0)
session.SetStatsLease(0)
dom, err := session.BootstrapSession(store)
dom.SetStatsUpdating(true)
return store, dom, errors.Trace(err)
}

Expand Down
1 change: 1 addition & 0 deletions server/statistics_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (ds *testDumpStatsSuite) startServer(c *C) {
session.SetStatsLease(0)
ds.domain, err = session.BootstrapSession(ds.store)
c.Assert(err, IsNil)
ds.domain.SetStatsUpdating(true)
tidbdrv := NewTiDBDriver(ds.store)

cfg := config.NewConfig()
Expand Down
5 changes: 3 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,9 @@ func CreateSession(store kv.Storage) (Session, error) {
}
privilege.BindPrivilegeManager(s, pm)

// Add statsUpdateHandle.
if do.StatsHandle() != nil {
// Add stats collector, and it will be freed by background stats worker
// which periodically updates stats using the collected data.
if do.StatsHandle() != nil && do.StatsUpdating() {
s.statsCollector = do.StatsHandle().NewSessionStatsCollector()
}

Expand Down
1 change: 1 addition & 0 deletions statistics/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,6 @@ func newStoreWithBootstrap(statsLease time.Duration) (kv.Storage, *domain.Domain
session.SetStatsLease(statsLease)
domain.RunAutoAnalyze = false
do, err := session.BootstrapSession(store)
do.SetStatsUpdating(true)
return store, do, errors.Trace(err)
}