Yicong-Huang commented on code in PR #59976:
URL: https://github.com/apache/spark/pull/59976#discussion_r4519715374


##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -796,25 +817,27 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
     val dataIn = if (usePipelined) {
       createPipelinedDataIn(worker, writer, handle, context)
     } else {
-      new DataInputStream(new BufferedInputStream(
-        new ReaderInputStream(worker, writer, handle,
-          idleTimeoutSeconds, killOnIdleTimeout, context),
-        bufferSize))
+      val input = new ReaderInputStream(worker, writer, handle,
+        idleTimeoutSeconds, killOnIdleTimeout, context)
+      new PythonWorkerInputStream(
+        new BufferedInputStream(input, bufferSize), input.readInitResponse)
     }
     val stdoutIterator = newReaderIterator(
       dataIn, writer, startTime, env, worker, handle, releasedOrClosed, 
context)
     new InterruptibleIterator(context, stdoutIterator)
   }
 
   /**
-   * Sets up pipelined mode: switches the socket to blocking mode, starts the 
writer
-   * thread, configures idle timeout, and returns a DataInputStream for 
reading output.
+   * Sets up pipelined mode: switches the socket to blocking mode and 
configures idle timeout.
+   * Sends the command and reads the init response synchronously on the task 
thread.
+   * After successful init, starts the input writer thread and returns a 
DataInputStream
+   * for reading output.

Review Comment:
   can you separate the init request/response from this function? 



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -553,6 +553,27 @@ private[spark] object BasePythonRunner extends Logging {
     }
   }
 
+  private[python] case class InitResponse(
+      error: Option[PythonWorkerInitError],
+      receivedAtMs: Long)

Review Comment:
   I don't suggest we put this `receivedAtMs` in the response case class. it is 
not part of the response message. 



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -553,6 +553,27 @@ private[spark] object BasePythonRunner extends Logging {
     }
   }
 
+  private[python] case class InitResponse(
+      error: Option[PythonWorkerInitError],
+      receivedAtMs: Long)
+
+  private[python] def readInitResponseFrame(stream: DataInputStream): 
InitResponse = {
+    stream.readInt() match {
+      case SpecialLengths.START_OF_INIT_RESPONSE =>
+        val error = readInitResponse(stream)
+        InitResponse(error, System.currentTimeMillis())
+      case other =>
+        throw SparkException.internalError(
+          s"Expected the Python worker init response, but got $other")
+    }
+  }
+
+  private[python] class PythonWorkerInputStream(
+      input: InputStream,
+      receiveInit: DataInputStream => InitResponse) extends 
DataInputStream(input) {
+    lazy val initResponse: Try[InitResponse] = Try(receiveInit(this))

Review Comment:
   can we decouple init life cycle from the input stream? the original base is 
better: `DataInputStream` only handles data, no init in it.



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