Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preempt based on flavor order #844

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apis/kueue/v1beta1/clusterqueue_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ const (
PreemptionPolicyLowerOrNewerEqualPriority PreemptionPolicy = "LowerOrNewerEqualPriority"
)

type FlavorFungibilityPolicy string

const (
Borrow FlavorFungibilityPolicy = "Borrow"
Preempt FlavorFungibilityPolicy = "Preempt"
TryNextFlavor FlavorFungibilityPolicy = "TryNextFlavor"
)

type FlavorFungibility struct {
// +kubebuilder:validation:Enum="Borrow,TryNextFlavor"
WhenCanBorrow FlavorFungibilityPolicy `json:"whenCanBorrow"`
// +kubebuilder:validation:Enum="Preempt,TryNextFlavor"
WhenCanPreempt FlavorFungibilityPolicy `json:"whenCanPreempt"`
}

// ClusterQueuePreemption contains policies to preempt Workloads from this
// ClusterQueue or the ClusterQueue's cohort.
type ClusterQueuePreemption struct {
Expand Down Expand Up @@ -276,6 +291,8 @@ type ClusterQueuePreemption struct {
// +kubebuilder:default=Never
// +kubebuilder:validation:Enum=Never;LowerPriority;LowerOrNewerEqualPriority
WithinClusterQueue PreemptionPolicy `json:"withinClusterQueue,omitempty"`

FlavorFungibility FlavorFungibility `json:"flavorFungibility"`
}

//+kubebuilder:object:root=true
Expand Down
3 changes: 3 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/kueue/apis/kueue/v1beta1"
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
utilindexer "sigs.k8s.io/kueue/pkg/controller/core/indexer"
"sigs.k8s.io/kueue/pkg/metrics"
Expand Down Expand Up @@ -141,6 +142,8 @@ type ClusterQueue struct {
Preemption kueue.ClusterQueuePreemption
Status metrics.ClusterQueueStatus

FlavorFungibility v1beta1.FlavorFungibility

// The following fields are not populated in a snapshot.

// Key is localQueue's key (namespace/name).
Expand Down
26 changes: 22 additions & 4 deletions pkg/scheduler/flavorassigner/flavorassigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/component-helpers/scheduling/corev1/nodeaffinity"
"k8s.io/utils/pointer"

"sigs.k8s.io/kueue/apis/kueue/v1beta1"
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/pkg/cache"
"sigs.k8s.io/kueue/pkg/workload"
Expand Down Expand Up @@ -339,6 +340,9 @@ func (a *Assignment) findFlavorForResourceGroup(
var bestAssignment ResourceAssignment
bestAssignmentMode := NoFit

policyBorrow := cq.FlavorFungibility.WhenCanBorrow
policyPreempt := cq.FlavorFungibility.WhenCanPreempt

// We will only check against the flavors' labels for the resource.
selector := flavorSelector(spec, rg.LabelKeys)
for _, flvQuotas := range rg.Flavors {
Expand All @@ -364,6 +368,7 @@ func (a *Assignment) findFlavorForResourceGroup(
continue
}

whetherNeedBorrowing := false
assignments := make(ResourceAssignment, len(requests))
// Calculate representativeMode for this assignment as the worst mode among all requests.
representativeMode := Fit
Expand All @@ -376,6 +381,7 @@ func (a *Assignment) findFlavorForResourceGroup(
}
if mode < representativeMode {
representativeMode = mode
whetherNeedBorrowing = whetherNeedBorrowing || borrow > 0
}
if representativeMode == NoFit {
// The flavor doesn't fit, no need to check other resources.
Expand All @@ -389,13 +395,25 @@ func (a *Assignment) findFlavorForResourceGroup(
}
}

if representativeMode == Preempt && policyPreempt == v1beta1.Preempt {
return assignments, nil
}

if representativeMode == Fit && whetherNeedBorrowing && policyBorrow == v1beta1.Borrow {
return assignments, nil
}

if representativeMode == Fit && !whetherNeedBorrowing {
return assignments, nil
}

if representativeMode > bestAssignmentMode {
bestAssignment = assignments
bestAssignmentMode = representativeMode
if bestAssignmentMode == Fit {
// All the resources fit in the cohort, no need to check more flavors.
return bestAssignment, nil
}
// if bestAssignmentMode == Fit {
// // All the resources fit in the cohort, no need to check more flavors.
// return bestAssignment, nil
// }
}
}
return bestAssignment, status
Expand Down