Skip to content

Commit

Permalink
Introduce the structure for preemptedWorkloads (#4447)
Browse files Browse the repository at this point in the history
  • Loading branch information
mimowo authored Mar 3, 2025
1 parent d135c1d commit f17779d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
39 changes: 39 additions & 0 deletions pkg/scheduler/preempted_workloads.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scheduler

import (
"sigs.k8s.io/kueue/pkg/scheduler/preemption"
"sigs.k8s.io/kueue/pkg/workload"
)

type preemptedWorkloads map[string]*workload.Info

func (p preemptedWorkloads) hasAny(newTargets []*preemption.Target) bool {
for _, target := range newTargets {
if _, found := p[workload.Key(target.WorkloadInfo.Obj)]; found {
return true
}
}
return false
}

func (p preemptedWorkloads) insert(newTargets []*preemption.Target) {
for _, target := range newTargets {
p[workload.Key(target.WorkloadInfo.Obj)] = target.WorkloadInfo
}
}
11 changes: 3 additions & 8 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/clock"
Expand Down Expand Up @@ -210,7 +209,7 @@ func (s *Scheduler) schedule(ctx context.Context) wait.SpeedSignal {
// This is because there can be other workloads deeper in a clusterQueue whose
// head got admitted that should be scheduled in the cohort before the heads
// of other clusterQueues.
preemptedWorkloads := sets.New[string]()
preemptedWorkloads := make(preemptedWorkloads)
skippedPreemptions := make(map[string]int)
for i := range entries {
e := &entries[i]
Expand All @@ -233,11 +232,7 @@ func (s *Scheduler) schedule(ctx context.Context) wait.SpeedSignal {
}

// We skip multiple-preemptions per cohort if any of the targets are overlapping
pendingPreemptions := make([]string, 0, len(e.preemptionTargets))
for _, target := range e.preemptionTargets {
pendingPreemptions = append(pendingPreemptions, workload.Key(target.WorkloadInfo.Obj))
}
if preemptedWorkloads.HasAny(pendingPreemptions...) {
if preemptedWorkloads.hasAny(e.preemptionTargets) {
setSkipped(e, "Workload has overlapping preemption targets with another workload")
skippedPreemptions[cq.Name]++
continue
Expand All @@ -251,7 +246,7 @@ func (s *Scheduler) schedule(ctx context.Context) wait.SpeedSignal {
}
continue
}
preemptedWorkloads.Insert(pendingPreemptions...)
preemptedWorkloads.insert(e.preemptionTargets)
cq.AddUsage(usage)

if e.assignment.RepresentativeMode() == flavorassigner.Preempt {
Expand Down

0 comments on commit f17779d

Please sign in to comment.