Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <admin@liudos.us>
  • Loading branch information
lhy1024 committed Sep 18, 2023
1 parent 0888ef6 commit 9a442d1
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/schedule/schedulers/scheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ var denySchedulersByLabelerCounter = labeler.LabelerEventCounter.WithLabelValues
// Controller is used to manage all schedulers.
type Controller struct {
sync.RWMutex
wg sync.WaitGroup
// we cannot use waitGroup here because addScheduler will be called in any time
// but `new Add calls must happen after all previous Wait calls have returned`
// so we need to use a counter to avoid this
count atomic.Int32
ctx context.Context
cluster sche.SchedulerCluster
storage endpoint.ConfigStorage
Expand All @@ -67,7 +70,9 @@ func NewController(ctx context.Context, cluster sche.SchedulerCluster, storage e

// Wait waits on all schedulers to exit.
func (c *Controller) Wait() {
c.wg.Wait()
for c.count.Load() > 0 {
time.Sleep(10 * time.Millisecond)
}
}

// GetScheduler returns a schedule controller by name.
Expand Down Expand Up @@ -184,7 +189,7 @@ func (c *Controller) AddScheduler(scheduler Scheduler, args ...string) error {
return err
}

c.wg.Add(1)
c.count.Add(1)
go c.runScheduler(s)
c.schedulers[s.Scheduler.GetName()] = s
c.cluster.GetSchedulerConfig().AddSchedulerCfg(s.Scheduler.GetType(), args)
Expand Down Expand Up @@ -320,7 +325,7 @@ func (c *Controller) IsSchedulerExisted(name string) (bool, error) {

func (c *Controller) runScheduler(s *ScheduleController) {
defer logutil.LogPanic()
defer c.wg.Done()
defer c.count.Add(-1)
defer s.Scheduler.Cleanup(c.cluster)

ticker := time.NewTicker(s.GetInterval())
Expand Down

0 comments on commit 9a442d1

Please sign in to comment.