dongjoon-hyun commented on code in PR #60539:
URL: https://github.com/apache/spark/pull/60539#discussion_r4581510938


##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -605,6 +605,24 @@ private[spark] object BasePythonRunner extends Logging {
       .getOrElse(message)
   }
 
+  /**
+   * Converts the idle timeout of a Python worker, in seconds, to the value to 
pass to
+   * `Socket.setSoTimeout`, which takes an `Int` of milliseconds and treats 0 
as no timeout.
+   *
+   * A timeout that is zero or negative means no timeout and gives 0. A 
positive timeout is
+   * converted without overflow, and one that exceeds `Int.MaxValue` 
milliseconds (about 24.8
+   * days) is capped at `Int.MaxValue`, the longest read timeout a socket can 
have, so that a
+   * positive timeout never turns into a negative value, a shorter timeout or 
no timeout.
+   */
+  private[spark] def idleTimeoutToSoTimeoutMillis(idleTimeoutSeconds: Long): 
Int = {
+    if (idleTimeoutSeconds <= 0) {
+      0
+    } else {
+      // TimeUnit.toMillis saturates at Long.MaxValue instead of overflowing.
+      math.min(TimeUnit.SECONDS.toMillis(idleTimeoutSeconds), 
Int.MaxValue.toLong).toInt

Review Comment:
   With this cap, an idle timeout above 2147483 seconds still fires earlier 
than configured, after about 24.8 days.
   
   For example, with `spark.python.worker.idleTimeoutSeconds=30d` (2592000 
seconds) and `killOnIdleTimeout=true`, a pipelined read gets a 
`SocketTimeoutException` after 2147483.647 seconds. Then `onIdleTimeout` calls 
`handle.destroy()`, the read gets -1, and the task fails with 
`PythonWorkerException` about 5 days before the configured 30 days.
   
   This is much better than the overflow, but it is still a shorter timeout 
than the user asked for. Could we honor the configured value instead? Please 
see my comment on L991 for two options.



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -968,8 +986,7 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
     // Set socket read timeout for idle timeout detection in pipelined mode.
     // Always set explicitly (including 0 = no timeout) because reused workers 
may
     // retain a stale SO_TIMEOUT from a previous task that had a different 
setting.
-    worker.channel.socket().setSoTimeout(
-      if (idleTimeoutSeconds > 0) idleTimeoutSeconds.toInt * 1000 else 0)
+    
worker.channel.socket().setSoTimeout(idleTimeoutToSoTimeoutMillis(idleTimeoutSeconds))

Review Comment:
   The new test covers only the pure conversion. No test checks that 
`createPipelinedDataIn` actually sets `SO_TIMEOUT` with this helper. If this 
line is reverted to `idleTimeoutSeconds.toInt * 1000`, `BasePythonRunnerSuite` 
still passes.
   
   Could we add a test that creates the pipelined input with a large idle 
timeout and checks `worker.channel.socket().getSoTimeout`? Then the original 
`IllegalArgumentException` cannot regress silently.



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -968,8 +986,7 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
     // Set socket read timeout for idle timeout detection in pipelined mode.
     // Always set explicitly (including 0 = no timeout) because reused workers 
may
     // retain a stale SO_TIMEOUT from a previous task that had a different 
setting.
-    worker.channel.socket().setSoTimeout(
-      if (idleTimeoutSeconds > 0) idleTimeoutSeconds.toInt * 1000 else 0)
+    
worker.channel.socket().setSoTimeout(idleTimeoutToSoTimeoutMillis(idleTimeoutSeconds))
 
     // Wrap the socket InputStream to handle idle timeout, matching sync mode 
behavior:
     // - Log warning on each timeout

Review Comment:
   When the cap applies, the warnings and the exception still report the 
configured value, not the actual wait.
   
   With `idleTimeoutSeconds=4294968`, the read times out after about 24.8 days, 
but the messages say `Idle timeout reached for Python worker (timeout: 4294968 
seconds)` 
([L694-L695](https://github.com/apache/spark/blob/d745bdb43cc58eb0d907c2e345f87b92c3828872/core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala#L694-L695)),
 `Terminating Python worker process due to idle timeout (timeout: 4294968 
seconds)` 
([L709-L710](https://github.com/apache/spark/blob/d745bdb43cc58eb0d907c2e345f87b92c3828872/core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala#L709-L710))
 and `Python worker process terminated due to idle timeout (timeout: 4294968 
seconds)` 
([L729-L730](https://github.com/apache/spark/blob/d745bdb43cc58eb0d907c2e345f87b92c3828872/core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala#L729-L730)).
 An operator would think that about 49.7 days have passed.
   
   This goes away if the configured value is honored or bounded (see L991). 
Otherwise, the messages should report the effective timeout.



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -605,6 +605,24 @@ private[spark] object BasePythonRunner extends Logging {
       .getOrElse(message)
   }
 
+  /**
+   * Converts the idle timeout of a Python worker, in seconds, to the value to 
pass to
+   * `Socket.setSoTimeout`, which takes an `Int` of milliseconds and treats 0 
as no timeout.
+   *
+   * A timeout that is zero or negative means no timeout and gives 0. A 
positive timeout is
+   * converted without overflow, and one that exceeds `Int.MaxValue` 
milliseconds (about 24.8
+   * days) is capped at `Int.MaxValue`, the longest read timeout a socket can 
have, so that a
+   * positive timeout never turns into a negative value, a shorter timeout or 
no timeout.

Review Comment:
   nit: The doc says the cap makes sure that a positive timeout never turns 
into a shorter timeout. But capping at `Int.MaxValue` makes every timeout above 
2147483 seconds exactly that, a shorter timeout. If we keep the cap, could we 
say so explicitly, e.g. `... is capped at Int.MaxValue, so such a timeout is 
shortened to about 24.8 days`?



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -605,6 +605,24 @@ private[spark] object BasePythonRunner extends Logging {
       .getOrElse(message)
   }
 
+  /**
+   * Converts the idle timeout of a Python worker, in seconds, to the value to 
pass to
+   * `Socket.setSoTimeout`, which takes an `Int` of milliseconds and treats 0 
as no timeout.
+   *
+   * A timeout that is zero or negative means no timeout and gives 0. A 
positive timeout is
+   * converted without overflow, and one that exceeds `Int.MaxValue` 
milliseconds (about 24.8
+   * days) is capped at `Int.MaxValue`, the longest read timeout a socket can 
have, so that a
+   * positive timeout never turns into a negative value, a shorter timeout or 
no timeout.
+   */
+  private[spark] def idleTimeoutToSoTimeoutMillis(idleTimeoutSeconds: Long): 
Int = {

Review Comment:
   nit: This helper has a single caller (L989) and seems to exist mainly for 
the test. That is fine by itself. But if the configured value is honored or 
bounded as suggested on L991, the cap and this helper may no longer be needed.



##########
core/src/test/scala/org/apache/spark/api/python/BasePythonRunnerSuite.scala:
##########
@@ -928,6 +928,31 @@ class BasePythonRunnerSuite extends SparkFunSuite with 
SharedSparkContext {
     }
   }
 
+  test("idle timeout in seconds to socket read timeout in milliseconds") {

Review Comment:
   nit: This test lands in the middle of the helpers for the 
`IdleTimeoutHandler` tests. `RecordingWorkerHandle` and `withIdleWorker` are 
above it, and `idleTimeoutLoggerName`, `idleTimeoutWarnings` and 
`nonBlockingWorkerStatus` are below it. It uses none of them, so could we move 
it before `RecordingWorkerHandle` (L887) or after the idle timeout tests at the 
end of the suite? Then the helpers stay together.



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -968,8 +986,7 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
     // Set socket read timeout for idle timeout detection in pipelined mode.
     // Always set explicitly (including 0 = no timeout) because reused workers 
may
     // retain a stale SO_TIMEOUT from a previous task that had a different 
setting.
-    worker.channel.socket().setSoTimeout(
-      if (idleTimeoutSeconds > 0) idleTimeoutSeconds.toInt * 1000 else 0)
+    
worker.channel.socket().setSoTimeout(idleTimeoutToSoTimeoutMillis(idleTimeoutSeconds))
 
     // Wrap the socket InputStream to handle idle timeout, matching sync mode 
behavior:

Review Comment:
   After this PR, pipelined mode no longer matches sync mode for timeouts above 
about 24.8 days.
   
   The sync path and `PythonPlannerRunner` use 
[`IdleTimeoutHandler.select`](https://github.com/apache/spark/blob/d745bdb43cc58eb0d907c2e345f87b92c3828872/core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala#L669-L679),
 which passes the `Long` to `Selector.select`. However, the JDK selectors 
(`EPollSelectorImpl`, `KQueueSelectorImpl`) cap the wait at `Integer.MAX_VALUE` 
ms. The select returns 0 after about 24.8 days, and the `(end - start) >= 
idleTimeoutMillis` guard skips it. Since `start` is reset on every call, the 
idle timeout never fires there.
   
   So with the same `30d` setting, sync mode never warns or kills, while 
pipelined mode kills at 24.8 days 
([`doRead`](https://github.com/apache/spark/blob/d745bdb43cc58eb0d907c2e345f87b92c3828872/core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala#L1002-L1016)).
 The PR description says the sync path and `PythonPlannerRunner` are not 
affected. They avoid the overflow, but they never time out for such values.
   
   Two ways to make both paths agree:
   1. Track the elapsed time. In `doRead`, on `SocketTimeoutException`, call 
`onIdleTimeout` only when the time since the read started is at least 
`TimeUnit.SECONDS.toMillis(idleTimeoutSeconds)`, and retry the read otherwise. 
`select` could track the elapsed time across calls in the same way. Then both 
paths honor the configured value.
   2. Bound `PYTHON_WORKER_IDLE_TIMEOUT_SECONDS` in its `checkValue` (e.g. at 
most `Int.MaxValue / 1000` seconds), so that such values are rejected in both 
paths. Then no cap is needed here.



##########
core/src/test/scala/org/apache/spark/api/python/BasePythonRunnerSuite.scala:
##########
@@ -928,6 +928,31 @@ class BasePythonRunnerSuite extends SparkFunSuite with 
SharedSparkContext {
     }
   }
 
+  test("idle timeout in seconds to socket read timeout in milliseconds") {
+    import BasePythonRunner.idleTimeoutToSoTimeoutMillis
+
+    // No timeout.
+    assert(idleTimeoutToSoTimeoutMillis(0L) === 0)
+    assert(idleTimeoutToSoTimeoutMillis(-1L) === 0)

Review Comment:
   nit: `PYTHON_WORKER_IDLE_TIMEOUT_SECONDS` has `checkValue(_ >= 0, ...)`, and 
`spark.sql.execution.pyspark.udf.idleTimeoutSeconds` falls back to it, so a 
negative value cannot reach this helper. The `-1L` and `Long.MinValue` cases 
here, and `zero or negative` in the Scaladoc, cover an input that cannot 
happen. Checking `0L` should be enough.



-- 
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]

Reply via email to