viirya commented on code in PR #60507:
URL: https://github.com/apache/spark/pull/60507#discussion_r4575378804
##########
core/src/test/scala/org/apache/spark/api/python/BasePythonRunnerSuite.scala:
##########
@@ -883,4 +883,211 @@ class BasePythonRunnerSuite extends SparkFunSuite with
SharedSparkContext {
}
}
}
+
+ /** A worker handle that records how often it was asked to terminate the
worker. */
+ private class RecordingWorkerHandle(
+ destroyIssued: Boolean = true,
+ diagnostics: Option[String] = None) extends PythonWorkerHandle {
+ val destroyCalls = new AtomicInteger(0)
+ override def isAlive(): Boolean = true
+ override def destroy(): Boolean = {
+ destroyCalls.incrementAndGet()
+ destroyIssued
+ }
+ override def terminationDiagnostics(): Option[String] = diagnostics
+ }
+
+ /**
+ * Runs `f` with a worker whose channel is one end of a loopback connection,
and with the
+ * other end. With `blocking`, the worker is set up like in pipelined mode,
without a selector.
+ * Otherwise the worker only waits for output, like a worker whose input was
sent completely.
+ */
+ private def withIdleWorker(blocking: Boolean)(f: (PythonWorker,
SocketChannel) => Unit): Unit = {
+ val server = ServerSocketChannel.open()
+ server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress, 0))
+ val client = SocketChannel.open(server.getLocalAddress)
+ val accepted = server.accept()
+ client.configureBlocking(blocking)
+ val worker = PythonWorker(client).refresh()
Review Comment:
Each socket is now closed in its own `finally`.
##########
core/src/test/scala/org/apache/spark/api/python/BasePythonRunnerSuite.scala:
##########
@@ -883,4 +883,211 @@ class BasePythonRunnerSuite extends SparkFunSuite with
SharedSparkContext {
}
}
}
+
+ /** A worker handle that records how often it was asked to terminate the
worker. */
+ private class RecordingWorkerHandle(
+ destroyIssued: Boolean = true,
+ diagnostics: Option[String] = None) extends PythonWorkerHandle {
+ val destroyCalls = new AtomicInteger(0)
+ override def isAlive(): Boolean = true
+ override def destroy(): Boolean = {
+ destroyCalls.incrementAndGet()
+ destroyIssued
+ }
+ override def terminationDiagnostics(): Option[String] = diagnostics
+ }
+
+ /**
+ * Runs `f` with a worker whose channel is one end of a loopback connection,
and with the
+ * other end. With `blocking`, the worker is set up like in pipelined mode,
without a selector.
+ * Otherwise the worker only waits for output, like a worker whose input was
sent completely.
+ */
+ private def withIdleWorker(blocking: Boolean)(f: (PythonWorker,
SocketChannel) => Unit): Unit = {
+ val server = ServerSocketChannel.open()
+ server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress, 0))
+ val client = SocketChannel.open(server.getLocalAddress)
+ val accepted = server.accept()
+ client.configureBlocking(blocking)
+ val worker = PythonWorker(client).refresh()
+ try {
+ if (!blocking) {
+ worker.selectionKey.interestOps(SelectionKey.OP_READ)
+ }
+ f(worker, accepted)
+ } finally {
+ worker.stop()
+ accepted.close()
+ server.close()
+ }
+ }
+
+ private val idleTimeoutLoggerName =
"org.apache.spark.api.python.IdleTimeoutTestRunner"
+
+ /** Runs `f` and returns the warnings logged under `idleTimeoutLoggerName`.
*/
+ private def idleTimeoutWarnings(f: => Unit): Seq[String] = {
+ val logAppender = new LogAppender("idle timeout")
+ withLogAppender(logAppender, loggerNames = Seq(idleTimeoutLoggerName)) {
+ f
+ }
+ logAppender.loggingEvents.toSeq.map { event =>
+ assert(event.getLevel === Level.WARN)
+ assert(event.getLoggerName === idleTimeoutLoggerName)
+ event.getMessage.getFormattedMessage
+ }
+ }
+
+ private val nonBlockingWorkerStatus = "handle.map(_.isAlive) = Some(true), "
+
+ "channel.isConnected = true, channel.isBlocking = false, selector.isOpen =
true, " +
+ "selectionKey.isValid = true, selectionKey.interestOps = 1, "
+
+ test("idle timeout on the selector kills the worker once and fails the read
at the end " +
+ "of the stream") {
+ withIdleWorker(blocking = false) { (worker, _) =>
+ val handle = new RecordingWorkerHandle(diagnostics = Some("worker
stack"))
+ val handler = new BasePythonRunner.IdleTimeoutHandler(
+ worker, Some(handle), idleTimeoutSeconds = 1, killOnIdleTimeout = true,
+ Some(TaskContext.empty()), idleTimeoutLoggerName)
+ val task = "task 0.0 in stage 0 (TID 0)"
+
+ val firstTimeout = idleTimeoutWarnings {
+ handler.select(hasInputs = false)
+ }
+ assert(firstTimeout === Seq(
+ "Idle timeout reached for Python worker (timeout: 1 seconds). " +
+ "No data received from the worker process - " +
nonBlockingWorkerStatus +
+ s"hasInputs = false - $task",
+ s"Terminating Python worker process due to idle timeout (timeout: 1
seconds) - $task"))
+ assert(handle.destroyCalls.get() === 1)
+
+ // Until the worker closes the connection, later timeouts are only
logged.
+ val secondTimeout = idleTimeoutWarnings {
+ handler.select(hasInputs = true)
+ }
+ assert(secondTimeout === Seq(
+ "Waiting for Python worker process to terminate after idle timeout: " +
+ nonBlockingWorkerStatus + "hasInputs = true"))
+ assert(handle.destroyCalls.get() === 1)
+
+ // Only the end of the stream fails the read.
+ handler.throwIfKilled(0)
+ handler.throwIfKilled(16)
+ val e = intercept[PythonWorkerException] {
+ handler.throwIfKilled(-1)
+ }
+ assert(e.getMessage ===
+ "Python worker process terminated due to idle timeout (timeout: 1
seconds): worker stack")
+ }
+ }
+
+ test("idle timeout on the selector is not handled when the worker has output
or when " +
+ "there is no idle timeout") {
+ withIdleWorker(blocking = false) { (worker, peer) =>
+ val handle = new RecordingWorkerHandle()
+ val warnings = idleTimeoutWarnings {
+ peer.write(ByteBuffer.wrap(new Array[Byte](1)))
+ new BasePythonRunner.IdleTimeoutHandler(
+ worker, Some(handle), idleTimeoutSeconds = 1, killOnIdleTimeout =
true,
+ Some(TaskContext.empty()), idleTimeoutLoggerName).select(hasInputs =
false)
+ assert(worker.selectionKey.isReadable)
+ // Without an idle timeout the wait has no limit, so the pending
output ends it.
+ val noTimeoutHandler = new BasePythonRunner.IdleTimeoutHandler(
+ worker, Some(handle), idleTimeoutSeconds = 0, killOnIdleTimeout =
true,
+ Some(TaskContext.empty()), idleTimeoutLoggerName)
+ noTimeoutHandler.select(hasInputs = false)
+ noTimeoutHandler.throwIfKilled(-1)
+ }
+ assert(warnings.isEmpty)
+ assert(handle.destroyCalls.get() === 0)
+ }
+ }
+
+ test("idle timeout without killOnIdleTimeout only logs, without a task and
with another " +
+ "worker name") {
+ withIdleWorker(blocking = false) { (worker, _) =>
+ val handle = new RecordingWorkerHandle()
+ val handler = new BasePythonRunner.IdleTimeoutHandler(
+ worker, Some(handle), idleTimeoutSeconds = 1, killOnIdleTimeout =
false,
+ None, idleTimeoutLoggerName, workerName = "Python planner worker")
+ val expected = "Idle timeout reached for Python planner worker (timeout:
1 seconds). " +
+ "No data received from the worker process: " + nonBlockingWorkerStatus
+
+ "hasInputs = true"
+ // Every timeout is logged the same way, since the worker is never
killed.
+ assert(idleTimeoutWarnings(handler.select(hasInputs = true)) ===
Seq(expected))
+ assert(idleTimeoutWarnings(handler.select(hasInputs = true)) ===
Seq(expected))
Review Comment:
Only the first test waits for a real timeout now; the others call
`onIdleTimeout` directly.
--
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]