You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "Ngone51 (via GitHub)" <gi...@apache.org> on 2024/07/03 06:41:14 UTC
[PR] [SPARK-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Ngone51 opened a new pull request, #47197:
URL: https://github.com/apache/spark/pull/47197
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
This PR proposes to use the `ArrayBuffer` together with the read/write lock rather than `CopyOnWriteArrayList` for `TaskMetrics._externalAccums`.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
Fix the perf regression that caused by the accumulators registration overhead using `CopyOnWriteArrayList`.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
Manually tested.
### 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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #47197:
URL: https://github.com/apache/spark/pull/47197#issuecomment-2220414831
@Ngone51 please help to create 3.4/3.5 backport PRs.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670291970
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
Yes, `tm` is a local variable, and it is unlikely that there will be a reference to `tm` from outside before this function returns, so I also believe that there should be no thread safety issues 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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1665370326
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
Since it compiles, I think there is no caller of `externalAccums` trying to modify it. I think it's OK to eliminate the `toSeq`.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663609924
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums = withReadLock {
+ _externalAccums
+ }
Review Comment:
How do we handle such cases in general? Seems it's impossible to avoid the misuse completely from the caller.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663603187
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -340,7 +365,7 @@ private[spark] object TaskMetrics extends Logging {
externalAccums.add(tmpAcc)
}
}
- tm._externalAccums.addAll(externalAccums)
+ tm._externalAccums ++= externalAccums.asScala
Review Comment:
I'm wondering if we still need the local `externalAccums`. `tm._externalAccums` is `ArrayBuffer` now and the one-by-one insertion is no longer an issue.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1665413890
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
It might still be necessary to convert to the new `immutable.Seq`. The initial change of `_externalAccums` to use `CopyOnWriteArrayList` was to address the bug described in SPARK-39696, at which time the following test case was added:
https://github.com/apache/spark/blob/d5dc22356d23b9d15b4f7c195e60c3767ea205ad/core/src/test/scala/org/apache/spark/executor/ExecutorSuite.scala#L274-L293
IIRC, from a test a long time ago, converting `ArrayBuffer` to an `Array` first and then wrapping it as an `ArraySeq` showed better performance than using `toSeq` directly
```scala
import org.apache.spark.util.ArrayImplicits._
_externalAccums.toArray.toImmutableArraySeq
```
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663611672
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -340,7 +365,7 @@ private[spark] object TaskMetrics extends Logging {
externalAccums.add(tmpAcc)
}
}
- tm._externalAccums.addAll(externalAccums)
+ tm._externalAccums ++= externalAccums.asScala
Review Comment:
Make sense. Will remove it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1664124196
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
In Scala 2.13, `ArrayBuffer.toSeq` ultimately invokes `Nil.prependedAll`, and constructs a new `immutable.List` by iterating over the `ArrayBuffer`.
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
In Scala 2.13, `ArrayBuffer#toSeq` ultimately invokes `Nil.prependedAll`, and constructs a new `immutable.List` by iterating over the `ArrayBuffer`.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #47197:
URL: https://github.com/apache/spark/pull/47197#issuecomment-2219547588
@Ngone51 when did we introduce `CopyOnWriteArrayList`? We should backport this fix to that branch.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670374260
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
It has to do with some nits with Java memory model guarantees - the updates to the list need not be visible when queried from the read/write lock since it did not go through the same barrier when updates were made.
(I am reviewing only based on diff though (afk); so not sure if there are mitigating reasons why it might not apply)
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #47197: [SPARK-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList
URL: https://github.com/apache/spark/pull/47197
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663603110
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums = withReadLock {
+ _externalAccums
+ }
Review Comment:
By returning `_externalAccums`, we are allowing it to escape the read lock and get used directly in non-MT safe contexts (like `accumulators`).
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #47197:
URL: https://github.com/apache/spark/pull/47197#issuecomment-2220412301
3.3 is EOL, we need it in 3.4 and 3.5
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663648767
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums = withReadLock {
+ _externalAccums
+ }
Review Comment:
b) sounds good to me.
Just a bit concern for "not use externalAccums directly", does it mean for all the usages upon `accumulators()` should be written within `TaskMetrics` instead?
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1666017354
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
Agree, we should ensure this is thread safe.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663639641
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums = withReadLock {
+ _externalAccums
+ }
Review Comment:
Shall we simply change the return type of `def externalAccums` to be `Seq[AccumulatorV2[_, _]]`?
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on PR #47197:
URL: https://github.com/apache/spark/pull/47197#issuecomment-2219572494
> @Ngone51 when did we introduce `CopyOnWriteArrayList`? We should backport this fix to that branch.
from 3.3, but a manual pull request submission should be required for the fix, as branch-3.x supports two Scala versions, but Scala 2.12 does not have `immutable.ArraySeq`.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1666028339
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums = withReadLock {
+ _externalAccums
+ }
Review Comment:
Looking at this more, I assumed `accumulators` was used a bit more than that it currently is (which is the one place where I thought something like this would help: `private[spark] def accumulators(): Seq[AccumulatorV2[_, _]] = withReadLock { internalAccums ++ externalAccums }`) - looks like it is only in heartbeats - so not that common as well.
Given all other usages of `externalAccums` and `accumulators` is part of task end - conversion from `ArrayBuffer` to an immutable list should be fine.
So let us simply make it:
```
private[spark] def externalAccums = withReadLock {
_externalAccums.toList
}
```
Note, we have to explicitly create an (immutable) copy within the read lock (keeping [this comment](https://github.com/apache/spark/pull/47197#discussion_r1665413890) in mind as well)
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
+ }
+
+ private val (readLock, writeLock) = {
+ val lock = new ReentrantReadWriteLock()
+ (lock.readLock(), lock.writeLock())
+ }
Review Comment:
Move this definition above, along with the rest of the `val` definitions.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663730445
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
It's cheap. It essentially creates a linked list without clone items. The complexity is O(n).
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663626924
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums = withReadLock {
+ _externalAccums
+ }
Review Comment:
As an example, `ShuffleStatus.withReadLock` has one such formulation on how to do it in a thread safe manner.
Given `externalAccums` is `private[spark]`, should be possible to adapt something like that ... though this might be slightly cumbersome.
My preference would be to:
a) Replace existing high frequency uses of `externalAccums` within `TaskMetrics` by surrounding with `withReadLock` in `core` - and not use `externalAccums` directly (for ex `accumulators`, `Task.collectAccumulatorUpdates`).
b) For all external use (like from `sql`), return a `.toList` within the read lock:
```
private[spark] def externalAccums = withReadLock {
_externalAccums.toList
}
```
Thoughts ?
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663616953
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums = withReadLock {
+ _externalAccums
+ }
Review Comment:
Or maybe we should return an immutable copy of `_externalAccums`. This brings extra overhead though.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670486465
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
https://github.com/apache/spark/blob/43b67186554229c557c84b83146499e170dc960e/core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala#L1773-L1793
This is an interesting scenario. Referring to the code above, the process of `TaskMetrics.fromAccumulators(event.accumUpdates)` is invoked within a single thread. That is to say, the `taskMetrics` object is fully constructed within the current thread before it is published to other threads through `listenerBus`(another thread). Would this initialization process itself already serve as a happens-before barrier?
My understanding of this is quite rudimentary, so please correct me if I'm mistaken.
If my understanding above is incorrect, then indeed we should be using `withWriteLock` 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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670486465
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
https://github.com/apache/spark/blob/43b67186554229c557c84b83146499e170dc960e/core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala#L1773-L1793
This is an interesting scenario. Referring to the code above, the process of `TaskMetrics.fromAccumulators(event.accumUpdates)` is invoked within a single thread. That is to say, the `taskMetrics` object is fully constructed within the current thread before it is published to other threads through `listenerBus`(another thread). Would this initialization process itself already serve as a happens-before barrier? My understanding of this is quite rudimentary, so please correct me if I'm mistaken.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670374260
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
It has to do with some nits with Java memory model guarantees - the updates to the list need not be visible when queried from the read/write lock since it did not go through the same barrier when updates were made.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670291970
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
Agree with @Ngone51 `tm` is a local variable, and it is unlikely that there will be a reference to `tm` from outside before this function returns, so I also believe that there should be no thread safety issues 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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670374260
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
It has to do with some nits with Java memory model guarantees - the updates to the list need not be visible when queried from the read/write lock since it did not go through the same barrier when updates were made.
(I am reviewing only based on diff though (afk); so not sure if there are mitigating reasons why it might not apply)
See more here: https://docs.oracle.com/javase/specs/jls/se21/html/jls-17.html#jls-17.4.5
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #47197:
URL: https://github.com/apache/spark/pull/47197#issuecomment-2220412656
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670084988
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
Updated to `_externalAccums.toArray.toImmutableArraySeq`.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670666981
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
This local variable is well-scoped and I don't think it has multi-thread issues.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #47197:
URL: https://github.com/apache/spark/pull/47197#issuecomment-2223339275
Thank you for making this fix. Could you update the JIRA issue type from `Improvement` to `Bug` because this claims a regression, @Ngone51 ?

