Skip to content

fix(server): allow disabling periodic idle vNPU cleanup to avoid premature reclamation#120

Open
dartagnanli wants to merge 1 commit into
Project-HAMi:mainfrom
dartagnanli:fix/disable-periodic-idle-vnpu-cleanup
Open

fix(server): allow disabling periodic idle vNPU cleanup to avoid premature reclamation#120
dartagnanli wants to merge 1 commit into
Project-HAMi:mainfrom
dartagnanli:fix/disable-periodic-idle-vnpu-cleanup

Conversation

@dartagnanli

@dartagnanli dartagnanli commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

What & Why

Issue #86 reports that the periodic idle vNPU cleanup goroutine (startPeriodicCheckIdleVNPUs) destroys vNPUs based solely on IsContainerUsed == 0. Because a vNPU allocated to a Pod that is already Running but whose container has not yet started using the NPU also reports IsContainerUsed == 0, the cleanup can reclaim the vNPU too early — breaking workloads (e.g. npu-smi info fails inside the container shortly after the Pod starts).

#86 is framed as a discussion and lists several more thorough directions (combining IsContainerUsed with Pod allocation state, adding a protection window, or a Pod-referenced rule like mind-cluster's DestroyNotUsedVNPU). Those require deeper changes to the cleanup policy.

This PR takes a smaller, complementary step: give operators a switch to disable the periodic cleanup goroutine so the premature-reclamation symptom stops on affected nodes without waiting for the larger redesign.

Changes

  • New bool flag --enable_periodic_idle_vnpu_cleanup (default false). When false, Start() does not launch the periodic startPeriodicCheckIdleVNPUs goroutine.
  • The one-shot CleanupIdleVNPUs() on plugin restart (cmd/main.go) is intentionally left unconditional and unchanged — it is a separate entry point. The flag name (enable_periodic_...) and its help text make this boundary explicit to avoid the "total cleanup switch" ambiguity.
  • Wiring threaded through NewPluginServer.

Scope note

This is a mitigation, not a full resolution of #86's cleanup-policy discussion. The flag defaults to false (periodic cleanup disabled) to stop the premature-reclamation symptom by default; setting it to true restores the previous behavior. Reviewers may prefer a different default — easy to adjust.

Testing

  • go build ./internal/server/... ./cmd/...
  • go vet ./internal/server/... ./cmd/...
  • go test ./internal/server/ — 109 passed, consistent with baseline (ran 3×)

Modernizations (in the touched files)

While editing these files I also applied the Go modernization lints the editor flagged. All behavior-preserving; happy to split into a separate PR if preferred:

  • interface{}any
  • wg.Add(1) + go func(){ defer Done() }wg.Go(...)
  • for i := 0; i < n; i++for i := range n
  • reverse index loop → slices.Backward

Refs #86

Summary by CodeRabbit

  • New Features

    • Added an option to enable periodic cleanup of idle vNPU resources.
    • Cleanup is disabled by default and can be activated through the CLI flag.
  • Bug Fixes

    • Improved server goroutine lifecycle management for more reliable startup and shutdown behavior.
  • Tests

    • Updated server tests to cover the new cleanup configuration and restart behavior.

@hami-robot

hami-robot Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dartagnanli
Once this PR has been reviewed and has the lgtm label, please assign archlitchi 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

@hami-robot hami-robot Bot added the size/L label Jul 25, 2026
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@dartagnanli, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 47 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dd1d77fe-1d4f-4824-ab9e-432ceefbae6a

📥 Commits

Reviewing files that changed from the base of the PR and between 34bc10b and 0243445.

📒 Files selected for processing (4)
  • cmd/main.go
  • internal/server/register.go
  • internal/server/server.go
  • internal/server/server_test.go
📝 Walkthrough

Walkthrough

The CLI adds a flag for periodic idle vNPU cleanup. PluginServer accepts and applies the flag, updates stop-channel typing, and uses WaitGroup.Go for goroutine startup. Registration and server tests are updated accordingly.

Changes

Server cleanup lifecycle

Layer / File(s) Summary
Cleanup configuration and server contract
cmd/main.go, internal/server/server.go
The CLI flag is passed into NewPluginServer; the server stores it, conditionally resets and exposes stopCh, and updates related types to any.
Runtime goroutine lifecycle
internal/server/server.go, internal/server/register.go
Periodic cleanup and gRPC serving use ps.wg.Go, while watchAndRegister no longer performs its own WaitGroup.Done; device construction uses for i := range vCount.
Server test alignment
internal/server/server_test.go
Tests adopt the constructor and channel changes, update logger variadics to any, modernize cleanup and loop syntax, and enable periodic cleanup in restart setup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant PluginServer
  participant Cleanup
  participant GRPCServer
  CLI->>PluginServer: pass cleanup flag to NewPluginServer
  PluginServer->>PluginServer: Start with ps.wg.Go
  PluginServer->>Cleanup: start periodic cleanup when enabled
  PluginServer->>GRPCServer: start serving through ps.wg.Go
  GRPCServer-->>PluginServer: stop or restart serving
Loading

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: dsfans2014, ashergaga

Poem

I’m a rabbit with cleanup to do,
Idle vNPUs get swept anew.
Goroutines hop in a managed line,
Stop channels now type just fine.
Tests thump paws and restart bright—
All servers burrow through the night.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: making periodic idle vNPU cleanup optional to prevent premature reclamation.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai
coderabbitai Bot requested a review from ashergaga July 25, 2026 04:24
@coderabbitai coderabbitai Bot added the enhancement New feature or request label Jul 25, 2026
…ature reclamation

The periodic idle vNPU cleanup goroutine (startPeriodicCheckIdleVNPUs) destroys vNPUs whose IsContainerUsed == 0. As discussed in Project-HAMi#86, a vNPU allocated to a Running Pod that has not yet started using the NPU also reports IsContainerUsed == 0, so the cleanup can reclaim it too early and break workloads (e.g. npu-smi info fails inside the container).

Add the --enable_periodic_idle_vnpu_cleanup flag (default false) so operators can disable the periodic cleanup goroutine while the stricter policy is being revisited. The one-shot cleanup on plugin restart (cmd/main.go) is intentionally left unconditional and unchanged.

Also apply Go modernizations in the touched files: interface{} -> any, wg.Add(1)+go -> wg.Go, range over int, slices.Backward.

Refs Project-HAMi#86
@dartagnanli
dartagnanli force-pushed the fix/disable-periodic-idle-vnpu-cleanup branch from 34bc10b to 0243445 Compare July 25, 2026 04:36
@hami-robot

hami-robot Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Thanks for your pull request. Before we can look at it, you'll need to add a 'DCO signoff' to your commits.

📝 Please follow instructions in the contributing guide to update your commits with the DCO

Full details of the Developer Certificate of Origin can be found at developercertificate.org.

The list of commits missing DCO signoff:

  • 0243445 fix(server): allow disabling periodic idle vNPU cleanup to avoid premature reclamation
Details

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-sigs/prow repository. I understand the commands that are listed here.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant