Skip to content

Use ReadLine instead of ReadBytes to prevent daemon from OOM caused b… - #14702

Closed
chenchun wants to merge 1 commit into
moby:masterfrom
chenchun:fix_oom
Closed

chenchun wants to merge 1 commit into
moby:masterfrom
chenchun:fix_oom

Conversation

@chenchun

Copy link
Copy Markdown
Contributor

Signed-off-by: Chun Chen [email protected]

If a single line is huge, see #9139 (comment), it costs so much to buffer the whole line in the memory. We could use ReadLine instead of ReadBytes to fix such case. The default buf size is 4096 bytes in bufio which I think is enough for most cases. If you think it's necessary to add an option for users to config the size, I can update the patch.

@chenchun

Copy link
Copy Markdown
Contributor Author

Fixed failed tests.

@chenchun

Copy link
Copy Markdown
Contributor Author

@unclejack , can you take a look ?

@unclejack

Copy link
Copy Markdown
Contributor

@chenchun I'll have to test to confirm it helps.

Comment thread daemon/logger/copier.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.

@chenchun we might as well not do bufio.NewReader(src), and simply call src.Read(p) with a specified buffer size for p. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems we have to handle '\n' or '\r\n' and do things like cuting of the line in buffer p ourselves manually if using src.Read(p). What is your concerns of using bufio.NewReader(src)?

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.

fair enough :)

@chenchun
chenchun force-pushed the fix_oom branch 3 times, most recently from a147870 to 6610089 Compare July 30, 2015 03:08
@chenchun

Copy link
Copy Markdown
Contributor Author

Failed tests are not related.

Comment thread daemon/logger/copier.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.

@chenchun comment here is out of date, should be ReadLine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for missing that. Removed the comment here and || len(line) > 0 since "ReadLine either returns a non-nil line or it returns an error, never both."

@chenchun

Copy link
Copy Markdown
Contributor Author

@tiborvass , thanks for reviewing, rebased.

@tiborvass

Copy link
Copy Markdown
Contributor

@chenchun sorry again but I thought again about this PR and I'm not 100% sure this is the right solution. Let me explain.

The Message type has a field called Line, which assumes that it is each line of a log message. My feeling is that, that API should be updated to do: s/Line/Data/ or Buffer or Bytes or whatever.
If we don't do that, at least we should add a IsPrefix bool to Message that way we map the ReadLine API. Otherwise, clients won't be able to differentiate one long line from two lines.

Ping @LK4D4 @calavera

Comment thread daemon/logger/copier.go

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.

Should we not check isPrefix and do something about it? Silently corrupting the log message is probably not the right solution.

@stevvooe

Copy link
Copy Markdown
Contributor

I'm worried this will cause log messages to get silently corrupted when they are over 4096 bytes in size. This is problematic in certain cases where log messages actually have structure, which is becoming more and more common. If it helps, bufio.Scanner was designed to deal with line buffering more carefully. Albeit, it would hard error on an overflow. A hard error here would be much better than a silent one.

Is there any reason container logs aren't treated as an opaque stream or am I missing something?

@chenchun

Copy link
Copy Markdown
Contributor Author

@stevvooe, #13564 changed logger.Copier to use bufio.Reader instead of bufio.Scanner.

I think this needs to be fixed in case of some evil behavior which can crash down the daemon by printing logs without a \n.

I propose to increase the size from 4096 bytes to some big enough size like 1MB and add a option to docker daemon for users to config the size or add an isPrefix field in Message to indicate it's part of a long line.
WDYT?

@unclejack

Copy link
Copy Markdown
Contributor

I propose to increase the size from 4096 bytes to some big enough size like 1MB and add a option to docker daemon for users to config the size or add an isPrefix field in Message to indicate it's part of a long line.
WDYT?

This will make things extremely bad when it comes to memory, so I'm NOT OK with such a change.

@chenchun

Copy link
Copy Markdown
Contributor Author

This will make things extremely bad when it comes to memory, so I'm NOT OK with such a change.

But currently logger.Copier buffers the whole logs in the memory in enix's #9139 (comment) case.
If we add an option to docker daemon to config the max line size, we can leave it to users to control the memory overhead.

@unclejack

Copy link
Copy Markdown
Contributor

@chenchun
-1 on having a docker daemon config option. This is a slippery slope and it feels like we're sweeping the dirt under the rug, which isn't OK.

enix's issue in #9139 is a degenerate case. The problem has been solved for the general case and I really think we need something smarter than just giving up and allocating 1 MB of memory for this. What happens when I have 100 containers? Do I need n megabytes of RAM for this, where n is the number of containers?

Allocating chunks of 1 MB for this is unreasonable. What happens when someone says "I'm seeing this problem with 1.02 MB of data, increase the size of the chunk to 2 MB"? Do they have to keep bumping that config option to handle this? What happens when someone attempts to run containers which require less than 1 MB of RAM, but the daemon eats up 1 MB per container for this?

I've done some tests in the past to fix other issues in Docker which required buffers. I can tell you raising buffers to such huge sizes isn't a good solution.

@cpuguy83

Copy link
Copy Markdown
Member

Given the resistance to this particular fix for the issue, maybe we can work on some other ideas to resolve this issue?
Does it make sense to go ahead and close this one?

@unclejack

Copy link
Copy Markdown
Contributor

@cpuguy83 I really think it should be closed because fixing this issue is going to require a more complex solution which doesn't involve allocating a lot of memory and doesn't require daemon options.

@cpuguy83

Copy link
Copy Markdown
Member

@chenchun wdyt? Maybe we can discuss on IRC a good solution for this?

@stevvooe

Copy link
Copy Markdown
Contributor

@chenchun Looks like #13564 might not have been the correct fix. It probably should have just restarted on the copy on the "too long" error, rather than allocating all of memory. When it comes right down to it, this data should not be line buffered. Even if it has to be, there should be a way to partition it across log records.

While running out of memory is not ideal, silently corrupting logs and configurable line buffering sizes are not ideal solutions, if even pathological (buffer creep, per @unclejack). For example, if I pipe /dev/urandom to my terminal, while saving all history, does my terminal corrupt the output? No, it tries to allocate what the application asked of it and crashes if it can't do it. The crash protects our data.

The real issue here is isolation. Can we find a way to catch the out of memory error and recover? Perhaps, a recover for the out of memory panic would be sufficient. On recovery, we could kill the ill-behaved container, saving the daemon.

@cpuguy83 Thoughts?

@chenchun

Copy link
Copy Markdown
Contributor Author

Thanks for the patient and detailed review. closed the PR.

@chenchun chenchun closed this Aug 18, 2015
@chenchun
chenchun deleted the fix_oom branch August 25, 2015 13:16
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.

7 participants