Skip to content
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

s3: fix s3 concurrent uploader will overwrite error #48163

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions br/pkg/storage/ks3.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,9 @@ func (rs *KS3Storage) Create(ctx context.Context, name string, option *WriterOpt
s3Writer.wg.Add(1)
go func() {
_, err := up.Upload(upParams)
err1 := rd.Close()
if err != nil {
log.Warn("upload to s3 failed", zap.String("filename", name), zap.Error(err), zap.Error(err1))
log.Warn("upload to ks3 failed", zap.String("filename", name), zap.Error(err))
_ = rd.CloseWithError(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

so there's no need to close rd when Upload returns no err?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I see PipeReader does not release resources in Close.

Copy link
Contributor

Choose a reason for hiding this comment

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

better call it or explain in the comments, looks like a bug

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
s3Writer.err = err
s3Writer.wg.Done()
Expand Down
4 changes: 2 additions & 2 deletions br/pkg/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,9 @@ func (rs *S3Storage) Create(ctx context.Context, name string, option *WriterOpti
s3Writer.wg.Add(1)
go func() {
_, err := up.UploadWithContext(ctx, upParams)
err1 := rd.Close()
if err != nil {
log.Warn("upload to s3 failed", zap.String("filename", name), zap.Error(err), zap.Error(err1))
log.Warn("upload to s3 failed", zap.String("filename", name), zap.Error(err))
_ = rd.CloseWithError(err)
}
s3Writer.err = err
s3Writer.wg.Done()
Expand Down
18 changes: 18 additions & 0 deletions br/pkg/storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,24 @@ func TestWriteNoError(t *testing.T) {
require.NoError(t, err)
}

func TestMultiUploadErrorNotOverwritten(t *testing.T) {
s := createS3Suite(t)
ctx := aws.BackgroundContext()

s.s3.EXPECT().
CreateMultipartUploadWithContext(ctx, gomock.Any(), gomock.Any()).
Return(nil, errors.New("mock error"))

w, err := s.storage.Create(ctx, "file", &WriterOption{Concurrency: 2})
require.NoError(t, err)
// data should be larger than 5MB to trigger CreateMultipartUploadWithContext path
data := make([]byte, 5*1024*1024+6716)
n, err := w.Write(ctx, data)
require.NoError(t, err)
require.Equal(t, 5*1024*1024+6716, n)
require.ErrorContains(t, w.Close(ctx), "mock error")
}

// TestReadNoError ensures the ReadFile API issues a GetObject request and correctly
// read the entire body.
func TestReadNoError(t *testing.T) {
Expand Down