pan3793 commented on code in PR #7827:
URL: https://github.com/apache/kyuubi/pull/7827#discussion_r4421244540
##########
kyuubi-common/src/main/scala/org/apache/kyuubi/operation/log/Log4j2DivertAppender.scala:
##########
@@ -63,24 +60,10 @@ class Log4j2DivertAppender(
}
})
- private val writeLock =
- getField[ReadWriteLock]((classOf[AbstractWriterAppender[_]], this),
"readWriteLock").writeLock
-
- /**
- * Overrides AbstractWriterAppender.append(), which does the real logging.
No need
- * to worry about concurrency since log4j calls this synchronously.
- */
override def append(event: LogEvent): Unit = {
Review Comment:
Nothing writes to the `CharArrayWriter`/`WriterManager` any more, and the
`writer` and `immediateFlush` parameters are unused. Could this extend
`AbstractAppender` directly?
##########
kyuubi-common/src/main/scala/org/apache/kyuubi/operation/log/Log4j2DivertAppender.scala:
##########
@@ -63,24 +60,10 @@ class Log4j2DivertAppender(
}
})
- private val writeLock =
- getField[ReadWriteLock]((classOf[AbstractWriterAppender[_]], this),
"readWriteLock").writeLock
-
- /**
- * Overrides AbstractWriterAppender.append(), which does the real logging.
No need
- * to worry about concurrency since log4j calls this synchronously.
- */
override def append(event: LogEvent): Unit = {
- super.append(event)
- writeLock.lock()
- try {
- // That should've gone into our writer. Notify the LogContext.
- val logOutput = writer.toString
- writer.reset()
- OperationLog.getCurrentOperationLog.foreach(_.write(logOutput))
- } finally {
- writeLock.unlock()
- }
+ // Formatting can wait for another logging thread. Do not hold an appender
lock here.
+ val logOutput = getStringLayout.toSerializable(event)
+ OperationLog.getCurrentOperationLog.foreach(_.write(logOutput))
Review Comment:
`AbstractWriterAppender.append` skipped empty output, and this check is
gone. Also, the event is formatted even when there is no current operation log.
Suggest:
```scala
OperationLog.getCurrentOperationLog.foreach { log =>
val message = getStringLayout.toSerializable(event)
if (message.nonEmpty) log.write(message)
}
```
##########
kyuubi-common/src/test/scala/org/apache/kyuubi/operation/log/OperationLogSuite.scala:
##########
@@ -126,6 +131,107 @@ class OperationLogSuite extends KyuubiFunSuite {
operationLog.close()
}
+ test("log4j2 divert appender allows formatting to wait for another logging
thread") {
+ val directory = Utils.createTempDir()
+ val mainPath = directory.resolve("main.log")
+ val workerPath = directory.resolve("worker.log")
+ val mainLog = new OperationLog(mainPath)
+ val workerLog = new OperationLog(workerPath)
+ val executor = Executors.newSingleThreadExecutor()
+ val appender = new Log4j2DivertAppender(
+ "formatting-test",
+ PatternLayout.newBuilder().withPattern("%m%n").build(),
+ null,
+ false,
+ true,
+ new CharArrayWriter())
+ val message = new Message {
+ override def getFormattedMessage: String = {
+ executor.submit(new Callable[String] {
+ override def call(): String = {
+ OperationLog.setCurrentOperationLog(workerLog)
+ try {
+ appender.append(Log4jLogEvent.newBuilder()
+ .setMessage(new SimpleMessage("worker message")).build())
+ "main message"
+ } finally {
+ OperationLog.removeCurrentOperationLog()
+ }
+ }
+ }).get(10, TimeUnit.SECONDS)
+ }
+
+ override def getFormat: String = "main message"
+ override def getParameters: Array[AnyRef] = Array.empty
+ override def getThrowable: Throwable = null
+ }
+
+ try {
+ appender.start()
+ OperationLog.setCurrentOperationLog(mainLog)
+ appender.append(Log4jLogEvent.newBuilder().setMessage(message).build())
Review Comment:
These tests call `appender.append` directly, so the filter and log4j's
normal logging path are never exercised. Logging through a `Logger` with the
appender attached would cover that path.
##########
kyuubi-common/src/test/scala/org/apache/kyuubi/operation/log/OperationLogSuite.scala:
##########
@@ -126,6 +131,107 @@ class OperationLogSuite extends KyuubiFunSuite {
operationLog.close()
}
+ test("log4j2 divert appender allows formatting to wait for another logging
thread") {
+ val directory = Utils.createTempDir()
+ val mainPath = directory.resolve("main.log")
+ val workerPath = directory.resolve("worker.log")
+ val mainLog = new OperationLog(mainPath)
+ val workerLog = new OperationLog(workerPath)
+ val executor = Executors.newSingleThreadExecutor()
+ val appender = new Log4j2DivertAppender(
+ "formatting-test",
+ PatternLayout.newBuilder().withPattern("%m%n").build(),
+ null,
+ false,
+ true,
+ new CharArrayWriter())
+ val message = new Message {
+ override def getFormattedMessage: String = {
+ executor.submit(new Callable[String] {
+ override def call(): String = {
+ OperationLog.setCurrentOperationLog(workerLog)
+ try {
+ appender.append(Log4jLogEvent.newBuilder()
+ .setMessage(new SimpleMessage("worker message")).build())
+ "main message"
+ } finally {
+ OperationLog.removeCurrentOperationLog()
+ }
+ }
+ }).get(10, TimeUnit.SECONDS)
+ }
+
+ override def getFormat: String = "main message"
+ override def getParameters: Array[AnyRef] = Array.empty
+ override def getThrowable: Throwable = null
+ }
+
+ try {
+ appender.start()
+ OperationLog.setCurrentOperationLog(mainLog)
+ appender.append(Log4jLogEvent.newBuilder().setMessage(message).build())
+ assert(Files.readAllLines(mainPath, StandardCharsets.UTF_8).asScala ===
Seq("main message"))
+ assert(Files.readAllLines(
+ workerPath,
+ StandardCharsets.UTF_8).asScala === Seq("worker message"))
+ } finally {
+ OperationLog.removeCurrentOperationLog()
+ executor.shutdownNow()
+ assert(executor.awaitTermination(10, TimeUnit.SECONDS))
Review Comment:
If the deadlock comes back, the worker thread can't be interrupted. The
`assert(awaitTermination(...))` in `finally` then fails and hides the original
`TimeoutException`. Suggest not asserting in `finally`. Same at L228.
--
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]