Skip to content

feat(client): route external client requests using cached grain locations - #11508

Open
ReubenBond wants to merge 3 commits into
dotnet:mainfrom
ReubenBond:rb-feat-client-grain-location-cache
Open

ReubenBond wants to merge 3 commits into
dotnet:mainfrom
ReubenBond:rb-feat-client-grain-location-cache

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Problem

External clients send every grain request to a gateway without location information, so the gateway addresses each request from its own cache or the grain directory. When the gateway changes or its cache lacks an entry (for example after grains move during silo retirement or rebalancing), the client cannot contribute what it has already learned about where a grain lives.

Solution

Client (Orleans.Core)

  • ClientGrainLocationCache caches grain locations in a lock-free ConcurrentLruCache. It learns from GrainAddressCacheUpdate entries in response cache invalidation headers (valid addresses are added, invalid addresses removed), and removes a location when that silo rejects a request without a cache update.
  • Updates are compare-and-set via TryRemove(key, predicate, arg) + TryAdd: an update replaces a cached location only when the cache holds the superseded location or none, so a delayed update does not overwrite a newer location.
  • Message.IsRelocatableRequest identifies application requests to grains (not system targets, clients, system messages, or local-only messages). For these, the client's TargetSilo is the cached location hint which the gateway routes with; for all other messages it continues to select the gateway. ClientMessageCenter and ClientOutboundConnection check TargetSilo != null before the relocatable check and preserve hints when recycling messages.

Gateway (Orleans.Runtime)

  • GatewayInboundConnection retains a relocatable request's target silo, and MessageCenter.RerouteMessage sends it directly to the hinted silo when that silo is active and the gateway's own cache holds no live address. The gateway's live placement cache takes precedence over hints.
  • Hints to silos which are not active are re-addressed through normal placement. Hints to dead silos also add a cache invalidation entry, so the response removes the hint from the client's cache.

Locations come only from cache updates which the runtime already produces, so steady-state responses carry no extra data and the client does no per-response cache writes.

Compatibility

Older gateways clear the target silo of client grain requests, so they ignore hints from newer clients. Newer gateways treat an older client's gateway address as a hint which does not refer to an active silo and address the request normally.

Performance

ConcurrentPing_OneSilo (net10.0, Release, interleaved base/new runs on a shared machine, 30 runs each): base median 117.7k req/s, new median 119.3k req/s (means 116.0k vs 116.3k). No regression within noise.

Tests

  • ClientGrainLocationCacheTests: learning, compare-and-set replacement, preservation of newer locations, invalidation, rejection handling, and IsRelocatableRequest classification.
  • ClientGrainLocationRoutingTests (2-silo cluster, single gateway): the gateway routes to the client's cached location, prefers its own cache over a stale hint without detouring through the hinted silo, and re-addresses a hint to a dead silo while clearing it from the client cache. Each test fails when the corresponding gateway behavior is removed.
Microsoft Reviewers: Open in CodeFlow

Copilot AI balanced review requested due to automatic review settings October 14, 2026 22:47

Copilot AI 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.

🔵 Needs a closer look

Cache concurrency and mixed-version routing need correction, with human validation of cross-layer failover behavior.

2 open findings
What changed in this PR

Adds client-side grain location hints so gateways can reuse locations learned from earlier responses.

Changes:

  • Caches client grain locations and preserves hints through gateway selection.
  • Routes hints using gateway-cache precedence and silo status.
  • Adds cache and routing tests plus architecture documentation.

Automated coverage results were unavailable.

File Description
test/​Orleans.Runtime.Tests/​ClientConnectionTests/​ClientGrainLocationRoutingTests.cs Tests gateway routing and dead hints.
test/​Orleans.Core.Tests/​Runtime/​ClientGrainLocationCacheTests.cs Tests cache updates and request classification.
src/​Orleans.Runtime/​Networking/​GatewayInboundConnection.cs Preserves incoming location hints.
src/​Orleans.Runtime/​Messaging/​MessageCenter.cs Routes hints and invalidates dead locations.
src/​Orleans.Core/​Runtime/​OutsideRuntimeClient.cs Learns and attaches cached locations.
src/​Orleans.Core/​Runtime/​ClientGrainLocationCache.cs Implements the client location cache.
src/​Orleans.Core/​Networking/​ClientOutboundConnection.cs Preserves hints during connection recycling.
src/​Orleans.Core/​Messaging/​Message.cs Classifies relocatable requests.
src/​Orleans.Core/​Messaging/​ClientMessageCenter.cs Separates hints from gateway selection.
docs/​site/​src/​content/​docs/​implementation/​runtime-architecture.md Documents client-assisted routing.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Code coverage

Metric Pull request Current main Variance
Lines 83.56% (119,072 / 142,492) 83.58% (119,037 / 142,429) -0.0124 pp
Branches 73.22% (35,478 / 48,456) 73.21% (35,432 / 48,398) +0.0073 pp

Report-only conclusion: mixed.

The current-main baseline is commit fbe240c34e and uses the same reviewed coverage matrix.

Coverage combines every CI test matrix job, including providers, CodeGen, .NET 8/10, Linux, Windows, and macOS, using canonical physical source and branch identities.

The comparison remains report-only while normal line and branch variance is calibrated.

Coverage details

Copilot AI balanced review requested due to automatic review settings October 18, 2026 17:56
@ReubenBond
ReubenBond force-pushed the rb-feat-client-grain-location-cache branch from b7272e4 to ad4b382 Compare October 18, 2026 17:56

Copilot AI 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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
2 resolved since last review

Review effort: Lite

Copilot AI balanced review requested due to automatic review settings October 18, 2026 18:27

Copilot AI 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.

🔵 Needs a closer look

Hints to pruned silos can persist and cause repeated full membership scans despite a warm gateway cache.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Invalidate cached hints when silo status is None

src/​Orleans.Runtime/​Messaging/​MessageCenter.cs:467

Invalidate SiloStatus.None hints as well. If a client resumes calls after its cached host's membership entry has been pruned, with no successor at that endpoint, MembershipTableSnapshot.GetSiloStatus scans every membership entry and returns None. Normal placement can then serve the request without a cache update, leaving the obsolete hint in the client's non-expiring cache. Subsequent calls repeat that full scan even when the gateway's placement cache is warm. Include None here so the response removes the hint, and add a regression test for a pruned-silo hint.

2 resolved since last review

Review effort: Balanced

…ions

External clients cache grain locations learned from cache invalidation headers on responses and drop locations which reject requests. Relocatable grain requests carry the cached location as a target silo hint, which gateways use when the hinted silo is active and their own cache has no live address. Gateways inform clients of hints to dead silos.
Copilot AI balanced review requested due to automatic review settings October 19, 2026 21:05
@ReubenBond
ReubenBond force-pushed the rb-feat-client-grain-location-cache branch from 909f832 to d015d9c Compare October 19, 2026 21:05

Copilot AI 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.

🔵 Needs a closer look

Cross-layer routing and rolling-upgrade compatibility require final human validation beyond the available static review.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
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.

2 participants