Skip to content

Commit

Permalink
schedule: avoid data race about scheduler controller
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 d7d4756 commit 9bc6843
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/schedule/schedulers/scheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var denySchedulersByLabelerCounter = labeler.LabelerEventCounter.WithLabelValues
type Controller struct {
sync.RWMutex
wg sync.WaitGroup
wgLock sync.Mutex
ctx context.Context
cluster sche.SchedulerCluster
storage endpoint.ConfigStorage
Expand All @@ -67,7 +68,9 @@ func NewController(ctx context.Context, cluster sche.SchedulerCluster, storage e

// Wait waits on all schedulers to exit.
func (c *Controller) Wait() {
c.wgLock.Lock()
c.wg.Wait()
c.wgLock.Unlock()
}

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

c.wgLock.Lock()
c.wg.Add(1)
c.wgLock.Unlock()

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

func (c *Controller) runScheduler(s *ScheduleController) {
defer logutil.LogPanic()
defer c.wg.Done()
defer func() {
c.wgLock.Lock()
c.wg.Done()
c.wgLock.Unlock()
}()
defer s.Scheduler.Cleanup(c.cluster)

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

0 comments on commit 9bc6843

Please sign in to comment.