Skip to content

Comments

fix: guard preemptive reclaim by realCapability#5053

Open
FAUST-BENCHOU wants to merge 1 commit intovolcano-sh:masterfrom
FAUST-BENCHOU:fix/preemptiveFn
Open

fix: guard preemptive reclaim by realCapability#5053
FAUST-BENCHOU wants to merge 1 commit intovolcano-sh:masterfrom
FAUST-BENCHOU:fix/preemptiveFn

Conversation

@FAUST-BENCHOU
Copy link
Contributor

What type of PR is this?

What this PR does / why we need it:

Which issue(s) this PR fixes:

part of #5048
Bug3:preemptiveFn does not check queue capability which allows reclaim above capability/realCapability

Special notes for your reviewer:

preemptiveFn is a closure and not exported, so it cannot be tested; the test only reproduces the logic of "capability first, then deserved".

Signed-off-by: zhoujinyu <2319109590@qq.com>
Copilot AI review requested due to automatic review settings February 21, 2026 16:50
@volcano-sh-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign shinytang6 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link

Summary of Changes

Hello @FAUST-BENCHOU, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a bug in the scheduler's capacity plugin where the preemptiveFn could allow resource preemption even if it resulted in a queue exceeding its realCapability. The change introduces a crucial check to ensure that preemption decisions always respect the realCapability limit, preventing resource over-allocation and maintaining system stability.

Highlights

  • Preemptive Reclaim Guard: Implemented a new check within the preemptiveFn to prevent tasks from reclaiming resources if their future resource usage would exceed the queue's realCapability, thus enforcing resource limits more strictly.
  • Capability Test Case: Added TestPreemptiveFn_RespectsCapability to validate that the preemption logic correctly honors the realCapability constraint, ensuring that preemption is blocked when this limit is reached.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • pkg/scheduler/plugins/capacity/capacity.go
    • Added a realCapability check in preemptiveFn to ensure futureUsed resources do not exceed the queue's realCapability before allowing preemption.
    • Included logging and metrics updates when a queue is deemed overused due to this check.
  • pkg/scheduler/plugins/capacity/capacity_test.go
    • Introduced TestPreemptiveFn_RespectsCapability to specifically test the new realCapability preemption guard.
Activity
  • No specific human activity (comments, reviews) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@volcano-sh-bot volcano-sh-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 21, 2026
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a critical fix to the preemptiveFn in the capacity plugin, ensuring that preemptive reclaim operations respect the realCapability of a queue. Previously, the function only checked against deserved resources, potentially leading to over-reclamation beyond the queue's actual capacity. The change adds a LessEqualWithDimensionAndResourcesName check against realCapability before proceeding with the deserved check. A new test case TestPreemptiveFn_RespectsCapability has been added to validate this behavior. The changes are well-contained and directly address the identified bug.

