You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "cloud-fan (via GitHub)" <gi...@apache.org> on 2025/09/01 16:43:49 UTC
[PR] [SPARK-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
cloud-fan opened a new pull request, #52190:
URL: https://github.com/apache/spark/pull/52190
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
This is a followup of https://github.com/apache/spark/pull/47856 . It makes the memory tracking more accurate in several places:
1. In `ShuffleExternalSorter`/`UnsafeExternalSorter`, the memory is used by both the sorter itself, and its underlying in-memort sorter (for sorting shuffle partition ids). We need to add them up to calcuate the current memory usage.
2. In `ExternalAppendOnlyUnsafeRowArray`, the records are inserted to an in-memory buffer first. If the buffer gets too large (currently based on num records), we switch to `UnsafeExternalSorter`. The in-memory buffer also needs a memory based threshold
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
3. If you fix a bug, you can clarify why it is a bug.
-->
More accurate memory tracking results to better spill decisions
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No, the feature is not released yet.
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
existing tests
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
no
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2317718414
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExternalAppendOnlyUnsafeRowArray.scala:
##########
@@ -44,20 +44,23 @@ import org.apache.spark.util.collection.unsafe.sort.{UnsafeExternalSorter, Unsaf
* excessive disk writes. This may lead to a performance regression compared to the normal case
* of using an [[ArrayBuffer]] or [[Array]].
*/
-private[sql] class ExternalAppendOnlyUnsafeRowArray(
+class ExternalAppendOnlyUnsafeRowArray(
Review Comment:
I've updated the classdoc
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 closed pull request #52190: [SPARK-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold
URL: https://github.com/apache/spark/pull/52190
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2315148519
##########
core/src/main/java/org/apache/spark/shuffle/sort/ShuffleExternalSorter.java:
##########
@@ -90,9 +90,9 @@ final class ShuffleExternalSorter extends MemoryConsumer implements ShuffleCheck
private final int numElementsForSpillThreshold;
/**
- * Force this sorter to spill when the size in memory is beyond this threshold.
+ * Force this sorter to spill when the in memory size in bytes is beyond this threshold.
*/
- private final long recordsSizeForSpillThreshold;
+ private final long sizeInBytesForSpillThreshold;
Review Comment:
```suggestion
private final long sizeInBytesForSpillThreshold ;
```
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2315149038
##########
core/src/main/java/org/apache/spark/shuffle/sort/ShuffleExternalSorter.java:
##########
@@ -90,9 +90,9 @@ final class ShuffleExternalSorter extends MemoryConsumer implements ShuffleCheck
private final int numElementsForSpillThreshold;
/**
- * Force this sorter to spill when the size in memory is beyond this threshold.
+ * Force this sorter to spill when the in memory size in bytes is beyond this threshold.
*/
- private final long recordsSizeForSpillThreshold;
+ private final long sizeInBytesForSpillThreshold ;
Review Comment:
```suggestion
private final long sizeInBytesForSpillThreshold;
```
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2317713170
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExternalAppendOnlyUnsafeRowArray.scala:
##########
@@ -44,20 +44,23 @@ import org.apache.spark.util.collection.unsafe.sort.{UnsafeExternalSorter, Unsaf
* excessive disk writes. This may lead to a performance regression compared to the normal case
* of using an [[ArrayBuffer]] or [[Array]].
*/
-private[sql] class ExternalAppendOnlyUnsafeRowArray(
+class ExternalAppendOnlyUnsafeRowArray(
Review Comment:
other data structures (`ShuffleExternalSorter`, `UnsafeExternalSorter`, etc.) are all public classes under a private package. I just make them consistent here, not a big deal and I'm fine to revert 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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #52190:
URL: https://github.com/apache/spark/pull/52190#issuecomment-3242906867
cc @cxzl25 @attilapiros @mridulm
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2314344281
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/ArrowWindowPythonEvaluatorFactory.scala:
##########
@@ -149,7 +149,7 @@ class ArrowWindowPythonEvaluatorFactory(
private val inMemoryThreshold = conf.windowExecBufferInMemoryThreshold
private val spillThreshold = conf.windowExecBufferSpillThreshold
- private val spillSizeThreshold = conf.windowExecBufferSpillSizeThreshold
+ private val sizeInBytesSpillThreshold = conf.windowExecBufferSpillSizeThreshold
Review Comment:
For now I reuse the spill threshold config for the `ExternalAppendOnlyUnsafeRowArray` in-memory buffer threshold as well. We can add new configs if we want (but it will be a lot of configs).
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2317713815
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExternalAppendOnlyUnsafeRowArray.scala:
##########
@@ -116,19 +121,24 @@ private[sql] class ExternalAppendOnlyUnsafeRowArray(
spillableArray = null
} else if (inMemoryBuffer != null) {
inMemoryBuffer.clear()
+ inMemoryBufferSizeInBytes = 0;
}
numFieldsPerRow = 0
numRows = 0
modificationsCount += 1
}
def add(unsafeRow: UnsafeRow): Unit = {
- if (numRows < numRowsInMemoryBufferThreshold) {
+ // Once spills, we will switch to UnsafeExternalSorter permanently.
+ if (spillableArray == null && numRows < numRowsInMemoryBufferThreshold &&
+ inMemoryBufferSizeInBytes < sizeInBytesSpillThreshold) {
Review Comment:
```suggestion
inMemoryBufferSizeInBytes < sizeInBytesInMemoryBufferThreshold) {
```
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on PR #52190:
URL: https://github.com/apache/spark/pull/52190#issuecomment-3251941953
Merged to master, 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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2317713619
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExternalAppendOnlyUnsafeRowArray.scala:
##########
@@ -116,19 +121,24 @@ private[sql] class ExternalAppendOnlyUnsafeRowArray(
spillableArray = null
} else if (inMemoryBuffer != null) {
inMemoryBuffer.clear()
+ inMemoryBufferSizeInBytes = 0;
}
numFieldsPerRow = 0
numRows = 0
modificationsCount += 1
}
def add(unsafeRow: UnsafeRow): Unit = {
- if (numRows < numRowsInMemoryBufferThreshold) {
+ // Once spills, we will switch to UnsafeExternalSorter permanently.
+ if (spillableArray == null && numRows < numRowsInMemoryBufferThreshold &&
+ inMemoryBufferSizeInBytes < sizeInBytesSpillThreshold) {
Review Comment:
oh I made a typo here...
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cxzl25 (via GitHub)" <gi...@apache.org>.
cxzl25 commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2317701187
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExternalAppendOnlyUnsafeRowArray.scala:
##########
@@ -116,19 +121,24 @@ private[sql] class ExternalAppendOnlyUnsafeRowArray(
spillableArray = null
} else if (inMemoryBuffer != null) {
inMemoryBuffer.clear()
+ inMemoryBufferSizeInBytes = 0;
}
numFieldsPerRow = 0
numRows = 0
modificationsCount += 1
}
def add(unsafeRow: UnsafeRow): Unit = {
- if (numRows < numRowsInMemoryBufferThreshold) {
+ // Once spills, we will switch to UnsafeExternalSorter permanently.
+ if (spillableArray == null && numRows < numRowsInMemoryBufferThreshold &&
+ inMemoryBufferSizeInBytes < sizeInBytesSpillThreshold) {
Review Comment:
Should `sizeInBytesInMemoryBufferThreshold` be used ?
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExternalAppendOnlyUnsafeRowArray.scala:
##########
@@ -44,20 +44,23 @@ import org.apache.spark.util.collection.unsafe.sort.{UnsafeExternalSorter, Unsaf
* excessive disk writes. This may lead to a performance regression compared to the normal case
* of using an [[ArrayBuffer]] or [[Array]].
*/
-private[sql] class ExternalAppendOnlyUnsafeRowArray(
+class ExternalAppendOnlyUnsafeRowArray(
Review Comment:
Does `private[sql]` need to be removed?
The previous PR added parameters in `ExternalAppendOnlyUnsafeRowArray`, but the documentation for `ExternalAppendOnlyUnsafeRowArray` was not updated.
--
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-49386][CORE][SQL][FOLLOWUP] More accurate memory tracking for memory based spill threshold [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52190:
URL: https://github.com/apache/spark/pull/52190#discussion_r2318591245
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExternalAppendOnlyUnsafeRowArray.scala:
##########
@@ -34,30 +34,34 @@ import org.apache.spark.util.collection.unsafe.sort.{UnsafeExternalSorter, Unsaf
/**
* An append-only array for [[UnsafeRow]]s that strictly keeps content in an in-memory array
- * until [[numRowsInMemoryBufferThreshold]] is reached post which it will switch to a mode which
- * would flush to disk after [[numRowsSpillThreshold]] is met (or before if there is
- * excessive memory consumption). Setting these threshold involves following trade-offs:
+ * until [[numRowsInMemoryBufferThreshold]] or [[sizeInBytesInMemoryBufferThreshold]] is reached
+ * post which it will switch to a mode (backed by [[UnsafeExternalSorter]]) which would flush to
+ * disk after [[numRowsSpillThreshold]] or [[sizeInBytesSpillThreshold]] is met (or before if there
+ * is excessive memory consumption). Setting these threshold involves following trade-offs:
*
- * - If [[numRowsInMemoryBufferThreshold]] is too high, the in-memory array may occupy more memory
- * than is available, resulting in OOM.
- * - If [[numRowsSpillThreshold]] is too low, data will be spilled frequently and lead to
- * excessive disk writes. This may lead to a performance regression compared to the normal case
- * of using an [[ArrayBuffer]] or [[Array]].
+ * - If [[numRowsInMemoryBufferThreshold]] and [[sizeInBytesInMemoryBufferThreshold]] are too high,
+ * the in-memory array may occupy more memory than is available, resulting in OOM.
+ * - If [[numRowsSpillThreshold]] or [[sizeInBytesSpillThreshold]] is too low, data will be spilled
+ * frequently and lead to excessive disk writes. This may lead to a performance regression compared
+ * to the normal case of using an [[ArrayBuffer]] or [[Array]].
Review Comment:
```suggestion
* frequently and lead to excessive disk writes. This may lead to a performance regression
* compared to the normal case of using an [[ArrayBuffer]] or [[Array]].
```
--
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