You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "erenavsarogullari (via GitHub)" <gi...@apache.org> on 2024/02/23 18:26:06 UTC
[PR] [SPARK-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
erenavsarogullari opened a new pull request, #45234:
URL: https://github.com/apache/spark/pull/45234
<!--
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?
AQE can materialize `ShuffleQueryStage` on the cancellation. This causes unnecessary stage materialization by submitting Shuffle Job. Under normal circumstances, if the stage is already non-materialized (a.k.a `ShuffleQueryStage.shuffleFuture` is not initialized yet), it should just be skipped without materializing it.
Please find sample use-case:
1- Stage Materialization Steps:
When stage materialization is failed:
```
1.1- ShuffleQueryStage1 - is materialized successfully,
1.2- ShuffleQueryStage2 - materialization is failed,
1.3- ShuffleQueryStage3 - Not materialized yet so ShuffleQueryStage3.shuffleFuture is not initialized yet
```
2- Stage Cancellation Steps:
```
2.1- ShuffleQueryStage1 - is canceled due to already materialized,
2.2- ShuffleQueryStage2 - is earlyFailedStage so currently, it is skipped as default because it could not be materialized,
2.3- ShuffleQueryStage3 - Problem is here: This stage is not materialized yet but currently, it is also tried to cancel and this stage requires to be materialized first.
```
### Why are the changes needed?
Current logic introduces unnecessary Shuffle Job to be able to cancel ShuffleQueryStage.
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Added new Unit Tests
### Was this patch authored or co-authored using generative AI tooling?
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506968570
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
`BroadcastExchangeExec.relationFuture` returns Java Future but ShuffleExchangeExec.mapOutputStatisticsFuture returns Scala Future. However, i reproduced today as `BroadcastQueryStageExec` can also have same problem although it has this check (`!broadcast.relationFuture.isDone`) because this check can be called before `broadcast.relationFuture` is not initialized yet. In terms of these, i fixed `BroadcastQueryStage` case as well so please see my second commit. Thanks.
**Reproduce Steps:**
We can also have following case:
**Before fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture needs to be initialized because it is not materialized yet,
BroadcastQueryStage2.relationFuture cancellation is finished with COMPLETED status,
Same for other BroadcastQueryStages which are not materialized yet.
```
**After fix (my second commit):**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture cancellation is skipped with SKIPPED status because it is not materialized yet,
Same for other BroadcastQueryStages which are not materialized yet.
```
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1684094611
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/ShuffleExchangeExec.scala:
##########
@@ -47,6 +47,15 @@ import org.apache.spark.util.random.XORShiftRandom
*/
trait ShuffleExchangeLike extends Exchange {
+ /**
+ * The asynchronous job that materializes the shuffle. It also does the preparations work,
+ * such as waiting for the subqueries.
+ */
+ @transient private lazy val shuffleFuture: Future[MapOutputStatistics] = executeQuery {
+ materializationStarted.set(true)
Review Comment:
Further more, I don't think this idea works. Let's say when we want to cancel a query stage and the `materializationStarted` flag is false, we decide to skip the cancellation but maybe the next second the `materializationStarted` becomes true and we miss to cancel the shuffle job.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1525339983
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
We need to have stage failure to trigger the cancellation and used the [existing test](https://github.com/apache/spark/pull/45234/files#diff-f89f2fe78b324c6bc7190bef84220181f3616efc156ea99b3f15d375a22d7f88R878) to simulate this case as well.
I think we can have single test case checking both new properties: `QueryStageExec.materializationStarted` and `name`.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #45234:
URL: https://github.com/apache/spark/pull/45234#issuecomment-2082863674
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1544936688
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
I have updated the unit tests by simulating `ExchangeQueryStageExec` materialization failure and triggering the stage cancellation.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1515240053
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
This depends on the query complexity (`numOfExchangeQueryStage`) and which stage' s materialization is failed such as first stage or last stage. For example, if the query introduces multiple `ShuffleQueryStages` and if the first stage' s materialization is failed, currently, cancellation is being skipped for all of the remaining stages by new logic due to their `materializationStarted = false` such as:
Let there are 3 `ShuffleQueryStages`:
```
- ShuffleQueryStage-0' s materialization is failed (and its materializationStarted = true),
- ShuffleQueryStage-1' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false,
- ShuffleQueryStage-2' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false,
...
```
I can also reproduce same behaviour for more complex queries.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1512850096
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -148,6 +148,18 @@ abstract class QueryStageExec extends LeafExecNode {
*/
abstract class ExchangeQueryStageExec extends QueryStageExec {
+ /**
+ * AQE can trigger stage cancellation before doMaterialize function is called. This causes
Review Comment:
is it possible to avoid this? The AQE framework should know if a stage has been submitted or not.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on PR #45234:
URL: https://github.com/apache/spark/pull/45234#issuecomment-2070062199
Build is green now so PR is ready to be merged. Thanks in advance.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1525339983
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
We need to have stage failure to trigger the cancellation and used the [existing test](https://github.com/apache/spark/pull/45234/files#diff-f89f2fe78b324c6bc7190bef84220181f3616efc156ea99b3f15d375a22d7f88R878) to simulate this case as well.
I think we can have single test case checking both new properties: `QueryStageExec.materializationStarted` and `name` like last commit: https://github.com/apache/spark/pull/45234/commits/867134b581f496669f41aeb7c3b4695d95c5c1db.
--
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-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1505215438
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
shall we do something like shuffle query stage ?
--
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-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506968570
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
`BroadcastExchangeExec.relationFuture` returns Java Future but ShuffleExchangeExec.mapOutputStatisticsFuture returns Scala Future. However, i reproduced today as `BroadcastQueryStageExec` can also have same problem although it has this check(`!broadcast.relationFuture.isDone`). For example, we can also have following case:
**Before fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture needs to be initialized because it is not materialized yet,
BroadcastQueryStage2.relationFuture cancellation is finished with COMPLETED status,
Same for other BroadcastQueryStages which are not materialized yet.
```
**After fix (my second commit):**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture cancellation is skipped with SKIPPED status because it is not materialized yet,
Same for other BroadcastQueryStages which are not materialized yet.
```
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1517390394
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
Do we still need this change if all `cancel` implementations already check this flag?
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1514404676
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -148,6 +148,17 @@ abstract class QueryStageExec extends LeafExecNode {
*/
abstract class ExchangeQueryStageExec extends QueryStageExec {
+ /**
+ * This flag aims to detect if the stage materialization is started. This helps
+ * to avoid unnecessary stage materialization when the stage is canceled.
+ */
+ @transient protected val materializationStarted = new AtomicBoolean()
Review Comment:
can we set this flag in `def materialize()`?
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1575677485
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -51,13 +51,30 @@ abstract class QueryStageExec extends LeafExecNode {
*/
val plan: SparkPlan
+ /**
+ * Name of this query stage which is unique in the entire query plan.
+ */
+ val name: String = s"${this.getClass.getSimpleName}-$id"
+
+ /**
+ * This flag aims to detect if the stage materialization is started. This helps
+ * to avoid unnecessary stage materialization when the stage is canceled.
+ */
+ private val materializationStarted = new AtomicBoolean()
Review Comment:
sorry for the last-minute proposal, but I'm wondering if it's more efficient to push this cancelation optimization into shuffle and broadcast nodes.
It looks a bit fragile to operate on the `shuffleFuture` directly in `ShuffleQueryStageExec.cancel`. I think we should let `ShuffleExchangeLike` provide clear APIs to do it. Today it provides `submitShuffleJob`, and it should also provide `cancelShuffleJob`.
Within `ShuffleExchangeLike`, we can do more optimizations. e.g. even if we cancel the shuffle stage after the shuffle stage is submitted, we can still avoid submitting the shuffle job, as the shuffle node might be doing other preparation work: generating the shuffle dependency, waiting for subqueries to finish, etc. It's more efficient to check the isCanceled flag at the last minute, right before submitting the shuffle job.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1544936688
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
I have updated the unit tests by simulating `ExchangeQueryStageExec` materialization failure and triggering the stage cancellation. I think this way is more cost-effective than previous iteration using bucketing and parquet writes.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1544936688
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
I have updated the unit tests by simulating `ExchangeQueryStageExec` materialization failure and triggering the stage cancellation. This way is more cost-effective than previous iteration using bucketing and parquet writes.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506968570
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
`BroadcastExchangeExec.relationFuture` returns Java Future but ShuffleExchangeExec.mapOutputStatisticsFuture returns Scala Future. However, i reproduced today as `BroadcastQueryStageExec` can also have same problem although it has this check(`!broadcast.relationFuture.isDone`) because this check can be called before `broadcast.relationFuture` is not initialized yet. In terms of these, i fixed `BroadcastQueryStage` case as well so please see my second commit. Thanks.
**Reproduce Steps:**
We can also have following case:
**Before fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture needs to be initialized because it is not materialized yet,
BroadcastQueryStage2.relationFuture cancellation is finished with COMPLETED status,
Same for other BroadcastQueryStages which are not materialized yet.
```
**After fix (my second commit):**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture cancellation is skipped with SKIPPED status because it is not materialized yet,
Same for other BroadcastQueryStages which are not materialized yet.
```
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on PR #45234:
URL: https://github.com/apache/spark/pull/45234#issuecomment-1971998442
Should we also backport this patch to `v3.4.x` and `v3.5.x`?
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1582463921
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -51,13 +51,30 @@ abstract class QueryStageExec extends LeafExecNode {
*/
val plan: SparkPlan
+ /**
+ * Name of this query stage which is unique in the entire query plan.
+ */
+ val name: String = s"${this.getClass.getSimpleName}-$id"
+
+ /**
+ * This flag aims to detect if the stage materialization is started. This helps
+ * to avoid unnecessary stage materialization when the stage is canceled.
+ */
+ private val materializationStarted = new AtomicBoolean()
Review Comment:
I agree that we need clean API definition so please find new refactoring on this direction by last commit.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1515240053
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
This depends on the query complexity and which stage' s materialization is failed such as first stage or last stage. For example, if the query introduces multiple `ShuffleQueryStages` and if the first stage' s materialization is failed, currently, cancellation is being skipped for all of the remaining stages by new logic due to their `materializationStarted = false` such as:
Let there are 3 `ShuffleQueryStages`:
```
- ShuffleQueryStage-0' s materialization is failed,
- ShuffleQueryStage-1' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false,
- ShuffleQueryStage-2' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false.
```
I can also reproduce same behaviour for more complex queries.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1582461066
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -198,18 +215,23 @@ case class ShuffleQueryStageExec(
reuse
}
- override def cancel(): Unit = shuffleFuture match {
- case action: FutureAction[MapOutputStatistics] if !action.isCompleted =>
- action.cancel()
- case _ =>
+ override def cancel(): Unit = {
Review Comment:
Added `doCancel()` to `ExchangeQueryStageExec` and both QueryStageExecs implement it. Please find last commit.
--
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-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506968570
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
`BroadcastExchangeExec.relationFuture` returns Java Future but ShuffleExchangeExec.mapOutputStatisticsFuture returns Scala Future. However, i reproduced today as `BroadcastQueryStageExec` can also have same problem although it has this check(`!broadcast.relationFuture.isDone`). For example, we can also have following case:
**Before fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture needs to be initialized because it is not materialized yet,
BroadcastQueryStage2.relationFuture cancellation is finished with COMPLETED status,
Same for other BroadcastQueryStages which are not materialized yet.
```
**After fix (my second commit):**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture cancellation is skipped with SKIPPED status because it is not materialized yet,
Same for other BroadcastQueryStages which are not materialized yet.
```
Also, i fixed BroadcastQueryStage case so please see my second commit. 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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1514403933
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
just to double check, this is for a rare case, right? When a query stage fails, the other query stages should have been submitted already.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1515240053
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
This depends on the query complexity (`numOfExchangeQueryStage`) and which stage' s materialization is failed such as first stage or last stage. For example, if the query introduces multiple `ShuffleQueryStages` and if the first stage' s materialization is failed, currently, cancellation is being skipped for all of the remaining stages by new logic due to their `materializationStarted = false` such as:
Let there are 3 `ShuffleQueryStages`:
```
- ShuffleQueryStage-0' s materialization is failed (and its materializationStarted = true),
- ShuffleQueryStage-1' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false,
- ShuffleQueryStage-2' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false.
```
I can also reproduce same behaviour for more complex queries.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1551407392
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case sqse: ShuffleQueryStageExec => sqse
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"Physical Plan should include " +
+ s"3 ShuffleQueryStages. Physical Plan: $adaptivePlan")
+ shuffleQueryStageExecs.foreach(sqse => assert(sqse.name.contains("ShuffleQueryStageExec-")))
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ } finally {
+ spark.experimental.extraStrategies = Nil
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case bqse: BroadcastQueryStageExec => bqse
+ }
+ assert(broadcastQueryStageExecs.length == 2, adaptivePlan)
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.name.contains("BroadcastQueryStageExec-"))
+ // Both BroadcastQueryStages are materialized at the beginning.
Review Comment:
can we add hint to broadcast the join side that has repartition? Then the second broadcast won't be materialized.
--
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-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506964254
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -198,10 +221,18 @@ case class ShuffleQueryStageExec(
reuse
}
- override def cancel(): Unit = shuffleFuture match {
- case action: FutureAction[MapOutputStatistics] if !action.isCompleted =>
- action.cancel()
- case _ =>
+ override def cancel(): StageCancellationStatus = {
+ if (isShuffleFutureTriggered.get()) {
Review Comment:
@ulysses-you Firstly, thanks for quick review. This is not hard requirement but helps specially for `testing` and `logging` because stage cancelation can have different statuses. Otherwise, we miss cancellation status and can not verify at UT level.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1513862684
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -148,6 +148,18 @@ abstract class QueryStageExec extends LeafExecNode {
*/
abstract class ExchangeQueryStageExec extends QueryStageExec {
+ /**
+ * AQE can trigger stage cancellation before doMaterialize function is called. This causes
Review Comment:
Firstly, thanks for the review. Yes, that is possible. Current logic iterates over `currentPhysicalPlan` for `ExchangeQueryStageExec` and try to cancel stages without checking if the stage materialization is started or not. I have just added this check as well by the last commit. After `isMaterializationStarted()` check is added to `cleanUpAndThrowException()`, `isMaterializationStarted()` check becomes redundant on `ShuffleQueryStageExec.cancel()` and `BroadcastQueryStageExec.cancel()` but i think they can still be worth if there is direct call to stage cancellation without checking` isMaterializationStarted()` for the long-term. This aims additional protection on the cancelation API but it is also up-to-us.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1518243547
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
I have added this check in terms of https://github.com/apache/spark/pull/45234#discussion_r1512850096. However, `cancel()` functions already has this check so we can continue with that.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1519578742
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
Review Comment:
What does this do? Remove the data then write the same data again?
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1518243547
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
I have added this check in terms of https://github.com/apache/spark/pull/45234#discussion_r1512850096.
However, `cancel()` functions already has this check so we can continue with that.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1515240053
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
This depends on the query complexity / `numOfExchangeQueryStage` and which stage' s materialization is failed such as first stage or last stage. For example, if the query introduces multiple `ShuffleQueryStages` and if the first stage' s materialization is failed, currently, cancellation is being skipped for all of the remaining stages by new logic due to their `materializationStarted = false` such as:
Let there are 3 `ShuffleQueryStages`:
```
- ShuffleQueryStage-0' s materialization is failed,
- ShuffleQueryStage-1' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false,
- ShuffleQueryStage-2' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false.
```
I can also reproduce same behaviour for more complex queries.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1515240053
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
This depends on the query complexity/numOfExchangeQueryStage and which stage' s materialization is failed such as first stage or last stage. For example, if the query introduces multiple `ShuffleQueryStages` and if the first stage' s materialization is failed, currently, cancellation is being skipped for all of the remaining stages by new logic due to their `materializationStarted = false` such as:
Let there are 3 `ShuffleQueryStages`:
```
- ShuffleQueryStage-0' s materialization is failed,
- ShuffleQueryStage-1' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false,
- ShuffleQueryStage-2' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false.
```
I can also reproduce same behaviour for more complex queries.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1582463921
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -51,13 +51,30 @@ abstract class QueryStageExec extends LeafExecNode {
*/
val plan: SparkPlan
+ /**
+ * Name of this query stage which is unique in the entire query plan.
+ */
+ val name: String = s"${this.getClass.getSimpleName}-$id"
+
+ /**
+ * This flag aims to detect if the stage materialization is started. This helps
+ * to avoid unnecessary stage materialization when the stage is canceled.
+ */
+ private val materializationStarted = new AtomicBoolean()
Review Comment:
I agree on this and we need clean API definition so please find new refactoring on this direction by last commit.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1509646119
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -198,10 +221,18 @@ case class ShuffleQueryStageExec(
reuse
}
- override def cancel(): Unit = shuffleFuture match {
- case action: FutureAction[MapOutputStatistics] if !action.isCompleted =>
- action.cancel()
- case _ =>
+ override def cancel(): StageCancellationStatus = {
+ if (isShuffleFutureTriggered.get()) {
Review Comment:
This is also required for UT verification so i used `materializationStarted` state for UT verification and removed `StageCancellationStatus`.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1684101953
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/ShuffleExchangeExec.scala:
##########
@@ -47,6 +47,15 @@ import org.apache.spark.util.random.XORShiftRandom
*/
trait ShuffleExchangeLike extends Exchange {
+ /**
+ * The asynchronous job that materializes the shuffle. It also does the preparations work,
+ * such as waiting for the subqueries.
+ */
+ @transient private lazy val shuffleFuture: Future[MapOutputStatistics] = executeQuery {
+ materializationStarted.set(true)
Review Comment:
I think we need a bit of synchronization here. The shuffle node should have two fields: `isCancelled` flag and the shuffle job `Future`.
- When we cancel a shuffle, we lock on the shuffle node, and set `isCancelled` flag to true. Then if the shuffle job `Future` is present, we cancel it.
- When we are going to submit a shuffle, we lock on the shuffle node. Then: if the `isCancelled` flag is true, fail immediately, otherwise, submit the shuffle job and set the `Future` field.
--
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-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1505215060
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -198,10 +221,18 @@ case class ShuffleQueryStageExec(
reuse
}
- override def cancel(): Unit = shuffleFuture match {
- case action: FutureAction[MapOutputStatistics] if !action.isCompleted =>
- action.cancel()
- case _ =>
+ override def cancel(): StageCancellationStatus = {
+ if (isShuffleFutureTriggered.get()) {
Review Comment:
This change looks reasonable, but do we really need `StageCancellationStatus` ?
--
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-47148][SQL] Avoid to materialize AQE QueryStages on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506968570
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
`BroadcastExchangeExec.relationFuture` returns Java Future but ShuffleExchangeExec.mapOutputStatisticsFuture returns Scala Future. However, i reproduced today as `BroadcastQueryStageExec` can also have same problem although it has this check(`!broadcast.relationFuture.isDone`). In terms of these, i fixed `BroadcastQueryStage` case as well so please see my second commit. Thanks.
**Reproduce Steps:**
We can also have following case:
**Before fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture needs to be initialized because it is not materialized yet,
BroadcastQueryStage2.relationFuture cancellation is finished with COMPLETED status,
Same for other BroadcastQueryStages which are not materialized yet.
```
**After fix (my second commit):**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture cancellation is skipped with SKIPPED status because it is not materialized yet,
Same for other BroadcastQueryStages which are not materialized yet.
```
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on PR #45234:
URL: https://github.com/apache/spark/pull/45234#issuecomment-1978266178
cc @cloud-fan @maryannxue as well
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1573095590
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
Review Comment:
Addressed
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case sqse: ShuffleQueryStageExec => sqse
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"Physical Plan should include " +
+ s"3 ShuffleQueryStages. Physical Plan: $adaptivePlan")
+ shuffleQueryStageExecs.foreach(sqse => assert(sqse.name.contains("ShuffleQueryStageExec-")))
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ } finally {
+ spark.experimental.extraStrategies = Nil
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
Review Comment:
Addressed
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1518243547
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
I have added this check in terms of https://github.com/apache/spark/pull/45234#discussion_r1512850096. However, both `cancel()` functions already has this check so we can continue with that.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #45234: [SPARK-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation
URL: https://github.com/apache/spark/pull/45234
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1551401862
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
Review Comment:
this seems unnecessary as the test does not cache any dataframe
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case sqse: ShuffleQueryStageExec => sqse
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"Physical Plan should include " +
+ s"3 ShuffleQueryStages. Physical Plan: $adaptivePlan")
+ shuffleQueryStageExecs.foreach(sqse => assert(sqse.name.contains("ShuffleQueryStageExec-")))
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ } finally {
+ spark.experimental.extraStrategies = Nil
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
Review Comment:
ditto
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1515243849
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -148,6 +148,17 @@ abstract class QueryStageExec extends LeafExecNode {
*/
abstract class ExchangeQueryStageExec extends QueryStageExec {
+ /**
+ * This flag aims to detect if the stage materialization is started. This helps
+ * to avoid unnecessary stage materialization when the stage is canceled.
+ */
+ @transient protected val materializationStarted = new AtomicBoolean()
Review Comment:
Yes, it is addressed.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1518243547
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
I have added this check in terms of https://github.com/apache/spark/pull/45234#discussion_r1512850096. However, both `cancel()` functions already has this check so we can continue with default ones.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1525339983
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
We need to have stage failure to trigger the cancellation and used the [existing test](https://github.com/apache/spark/pull/45234/files#diff-f89f2fe78b324c6bc7190bef84220181f3616efc156ea99b3f15d375a22d7f88R878) to simulate this case as well.
I think we can have single test case checking both new properties: `QueryStageExec.materializationStarted` and `name` like last commit.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1573096494
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case sqse: ShuffleQueryStageExec => sqse
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"Physical Plan should include " +
+ s"3 ShuffleQueryStages. Physical Plan: $adaptivePlan")
+ shuffleQueryStageExecs.foreach(sqse => assert(sqse.name.contains("ShuffleQueryStageExec-")))
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ } finally {
+ spark.experimental.extraStrategies = Nil
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case bqse: BroadcastQueryStageExec => bqse
+ }
+ assert(broadcastQueryStageExecs.length == 2, adaptivePlan)
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.name.contains("BroadcastQueryStageExec-"))
+ // Both BroadcastQueryStages are materialized at the beginning.
Review Comment:
I have added `BROADCAST` hint, however, second BroadcastQueryStage materialization still seems to be kicked off. Please see last commit.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case sqse: ShuffleQueryStageExec => sqse
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"Physical Plan should include " +
+ s"3 ShuffleQueryStages. Physical Plan: $adaptivePlan")
+ shuffleQueryStageExecs.foreach(sqse => assert(sqse.name.contains("ShuffleQueryStageExec-")))
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ } finally {
+ spark.experimental.extraStrategies = Nil
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case bqse: BroadcastQueryStageExec => bqse
+ }
+ assert(broadcastQueryStageExecs.length == 2, adaptivePlan)
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.name.contains("BroadcastQueryStageExec-"))
+ // Both BroadcastQueryStages are materialized at the beginning.
Review Comment:
I have added `BROADCAST` hint, however, second `BroadcastQueryStage` materialization still seems to be kicked off. Please see last commit.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1684091860
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/ShuffleExchangeExec.scala:
##########
@@ -47,6 +47,15 @@ import org.apache.spark.util.random.XORShiftRandom
*/
trait ShuffleExchangeLike extends Exchange {
+ /**
+ * The asynchronous job that materializes the shuffle. It also does the preparations work,
+ * such as waiting for the subqueries.
+ */
+ @transient private lazy val shuffleFuture: Future[MapOutputStatistics] = executeQuery {
+ materializationStarted.set(true)
Review Comment:
After a closer look, I don't think this change works as we expect. We set this `materializationStarted` flag before we return the `Future`, which means we are still on the AQE loop's main thread. That said, once we submit a query stage, its `materializationStarted` becomes true immediately and we can't really avoid the wasted query stage execution.
The test passed because `ShuffleExchangeExec` calls `child.execute()` before returning the `Future`. Then we exit the AQE loop without cancelling other stages.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1508373311
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -198,10 +221,18 @@ case class ShuffleQueryStageExec(
reuse
}
- override def cancel(): Unit = shuffleFuture match {
- case action: FutureAction[MapOutputStatistics] if !action.isCompleted =>
- action.cancel()
- case _ =>
+ override def cancel(): StageCancellationStatus = {
+ if (isShuffleFutureTriggered.get()) {
Review Comment:
I think it's a bit overkill if it is only used for debug. Can not we add `logDebug` inside cancel method ?
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1687324526
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/ShuffleExchangeExec.scala:
##########
@@ -47,6 +47,15 @@ import org.apache.spark.util.random.XORShiftRandom
*/
trait ShuffleExchangeLike extends Exchange {
+ /**
+ * The asynchronous job that materializes the shuffle. It also does the preparations work,
+ * such as waiting for the subqueries.
+ */
+ @transient private lazy val shuffleFuture: Future[MapOutputStatistics] = executeQuery {
+ materializationStarted.set(true)
Review Comment:
It seems not an acutally issue for now. AQE always do materilize stage and cancel stage at main thread. So if we decide to cancel stage then that means we will never do materilize stage again. We may need improve this code if we support do materilize concurrently in future.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1515240053
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -790,7 +790,8 @@ case class AdaptiveSparkPlanExec(
currentPhysicalPlan.foreach {
// earlyFailedStage is the stage which failed before calling doMaterialize,
// so we should avoid calling cancel on it to re-trigger the failure again.
- case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) =>
+ case s: ExchangeQueryStageExec if !earlyFailedStage.contains(s.id) &&
+ s.isMaterializationStarted() =>
Review Comment:
This depends on the query complexity (`numOfExchangeQueryStage`) and which stage' s materialization is failed such as first stage or last stage. For example, if the query introduces multiple `ShuffleQueryStages` and if the first stage' s materialization is failed, currently, cancellation is being skipped for all of the remaining stages by new logic due to their `materializationStarted = false` such as:
Let there are 3 `ShuffleQueryStages`:
```
- ShuffleQueryStage-0' s materialization is failed,
- ShuffleQueryStage-1' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false,
- ShuffleQueryStage-2' s materialization is not started yet so it is not tried to cancel due to materializationStarted = false.
```
I can also reproduce same behaviour for more complex queries.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1548387200
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
@cloud-fan Please let me know if there is any other comment. Otherwise, patch is ready to be merged. 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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1520503365
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
Review Comment:
Yes, it helps to break `QueryStage` materialization in order to trigger `QueryStage` cancellation so we can simulate the failure case. For this test case, execution steps are as follows:
```
ShuffleQueryStage-1 - is materialized successfully,
ShuffleQueryStage-2 - materialization is failed and this stage is excluded from cancellation by flagged as 'earlyFailedStage',
ShuffleQueryStage-1 - cancellation is kicked-off,
ShuffleQueryStage-3 - cancellation is kicked-off (although, it is not materialized).
```
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1519582719
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +897,138 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count().repartition(5)
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j").count().repartition(5)
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i").count().repartition(5)
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j")).join(aggDF3, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // There should not be BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"$adaptivePlan")
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2", "bucketed_table3") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table3")
+
+ val warehouseFilePath = new URI(spark.sessionState.conf.warehousePath).getPath
+ val tableDir = new File(warehouseFilePath, "bucketed_table2")
+ Utils.deleteRecursively(tableDir)
+ df.write.parquet(tableDir.getAbsolutePath)
+
+ val aggDF1 = spark.table("bucketed_table1").groupBy("i", "j", "k").count()
+ val aggDF2 = spark.table("bucketed_table2").groupBy("i", "j", "k").count()
+ val aggDF3 = spark.table("bucketed_table3").groupBy("i", "j").count()
+ val joinedDF = aggDF1.join(aggDF2, Seq("i", "j", "k")).join(aggDF3, Seq("i", "j"))
+ .join(aggDF1, Seq("i"))
+
+ val error = intercept[Exception] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "Invalid bucket file")
+ assert(error.getSuppressed.size === 0)
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ // There should not be ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case r: ShuffleQueryStageExec => r
+ }
+ assert(shuffleQueryStageExecs.isEmpty, s"$adaptivePlan")
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case r: BroadcastQueryStageExec => r
+ }
+ assert(broadcastQueryStageExecs.length == 3, s"$adaptivePlan")
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.isMaterializationStarted(),
+ s"${bqse.name}' s materialization should be started before " +
+ "BroadcastQueryStage-1' s materialization is failed.")
+ }
+ }
+ }
+ }
+
+ test("SPARK-47148: Check AQE QueryStages names") {
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ withTable("bucketed_table1", "bucketed_table2") {
+ val df = (0 until 50).map(i => (i % 5, i % 13, i.toString)).toDF("i", "j", "k")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table1")
+ df.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table2")
+
+ val df1 = spark.table("bucketed_table1").persist()
+ val df2 = spark.table("bucketed_table2").persist()
+ val joinedDF = df1.join(df2, Seq("i", "j", "k")).join(df1, Seq("i"))
+ .repartition(5).sort("i")
+ joinedDF.collect()
+
+ // Verify QueryStageExecs names
+ val adaptivePlanOfJoinedDF =
+ joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+ val queryStageExecs = collect(adaptivePlanOfJoinedDF) {
+ case qse: QueryStageExec => qse
+ }
+ assert(queryStageExecs.size == 7, s"$adaptivePlanOfJoinedDF")
+ assert(queryStageExecs.filter(_.name.contains("TableCacheQueryStageExec-")).size == 3)
+ assert(queryStageExecs.filter(_.name.contains("BroadcastQueryStageExec-")).size == 2)
+ assert(queryStageExecs.filter(_.name.contains("ShuffleQueryStageExec-")).size == 2)
+ }
+ }
+ }
+
Review Comment:
All the tests look like just checking if the `isMaterializationStarted` flag is correctly set. I think it's better to write unit tests that manually manipulate `AdaptiveSparkPlanExec` to only submit a few stages and then check the flag.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on PR #45234:
URL: https://github.com/apache/spark/pull/45234#issuecomment-2068474192
Thanks @cloud-fan and @ulysses-you for the reviews and approval.
I have just rebased to get green build.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1573096494
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/AdaptiveQueryExecSuite.scala:
##########
@@ -897,6 +900,85 @@ class AdaptiveQueryExecSuite
}
}
+ test("SPARK-47148: AQE should avoid to materialize ShuffleQueryStage on the cancellation") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on ShuffleQueryStageExec
+ val shuffleQueryStageExecs = collect(adaptivePlan) {
+ case sqse: ShuffleQueryStageExec => sqse
+ }
+ assert(shuffleQueryStageExecs.length == 3, s"Physical Plan should include " +
+ s"3 ShuffleQueryStages. Physical Plan: $adaptivePlan")
+ shuffleQueryStageExecs.foreach(sqse => assert(sqse.name.contains("ShuffleQueryStageExec-")))
+ // First ShuffleQueryStage is materialized so it needs to be canceled.
+ assert(shuffleQueryStageExecs(0).isMaterializationStarted(),
+ "Materialization should be started.")
+ // Second ShuffleQueryStage materialization is failed so
+ // it is excluded from the cancellation due to earlyFailedStage.
+ assert(shuffleQueryStageExecs(1).isMaterializationStarted(),
+ "Materialization should be started but it is failed.")
+ // Last ShuffleQueryStage is not materialized yet so it does not require
+ // to be canceled and it is just skipped from the cancellation.
+ assert(!shuffleQueryStageExecs(2).isMaterializationStarted(),
+ "Materialization should not be started.")
+ }
+ } finally {
+ spark.experimental.extraStrategies = Nil
+ }
+ }
+
+ test("SPARK-47148: Check if BroadcastQueryStage materialization is started") {
+ try {
+ spark.experimental.extraStrategies = TestProblematicCoalesceStrategy :: Nil
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.CAN_CHANGE_CACHED_PLAN_OUTPUT_PARTITIONING.key -> "true") {
+ val joinedDF = createJoinedDF()
+
+ val error = intercept[SparkException] {
+ joinedDF.collect()
+ }
+ assert(error.getMessage() contains "ProblematicCoalesce execution is failed")
+
+ val adaptivePlan = joinedDF.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec]
+
+ // All QueryStages should be based on BroadcastQueryStageExec
+ val broadcastQueryStageExecs = collect(adaptivePlan) {
+ case bqse: BroadcastQueryStageExec => bqse
+ }
+ assert(broadcastQueryStageExecs.length == 2, adaptivePlan)
+ broadcastQueryStageExecs.foreach { bqse =>
+ assert(bqse.name.contains("BroadcastQueryStageExec-"))
+ // Both BroadcastQueryStages are materialized at the beginning.
Review Comment:
I have added `BROADCAST` hint, however, both BroadcastQueryStages materializations still seem to be kicked off. Please see last commit.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1575603485
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -198,18 +215,23 @@ case class ShuffleQueryStageExec(
reuse
}
- override def cancel(): Unit = shuffleFuture match {
- case action: FutureAction[MapOutputStatistics] if !action.isCompleted =>
- action.cancel()
- case _ =>
+ override def cancel(): Unit = {
Review Comment:
nit: it seems better to follow `def materialize` and `def doMaterialize`, to have `def cancel` and `def doCancel`. We can put this materialization check in `def cancel`, and maybe more common logic related to cancelation in the future.
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1684103901
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/ShuffleExchangeExec.scala:
##########
@@ -47,6 +47,15 @@ import org.apache.spark.util.random.XORShiftRandom
*/
trait ShuffleExchangeLike extends Exchange {
+ /**
+ * The asynchronous job that materializes the shuffle. It also does the preparations work,
+ * such as waiting for the subqueries.
+ */
+ @transient private lazy val shuffleFuture: Future[MapOutputStatistics] = executeQuery {
+ materializationStarted.set(true)
Review Comment:
cc @erenavsarogullari @ulysses-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-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506968570
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
`BroadcastExchangeExec.relationFuture` returns Java Future but ShuffleExchangeExec.mapOutputStatisticsFuture returns Scala Future. However, i reproduced today as `BroadcastQueryStageExec` can also have same problem although it has this check(`!broadcast.relationFuture.isDone`). For example, we can also have following case:
**Before fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture needs to be initialized because it is not materialized yet,
BroadcastQueryStage2.relationFuture cancellation is finished with COMPLETED status,
Same for other BroadcastQueryStages which are not materialized yet.
```
**After fix (my second commit):**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture cancellation is skipped with SKIPPED status because it is not materialized yet,
Same for other BroadcastQueryStages which are not materialized yet.
```
Also, fixed BroadcastQueryStage case so please see my second commit. 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-47148][SQL] Avoid to materialize AQE ShuffleQueryStage on the cancellation [spark]
Posted by "erenavsarogullari (via GitHub)" <gi...@apache.org>.
erenavsarogullari commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1506968570
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/QueryStageExec.scala:
##########
@@ -250,11 +281,15 @@ case class BroadcastQueryStageExec(
reuse
}
- override def cancel(): Unit = {
+ override def cancel(): StageCancellationStatus = {
if (!broadcast.relationFuture.isDone) {
Review Comment:
`BroadcastExchangeExec.relationFuture` returns Java Future but ShuffleExchangeExec.mapOutputStatisticsFuture returns Scala Future. However, i reproduced today as `BroadcastQueryStageExec` can also have same problem although it has this check(`!broadcast.relationFuture.isDone`). For example, we can also have following case:
**Before fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture needs to be initialized because it is not materialized yet,
BroadcastQueryStage2.relationFuture cancellation is finished with COMPLETED status,
Same for other BroadcastQueryStages which are not materialized yet.
```
**After fix:**
```
BroadcastQueryStage1.doMaterialize is called and it is failed,
BroadcastQueryStage1 is excluded from cancellation due to earlyFailedStage,
BroadcastQueryStage2 is tried to cancel,
BroadcastQueryStage2.relationFuture cancellation is skipped with SKIPPED status because it is not materialized yet,
Same for other BroadcastQueryStages which are not materialized yet.
```
--
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-47148][SQL] Avoid to materialize AQE ExchangeQueryStageExec on the cancellation [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45234:
URL: https://github.com/apache/spark/pull/45234#discussion_r1684091860
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/ShuffleExchangeExec.scala:
##########
@@ -47,6 +47,15 @@ import org.apache.spark.util.random.XORShiftRandom
*/
trait ShuffleExchangeLike extends Exchange {
+ /**
+ * The asynchronous job that materializes the shuffle. It also does the preparations work,
+ * such as waiting for the subqueries.
+ */
+ @transient private lazy val shuffleFuture: Future[MapOutputStatistics] = executeQuery {
+ materializationStarted.set(true)
Review Comment:
After a closer look, I don't think this change works as we expect. We set this `materializationStarted` flag before we return the `Future`, which means we are still on the AQE loop's main thread. That said, once we submit a query stage, its `materializationStarted` becomes true immediately and we can't really avoid the wasted query stage execution when cancelling it.
The test passed because `ShuffleExchangeExec` calls `child.execute()` before returning the `Future`. Then we exit the AQE loop without cancelling other stages.
--
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