-
Notifications
You must be signed in to change notification settings - Fork 12
perf(backend): parallelize retained PATCH copies #821
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
2 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,154 @@ | ||
| // Package patchcopy runs retained multipart copies with bounded concurrency. | ||
| package patchcopy | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // Copier copies an object range into one multipart part. | ||
| type Copier interface { | ||
| UploadPartCopy(ctx context.Context, destKey, uploadID string, partNumber int, sourceKey string, startByte, endByte int64) (string, error) | ||
| } | ||
|
|
||
| // Aborter aborts an incomplete multipart upload. | ||
| type Aborter interface { | ||
| AbortMultipartUpload(ctx context.Context, key, uploadID string) error | ||
| } | ||
|
|
||
| // Client supports both retained-part copies and multipart cleanup. | ||
| type Client interface { | ||
| Copier | ||
| Aborter | ||
| } | ||
|
|
||
| // Task describes one retained multipart range. | ||
| type Task struct { | ||
| PartNumber int | ||
| StartByte int64 | ||
| EndByte int64 | ||
| } | ||
|
|
||
| // PartError identifies the retained part whose copy failed. | ||
| type PartError struct { | ||
| PartNumber int | ||
| Err error | ||
| } | ||
|
|
||
| func (e *PartError) Error() string { | ||
| return fmt.Sprintf("copy part %d: %v", e.PartNumber, e.Err) | ||
| } | ||
|
|
||
| func (e *PartError) Unwrap() error { | ||
| return e.Err | ||
| } | ||
|
|
||
| // Copy runs every task with at most maxConcurrency in-flight requests. | ||
| func Copy( | ||
| ctx context.Context, | ||
| copier Copier, | ||
| destKey string, | ||
| uploadID string, | ||
| sourceKey string, | ||
| tasks []Task, | ||
| maxConcurrency int, | ||
| ) error { | ||
| if maxConcurrency <= 0 { | ||
| return fmt.Errorf("patch copy concurrency must be positive") | ||
| } | ||
| if err := ctx.Err(); err != nil { | ||
| return err | ||
| } | ||
| if len(tasks) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| copyCtx, cancel := context.WithCancel(ctx) | ||
| defer cancel() | ||
|
|
||
| workerCount := min(maxConcurrency, len(tasks)) | ||
| jobs := make(chan Task) | ||
|
|
||
| var workers sync.WaitGroup | ||
| var failureOnce sync.Once | ||
| var failure *PartError | ||
| workers.Add(workerCount) | ||
| for range workerCount { | ||
| go func() { | ||
| defer workers.Done() | ||
| for task := range jobs { | ||
| if copyCtx.Err() != nil { | ||
| return | ||
| } | ||
| if _, err := copier.UploadPartCopy( | ||
| copyCtx, | ||
| destKey, | ||
| uploadID, | ||
| task.PartNumber, | ||
| sourceKey, | ||
| task.StartByte, | ||
| task.EndByte, | ||
| ); err != nil { | ||
| failureOnce.Do(func() { | ||
| failure = &PartError{PartNumber: task.PartNumber, Err: err} | ||
| cancel() | ||
| }) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| feed: | ||
| for _, task := range tasks { | ||
| select { | ||
| case jobs <- task: | ||
| case <-copyCtx.Done(): | ||
| break feed | ||
| } | ||
| } | ||
| close(jobs) | ||
| workers.Wait() | ||
|
|
||
| if err := ctx.Err(); err != nil { | ||
| return err | ||
| } | ||
| if failure != nil { | ||
| return failure | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // CopyOrAbort waits for all workers to stop, then aborts once after failure. | ||
| func CopyOrAbort( | ||
| ctx context.Context, | ||
| client Client, | ||
| destKey string, | ||
| uploadID string, | ||
| sourceKey string, | ||
| tasks []Task, | ||
| maxConcurrency int, | ||
| abortTimeout time.Duration, | ||
| ) error { | ||
| copyErr := Copy(ctx, client, destKey, uploadID, sourceKey, tasks, maxConcurrency) | ||
| if copyErr == nil { | ||
| return nil | ||
| } | ||
| if abortErr := Abort(ctx, client, destKey, uploadID, abortTimeout); abortErr != nil { | ||
| return errors.Join(copyErr, fmt.Errorf("abort patch multipart upload: %w", abortErr)) | ||
| } | ||
| return copyErr | ||
| } | ||
|
|
||
| // Abort uses a detached bounded context so caller cancellation cannot skip cleanup. | ||
| func Abort(ctx context.Context, aborter Aborter, key string, uploadID string, timeout time.Duration) error { | ||
| if timeout <= 0 { | ||
| return fmt.Errorf("patch abort timeout must be positive") | ||
| } | ||
| abortCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), timeout) | ||
| defer cancel() | ||
| return aborter.AbortMultipartUpload(abortCtx, key, uploadID) | ||
| } | ||
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.
When one retained copy fails while other AWS
UploadPartCopyrequests are in flight, this cancellation can make their client calls return before S3 has necessarily stopped the server-side copies;workers.Waittherefore does not establish that those uploads have settled.CopyOrAbortsubsequently aborts exactly once, but S3'sAbortMultipartUploadcontract warns that in-progress uploads may finish after an abort and that repeated aborts can be necessary, so this failure path can leave orphaned, billable multipart parts. Either let active copies settle without canceling their request contexts or verify and retry the abort cleanup.Useful? React with 👍 / 👎.