viirya commented on code in PR #60539:
URL: https://github.com/apache/spark/pull/60539#discussion_r4582908388


##########
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:
   Agreed. I took the second option from your comment on L991: the config is 
now limited to `Int.MaxValue / 1000` seconds, and the cap is gone.



##########
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:
   Thanks, you are right. I confirmed `Math.min(timeout, Integer.MAX_VALUE)` in 
`KQueueSelectorImpl.doSelect`, so `select` returns early and the elapsed-time 
check skips it. I bounded the config in `checkValue`, which also covers 
`spark.sql.execution.pyspark.udf.idleTimeoutSeconds` since it falls back to the 
same converter. I preferred this to tracking the elapsed time because it keeps 
the read loops unchanged. I also corrected the PR description.



##########
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:
   With the config bounded, the effective timeout always equals the configured 
one, so the messages are accurate again.



##########
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:
   With the bound, every accepted value fits after multiplying by 1000, so this 
line can no longer overflow. I also switched it to `Math.toIntExact`, so it 
fails instead of wrapping if the bound is ever relaxed. The tests now check 
that both configs reject larger values.



##########
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:
   The helper and its Scaladoc are removed.



##########
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:
   Replaced by a config test at the end of the suite.



##########
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:
   Removed together with the helper.



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