Skip to content

Aggressively GC on high memory usage in worker - #1488

Merged
mrocklin merged 1 commit into
dask:masterfrom
ogrisel:aggressive-gc
Oct 23, 2017
Merged

mrocklin merged 1 commit into
dask:masterfrom
ogrisel:aggressive-gc

Conversation

@ogrisel

@ogrisel ogrisel commented Oct 20, 2017

Copy link
Copy Markdown
Contributor

This is a possible fix for some of the spurious worker restarts reported in the discussion of dask/dask#2470.

I ran the test script of dask/dask#2470 (comment) with additional logs before and after the call to gc.collect() and I confirm that many unnecessary restarts can be avoided this way when memory is tight.

@ogrisel

ogrisel commented Oct 20, 2017

Copy link
Copy Markdown
Contributor Author

I don't think there is an easy way to add a test for this.

Comment thread distributed/worker.py Outdated
if frac > min(self.memory_pause_fraction, self.memory_spill_fraction):
# Force a GC event so as to get more accurate memory usage
# measurements in case GC is lagging.
gc.collect()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How often is memory_monitor executed? gc.collect can become quite costly if there are many live objects.

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.

gc.collect is only executed when we are above the high memory fraction. This should not incur any significant performance impact when the memory usage is normal (lower than 70% of the limit by default I think).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But imagine a worker that's permanently above that fraction. Now you're running the GC over and again (even if it doesn't release any memory)...

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.

Here is the kind of log I got when running the linked snippet with the 1GB memory limit on the worker with additional logs before and after GC:

https://gist.github.com/ogrisel/6cb75c91d515601c4a659675506ea9ff

I had the code running for 10min or so. So it would not get triggered too often but saved a spurious restart.

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.

But imagine a worker that's permanently above that fraction. Now you're running the GC over and again (even if it doesn't release any memory)...

The default memory_monitor_interval is 200ms according to the default value of the kwarg. I could add a 5s cool-down period between two consecutive gc calls but that will make the code more complex / hackish.

@ogrisel ogrisel Oct 20, 2017 •

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.

I have tried a less aggressive / smarter strategy with extensive logs here: f3981a5 . It kind of works but I still get a random nanny OOM after a while so I am not fully satisfied.

@ogrisel ogrisel Oct 20, 2017 •

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.

At least that last option gets rid of the following misleading warning I get most often when running the original snippet on master: "Memory use is high but worker has no data to store to disk. Perhaps some other process leaking memory?"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nanny OOMs aren't entirely avoidable if your tasks can quickly generate a moderate fraction of your available memory. It will always be possible that they can conspire to generate enough data to send you over very quickly.

There are costs and benefits to both approaches.

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.

I will implement the ThrottledGC in this PR. But I am not sure if should better implement the strategy of this PR or the one in f3981a5.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have any thoughts on which strategy if better. If you think what you have in f3981a5 is nicer then go with that.

@ogrisel

ogrisel commented Oct 20, 2017 •

Copy link
Copy Markdown
Contributor Author

I have broken the tests by editing the other warning messages. I will push a fix.

@mrocklin

Copy link
Copy Markdown
Member

FYI I'm also tracking down some intermittent failures that have been popping up recently. I haven't seen these before though.

@mrocklin

Copy link
Copy Markdown
Member

See also #1255

cc @bluenote10

@ogrisel

ogrisel commented Oct 20, 2017

Copy link
Copy Markdown
Contributor Author

The failure on travis in test_pause_executor that fails to be paused is probably caused by my change that causes some sort of race condition in this test.

@mrocklin

Copy link
Copy Markdown
Member

I believe that you can safely ignore Python 2 nanny test failures: #1491

@mrocklin

Copy link
Copy Markdown
Member

By "nanny test failures" I mean any test in test_nanny.py or any test that starts with @gen_cluster(..., Worker=Nanny)

@ogrisel

ogrisel commented Oct 21, 2017 •

Copy link
Copy Markdown
Contributor Author

I have updated this PR to do less aggressive GC in the worker (only when it has a chance to be beneficial) while also implementing a throttling strategy based on the fraction of the time in GC over the time between 2 consecutive calls.

I ran a bunch of long-running tasks that process chunks with a low memory-limit and it seems to work as expected. The frequency of actual GC events depends on the value of the memory-limit but I no longer get the misleading "Perhaps some other process is leaking memory?" warning. I get the "Pausing" / "Resuming" warnings from time to time as expected. I have added debug logs to make it possible to use grep to check that the gc is actually called from time to time (e.g. every couple of seconds in my scripts, while each gc duration is on the order of 100ms).

I still get some spurious OOM events by the nanny process but those are much rarer than before on this use case.

@mrocklin mrocklin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Generally this looks good to me. I have a couple of questions about the parameters. I'm curious if this resolves the problems that @bluenote10 saw earlier.

Comment thread distributed/utils.py Outdated
throttled_gc.collect() does nothing when repeated calls are so costly and
so frequent that the thread would spend more than max_in_gc_frac doing GC.
"""
def __init__(self, max_in_gc_frac=0.2, warn_if_longer=10, logger=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you have any thoughts on the default value for max_in_gc_frac=0.2? Spending 20% of our time in garbage collection seems unpleasant.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The warn_if_longer= parameter could also use some mention in the docstring

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.

Spending 20% of our time in garbage collection seems unpleasant.

It's an upper-bound. In most situations memory-limit is high enough that GC will almost never be called at all.

But in cases where the worker is under heavy memory pressure, I find it better to spend that time in the GC than run the risk to be spuriously killed by nanny for no valid reason. Note that it's not a smoothed average, just based on the last 2 calls to collect.

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.

I will try to change the fraction to 5% and run some experiments.

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.

In the meantime, I have pushed an improvement to the docstring as requested. Let me know what you think.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's an upper-bound. In most situations memory-limit is high enough that GC will almost never be called at all.

OK, that makes sense to me.

In the meantime, I have pushed an improvement to the docstring as requested. Let me know what you think.

If we're spending 10s in gc then that is probably also something that we should know about. Thoughts on reducing this to 1s?

Comment thread distributed/worker.py
memory = proc.memory_info().rss
if total > need and memory > target:
# Issue a GC to ensure that the evicted data is actually
# freed from memory and taken into account by the monitor

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it expected for evicted data to have cyclic references?

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.

I don't think it's going to be the case for typical workloads but users are free to ship whatever data structures they like into delayed function calls. Why kind of problems do you anticipate?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I simply think it's bad hygiene to add gc.collect calls "just in case" :-)

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.

For the record, this is the kind of actual case that was observed by dask-distributed users:

dask/zict#19 (comment)

Comment thread distributed/utils.py
self.last_collect = collect_start
self.last_gc_duration = max(time() - collect_start, 1e-6)
if self.last_gc_duration > self.warn_if_longer:
self.logger.warning("gc.collect() took %0.3fs. This is usually"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, I'm curious why you think this is useful. Do you know a use case where that warning would have helped?

@ogrisel ogrisel Oct 21, 2017 •

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.

To warn datascientists that are inadvertently processing a list of 100 million small dicts at a time in each task instead of processing smaller lists of a couple of million each instead. For instance parsing large JSON files in large chunks instead of small chunks.

I assume that large GC pauses might make the worker non-responsive. But if you think this is useless we can get rid of this warning.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume that large GC pauses might make the worker non-responsive.

It will. But what if using many objects is deliberate and required by the workload? Do we want to warn against any possible programming mistake?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that users would appreciate getting a warning if GC is taking seconds. They have responded well to similar feedback that we have provided in the past (like long event loop pauses).

Comment thread distributed/utils.py Outdated
to control to log a warning level message whenever an actual call to
gc.collect() lasts too long.
"""
def __init__(self, max_in_gc_frac=0.2, warn_if_longer=10, logger=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpicking, but allowing 20% of the time inside the GC sounds like a lot.

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.

Would 5% be a reasonable default?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It certainly sounds better...

@ogrisel

ogrisel commented Oct 23, 2017

Copy link
Copy Markdown
Contributor Author

I have reworked this PR to address the comments. I now only trigger throttled GC when it's promising, that is after data eviction.

I decided to know longer call the throttled GC after a paused as it's better to call it after eviction, otherwise the throttling mechanism might prevent interesting collections.

@mrocklin

Copy link
Copy Markdown
Member

Merged.

At some point I might want to look at also calling self.gc.collect when we transition into a paused state. This requires some care to avoid the throttling mechanism. We might get around this by just calling the normal gc.collect() here, which might be ok given that this should occur infrequently.

@ogrisel
ogrisel deleted the aggressive-gc branch October 23, 2017 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants