You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "jwang0306 (via GitHub)" <gi...@apache.org> on 2024/03/01 02:07:51 UTC

[PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

jwang0306 opened a new pull request, #45348:
URL: https://github.com/apache/spark/pull/45348

   <!--
   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
        'common/utils/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.
   -->
   
   This PR proposes an improvement to reduce executor memory usage by broadcasting the generated code during whole stage codegen.
   
   In certain internal workloads, we have observed instances where the generated code can be up to hundreds of MBs, serving as the last straw leading to executor OOM. To improve stability and handle such pathological cases, we make the `cleanedSource` (the generated code) a broadcast variable. Theoretically, this change would reduce memory usage per executor from (`cleanedSource` * num tasks) to (`cleanedSource` * 1), with the trade-off of network latency.
   
   The feature is gated by a Spark conf `spark.sql.codegen.broadcastCleanedSourceThreshold`. This config sets a threshold to determine if we should use a broadcast variable for the generated code during WSCG. If it is set to a value less than 0, the feature is disabled, which is the default.
   
   ### 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.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   Stability improvement.
   
   ### 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.
   -->
   
   #### Unit Test
   Added a new unit test in `WholeStageCodegenSuite` for three cases:
   1. threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
   2. threshold is a larger number, shouldn't broadcast, not yet exceeded.
   3. threshold is 0, should broadcast since it's always smaller than generated code size.
   
   ```bash
   ~/spark$ build/sbt -java-home /usr/lib/jvm/java-17-openjdk-amd64 "sql/testOnly *WholeStageCodegenSuite -- -z SPARK-47238"
   ```
   
   #### Manual Test
   For the ease of demonstration, we created a query that generates a lot of "comments" which leads to large code size, and additionally configure the cluster with the following:
   - set `spark.sql.codegen.comments=true` to enable verbose comments.
   - set `spark.sql.adaptive.enabled=false` to ensure whole stage codegen.
   - set `spark.driver.memory=10G` to give driver enough memory.
   - set a larger core numbers (16) and a smaller overall memory (1G) for executor.
   
   ##### Build the code
   ```bash
   ~/spark$ build/sbt -mem 10000 -java-home /usr/lib/jvm/java-17-openjdk-amd64 package
   ```
   
   ##### Without Broadcast
   1. create a local cluster, with the configs mentioned above.
   ```bash
   ~/spark$ ./bin/spark-shell --master local-cluster[2,16,1024] --conf spark.sql.codegen.comments=true --conf spark.sql.adaptive.enabled=false --conf spark.driver.memory=10G
   ```
   2. run the artificial workload, executor OOM-ed.
   ```bash
   scala> spark.sql(s"select ${(1 to 99).map(i => s"id as ${"name" * 300000}$i").mkString(", ")} from range(100)").collect()
   ```
   
   ##### With Broadcast
   1. create a local cluster, with the configs mentioned above, and further set `spark.sql.codegen.broadcastCleanedSourceThreshold=0` to ensure it's broadcasting
   ```bash
   ~/spark$ ./bin/spark-shell --master local-cluster[2,16,1024] --conf spark.sql.codegen.comments=true --conf spark.sql.adaptive.enabled=false --conf spark.driver.memory=10G --conf spark.sql.codegen.broadcastCleanedSourceThreshold=0
   ```
   2. run the same artificial workload, the query finished successfully.
   ```bash
   scala> spark.sql(s"select ${(1 to 99).map(i => s"id as ${"name" * 300000}$i").mkString(", ")} from range(100)").collect()
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   
   No.


-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on PR #45348:
URL: https://github.com/apache/spark/pull/45348#issuecomment-1972332318

   cc. @JoshRosen @cloud-fan @rednaxelafx @hvanhovell, please take a look.


-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514559175


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -899,4 +900,28 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
       }
     }
   }
+
+  test("SPARK-47238: Test broadcast threshold for generated code") {
+    // case 1: threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
+    // case 2: threshold is a larger number, shouldn't broadcast since not yet exceeded.
+    // case 3: threshold is 0, should broadcast since it's always smaller than generated code size.
+    Seq((-1, false), (1000000000, false), (0, true)).foreach { case (threshold, shouldBroadcast) =>
+      withSQLConf(SQLConf.WHOLESTAGE_BROADCAST_CLEANED_SOURCE_THRESHOLD.key -> threshold.toString) {
+        val df = Seq(0, 1, 2).toDF().groupBy("value").sum()
+        // Invoke tryBroadcastCleanedSource and make sure it returns the desired variables.

Review Comment:
   how about we enable `conf.usePartitionEvaluator` and call `WholeStageCodegenExec.execute`, and cast the RDD to `MapPartitionsWithEvaluatorRDD`, and get and cast the `evaluatorFactory` to `WholeStageCodegenEvaluatorFactory`. Then we can check if it really uses broadcast. Otherwise the test is not very convincing as checking `tryBroadcastCleanedSource` does not prove we use broadcast at the end.



-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514792504


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -899,4 +900,28 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
       }
     }
   }
+
+  test("SPARK-47238: Test broadcast threshold for generated code") {
+    // case 1: threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
+    // case 2: threshold is a larger number, shouldn't broadcast since not yet exceeded.
+    // case 3: threshold is 0, should broadcast since it's always smaller than generated code size.
+    Seq((-1, false), (1000000000, false), (0, true)).foreach { case (threshold, shouldBroadcast) =>
+      withSQLConf(SQLConf.WHOLESTAGE_BROADCAST_CLEANED_SOURCE_THRESHOLD.key -> threshold.toString) {
+        val df = Seq(0, 1, 2).toDF().groupBy("value").sum()
+        // Invoke tryBroadcastCleanedSource and make sure it returns the desired variables.

Review Comment:
   Good idea, otherwise I was even thinking of logging something to validate the broadcast. However, due to the way `evaluatorFactory` and `cleanedSourceOpt` are declared, they are not directly accessible, and thus we would need to retrieve them using reflection (I prefer not to directly modify the visibility of the constructor fields).



-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #45348:
URL: https://github.com/apache/spark/pull/45348#issuecomment-1982346069

   thanks, merging to master!


-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on PR #45348:
URL: https://github.com/apache/spark/pull/45348#issuecomment-1981289286

   @cloud-fan thanks for the review - I have updated the PR as suggested, please take another look, thanks!


-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514777808


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/WholeStageCodegenExec.scala:
##########
@@ -830,6 +831,24 @@ case class WholeStageCodegenExec(child: SparkPlan)(val codegenStageId: Int)
 
   override protected def withNewChildInternal(newChild: SparkPlan): WholeStageCodegenExec =
     copy(child = newChild)(codegenStageId)
+
+  // Use broadcast if the conf is enabled and the code + comment size exceeds the threshold,
+  // otherwise, fallback to use cleanedSource directly. The method returns either a
+  // broadcast variable or the original form of cleanedSource.
+  private[spark] def tryBroadcastCleanedSource(code: CodeAndComment):
+      Either[broadcast.Broadcast[CodeAndComment], CodeAndComment] = {
+    def exceedThreshold(): Boolean = {
+      code.body.length + code.comment.iterator.map(
+        e => e._1.length + e._2.length).sum > conf.broadcastCleanedSourceThreshold
+    }
+    val enabled = conf.broadcastCleanedSourceThreshold >= 0
+    if (enabled && exceedThreshold()) {
+      SparkContext.getActive.map(sc => scala.util.Left(sc.broadcast(code)))

Review Comment:
   Ah didn't notice that, thanks for the input. I'll update 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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514792504


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -899,4 +900,28 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
       }
     }
   }
+
+  test("SPARK-47238: Test broadcast threshold for generated code") {
+    // case 1: threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
+    // case 2: threshold is a larger number, shouldn't broadcast since not yet exceeded.
+    // case 3: threshold is 0, should broadcast since it's always smaller than generated code size.
+    Seq((-1, false), (1000000000, false), (0, true)).foreach { case (threshold, shouldBroadcast) =>
+      withSQLConf(SQLConf.WHOLESTAGE_BROADCAST_CLEANED_SOURCE_THRESHOLD.key -> threshold.toString) {
+        val df = Seq(0, 1, 2).toDF().groupBy("value").sum()
+        // Invoke tryBroadcastCleanedSource and make sure it returns the desired variables.

Review Comment:
   Good idea, otherwise I was even thinking of logging something to validate the broadcast. However, due to the way `evaluatorFactory` and `cleanedSourceOpt` are declared, they are not directly accessible, and thus we would need to retrieve them using reflection (I prefer not to modify the visibility of the constructor fields).



-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on PR #45348:
URL: https://github.com/apache/spark/pull/45348#issuecomment-1983243153

   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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514792504


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -899,4 +900,28 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
       }
     }
   }
+
+  test("SPARK-47238: Test broadcast threshold for generated code") {
+    // case 1: threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
+    // case 2: threshold is a larger number, shouldn't broadcast since not yet exceeded.
+    // case 3: threshold is 0, should broadcast since it's always smaller than generated code size.
+    Seq((-1, false), (1000000000, false), (0, true)).foreach { case (threshold, shouldBroadcast) =>
+      withSQLConf(SQLConf.WHOLESTAGE_BROADCAST_CLEANED_SOURCE_THRESHOLD.key -> threshold.toString) {
+        val df = Seq(0, 1, 2).toDF().groupBy("value").sum()
+        // Invoke tryBroadcastCleanedSource and make sure it returns the desired variables.

Review Comment:
   Good idea, otherwise I was even thinking of logging something to validate the broadcast. However, due to the way `evaluatorFactory` and `cleanedSourceOpt` are declared, they are not directly accessible, and thus we would need to retrieve them using reflection (unless we'd rather directly modify the visibility of the constructor fields).



-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514792504


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -899,4 +900,28 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
       }
     }
   }
+
+  test("SPARK-47238: Test broadcast threshold for generated code") {
+    // case 1: threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
+    // case 2: threshold is a larger number, shouldn't broadcast since not yet exceeded.
+    // case 3: threshold is 0, should broadcast since it's always smaller than generated code size.
+    Seq((-1, false), (1000000000, false), (0, true)).foreach { case (threshold, shouldBroadcast) =>
+      withSQLConf(SQLConf.WHOLESTAGE_BROADCAST_CLEANED_SOURCE_THRESHOLD.key -> threshold.toString) {
+        val df = Seq(0, 1, 2).toDF().groupBy("value").sum()
+        // Invoke tryBroadcastCleanedSource and make sure it returns the desired variables.

Review Comment:
   Good idea, otherwise I was even thinking of logging something to validate the broadcast. However, due to the way `evaluatorFactory` and `cleanedSourceOpt` are declared, they are not directly accessible, and thus we would need to retrieve them using reflection (unless we'd rather directly modify the visibility of the constructor definition).



-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514792504


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -899,4 +900,28 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
       }
     }
   }
+
+  test("SPARK-47238: Test broadcast threshold for generated code") {
+    // case 1: threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
+    // case 2: threshold is a larger number, shouldn't broadcast since not yet exceeded.
+    // case 3: threshold is 0, should broadcast since it's always smaller than generated code size.
+    Seq((-1, false), (1000000000, false), (0, true)).foreach { case (threshold, shouldBroadcast) =>
+      withSQLConf(SQLConf.WHOLESTAGE_BROADCAST_CLEANED_SOURCE_THRESHOLD.key -> threshold.toString) {
+        val df = Seq(0, 1, 2).toDF().groupBy("value").sum()
+        // Invoke tryBroadcastCleanedSource and make sure it returns the desired variables.

Review Comment:
   Good idea, otherwise I was even thinking of logging something to validate the broadcast. However, due to the way `evaluatorFactory` and `cleanedSourceOpt` are declared, they are not directly accessible, and thus we would need to retrieve them using reflection (I prefer not to modify the visibility).



-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "jwang0306 (via GitHub)" <gi...@apache.org>.
jwang0306 commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514792504


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -899,4 +900,28 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
       }
     }
   }
+
+  test("SPARK-47238: Test broadcast threshold for generated code") {
+    // case 1: threshold is -1, shouldn't broadcast since smaller than 0 means disabled.
+    // case 2: threshold is a larger number, shouldn't broadcast since not yet exceeded.
+    // case 3: threshold is 0, should broadcast since it's always smaller than generated code size.
+    Seq((-1, false), (1000000000, false), (0, true)).foreach { case (threshold, shouldBroadcast) =>
+      withSQLConf(SQLConf.WHOLESTAGE_BROADCAST_CLEANED_SOURCE_THRESHOLD.key -> threshold.toString) {
+        val df = Seq(0, 1, 2).toDF().groupBy("value").sum()
+        // Invoke tryBroadcastCleanedSource and make sure it returns the desired variables.

Review Comment:
   Good idea, otherwise I was even thinking of logging something to validate the broadcast. However, due to the way `evaluatorFactory` and `cleanedSourceOpt` are declared, they are not directly accessible, and thus we would need to retrieve them using reflection (unless we'd rather directly modify the visibility of the constructor fields).



-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #45348: [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable
URL: https://github.com/apache/spark/pull/45348


-- 
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


Re: [PR] [SPARK-47238][SQL] Reduce executor memory usage by making generated code in WSCG a broadcast variable [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45348:
URL: https://github.com/apache/spark/pull/45348#discussion_r1514551447


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/WholeStageCodegenExec.scala:
##########
@@ -830,6 +831,24 @@ case class WholeStageCodegenExec(child: SparkPlan)(val codegenStageId: Int)
 
   override protected def withNewChildInternal(newChild: SparkPlan): WholeStageCodegenExec =
     copy(child = newChild)(codegenStageId)
+
+  // Use broadcast if the conf is enabled and the code + comment size exceeds the threshold,
+  // otherwise, fallback to use cleanedSource directly. The method returns either a
+  // broadcast variable or the original form of cleanedSource.
+  private[spark] def tryBroadcastCleanedSource(code: CodeAndComment):
+      Either[broadcast.Broadcast[CodeAndComment], CodeAndComment] = {
+    def exceedThreshold(): Boolean = {
+      code.body.length + code.comment.iterator.map(
+        e => e._1.length + e._2.length).sum > conf.broadcastCleanedSourceThreshold
+    }
+    val enabled = conf.broadcastCleanedSourceThreshold >= 0
+    if (enabled && exceedThreshold()) {
+      SparkContext.getActive.map(sc => scala.util.Left(sc.broadcast(code)))

Review Comment:
   within `SparkPlan`, we can directly call the method `sparkContext`.



-- 
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