izchen commented on a change in pull request #29028:
URL: https://github.com/apache/spark/pull/29028#discussion_r452858008
##########
File path: core/src/main/scala/org/apache/spark/rdd/RDD.scala
##########
@@ -1509,22 +1509,26 @@ abstract class RDD[T: ClassTag](
* @return an array of top elements
*/
def takeOrdered(num: Int)(implicit ord: Ordering[T]): Array[T] = withScope {
- if (num == 0) {
+ if (num == 0 || partitions.length == 0) {
Array.empty
} else {
- val mapRDDs = mapPartitions { items =>
- // Priority keeps the largest elements, so let's reverse the ordering.
- val queue = new BoundedPriorityQueue[T](num)(ord.reverse)
- queue ++= collectionUtils.takeOrdered(items, num)(ord)
- Iterator.single(queue)
- }
- if (mapRDDs.partitions.length == 0) {
- Array.empty
- } else {
+ if (conf.get(RDD_TAKE_ORDERED_MERGE_IN_DRIVER)) {
+ val mapRDDs = mapPartitions { items =>
+ // Priority keeps the largest elements, so let's reverse the
ordering.
+ val queue = new BoundedPriorityQueue[T](num)(ord.reverse)
+ queue ++= collectionUtils.takeOrdered(items, num)(ord)
+ Iterator.single(queue)
+ }
mapRDDs.reduce { (queue1, queue2) =>
queue1 ++= queue2
queue1
}.toArray.sorted(ord)
+ } else {
+ mapPartitions { items =>
+ collectionUtils.takeOrdered(items, num)(ord)
+ }.repartition(1).mapPartitions { items =>
Review comment:
Thank you very much for code review.
The analysis of several implementations is as follows:
_p = rdd.getNumPartitions_
_reduce - PriorityQueue_(Original implementation) => At worst, _O(p * k)_
memory may be used in the driver, but intermediate result(local TopK) merging
does not need to wait until all partitions are returned. This is executed in
parallel.
_repartition(1) - guava.QuickSelect_ => The merging takes place in the
executor. The merging algorithm itself uses _O(k)_ extra memory, and the time
complexity is _O(p * k)_. The intermediate result data is from
ShuffleBlockFetcherIterator of spark shuffle, the iterator does not use too
much memory.
_treeReduce(depth=2) - guava.QuickSelect_ => At worst, _O(sqrt(p) * k)_
memory may be used in the driver. The memory usage in the driver is the same as
_repartition(1)_. Compared with _repartition(1)_ it increases the parallelism
of local TopK merging, which can improve the speed of merging under very large
amount of data, but it uses more memory on the driver.
The number of intermediate results that need to be merged is _p * k_. in
general, this number is not very large, and the non parallel _O(p * k)_ merging
algorithm is acceptable.
So I think maybe _repartition(1)_ is a better choice.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]