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

[chunkwriting] Return original buffer to the pool #16067

Merged
merged 1 commit into from
Dec 16, 2021
Merged
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
35 changes: 15 additions & 20 deletions sdk/storage/azblob/chunkwriting.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type copier struct {
type copierChunk struct {
buffer []byte
id string
length int
}

// getErr returns an error by priority. First, if a function set an error, it returns that error. Next, if the Context has an error
Expand All @@ -138,37 +139,31 @@ func (c *copier) sendChunk() error {
}

n, err := io.ReadFull(c.reader, buffer)
switch {
case err == nil && n == 0:
return nil
case err == nil:
if n > 0 {
// Some data was read, schedule the write.
id := c.id.next()
c.wg.Add(1)
c.o.TransferManager.Run(
func() {
defer c.wg.Done()
c.write(copierChunk{buffer: buffer[0:n], id: id})
c.write(copierChunk{buffer: buffer, id: id, length: n})
},
)
return nil
case err != nil && (err == io.EOF || err == io.ErrUnexpectedEOF) && n == 0:
return io.EOF
} else {
// Return the unused buffer to the manager.
c.o.TransferManager.Put(buffer)
}

if err == io.EOF || err == io.ErrUnexpectedEOF {
id := c.id.next()
c.wg.Add(1)
c.o.TransferManager.Run(
func() {
defer c.wg.Done()
c.write(copierChunk{buffer: buffer[0:n], id: id})
},
)
if err == nil {
return nil
} else if err == io.EOF || err == io.ErrUnexpectedEOF {
return io.EOF
}
if err := c.getErr(); err != nil {
return err

if cerr := c.getErr(); cerr != nil {
return cerr
}

return err
}

Expand All @@ -180,7 +175,7 @@ func (c *copier) write(chunk copierChunk) {
return
}
stageBlockOptions := c.o.getStageBlockOptions()
_, err := c.to.StageBlock(c.ctx, chunk.id, internal.NopCloser(bytes.NewReader(chunk.buffer)), stageBlockOptions)
_, err := c.to.StageBlock(c.ctx, chunk.id, internal.NopCloser(bytes.NewReader(chunk.buffer[:chunk.length])), stageBlockOptions)
if err != nil {
c.errCh <- fmt.Errorf("write error: %w", err)
return
Expand Down