FrankYang0529 commented on code in PR #69254:
URL: https://github.com/apache/airflow/pull/69254#discussion_r3518354682


##########
java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Comm.kt:
##########
@@ -57,37 +61,19 @@ class CoordinatorComm(
   }
 
   private val nextId = AtomicInt(0)
-  private var shutDownRequested = false
-  private val commMutex = Mutex()
+  private val writeMutex = Mutex()
+  private val stateMutex = Mutex()
+  private val pending = mutableMapOf<Int, CompletableDeferred<IncomingFrame>>()
+  private var dispatcherStarted = false
+  private var readError: ApiError? = null
 
-  suspend fun startProcessing() {
-    while (!shutDownRequested) {
-      processOnce(::handleIncoming)
-    }
-    logger.debug("Goodbye")
-  }
-
-  private suspend fun processOnce(handle: suspend (IncomingFrame) -> Unit) {
-    val prefix = reader.readByteArray(4) // First 4 bytes as length.
-    if (prefix.size != 4) { // Something is terribly wrong. Let's bail.
-      logger.error("Need 4 prefix bytes", mapOf("actual" to prefix.size))
-      shutDownRequested = true
-      return
-    }
+  private val dispatcherScope = CoroutineScope(Dispatchers.IO + 
SupervisorJob())
 
-    val payloadLength = Frame.parseLengthPrefix(prefix)
-    val payload = reader.readByteArray(payloadLength)
-    if (payload.size != payloadLength) { // Something is terribly wrong. Let's 
bail.
-      logger.error(
-        "Payload length not right",
-        mapOf("expect" to payloadLength, "receive" to payload.size),
-      )
-      shutDownRequested = true
-      return
+  suspend fun readMessage(): IncomingFrame {
+    stateMutex.withLock {
+      check(!dispatcherStarted) { "readMessage cannot be used after the 
dispatcher has started" }
     }
-    val frame = decode(payload)
-    logger.debug("Handling", mapOf("id" to frame.id))
-    handle(frame)
+    return readFrame()
   }

Review Comment:
   The `readMessage` is only used in Server#dispatchTask currently. It doesn't 
run in parallel at that moment. However, I agree it's better to move 
`readFrame` into `stateMutex`. If we reuse this function in other places, it 
will be safer. Thanks for catching this.



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

Reply via email to