Skip to content

atenet-router: add safe actor assignments caching and retry logic - #2420

Merged
Lior Lieberman (LiorLieberman) merged 20 commits into
agent-substrate:mainfrom
panchoruy:ingress-cache
Nov 18, 2026
Merged

Lior Lieberman (LiorLieberman) merged 20 commits into
agent-substrate:mainfrom
panchoruy:ingress-cache

Conversation

@panchoruy

@panchoruy Pancho Trujillo (panchoruy) commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Every ingress request calls ext_proc in atenet-router, which calls ResumeActor on the ATE API server to find the actor's worker. This adds an Envoy Rust dynamic module (envoy_substrate_ingress_cache) that caches <atespace>/<actor> -> worker IP in Envoy, so bodiless requests to a warm actor skip that call. It's behind the IngressCache preview gate, off by default.

  • Cache hit: the HTTP filter, ahead of ext_proc, reads the actor from dev.ate.target.actor filter state or a single ate-target-actor header (an inner header on a terminated CONNECT tunnel is ignored). It sets the envoy.filters.listener.original_dst metadata (worker ip:atunnel_port, actor port, worker SPIFFE ID), x-ate-target-port and ate-target-actor, plus ate.ingress_cache hit dynamic metadata, and clears the route cache. A dedicated route that matches that metadata, which clients can't set, has ext_proc disabled.
  • Cache miss: the request goes through ext_proc unchanged. On a successful response the filter learns the binding from the original_dst metadata. Failures aren't learned.
  • Requests with a body: only requests that end with their headers or have content-length: 0 use the cache, because Envoy doesn't buffer bodies for replay. Everything else, including all gRPC, chunked or HTTP/2 GETs and upgrades, skips the lookup (bypass). Their responses still refresh the cache if successful.
  • Eviction: a hit that fails with 421 + x-ate-assignment-stale: true, 503 with UF/UH/UC/UR (Envoy flags), or grpc-status: 14 evicts the binding it used, but not a newer one. Application 503s without flags are left alone.
  • Tunnel Watcher: on the CONNECT path a refused tunnel (never established) and a tunnel cut off mid-request looks the same to the router (503 UC). The tunnel_watcher is a network filter on the actor_connect internal listener, before tcp_proxy, that can them apart from byte direction alone. A close with UF marks the actor's entries for that worker stale; an mid-flight close with a request outstanding marks them spoiled. Its close callback runs inside tcp_proxy's close, so the mark always lands before the router's 503.
  • Replay: requests with no body that hit stale cache are replayed through ext_proc with recreate_stream (restoring ate-target-actor; the new stream starts without the hit metadata) only when we have certainty an actor did NOT receive it: 421 stale, 503 UF/UH, or 503 UC with upstream_reset_before_response_started details AND only when the cache entry is stale and not spoiled. Replay of requests with body, including all gRPC is left out of scope of this initial implementation.
  • As of this implementation, the cache is per Envoy worker thread (no locks). It can be configured with atenet-router's --ingress-cache-ttl (default 5s) and --ingress-cache-max-cached-actors (default 10,000 per thread) set its TTL and size. Known limitations are in the module README.
    On kind with the router at 2 CPUs and 50 actors, the cache raised the max RPS within a 25ms tail-latency SLO from 3936 to 3968, cut p50 from 3.77ms to 1.03ms, and cut ext_proc calls by ~96%.

AI was used to assist with the development of this change: including knowledge gathering, adversarial testing and review, test coverage writing, refactoring, and all code writing. Line by line read of all deltas by the human author (except for autogenerated vendor/ dir), as well as benchmark and test results review.

  • Tests pass
  • Appropriate changes to documentation are included in the PR

@panchoruy

Copy link
Copy Markdown
Contributor Author

Note: linking related issue #1330.

@bowei Bowei Du (bowei) added area/network kind/feature An enhancement / feature request or implementation labels Oct 14, 2026
@panchoruy

Pancho Trujillo (panchoruy) commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Also found #593 which was filed even earlier but seems to point to the same issue that this PR will address.

@panchoruy

Pancho Trujillo (panchoruy) commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Benchmarking summary

Outdated comment, check latest below

@panchoruy
Pancho Trujillo (panchoruy) marked this pull request as ready for review October 15, 2026 19:49
@panchoruy Pancho Trujillo (panchoruy) changed the title Implement ingress cache Envoy dynamic module and routing Ingress location cache (Envoy dynamic module) Oct 16, 2026
@panchoruy

