FrankYang0529 commented on code in PR #75538:
URL: https://github.com/apache/airflow/pull/75538#discussion_r4572739359
##########
java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Logger.kt:
##########
@@ -216,48 +229,182 @@ internal class Logger(
arguments: Map<String, Any>,
) {
if (!Log.isEnabledForLevel(level, name)) return
- LogSender.send(LogMessage(event, arguments, name, level))
+ LogSender.send(LogMessage(event, arguments, name, level), waitsForRoom)
}
}
+// This object's lock keeps send, configure and detachAndFlush from losing or
reordering lines.
internal object LogSender {
+ internal const val PENDING_CHARS = 8 * 1024 * 1024
+
private var writer: ByteWriteChannel? = null
+
+ // Kept after detachAndFlush clears writer, so a later call can cancel the
channel an earlier one is stuck on.
+ private var configured: ByteWriteChannel? = null
internal val messages: ConcurrentLinkedDeque<LogMessage> =
ConcurrentLinkedDeque()
+ // Only the sender thread writes, so a caller's runBlocking cannot run the
task under this object's lock.
+ private val pending = LinkedBlockingQueue<() -> Unit>()
+
+ // One permit per queued character. Fair, so short lines cannot keep taking
room a long line waits for.
+ private val room = Semaphore(PENDING_CHARS, true)
+
+ // Not System.err, which a logging bridge can route back here and whose lock
a waiting caller can hold.
+ @Volatile
+ internal var errors = PrintStream(FileOutputStream(FileDescriptor.err), true)
+
+ private var failedChannel: ByteWriteChannel? = null
+
+ // Printing a report when a line fails could stall the lines already queued,
so wait for an empty queue or a flush.
+ private val reports = ArrayList<String>()
+
+ @Volatile
+ private var finishedAt = System.nanoTime()
+
+ private val sender =
+ thread(name = "Airflow log sender", isDaemon = true) {
+ while (true) {
+ try {
+ val task =
+ pending.poll() ?: run {
+ printReports()
+ pending.take()
+ }
+ task.invoke()
+ } catch (e: Throwable) {
+ // If the sender thread ended, flush would wait forever, and so
would every caller that waits for room.
+ }
+ }
+ }
+
+ @Synchronized
fun configure(channel: ByteWriteChannel) {
writer = channel
+ configured = channel
if (!channel.isClosedForWrite) {
while (messages.isNotEmpty()) {
- sendTo(channel, messages.removeFirst())
+ // Don't wait for room: these messages are already in memory, and
waiting would block the serving thread.
+ queueLine(channel, buildLine(messages.removeFirst()), chars = 0)
}
}
}
- fun send(message: LogMessage) {
- val channel = writer
- if (channel == null || channel.isClosedForWrite) {
- messages.addLast(message)
- } else {
- sendTo(channel, message)
+ fun send(
+ message: LogMessage,
+ waitForRoom: Boolean = true,
+ ) {
+ synchronized(this) {
+ if (!isQueuing()) {
+ messages.addLast(message)
+ return
+ }
+ }
+ // Build the line now, so that it shows the arguments as they were when
the caller logged them.
+ val line = buildLine(message)
+ // Only the sender thread gives back room, so it never waits. A line over
the limit asks for all the room.
+ val chars = if (waitForRoom && Thread.currentThread() !== sender)
minOf(line.length, PENDING_CHARS) else 0
+ // Wait before locking, or a logging sender thread can deadlock. A fair
semaphore makes acquire(0) wait too.
+ if (chars > 0) room.acquireUninterruptibly(chars)
+ synchronized(this) {
+ val channel = writer
+ if (channel == null || channel.isClosedForWrite) {
+ // Queuing stopped while this caller waited for room.
+ messages.addLast(message)
+ room.release(chars)
+ } else {
+ queueLine(channel, line, chars)
+ }
}
}
Review Comment:
Updated it. Thanks for the suggestion.
--
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]