-
Notifications
You must be signed in to change notification settings - Fork 9
feat(snapshot s3): download objects in parallel within a count and byte budget #1191
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4222ef3
feat(snapshot s3): download objects in parallel within a count and by…
mbevc1 9a243a3
feat(snapshot s3): add --download-concurrency and --download-budget f…
mbevc1 64e5446
refactor(snapshot s3): download with a fixed worker pool instead of a…
mbevc1 2503778
docs(adr): accept the record now that both deliveries have landed
mbevc1 b2bc926
refactor(snapshot s3): match the SDK's default part concurrency per o…
mbevc1 225124e
refactor(snapshot s3): keep only the comments that carry a fact the c…
mbevc1 390e787
fix(snapshot s3): start no more download workers than objects, and sa…
mbevc1 03c1247
fix(snapshot s3): reject "ib" and "bb" as byte-size units
mbevc1 bb7a69d
fix: align help text and testing
mbevc1 105bdd7
fix: missing testing Mutex
mbevc1 6960c08
chore: Update docs/adr/20260911-s3-fingerprint-from-virtual-tree.md
mbevc1 08113d1
fix(snapshot s3): restore the suite lock field and stop asserting fro…
mbevc1 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,66 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "math" | ||
| "strconv" | ||
| "strings" | ||
| "unicode" | ||
| ) | ||
|
|
||
| // byteSizeUnits maps a lower-cased unit, with any trailing "b" or "ib" already | ||
| // stripped, to bytes. Units are binary, as disk figures are. | ||
| var byteSizeUnits = map[string]int64{ | ||
| "": 1 << 20, // a bare number is megabytes | ||
| "b": 1, | ||
| "k": 1 << 10, | ||
| "m": 1 << 20, | ||
| "g": 1 << 30, | ||
| "t": 1 << 40, | ||
| } | ||
|
|
||
| // parseByteSize turns "512", "512M", "8GB" or "1.5G" into bytes: a bare number | ||
| // is megabytes, a K, M, G or T suffix takes an optional B, and "B" alone is bytes. | ||
| func parseByteSize(s string) (int64, error) { | ||
| s = strings.TrimSpace(s) | ||
| if s == "" { | ||
| return 0, errors.New("size is empty") | ||
| } | ||
|
|
||
| digits := 0 | ||
| for digits < len(s) && (s[digits] >= '0' && s[digits] <= '9' || s[digits] == '.') { | ||
| digits++ | ||
| } | ||
| number, unit := s[:digits], strings.TrimSpace(s[digits:]) | ||
| if number == "" || strings.ContainsFunc(unit, func(r rune) bool { return !unicode.IsLetter(r) }) { | ||
| return 0, fmt.Errorf("%q is not a size: expected a number with an optional K, M, G or T unit, e.g. 512M", s) | ||
| } | ||
| value, err := strconv.ParseFloat(number, 64) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("%q is not a size: expected a number with an optional K, M, G or T unit, e.g. 512M", s) | ||
| } | ||
|
|
||
| key := strings.ToLower(unit) | ||
| if key != "" && key != "b" { | ||
| // Only a single unit letter may precede the optional "b" or "ib", so | ||
| // "ib" alone and "bb" are unknown rather than a guess at megabytes or bytes. | ||
| key = strings.TrimSuffix(strings.TrimSuffix(key, "ib"), "b") | ||
| if len(key) != 1 || key == "b" { | ||
| return 0, fmt.Errorf("unknown unit %q in size %q: use K, M, G or T, optionally followed by B", unit, s) | ||
| } | ||
| } | ||
| multiplier, ok := byteSizeUnits[key] | ||
| if !ok { | ||
| return 0, fmt.Errorf("unknown unit %q in size %q: use K, M, G or T, optionally followed by B", unit, s) | ||
| } | ||
|
|
||
| bytes := value * float64(multiplier) | ||
| if bytes >= math.MaxInt64 { | ||
| return 0, fmt.Errorf("size %q is too large", s) | ||
| } | ||
| if bytes < 1 { | ||
| return 0, fmt.Errorf("size %q must be at least 1 byte", s) | ||
| } | ||
| return int64(bytes), nil | ||
| } | ||
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,68 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/kosli-dev/cli/internal/aws" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestParseByteSize(t *testing.T) { | ||
| const mib = int64(1) << 20 | ||
| for _, tc := range []struct { | ||
| input string | ||
| want int64 | ||
| wantErr string | ||
| }{ | ||
| {input: "512", want: 512 * mib}, | ||
| {input: "1", want: mib}, | ||
| {input: " 64 ", want: 64 * mib}, | ||
| {input: "512M", want: 512 * mib}, | ||
| {input: "512MB", want: 512 * mib}, | ||
| {input: "512mb", want: 512 * mib}, | ||
| {input: "512MiB", want: 512 * mib}, | ||
| {input: "8G", want: 8 << 30}, | ||
| {input: "8GB", want: 8 << 30}, | ||
| {input: "8 GB", want: 8 << 30}, | ||
| {input: "2T", want: 2 << 40}, | ||
| {input: "1024K", want: 1 << 20}, | ||
| {input: "4096KB", want: 4 << 20}, | ||
| {input: "1000B", want: 1000}, | ||
| {input: "1.5G", want: 3 << 29}, | ||
| {input: "0.5M", want: 512 << 10}, | ||
| {input: "", wantErr: "empty"}, | ||
| {input: "0", wantErr: "must be at least 1 byte"}, | ||
| {input: "0B", wantErr: "must be at least 1 byte"}, | ||
| {input: "-1", wantErr: "not a size"}, | ||
| {input: "-512M", wantErr: "not a size"}, | ||
| {input: "abc", wantErr: "not a size"}, | ||
| {input: "M", wantErr: "not a size"}, | ||
| {input: "512X", wantErr: `unknown unit "X"`}, | ||
| {input: "5ib", wantErr: `unknown unit "ib"`}, | ||
| {input: "5bb", wantErr: `unknown unit "bb"`}, | ||
| {input: "5KiBB", wantErr: `unknown unit "KiBB"`}, | ||
| {input: "512 megabytes", wantErr: `unknown unit "megabytes"`}, | ||
| {input: "1e3", wantErr: "not a size"}, | ||
| {input: "0x10", wantErr: "not a size"}, | ||
| {input: "1,024", wantErr: "not a size"}, | ||
| {input: "99999999999T", wantErr: "too large"}, | ||
| } { | ||
| t.Run(tc.input, func(t *testing.T) { | ||
| got, err := parseByteSize(tc.input) | ||
| if tc.wantErr != "" { | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), tc.wantErr) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| require.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // The flag default is a string, so it can drift from the value it spells. | ||
| func TestDefaultDownloadBudgetMatchesTheAwsDefault(t *testing.T) { | ||
| got, err := parseByteSize(defaultDownloadBudget) | ||
| require.NoError(t, err) | ||
| require.Equal(t, aws.DefaultDownloadLimits.BytesInFlight, got) | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.