Repository navigation
[core][sandbox] Add pluggable sandbox backends and an Agent Substrate backend - #68616
Draft
sunnylovestiramisu wants to merge 6 commits into
Draft
sunnylovestiramisu wants to merge 6 commits into
sunnylovestiramisu wants to merge 6 commits into
Conversation
…torage Stacked on ray-project#66471 (node-local checkpoint/restore), split out of it in review (ray-project#66471 (review)). Adds remote=True to Sandbox.checkpoint(), SandboxRuntime.checkpoint() and the backend for checkpoints written to storage that every node mounts at the same path (NFS, Filestore, Cloud Storage FUSE). The manifest and the returned CheckpointPath record it, sandbox.restore() then doesn't pin the restored actor to the checkpoint's node, and the backend allows a restore on another node. restore() also takes remote= to override the recorded value. Restoring on another node needs every node to build the sandbox's image identically; the restore-time superblock check refuses a node whose build differs. Reproducible builds come in a separate PR. Signed-off-by: Sunny Song <[email protected]>
- __init__.py: sandbox.restore(path, remote=True) with a plain path this process can't read now raises unless cpu and memory are passed, instead of reserving Ray's default 1 CPU while the restored sandbox keeps the checkpoint's cgroup limits. ray-project#67485 (comment) Signed-off-by: Sunny Song <[email protected]>
Review: ray-project#67485 (review) - gvisor.py: checkpoint_sandbox(remote=True) without a checkpoint_path raises instead of writing to the node-local default location that a restore on another node can't find. ray-project#67485 (comment) - sandbox.py, runtime.py, backend/base.py, backend/gvisor.py: remote comes after timeout_seconds in Sandbox.checkpoint, SandboxRuntime.checkpoint and restore, and the backend methods, as in sandbox.restore(), so a positional timeout can't bind to remote. ray-project#67485 (comment) - __init__.py: sandbox.restore() treats a readable manifest as authoritative for node_id, cpu, memory and remote, over a possibly stale CheckpointPath, and falls back to the CheckpointPath's metadata when the manifest can't be read (missing, or PermissionError on shared storage). ray-project#67485 (comment) - __init__.py: the error for a plain path this process can't read says to pass the CheckpointPath, or remote=True with cpu and memory, and the docstring says that cpu and memory set the restored sandbox's limits as well as the actor's reservation. ray-project#67485 (comment) - sandbox.py: Sandbox passes image and an explicit disk (including 0) on to restore, so a changed image or disk is rejected instead of silently ignored; disk now defaults to None (still no cap on create). ray-project#67485 (comment) Signed-off-by: Sunny Song <[email protected]>
- __init__.py: sandbox.restore() passes on only the caller's remote, not one inferred from a CheckpointPath, so the worker decides from the manifest on disk; a later node-local checkpoint at the same path is still rejected on another node. The inferred value still decides scheduling when this process can't read the manifest. ray-project#67485 (comment) ray-project#67485 (comment) - checkpoint_utils.py, sandboxes.md: copying a checkpoint onto storage where root can't set owners (NFS root_squash) fails with an error that says so, and the docs say that shared storage needs no_root_squash, since checkpoints keep the owners of the sandbox's files. ray-project#67485 (comment) Signed-off-by: Sunny Song <[email protected]>
…NDBOX_BACKEND Signed-off-by: Sunny Song <[email protected]>
Signed-off-by: Sunny Song <[email protected]>
This branch has not been deployed
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.
Why are these changes needed?
ray.experimental.sandboxhas only one backend: gVisor sandboxes on the Ray node. Users who already run Agent Substrate want to run the sameSandboxRuntime/Sandbox/ HTTP-API code against it, without changing that code. Substrate provides snapshot-based suspend, wake, and fork of sandboxes on a separate fleet. This PR makes the backend selectable and adds a Substrate backend.What does this PR do?
There are two commits, which can be reviewed separately.
1.
[sandbox] Let SandboxRuntime select its backend by argument or RAY_SANDBOX_BACKENDBackend-agnostic. gVisor stays the default and its behavior is unchanged.
SandboxRuntime(backend=...)takes a backend instance or a name. With no argument, it readsRAY_SANDBOX_BACKEND, and falls back togvisorwhen that is unset.gvisor,substrate);ray.sandbox_backendsgroup, so out-of-tree backends need no Ray change;module:Classimport path.The registry lives in
backend/registry.py.BaseSandboxBackendhooks:needs_image_manager: a backend that pulls images itself doesn't get a localImageManager.checkpoint_format:restore()refuses another backend's checkpoint, and the error names that backend.check_resources(): defaults to the existing local cgroup check.delete_checkpoint(): the default validates that the path is a checkpoint directory, then removes it. It refuses/,$HOME, and the cwd.SandboxRuntime:delete_checkpoint().restore()cancels an old TTL timer when the backend returns an id the runtime already tracks.commit()raisesNotImplementedErroron a backend that can't export a root filesystem.2.
[sandbox] Add a Substrate sandbox backendbackend/substrate.pyruns each sandbox as a Substrate environment. You select it withRAY_SANDBOX_BACKEND=substrate. Ray doesn't install its SDK:pip install ate-env-rl. The SDK is imported only when the backend is selected, and a test checks that the default gVisor path never imports it.Why the env-rl SDK and ate-env-api, rather than Substrate's control plane (ateapi) directly.
StartProcess/ReadFile/WriteFile;rootfs_image;ForkEnvironment;Semantics (differences from gVisor are also in the docs and the module docstring):
create(...)NODE_NAME→ate-env/domain). Fails closed on options it can't honor:mounts,cidr_allowlist,dns, non-defaultcapabilities,gpu_ids, non-zerodisk,oom_score_adj, andnetworkother than"none".cpu/memoryare logged, not enforced: the template sizes the sandbox.env,workdir, andshellbehave as on gVisor.exec(...)[shell, "-c", cmd]with the configured shell (default/bin/bash).timeout=Nonemeans no deadline, as on gVisor.user=isn't supported.checkpoint(sid, leave_running=False)exec(sid)then raises anddelete(sid)is a no-op.checkpoint(sid, leave_running=True)checkpoint(sid)without a pathsubstrate://CheckpointPathhandle that restores on any node.checkpoint(sid, checkpoint_path, remote=...)manifest.json(plus asubstratesection) andstate/, soray.experimental.sandbox.restore()andSandbox(restore_from=...)work. Refuses a non-empty directory that holds no checkpoint.restore(checkpoint)(default)ForkEnvironmentcall. The checkpoint stays and can be restored any number of times. Substrate refuses concurrent forks of one environment, so concurrent copies of one checkpoint serialize: they retry ABORTED with jittered backoff until the call'stimeout_seconds. Server-side coalescing is tracked in gke-labs/env-rl#69.restore(checkpoint, resume_in_place=True)(orRAY_SUBSTRATE_RESTORE_MODE=resume)SandboxError. It fits per-step suspend/resume loops.SandboxRuntime.delete_checkpoint(checkpoint)pause()/resume()Robustness.
flockon<path>/.substrate.lock. Copies read the atomically replaced manifest without a lock: they re-read it before each fork attempt, and check again after the fork. A copy therefore never forks a checkpoint that was resumed in place or deleted.os.fork()(register_at_fork), shuts down at exit, and keeps its per-process maps bounded.Configuration uses
RAY_SUBSTRATE_*env vars (endpoint, auth token or token file, atespace, templates per domain, node→domain map, restore mode, suspend mode, RPC timeout). They are documented indoc/source/core/sandboxes.mdunder "Sandbox backends".Dependencies and known limitations
checkpoint(leave_running=True)needClient.fork(ForkEnvironment), which is in env-rl commit 6fd75328 but not yet in an ate-env-rl release; added in ate-env-api: ForkEnvironment copies one environment N times gke-labs/env-rl#70, not yet released. The backend checks both the SDK (hasattr(Client, "fork")) and the server (fork_environmentinGetServerInfo.features), and points toresume_in_place=Trueotherwise.MIN_FORK_SDK_RELEASEwill be set when the release exists.grpcio>=1.85.1andprotobuf>=7.35. Ray's compiled constraints pingrpcio==1.85.0andprotobuf==5.27.5(Python < 3.12) /6.33.6(Python ≥ 3.12). So installing the SDK upgrades both past Ray's tested pins. Ray itself doesn't depend on the SDK. We will confirm that Ray's generated protobuf modules load on protobuf 7 before leaving draft.timeout_secondsaccordingly.substrate://handles coordinate within one process.Benchmark
The unchanged Ray Sandbox RL benchmark (
ray_prime_rl.py), with only the backend switched: 36,841 images × 6 rollouts × 40 steps = 8.84M tasks on the same 121 GKE nodes (9,328 vCPU). Each step wakes a suspended sandbox, runs a command and suspends it again. A fork pass in between copies 512 sandboxes 4 times each. Substrate run on this PR's exact code (c95449b663); the wake loop usesresume_in_place=Trueand the fork pass uses default copy restores.Input/output erroror hung. The fork check is the only benchmark step that runs a binary the image had not used yet (cat); plain sandboxes on the affected nodes failed the same way. Fork latencies above count only the copies that succeeded.Related issue number
ForkEnvironment(ate-env-api: ForkEnvironment copies one environment N times gke-labs/env-rl#70).Checks
-sflag, i.e.,git commit -s) in this PR.scripts/format.shequivalents).doc/source/core/sandboxes.md, "Sandbox backends" and "Agent Substrate backend".doc/source/tune/api/under the corresponding.rstfile. (SandboxRuntime(backend=...)andSandboxRuntime.delete_checkpointare alpha APIs underray.experimental.sandbox.)Tests. No cluster or SDK is needed:
tests/substrate_fake.pyis a file-backed fake of ate-env-api, loaded by path and shared across threads and processes. It models ABORTED on a busy source for both fork and delete.tests/test_backend_selection.py(15 tests):ImageManagerfor backends that don't need one;commit()guard;check_resourcesdelegation;delete_checkpointvalidation;tests/test_substrate_backend.py(51 tests):SandboxHostbooting on Substrate; configured shell.leave_running=True;restore();delete_checkpointand replacement while copies fork;os.fork()(runtimes built before and after the fork); shutdown and restart; bounded LRU maps.Sandboxactor checkpointing and restoring throughray.experimental.sandbox.restore().