Skip to content

Commit

Permalink
ddl: simply refactor the code called "needCheckClusterState" (#45660)
Browse files Browse the repository at this point in the history
ref #39751
  • Loading branch information
zimulala committed Jul 31, 2023
1 parent 06ea55d commit 6a2672c
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions ddl/job_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (d *ddl) startDispatchLoop() {
notifyDDLJobByEtcdCh = d.etcdCli.Watch(d.ctx, addingDDLJobConcurrent)
}
ticker := time.NewTicker(dispatchLoopWaitingDuration)
if err := d.doCheckClusterState(false); err != nil {
if err := d.checkAndUpdateClusterState(true); err != nil {
logutil.BgLogger().Fatal("dispatch loop get cluster state failed, it should not happen, please try restart TiDB", zap.Error(err))
}
defer ticker.Stop()
Expand Down Expand Up @@ -294,7 +294,7 @@ func (d *ddl) startDispatchLoop() {
case <-d.ctx.Done():
return
}
if err := d.needCheckClusterState(isOnce); err != nil {
if err := d.checkAndUpdateClusterState(isOnce); err != nil {
continue
}
isOnce = false
Expand All @@ -303,23 +303,17 @@ func (d *ddl) startDispatchLoop() {
}
}

func (d *ddl) needCheckClusterState(mustCheck bool) error {
func (d *ddl) checkAndUpdateClusterState(needUpdate bool) error {
select {
case _, ok := <-d.stateSyncer.WatchChan():
return d.doCheckClusterState(!ok)
if !ok {
d.stateSyncer.Rewatch(d.ctx)
}
default:
if mustCheck {
return d.doCheckClusterState(false)
if !needUpdate {
return nil
}
}
return nil
}

func (d *ddl) doCheckClusterState(needRewatch bool) error {
if needRewatch {
d.stateSyncer.Rewatch(d.ctx)
return nil
}

oldState := d.stateSyncer.IsUpgradingState()
stateInfo, err := d.stateSyncer.GetGlobalState(d.ctx)
Expand Down

0 comments on commit 6a2672c

Please sign in to comment.