viirya commented on code in PR #60540:
URL: https://github.com/apache/spark/pull/60540#discussion_r4583879044
##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -973,30 +1012,16 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
// Wrap the socket InputStream to handle idle timeout, matching sync mode
behavior:
// - Log warning on each timeout
- // - If killOnIdleTimeout=true: kill worker, then throw
PythonWorkerException
+ // - If killOnIdleTimeout=true: kill worker, then throw
PythonWorkerException once the
+ // read ends or fails
// - If killOnIdleTimeout=false: log only, retry read (continue waiting)
val idleTimeoutHandler = new IdleTimeoutHandler(
worker, handle, idleTimeoutSeconds, killOnIdleTimeout, Some(context),
logName)
val socketInput = new InputStream {
private val inner = worker.channel.socket().getInputStream
- override def read(): Int = doRead(() => inner.read())
+ override def read(): Int = idleTimeoutHandler.read(inner.read())
override def read(b: Array[Byte], off: Int, len: Int): Int =
- doRead(() => inner.read(b, off, len))
- private def doRead(op: () => Int): Int = {
- var result = 0
- var retry = true
- while (retry) {
- try {
- result = op()
- retry = false
- } catch {
- case _: java.net.SocketTimeoutException =>
- idleTimeoutHandler.onIdleTimeout(hasInputs = true)
- }
- }
- idleTimeoutHandler.throwIfKilled(result)
- result
- }
+ idleTimeoutHandler.read(inner.read(b, off, len))
Review Comment:
Agreed, and it is not rare: with a hanging UDF and more input than the
socket buffers hold, the writer's `Broken pipe` won every time I tried. Fixed
here rather than separately. I did not key it on `IOException` alone, since a
writer that failed to produce the input (e.g. an `IOException` from reading the
source) should stay the reported error. The pipelined writer now records
whether it failed in a write to the worker socket, and `handleException`
prefers the idle timeout error only then, with the writer's error added as
suppressed.
##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -702,15 +703,53 @@ private[spark] object BasePythonRunner extends Logging {
}
/**
- * Throws a [[PythonWorkerException]] if `n`, the result of a read from
the worker, is the
- * end of the stream and the worker was killed because of the idle timeout.
+ * Runs `op`, which reads from the worker, and returns its result.
+ *
+ * If the read times out, which only a blocking socket with a read timeout
does, the idle
+ * timeout is handled and the read is tried again. Once the worker has
been killed because
+ * of the idle timeout, the end of the stream and a failure of the read
both throw a
+ * [[PythonWorkerException]] for the idle timeout: the connection of a
killed worker can be
+ * reset instead of closed, for example when the worker had unread input.
A failure of the
+ * read with no kill is thrown as is.
*/
- def throwIfKilled(n: Int): Unit = {
+ def read(op: => Int): Int = {
+ var n = 0
+ var done = false
+ while (!done) {
+ try {
+ n = op
+ done = true
+ } catch {
+ case _: SocketTimeoutException =>
+ onIdleTimeout(hasInputs = true)
Review Comment:
Right, and it is the same as before. I would rather do it separately: it
turns a hang into a task failure, and for a worker forked by the daemon I am
not sure `isAlive()` is reliable between the kill and the daemon reaping it. I
will file a JIRA for it.
##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -702,15 +703,53 @@ private[spark] object BasePythonRunner extends Logging {
}
/**
- * Throws a [[PythonWorkerException]] if `n`, the result of a read from
the worker, is the
- * end of the stream and the worker was killed because of the idle timeout.
+ * Runs `op`, which reads from the worker, and returns its result.
+ *
+ * If the read times out, which only a blocking socket with a read timeout
does, the idle
+ * timeout is handled and the read is tried again. Once the worker has
been killed because
+ * of the idle timeout, the end of the stream and a failure of the read
both throw a
+ * [[PythonWorkerException]] for the idle timeout: the connection of a
killed worker can be
+ * reset instead of closed, for example when the worker had unread input.
A failure of the
+ * read with no kill is thrown as is.
*/
- def throwIfKilled(n: Int): Unit = {
+ def read(op: => Int): Int = {
+ var n = 0
+ var done = false
+ while (!done) {
+ try {
+ n = op
+ done = true
+ } catch {
+ case _: SocketTimeoutException =>
+ onIdleTimeout(hasInputs = true)
+ case e: IOException if pythonWorkerKilled =>
Review Comment:
Good point. A `ClosedChannelException` is no longer attributed to the kill,
for both reads and writes, and there is a test for it.
##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -702,15 +703,53 @@ private[spark] object BasePythonRunner extends Logging {
}
/**
- * Throws a [[PythonWorkerException]] if `n`, the result of a read from
the worker, is the
- * end of the stream and the worker was killed because of the idle timeout.
+ * Runs `op`, which reads from the worker, and returns its result.
+ *
+ * If the read times out, which only a blocking socket with a read timeout
does, the idle
+ * timeout is handled and the read is tried again. Once the worker has
been killed because
+ * of the idle timeout, the end of the stream and a failure of the read
both throw a
+ * [[PythonWorkerException]] for the idle timeout: the connection of a
killed worker can be
+ * reset instead of closed, for example when the worker had unread input.
A failure of the
+ * read with no kill is thrown as is.
*/
- def throwIfKilled(n: Int): Unit = {
+ def read(op: => Int): Int = {
+ var n = 0
+ var done = false
+ while (!done) {
+ try {
+ n = op
+ done = true
+ } catch {
+ case _: SocketTimeoutException =>
+ onIdleTimeout(hasInputs = true)
+ case e: IOException if pythonWorkerKilled =>
+ throw killedException(e)
+ }
+ }
if (n == -1 && pythonWorkerKilled) {
- throw new PythonWorkerException(withTerminationDiagnostics(
- "Python worker process terminated due to idle timeout " +
- s"(timeout: $idleTimeoutSeconds seconds)", handle))
+ throw killedException(null)
}
+ n
+ }
+
+ /**
+ * Runs `op`, which writes to the worker, and returns its result. A
failure of the write
+ * after the worker has been killed because of the idle timeout throws a
+ * [[PythonWorkerException]] for the idle timeout; with no kill it is
thrown as is.
+ */
+ def write(op: => Int): Int = {
+ try {
+ op
+ } catch {
+ case e: IOException if pythonWorkerKilled =>
+ throw killedException(e)
+ }
+ }
+
+ private def killedException(cause: Throwable): PythonWorkerException = {
+ new PythonWorkerException(withTerminationDiagnostics(
+ "Python worker process terminated due to idle timeout " +
Review Comment:
I kept the text as is, since it is what the existing tests and users see
today; changing it is a separate message change.
##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -1532,7 +1557,7 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
while (n == 0) {
idleTimeoutHandler.select(hasInput || buffer.hasRemaining)
if (worker.selectionKey.isReadable) {
- n = worker.channel.read(buf)
+ n = idleTimeoutHandler.read(worker.channel.read(buf))
Review Comment:
Done. The selector streams call `read(buf)` and `write(buf)`, which take the
buffer. The by-name form is now `readBlocking` and is only used by the
pipelined stream.
##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -612,9 +612,10 @@ private[spark] object BasePythonRunner extends Logging {
* worker has closed the connection.
Review Comment:
Updated the summary as suggested.
##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -702,15 +703,53 @@ private[spark] object BasePythonRunner extends Logging {
}
/**
- * Throws a [[PythonWorkerException]] if `n`, the result of a read from
the worker, is the
- * end of the stream and the worker was killed because of the idle timeout.
+ * Runs `op`, which reads from the worker, and returns its result.
+ *
+ * If the read times out, which only a blocking socket with a read timeout
does, the idle
+ * timeout is handled and the read is tried again. Once the worker has
been killed because
+ * of the idle timeout, the end of the stream and a failure of the read
both throw a
+ * [[PythonWorkerException]] for the idle timeout: the connection of a
killed worker can be
+ * reset instead of closed, for example when the worker had unread input.
A failure of the
+ * read with no kill is thrown as is.
*/
- def throwIfKilled(n: Int): Unit = {
+ def read(op: => Int): Int = {
+ var n = 0
+ var done = false
+ while (!done) {
+ try {
+ n = op
+ done = true
+ } catch {
+ case _: SocketTimeoutException =>
Review Comment:
Added: an `op` that times out twice and then returns -1, checking a single
`destroy` call and the "Waiting for" warning. I also left a comment on the case
order.
##########
core/src/test/scala/org/apache/spark/api/python/BasePythonRunnerSuite.scala:
##########
@@ -1108,7 +1111,120 @@ class BasePythonRunnerSuite extends SparkFunSuite with
SharedSparkContext {
"Idle timeout reached for Python worker (timeout: 3 seconds). " +
"No data received from the worker process - handle.map(_.isAlive) =
None, " +
s"$blockingWorkerStatus - $task"))
- handler.throwIfKilled(-1)
+ assert(handler.read(-1) === -1)
+ }
+ }
+
+ /**
+ * Closes `peer` so that the other end of the connection sees a reset
instead of the end of
+ * the stream, like the connection of a worker that is killed while it has
unread input.
+ */
+ private def reset(peer: SocketChannel): Unit = {
+ peer.setOption[Integer](StandardSocketOptions.SO_LINGER, 0)
+ peer.close()
+ }
+
+ test("a read that fails after the idle timeout killed the worker reports the
idle timeout") {
+ // Pipelined mode: the read times out, the worker is killed, and the read
that is tried
+ // again fails with a connection reset.
+ withIdleWorker(blocking = true) { (worker, peer) =>
+ val handle = new RecordingWorkerHandle(
+ diagnostics = Some("worker stack"), onDestroy = () => reset(peer))
+ val handler = new BasePythonRunner.IdleTimeoutHandler(
+ worker, Some(handle), idleTimeoutSeconds = 1, killOnIdleTimeout = true,
+ Some(TaskContext.empty()), idleTimeoutLoggerName)
+ worker.channel.socket().setSoTimeout(1000)
+ val in = worker.channel.socket().getInputStream
+ val e = intercept[PythonWorkerException] {
+ handler.read(in.read())
Review Comment:
Added a test that reads through a real `ReaderInputStream` whose peer is
reset on `destroy()`. The pipelined stream is built inside a private method
that also starts the writer thread, and the planner stream is private to
sql/core, so those two still rely on the PySpark tests.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]