@@ -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.
596612var 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 ():
0 commit comments