-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 cache] Faster download with parallelism #5604
Open
bpaquet
wants to merge
1
commit into
moby:master
Choose a base branch
from
bpaquet:parallel_download
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+317
−151
Open
Changes from all commits
Commits
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 was deleted.
Oops, something went wrong.
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -34,36 +34,40 @@ import ( | |||||
) | ||||||
|
||||||
const ( | ||||||
attrBucket = "bucket" | ||||||
attrRegion = "region" | ||||||
attrPrefix = "prefix" | ||||||
attrManifestsPrefix = "manifests_prefix" | ||||||
attrBlobsPrefix = "blobs_prefix" | ||||||
attrName = "name" | ||||||
attrTouchRefresh = "touch_refresh" | ||||||
attrEndpointURL = "endpoint_url" | ||||||
attrAccessKeyID = "access_key_id" | ||||||
attrSecretAccessKey = "secret_access_key" | ||||||
attrSessionToken = "session_token" | ||||||
attrUsePathStyle = "use_path_style" | ||||||
attrUploadParallelism = "upload_parallelism" | ||||||
maxCopyObjectSize = 5 * 1024 * 1024 * 1024 | ||||||
attrBucket = "bucket" | ||||||
attrRegion = "region" | ||||||
attrPrefix = "prefix" | ||||||
attrManifestsPrefix = "manifests_prefix" | ||||||
attrBlobsPrefix = "blobs_prefix" | ||||||
attrName = "name" | ||||||
attrTouchRefresh = "touch_refresh" | ||||||
attrEndpointURL = "endpoint_url" | ||||||
attrAccessKeyID = "access_key_id" | ||||||
attrSecretAccessKey = "secret_access_key" | ||||||
attrSessionToken = "session_token" | ||||||
attrUsePathStyle = "use_path_style" | ||||||
attrUploadParallelism = "upload_parallelism" | ||||||
attrDownloadParallelism = "download_parallelism" | ||||||
attrDownloadPartSize = "download_part_size" | ||||||
maxCopyObjectSize = 5 * 1024 * 1024 * 1024 | ||||||
) | ||||||
|
||||||
type Config struct { | ||||||
Bucket string | ||||||
Region string | ||||||
Prefix string | ||||||
ManifestsPrefix string | ||||||
BlobsPrefix string | ||||||
Names []string | ||||||
TouchRefresh time.Duration | ||||||
EndpointURL string | ||||||
AccessKeyID string | ||||||
SecretAccessKey string | ||||||
SessionToken string | ||||||
UsePathStyle bool | ||||||
UploadParallelism int | ||||||
Bucket string | ||||||
Region string | ||||||
Prefix string | ||||||
ManifestsPrefix string | ||||||
BlobsPrefix string | ||||||
Names []string | ||||||
TouchRefresh time.Duration | ||||||
EndpointURL string | ||||||
AccessKeyID string | ||||||
SecretAccessKey string | ||||||
SessionToken string | ||||||
UsePathStyle bool | ||||||
UploadParallelism int | ||||||
DownloadParallelism int | ||||||
DownloadPartSize int | ||||||
} | ||||||
|
||||||
func getConfig(attrs map[string]string) (Config, error) { | ||||||
|
@@ -141,20 +145,48 @@ func getConfig(attrs map[string]string) (Config, error) { | |||||
uploadParallelism = uploadParallelismInt | ||||||
} | ||||||
|
||||||
downloadParallism := 4 | ||||||
downloadParallismStr, ok := attrs[attrDownloadParallelism] | ||||||
if ok { | ||||||
downloadParallismInt, err := strconv.Atoi(downloadParallismStr) | ||||||
if err != nil { | ||||||
return Config{}, errors.Errorf("download_parallelism must be a positive integer") | ||||||
} | ||||||
if downloadParallismInt <= 0 { | ||||||
return Config{}, errors.Errorf("download_parallelism must be a positive integer") | ||||||
} | ||||||
downloadParallism = downloadParallismInt | ||||||
} | ||||||
|
||||||
downloadPartSize := 5 * 1024 * 1024 | ||||||
downloadPartSizeStr, ok := attrs[attrDownloadPartSize] | ||||||
if ok { | ||||||
downloadPartSizeInt, err := strconv.Atoi(downloadPartSizeStr) | ||||||
if err != nil { | ||||||
return Config{}, errors.Errorf("download_part_size must be a positive integer") | ||||||
} | ||||||
if downloadPartSizeInt <= 0 { | ||||||
return Config{}, errors.Errorf("download_part_size must be a positive integer") | ||||||
} | ||||||
downloadParallism = downloadPartSizeInt | ||||||
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.
Suggested change
Right? |
||||||
} | ||||||
|
||||||
return Config{ | ||||||
Bucket: bucket, | ||||||
Region: region, | ||||||
Prefix: prefix, | ||||||
ManifestsPrefix: manifestsPrefix, | ||||||
BlobsPrefix: blobsPrefix, | ||||||
Names: names, | ||||||
TouchRefresh: touchRefresh, | ||||||
EndpointURL: endpointURL, | ||||||
AccessKeyID: accessKeyID, | ||||||
SecretAccessKey: secretAccessKey, | ||||||
SessionToken: sessionToken, | ||||||
UsePathStyle: usePathStyle, | ||||||
UploadParallelism: uploadParallelism, | ||||||
Bucket: bucket, | ||||||
Region: region, | ||||||
Prefix: prefix, | ||||||
ManifestsPrefix: manifestsPrefix, | ||||||
BlobsPrefix: blobsPrefix, | ||||||
Names: names, | ||||||
TouchRefresh: touchRefresh, | ||||||
EndpointURL: endpointURL, | ||||||
AccessKeyID: accessKeyID, | ||||||
SecretAccessKey: secretAccessKey, | ||||||
SessionToken: sessionToken, | ||||||
UsePathStyle: usePathStyle, | ||||||
UploadParallelism: uploadParallelism, | ||||||
DownloadParallelism: downloadParallism, | ||||||
DownloadPartSize: downloadPartSize, | ||||||
}, nil | ||||||
} | ||||||
|
||||||
|
@@ -385,22 +417,15 @@ func (i *importer) Resolve(ctx context.Context, _ ocispecs.Descriptor, id string | |||||
return solver.NewCacheManager(ctx, id, keysStorage, resultStorage), nil | ||||||
} | ||||||
|
||||||
type readerAt struct { | ||||||
ReaderAtCloser | ||||||
size int64 | ||||||
} | ||||||
|
||||||
func (r *readerAt) Size() int64 { | ||||||
return r.size | ||||||
} | ||||||
|
||||||
type s3Client struct { | ||||||
*s3.Client | ||||||
*manager.Uploader | ||||||
bucket string | ||||||
prefix string | ||||||
blobsPrefix string | ||||||
manifestsPrefix string | ||||||
bucket string | ||||||
prefix string | ||||||
blobsPrefix string | ||||||
manifestsPrefix string | ||||||
downloadParallelism int | ||||||
downloadPartSize int | ||||||
} | ||||||
|
||||||
func newS3Client(ctx context.Context, config Config) (*s3Client, error) { | ||||||
|
@@ -419,12 +444,14 @@ func newS3Client(ctx context.Context, config Config) (*s3Client, error) { | |||||
}) | ||||||
|
||||||
return &s3Client{ | ||||||
Client: client, | ||||||
Uploader: manager.NewUploader(client), | ||||||
bucket: config.Bucket, | ||||||
prefix: config.Prefix, | ||||||
blobsPrefix: config.BlobsPrefix, | ||||||
manifestsPrefix: config.ManifestsPrefix, | ||||||
Client: client, | ||||||
Uploader: manager.NewUploader(client), | ||||||
bucket: config.Bucket, | ||||||
prefix: config.Prefix, | ||||||
blobsPrefix: config.BlobsPrefix, | ||||||
manifestsPrefix: config.ManifestsPrefix, | ||||||
downloadParallelism: config.DownloadParallelism, | ||||||
downloadPartSize: config.DownloadPartSize, | ||||||
}, nil | ||||||
} | ||||||
|
||||||
|
@@ -454,19 +481,6 @@ func (s3Client *s3Client) getManifest(ctx context.Context, key string, config *v | |||||
return true, nil | ||||||
} | ||||||
|
||||||
func (s3Client *s3Client) getReader(ctx context.Context, key string) (io.ReadCloser, error) { | ||||||
input := &s3.GetObjectInput{ | ||||||
Bucket: &s3Client.bucket, | ||||||
Key: &key, | ||||||
} | ||||||
|
||||||
output, err := s3Client.GetObject(ctx, input) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
return output.Body, nil | ||||||
} | ||||||
|
||||||
func (s3Client *s3Client) saveMutableAt(ctx context.Context, key string, body io.Reader) error { | ||||||
input := &s3.PutObjectInput{ | ||||||
Bucket: &s3Client.bucket, | ||||||
|
@@ -587,10 +601,18 @@ func (s3Client *s3Client) touch(ctx context.Context, key string, size *int64) (e | |||||
} | ||||||
|
||||||
func (s3Client *s3Client) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) { | ||||||
readerAtCloser := toReaderAtCloser(func(offset int64) (io.ReadCloser, error) { | ||||||
return s3Client.getReader(ctx, s3Client.blobKey(desc.Digest)) | ||||||
}) | ||||||
return &readerAt{ReaderAtCloser: readerAtCloser, size: desc.Size}, nil | ||||||
key := s3Client.blobKey(desc.Digest) | ||||||
input := &s3DownloaderInput{ | ||||||
Bucket: &s3Client.bucket, | ||||||
Key: &key, | ||||||
S3Client: s3Client.Client, | ||||||
Size: desc.Size, | ||||||
Parallelism: s3Client.downloadParallelism, | ||||||
PartSize: s3Client.downloadPartSize, | ||||||
} | ||||||
downloader := newDownloader(input) | ||||||
|
||||||
return downloader.Download(ctx) | ||||||
} | ||||||
|
||||||
func (s3Client *s3Client) manifestKey(name string) string { | ||||||
|
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.
Also ultra nitpick: