Repository navigation
[Core] Optimize token auth: use shared_ptr caching and avoid per-RPC object construction - #59500
Conversation
Signed-off-by: sampan <[email protected]>
Signed-off-by: sampan <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces significant optimizations to the token authentication mechanism, primarily by leveraging std::shared_ptr to cache the authentication token and avoid repeated object construction and destruction. The changes are consistently applied across the C++, Cython, and Python layers. Key improvements include caching the token in the client interceptor, passing it by shared_ptr to avoid per-RPC copies, and using string_view for more efficient, constant-time token comparison on the server side, which avoids unnecessary object creation. The implementation is solid and aligns well with the goal of reducing authentication overhead. I have one minor suggestion to further reduce allocations in the K8S authentication path.
Signed-off-by: sampan <[email protected]>
Signed-off-by: sampan <[email protected]>
Signed-off-by: sampan <[email protected]>
|
@sampan-s-nayak please post benchmark details and results in the PR description |
|
@dayshah @ZacAttack please review |
| namespace rpc { | ||
|
|
||
| RayTokenAuthClientInterceptor::RayTokenAuthClientInterceptor() | ||
| : token_(AuthenticationTokenLoader::instance().GetToken()) {} |
There was a problem hiding this comment.
So... the old code would call instance().GetToken everytime... This change now caches it at time of construction. The effect of this is that if the token ever changes for any reason post this construction the change won't take effect. Though I'm a little unclear if that's likely? Looking at AuthenticationTokenLoader it seems like this might break if ResetCache is ever called?
There was a problem hiding this comment.
we dont really support token rotation (there is no way to use a new tokwn without restarting the cluster) so this is not going to be a problem. the resetCache method is mainly being used in unit and integration tests.
ZacAttack
left a comment
There was a problem hiding this comment.
Gave it a look through. Most are nits, but I'm a little hazy on the impact of handling things like reset or if the token needs to rotate....
thanks, we dont support token rotation and reset is currently only used internally for testing purposes so this might be ok for now. |
| // Convert to vector and use constant-time comparison | ||
| std::vector<uint8_t> provided_bytes(provided_token.begin(), provided_token.end()); | ||
| return ConstTimeEqual(secret_, provided_bytes); | ||
| } |
There was a problem hiding this comment.
Bug: noexcept function can throw on memory allocation
The CompareWithMetadata function is declared noexcept but internally constructs a std::vector<uint8_t> on line 97, which can throw std::bad_alloc if memory allocation fails. When a noexcept function throws, std::terminate() is called, causing an immediate program crash. While this is an extreme edge case (out-of-memory during authentication), the noexcept specification is technically incorrect. Other similar methods like Equals don't allocate and are correctly marked noexcept, but this new method breaks that pattern.
Signed-off-by: sampan <[email protected]>
Signed-off-by: sampan <[email protected]>
…object construction (ray-project#59500) ## Description Reduce overhead added by token authentication: - Return shared_ptr from AuthenticationTokenLoader::GetToken() instead of constructing a new AuthenticationToken object copy every time (which would also add object destruction overhead) - Cache token in client interceptor at construction (previously called GetToken() for every RPC) - Use CompareWithMetadata() to validate tokens directly from string_view without constructing new AuthenticationToken objects - Pass shared_ptr through ServerCallFactory to avoid per-call copies release tests: without this change, the microbenchmark `multi_client_put_gigabytes` was in 25-30 range, eg run: https://buildkite.com/ray-project/release/builds/70658 now with this change it is in 40-45 range https://buildkite.com/ray-project/release/builds/72070 --------- Signed-off-by: sampan <[email protected]> Co-authored-by: sampan <[email protected]> Signed-off-by: jasonwrwang <[email protected]>
## Why are these changes needed? This PR adds gRPC-based inter-deployment communication for Ray Serve, allowing deployments to communicate with each other using gRPC transport instead of Ray actor calls. This can provide performance benefits in certain scenarios. ### Key Changes 1. **gRPC Server on Replicas**: Each replica now starts a gRPC server that can handle requests from other deployments. 2. **gRPC Replica Wrapper**: A new `gRPCReplicaWrapper` class handles sending requests via gRPC and processing responses. 3. **Handle Options**: The `_by_reference` option on handles controls whether to use Ray actor calls (`True`) or gRPC transport (`False`). 4. **New Environment Variables**: - `RAY_SERVE_USE_GRPC_BY_DEFAULT`: Master flag to enable gRPC transport by default for all inter-deployment communication - `RAY_SERVE_PROXY_USE_GRPC`: Controls whether the proxy uses gRPC transport (defaults to the master flag value) - `RAY_SERVE_GRPC_MAX_MESSAGE_SIZE`: Configures the maximum gRPC message size (default: 2GB-1) ## Related issue number N/A ## Checks - [x] I've signed all my commits - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [ ] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference. For example, if I added a temporary testing hook, I've added it under the API Reference (Experimental) page. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests. ## Test Plan - `python/ray/serve/tests/test_grpc_e2e.py` - `python/ray/serve/tests/test_grpc_replica_wrapper.py` - `python/ray/serve/tests/unit/test_grpc_replica_result.py` ## Benchmarks Script available [here](https://gist.github.com/eicherseiji/02808c32d0e377803888671da64524d1) Results show throughput/latency improvements w/ gRPC for message size < ~1MB. <img width="2229" height="740" alt="benchmark_plot" src="https://github.com/user-attachments/assets/e7e25f94-00b4-434d-9eff-10cd36047356" /> ``` ⎿ ============================================================================== gRPC vs Plasma Benchmark Results ============================================================================== Payload Metric Plasma gRPC Δ Winner ---------------------------------------------------------------------- 1 KB Latency p50 2.63ms 1.89ms +28% gRPC Chain p50 4.11ms 3.02ms +26% gRPC Throughput 160/s 190/s +16% gRPC ---------------------------------------------------------------------- 10 KB Latency p50 2.68ms 1.68ms +37% gRPC Chain p50 3.91ms 2.94ms +25% gRPC Throughput 167/s 185/s +10% gRPC ---------------------------------------------------------------------- 100 KB Latency p50 2.74ms 2.02ms +26% gRPC Chain p50 4.28ms 3.06ms +28% gRPC Throughput 157/s 182/s +13% gRPC ---------------------------------------------------------------------- 500 KB Latency p50 5.78ms 3.52ms +39% gRPC Chain p50 5.65ms 4.82ms +15% gRPC Throughput 114/s 144/s +21% gRPC ---------------------------------------------------------------------- 1 MB Latency p50 6.31ms 5.18ms +18% gRPC Chain p50 5.96ms 6.20ms -4% Plasma Throughput 130/s 165/s +21% gRPC ---------------------------------------------------------------------- 2 MB Latency p50 8.82ms 9.57ms -9% Plasma Chain p50 7.20ms 10.69ms -48% Plasma Throughput 123/s 106/s -16% Plasma ---------------------------------------------------------------------- 5 MB Latency p50 15.20ms 23.72ms -56% Plasma Chain p50 8.90ms 23.25ms -161% Plasma Throughput 78/s 49/s -58% Plasma ---------------------------------------------------------------------- 10 MB Latency p50 25.02ms 34.34ms -37% Plasma Chain p50 9.72ms 34.71ms -257% Plasma Throughput 38/s 31/s -24% Plasma ---------------------------------------------------------------------- ``` Compared to parity implementation: ``` ============================================================================== gRPC Transport: OSS 3.0.0.dev0 vs Parity 2.53.0 ============================================================================== Payload Metric OSS 3.0.0.dev0 Parity 2.53.0 ---------------------------------------------------------------------- 1 KB Latency p50 1.82ms 2.27ms Chain p50 2.95ms 2.99ms Throughput 268/s 272/s ---------------------------------------------------------------------- 10 KB Latency p50 1.82ms 2.05ms Chain p50 2.85ms 2.80ms Throughput 246/s 293/s ---------------------------------------------------------------------- 100 KB Latency p50 2.04ms 2.35ms Chain p50 3.27ms 3.12ms Throughput 262/s 257/s ---------------------------------------------------------------------- 500 KB Latency p50 3.67ms 3.78ms Chain p50 5.77ms 4.91ms Throughput 186/s 192/s ---------------------------------------------------------------------- 1 MB Latency p50 4.99ms 5.39ms Chain p50 5.95ms 6.56ms Throughput 177/s 156/s ---------------------------------------------------------------------- 2 MB Latency p50 7.91ms 7.37ms Chain p50 8.26ms 12.16ms Throughput 117/s 129/s ---------------------------------------------------------------------- 5 MB Latency p50 17.86ms 19.53ms Chain p50 22.65ms 23.85ms Throughput 87/s 54/s ---------------------------------------------------------------------- 10 MB Latency p50 23.79ms 27.78ms Chain p50 35.67ms 31.06ms Throughput 48/s 27/s ---------------------------------------------------------------------- Cluster: 2 worker nodes (48 CPU, 4 GPU, 192GB RAM, 54.5GB object store each) 3-trial average ``` Note: OSS 3.0.0.dev0 includes a token auth optimization (#59500) that reduces per-RPC overhead by caching auth tokens and avoiding object construction on each call. This likely explains the improved latency and throughput at larger payload sizes. --------- Signed-off-by: Seiji Eicher <[email protected]>
Description
Reduce overhead added by token authentication:
release tests:
without this change, the microbenchmark
multi_client_put_gigabyteswas in 25-30 range, eg run: https://buildkite.com/ray-project/release/builds/70658now with this change it is in 40-45 range
https://buildkite.com/ray-project/release/builds/72070