skypilot icon indicating copy to clipboard operation
skypilot copied to clipboard

[Pools] Prioritize Idle Workers for Scale Down

Open lloyd-brown opened this issue 1 month ago • 5 comments

This PR addresses a problem where changing the number of workers in a pool leads to us downing workers that are actively running jobs. Here is the working example: we have two workers and there is only a job running on worker 2, we want to scale the number of workers down to 1, ideally we would only tear down worker 1 and leave worker 2 to finish its job, this does not work, currently we tear down both workers in order to update to the new service version that has 2 workers.

Changes made

  • The first change we made is a simple modification to the autoscaler to also consider the number of jobs running on a worker when scaling down the number of replicas. This PR changes the selection logic when scaling down workers to prioritize idle workers. We add another sort criteria so that we first sort by replica status, then version, then number of running jobs, then replica ID.
    • This alone does not suffice, with only this change we will still update the version and scale down worker 2 because it is “outdated”
  • Instead of creating a new version for --workers updates (which do not allow a new YAML so the only thing that can be changing is the number of workers) we introduce a new call in the serve controller to directly change the number of replicas in the autoscaler.
    • Why do we do this?
      • By changing the version the autoscaler will terminate the existing replicas and schedule new ones which will remove any existing jobs.
    • A side effect of this approach is that changing the number of workers is now faster
    • I could also see this being useful for SkyServe if a user just wants to quickly set the number of replicas manually via CLI

Backwards compatibility:

If we don’t have support for updating replicas directly we will fall back to the old logic of creating a new version with the updated number of workers, losing the ability to scale without cancelling jobs.

I added several unit tests to make sure

  • We prioritize replicas without running jobs
  • We don't change the existing logic when the job counts are the same
  • We value Status and Version more than the job count

I added a smoke test that makes sure when we scale down a pool that has 2 workers and only 1 of them is running a job we always scale down the one that is idle.

Tested (run the relevant ones):

  • [ ] Code formatting: install pre-commit (auto-check on commit) or bash format.sh
  • [ ] Any manual or new tests for this PR (please specify below)
  • [ ] All smoke tests: /smoke-test (CI) or pytest tests/test_smoke.py (local)
  • [ ] Relevant individual tests: /smoke-test -k test_name (CI) or pytest tests/test_smoke.py::test_name (local)
  • [ ] Backward compatibility: /quicktest-core (CI) or pytest tests/smoke_tests/test_backward_compat.py (local)

lloyd-brown avatar Dec 09 '25 19:12 lloyd-brown

Summary of Changes

Hello @lloyd-brown, I'm Gemini Code Assist[^1]! 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 enhances the worker pool autoscaling mechanism by introducing a more intelligent selection process for scaling down workers. The primary goal is to minimize disruption and optimize resource utilization by ensuring that workers with active jobs are retained over idle ones when a scale-down event occurs. This change refines the existing sorting criteria to explicitly account for the number of running jobs on each worker, making the autoscaler more efficient and job-aware.

Highlights

  • Prioritized Scale Down: The worker selection logic for scaling down pools now prioritizes idle workers (those with fewer running jobs).
  • New Sorting Criteria: The sorting mechanism for selecting workers to scale down has been updated to consider replica status, then version, then the number of running jobs, and finally replica ID.
  • Comprehensive Testing: New unit tests were added to verify the job count prioritization, ensure existing logic for equal job counts is preserved, and confirm that status and version priorities are still respected. A smoke test was also introduced to validate the idle worker prioritization in a real-world scenario.
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 :thumbsup: and :thumbsdown: 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.

[^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.

gemini-code-assist[bot] avatar Dec 09 '25 19:12 gemini-code-assist[bot]

/smoke-test

lloyd-brown avatar Dec 09 '25 20:12 lloyd-brown

/smoke-test -k test_pool_scale_down_with_job_count_priority --kubernetes /smoke-test -k test_pool_scale_down_with_job_count_priority --kubernetes --grpc /smoke-test -k test_pool_scale_down_with_job_count_priority --gcp

lloyd-brown avatar Dec 09 '25 23:12 lloyd-brown

/smoke-test

lloyd-brown avatar Dec 10 '25 00:12 lloyd-brown

Tests only failing due to issue fixed in https://github.com/skypilot-org/skypilot/pull/8256 and resource unavailability.

lloyd-brown avatar Dec 10 '25 02:12 lloyd-brown