viirya opened a new pull request, #60507:
URL: https://github.com/apache/spark/pull/60507
### What changes were proposed in this pull request?
The handling of the Python worker idle timeout existed in three copies:
- `BasePythonRunner.ReaderInputStream`, the synchronous read path;
- the socket input stream that `BasePythonRunner.createPipelinedDataIn`
builds for the pipelined read path;
- `PythonPlannerRunner.WorkerInputStream`, on the driver.
Each copy logged the timeout, killed the worker if `killOnIdleTimeout` is
set, and failed the read with a `PythonWorkerException` once the killed worker
had closed the connection.
This PR moves that logic into one class,
`BasePythonRunner.IdleTimeoutHandler`, next to
`pythonWorkerStatusMessageWithContext`:
```scala
private[spark] class IdleTimeoutHandler(
worker: PythonWorker,
handle: Option[PythonWorkerHandle],
idleTimeoutSeconds: Long,
killOnIdleTimeout: Boolean,
context: Option[TaskContext],
loggerName: String,
workerName: String = "Python worker") extends Logging {
def select(hasInputs: => Boolean): Unit
def onIdleTimeout(hasInputs: => Boolean): Unit
def throwIfKilled(n: Int): Unit
}
```
The readers still decide how a timeout is detected, because that differs for
a real reason: the synchronous and planner readers wait on the worker's
selector (`select`), while the pipelined reader relies on the socket read
timeout and calls `onIdleTimeout` on `SocketTimeoutException`. All three pass
the result of each read to `throwIfKilled`.
The parameters cover the differences that existed between the copies:
- `hasInputs`, which only appears in the log message, is `hasInput ||
buffer.hasRemaining` on the synchronous path, `true` on the pipelined path and
`buffer.hasRemaining` in the planner runner.
- `context` is the task named in the log messages. The planner runner runs
on the driver and has none, so its messages have no task suffix.
- `workerName` is "Python planner worker" for the planner runner.
- `loggerName` keeps the messages under the logger of the runner that owns
the stream.
The handler keeps the "worker was killed" flag that each stream held before.
Like before, it is only touched by the thread that reads from the worker.
Differences between the copies that this PR deliberately keeps as they are:
- The pipelined path computes the socket timeout as
`idleTimeoutSeconds.toInt * 1000`, in `Int`, while the other two use
`TimeUnit.SECONDS.toMillis`.
- The pipelined path has no idle timeout when Unix domain sockets are
enabled.
- The planner runner separates the worker status in its log message with `:
` where the other two use ` - `.
### Why are the changes needed?
A fix to the idle timeout handling had to be repeated by hand in each copy,
and it was easy to miss one. The pipelined path also had no test for the idle
timeout.
### Does this PR introduce _any_ user-facing change?
No. The log messages, the loggers they are logged under and the exception
are the same as before on all three paths.
### How was this patch tested?
- New tests in `BasePythonRunnerSuite` for `IdleTimeoutHandler`. They
compare the exact log messages, the logger name and the exception message for
the three ways the handler is used, and cover `killOnIdleTimeout` on and off, a
`destroy()` that issues no termination request, a missing handle, and a
selector wait that ends because the worker has output.
- New PySpark test `PipelinedUDFTests.test_udf_kill_on_idle_timeout` for the
pipelined path.
- Existing PySpark tests `UDFTests.test_udf_kill_on_timeout` and
`UDTFTests.test_udtf_kill_on_timeout`, which cover the synchronous path and the
planner runner.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Fable 5.2)
This pull request and its description were written by Isaac.
--
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]