Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions pkg/backend/internal/patchcopy/copy.go
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Re-abort multipart uploads after canceling active copies

When one retained copy fails while other AWS UploadPartCopy requests are in flight, this cancellation can make their client calls return before S3 has necessarily stopped the server-side copies; workers.Wait therefore does not establish that those uploads have settled. CopyOrAbort subsequently aborts exactly once, but S3's AbortMultipartUpload contract 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 👍 / 👎.

})
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)
}
Loading
Loading