Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/src/main/scala/org/apache/spark/rdd/RDDBarrier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,27 @@ class RDDBarrier[T: ClassTag] private[spark] (rdd: RDD[T]) {
)
}

/**
* :: Experimental ::
* Returns a new RDD by applying a function to each partition of the wrapped RDD, while tracking
* the index of the original partition. And all tasks are launched together in a barrier stage.
* The interface is the same as [[org.apache.spark.rdd.RDD#mapPartitionsWithIndex]].
* Please see the API doc there.
* @see [[org.apache.spark.BarrierTaskContext]]
*/
@Experimental
@Since("3.0.0")
def mapPartitionsWithIndex[S: ClassTag](
f: (Int, Iterator[T]) => Iterator[S],
preservesPartitioning: Boolean = false): RDD[S] = rdd.withScope {
val cleanedF = rdd.sparkContext.clean(f)
new MapPartitionsRDD(
rdd,
(_: TaskContext, index: Int, iter: Iterator[T]) => cleanedF(index, iter),
preservesPartitioning,
isFromBarrier = true
)
}

// TODO: [SPARK-25247] add extra conf to RDDBarrier, e.g., timeout.
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class RDDBarrierSuite extends SparkFunSuite with SharedSparkContext {
assert(rdd2.isBarrier())
}

test("RDDBarrier mapPartitionsWithIndex") {
val rdd = sc.parallelize(1 to 12, 4)
assert(rdd.isBarrier() === false)

val rdd2 = rdd.barrier().mapPartitionsWithIndex((index, iter) => Iterator(index))
assert(rdd2.isBarrier())
assert(rdd2.collect().toList === List(0, 1, 2, 3))
}

test("create an RDDBarrier in the middle of a chain of RDDs") {
val rdd = sc.parallelize(1 to 10, 4).map(x => x * 2)
val rdd2 = rdd.barrier().mapPartitions(iter => iter).map(x => (x, x + 1))
Expand Down
1 change: 1 addition & 0 deletions dev/sparktestsupport/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def __hash__(self):
"pyspark.tests.test_join",
"pyspark.tests.test_profiler",
"pyspark.tests.test_rdd",
"pyspark.tests.test_rddbarrier",
"pyspark.tests.test_readwrite",
"pyspark.tests.test_serializers",
"pyspark.tests.test_shuffle",
Expand Down
14 changes: 14 additions & 0 deletions python/pyspark/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,20 @@ def func(s, iterator):
return f(iterator)
return PipelinedRDD(self.rdd, func, preservesPartitioning, isFromBarrier=True)

def mapPartitionsWithIndex(self, f, preservesPartitioning=False):
"""
.. note:: Experimental

Returns a new RDD by applying a function to each partition of the wrapped RDD, while
tracking the index of the original partition. And all tasks are launched together
in a barrier stage.
The interface is the same as :func:`RDD.mapPartitionsWithIndex`.
Please see the API doc there.

.. versionadded:: 3.0.0
"""
return PipelinedRDD(self.rdd, f, preservesPartitioning, isFromBarrier=True)


class PipelinedRDD(RDD):

Expand Down
50 changes: 50 additions & 0 deletions python/pyspark/tests/test_rddbarrier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# 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.
#
from pyspark.testing.utils import ReusedPySparkTestCase


class RDDBarrierTests(ReusedPySparkTestCase):
def test_map_partitions(self):
"""Test RDDBarrier.mapPartitions"""
rdd = self.sc.parallelize(range(12), 4)
self.assertFalse(rdd._is_barrier())

rdd1 = rdd.barrier().mapPartitions(lambda it: it)
self.assertTrue(rdd1._is_barrier())

def test_map_partitions_with_index(self):
"""Test RDDBarrier.mapPartitionsWithIndex"""
rdd = self.sc.parallelize(range(12), 4)
self.assertFalse(rdd._is_barrier())

def f(index, iterator):
yield index
rdd1 = rdd.barrier().mapPartitionsWithIndex(f)
self.assertTrue(rdd1._is_barrier())
self.assertEqual(rdd1.collect(), [0, 1, 2, 3])


if __name__ == "__main__":
import unittest
from pyspark.tests.test_rddbarrier import *

try:
import xmlrunner
testRunner = xmlrunner.XMLTestRunner(output='target/test-reports', verbosity=2)
except ImportError:
testRunner = None
unittest.main(testRunner=testRunner, verbosity=2)