ruanwenjun commented on code in PR #7827:
URL: https://github.com/apache/kyuubi/pull/7827#discussion_r4421835201
##########
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:
Thanks, that makes sense. It now extends `AbstractAppender` directly, and I
removed the unused writer and flush parameters.
##########
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:
Thanks for catching this. Formatting now happens only when an operation log
is present, and empty output is skipped. Added regression tests for both cases.
##########
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:
Updated both regressions to log through a `Logger` with the appender
attached. Each test uses a separate logger context. I also checked them against
the original implementation: they still catch the timeout and mixed operation
logs.
##########
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:
Removed both assertions from `finally`. Cleanup still waits for termination
without replacing the original failure. Checked this against the original
implementation and confirmed the timeout remains visible.
--
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]