Pancho Trujillo (panchoruy) commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Lior Lieberman (@LiorLieberman) Bowei Du (@bowei) Thank both for our offline discussion! Copying some follow up to your feedback here for posterity, regarding 503 handling and thread nature of this cache implementation:

On 503 (upstream failure) handling.

  • Currently the cache will treat ANY 503 as a stale actor, evicts the cache entry, and retries ONCE through ext_proc. Whether the failure itself relates to staleness or something else (eg worker node rebooted, network link dropped, or atunnel hung) the cached route is still worth invalidating bc the worker IP is dead.
  • Since we're guarding with was_cache_hit == true, we are only bound to retry at most once: on cache hit. After the eviction - it's no longer a cache hit.
  • So I'm pretty convinced now that we should very likely NOT depend on additional metadata to distinguish the 503s (eg tcp_proxy closing a connection due to staleness, vs other). The only useful reason we would really add metadata would be to have sligthly better observability (eg to have the counts would be more accurate, like maybe count evictions by "cause"). However tcp_proxy does not attach HTTP dynamic metadata on failure so passing and decoding that protobuf/header map across the internal listener boundary into the Rust dynamic module introduces extra overhead and coupling for marginal practical benefit.

On threads and thread-safety:

  • This cache implementation is process-global, so shared across all Envoy worker threads. This is still thread-safe and actually not an issue. Rust has OnceLock to handle this, and DashMap is sharded to avoid write lock contention. Concurrent calls to ext_proc are possible (eg on near-simultaneous cache-misses) and will just result in the cache entry getting overwritten/ refreshed within a small interval. Since routing is idempotent, this only keeps the latest expiration, and otherwise won't cause a problem.

@panchoruy

Copy link
Copy Markdown
Contributor Author

Rebased to current main HEAD

@panchoruy

Copy link
Copy Markdown
Contributor Author

Looking into the e2e failures

@LiorLieberman

Copy link
Copy Markdown
Collaborator

Thanks for the followup Pancho!

So I'm pretty convinced now that we should very likely NOT depend on additional metadata to distinguish the 503s (eg tcp_proxy closing a connection due to staleness, vs other).

  1. How do we know a 503 is safe to replay? It could be the actor's own 503 after it already ran the request.
  2. IIUC recreate_stream only replays the body if something buffered it, and nothing does right now. So if I send a POST , it gets replayed with no body..? but still the same old content-length?
  3. With upstream-connect (the default), the route removes ate-target-actor before forwarding, and the replay reuses those modified headers. So ext_proc gets the replay with no actor header and rejects it with 404 - I think its "invalid actor reference" ?
  4. The 421 check uses is_none_or, so the actor's own 421 (no header) counts as stale too. probably should fix
  5. For gRPC, we return 200 for 503 errors with grpc-status: 14, so the replay never triggered.

Some ideas;

  • For 1 - if there's no way to tell on CONNECT path, maybe just evict the entry and pass the 503 through without replaying. - I dont love it but.. the alternative is to determine where its safe which would be hard to do and prob very opinionated as we dont know the actors logic..?

  • For 2 - Don't replay requests that had a body (end_of_stream false in on_request_headers). Buffering the whole body on every request just to cover this case isn't worth it.

  • For 3 - if any replay stays on the upstream-connect path, set ate-target-actor back before calling recreate_stream.

Some other nits;

  • We need e2e tests for Envoy for all that logic (after we settle on it)
  • Fix the PR description, it mentions a retry policy that isn't there.

@panchoruy

Copy link
Copy Markdown
Contributor Author

Amended the commit with a fix for the e2e test: the ingress cache dynamic module needed to learn and stamp the worker's SPIFFE ID into dynamic metadata alongside the worker IP, fixing upstream mTLS certificate SAN validation failures on cache hits.

@panchoruy

Pancho Trujillo (panchoruy) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you Lior Lieberman (@LiorLieberman) for your careful and thorough feedback - it is sorely needed! Addressing some your questions below, which include a couple of questions for you as well.

1 & 2- I completely missed that an actor could return a 503... Looking deeper into this, turns out there IS a way to tell when the 503 is coming from Envoy tcp_proxy, as certain internal flags (ResponseFlags) are set by the Envoy, which allow us to be more granular in our strategy. Another conclusion I gathered is that cache eviction and "replayability" should be decoupled, since there are circumstances in which we will want to evict but not replay (the latter always requires the former, of course). Also, agreed on leaving buffering out of scope here.

