You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/10/08 08:28:48 UTC

[GitHub] [spark] ulysses-you opened a new pull request, #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

ulysses-you opened a new pull request, #38159:
URL: https://github.com/apache/spark/pull/38159

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   ShuffledHashJoin releases the built hashed relation at the end of task using taskCompletionListener. It is not always good enough for complex sql query.
   
   If a smj or window on the top of the shj without a shuffle, then the hashed relation in shj would be leak. All rows have been consumed in sort before smj or window then the buffer can not allocate the memory which is hold by hashed relation. Then it causes unnecessary spill.
   
   It is a common case in multi-join, since AQE supports convert smj to shj at runtime.
   
   This pr cleanup resource for two cases:
   1. after building hashed relation, cleanup build plan
   2. after inserting rows in sort, cleanup child
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     3. If you fix a bug, you can clarify why it is a bug.
   -->
   Eagerly release hashed relation to avoid unnecessary spill in other memory consumers.
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   no
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   A local example to see the difference of spill size:
   
   ```scala
   ./bin/spark-shell --driver-memory 512M --conf spark.sql.adaptive.enabled=false --conf spark.sql.shuffle.partitions=10
   
   val fake = 1.to(50).mkString("")
   val df1 = spark.range(500).selectExpr("id % 17 as c1")
   val df2 = spark.range(100000).selectExpr("id % 17 as c2", s"'$fake' as c3")
   val df = df1.join(df2.hint("shuffle_hash"), $"c1" === $"c2").selectExpr("rank() over (partition by c1 order by c2) as r1", "c3")
   df.write.format("noop").mode("append").save
   ```
   
   before:
   <img width="1703" alt="image" src="https://user-images.githubusercontent.com/12025282/194694712-e0ae5aec-1ca8-4e3a-aafb-b4a497c4e4b1.png">
   
   after:
   <img width="1696" alt="image" src="https://user-images.githubusercontent.com/12025282/194694685-74cc3290-3b00-44c3-adf0-a647079221ec.png">
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] github-actions[bot] closed pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin
URL: https://github.com/apache/spark/pull/38159


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] dongjoon-hyun commented on a diff in pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
dongjoon-hyun commented on code in PR #38159:
URL: https://github.com/apache/spark/pull/38159#discussion_r990825455


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkPlan.scala:
##########
@@ -542,6 +542,8 @@ abstract class SparkPlan extends QueryPlan[SparkPlan] with Logging with Serializ
    * Cleans up the resources used by the physical operator (if any). In general, all the resources
    * should be cleaned up when the task finishes but operators like SortMergeJoinExec and LimitExec
    * may want eager cleanup to free up tight resources (e.g., memory).
+   *
+   * Note that, this method is called at executor side.

Review Comment:
   If you don't mind, could you revert this change to keep this PR simpler?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] github-actions[bot] commented on pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #38159:
URL: https://github.com/apache/spark/pull/38159#issuecomment-1396273075

   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] ulysses-you commented on pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on PR #38159:
URL: https://github.com/apache/spark/pull/38159#issuecomment-1272491644

   cc @cloud-fan @JoshRosen @c21  thank you


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] dongjoon-hyun commented on pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
dongjoon-hyun commented on PR #38159:
URL: https://github.com/apache/spark/pull/38159#issuecomment-1272607336

   BTW, this is an improvement, `leak` sounds like a bug. Did you want to say `spilled` instead of `leak`?
   > then the hashed relation in shj would be leak.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] ulysses-you commented on pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on PR #38159:
URL: https://github.com/apache/spark/pull/38159#issuecomment-1272733430

   @dongjoon-hyun ah, I see your concern. I'm not saying it is the definition but is the result. If you think the word `leak` is overkill, I'm fine to revise it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] ulysses-you commented on pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on PR #38159:
URL: https://github.com/apache/spark/pull/38159#issuecomment-1272706229

   @dongjoon-hyun I do not say it is a bug because in general it would not cause task failure just unnecessary spill. It acutally causes memory leak before the task completion. At that time, other memory consumer can not get the memory which they could get.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #38159:
URL: https://github.com/apache/spark/pull/38159#discussion_r990897121


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledHashJoinExec.scala:
##########
@@ -88,6 +90,9 @@ case class ShuffledHashJoinExec(
       ignoresDuplicatedKey = ignoreDuplicatedKey)
     buildTime += NANOSECONDS.toMillis(System.nanoTime() - start)
     buildDataSize += relation.estimatedSize
+    hashRelation = relation
+    // cleanup build plan resource eagerly since all rows have been consumed

Review Comment:
   is it possible to release the memory by hash join itself instead of asking several downstream operators to do so? e.g. release the hash relation if the stream side iterator has been consumed up. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] dongjoon-hyun commented on pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
dongjoon-hyun commented on PR #38159:
URL: https://github.com/apache/spark/pull/38159#issuecomment-1272720796

   @ulysses-you . Are you sure that this is a definition of memory leak?
   > It acutally causes memory leak before the task completion. At that time, other memory consumer can not get the memory which they could get.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] ulysses-you commented on a diff in pull request #38159: [SPARK-40594][SQL] Eagerly release hashed relation in ShuffledHashJoin

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #38159:
URL: https://github.com/apache/spark/pull/38159#discussion_r990982714


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledHashJoinExec.scala:
##########
@@ -88,6 +90,9 @@ case class ShuffledHashJoinExec(
       ignoresDuplicatedKey = ignoreDuplicatedKey)
     buildTime += NANOSECONDS.toMillis(System.nanoTime() - start)
     buildDataSize += relation.estimatedSize
+    hashRelation = relation
+    // cleanup build plan resource eagerly since all rows have been consumed

Review Comment:
   I thought about it. It needs more code change if we want to do this in shj itself.
   
   shj does not have a unified scanner compared with smj, so if we want to release hash relation at the end of output itrator, we need to wrap a new Iterator for shj. Then change all join types with codegen non-codegen code path to use the new Iterator. It will be a code refactor.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org