Skip to content

fix(balance): clear cached P2C ready index after a discovery removal#874

Open
ameyypawar wants to merge 2 commits into
tower-rs:masterfrom
ameyypawar:fix/p2c-stale-ready-index
Open

fix(balance): clear cached P2C ready index after a discovery removal#874
ameyypawar wants to merge 2 commits into
tower-rs:masterfrom
ameyypawar:fix/p2c-stale-ready-index

Conversation

@ameyypawar

Copy link
Copy Markdown

Summary

Fixes #856.

p2c::Balance caches the P2C-selected endpoint as a numeric ready_index. When a subsequent poll_ready processes a Change::Remove, update_pending_from_discover calls ReadyCache::evict, which removes from the ready set with swap_remove_full. That can move a different endpoint into the slot the cached ready_index points at. poll_ready then revalidates the stale index with check_ready_index (which succeeds for whatever now occupies that slot) and call dispatches the request there — silently bypassing the P2C choice during endpoint churn.

This matches the warning already in ready_cache's own docs:

calls to ReadyCache::poll_pending and ReadyCache::evict may perturb the order of the ready set, so any cached indexes should be discarded after such a call.

Fix

Clear ready_index whenever a removal evicts a service, so the next poll_ready re-runs P2C over the current ready set:

Some(Change::Remove(key)) => {
    trace!("remove");
    self.services.evict(&key);
    // `evict` removes from the ready set with `swap_remove`, which can move a
    // different endpoint into the slot a previously-selected `ready_index`
    // points at. Discard the cached selection so `poll_ready` re-runs P2C.
    self.ready_index = None;
}

I also corrected the poll_ready comment that claimed "These updates cannot disturb the order of existing ready services" — they can, via swap_remove.

Why this minimal fix is sufficient

The only operation that reorders the existing ready set between caching ready_index and reusing it is evict's swap_remove_full:

  • ReadyCache::poll_pending only inserts into the ready IndexMap (appends new keys / updates existing in place — existing indices stay put).
  • Change::Insert pushes to the pending set, never the ready set.
  • check_ready_index's not-ready/error arms and call_ready_index also swap_remove, but only after ready_index has been .take()n within the same poll_ready loop, each immediately followed by a fresh P2C selection — so no stale index survives across calls.

The issue notes a more robust alternative (cache the key instead of the index); that's a larger change and isn't required to close this bug, but happy to go that route if you'd prefer it.

Tests

Not included in this commit. A deterministic regression test needs a dynamic Discover (to emit Change::Remove between poll_ready calls) plus a seeded Rng, which the current p2c tests don't have scaffolding for. Glad to follow up with that test (building the small test Discover + mock Rng) if you'd like it in this PR.

`Balance::poll_ready` caches the P2C-selected endpoint as a numeric
`ready_index`. When a later `poll_ready` processes a `Change::Remove`, it
calls `ReadyCache::evict`, which removes from the ready set with `swap_remove`
and can move a different endpoint into the slot the cached index points at.
The stale index was then revalidated by `check_ready_index` and dispatched to
by `call`, silently bypassing the P2C choice during endpoint churn.

Clear `ready_index` whenever a removal evicts a service, so the next
`poll_ready` re-runs P2C over the current ready set. Also correct the
`poll_ready` comment that wrongly claimed discovery updates cannot disturb the
order of existing ready services.
…s#856)

Drive a balancer to cache a P2C selection, remove that endpoint so eviction
swap-moves a different endpoint into the cached slot, and assert the next
request is routed by a fresh P2C selection rather than to the swapped-in
endpoint. Uses a fed-on-demand Discover (deterministic ready-set order) and a
zero RNG (deterministic P2C sampling).
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.

balance/p2c: stale cached ready index after discovery removal can route requests to a different endpoint

1 participant