Regarding the 503s classification:

  • There is a clear subset of ResponseFlags set by Envoy (UF, UH, UC, UR) that represent upstream worker connectivity loss. In these cases we should evict the cache.
    • For UF, UH it is guaranteed that no bytes have reached the actor. In that case we will replay any requests with no body (since we're going to leave buffering out for now).
    • For UC, UR some or all bytes may have reached the actor. In this case we will never replay - out of caution, and just propagate the error downstream. While it should be safe theoretically to replay idempotent methods (also w/o request body, like GET, DELETE, etc) we can leave this out for now to not get too far into the weeds.
      Question: WDYT? Let me know if you think I should enable replaying only idempotent methods in this case.
  • When all of (UF, UH, UC, UR) are not set, then either the 503 came from the actor (IE NO ResponseFlags are set) or it came from Envoy (IE any other ResponseFlags) but it's not evidence that the cache is stale, since other flags represent normal/slow actor application behavior, client-side issues, or local router self-protection where evicting the cache would be detrimental.
                                  HTTP 503 Arrives
                                         │
              Are Specific Failure ResponseFlags Set? (UF, UH, UC, or UR)
                                         │
                     ┌───────────────────┴───────────────────┐
                    YES                                      NO
                     │                             (flags == 0 or other
                     │                              router flags eg UO/UT)
                     │                                       │
            EVICT Cache Entry                         Do NOT Evict
                     │                                Do NOT Replay
            Does Request have Body?                   Pass 503 downstream to Client
            (end_of_stream == false)                         
                     │
          ┌──────────┴──────────┐
         YES                    NO
          │                      │
    Do NOT Replay         Which ResponseFlag?
    (Body unbuffered)            │
    Pass 503 to Client   ┌───────┴───────┐
                     UF or UH         UC or UR
                         │               │
                 REPLAY SAFE        Do NOT Replay (even if idempotent)
              recreate_stream()    (Mid-flight disconnection;
              Pass to ext_proc      pass 503 to client)

3- You're totally right. I started re-injecting the header before recreate_stream per your suggestion and added tests.
4- Yikes! my bad. Changed with is_some_and() which has the actual desired behavior (checking presence).
5- For gRPC with grpc-status: 14, IIUC this status should already be propagated downstream in the 200 response. Standard gRPC clients automatically and transparently retry the call when grpc-status: 14 is set. That said, I figured we should be evicting the cache (on cache hit) in this case as well - just did this now. We should NOT call recreate_stream - replaying would get tricky since we're not buffering request body (and every gRPC request uses body). Letting the gRPC client retry cleanly (which will be a cache miss) seems the most appropriate here.

I also added & updated unit tests for all of the above, and I'm now looking into debugging e2e failures, as well as improving the e2e coverage. I'll follow up here with updates on that.

@panchoruy

Copy link
Copy Markdown
Contributor Author

Also updated PR description to reflect latest status.

@LiorLieberman

Copy link
Copy Markdown
Collaborator

Question: WDYT? Let me know if you think I should enable replaying only idempotent methods in this case.

I think like you, we should not try to classify whether a req is idempotent based on http methods. so no need to replay until someone shouts

On Does Request have Body? from the diagram. Only by reading your descirption before the diagram it looks like this check is redundant. looks like this can just be is it UF, UH or UC, UR..? no?

Also - reading the code, I think that on the CONNECT path, a refused CONNECT (with actual stale case) shows up as UC, so it's evicted but not replayed. This means the deafult path (which is CONNECT) never gets a replay. Is my understanding from the code correct? probably not the intention

Standard gRPC clients automatically and transparently retry the call when grpc-status: 14 is set.

I could not confirm this - where can I find more about it? I dont think necessarly every client does that

On the config.rs impl - I think that the pr description is stale, at least I could not find max_entries in the code. I think the DashMap is unbounded and every actor the router ever reached stays in memory until restart..?

@bowei

Copy link
Copy Markdown
Collaborator

+1 do not assume based on verb that you can replay a request. There are many applications that don't obey these semantics unfortunately and there is no way to really tell people to fix this.

@panchoruy

Pancho Trujillo (panchoruy) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks again Lior Lieberman (@LiorLieberman) . Here's another update

  • Re: request body: so IIUC has_request_body was orthogonal to the ResponseFlags (UF, UH, UC, UR) is required because we cannot replay bodiless requests (not buffered) even if they failed before the actor got any bytes (the body is no longer in Envoy memory). The diagram corresponds specifically to the 503 case (the has_request_body check prevents replays of requests in both the 503 cases and also in the direct HTTP 421 error case). Note that the "has request body" matters for whether we should replay the request - but we've already decided to evict the cache entry. Lmk if this clarifies your question!

  • Re: gRPC retry mechanisms: Ah actually I made a mistake here. I don't think gRPC clients will "automatically" retry on status 14. It's configurable in retry policy, but not on by default. However, given gRPC use of body and the fact we're not buffering still means we have to propagate this downstream, and it will be on the client to retry.

  • Re: unbounded cache - good catch, updated the PR description!

  • Fixed the e2e failures, some of which actually relate to the issues mentioned above.

  • Re: UC ResponseFlag (refused CONNECT failure): Your understanding is correct. I see how it would not be ideal though, as it means we're introducing a new failure mode in the default path. Unfortunately we have no good way of telling apart a 503 UC from tcp_proxy (actual stale cache hit) vs a random 503 UC (conn dropped in flight, actor may have received bytes and/or done things). Let's chat offline about this.
    In the current state, I'm still keeping the UC/UH behavior where we will evict the entry but not replay.

@panchoruy

Copy link
Copy Markdown
Contributor Author

Switching this PR to draft temporarily while we hash out some design decisions offline.

@panchoruy
Pancho Trujillo (panchoruy) marked this pull request as draft October 22, 2026 20:49
Bindings for actors that were never looked up again stayed in the cache
forever, once per Envoy worker thread. A full thread cache now drops
expired bindings before it binds another actor, and doesn't cache the
actor if none had expired. It scans at most once a second.

Signed-off-by: Pancho <[email protected]>
The websocket testserver gains /hang, which never answers, and
/hanging, which counts waiting /hang requests, so the test can suspend
an actor while a cache hit is in flight.

Signed-off-by: Pancho <[email protected]>
Read the cap from the filter config's max_actors_per_thread, which
defaults to 10,000. The router doesn't set it yet.

Signed-off-by: Pancho <[email protected]>
Add --ingress-cache-ttl and --ingress-cache-max-cached-actors, which
the router passes to the ingress-cache filter as its JSON config. The
cap is per Envoy worker thread. Rename the module setting from
max_actors_per_thread to max_cached_actors to match.

Signed-off-by: Pancho <[email protected]>
Envoy's HTTP/1 codec delivers a bodiless request's headers with
end_of_stream, so a GET or HEAD without it or content-length may carry
a body: chunked, HTTP/2 without END_STREAM, or an upgrade. A stale hit
would replay it without the body. Serve from the cache only requests
that end with their headers or have content-length: 0.

Signed-off-by: Pancho <[email protected]>
A stale cache hit is now replayed, so the combinedvolumes and identity
suites no longer retry after one. Fix test comments that still said
UC is never replayed and that the cache is process-wide.

Signed-off-by: Pancho <[email protected]>
Clients could forge the ate-endpoint-cached header, and it reached the
actor. Match the cached route on dynamic metadata the filter sets
instead, and drop the header.

Signed-off-by: Pancho <[email protected]>
@panchoruy

Copy link
Copy Markdown
Contributor Author

Thank you Lior! It's behind the IngressCache preview gate, OFF by default.
I had to rebase again and resolve some conflicts. Please restamp when you can :)

@LiorLieberman Lior Lieberman (LiorLieberman) added this to the v1.1 milestone Oct 30, 2026
@LiorLieberman

Copy link
Copy Markdown
Collaborator

For posterity, we are in stabilization period until v1.0. I added v1.1 milestone here. This PR is ready to merge. We should merge it as soon as the stabilization window finishes.

@LiorLieberman

Copy link
Copy Markdown
Collaborator

Approved. Lets rebase this one and track it to merge on 11/18 (per https://groups.google.com/g/ate-dev/c/OnUyIc_FGBs/m/kfeKrnwlAAAJ)

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 18, 2026
Merged via the queue into agent-substrate:main with commit abf55fe Nov 18, 2026
5 checks passed
@panchoruy
Pancho Trujillo (panchoruy) deleted the ingress-cache branch November 18, 2026 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/network kind/feature An enhancement / feature request or implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants