FrankYang0529 commented on code in PR #69254:
URL: https://github.com/apache/airflow/pull/69254#discussion_r3651649804
##########
java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Comm.kt:
##########
@@ -138,4 +119,57 @@ class CoordinatorComm(
else -> throw ApiError("Unexpected response type
${response::class.java}")
}
}
+
+ override fun close() {
+ dispatcherScope.cancel()
+ }
+
+ private suspend fun readFrame(): IncomingFrame {
+ val prefix = reader.readByteArray(4) // First 4 bytes as length.
+ if (prefix.size != 4) {
+ throw ApiError("Coordinator socket closed while reading frame length")
+ }
+ val payloadLength = Frame.parseLengthPrefix(prefix)
+ val payload = reader.readByteArray(payloadLength)
+ if (payload.size != payloadLength) {
+ throw ApiError("Coordinator socket closed while reading frame payload")
+ }
+ val frame = decode(payload)
+ logger.debug("Received", mapOf("id" to frame.id))
+ return frame
+ }
+
+ private suspend fun readLoop() {
+ while (true) {
+ val frame =
+ try {
+ readFrame()
+ } catch (e: CancellationException) {
+ // Coroutine cancellation is delivered by throwing this exception,
and
+ // cooperative cancellation requires rethrowing it so it propagates.
+ throw e
+ } catch (e: Throwable) {
+ failAllWaiters(e)
+ return
+ }
+ val waiter = stateMutex.withLock { pending.remove(frame.id) }
+ if (waiter == null) {
+ logger.warning("Discarding response with no matching request",
mapOf("id" to frame.id))
+ continue
+ }
+ waiter.complete(frame)
+ }
+ }
+
+ private suspend fun failAllWaiters(cause: Throwable) {
+ val error = cause as? ApiError ?: ApiError("Coordinator comm closed:
${cause.message}")
Review Comment:
Thanks for the review. Add `initCause(cause)`.
--
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]