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

[BUGFIX] Fix resource leak when shuffle read #174

Merged
merged 2 commits into from Aug 22, 2022

Conversation

izchen
Copy link
Contributor

@izchen izchen commented Aug 22, 2022

What changes were proposed in this pull request?

Use org.apache.spark.TaskContext#addTaskCompletionListener to clean up resources used by RssShuffleDataIterator. This avoids possible resource leaks.

Why are the changes needed?

Before this PR, RssShuffleDataIterator would only clean up used resources after all records read.

When the Spark Task fails or cancels, or runs some special logic such as LocalLimit, the resource will not be cleaned up. This creates potential resource leaks.

Does this PR introduce any user-facing change?

No

How was this patch tested?

Added a UT case

@@ -201,13 +202,21 @@ public Configuration getHadoopConf() {
RssShuffleDataIterator iterator = new RssShuffleDataIterator<K, C>(
shuffleDependency.serializer(), shuffleReadClient,
readMetrics);
iterators.add(iterator);
CompletionIterator<Product2<K, C>, RssShuffleDataIterator<K, C>> completionIterator =
Copy link
Contributor

@jerqi jerqi Aug 22, 2022

Choose a reason for hiding this comment

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

If we use AQE, we could have many iterators, if we don't release the resource of them after we use them, we may occur OOM. I means that we use one iterator, and then release the iterator. We shouldn't release all the iterators at the end of task.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

org.apache.spark.util.CompletionIterator will clean up as soon as its wrapped RssShuffleDataIterator.hasNext returns false :

// org.apache.spark.util.CompletionIterator
  def hasNext: Boolean = {
    val r = iter.hasNext  // iter => RssShuffleDataIterator
    if (!r && !completed) {
      completed = true
      // reassign to release resources of highly resource consuming iterators early
      iter = Iterator.empty.asInstanceOf[I]
      completion()
    }
    r
  }

  def completion(): Unit  // completion() => RssShuffleDataIterator.cleanup

After this PR, if there is no special case for the Spark Task, the timing of resource cleanup is still when the RssShuffleDataIterator ends, not when the Spark Task ends.

This is the same behavior as before the PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

org.apache.spark.util.CompletionIterator will clean up as soon as its wrapped RssShuffleDataIterator.hasNext returns false :

// org.apache.spark.util.CompletionIterator
  def hasNext: Boolean = {
    val r = iter.hasNext  // iter => RssShuffleDataIterator
    if (!r && !completed) {
      completed = true
      // reassign to release resources of highly resource consuming iterators early
      iter = Iterator.empty.asInstanceOf[I]
      completion()
    }
    r
  }

  def completion(): Unit  // completion() => RssShuffleDataIterator.cleanup

After this PR, if there is no special case for the Spark Task, the timing of resource cleanup is still when the RssShuffleDataIterator ends, not when the Spark Task ends.

This is the same behavior as before the PR.

OK, I got it. Good catch.

@codecov-commenter
Copy link

Codecov Report

Merging #174 (d492aea) into master (74cd40b) will decrease coverage by 1.37%.
The diff coverage is n/a.

@@             Coverage Diff              @@
##             master     #174      +/-   ##
============================================
- Coverage     58.29%   56.92%   -1.38%     
+ Complexity     1262     1183      -79     
============================================
  Files           158      149       -9     
  Lines          8397     7902     -495     
  Branches        779      749      -30     
============================================
- Hits           4895     4498     -397     
+ Misses         3251     3162      -89     
+ Partials        251      242       -9     
Impacted Files Coverage Δ
...storage/handler/impl/DataSkippableReadHandler.java 81.25% <0.00%> (-3.13%) ⬇️
...org/apache/uniffle/server/ShuffleFlushManager.java 77.65% <0.00%> (-1.68%) ⬇️
...e/spark/shuffle/reader/RssShuffleDataIterator.java
...ava/org/apache/spark/shuffle/RssShuffleHandle.java
...org/apache/spark/shuffle/RssSparkShuffleUtils.java
...che/spark/shuffle/writer/BufferManagerOptions.java
.../java/org/apache/spark/shuffle/RssSparkConfig.java
...org/apache/spark/shuffle/writer/AddBlockEvent.java
...k/shuffle/writer/WrappedByteArrayOutputStream.java
...pache/spark/shuffle/writer/WriteBufferManager.java
... and 1 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@jerqi jerqi left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @izchen

@jerqi jerqi merged commit df4163f into apache:master Aug 22, 2022
@izchen
Copy link
Contributor Author

izchen commented Aug 22, 2022

Thanks for your review, @jerqi

@izchen izchen deleted the fix_read_resource_leak branch August 22, 2022 09:10
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.

None yet

3 participants