Skip to content

[Core] Introduce local port service discovery - #59065

Closed
Yicheng-Lu-llll wants to merge 68 commits into
ray-project:masterfrom
Yicheng-Lu-llll:agent-port-self-discovery
Closed

Yicheng-Lu-llll wants to merge 68 commits into
ray-project:masterfrom
Yicheng-Lu-llll:agent-port-self-discovery

Conversation

@Yicheng-Lu-llll

@Yicheng-Lu-llll Yicheng-Lu-llll commented Nov 28, 2025 •

Copy link
Copy Markdown
Member

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:
image

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✅

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

  • 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:

[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:

[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

@Yicheng-Lu-llll Yicheng-Lu-llll changed the title [Core] Allow agents to self-assign ports and report via pipe [Core] Allow GCS && runtime env agent to self-assign ports and report via pipe Dec 2, 2025
@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the agent-port-self-discovery branch from 78680b8 to 222b544 Compare December 2, 2025 18:52
@Yicheng-Lu-llll
Yicheng-Lu-llll marked this pull request as ready for review December 2, 2025 19:10
@Yicheng-Lu-llll
Yicheng-Lu-llll requested a review from a team as a code owner December 2, 2025 19:10
Comment thread src/ray/util/pipe.h Resolved
Comment thread python/ray/tests/test_client_proxy.py Resolved
Comment thread python/ray/_private/services.py Resolved
@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the agent-port-self-discovery branch from 697bd9c to 1cfdeb3 Compare December 2, 2025 20:47
Comment thread src/ray/util/pipe.h Resolved
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Dec 3, 2025
Comment thread .buildkite/windows.rayci.yml Resolved
Signed-off-by: yicheng <[email protected]>
Signed-off-by: yicheng <[email protected]>
Comment thread src/ray/util/pipe.h Resolved
@testinfected

Copy link
Copy Markdown

Awesome @Yicheng-Lu-llll ❤️

Signed-off-by: yicheng <[email protected]>
Comment thread src/ray/protobuf/gcs.proto Resolved
Comment thread src/ray/protobuf/gcs.proto Resolved
Comment thread src/ray/protobuf/gcs.proto Resolved
@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the agent-port-self-discovery branch from 8677259 to 40f467b Compare December 22, 2025 10:33
@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the agent-port-self-discovery branch from 40f467b to 8677259 Compare December 22, 2025 10:40
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Core] Raylet heartbeat misses

7 participants