Skip to content

Commit d07d7cc

Browse files
committed
[KYUUBI #3143] Check class loadable before applying SLF4JBridgeHandler
### _Why are the changes needed?_ To fix the NoClassDefFoundError ``` Error: org.apache.kyuubi.KyuubiSQLException: org.apache.kyuubi.KyuubiSQLException: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/bridge/SLF4JBridgeHandler at org.apache.kyuubi.Logging.initializeLogging(Logging.scala:97) at org.apache.kyuubi.Logging.initializeLoggerIfNecessary(Logging.scala:89) at org.apache.kyuubi.Logging.initializeLoggerIfNecessary$(Logging.scala:85) at org.apache.kyuubi.engine.flink.FlinkSQLEngine$.initializeLoggerIfNecessary(FlinkSQLEngine.scala:62) at org.apache.kyuubi.Logging.logger(Logging.scala:43) at org.apache.kyuubi.Logging.logger$(Logging.scala:41) at org.apache.kyuubi.engine.flink.FlinkSQLEngine$.logger(FlinkSQLEngine.scala:62) at org.apache.kyuubi.engine.flink.FlinkSQLEngine$.main(FlinkSQLEngine.scala:72) at org.apache.kyuubi.engine.flink.FlinkSQLEngine.main(FlinkSQLEngine.scala) Caused by: java.lang.ClassNotFoundException: org.slf4j.bridge.SLF4JBridgeHandler at java.net.URLClassLoader.findClass(URLClassLoader.java:387) at java.lang.ClassLoader.loadClass(ClassLoader.java:419) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) at java.lang.ClassLoader.loadClass(ClassLoader.java:352) See more: /Users/chengpan/app/kyuubi/work/anonymous/kyuubi-flink-sql-engine.log.0 at org.apache.kyuubi.KyuubiSQLException$.apply(KyuubiSQLException.scala:69) at org.apache.kyuubi.engine.ProcBuilder.$anonfun$start$1(ProcBuilder.scala:219) at java.lang.Thread.run(Thread.java:750) ``` ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #3143 from pan3793/bridge. Closes #3143 266c9bd [Cheng Pan] Check class loadable before applying SLF4JBridgeHandler Authored-by: Cheng Pan <chengpan@apache.org> Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent 7b15f2e commit d07d7cc

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

kyuubi-common/src/main/scala/org/apache/kyuubi/Logging.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import org.slf4j.{Logger, LoggerFactory}
2424
import org.slf4j.bridge.SLF4JBridgeHandler
2525
import org.slf4j.impl.StaticLoggerBinder
2626

27+
import org.apache.kyuubi.util.ClassUtils
28+
2729
/**
2830
* Simple version of logging adopted from Apache Spark.
2931
*/
@@ -93,9 +95,11 @@ trait Logging {
9395
}
9496

9597
private def initializeLogging(isInterpreter: Boolean): Unit = {
96-
// Handles configuring the JUL -> SLF4J bridge
97-
SLF4JBridgeHandler.removeHandlersForRootLogger()
98-
SLF4JBridgeHandler.install()
98+
if (ClassUtils.classIsLoadable("org.slf4j.bridge.SLF4JBridgeHandler")) {
99+
// Handles configuring the JUL -> SLF4J bridge
100+
SLF4JBridgeHandler.removeHandlersForRootLogger()
101+
SLF4JBridgeHandler.install()
102+
}
99103

100104
if (Logging.isLog4j2) {
101105
// scalastyle:off println

kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.kyuubi.util
1919

20+
import scala.util.Try
21+
2022
import org.apache.kyuubi.KyuubiException
2123
import org.apache.kyuubi.config.KyuubiConf
2224

@@ -49,4 +51,11 @@ object ClassUtils {
4951
s"$className must extend of ${expected.getName}")
5052
}
5153
}
54+
55+
/** Determines whether the provided class is loadable. */
56+
def classIsLoadable(
57+
clazz: String,
58+
cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean = {
59+
Try { Class.forName(clazz, false, cl) }.isSuccess
60+
}
5261
}

0 commit comments

Comments
 (0)