-
Notifications
You must be signed in to change notification settings - Fork 6k
*: support concurrent write for S3 writer #45723
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"regexp" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
alicred "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" | ||
|
@@ -912,11 +913,59 @@ func (rs *S3Storage) CreateUploader(ctx context.Context, name string) (ExternalF | |
}, nil | ||
} | ||
|
||
// Create creates multi upload request. | ||
func (rs *S3Storage) Create(ctx context.Context, name string) (ExternalFileWriter, error) { | ||
uploader, err := rs.CreateUploader(ctx, name) | ||
type s3ObjectWriter struct { | ||
wd *io.PipeWriter | ||
wg *sync.WaitGroup | ||
err error | ||
} | ||
|
||
// Write implement the io.Writer interface. | ||
func (s *s3ObjectWriter) Write(ctx context.Context, p []byte) (int, error) { | ||
n, err := s.wd.Write(p) | ||
return n, err | ||
} | ||
|
||
// Close implement the io.Closer interface. | ||
func (s *s3ObjectWriter) Close(ctx context.Context) error { | ||
err := s.wd.Close() | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
s.wg.Wait() | ||
return s.err | ||
} | ||
|
||
// Create creates multi upload request. | ||
func (rs *S3Storage) Create(ctx context.Context, name string, option *WriterOption) (ExternalFileWriter, error) { | ||
var uploader ExternalFileWriter | ||
var err error | ||
if option == nil || option.Concurrency <= 1 { | ||
uploader, err = rs.CreateUploader(ctx, name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
up := s3manager.NewUploaderWithClient(rs.svc, func(u *s3manager.Uploader) { | ||
u.Concurrency = option.Concurrency | ||
u.BufferProvider = s3manager.NewBufferedReadSeekerWriteToPool(option.Concurrency * 8 * 1024 * 1024) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will we need to expose the 8MB as a configuration in future? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so, we can use hardcode for now. |
||
}) | ||
rd, wd := io.Pipe() | ||
upParams := &s3manager.UploadInput{ | ||
Bucket: aws.String(rs.options.Bucket), | ||
Key: aws.String(rs.options.Prefix + name), | ||
Body: rd, | ||
} | ||
s3Writer := &s3ObjectWriter{wd: wd, wg: &sync.WaitGroup{}} | ||
s3Writer.wg.Add(1) | ||
go func() { | ||
_, err := up.Upload(upParams) | ||
lance6716 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Warn("upload to s3 failed", zap.String("filename", name), zap.Error(err)) | ||
} | ||
s3Writer.err = err | ||
s3Writer.wg.Done() | ||
}() | ||
uploader = s3Writer | ||
} | ||
uploaderWriter := newBufferedWriter(uploader, hardcodedS3ChunkSize, NoCompression) | ||
return uploaderWriter, nil | ||
|
Uh oh!
There was an error while loading. Please reload this page.