Skip to content

Conversation

@j-baker
Copy link

@j-baker j-baker commented Jul 5, 2017

What changes were proposed in this pull request?

UnsafeExternalRowSorter.RowComparator contains references to the objects
backing the last arrays sorted. This causes memory leaks, since those
objects become deallocated but are still live.

We make sure to unset them.

How was this patch tested?

Not sure how to explicitly test this. Would appreciate guidance on what kind of testing might be necessary here. Evidence for the bug is provided in the JIRA ticket here.

UnsafeExternalRowSorter.RowComparator contains references to the objects
backing the last arrays sorted. This causes memory leaks, since those
objects become deallocated but are still live.

We make sure to unset them.
@srowen
Copy link
Member

srowen commented Jul 5, 2017

Seems reasonable, though I don't know this code well. There is a similar pattern in KVComparator. I wonder if it's even more efficient to have a clear() method to reset state of an UnsafeRow?

@j-baker j-baker force-pushed the jbaker/fix_memory_leak branch from cdf869a to 5e7a935 Compare July 5, 2017 15:28
@hvanhovell
Copy link
Contributor

ok to test

@SparkQA
Copy link

SparkQA commented Jul 6, 2017

Test build #79247 has finished for PR 18543 at commit 5e7a935.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@j-baker
Copy link
Author

j-baker commented Jul 6, 2017

Can confirm that my job which was exhibiting OOMs no longer OOMs after this change.

@srowen
Copy link
Member

srowen commented Jul 10, 2017

@davies or @rxin might be worth a look. The only possible downside here is the extra cycles to clear the reference, so I raise it in case you think we need to find a more optimized way to deal with this. But the change itself looks like a straightforward fix other than that.

return ordering.compare(row1, row2);
int comparison = ordering.compare(row1, row2);
row1.pointTo(null, 0L, -1);
row2.pointTo(null, 0L, -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

can we avoid to do it per comparison? is there any places we can do a cleanup at the end?

Copy link
Member

Choose a reason for hiding this comment

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

Good idea @cloud-fan . Looks like RowComparator and KVComparator could be cleaned up in UnsafeExternalSorter.cleanupResources and UnsafeInMemorySorter.free. @davies I know this is from a long while back, but does that make sense? Seems like reasonable places to simply 'flush' the references, and won't hurt anything.

I am not 100% sure there's not another path to take care of, or if this frees the ref soon enough to avoid the problem. Thoughts @j-baker ?

Copy link
Member

@kiszk kiszk Jul 17, 2017

Choose a reason for hiding this comment

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

@cloud-fan @srowen It is good idea to do this cleanup only once at the end. Now, I am curious how to implement this cleanup.

While @srowen proposed to use nsafeExternalSorter.cleanupResources and UnsafeInMemorySorter.free that will be called when a task is finished, to do cleanup here does not seem to work in this case. This is because this issue occurs before completing a task since UnsafeExternalSorter instance is registered into the task taskContext at here. This cleanup approach will not be performed before an OOM occurs during execution of the task.

IIUC, the end of sort is here. This line calls this sort method. Either to do the cleanup at the first part or to do the cleanup after checking type of a given comparator at the second part could work.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

So I feel like if the thing ends up in memory, this is correct - but otherwise the comparator is used in the UnsafeSorterSpillMerger.

Since we're handing back an iterator, am I right in thinking that without some periodic cleanup task you always stand a risk from this kind of leak unless you clear after each comparison or have some kind of async cleanup task?

Copy link
Author

@j-baker j-baker Jul 19, 2017

Choose a reason for hiding this comment

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

I suppose that if you assume that you can only use these sorters once, then you can probably null out the reference to the comparator in the UnsafeExternalSorter once you've constructed the UnsafeSorterSpillMerger using it, and that would also solve the problem I've been seeing (the callback keeping a strong reference).

It'd still feel weird that you have a comparator that could potentially be responsible for the liveness of hundreds of megabytes of memory, though.

Copy link
Author

Choose a reason for hiding this comment

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

maybe it's worth having a clone method on the comparator and making sure we clone the comparator before passing it to anything?

Copy link
Author

Choose a reason for hiding this comment

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

...I'll update this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

The merger reads data from spill files lazily, so when the merging finishes, it's end of the task.

Copy link
Author

Choose a reason for hiding this comment

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

Then why would we be seeing the OOMs? If at the end of the task the taskcompletionlistener fires and is removed, then the whole comparator becomes unreachable and we have no problem here.

My job looks something like:

dataset.sortWithinPartitions().coalescePartitions() - would not we potentially finish doing some merging before reaching the end of the task?

Copy link
Contributor

@cloud-fan cloud-fan Jul 19, 2017

Choose a reason for hiding this comment

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

i see, some partition may finish merging but some do not, and the merger which is finished is not referred, but it changed the comparator and make it keep the input rows it compared last time.

@cloud-fan
Copy link
Contributor

Hi @j-baker , do you have any code snippet to reproduce this memory leak?

@kiszk
Copy link
Member

kiszk commented Jul 12, 2017

This program works well with this PR while it causes an infinite loop in org.apache.spark.memory.TaskMemoryManager.allocatePage without this PR due to OOM.

j-baker added 2 commits July 19, 2017 14:37
Instead of holding a single comparator, keep a factory around which
produces objects which may be more tightly scoped. No explicit free work
is provided - instead long lived object should not keep around a
comparator.
final MemoryConsumer consumer,
final TaskMemoryManager memoryManager,
final RecordComparator recordComparator,
final RecordComparator.Factory recordComparatorFactory,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to change UnsafeInMemorySorter. The TaskContext refers UnsafeExternalSorter, so we only need the comparator factory in UnsafeExternalSorter.

@cloud-fan
Copy link
Contributor

BTW, since the GC root is TaskContext, is it possible to update https://github.com/apache/spark/blob/master/core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java#L159-L161 to not let UnsafeExternalSorter being referred by TaskContext?

@SparkQA
Copy link

SparkQA commented Jul 19, 2017

Test build #79756 has finished for PR 18543 at commit 05fc618.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jul 19, 2017

Test build #79757 has finished for PR 18543 at commit 51ec9a0.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@cloud-fan
Copy link
Contributor

ping

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.

6 participants