Repository navigation
[Core] Introduce local port service discovery - #59065
Closed
Yicheng-Lu-llll wants to merge 68 commits into
Closed
Yicheng-Lu-llll wants to merge 68 commits into
Yicheng-Lu-llll wants to merge 68 commits into
Conversation
Signed-off-by: yicheng <[email protected]>
Yicheng-Lu-llll
force-pushed
the
agent-port-self-discovery
branch
from
December 2, 2025 18:52
78680b8 to
222b544
Compare
Yicheng-Lu-llll
marked this pull request as ready for review
December 2, 2025 19:10
Signed-off-by: yicheng <[email protected]>
Yicheng-Lu-llll
force-pushed
the
agent-port-self-discovery
branch
from
December 2, 2025 20:47
697bd9c to
1cfdeb3
Compare
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
…_client_proxy.py Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
…r Windows handle inheritance Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
|
Awesome @Yicheng-Lu-llll ❤️ |
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Yicheng-Lu-llll
force-pushed
the
agent-port-self-discovery
branch
from
December 22, 2025 10:33
8677259 to
40f467b
Compare
Yicheng-Lu-llll
requested review from
a team,
SongGuyang,
kfstorm,
pcmoritz,
raulchen and
thomasdesr
as code owners
December 22, 2025 10:33
Yicheng-Lu-llll
force-pushed
the
agent-port-self-discovery
branch
from
December 22, 2025 10:40
40f467b to
8677259
Compare
edoakes
pushed a commit
that referenced
this pull request
Dec 23, 2025
Previously, if the user did not specify them, Ray preassigned the GCS port, dashboard agent port, runtime environment port, etc., and passed them to each component at startup. This created a race condition: Ray might believe a port is free, but by the time the port information is propagated to each component, another process may have already bound to that port. This can cause user-facing issues, for example when Raylet heartbeat messages are missed frequently enough that the GCS considers the node unhealthy and removes it. We originally did this because there was no standard local service discovery, so components had no way to know each other’s serving ports unless they were preassigned. The final port discovery design is here: <img width="2106" height="1492" alt="image" src="https://github.com/user-attachments/assets/eaac8190-99d8-404b-8a8d-283a4f2f0f33" /> This PR addresses port discovery for: - GCS reporting back to the startup script (driver)✅ - The runtime env agent reporting back to the raylet✅ - The dashboard agent reporting back to the raylet ✅ - The raylet blocking registration with the GCS until it has collected port information from all agents ✅ - GCS adding InitMetricsExporter to node_added_listeners_ so it starts the MetricsExporter as soon as the raylet registers with the GCS with complete port information ✅ - The Ray client server obtaining the runtime env agent port from GCS✅ - Ensuring that both a connected-only driver (e.g., `ray.init()`) and a startup driver still receive all port information from the GCS✅ - Ensure GCS FT Works:Using the same GCS port as before✅ - Ensure no metric loss✅ - Clean up the old cache port code✅ (Note that this PR is a clean-up version of #59065) ## Consideration **GCS Fault tolerance:** GCS fault tolerance requires GCS to restart using exactly the same port, even if it initially starts with a dynamically assigned port (0). Before this PR, GCS cached the port in a file, and this PR preserves the same behavior (although ideally, the port should only be read from the file by the Raylet and its agent). This can be further improved by storing the GCS port in Redis, but that should be addressed in a separate PR. **GCS start sequence related:** OpenCensus Exporter and the Event Aggregator Client are now constructed without connecting to the agent port; instead, they defer the actual connection until the head Raylet registers via a callback. At that point, the actual metrics_agent_port is known from the node information. The OpenTelemetry Exporter is now also initialized at head Raylet registration time. **Ray nodes that share the same file system:** There are cases where people run multiple Ray nodes from the same or different Ray clusters, so the port file name is based on a fixed prefix plus the node ID. ## Related issues Closes #54321 ## Test For GCS-related work, here is a detailed test I wrote that covers seven starting/connecting cases: - https://github.com/Yicheng-Lu-llll/ray/blob/port-self-discovery-test-file/python/ray/tests/test_gcs_port_reporting.py - ray.init starts a head node and exposes a dynamic GCS port. - Connect a driver via address="auto" using the address file - Connect a driver via an explicit address - CLI starts head with dynamic GCS port - CLI starts worker connecting to the head via GCS address - CLI starts head with an explicit GCS port - CLI starts head with default GCS port For runtime env agent: - https://github.com/Yicheng-Lu-llll/ray/blob/port-self-discovery-test-file/test_agent_port.py - ray start --head (auto port discovery) - ray start --head with fixed runtime-env-agent-port - ray.init() local cluster (auto port discovery) - (we don't have ray.init() with fixed _runtime_env_agent_port) Test that ray_client_server works correctly with dynamic runtime env agent port: - https://github.com/Yicheng-Lu-llll/ray/blob/port-self-discovery-test-file/test_ray_client_with_runtime_env.py For dashboard agent ports, the existing tests already cover this quite well. ## Follow up - The dashboard agent reporting back to the raylet - The dashboard agent now also writes to GCS, but we should allow only the raylet to write to GCS ## performance before this PR: ```shell [0.000s] Starting ray.init()... [0.000s] Session dir created [0.070s] Process: gcs_server [6.885s] Process: runtime_env_agent [6.955s] Process: raylet [6.955s] Process: dashboard_agent 2025-12-12 04:47:34,391 INFO worker.py:2014 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265 /home/ubuntu/ray/python/ray/_private/worker.py:2062: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0 warnings.warn( [9.061s] ray.init() completed ``` After This PR: ```shell [0.000s] Starting ray.init()... [0.075s] Process: gcs_server [0.075s] Session dir created [0.075s] File: gcs_server_port.json = 39451 [6.976s] Process: raylet [6.976s] Process: dashboard_agent [6.976s] Process: runtime_env_agent [7.576s] File: runtime_env_agent_port.json = 38747 [7.640s] File: metrics_agent_port.json = 40005 [8.083s] File: metrics_export_port.json = 44515 [8.083s] File: dashboard_agent_listen_port.json = 52365 2025-12-12 02:02:54,925 INFO worker.py:1998 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265 /home/ubuntu/ray/python/ray/_private/worker.py:2046: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0 warnings.warn( [10.035s] ray.init() completed ``` We can see that the dominant time is actually at the start of GCS. We wait for GCS to be ready and write the cluster info. The port reporting speed is quite fast (file appearance time − raylet start time). https://github.com/ray-project/ray/blob/863ae9fd573b13a05dcae63b483e9b1eb0175571/python/ray/_private/node.py#L1365-L367 --------- Signed-off-by: yicheng <[email protected]> Co-authored-by: yicheng <[email protected]>
AYou0207
pushed a commit
to AYou0207/ray
that referenced
this pull request
Jan 13, 2026
Previously, if the user did not specify them, Ray preassigned the GCS port, dashboard agent port, runtime environment port, etc., and passed them to each component at startup. This created a race condition: Ray might believe a port is free, but by the time the port information is propagated to each component, another process may have already bound to that port. This can cause user-facing issues, for example when Raylet heartbeat messages are missed frequently enough that the GCS considers the node unhealthy and removes it. We originally did this because there was no standard local service discovery, so components had no way to know each other’s serving ports unless they were preassigned. The final port discovery design is here: <img width="2106" height="1492" alt="image" src="https://github.com/user-attachments/assets/eaac8190-99d8-404b-8a8d-283a4f2f0f33" /> This PR addresses port discovery for: - GCS reporting back to the startup script (driver)✅ - The runtime env agent reporting back to the raylet✅ - The dashboard agent reporting back to the raylet ✅ - The raylet blocking registration with the GCS until it has collected port information from all agents ✅ - GCS adding InitMetricsExporter to node_added_listeners_ so it starts the MetricsExporter as soon as the raylet registers with the GCS with complete port information ✅ - The Ray client server obtaining the runtime env agent port from GCS✅ - Ensuring that both a connected-only driver (e.g., `ray.init()`) and a startup driver still receive all port information from the GCS✅ - Ensure GCS FT Works:Using the same GCS port as before✅ - Ensure no metric loss✅ - Clean up the old cache port code✅ (Note that this PR is a clean-up version of ray-project#59065) ## Consideration **GCS Fault tolerance:** GCS fault tolerance requires GCS to restart using exactly the same port, even if it initially starts with a dynamically assigned port (0). Before this PR, GCS cached the port in a file, and this PR preserves the same behavior (although ideally, the port should only be read from the file by the Raylet and its agent). This can be further improved by storing the GCS port in Redis, but that should be addressed in a separate PR. **GCS start sequence related:** OpenCensus Exporter and the Event Aggregator Client are now constructed without connecting to the agent port; instead, they defer the actual connection until the head Raylet registers via a callback. At that point, the actual metrics_agent_port is known from the node information. The OpenTelemetry Exporter is now also initialized at head Raylet registration time. **Ray nodes that share the same file system:** There are cases where people run multiple Ray nodes from the same or different Ray clusters, so the port file name is based on a fixed prefix plus the node ID. ## Related issues Closes ray-project#54321 ## Test For GCS-related work, here is a detailed test I wrote that covers seven starting/connecting cases: - https://github.com/Yicheng-Lu-llll/ray/blob/port-self-discovery-test-file/python/ray/tests/test_gcs_port_reporting.py - ray.init starts a head node and exposes a dynamic GCS port. - Connect a driver via address="auto" using the address file - Connect a driver via an explicit address - CLI starts head with dynamic GCS port - CLI starts worker connecting to the head via GCS address - CLI starts head with an explicit GCS port - CLI starts head with default GCS port For runtime env agent: - https://github.com/Yicheng-Lu-llll/ray/blob/port-self-discovery-test-file/test_agent_port.py - ray start --head (auto port discovery) - ray start --head with fixed runtime-env-agent-port - ray.init() local cluster (auto port discovery) - (we don't have ray.init() with fixed _runtime_env_agent_port) Test that ray_client_server works correctly with dynamic runtime env agent port: - https://github.com/Yicheng-Lu-llll/ray/blob/port-self-discovery-test-file/test_ray_client_with_runtime_env.py For dashboard agent ports, the existing tests already cover this quite well. ## Follow up - The dashboard agent reporting back to the raylet - The dashboard agent now also writes to GCS, but we should allow only the raylet to write to GCS ## performance before this PR: ```shell [0.000s] Starting ray.init()... [0.000s] Session dir created [0.070s] Process: gcs_server [6.885s] Process: runtime_env_agent [6.955s] Process: raylet [6.955s] Process: dashboard_agent 2025-12-12 04:47:34,391 INFO worker.py:2014 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265 /home/ubuntu/ray/python/ray/_private/worker.py:2062: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0 warnings.warn( [9.061s] ray.init() completed ``` After This PR: ```shell [0.000s] Starting ray.init()... [0.075s] Process: gcs_server [0.075s] Session dir created [0.075s] File: gcs_server_port.json = 39451 [6.976s] Process: raylet [6.976s] Process: dashboard_agent [6.976s] Process: runtime_env_agent [7.576s] File: runtime_env_agent_port.json = 38747 [7.640s] File: metrics_agent_port.json = 40005 [8.083s] File: metrics_export_port.json = 44515 [8.083s] File: dashboard_agent_listen_port.json = 52365 2025-12-12 02:02:54,925 INFO worker.py:1998 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265 /home/ubuntu/ray/python/ray/_private/worker.py:2046: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0 warnings.warn( [10.035s] ray.init() completed ``` We can see that the dominant time is actually at the start of GCS. We wait for GCS to be ready and write the cluster info. The port reporting speed is quite fast (file appearance time − raylet start time). https://github.com/ray-project/ray/blob/863ae9fd573b13a05dcae63b483e9b1eb0175571/python/ray/_private/node.py#L1365-L367 --------- Signed-off-by: yicheng <[email protected]> Co-authored-by: yicheng <[email protected]> Signed-off-by: jasonwrwang <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Previously, if the user did not specify them, Ray preassigned the GCS port, dashboard agent port, runtime environment port, etc., and passed them to each component at startup. This created a race condition: Ray might believe a port is free, but by the time the port information is propagated to each component, another process may have already bound to that port.
This can cause user-facing issues, for example when Raylet heartbeat messages are missed frequently enough that the GCS considers the node unhealthy and removes it.
We originally did this because there was no standard local service discovery, so components had no way to know each other’s serving ports unless they were preassigned.
The final port discovery design is here:

This PR addresses port discovery for:
ray.init()) and a startup driver still receive all port information from the GCS✅Consideration
GCS Fault tolerance:
GCS fault tolerance requires GCS to restart using exactly the same port, even if it initially starts with a dynamically assigned port (0). Before this PR, GCS cached the port in a file, and this PR preserves the same behavior (although ideally, the port should only be read from the file by the Raylet and its agent).
This can be further improved by storing the GCS port in Redis, but that should be addressed in a separate PR.
GCS start sequence related:
OpenCensus Exporter and the Event Aggregator Client are now constructed without connecting to the agent port; instead, they defer the actual connection until the head Raylet registers via a callback. At that point, the actual metrics_agent_port is known from the node information.
The OpenTelemetry Exporter is now also initialized at head Raylet registration time.
Ray nodes that share the same file system:
There are cases where people run multiple Ray nodes from the same or different Ray clusters, so the port file name is based on a fixed prefix plus the node ID.
Related issues
Closes #54321
Test
For GCS-related work, here is a detailed test I wrote that covers seven starting/connecting cases:
For runtime env agent:
Test that ray_client_server works correctly with dynamic runtime env agent port:
For dashboard agent ports, the existing tests already cover this quite well.
Follow up
performance
before this PR:
After This PR:
We can see that the dominant time is actually at the start of GCS. We wait for GCS to be ready and write the cluster info.
The port reporting speed is quite fast (file appearance time − raylet start time).
https://github.com/ray-project/ray/blob/863ae9fd573b13a05dcae63b483e9b1eb0175571/python/ray/_private/node.py#L1365-L367