generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 228
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
Distinguish between Preemption due to reclamation and fair sharing #2411
Merged
k8s-ci-robot
merged 6 commits into
kubernetes-sigs:main
from
epam:detailed-preemption-condition
Jul 10, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27f12e8
Distinguish between Preemption due to reclamation and fair sharing
vladikkuzn 3b0785d
Distinguish between Preemption due to reclamation and fair sharing
vladikkuzn 50768c4
Distinguish between Preemption due to reclamation and fair sharing
vladikkuzn cad0de8
Distinguish between Preemption due to reclamation and fair sharing
vladikkuzn 7327a1e
Distinguish between Preemption due to reclamation and fair sharing
vladikkuzn f62b1bb
Distinguish between Preemption due to reclamation and fair sharing
vladikkuzn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,8 +96,13 @@ func candidatesFromCQOrUnderThreshold(candidates []*workload.Info, clusterQueue | |
return result | ||
} | ||
|
||
type Target struct { | ||
WorkloadInfo *workload.Info | ||
Reason string | ||
} | ||
|
||
// GetTargets returns the list of workloads that should be evicted in order to make room for wl. | ||
func (p *Preemptor) GetTargets(log logr.Logger, wl workload.Info, assignment flavorassigner.Assignment, snapshot *cache.Snapshot) []*workload.Info { | ||
func (p *Preemptor) GetTargets(log logr.Logger, wl workload.Info, assignment flavorassigner.Assignment, snapshot *cache.Snapshot) []*Target { | ||
resPerFlv := resourcesRequiringPreemption(assignment) | ||
cq := snapshot.ClusterQueues[wl.ClusterQueue] | ||
|
||
|
@@ -164,35 +169,47 @@ func canBorrowWithinCohort(cq *cache.ClusterQueueSnapshot, wl *kueue.Workload) ( | |
return true, &threshold | ||
} | ||
|
||
// In cluster queue preemption reasons | ||
const ( | ||
InClusterQueueReason string = "InClusterQueue" | ||
) | ||
|
||
// In cohort preemption reasons | ||
const ( | ||
InCohortReclamationReason string = "InCohortReclamation" | ||
InCohortFairSharingReason string = "InCohortFairSharing" | ||
InCohortReclaimWhileBorrowingReason string = "InCohortReclaimWhileBorrowing" | ||
) | ||
|
||
var HumanReadablePreemptionReasons = map[string]string{ | ||
InClusterQueueReason: "prioritization in the ClusterQueue", | ||
InCohortReclamationReason: "reclamation within the cohort", | ||
InCohortFairSharingReason: "fair sharing within the cohort", | ||
InCohortReclaimWhileBorrowingReason: "reclamation within the cohort while borrowing", | ||
} | ||
|
||
// IssuePreemptions marks the target workloads as evicted. | ||
func (p *Preemptor) IssuePreemptions(ctx context.Context, preemptor *workload.Info, targets []*workload.Info, cq *cache.ClusterQueueSnapshot) (int, error) { | ||
func (p *Preemptor) IssuePreemptions(ctx context.Context, preemptor *workload.Info, targets []*Target, cq *cache.ClusterQueueSnapshot) (int, error) { | ||
log := ctrl.LoggerFrom(ctx) | ||
errCh := routine.NewErrorChannel() | ||
ctx, cancel := context.WithCancel(ctx) | ||
var successfullyPreempted int64 | ||
defer cancel() | ||
workqueue.ParallelizeUntil(ctx, parallelPreemptions, len(targets), func(i int) { | ||
target := targets[i] | ||
if !meta.IsStatusConditionTrue(target.Obj.Status.Conditions, kueue.WorkloadEvicted) { | ||
origin := "ClusterQueue" | ||
reason := "InClusterQueue" | ||
if cq.Name != target.ClusterQueue { | ||
origin = "cohort" | ||
reason = "InCohort" | ||
} | ||
|
||
message := fmt.Sprintf("Preempted to accommodate a workload (UID: %s) in the %s", preemptor.Obj.UID, origin) | ||
err := p.applyPreemption(ctx, target.Obj, reason, message) | ||
if !meta.IsStatusConditionTrue(target.WorkloadInfo.Obj.Status.Conditions, kueue.WorkloadEvicted) { | ||
message := fmt.Sprintf("Preempted to accommodate a workload (UID: %s) due to %s", preemptor.Obj.UID, HumanReadablePreemptionReasons[target.Reason]) | ||
err := p.applyPreemption(ctx, target.WorkloadInfo.Obj, target.Reason, message) | ||
if err != nil { | ||
errCh.SendErrorWithCancel(err, cancel) | ||
return | ||
} | ||
|
||
log.V(3).Info("Preempted", "targetWorkload", klog.KObj(target.Obj), "reason", reason, "message", message) | ||
p.recorder.Eventf(target.Obj, corev1.EventTypeNormal, "Preempted", message) | ||
metrics.ReportEvictedWorkloads(target.ClusterQueue, kueue.WorkloadEvictedByPreemption) | ||
log.V(3).Info("Preempted", "targetWorkload", klog.KObj(target.WorkloadInfo.Obj), "reason", target.Reason, "message", message) | ||
p.recorder.Eventf(target.WorkloadInfo.Obj, corev1.EventTypeNormal, "Preempted", message) | ||
metrics.ReportEvictedWorkloads(target.WorkloadInfo.ClusterQueue, kueue.WorkloadEvictedByPreemption) | ||
} else { | ||
log.V(3).Info("Preemption ongoing", "targetWorkload", klog.KObj(target.Obj)) | ||
log.V(3).Info("Preemption ongoing", "targetWorkload", klog.KObj(target.WorkloadInfo.Obj)) | ||
} | ||
atomic.AddInt64(&successfullyPreempted, 1) | ||
}) | ||
|
@@ -214,35 +231,45 @@ func (p *Preemptor) applyPreemptionWithSSA(ctx context.Context, w *kueue.Workloa | |
// Once the Workload fits, the heuristic tries to add Workloads back, in the | ||
// reverse order in which they were removed, while the incoming Workload still | ||
// fits. | ||
func minimalPreemptions(log logr.Logger, wlReq resources.FlavorResourceQuantitiesFlat, cq *cache.ClusterQueueSnapshot, snapshot *cache.Snapshot, resPerFlv resourcesPerFlavor, candidates []*workload.Info, allowBorrowing bool, allowBorrowingBelowPriority *int32) []*workload.Info { | ||
func minimalPreemptions(log logr.Logger, wlReq resources.FlavorResourceQuantitiesFlat, cq *cache.ClusterQueueSnapshot, snapshot *cache.Snapshot, resPerFlv resourcesPerFlavor, candidates []*workload.Info, allowBorrowing bool, allowBorrowingBelowPriority *int32) []*Target { | ||
if logV := log.V(5); logV.Enabled() { | ||
logV.Info("Simulating preemption", "candidates", workload.References(candidates), "resourcesRequiringPreemption", resPerFlv, "allowBorrowing", allowBorrowing, "allowBorrowingBelowPriority", allowBorrowingBelowPriority) | ||
} | ||
// Simulate removing all candidates from the ClusterQueue and cohort. | ||
var targets []*workload.Info | ||
var targets []*Target | ||
fits := false | ||
for _, candWl := range candidates { | ||
candCQ := snapshot.ClusterQueues[candWl.ClusterQueue] | ||
if cq != candCQ && !cqIsBorrowing(candCQ, resPerFlv) { | ||
sameCq := cq == candCQ | ||
reason := InClusterQueueReason | ||
if !sameCq && !cqIsBorrowing(candCQ, resPerFlv) { | ||
continue | ||
} | ||
if cq != candCQ && allowBorrowingBelowPriority != nil && priority.Priority(candWl.Obj) >= *allowBorrowingBelowPriority { | ||
// We set allowBorrowing=false if there is a candidate with priority | ||
// exceeding allowBorrowingBelowPriority added to targets. | ||
// | ||
// We need to be careful mutating allowBorrowing. We rely on the | ||
// fact that once there is a candidate exceeding the priority added | ||
// to targets, then at least one such candidate is present in the | ||
// final set of targets (after the second phase of the function). | ||
// | ||
// This is true, because the candidates are ordered according | ||
// to priorities (from lowest to highest, using candidatesOrdering), | ||
// and the last added target is not removed in the second phase of | ||
// the function. | ||
allowBorrowing = false | ||
if !sameCq { | ||
reason = InCohortReclamationReason | ||
if allowBorrowingBelowPriority != nil && priority.Priority(candWl.Obj) >= *allowBorrowingBelowPriority { | ||
// We set allowBorrowing=false if there is a candidate with priority | ||
// exceeding allowBorrowingBelowPriority added to targets. | ||
// | ||
// We need to be careful mutating allowBorrowing. We rely on the | ||
// fact that once there is a candidate exceeding the priority added | ||
// to targets, then at least one such candidate is present in the | ||
// final set of targets (after the second phase of the function). | ||
// | ||
// This is true, because the candidates are ordered according | ||
// to priorities (from lowest to highest, using candidatesOrdering), | ||
// and the last added target is not removed in the second phase of | ||
// the function. | ||
allowBorrowing = false | ||
} else { | ||
reason = InCohortReclaimWhileBorrowingReason | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a bug here. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should be more like if allowBorrowingBelowPriority != nil {
if priority.Priority(candWl.Obj) >= *allowBorrowingBelowPriority {
allowBorrowing = false
} else {
reason = InCohortReclaimWhileBorrowingReason
}
} |
||
} | ||
} | ||
snapshot.RemoveWorkload(candWl) | ||
targets = append(targets, candWl) | ||
targets = append(targets, &Target{ | ||
WorkloadInfo: candWl, | ||
Reason: reason, | ||
}) | ||
if workloadFits(wlReq, cq, allowBorrowing) { | ||
fits = true | ||
break | ||
|
@@ -257,24 +284,24 @@ func minimalPreemptions(log logr.Logger, wlReq resources.FlavorResourceQuantitie | |
return targets | ||
} | ||
|
||
func fillBackWorkloads(targets []*workload.Info, wlReq resources.FlavorResourceQuantitiesFlat, cq *cache.ClusterQueueSnapshot, snapshot *cache.Snapshot, allowBorrowing bool) []*workload.Info { | ||
func fillBackWorkloads(targets []*Target, wlReq resources.FlavorResourceQuantitiesFlat, cq *cache.ClusterQueueSnapshot, snapshot *cache.Snapshot, allowBorrowing bool) []*Target { | ||
// In the reverse order, check if any of the workloads can be added back. | ||
for i := len(targets) - 2; i >= 0; i-- { | ||
snapshot.AddWorkload(targets[i]) | ||
snapshot.AddWorkload(targets[i].WorkloadInfo) | ||
if workloadFits(wlReq, cq, allowBorrowing) { | ||
// O(1) deletion: copy the last element into index i and reduce size. | ||
targets[i] = targets[len(targets)-1] | ||
targets = targets[:len(targets)-1] | ||
} else { | ||
snapshot.RemoveWorkload(targets[i]) | ||
snapshot.RemoveWorkload(targets[i].WorkloadInfo) | ||
} | ||
} | ||
return targets | ||
} | ||
|
||
func restoreSnapshot(snapshot *cache.Snapshot, targets []*workload.Info) { | ||
func restoreSnapshot(snapshot *cache.Snapshot, targets []*Target) { | ||
for _, t := range targets { | ||
snapshot.AddWorkload(t) | ||
snapshot.AddWorkload(t.WorkloadInfo) | ||
} | ||
} | ||
|
||
|
@@ -309,15 +336,15 @@ func parseStrategies(s []config.PreemptionStrategy) []fsStrategy { | |
return strategies | ||
} | ||
|
||
func (p *Preemptor) fairPreemptions(log logr.Logger, wl *workload.Info, assignment flavorassigner.Assignment, snapshot *cache.Snapshot, resPerFlv resourcesPerFlavor, candidates []*workload.Info, allowBorrowingBelowPriority *int32) []*workload.Info { | ||
func (p *Preemptor) fairPreemptions(log logr.Logger, wl *workload.Info, assignment flavorassigner.Assignment, snapshot *cache.Snapshot, resPerFlv resourcesPerFlavor, candidates []*workload.Info, allowBorrowingBelowPriority *int32) []*Target { | ||
if logV := log.V(5); logV.Enabled() { | ||
logV.Info("Simulating fair preemption", "candidates", workload.References(candidates), "resourcesRequiringPreemption", resPerFlv, "allowBorrowingBelowPriority", allowBorrowingBelowPriority) | ||
} | ||
cqHeap := cqHeapFromCandidates(candidates, false, snapshot) | ||
nominatedCQ := snapshot.ClusterQueues[wl.ClusterQueue] | ||
wlReq := assignment.TotalRequestsFor(wl) | ||
newNominatedShareValue, _ := nominatedCQ.DominantResourceShareWith(wlReq) | ||
var targets []*workload.Info | ||
var targets []*Target | ||
fits := false | ||
var retryCandidates []*workload.Info | ||
for cqHeap.Len() > 0 && !fits { | ||
|
@@ -326,7 +353,10 @@ func (p *Preemptor) fairPreemptions(log logr.Logger, wl *workload.Info, assignme | |
if candCQ.cq == nominatedCQ { | ||
candWl := candCQ.workloads[0] | ||
snapshot.RemoveWorkload(candWl) | ||
targets = append(targets, candWl) | ||
targets = append(targets, &Target{ | ||
WorkloadInfo: candWl, | ||
Reason: InClusterQueueReason, | ||
}) | ||
if workloadFits(wlReq, nominatedCQ, true) { | ||
fits = true | ||
break | ||
|
@@ -343,9 +373,18 @@ func (p *Preemptor) fairPreemptions(log logr.Logger, wl *workload.Info, assignme | |
for i, candWl := range candCQ.workloads { | ||
belowThreshold := allowBorrowingBelowPriority != nil && priority.Priority(candWl.Obj) < *allowBorrowingBelowPriority | ||
newCandShareVal, _ := candCQ.cq.DominantResourceShareWithout(candWl.FlavorResourceUsage()) | ||
if belowThreshold || p.fsStrategies[0](newNominatedShareValue, candCQ.share, newCandShareVal) { | ||
strategy := p.fsStrategies[0](newNominatedShareValue, candCQ.share, newCandShareVal) | ||
if belowThreshold || strategy { | ||
snapshot.RemoveWorkload(candWl) | ||
targets = append(targets, candWl) | ||
reason := InCohortFairSharingReason | ||
if !strategy { | ||
reason = InCohortReclaimWhileBorrowingReason | ||
} | ||
|
||
targets = append(targets, &Target{ | ||
WorkloadInfo: candWl, | ||
Reason: reason, | ||
}) | ||
if workloadFits(wlReq, nominatedCQ, true) { | ||
fits = true | ||
break | ||
|
@@ -374,7 +413,10 @@ func (p *Preemptor) fairPreemptions(log logr.Logger, wl *workload.Info, assignme | |
// The criteria doesn't depend on the preempted workload, so just preempt the first candidate. | ||
candWl := candCQ.workloads[0] | ||
snapshot.RemoveWorkload(candWl) | ||
targets = append(targets, candWl) | ||
targets = append(targets, &Target{ | ||
WorkloadInfo: candWl, | ||
Reason: InCohortFairSharingReason, | ||
}) | ||
if workloadFits(wlReq, nominatedCQ, true) { | ||
fits = true | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also please add
targetClusterQueue
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vladikkuzn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in #2538
WDYT should I also add preemptor's cluster queue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The preemptor's information is already part of the log (it comes from the scheduler.go file, which adds the fields to the contextual log).