Comment on lines +183 to +188
if allocatable, _ := futureUsed.LessEqualWithDimensionAndResourcesName(attr.realCapability, task.Resreq); !allocatable {
klog.V(3).Infof("Queue <%v> cannot reclaim for <%s/%s> because futureUsed <%v> exceeds realCapability <%v>.",
queue.Name, task.Namespace, task.Name, futureUsed, attr.realCapability)
metrics.UpdateQueueOverused(attr.name, true)
return false
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This new check correctly guards against reclaiming beyond the realCapability. It's crucial for maintaining resource limits and preventing a queue from consuming more resources than it's allowed, even if its deserved resources are higher. This directly addresses the bug described in the PR description.

Comment on lines +962 to +1010
func TestPreemptiveFn_RespectsCapability(t *testing.T) {
// Queue has deserved and capability both {cpu: 8, mem: 100Gi}, allocated {cpu: 7, mem: 90Gi}.
// A task requesting {cpu: 2, mem: 5Gi} would keep memory below deserved but push CPU above capability.
// preemptiveFn should reject preemption in this case.

deserved := api.NewResource(api.BuildResourceList("8", "100Gi"))
allocated := api.NewResource(api.BuildResourceList("7", "90Gi"))
capability := api.NewResource(api.BuildResourceList("8", "100Gi"))

attr := &queueAttr{
queueID: "q-test",
name: "q-test",
deserved: deserved,
allocated: allocated,
capability: capability,
realCapability: capability.Clone(),
}

taskRes := api.NewResource(api.BuildResourceList("2", "5Gi"))
task := &api.TaskInfo{
Resreq: taskRes,
InitResreq: taskRes,
}

queue := &api.QueueInfo{
UID: "q-test",
Name: "q-test",
}

futureUsed := attr.allocated.Clone().Add(task.Resreq)
if allocatable, _ := futureUsed.LessEqualWithDimensionAndResourcesName(attr.realCapability, task.Resreq); allocatable {
t.Fatalf("precondition error: futureUsed %v should exceed capability %v", futureUsed, attr.realCapability)
}

// Simulate the logic in preemptiveFn after our fix: first capability, then deserved.
allocatable, _ := futureUsed.LessEqualWithDimensionAndResourcesName(attr.realCapability, task.Resreq)
if allocatable {
t.Fatalf("expected allocatable=false when futureUsed exceeds capability, got true")
}

// For completeness, check that even though memory is below deserved, we still don't allow reclaim
// because capability check fails.
isPreemptive, _ := futureUsed.LessEqualPartlyWithDimensionZeroFiltered(attr.deserved, task.Resreq)
if isPreemptive {
t.Logf("deserved check alone would allow reclaim, but capability check must block it")
}

_ = queue // queue is only needed to mirror function signature; logic above is independent of it.
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new test case TestPreemptiveFn_RespectsCapability effectively validates the fix. It sets up a scenario where the futureUsed resources would exceed realCapability but not deserved, and correctly asserts that preemption is rejected. This is a good, targeted test for the introduced change.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Bug #3 from issue #5048 by adding a capability check to the preemptiveFn in the capacity plugin. The bug allowed queues to reclaim resources above their capability/realCapability because the preemptiveFn only checked against the deserved value, not the capability limit.

Changes:

  • Adds an early capability check in preemptiveFn that blocks preemption if futureUsed would exceed realCapability
  • Adds a test that verifies the logic (though not the actual preemptiveFn callback, which is unexported)

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/scheduler/plugins/capacity/capacity.go Adds capability check before deserved check in preemptiveFn to prevent reclaim above capacity
pkg/scheduler/plugins/capacity/capacity_test.go Adds test verifying the capability-first-then-deserved logic for preemption decisions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +183 to +188
if allocatable, _ := futureUsed.LessEqualWithDimensionAndResourcesName(attr.realCapability, task.Resreq); !allocatable {
klog.V(3).Infof("Queue <%v> cannot reclaim for <%s/%s> because futureUsed <%v> exceeds realCapability <%v>.",
queue.Name, task.Namespace, task.Name, futureUsed, attr.realCapability)
metrics.UpdateQueueOverused(attr.name, true)
return false
}
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The metrics.UpdateQueueOverused call at line 186 creates a duplicate metric update issue. When the capability check fails, this sets overused=true and returns. However, if the capability check passes but the deserved check fails, line 202 will update the same metric again with overused=!isPreemptive. This means the metric can be updated twice for different reasons in a single invocation.

Consider removing the metric update from line 186 and only keeping the one at line 202, or restructure the logic to ensure the metric is updated only once at the end of the function based on the final decision. The metric should reflect whether the queue is overused, not intermediate check failures.

Copilot uses AI. Check for mistakes.
@FAUST-BENCHOU
Copy link
Contributor Author

/retest

@volcano-sh-bot
Copy link
Contributor

@FAUST-BENCHOU: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

Details

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@FAUST-BENCHOU
Copy link
Contributor Author

/assign @hajnalmt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants