You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2024/07/19 03:12:21 UTC
(spark) branch branch-3.4 updated: [SPARK-48791][CORE][3.4] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList
This is an automated email from the ASF dual-hosted git repository.
dongjoon pushed a commit to branch branch-3.4
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-3.4 by this push:
new 20da97572574 [SPARK-48791][CORE][3.4] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList
20da97572574 is described below
commit 20da9757257439a161b977f684efc9df832be63e
Author: Yi Wu <yi...@databricks.com>
AuthorDate: Thu Jul 18 20:12:12 2024 -0700
[SPARK-48791][CORE][3.4] Fix perf regression caused by the accumulators registration overhead using CopyOnWriteArrayList
This PR backports https://github.com/apache/spark/pull/47197 to branch-3.4.
### What changes were proposed in this pull request?
This PR proposes to use the `ArrayBuffer` together with the read/write lock rather than `CopyOnWriteArrayList` for `TaskMetrics._externalAccums`.
### Why are the changes needed?
Fix the perf regression that caused by the accumulators registration overhead using `CopyOnWriteArrayList`.
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Manually tested.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #47348 from Ngone51/SPARK-48791-3.4.
Authored-by: Yi Wu <yi...@databricks.com>
Signed-off-by: Dongjoon Hyun <dh...@apple.com>
---
core/pom.xml | 4 +++
.../org/apache/spark/util/ArrayImplicits.scala | 35 ++++++++++++++++++++
.../org/apache/spark/util/ArrayImplicits.scala | 35 ++++++++++++++++++++
.../org/apache/spark/executor/TaskMetrics.scala | 38 ++++++++++++++++++----
mllib-local/pom.xml | 12 +++++++
mllib/pom.xml | 10 ++++++
pom.xml | 6 ++++
7 files changed, 134 insertions(+), 6 deletions(-)
diff --git a/core/pom.xml b/core/pom.xml
index a12b328ddad1..0f03147dffac 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -267,6 +267,10 @@
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-xml_${scala.binary.version}</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.scala-lang.modules</groupId>
+ <artifactId>scala-collection-compat_${scala.binary.version}</artifactId>
+ </dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
diff --git a/core/src/main/scala-2.12/org/apache/spark/util/ArrayImplicits.scala b/core/src/main/scala-2.12/org/apache/spark/util/ArrayImplicits.scala
new file mode 100644
index 000000000000..82c6c75bd51a
--- /dev/null
+++ b/core/src/main/scala-2.12/org/apache/spark/util/ArrayImplicits.scala
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.util
+
+import scala.collection.compat.immutable
+
+/**
+ * Implicit methods related to Scala Array.
+ */
+private[spark] object ArrayImplicits {
+
+ implicit class SparkArrayOps[T](xs: Array[T]) {
+
+ /**
+ * Wraps an Array[T] as an immutable.ArraySeq[T] without copying.
+ */
+ def toImmutableArraySeq: immutable.ArraySeq[T] =
+ immutable.ArraySeq.unsafeWrapArray(xs)
+ }
+}
diff --git a/core/src/main/scala-2.13/org/apache/spark/util/ArrayImplicits.scala b/core/src/main/scala-2.13/org/apache/spark/util/ArrayImplicits.scala
new file mode 100644
index 000000000000..38c2a415af3d
--- /dev/null
+++ b/core/src/main/scala-2.13/org/apache/spark/util/ArrayImplicits.scala
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.util
+
+import scala.collection.immutable
+
+/**
+ * Implicit methods related to Scala Array.
+ */
+private[spark] object ArrayImplicits {
+
+ implicit class SparkArrayOps[T](xs: Array[T]) {
+
+ /**
+ * Wraps an Array[T] as an immutable.ArraySeq[T] without copying.
+ */
+ def toImmutableArraySeq: immutable.ArraySeq[T] =
+ immutable.ArraySeq.unsafeWrapArray(xs)
+ }
+}
diff --git a/core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala b/core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala
index 78b39b0cbda6..468969b8d332 100644
--- a/core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala
+++ b/core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala
@@ -17,7 +17,7 @@
package org.apache.spark.executor
-import java.util.concurrent.CopyOnWriteArrayList
+import java.util.concurrent.locks.ReentrantReadWriteLock
import scala.collection.JavaConverters._
import scala.collection.mutable.{ArrayBuffer, LinkedHashMap}
@@ -29,6 +29,7 @@ import org.apache.spark.internal.config.Tests.IS_TESTING
import org.apache.spark.scheduler.AccumulableInfo
import org.apache.spark.storage.{BlockId, BlockStatus}
import org.apache.spark.util._
+import org.apache.spark.util.ArrayImplicits._
/**
@@ -150,6 +151,11 @@ class TaskMetrics private[spark] () extends Serializable {
private[spark] def setUpdatedBlockStatuses(v: Seq[(BlockId, BlockStatus)]): Unit =
_updatedBlockStatuses.setValue(v.asJava)
+ private val (readLock, writeLock) = {
+ val lock = new ReentrantReadWriteLock()
+ (lock.readLock(), lock.writeLock())
+ }
+
/**
* Metrics related to reading data from a [[org.apache.spark.rdd.HadoopRDD]] or from persisted
* data, defined only in tasks with input.
@@ -264,12 +270,32 @@ 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.toArray.toImmutableArraySeq
+ }
+
+ private def withReadLock[B](fn: => B): B = {
+ readLock.lock()
+ try {
+ fn
+ } finally {
+ readLock.unlock()
+ }
+ }
+
+ private def withWriteLock[B](fn: => B): B = {
+ writeLock.lock()
+ try {
+ fn
+ } finally {
+ writeLock.unlock()
+ }
+ }
- private[spark] def registerAccumulator(a: AccumulatorV2[_, _]): Unit = {
- _externalAccums.add(a)
+ private[spark] def registerAccumulator(a: AccumulatorV2[_, _]): Unit = withWriteLock {
+ _externalAccums += a
}
private[spark] def accumulators(): Seq[AccumulatorV2[_, _]] = internalAccums ++ externalAccums
@@ -335,7 +361,7 @@ private[spark] object TaskMetrics extends Logging {
tmAcc.metadata = acc.metadata
tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
} else {
- tm._externalAccums.add(acc)
+ tm._externalAccums += acc
}
}
tm
diff --git a/mllib-local/pom.xml b/mllib-local/pom.xml
index 36d43f97dd43..dba493b22a92 100644
--- a/mllib-local/pom.xml
+++ b/mllib-local/pom.xml
@@ -37,7 +37,19 @@
<dependency>
<groupId>org.scalanlp</groupId>
<artifactId>breeze_${scala.binary.version}</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>org.scala-lang.modules</groupId>
+ <artifactId>scala-collection-compat_${scala.binary.version}</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
+ <!-- #if scala-2.13 --><!--
+ <dependency>
+ <groupId>org.scala-lang.modules</groupId>
+ <artifactId>scala-collection-compat_${scala.binary.version}</artifactId>
+ </dependency>
+ --><!-- #endif scala-2.13 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
diff --git a/mllib/pom.xml b/mllib/pom.xml
index 6e1a40db331e..1366fa6f9e01 100644
--- a/mllib/pom.xml
+++ b/mllib/pom.xml
@@ -96,10 +96,20 @@
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-parallel-collections_${scala.binary.version}</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.scala-lang.modules</groupId>
+ <artifactId>scala-collection-compat_${scala.binary.version}</artifactId>
+ </dependency>
--><!-- #endif scala-2.13 -->
<dependency>
<groupId>org.scalanlp</groupId>
<artifactId>breeze_${scala.binary.version}</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>org.scala-lang.modules</groupId>
+ <artifactId>scala-collection-compat_${scala.binary.version}</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
diff --git a/pom.xml b/pom.xml
index 282a46910902..9c70f29162e8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -170,6 +170,7 @@
<commons.collections4.version>4.4</commons.collections4.version>
<scala.version>2.12.17</scala.version>
<scala.binary.version>2.12</scala.binary.version>
+ <scala-collection-compat.version>2.7.0</scala-collection-compat.version>
<scalatest-maven-plugin.version>2.2.0</scalatest-maven-plugin.version>
<!--
This needs to be managed in different profiles to avoid
@@ -1110,6 +1111,11 @@
<artifactId>scala-xml_${scala.binary.version}</artifactId>
<version>2.1.0</version>
</dependency>
+ <dependency>
+ <groupId>org.scala-lang.modules</groupId>
+ <artifactId>scala-collection-compat_${scala.binary.version}</artifactId>
+ <version>${scala-collection-compat.version}</version>
+ </dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org