Skip to content

Commit 44cb05f

Browse files
alivxxxngaut
authored andcommitted
domain: fix stats updating bug when no stats woker (#7864) (#7871)
1 parent 173a792 commit 44cb05f

File tree

7 files changed

+31
-5
lines changed

7 files changed

+31
-5
lines changed

domain/domain.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/grpc-ecosystem/go-grpc-prometheus"
2626
"github.com/juju/errors"
2727
"github.com/ngaut/pools"
28+
"github.com/ngaut/sync2"
2829
"github.com/pingcap/tidb/ddl"
2930
"github.com/pingcap/tidb/infoschema"
3031
"github.com/pingcap/tidb/kv"
@@ -52,6 +53,7 @@ type Domain struct {
5253
privHandle *privileges.Handle
5354
statsHandle unsafe.Pointer
5455
statsLease time.Duration
56+
statsUpdating sync2.AtomicInt32
5557
ddl ddl.DDL
5658
m sync.Mutex
5759
SchemaValidator SchemaValidator
@@ -592,6 +594,20 @@ func (do *Domain) CreateStatsHandle(ctx sessionctx.Context) {
592594
atomic.StorePointer(&do.statsHandle, unsafe.Pointer(statistics.NewHandle(ctx, do.statsLease)))
593595
}
594596

597+
// StatsUpdating checks if the stats worker is updating.
598+
func (do *Domain) StatsUpdating() bool {
599+
return do.statsUpdating.Get() > 0
600+
}
601+
602+
// SetStatsUpdating sets the value of stats updating.
603+
func (do *Domain) SetStatsUpdating(val bool) {
604+
if val {
605+
do.statsUpdating.Set(1)
606+
} else {
607+
do.statsUpdating.Set(0)
608+
}
609+
}
610+
595611
// RunAutoAnalyze indicates if this TiDB server starts auto analyze worker and can run auto analyze job.
596612
var RunAutoAnalyze = true
597613

@@ -608,6 +624,7 @@ func (do *Domain) UpdateTableStatsLoop(ctx sessionctx.Context) error {
608624
}
609625
owner := do.newStatsOwner()
610626
do.wg.Add(1)
627+
do.SetStatsUpdating(true)
611628
go do.updateStatsWorker(ctx, owner)
612629
if RunAutoAnalyze {
613630
do.wg.Add(1)
@@ -656,7 +673,11 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager)
656673
} else {
657674
log.Info("[stats] init stats info takes ", time.Now().Sub(t))
658675
}
659-
defer recoverInDomain("updateStatsWorker", false)
676+
defer func() {
677+
do.SetStatsUpdating(false)
678+
recoverInDomain("updateStatsWorker", false)
679+
do.wg.Done()
680+
}()
660681
for {
661682
select {
662683
case <-loadTicker.C:
@@ -665,7 +686,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager)
665686
log.Debug("[stats] update stats info fail: ", errors.ErrorStack(err))
666687
}
667688
case <-do.exit:
668-
do.wg.Done()
669689
return
670690
// This channel is sent only by ddl owner or the drop stats executor.
671691
case t := <-statsHandle.DDLEventCh():

executor/analyze.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ func (e *AnalyzeExec) Next(ctx context.Context, chk *chunk.Chunk) error {
6969
close(taskCh)
7070
dom := domain.GetDomain(e.ctx)
7171
lease := dom.StatsHandle().Lease
72-
if lease > 0 {
72+
// The analyze result will be consumed by background stats worker.
73+
if lease > 0 && dom.StatsUpdating() {
7374
var err1 error
7475
for i := 0; i < len(e.tasks); i++ {
7576
result := <-resultCh

executor/executor_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (s *testSuite) SetUpSuite(c *C) {
107107
}
108108
d, err := session.BootstrapSession(s.store)
109109
c.Assert(err, IsNil)
110+
d.SetStatsUpdating(true)
110111
s.domain = d
111112
}
112113

infoschema/tables_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (s *testSuite) TestDataForTableRowsCountField(c *C) {
2929
defer store.Close()
3030
do, err := session.BootstrapSession(store)
3131
c.Assert(err, IsNil)
32+
do.SetStatsUpdating(true)
3233
defer do.Close()
3334

3435
h := do.StatsHandle()

plan/cbo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {
632632
session.SetSchemaLease(0)
633633
session.SetStatsLease(0)
634634
dom, err := session.BootstrapSession(store)
635+
dom.SetStatsUpdating(true)
635636
return store, dom, errors.Trace(err)
636637
}
637638

session/session.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,9 @@ func CreateSession(store kv.Storage) (Session, error) {
11041104
}
11051105
privilege.BindPrivilegeManager(s, pm)
11061106

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

statistics/handle_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,5 +445,6 @@ func newStoreWithBootstrap(statsLease time.Duration) (kv.Storage, *domain.Domain
445445
session.SetStatsLease(statsLease)
446446
domain.RunAutoAnalyze = false
447447
do, err := session.BootstrapSession(store)
448+
do.SetStatsUpdating(true)
448449
return store, do, errors.Trace(err)
449450
}

0 commit comments

Comments
 (0)