-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add disk space pre-check for build and pull #471
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
Open
aftersnow
wants to merge
3
commits into
main
Choose a base branch
from
feature/disk-space-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+355
−4
Open
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
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
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
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
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,110 @@ | ||
| /* | ||
| * Copyright 2025 The CNAI Authors | ||
| * | ||
| * 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 diskspace | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
aftersnow marked this conversation as resolved.
Show resolved
Hide resolved
aftersnow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| const ( | ||
| // safetyMargin is the extra space ratio to account for metadata overhead | ||
| // (manifests, temporary files, etc.). 10% extra required. | ||
| safetyMargin = 1.1 | ||
| ) | ||
|
|
||
| // Check checks if the directory has enough disk space for the required bytes. | ||
| // It returns a descriptive error if space is insufficient, or nil if space is enough. | ||
| // The caller should use the returned error for warning purposes only and not | ||
| // treat it as a fatal error. | ||
| func Check(dir string, requiredBytes int64) error { | ||
| if requiredBytes <= 0 { | ||
| return nil | ||
| } | ||
|
|
||
| // Ensure the directory exists for statfs; walk up to find an existing parent. | ||
| checkDir := dir | ||
| for { | ||
| if _, err := os.Stat(checkDir); err == nil { | ||
| break | ||
| } | ||
| parent := filepath.Dir(checkDir) | ||
| if parent == checkDir { | ||
| // Reached filesystem root without finding an existing directory. | ||
| return fmt.Errorf("cannot determine disk space: no existing directory found for path %s", dir) | ||
| } | ||
| checkDir = parent | ||
| } | ||
|
|
||
| var stat unix.Statfs_t | ||
| if err := unix.Statfs(checkDir, &stat); err != nil { | ||
| return fmt.Errorf("failed to check disk space for %s: %w", dir, err) | ||
| } | ||
|
|
||
| // Available space for non-root users. | ||
| // Guard against overflow: on Linux Bavail is uint64, and values exceeding | ||
| // math.MaxInt64 would wrap negative when cast to int64. Cap at MaxInt64. | ||
| bavail := stat.Bavail | ||
| bsize := uint64(stat.Bsize) | ||
| var availableBytes int64 | ||
| if bavail > 0 && bsize > uint64(math.MaxInt64)/bavail { | ||
| availableBytes = math.MaxInt64 | ||
| } else { | ||
| availableBytes = int64(bavail * bsize) | ||
| } | ||
| requiredWithMargin := int64(float64(requiredBytes) * safetyMargin) | ||
|
|
||
| if availableBytes < requiredWithMargin { | ||
| return fmt.Errorf( | ||
| "insufficient disk space in %s: available %s, required %s (with 10%% safety margin)", | ||
| dir, formatBytes(availableBytes), formatBytes(requiredWithMargin), | ||
| ) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // formatBytes formats bytes into a human-readable string. | ||
| func formatBytes(bytes int64) string { | ||
| if bytes < 0 { | ||
| return "0 B" | ||
| } | ||
|
|
||
| const ( | ||
| kb = 1024 | ||
| mb = kb * 1024 | ||
| gb = mb * 1024 | ||
| tb = gb * 1024 | ||
| ) | ||
|
|
||
| switch { | ||
| case bytes >= tb: | ||
| return fmt.Sprintf("%.2f TB", float64(bytes)/float64(tb)) | ||
| case bytes >= gb: | ||
| return fmt.Sprintf("%.2f GB", float64(bytes)/float64(gb)) | ||
| case bytes >= mb: | ||
| return fmt.Sprintf("%.2f MB", float64(bytes)/float64(mb)) | ||
| case bytes >= kb: | ||
| return fmt.Sprintf("%.2f KB", float64(bytes)/float64(kb)) | ||
| default: | ||
| return fmt.Sprintf("%d B", bytes) | ||
| } | ||
| } | ||
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.
estimateBuildSizetreats each Modelfile entry as a literal path (filepath.Join+os.Stat) and skips it on stat errors, so wildcard entries (e.g.models/*.bin) and absolute paths are not counted. The build path expansion logic inprocessor/base.godoes handle those patterns, so this pre-check can report enough space even when large matched files will still be built, which defeats the new warning for common Modelfile inputs and leads to avoidable disk-full failures during build.Useful? React with 👍 / 👎.