Skip to content

fix(http_proxy): keep round-robin position across requests - #6177

Merged
gitlawr merged 2 commits into
gpustack:mainfrom
bakiburakogun:fix/roundrobin-instance-ids
Sep 22, 2026
Merged

gitlawr merged 2 commits into
gpustack:mainfrom
bakiburakogun:fix/roundrobin-instance-ids

Conversation

@bakiburakogun

Copy link
Copy Markdown
Contributor

RoundRobinStrategy.select_instance decides whether to rebuild its itertools.cycle by comparing the ModelInstance objects from the previous call with the ones passed now. Every request fetches fresh objects from the database, and any field that changed in between (heartbeat data, restart counters, state) makes the lists compare unequal, so the cycle is recreated and starts from the first instance again.

On a two-replica reranker deployment (GPUStack v2.2.3) this showed up as very uneven splits under bursty load: 37 requests went 27/10, another batch 21/16. Over long periods it evens out, which is probably why it went unnoticed.

This change tracks the sorted instance ids instead, rebuilds the cycle only when the membership changes, and returns the instance object from the current list so callers never get a stale object.

Same approach as #5879, which was closed without being merged; tests added under tests/http_proxy. With the old code two of the six tests fail (test_cycles_when_each_call_passes_fresh_objects, test_returns_object_from_current_list).

RoundRobinStrategy compared the ModelInstance objects it received on
the previous call with the ones passed now. Every request builds fresh
objects from the database, and any field that moved in between
(heartbeat data, restart counters, state) makes that comparison fail,
so the cycle iterator was recreated and started from the first instance
again. Under bursty load this sends most requests to the first replica;
on a two-replica reranker we measured splits of 27/10 and 21/16.

Track the sorted instance ids instead, rebuild the cycle only when the
membership changes, and return the instance object from the current
list so callers never get a stale object.

Same approach as gpustack#5879, which was closed without being merged.

Signed-off-by: Baki Burak Ogun <[email protected]>
Copilot AI balanced review requested due to automatic review settings September 5, 2026 19:46
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 improves the reliability of the RoundRobinStrategy by decoupling the load balancing cycle from the volatile state of model instance objects. By tracking instance IDs rather than the objects themselves, the strategy maintains a stable rotation across requests, ensuring even distribution of traffic even when underlying instance data changes between calls.

Highlights

  • Round-Robin Strategy Optimization: Updated the RoundRobinStrategy to track instance IDs instead of full object lists, preventing unnecessary iterator resets when object metadata changes.
  • Improved Load Balancing Stability: Ensures consistent round-robin distribution even under bursty loads where database-fetched objects might have varying internal states.
  • Added Comprehensive Test Suite: Introduced new tests in tests/http_proxy/test_strategies.py to verify cycle persistence, membership changes, and model-specific tracking.
New Features

🧠 You can now enable Memory (public preview) 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.

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 the 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 counterproductive. 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.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, 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 gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the RoundRobinStrategy load balancing strategy to track and cycle through instance IDs instead of full ModelInstance objects, preventing unnecessary iterator resets when fresh objects with the same IDs are passed. It also adds comprehensive unit tests for the strategy. The reviewer suggested replacing the generator expression inside next() with a standard loop when finding the selected instance to avoid potential RuntimeError issues caused by StopIteration escaping an async function (per PEP 479).

Comment thread gpustack/http_proxy/strategies.py Outdated

return next(self._iterators[model_id])
next_id = next(self._iterators[model_id])
return next(instance for instance in instances if instance.id == next_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using next() on a generator expression inside an async def function can raise a StopIteration if the item is not found. In Python, if a StopIteration escapes an async def coroutine, it is automatically converted by the runtime into a confusing RuntimeError: coroutine raised StopIteration (per PEP 479).

Although next_id is expected to always be present in instances, it is safer and more robust to use a standard loop. A standard loop also avoids the overhead of creating a generator object.

Suggested change
return next(instance for instance in instances if instance.id == next_id)
for instance in instances:
if instance.id == next_id:
return instance
raise Exception(f"Instance with id {next_id} not found")

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation directly addresses the reset behavior and has focused regression coverage.

Pull request overview

Fixes round-robin load balancing so database refreshes do not reset replica selection.

Changes:

  • Tracks sorted instance IDs instead of mutable model objects.
  • Returns current instance objects and adds regression coverage.
File summaries
File Description
gpustack/http_proxy/strategies.py Preserves round-robin position across refreshed objects.
tests/http_proxy/test_strategies.py Tests cycling, membership changes, isolation, and edge cases.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Wrap the long test line for black and replace the next() call on a
generator expression with a dict lookup so no StopIteration can escape
the coroutine.

Signed-off-by: Baki Burak Ogun <[email protected]>
@bakiburakogun

Copy link
Copy Markdown
Contributor Author

The remaining CI failure here (test_ui_static.py::test_the_gz_itself_is_still_reachable_by_its_own_name) is unrelated to this change: the test recompresses with gzip.compress() and the gzip header timestamp differs when a second boundary falls between fixture and assertion. It passes on main and only this test failed in the run (3721 passed). #6179 makes the fixture bytes deterministic.

@gitlawr gitlawr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@gitlawr
gitlawr merged commit 80ff3ac into gpustack:main Sep 22, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants