Skip to content

fix: keep the backend order in middlewares - #319

Merged
uniqueg merged 3 commits into
devfrom
fix/backend-order
Oct 24, 2026
Merged

uniqueg merged 3 commits into
devfrom
fix/backend-order

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Summary

The task distribution middlewares removed duplicate backends with list(set(...)), which loses the order of custom.tes.service_list and of the discovered backends (and, with hash randomization, differs between processes). They now use dict.fromkeys(...), which removes duplicates the same way and keeps the order. In the distance middleware, backends at the same distance keep their configured order (stable sort). The random middleware shuffles from a fixed starting order, so a seeded shuffle is reproducible.

Verification

Run locally:

  • ruff, ruff format, mypy, ast-grep (and its rule tests), semgrep: passed
  • pytest: 1039 passed, 80 skipped (chart and real-MongoDB tests), 100% coverage
  • The new tests fail against the old code under several PYTHONHASHSEED values (checked by the reviewer and after the fix)
  • mkdocs build --strict: passed

Reviewed with the adversarial and quality reviewer subagents: no high or medium findings. The low finding (a test that couldn't fail on the old code) is fixed: it now compares against the literal order for random.seed(1).

Not run locally: the Docker Compose smoke test and the MongoDB 7.0 CI entry.

Open questions

None.

Closes #314

🤖 Generated with Claude Code

Summary by Sourcery

Preserve backend ordering across task-distribution middlewares while retaining duplicate removal and deterministic seeded shuffling.

Bug Fixes:

  • Preserve configured and discovered backend order while removing duplicate task-distribution endpoints.
  • Keep distance-based backend ties stable and make random backend selection reproducible from the preserved starting order.

Documentation:

  • Document backend ordering and stable ordering for equal-distance instances.

Tests:

  • Add coverage for duplicate removal, stable distance ties, and reproducible random ordering.

uniqueg and others added 2 commits October 25, 2026 00:04
The task distribution middlewares removed duplicate backends with
`set()`, which loses the order of `custom.tes.service_list` and the
discovered backends; with hash randomization, the order even differed
between processes. They now keep the first occurrence of each backend,
in order: the base class forwards in the configured order, the
distance-based middleware keeps that order for backends at the same
distance, and the random order depends on the random state only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test of the reproducible random order compares with the literal
order for `random.seed(1)`, so it fails with the old `set()`-based
order, which depends on the hash seed.

Refs #314

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR replaces hash-dependent set deduplication with insertion-order-preserving logic throughout task distribution middleware processing, ensuring configured backends precede discovered ones, equal-distance ranking is stable, and seeded random distribution is reproducible; documentation and regression tests capture these guarantees.

Flow diagram for order-preserving backend distribution

flowchart LR
    A["Configured service_list"] --> C["TaskDistributionBaseClass._set_tes_urls"]
    B["Discovered backends"] --> C
    C --> D["dict.fromkeys: remove duplicates, preserve order"]
    D --> E["tes_urls"]
    E --> F["Distance middleware: stable distance sort"]
    E --> G["Random middleware: seeded shuffle"]
Loading

File-Level Changes

Change Details Files
Preserve the configured and discovered backend order while removing duplicates.
  • Replace unordered set-based deduplication with insertion-order-preserving deduplication in the base and random middlewares.
  • Apply the same ordering guarantees when initializing distance-middleware backend and input collections, including URI lookup deduplication.
  • Document that service-list order precedes discovered backends and is retained for duplicate removal.
pro_tes/plugins/middlewares/task_distribution/base.py
pro_tes/plugins/middlewares/task_distribution/random.py
pro_tes/plugins/middlewares/task_distribution/distance.py
docs/middlewares.md
Make distance ranking and random distribution deterministic with respect to their starting backend order.
  • Rely on stable sorting so equal-distance backends retain base-class order.
  • Shuffle the preserved ordered list, making seeded randomization reproducible instead of dependent on hash randomization.
pro_tes/plugins/middlewares/task_distribution/distance.py
pro_tes/plugins/middlewares/task_distribution/random.py
Add regression coverage for ordering, stable ties, and reproducible seeded shuffling.
  • Test first-occurrence order after duplicate removal.
  • Test both backend orders when distances tie.
  • Assert the literal seeded shuffle result and document the expected permutation.
tests/plugins/middlewares/task_distribution/test_distance.py
tests/plugins/middlewares/task_distribution/test_random.py

Assessment against linked issues

Issue Objective Addressed Explanation
#314 De-duplicate backend URLs in task-distribution middlewares while preserving the first-occurrence order from configured and discovered backends, including the base and random middlewares. ✅
#314 Preserve the incoming backend order for distance ties and retain first-occurrence order when de-duplicating distance middleware inputs and URLs. ✅
#314 Document the backend ordering guarantees and test configured-order preservation, stable distance ties, and seeded random ordering. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@read-the-docs-community

read-the-docs-community Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 proTES | 🛠️ Build #35384887 | 📁 Comparing 451878d against latest (0770405)

  🔍 Preview build  

2 files changed
± middlewares/index.html
± reference/index.html

@uniqueg
uniqueg marked this pull request as ready for review October 24, 2026 23:06
@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0770405) to head (451878d).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #319   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           29        29           
  Lines         2469      2469           
=========================================
  Hits          2469      2469           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sourcery-ai sourcery-ai Bot 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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@uniqueg
uniqueg merged commit 5a15b5e into dev Oct 24, 2026
19 checks passed
@uniqueg
uniqueg deleted the fix/backend-order branch October 24, 2026 23:12
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.

fix: keep the backend order in middlewares

1 participant