Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
15 changes: 14 additions & 1 deletion core/src/main/scala/org/apache/spark/rdd/RDDBarrier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package org.apache.spark.rdd

import scala.reflect.ClassTag

import org.apache.spark.BarrierTaskContext
import org.apache.spark.TaskContext
import org.apache.spark.annotation.{Experimental, Since}
import org.apache.spark.api.java.JavaRDD

/** Represents an RDD barrier, which forces Spark to launch tasks of this stage together. */
class RDDBarrier[T: ClassTag](rdd: RDD[T]) {
Expand All @@ -47,5 +47,18 @@ class RDDBarrier[T: ClassTag](rdd: RDD[T]) {
)
}

/**
* Expose a JavaRDD that wraps a barrier RDD generated from the prev RDD, to support launch
* barrier stage from python side.
*/
private[spark] def toJavaRDD(): JavaRDD[T] = {
val barrierRDD = new MapPartitionsRDD[T, T](
rdd,
(context, pid, iter) => iter,
preservesPartitioning = false,
isFromBarrier = true)
JavaRDD.fromRDD(barrierRDD)
}

/** TODO extra conf(e.g. timeout) */
}
50 changes: 50 additions & 0 deletions python/pyspark/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,26 @@ def toLocalIterator(self):
sock_info = self.ctx._jvm.PythonRDD.toLocalIteratorAndServe(self._jrdd.rdd())
return _load_from_socket(sock_info, self._jrdd_deserializer)

def barrier(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why we didn't mark the version so far here but we really should .. versionadded:: 2.4.0 here or

@since(2.4)
def barrier(self):
    ...

"""
.. note:: Experimental

Indicates that Spark must launch the tasks together for the current stage.

.. versionadded:: 2.4.0
"""
return RDDBarrier(self)

def isBarrier(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have this API in the JVM RDD?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In scala RDD there is a private[spark] isBarrier() function, we don't add this to JavaRDD

"""
.. note:: Experimental

Whether this RDD is in a barrier stage.

.. versionadded:: 2.4.0
"""
return self._jrdd.rdd().isBarrier()


def _prepare_for_python_RDD(sc, command):
# the serialized command will be compressed by broadcast
Expand All @@ -2429,6 +2449,36 @@ def _wrap_function(sc, func, deserializer, serializer, profiler=None):
sc.pythonVer, broadcast_vars, sc._javaAccumulator)


class RDDBarrier(object):

"""
.. note:: Experimental

An RDDBarrier turns an RDD into a barrier RDD, which forces Spark to launch tasks of the stage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: RDDBarrier -> RDD barrier

contains this RDD together.
Copy link
Member

@HyukjinKwon HyukjinKwon Aug 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto let's add .. versionadded:: 2.4.0 at the end. I guess optionally add them to each API here exposed as well.


.. versionadded:: 2.4.0
"""

def __init__(self, rdd):
self.rdd = rdd
self._jrdd = rdd._jrdd

def mapPartitions(self, f, preservesPartitioning=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we expose a package private method to get the annotated RDD with isBarrier=True in RDDBarrier, we can implement mapPartitions easily here:

jBarrierRdd = self._jrdd.rdd.barrier().barrierRdd.javaRdd
pyBarrierRdd = RDD(self._jrdd.rdd.barrier().barrierRdd.javaRdd)
pyBarrierRdd.mapPartitions(f, preservesPartitioning)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring?

"""
.. note:: Experimental

Return a new RDD by applying a function to each partition of this RDD.

.. versionadded:: 2.4.0
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we match the documentation, or why is it different?

FWIW, for coding block, just `blabla` should be good enough. Nicer if linked properly by like :class:`ClassName`.

def func(s, iterator):
return f(iterator)
jBarrierRdd = self._jrdd.rdd().barrier().toJavaRDD()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will materialize the java RDD, which means the map functions before and after barrier will be executed by 2 python workers.

We should not materialize the java RDD here, but just set a isBarrier flag in the pythhon PipelinedRDD.

pyBarrierRdd = RDD(jBarrierRdd, self.rdd.ctx, self.rdd._jrdd_deserializer)
return pyBarrierRdd.mapPartitions(f, preservesPartitioning)


class PipelinedRDD(RDD):

"""
Expand Down