--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1663677608
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -264,12 +264,37 @@ class TaskMetrics private[spark] () extends Serializable {
/**
* External accumulators registered with this task.
*/
- @transient private[spark] lazy val _externalAccums = new CopyOnWriteArrayList[AccumulatorV2[_, _]]
+ @transient private[spark] lazy val _externalAccums = new ArrayBuffer[AccumulatorV2[_, _]]
- private[spark] def externalAccums = _externalAccums.asScala
+ private[spark] def externalAccums: Seq[AccumulatorV2[_, _]] = withReadLock {
+ _externalAccums.toSeq
Review Comment:
can we double check if `ArrayBuffer#toSeq` is cheap?
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670374260
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
It has to do with some nits with Java memory model guarantees - the updates to the list need not be visible when queried from the read/write lock since it did not go through the same barrier when updates were made.
(I am reviewing only based on diff though (afk); so not sure if there are mitigating reasons why it might not apply)
See more here: https://docs.oracle.com/javase/specs/jls/se21/html/jls-17.html#jls-17.4.5
If we believe that the updates to the list are guaranteed to satisfy the happens-before, wrt any other subsequent read/write (within the lock), then we don't need to make changes - my quick review indicated this was not guaranteed, but perhaps I missed something ?
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670486465
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
https://github.com/apache/spark/blob/43b67186554229c557c84b83146499e170dc960e/core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala#L1773-L1793
This is an interesting scenario. Referring to the code above, the process of `TaskMetrics.fromAccumulators(event.accumUpdates)` is invoked within a single thread. That is to say, the `taskMetrics` object is fully constructed within the current thread before it is published to other threads through `listenerBus`. Would this initialization process itself already serve as a happens-before barrier? My understanding of this is quite rudimentary, so please correct me if I'm mistaken.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1671012835
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
@LuciferYang thanks, the publish to `LinkedBlockingQueue` is what provides the necessary happens-before relationship and so ensures visibility !
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670065143
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
Discuss: This is technically an MT-safety change ... `_externalAccums` is getting updated outside of the rw-lock; and so potentially impacts visibility. If we are to be extremely defensive, I would make this update within the write lock.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670105206
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
I didn't use the write lock by design as `tm` is a local variable so theoretically there won't be thread-safe issue for the write operation on `tm. _externalAccums`. Using write lock to be defensive is also fine given it's fast since there is no lock competition with other threads.
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on code in PR #47197:
URL: https://github.com/apache/spark/pull/47197#discussion_r1670653152
##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,19 +354,16 @@ private[spark] object TaskMetrics extends Logging {
*/
def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
val tm = new TaskMetrics
- val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
for (acc <- accums) {
val name = acc.name
- val tmpAcc = acc.asInstanceOf[AccumulatorV2[Any, Any]]
if (name.isDefined && tm.nameToAccums.contains(name.get)) {
val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
tmAcc.metadata = acc.metadata
- tmAcc.merge(tmpAcc)
+ tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- externalAccums.add(tmpAcc)
+ tm._externalAccums += acc
Review Comment:
> If we believe that the updates to the list are guaranteed to satisfy the happens-before, wrt any other subsequent read/write (within the lock), then we don't need to make changes - my quick review indicated this was not guaranteed, but perhaps I missed something ?
There is only the access to `nameToAccums` of `tm` other than the updates to `_externalAccums`. And the access doesn't require the read/write lock. So I think it satisfies the happens-before relationship?
--
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-48791][CORE] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList [spark]
Posted by "Ngone51 (via GitHub)" <gi...@apache.org>.
Ngone51 commented on PR #47197:
URL: https://github.com/apache/spark/pull/47197#issuecomment-2222240507
Here is the backport PR https://github.com/apache/spark/pull/47297. @cloud-fan @LuciferYang Could you take a look? Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org