You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/12/14 02:44:37 UTC
[GitHub] [spark] amaliujia opened a new pull request, #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
amaliujia opened a new pull request, #39057:
URL: https://github.com/apache/spark/pull/39057
<!--
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
'core/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.
-->
In current Spark optimizer, a single partition shuffle might be created for a limit if this limit is not the last non-action operation (e.g. a filter following the limit and the data size exceeds a limit). There is a possibility that the previous output partitions before go into this limit are sorted. The single partition shuffle approach has a correctness bug in this case: shuffle read partitions could be out of partition order and the limit exec just take the first limit rows which could lose the order thus result into wrong result. This is a shuffle so it is relatively costly. Meanwhile, to correct this bug, a native solution is to sort all the data fed into limit again, which is another overhead.
So we propose a row count based AQE algorithm that optimizes this problem by two folds:
So we propose a row count based AQE algorithm that optimizes this problem by two folds:
Avoid the extra sort on the shuffle read side (or with the limit exec) to achieve the correct result.
Avoid reading all shuffle data from mappers for this single partition shuffle to reduce shuffle cost.
Note that 1. is only applied for the sorted partition case where 2. is applied for general single partition shuffle + limit case
The algorithm works as the following:
1. Each mapper will record a row count when writing shuffle data.
2. Since this is single shuffle partition case, there is only one partition but N mappers.
3. A accumulatorV2 is implemented to collect a list of tuple which records the mapping between mapper id and the number of row written by the mapper (row count metrics)
4. AQE framework detects a plan shape of shuffle plus a global limit.
5. AQE framework reads necessary data from mappers based on the limit. For example, if mapper 1 writes 200 rows and mapper 2 writes 300 rows, and the limit is 500, AQE creates shuffle read node to write from both mapper 1 and 2, thus skip the left mappers.
6. This is both correct for limit with the sorted or non-sorted partitions.
This is the first step to implement the idea in https://issues.apache.org/jira/browse/SPARK-41512, which is to implement a row count accumulator that will be used to collect row
### 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.
-->
Implement the optimization algorithm for global limit with single partition shuffle
### 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.
-->
UT
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1053988208
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Long, jl.Long)]] {
Review Comment:
row count should be long, map index should be int.
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1051833571
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Long, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Long, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Long, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean =
+ this.synchronized(getOrCreate.isEmpty)
Review Comment:
```suggestion
override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
```
--
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
[GitHub] [spark] amaliujia commented on pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on PR #39057:
URL: https://github.com/apache/spark/pull/39057#issuecomment-1350378556
@cloud-fan
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1048700762
##########
core/src/main/scala/org/apache/spark/util/AccumulatorV2.scala:
##########
@@ -513,3 +513,81 @@ class CollectionAccumulator[T] extends AccumulatorV2[T, java.util.List[T]] {
getOrCreate.addAll(newValue)
}
}
+
+
+/**
+ * An [[AccumulatorV2 counter]] for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[java.util.List[jl.Long]]] {
Review Comment:
We can put it in the sql module, or probably the same file with shuffle node.
--
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
[GitHub] [spark] cloud-fan closed pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan closed pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
URL: https://github.com/apache/spark/pull/39057
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1048850181
##########
core/src/main/scala/org/apache/spark/util/AccumulatorV2.scala:
##########
@@ -513,3 +513,81 @@ class CollectionAccumulator[T] extends AccumulatorV2[T, java.util.List[T]] {
getOrCreate.addAll(newValue)
}
}
+
+
+/**
+ * An [[AccumulatorV2 counter]] for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[java.util.List[jl.Long]]] {
+
+ private var _agg: java.util.List[java.util.List[jl.Long]] = _
Review Comment:
Which of the following that you are thinking:
1. a list of integers where every two forms a pair for partition id and its row count?
2. a list of integers that the index is the mapper/partition id and the value is the row count?
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1048703036
##########
core/src/main/scala/org/apache/spark/util/AccumulatorV2.scala:
##########
@@ -513,3 +513,81 @@ class CollectionAccumulator[T] extends AccumulatorV2[T, java.util.List[T]] {
getOrCreate.addAll(newValue)
}
}
+
+
+/**
+ * An [[AccumulatorV2 counter]] for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[java.util.List[jl.Long]]] {
+
+ private var _agg: java.util.List[java.util.List[jl.Long]] = _
Review Comment:
why it's a list of list? we need row counts for N mappers, and a list of integer should work.
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1053786296
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Long, jl.Long)]] {
Review Comment:
The firs long here (aka `IN`) defines
```
/**
* Takes the inputs and accumulates.
*/
def add(v: IN): Unit
```
So this should be a long for the row count?
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1054781242
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Long, jl.Long)]] {
Review Comment:
I think I wrongly understand the template type.
Changed to Integer for the map index.
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1055080098
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper index, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Integer, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Integer, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Integer, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
+ val p = getOrCreate.get(0)._1
+ val n = getOrCreate.get(0)._2 + 1
+ getOrCreate.set(0, (p, n))
+ }
+ }
+
+ def setPartitionId(id: jl.Integer): Unit = {
+ this.synchronized {
+ if (isZero) {
+ getOrCreate.add((id, 0))
+ } else {
+ val n = getOrCreate.get(0)._2
Review Comment:
let's add an assert
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1055065203
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper index, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Integer, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Integer, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Integer, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
+ val p = getOrCreate.get(0)._1
+ val n = getOrCreate.get(0)._2 + 1
+ getOrCreate.set(0, (p, n))
+ }
+ }
+
+ def setPartitionId(id: jl.Integer): Unit = {
+ this.synchronized {
+ if (isZero) {
+ getOrCreate.add((id, 0))
+ } else {
+ val n = getOrCreate.get(0)._2
Review Comment:
This is because I don't know the invocation sequence of the accumulator APIs on the executor side. So I added this branch for safe.
If the `setPartitionId` is always called before any `add`, then we can does an assert on isZero and remove this branch.
What do you think?
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1051836985
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
Review Comment:
```suggestion
* An AccumulatorV2 counter for collecting a list of (mapper index, row count).
```
mapper id has a different meaning.
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1051837195
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Long, jl.Long)]] {
Review Comment:
`TaskContext.partitionId` is int, do we really need long 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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1055049567
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper index, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Integer, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Integer, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Integer, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
+ val p = getOrCreate.get(0)._1
+ val n = getOrCreate.get(0)._2 + 1
+ getOrCreate.set(0, (p, n))
+ }
+ }
+
+ def setPartitionId(id: jl.Integer): Unit = {
+ this.synchronized {
+ if (isZero) {
+ getOrCreate.add((id, 0))
+ } else {
+ val n = getOrCreate.get(0)._2
Review Comment:
when can we hit this 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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1055049402
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper index, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Integer, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Integer, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Integer, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
Review Comment:
assert(getOrCreate.size == 1)?
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1051834773
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Long, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Long, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Long, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean =
+ this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
+ val p = getOrCreate.get(0)._1
+ val n = getOrCreate.get(0)._2 + 1
+ getOrCreate.set(0, (p, n))
+ }
+ }
+
+ def setPartitionId(id: Long): Unit = {
+ this.synchronized {
+ val p = id
Review Comment:
what does this do?
--
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
[GitHub] [spark] AmplabJenkins commented on pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on PR #39057:
URL: https://github.com/apache/spark/pull/39057#issuecomment-1354336854
Can one of the admins verify this patch?
--
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
[GitHub] [spark] cloud-fan commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1049199171
##########
core/src/main/scala/org/apache/spark/util/AccumulatorV2.scala:
##########
@@ -513,3 +513,81 @@ class CollectionAccumulator[T] extends AccumulatorV2[T, java.util.List[T]] {
getOrCreate.addAll(newValue)
}
}
+
+
+/**
+ * An [[AccumulatorV2 counter]] for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[java.util.List[jl.Long]]] {
+
+ private var _agg: java.util.List[java.util.List[jl.Long]] = _
Review Comment:
a list of tuple2[int, int]?
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1050232038
##########
core/src/main/scala/org/apache/spark/util/AccumulatorV2.scala:
##########
@@ -513,3 +513,81 @@ class CollectionAccumulator[T] extends AccumulatorV2[T, java.util.List[T]] {
getOrCreate.addAll(newValue)
}
}
+
+
+/**
+ * An [[AccumulatorV2 counter]] for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[java.util.List[jl.Long]]] {
Review Comment:
Moved to sql module.
##########
core/src/main/scala/org/apache/spark/util/AccumulatorV2.scala:
##########
@@ -513,3 +513,81 @@ class CollectionAccumulator[T] extends AccumulatorV2[T, java.util.List[T]] {
getOrCreate.addAll(newValue)
}
}
+
+
+/**
+ * An [[AccumulatorV2 counter]] for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[java.util.List[jl.Long]]] {
+
+ private var _agg: java.util.List[java.util.List[jl.Long]] = _
Review Comment:
Changed to use Scala tuple
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1053782460
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper id, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Long, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Long, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Long, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean =
+ this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
+ val p = getOrCreate.get(0)._1
+ val n = getOrCreate.get(0)._2 + 1
+ getOrCreate.set(0, (p, n))
+ }
+ }
+
+ def setPartitionId(id: Long): Unit = {
+ this.synchronized {
+ val p = id
Review Comment:
hmm I can remove this assignment and use `id` directly.
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1055065203
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper index, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Integer, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Integer, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Integer, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
+ val p = getOrCreate.get(0)._1
+ val n = getOrCreate.get(0)._2 + 1
+ getOrCreate.set(0, (p, n))
+ }
+ }
+
+ def setPartitionId(id: jl.Integer): Unit = {
+ this.synchronized {
+ if (isZero) {
+ getOrCreate.add((id, 0))
+ } else {
+ val n = getOrCreate.get(0)._2
Review Comment:
This is because I don't know the invocation sequence of the accumulator APIs on the executor side.
So I added this branch for safe.
If the `setPartitionId` is always called before any `add`, then we can does an assert on isZero and remove this branch.
What do you think?
--
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
[GitHub] [spark] amaliujia commented on a diff in pull request #39057: [SPARK-41513][SQL] Implement an accumulator to collect per mapper row count metrics
Posted by GitBox <gi...@apache.org>.
amaliujia commented on code in PR #39057:
URL: https://github.com/apache/spark/pull/39057#discussion_r1055085894
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper index, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Integer, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Integer, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Integer, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
Review Comment:
done
##########
sql/core/src/main/scala/org/apache/spark/sql/util/MapperRowCounter.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.sql.util
+
+import java.{lang => jl}
+
+import org.apache.spark.util.AccumulatorV2
+
+/**
+ * An AccumulatorV2 counter for collecting a list of (mapper index, row count).
+ *
+ * @since 3.4.0
+ */
+class MapperRowCounter extends AccumulatorV2[jl.Long, java.util.List[(jl.Integer, jl.Long)]] {
+
+ private var _agg: java.util.List[(jl.Integer, jl.Long)] = _
+
+ private def getOrCreate = {
+ _agg = Option(_agg).getOrElse(new java.util.ArrayList[(jl.Integer, jl.Long)]())
+ _agg
+ }
+
+ /**
+ * Returns false if this accumulator has had any values added to it or the sum is non-zero.
+ */
+ override def isZero: Boolean = this.synchronized(getOrCreate.isEmpty)
+
+ override def copyAndReset(): MapperRowCounter = new MapperRowCounter
+
+ override def copy(): MapperRowCounter = {
+ val newAcc = new MapperRowCounter()
+ this.synchronized {
+ newAcc.getOrCreate.addAll(getOrCreate)
+ }
+ newAcc
+ }
+
+ override def reset(): Unit = {
+ this.synchronized {
+ _agg = null
+ }
+ }
+
+ override def add(v: jl.Long): Unit = {
+ this.synchronized {
+ assert(!isZero, "agg must have been initialized")
+ val p = getOrCreate.get(0)._1
+ val n = getOrCreate.get(0)._2 + 1
+ getOrCreate.set(0, (p, n))
+ }
+ }
+
+ def setPartitionId(id: jl.Integer): Unit = {
+ this.synchronized {
+ if (isZero) {
+ getOrCreate.add((id, 0))
+ } else {
+ val n = getOrCreate.get(0)._2
Review Comment:
done
--
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