-
Notifications
You must be signed in to change notification settings - Fork 223
atelet: preserve holes when copying a local checkpoint #769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Dmitry Berkovich (dberkov)
merged 3 commits into
agent-substrate:main
from
BenTheElder:atelet-sparse-copy
Aug 8, 2026
+484
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // maxKernelCopy caps a single copy_file_range request. The syscall takes an int | ||
| // length, and a bounded request keeps one call from monopolising the thread. | ||
| const maxKernelCopy = 1 << 30 | ||
|
|
||
| // kernelCopyRange copies up to length bytes at off from srcFd to dstFd without the | ||
| // data crossing into userspace, and reports how much it copied (short copies are | ||
| // normal, so callers must loop). | ||
| // | ||
| // It reports errKernelCopyUnsupported when the kernel or filesystem cannot do the | ||
| // copy — most commonly EXDEV, when source and destination are on different | ||
| // filesystems — so the caller can fall back to a userspace copy. | ||
| func kernelCopyRange(srcFd, dstFd int, off, length int64) (int64, error) { | ||
| if length > maxKernelCopy { | ||
| length = maxKernelCopy | ||
| } | ||
| for { | ||
| // The offsets are only advanced by bytes the call actually copied, so a | ||
| // retry after EINTR resumes from the right place. | ||
| roff, woff := off, off | ||
| n, err := unix.CopyFileRange(srcFd, &roff, dstFd, &woff, int(length), 0) | ||
| switch { | ||
| case err == nil: | ||
| if n == 0 { | ||
| // No error and no progress: treat as unsupported rather than spin. | ||
| return 0, errKernelCopyUnsupported | ||
| } | ||
| return int64(n), nil | ||
| case errors.Is(err, unix.EINTR): | ||
| // A copy this large is interruptible, and the Go runtime signals | ||
| // goroutines for preemption, so this is expected rather than fatal. | ||
| continue | ||
| case errors.Is(err, unix.ENOSYS), // pre-4.5 kernel, or blocked by seccomp | ||
| errors.Is(err, unix.EXDEV), // different filesystems | ||
| errors.Is(err, unix.EOPNOTSUPP), // filesystem does not implement it | ||
| errors.Is(err, unix.EPERM), // e.g. append-only destination | ||
| errors.Is(err, unix.EINVAL), // ranges or flags this kernel rejects | ||
| errors.Is(err, unix.EBADF): // not both regular files | ||
| return 0, errKernelCopyUnsupported | ||
| default: | ||
| return 0, err | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //go:build linux | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestKernelCopyRange exercises the copy_file_range path directly. copySparse falls | ||
| // back to userspace when the kernel refuses, so without this the syscall could be | ||
| // broken and every other test would still pass. | ||
| func TestKernelCopyRange(t *testing.T) { | ||
| const ( | ||
| size = 1 << 20 | ||
| off = 64 << 10 | ||
| ) | ||
| dir := t.TempDir() | ||
|
|
||
| payload := bytes.Repeat([]byte{0x5A}, 128<<10) | ||
| srcPath := filepath.Join(dir, "src") | ||
| src, err := os.Create(srcPath) | ||
| if err != nil { | ||
| t.Fatalf("creating src: %v", err) | ||
| } | ||
| defer src.Close() | ||
| if err := src.Truncate(size); err != nil { | ||
| t.Fatalf("sizing src: %v", err) | ||
| } | ||
| if _, err := src.WriteAt(payload, off); err != nil { | ||
| t.Fatalf("writing src: %v", err) | ||
| } | ||
| if err := src.Sync(); err != nil { | ||
| t.Fatalf("syncing src: %v", err) | ||
| } | ||
|
|
||
| dstPath := filepath.Join(dir, "dst") | ||
| dst, err := os.Create(dstPath) | ||
| if err != nil { | ||
| t.Fatalf("creating dst: %v", err) | ||
| } | ||
| defer dst.Close() | ||
| if err := dst.Truncate(size); err != nil { | ||
| t.Fatalf("sizing dst: %v", err) | ||
| } | ||
|
|
||
| // Short copies are legal, so loop like copySparse does. | ||
| remaining := int64(len(payload)) | ||
| pos := int64(off) | ||
| for remaining > 0 { | ||
| n, err := kernelCopyRange(int(src.Fd()), int(dst.Fd()), pos, remaining) | ||
| if errors.Is(err, errKernelCopyUnsupported) { | ||
| t.Skipf("copy_file_range unavailable on this filesystem after %d of %d bytes", | ||
| int64(len(payload))-remaining, int64(len(payload))) | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("kernelCopyRange at %d: %v", pos, err) | ||
| } | ||
| if n <= 0 { | ||
| t.Fatalf("kernelCopyRange reported %d bytes copied", n) | ||
| } | ||
| pos += n | ||
| remaining -= n | ||
| } | ||
|
|
||
| got := make([]byte, len(payload)) | ||
| if _, err := dst.ReadAt(got, off); err != nil { | ||
| t.Fatalf("reading dst: %v", err) | ||
| } | ||
| if !bytes.Equal(got, payload) { | ||
| t.Error("copied range does not match the source") | ||
| } | ||
|
|
||
| // Everything outside the copied range must still be untouched. | ||
| head := make([]byte, off) | ||
| if _, err := dst.ReadAt(head, 0); err != nil { | ||
| t.Fatalf("reading dst head: %v", err) | ||
| } | ||
| if !bytes.Equal(head, make([]byte, off)) { | ||
| t.Error("copy wrote outside the requested range") | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //go:build !linux | ||
|
|
||
| package main | ||
|
|
||
| // kernelCopyRange has no implementation off Linux (atelet runs on Linux; this keeps | ||
| // the package building for local development on other platforms), so callers copy | ||
| // through userspace. | ||
| func kernelCopyRange(_, _ int, _, _ int64) (int64, error) { | ||
| return 0, errKernelCopyUnsupported | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxKernelCopy is set to 1 << 30 (1 GiB). Performing a 1 GiB copy_file_range syscall can take hundreds of milliseconds to seconds. In Go 1.14+, the Go runtime sends SIGURG signals for asynchronous goroutine preemption. If a signal is caught while copy_file_range is in progress before any bytes are transferred, unix.CopyFileRange returns unix.EINTR.
Currently, unix.EINTR is not handled in kernelCopyRange nor caught as unsupported in copySparse. As a result, copySparse returns copying ...: interrupted system call, aborting the entire checkpoint restore on a transient signal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed