Repository navigation
[core][sandbox] Download image layers in parallel and decompress each once - #67023
Conversation
… once A cold pull downloaded an image's layers one at a time and extracted each only after its download finished, so a SWE-bench image (1.1 GiB in layers) spent 22 s downloading and 26 s extracting, one after the other. Extraction also listed each layer's members with getmembers() and then read them through extractfile(), which rewinds a compressed stream, so every layer was decompressed twice. Layers now download three at a time, as in Docker, into anonymous temporary files, and each is extracted in manifest order as soon as it and the layers before it have arrived. A failed download fails the pull at once; downloads still running stop at their next read. Extraction reads each layer in a single pass in tarfile's stream mode. Signed-off-by: xyuzh <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request optimizes the image layer extraction process by downloading registry blobs in parallel using a thread pool while ensuring they are applied in the correct order. Additionally, it updates extract_tar_layer to stream mode to prevent reading and decompressing layers twice, and introduces robust unit tests to verify parallel downloads and error handling. The feedback suggests checking the stop event before initiating network requests in _download_blob to prevent redundant connections if another layer has already failed.
| blob = tempfile.TemporaryFile(dir=blob_dir) | ||
| try: | ||
| req = _registry_request(url, headers, auth_header) |
There was a problem hiding this comment.
Checking stop.is_set() before initiating the network request with urllib.request.urlopen avoids making unnecessary network connections if the image pull has already been aborted due to a failure in another layer. This helps save network resources and avoids potential timeouts or rate-limiting issues on aborted pulls.
blob = tempfile.TemporaryFile(dir=blob_dir)
try:
if stop.is_set():
blob.seek(0)
return blob
req = _registry_request(url, headers, auth_header)There was a problem hiding this comment.
Fixed in 8a57812: _download_blob checks stop right after it creates the temporary file and returns before opening a connection. The empty file it returns is closed by the same done-callback that closes the other blobs of a failed pull.
…stopped A download that a worker picks up after the pull failed opened its connection before it checked whether to stop. It now checks first. Review follow-up. Signed-off-by: xyuzh <[email protected]>
Stream mode (r|*) read each layer once, but it doesn't check where a gzip stream ends. When a registry closes a blob response early, read1() returns no error, so a layer cut short on a member boundary extracted without one, and the image was cached with files missing. Master's GzipFile reader raised on every such cut: in 2,000 random cuts of a test layer, stream mode read 29 without an error. Iterating an r:* archive, instead of calling getmembers() first, also reads the layer once and keeps that check. On a 572 MiB layer it was a little faster than stream mode (1.75 s vs 1.93 s). Signed-off-by: xyuzh <[email protected]>
|
One more commit after the approval, 93604c1. Stream mode ( |
Signed-off-by: Philipp Moritz <[email protected]>
… once (ray-project#67023) ## Description Cold pulls of large images are slow. A SWE-bench image with 1.1 GiB of layers took 57-64 s to pull on a CoreWeave node, and two things wasted most of that time: - Layers were downloaded one at a time, and each was extracted only after its download finished. - Each layer was decompressed twice: `tar.getmembers()` reads the whole gzip stream, then `extractfile()` seeks back and decompresses it again. This PR downloads up to three layers at once (Docker's default) into temporary files and extracts them in manifest order as they arrive, so whiteouts and overrides apply exactly as before. A failed download fails the pull right away. The other downloads stop at their next read, and ones that haven't started never connect. `extract_tar_layer` iterates the archive instead of calling `getmembers()` first, so it reads each layer once. It keeps the `r:*` reader rather than stream mode (`r|*`), because only `r:*` checks where a gzip stream ends. That check catches a layer download the registry closed early. Same image, two cold pulls each, alternating: | | master | this PR | | --- | --- | --- | | Whole pull | 63.7 s / 56.7 s | 37.6 s / 37.8 s | | Download and extraction | 38.1 s / 33.0 s | 18.6 s / 16.8 s | This PR doesn't touch the EROFS build. It took 17-24 s in these runs, and both sides built the same 2.92 GiB image. ## Related issues None filed. Found while running SkyRL's SWE-bench Multimodal tasks on Ray Sandbox (skyrl-project/SkyRL#2467). ## Additional information - Not a duplicate: no open PR changes how layers are fetched or read. ray-project#66469, ray-project#66985 and ray-project#67026 edit other parts of `image_utils.py`. - Tests: four new tests. Three layer downloads must run at once and still apply in order. A failed layer must fail the pull within 5 s and close the other downloads. A layer download cut short on a member boundary must fail the pull. A 2 MiB layer must be read once. The first, second and fourth fail on master, and the third fails with stream mode. - Tested on a CoreWeave pod (runsc release-20261005.0, erofs-utils 1.9.4): `TEST_SANDBOX=1 pytest tests/test_gvisor_backend.py tests/test_image_manager.py tests/test_image_utils.py tests/test_sandbox_config.py tests/test_sandbox_runtime.py -k "not netns"`: 93 passed, 1 skipped. `-k netns`: 7 passed. pre-commit passes. - I used Claude Code to help write and test this change. --------- Signed-off-by: xyuzh <[email protected]> Signed-off-by: Philipp Moritz <[email protected]> Co-authored-by: Philipp Moritz <[email protected]> Signed-off-by: Pawel Beza <[email protected]> Co-authored-by: Copilot <[email protected]> Copilot-Session: 4b9df5c4-6ec3-4e1f-ac3e-54f320ebd8c4
Description
Cold pulls of large images are slow. A SWE-bench image with 1.1 GiB of layers took 57-64 s to pull on a CoreWeave node, and two things wasted most of that time:
tar.getmembers()reads the whole gzip stream, thenextractfile()seeks back and decompresses it again.This PR downloads up to three layers at once (Docker's default) into temporary files and extracts them in manifest order as they arrive, so whiteouts and overrides apply exactly as before. A failed download fails the pull right away. The other downloads stop at their next read, and ones that haven't started never connect.
extract_tar_layeriterates the archive instead of callinggetmembers()first, so it reads each layer once. It keeps ther:*reader rather than stream mode (r|*), because onlyr:*checks where a gzip stream ends. That check catches a layer download the registry closed early.Same image, two cold pulls each, alternating:
This PR doesn't touch the EROFS build. It took 17-24 s in these runs, and both sides built the same 2.92 GiB image.
Related issues
None filed. Found while running SkyRL's SWE-bench Multimodal tasks on Ray Sandbox (skyrl-project/SkyRL#2467).
Additional information
image_utils.py.TEST_SANDBOX=1 pytest tests/test_gvisor_backend.py tests/test_image_manager.py tests/test_image_utils.py tests/test_sandbox_config.py tests/test_sandbox_runtime.py -k "not netns": 93 passed, 1 skipped.-k netns: 7 passed. pre-commit passes.