Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ trait EvalPythonExec extends UnaryExecNode {

inputRDD.mapPartitions { iter =>
val context = TaskContext.get()
val contextAwareIterator = new ContextAwareIterator(iter, context)

// The queue used to buffer input rows so we can drain it to
// combine input with output from Python.
Expand Down Expand Up @@ -120,7 +121,7 @@ trait EvalPythonExec extends UnaryExecNode {
}.toSeq)

// Add rows to queue to join later with the result.
val projectedRowIter = iter.map { inputRow =>
val projectedRowIter = contextAwareIterator.map { inputRow =>
queue.add(inputRow.asInstanceOf[UnsafeRow])
projection(inputRow)
}
Expand All @@ -137,3 +138,18 @@ trait EvalPythonExec extends UnaryExecNode {
}
}
}

/**
* A TaskContext aware iterator.
*
* As the Python evaluation consumes the parent iterator in a separate thread,
* it could consume more data from the parent even after the task ends and the parent is closed.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add "If an off-heap column vector exists in the parent iterator, it could cause segmentation fault which crashes the executor." here too?

Current phrase is not clear why it is bad to read closed parent.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added the sentence. Thanks!

* Thus, we should use ContextAwareIterator to stop consuming after the task ends.
*/
class ContextAwareIterator[IN](iter: Iterator[IN], context: TaskContext) extends Iterator[IN] {
Copy link
Member

@dongjoon-hyun dongjoon-hyun Dec 23, 2020

Choose a reason for hiding this comment

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

This looks like a general class. Can we put this into more general package instead of org.apache.sql.execution.python package as a separate file?

Copy link
Member Author

@ueshin ueshin Dec 23, 2020

Choose a reason for hiding this comment

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

I moved the class to org.apache.spark.util package. cc @gatorsmile


override def hasNext: Boolean =
!context.isCompleted() && !context.isInterrupted() && iter.hasNext

override def next(): IN = iter.next()
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ case class MapInPandasExec(
val pythonRunnerConf = ArrowUtils.getPythonRunnerConfMap(conf)
val outputTypes = child.schema

val context = TaskContext.get()
val contextAwareIterator = new ContextAwareIterator(inputIter, context)

// Here we wrap it via another row so that Python sides understand it
// as a DataFrame.
val wrappedIter = inputIter.map(InternalRow(_))
val wrappedIter = contextAwareIterator.map(InternalRow(_))

// DO NOT use iter.grouped(). See BatchIterator.
val batchIter =
if (batchSize > 0) new BatchIterator(wrappedIter, batchSize) else Iterator(wrappedIter)

val context = TaskContext.get()

val columnarBatchIter = new ArrowPythonRunner(
chainedFunc,
PythonEvalType.SQL_MAP_PANDAS_ITER_UDF,
Expand Down