Skip to content

Cap the amount of buffering done by BytesPipe - #17877

Merged
unclejack merged 1 commit into
moby:masterfrom
aaronlehmann:capped-bytespipe
Nov 13, 2015
Merged

unclejack merged 1 commit into
moby:masterfrom
aaronlehmann:capped-bytespipe

Conversation

@aaronlehmann

Copy link
Copy Markdown

Previously, BytesPipe would grow without a limit. This was dangerous if
data was written to it faster than it was read, it would exhaust available
memory.

Turn BytesPipe's Read and Write functions into blocking, goroutine-safe
functions. Add a CloseWithError function to propagate an error code to
the Read function.

Adjust tests to work with the blocking Read and Write functions.

Remove BufReader, since now its users can use BytesPipe directly.

This version of the commit hardcodes a 1 MB threshold for blocking on writes. I'm hoping that we can do some testing around this and determine if this number should be tweaked or made configurable.

cc @unclejack @vcaputo @jonathanperret

@LK4D4

LK4D4 commented Nov 11, 2015

Copy link
Copy Markdown
Contributor

Looks super-cool. I'll review tomorrow.

@LK4D4 LK4D4 added this to the 1.10 milestone Nov 11, 2015
Comment thread pkg/ioutils/bytespipe.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as I see, bp.closed and bp.closeErr always changed together, so maybe we can eliminate closed and use just closeErr != nil instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed.

@LK4D4

LK4D4 commented Nov 12, 2015

Copy link
Copy Markdown
Contributor

LGTM

@unclejack

Copy link
Copy Markdown
Contributor

@aaronlehmann There seems to be some kind of problem. The benchmark doesn't seem to finish right now. BenchmarkBytesPipeRead-2 hangs forever for no apparent reason for me. This is something I can reproduce every single time.

Can you look into this, please?

update: I can confirm that the code is indeed deadlocking in the benchmarks:

*** Test killed with quit: ran too long (10m0s).                                          

@LK4D4

LK4D4 commented Nov 12, 2015

Copy link
Copy Markdown
Contributor

I wonder how that behchmark worked before :) It writes 500 1024 bytes chunk, but reading 1000 and it has check that n is 1024 each time.

Turn BytesPipe's Read and Write functions into blocking, goroutine-safe
functions. Add a CloseWithError function to propagate an error code to
the Read function.

Adjust tests to work with the blocking Read and Write functions.

Remove BufReader, since now its users can use BytesPipe directly.

Signed-off-by: Aaron Lehmann <[email protected]>
@aaronlehmann

Copy link
Copy Markdown
Author

Sorry about that - I missed the benchmarks.

As @LK4D4 mentioned, the benchmark is trying to read more data than is written. With the new blocking behavior, this causes a deadlock (not that it would have worked before, either).

I've fixed this by making the benchmark read one thousand 512 byte chunks instead.

@aaronlehmann

Copy link
Copy Markdown
Author

BTW, here is the change in benchmark results:

before:

BenchmarkBytesPipeWrite    10000        172635 ns/op
BenchmarkBytesPipeRead     30000         40503 ns/op

after:

BenchmarkBytesPipeWrite    10000        197825 ns/op
BenchmarkBytesPipeRead     10000        127205 ns/op

I think the worse nubers in the "read" benchmark reflect the locking that was added. However, this doesn't reflect a real-world slowdown, because formerly BytesPipe had to be used through BufReader, which implemented locking on top of it. I suspect performance of the new BytesPipe implementation at least matches the old BytesPipe + BufReader. It might even be significantly better, since it avoids an unnecessary goroutine running io.Copy.

@unclejack

Copy link
Copy Markdown
Contributor

The fact that it's slightly slower doesn't even matter. The whole point is to avoid the memory issues and have good performance while doing that.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants