Skip to content

Commit

Permalink
fix: high CPU utilization by scheduler while idle
Browse files Browse the repository at this point in the history
resolves project-zot#2155

Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron committed Jan 11, 2024
1 parent 77d6829 commit 449b9ca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions pkg/exporter/api/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func TestNewExporter(t *testing.T) {
baseURL := fmt.Sprintf(BaseURL, serverPort)
servercConfig.HTTP.Port = serverPort
servercConfig.BinaryType = "minimal"
servercConfig.Storage.Dedupe = false
servercConfig.Storage.GC = false
serverController := zotapi.NewController(servercConfig)
So(serverController, ShouldNotBeNil)

Expand Down
14 changes: 9 additions & 5 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (pq *generatorsPriorityQueue) Pop() any {

const (
rateLimiterScheduler = 400
rateLimit = 5 * time.Second
rateLimit = 50 * time.Millisecond
NumWorkersMultiplier = 4
sendMetricsInterval = 5 * time.Second
)
Expand Down Expand Up @@ -241,7 +241,7 @@ func (scheduler *Scheduler) RunScheduler() {
ctx, cancel := context.WithCancel(context.Background())
scheduler.cancelFunc = cancel

throttle := time.NewTicker(rateLimit).C
throttle := time.NewTicker(scheduler.RateLimit).C

numWorkers := scheduler.NumWorkers

Expand Down Expand Up @@ -278,10 +278,14 @@ func (scheduler *Scheduler) RunScheduler() {

task := scheduler.getTask()

if task != nil {
// push tasks into worker pool until workerChan is full.
scheduler.workerChan <- task
if task == nil {
<-throttle

continue
}

// push tasks into worker pool until workerChan is full.
scheduler.workerChan <- task
}
}
}()
Expand Down
9 changes: 5 additions & 4 deletions pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func TestScheduler(t *testing.T) {

genH := &shortGenerator{log: logger, priority: "high priority"}
// interval has to be higher than throttle value to simulate
sch.SubmitGenerator(genH, 6*time.Second, scheduler.HighPriority)
sch.SubmitGenerator(genH, 1*time.Second, scheduler.HighPriority)

sch.RunScheduler()
time.Sleep(7 * time.Second)
time.Sleep(2 * time.Second)
sch.Shutdown()

data, err := os.ReadFile(logFile.Name())
Expand All @@ -152,6 +152,7 @@ func TestScheduler(t *testing.T) {
cfg.Scheduler = &config.SchedulerConfig{NumWorkers: 3}
metrics := monitoring.NewMetricsServer(true, logger)
sch := scheduler.NewScheduler(cfg, metrics, logger)
sch.RateLimit = 5 * time.Second

genL := &generator{log: logger, priority: "low priority"}
sch.SubmitGenerator(genL, time.Duration(0), scheduler.LowPriority)
Expand Down Expand Up @@ -212,7 +213,7 @@ func TestScheduler(t *testing.T) {
sch.SubmitGenerator(genL, 20*time.Millisecond, scheduler.LowPriority)

sch.RunScheduler()
time.Sleep(4 * time.Second)
time.Sleep(1 * time.Second)
sch.Shutdown()

data, err := os.ReadFile(logFile.Name())
Expand Down Expand Up @@ -275,7 +276,7 @@ func TestScheduler(t *testing.T) {
sch.SubmitGenerator(genL, 20*time.Millisecond, scheduler.MediumPriority)

sch.RunScheduler()
time.Sleep(4 * time.Second)
time.Sleep(1 * time.Second)
sch.Shutdown()

data, err := os.ReadFile(logFile.Name())
Expand Down

0 comments on commit 449b9ca

Please sign in to comment.