You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ch...@apache.org on 2022/07/26 09:11:06 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #3143] Check class loadable before applying SLF4JBridgeHandler

This is an automated email from the ASF dual-hosted git repository.

chengpan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new d07d7cc25 [KYUUBI #3143] Check class loadable before applying SLF4JBridgeHandler
d07d7cc25 is described below

commit d07d7cc25bbae78484ab972f5403da885633b248
Author: Cheng Pan <ch...@apache.org>
AuthorDate: Tue Jul 26 17:10:56 2022 +0800

    [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
    
    266c9bd1 [Cheng Pan] Check class loadable before applying SLF4JBridgeHandler
    
    Authored-by: Cheng Pan <ch...@apache.org>
    Signed-off-by: Cheng Pan <ch...@apache.org>
---
 kyuubi-common/src/main/scala/org/apache/kyuubi/Logging.scala   | 10 +++++++---
 .../src/main/scala/org/apache/kyuubi/util/ClassUtils.scala     |  9 +++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/Logging.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/Logging.scala
index 30439c0c7..dd176e115 100644
--- a/kyuubi-common/src/main/scala/org/apache/kyuubi/Logging.scala
+++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/Logging.scala
@@ -24,6 +24,8 @@ import org.slf4j.{Logger, LoggerFactory}
 import org.slf4j.bridge.SLF4JBridgeHandler
 import org.slf4j.impl.StaticLoggerBinder
 
+import org.apache.kyuubi.util.ClassUtils
+
 /**
  * Simple version of logging adopted from Apache Spark.
  */
@@ -93,9 +95,11 @@ trait Logging {
   }
 
   private def initializeLogging(isInterpreter: Boolean): Unit = {
-    // Handles configuring the JUL -> SLF4J bridge
-    SLF4JBridgeHandler.removeHandlersForRootLogger()
-    SLF4JBridgeHandler.install()
+    if (ClassUtils.classIsLoadable("org.slf4j.bridge.SLF4JBridgeHandler")) {
+      // Handles configuring the JUL -> SLF4J bridge
+      SLF4JBridgeHandler.removeHandlersForRootLogger()
+      SLF4JBridgeHandler.install()
+    }
 
     if (Logging.isLog4j2) {
       // scalastyle:off println
diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala
index 1523f69e8..bcbfdabfb 100644
--- a/kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala
+++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala
@@ -17,6 +17,8 @@
 
 package org.apache.kyuubi.util
 
+import scala.util.Try
+
 import org.apache.kyuubi.KyuubiException
 import org.apache.kyuubi.config.KyuubiConf
 
@@ -49,4 +51,11 @@ object ClassUtils {
           s"$className must extend of ${expected.getName}")
     }
   }
+
+  /** Determines whether the provided class is loadable. */
+  def classIsLoadable(
+      clazz: String,
+      cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean = {
+    Try { Class.forName(clazz, false, cl) }.isSuccess
+  }
 }