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

[storage][azblob]DownloadFile: Download large files serially #21259

Merged
merged 19 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
162 changes: 157 additions & 5 deletions sdk/storage/azblob/blob/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ package blob

import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
"io"
"os"
"sync"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
Expand Down Expand Up @@ -314,8 +315,8 @@ func (b *Client) GetSASURL(permissions sas.BlobPermissions, expiry time.Time, o

// Concurrent Download Functions -----------------------------------------------------------------------------------------

// download downloads an Azure blob to a WriterAt in parallel.
func (b *Client) download(ctx context.Context, writer io.WriterAt, o downloadOptions) (int64, error) {
// downloadBuffer downloads an Azure blob to a WriterAt in parallel.
func (b *Client) downloadBuffer(ctx context.Context, writer io.WriterAt, o downloadOptions) (int64, error) {
if o.BlockSize == 0 {
o.BlockSize = DefaultDownloadBlockSize
}
Expand Down Expand Up @@ -381,6 +382,157 @@ func (b *Client) download(ctx context.Context, writer io.WriterAt, o downloadOpt
return count, nil
}

// downloadFile downloads an Azure blob to a Writer. The blocks are downloaded parallely,
// but written to file serially
func (b *Client) downloadFile(ctx context.Context, writer io.Writer, o downloadOptions) (int64, error) {
ctx, cancel := context.WithCancel(ctx)
vibhansa-msft marked this conversation as resolved.
Show resolved Hide resolved
defer cancel()
if o.BlockSize == 0 {
vibhansa-msft marked this conversation as resolved.
Show resolved Hide resolved
o.BlockSize = DefaultDownloadBlockSize
}

if o.Concurrency == 0 {
o.Concurrency = defaultConcurrency
}

count := o.Range.Count
if count == CountToEnd { // If size not specified, calculate it
// If we don't have the length at all, get it
gr, err := b.GetProperties(ctx, o.getBlobPropertiesOptions())
if err != nil {
return 0, err
}
count = *gr.ContentLength - o.Range.Offset
}

if count <= 0 {
// The file is empty, there is nothing to download.
return 0, nil
}

progress := int64(0)
progressLock := &sync.Mutex{}

// helper routine to get body
getBodyForRange := func(ctx context.Context, chunkStart, size int64) (io.ReadCloser, error) {
downloadBlobOptions := o.getDownloadBlobOptions(HTTPRange{
Offset: chunkStart + o.Range.Offset,
Count: size,
}, nil)
dr, err := b.DownloadStream(ctx, downloadBlobOptions)
if err != nil {
return nil, err
}

var body io.ReadCloser = dr.NewRetryReader(ctx, &o.RetryReaderOptionsPerBlock)
if o.Progress != nil {
rangeProgress := int64(0)
body = streaming.NewResponseProgress(
body,
func(bytesTransferred int64) {
diff := bytesTransferred - rangeProgress
rangeProgress = bytesTransferred
progressLock.Lock()
progress += diff
o.Progress(progress)
progressLock.Unlock()
})
}

return body, nil
}

// if file fits in a single buffer, we'll download here.
if count <= o.BlockSize {
body, err := getBodyForRange(ctx, int64(0), count)
if err != nil {
return 0, err
}

return io.Copy(writer, body)
}

buffers := shared.NewMMBPool(int(o.Concurrency), o.BlockSize)
defer buffers.Free()
aquireBuffer := func() ([]byte, error) {
select {
case b := <-buffers.Acquire():
// got a buffer
return b, nil
default:
// no buffer available; allocate a new buffer if possible
if _, err := buffers.Grow(); err != nil {
return nil, err
}

// either grab the newly allocated buffer or wait for one to become available
return <-buffers.Acquire(), nil
}
}

numChunks := uint16((count-1)/o.BlockSize) + 1
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved
blocks := make([]chan []byte, numChunks)
for b := range blocks {
blocks[b] = make(chan []byte)
}

writerError := make(chan error)
go func(ch chan error) {
for _, block := range blocks {
select {
case <-ctx.Done():
return
case block := <-block:
_, err := writer.Write(block)
buffers.Release(block)
if err != nil {
ch <- err
return
}
}
}
ch <- nil
}(writerError)

// Prepare and do parallel download.
err := shared.DoBatchTransfer(ctx, &shared.BatchTransferOptions{
OperationName: "downloadBlobToWriterAt",
TransferSize: count,
ChunkSize: o.BlockSize,
Concurrency: o.Concurrency,
Operation: func(ctx context.Context, chunkStart int64, count int64) error {
body, err := getBodyForRange(ctx, chunkStart, count)
if err != nil {
return nil
}

buff, err := aquireBuffer()
if err != nil {
return err
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved
}

_, err = io.ReadFull(body, buff[:count])
if err != nil {
return err
nakulkar-msft marked this conversation as resolved.
Show resolved Hide resolved
}
body.Close()

blockIndex := (chunkStart / o.BlockSize)
blocks[blockIndex] <- buff
return nil
},
})

if err != nil {
return 0, err
}
// error from writer thread.
if err = <-writerError; err != nil {
return 0, err
}
return count, nil
}

// DownloadStream reads a range of bytes from a blob. The response also includes the blob's properties and metadata.
// For more information, see https://docs.microsoft.com/rest/api/storageservices/get-blob.
func (b *Client) DownloadStream(ctx context.Context, o *DownloadStreamOptions) (DownloadStreamResponse, error) {
Expand Down Expand Up @@ -409,7 +561,7 @@ func (b *Client) DownloadBuffer(ctx context.Context, buffer []byte, o *DownloadB
if o == nil {
o = &DownloadBufferOptions{}
}
return b.download(ctx, shared.NewBytesWriter(buffer), (downloadOptions)(*o))
return b.downloadBuffer(ctx, shared.NewBytesWriter(buffer), (downloadOptions)(*o))
}

// DownloadFile downloads an Azure blob to a local file.
Expand Down Expand Up @@ -448,7 +600,7 @@ func (b *Client) DownloadFile(ctx context.Context, file *os.File, o *DownloadFil
}

if size > 0 {
return b.download(ctx, file, *do)
return b.downloadFile(ctx, file, *do)
} else { // if the blob's size is 0, there is no need in downloading it
return 0, nil
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/storage/azblob/blob/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (

// DefaultDownloadBlockSize is default block size
DefaultDownloadBlockSize = int64(4 * 1024 * 1024) // 4MB

defaultConcurrency = 5
nakulkar-msft marked this conversation as resolved.
Show resolved Hide resolved
)

// BlobType defines values for BlobType
Expand Down
68 changes: 2 additions & 66 deletions sdk/storage/azblob/blockblob/chunkwriting.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
"github.com/Azure/azure-sdk-for-go/sdk/internal/uuid"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/internal/shared"
)

// blockWriter provides methods to upload blocks that represent a file to a server and commit them.
Expand All @@ -28,27 +29,8 @@ type blockWriter interface {
CommitBlockList(context.Context, []string, *CommitBlockListOptions) (CommitBlockListResponse, error)
}

// bufferManager provides an abstraction for the management of buffers.
// this is mostly for testing purposes, but does allow for different implementations without changing the algorithm.
type bufferManager[T ~[]byte] interface {
// Acquire returns the channel that contains the pool of buffers.
Acquire() <-chan T

// Release releases the buffer back to the pool for reuse/cleanup.
Release(T)

// Grow grows the number of buffers, up to the predefined max.
// It returns the total number of buffers or an error.
// No error is returned if the number of buffers has reached max.
// This is called only from the reading goroutine.
Grow() (int, error)

// Free cleans up all buffers.
Free()
}

// copyFromReader copies a source io.Reader to blob storage using concurrent uploads.
func copyFromReader[T ~[]byte](ctx context.Context, src io.Reader, dst blockWriter, options UploadStreamOptions, getBufferManager func(maxBuffers int, bufferSize int64) bufferManager[T]) (CommitBlockListResponse, error) {
func copyFromReader[T ~[]byte](ctx context.Context, src io.Reader, dst blockWriter, options UploadStreamOptions, getBufferManager func(maxBuffers int, bufferSize int64) shared.BufferManager[T]) (CommitBlockListResponse, error) {
options.setDefaults()

wg := sync.WaitGroup{} // Used to know when all outgoing blocks have finished processing
Expand Down Expand Up @@ -265,49 +247,3 @@ func (ubi uuidBlockID) WithBlockNumber(blockNumber uint32) uuidBlockID {
func (ubi uuidBlockID) ToBase64() string {
return blockID(ubi).ToBase64()
}

// mmbPool implements the bufferManager interface.
// it uses anonymous memory mapped files for buffers.
// don't use this type directly, use newMMBPool() instead.
type mmbPool struct {
buffers chan mmb
count int
max int
size int64
}

func newMMBPool(maxBuffers int, bufferSize int64) bufferManager[mmb] {
return &mmbPool{
buffers: make(chan mmb, maxBuffers),
max: maxBuffers,
size: bufferSize,
}
}

func (pool *mmbPool) Acquire() <-chan mmb {
return pool.buffers
}

func (pool *mmbPool) Grow() (int, error) {
if pool.count < pool.max {
buffer, err := newMMB(pool.size)
if err != nil {
return 0, err
}
pool.buffers <- buffer
pool.count++
}
return pool.count, nil
}

func (pool *mmbPool) Release(buffer mmb) {
pool.buffers <- buffer
}

func (pool *mmbPool) Free() {
for i := 0; i < pool.count; i++ {
buffer := <-pool.buffers
buffer.delete()
}
pool.count = 0
}
15 changes: 8 additions & 7 deletions sdk/storage/azblob/blockblob/chunkwriting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/internal/shared"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -115,19 +116,19 @@ func calcMD5(data []byte) string {

// used to track proper acquisition and closing of buffers
type bufMgrTracker struct {
inner bufferManager[mmb]
inner shared.BufferManager[shared.Mmb]

Count int // total count of allocated buffers
Freed bool // buffers were freed
}

func newBufMgrTracker(maxBuffers int, bufferSize int64) *bufMgrTracker {
return &bufMgrTracker{
inner: newMMBPool(maxBuffers, bufferSize),
inner: shared.NewMMBPool(maxBuffers, bufferSize),
}
}

func (pool *bufMgrTracker) Acquire() <-chan mmb {
func (pool *bufMgrTracker) Acquire() <-chan shared.Mmb {
return pool.inner.Acquire()
}

Expand All @@ -140,7 +141,7 @@ func (pool *bufMgrTracker) Grow() (int, error) {
return n, nil
}

func (pool *bufMgrTracker) Release(buffer mmb) {
func (pool *bufMgrTracker) Release(buffer shared.Mmb) {
pool.inner.Release(buffer)
}

Expand All @@ -161,7 +162,7 @@ func TestSlowDestCopyFrom(t *testing.T) {

errs := make(chan error, 1)
go func() {
_, err := copyFromReader(context.Background(), bytes.NewReader(bigSrc), fakeBB, UploadStreamOptions{}, func(maxBuffers int, bufferSize int64) bufferManager[mmb] {
_, err := copyFromReader(context.Background(), bytes.NewReader(bigSrc), fakeBB, UploadStreamOptions{}, func(maxBuffers int, bufferSize int64) shared.BufferManager[shared.Mmb] {
tracker = newBufMgrTracker(maxBuffers, bufferSize)
return tracker
})
Expand Down Expand Up @@ -270,7 +271,7 @@ func TestCopyFromReader(t *testing.T) {

var tracker *bufMgrTracker

_, err := copyFromReader(test.ctx, bytes.NewReader(from), fakeBB, test.o, func(maxBuffers int, bufferSize int64) bufferManager[mmb] {
_, err := copyFromReader(test.ctx, bytes.NewReader(from), fakeBB, test.o, func(maxBuffers int, bufferSize int64) shared.BufferManager[shared.Mmb] {
tracker = newBufMgrTracker(maxBuffers, bufferSize)
return tracker
})
Expand Down Expand Up @@ -322,7 +323,7 @@ func TestCopyFromReaderReadError(t *testing.T) {
reader: bytes.NewReader(make([]byte, 5*_1MiB)),
failOn: 2,
}
_, err := copyFromReader(context.Background(), &rf, fakeBB, UploadStreamOptions{}, func(maxBuffers int, bufferSize int64) bufferManager[mmb] {
_, err := copyFromReader(context.Background(), &rf, fakeBB, UploadStreamOptions{}, func(maxBuffers int, bufferSize int64) shared.BufferManager[shared.Mmb] {
tracker = newBufMgrTracker(maxBuffers, bufferSize)
return tracker
})
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/azblob/blockblob/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func (bb *Client) UploadStream(ctx context.Context, body io.Reader, o *UploadStr
o = &UploadStreamOptions{}
}

result, err := copyFromReader(ctx, body, bb, *o, newMMBPool)
result, err := copyFromReader(ctx, body, bb, *o, shared.NewMMBPool)
if err != nil {
return CommitBlockListResponse{}, err
}
Expand Down
Loading