You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ho...@apache.org on 2022/05/12 09:09:39 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #2601] Add a config to support different service discovery client class implementation

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

hongdd 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 50584f2a8 [KYUUBI #2601] Add a config to support different service discovery client class implementation
50584f2a8 is described below

commit 50584f2a81d7d97e80fbd81f8c6d428842341bec
Author: BoYang <da...@gmail.com>
AuthorDate: Thu May 12 17:09:27 2022 +0800

    [KYUUBI #2601] Add a config to support different service discovery client class implementation
    
    ### _Why are the changes needed?_
    We want to implement different service discovery client, thus add a config to support different service discovery client class implementation.
    
    ### _How was this patch tested?_
    - [x] 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 #2601 from hiboyang/datapunch-DiscoveryClient-Configurable.
    
    Closes #2601
    
    719685f1 [BoYang] Update settings.md
    8af4f5da [BoYang] Change name to HA_DISCOVERY_CLIENT_CLASS
    22139b6d [BoYang] Update settings.md
    c9845cee [BoYang] Revert unrelated changes
    1713e050 [BoYang] Add .github files
    22222a4d [BoYang] Merge branch 'master' into datapunch-DiscoveryClient-Configurable
    c0c293db [BoYang] Merge branch 'master' of https://github.com/apache/incubator-kyuubi
    3e53ef48 [BoYang] Do not copy spark files in docker
    887be439 [BoYang] Add dot after docker build command
    48b70053 [BoYang] Update manual-release.yml
    39d0bb21 [BoYang] Update workflow: cd dist
    9adae8c1 [BoYang] Update manual-release.yml
    da76f568 [BoYang] Use nonEmpty
    bf8fe2ff [BoYang] Run mvn spotless:apply
    188d71c5 [BoYang] Update per comments
    fffa3bfe [BoYang] Create manual-release.yml
    65c62c31 [BoYang] Add KyuubiConf.createInstance and reuse some class checking code
    30f0ff35 [BoYang] Add a config to support different service discovery client class implementation
    1cdf8809 [BoYang] Delete github workflows
    
    Authored-by: BoYang <da...@gmail.com>
    Signed-off-by: hongdongdong <ho...@cmss.chinamobile.com>
---
 docs/deployment/settings.md                        |  1 +
 .../AuthenticationProviderFactory.scala            | 12 ++------
 .../scala/org/apache/kyuubi/util/ClassUtils.scala  | 36 ++++++++++------------
 .../apache/kyuubi/ha/HighAvailabilityConf.scala    |  8 +++++
 .../kyuubi/ha/client/DiscoveryClientProvider.scala | 22 +++++++++++--
 .../apache/kyuubi/ha/client/ServiceDiscovery.scala |  3 +-
 6 files changed, 48 insertions(+), 34 deletions(-)

diff --git a/docs/deployment/settings.md b/docs/deployment/settings.md
index 56e7d3a72..c07d0a050 100644
--- a/docs/deployment/settings.md
+++ b/docs/deployment/settings.md
@@ -280,6 +280,7 @@ Key | Default | Meaning | Type | Since
 
 Key | Default | Meaning | Type | Since
 --- | --- | --- | --- | ---
+<code>kyuubi.ha.service.discovery.client.class</code>|<div style='width: 65pt;word-wrap: break-word;white-space: normal'>org.apache.kyuubi.ha.client.zookeeper.ZookeeperDiscoveryClient</div>|<div style='width: 170pt;word-wrap: break-word;white-space: normal'>Class name for service discovery client.</div>|<div style='width: 30pt'>string</div>|<div style='width: 20pt'>1.6.0</div>
 <code>kyuubi.ha.zookeeper.acl.enabled</code>|<div style='width: 65pt;word-wrap: break-word;white-space: normal'>false</div>|<div style='width: 170pt;word-wrap: break-word;white-space: normal'>Set to true if the zookeeper ensemble is kerberized</div>|<div style='width: 30pt'>boolean</div>|<div style='width: 20pt'>1.0.0</div>
 <code>kyuubi.ha.zookeeper.auth.digest</code>|<div style='width: 65pt;word-wrap: break-word;white-space: normal'>&lt;undefined&gt;</div>|<div style='width: 170pt;word-wrap: break-word;white-space: normal'>The digest auth string is used for zookeeper authentication, like: username:password.</div>|<div style='width: 30pt'>string</div>|<div style='width: 20pt'>1.3.2</div>
 <code>kyuubi.ha.zookeeper.auth.keytab</code>|<div style='width: 65pt;word-wrap: break-word;white-space: normal'>&lt;undefined&gt;</div>|<div style='width: 170pt;word-wrap: break-word;white-space: normal'>Location of Kyuubi server's keytab is used for zookeeper authentication.</div>|<div style='width: 30pt'>string</div>|<div style='width: 20pt'>1.3.2</div>
diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/AuthenticationProviderFactory.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/AuthenticationProviderFactory.scala
index 1361f5281..ac4e8e58e 100644
--- a/kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/AuthenticationProviderFactory.scala
+++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/AuthenticationProviderFactory.scala
@@ -21,6 +21,7 @@ import javax.security.sasl.AuthenticationException
 
 import org.apache.kyuubi.config.KyuubiConf
 import org.apache.kyuubi.service.authentication.AuthMethods.AuthMethod
+import org.apache.kyuubi.util.ClassUtils
 
 /**
  * This class helps select a [[PasswdAuthenticationProvider]] for a given [[AuthMethods]]
@@ -53,16 +54,7 @@ object AuthenticationProviderFactory {
       val cls = Class.forName(className.get, true, classLoader)
       cls match {
         case c if classOf[PasswdAuthenticationProvider].isAssignableFrom(cls) =>
-          val confConstructor = c.getConstructors.exists(p => {
-            val params = p.getParameterTypes
-            params.length == 1 && classOf[KyuubiConf].isAssignableFrom(params(0))
-          })
-          if (confConstructor) {
-            c.getConstructor(classOf[KyuubiConf]).newInstance(conf)
-              .asInstanceOf[PasswdAuthenticationProvider]
-          } else {
-            c.newInstance().asInstanceOf[PasswdAuthenticationProvider]
-          }
+          ClassUtils.createInstance[PasswdAuthenticationProvider](c, conf)
         case _ => throw new AuthenticationException(
             s"$className must extend of PasswdAuthenticationProvider.")
       }
diff --git a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/DiscoveryClientProvider.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala
similarity index 56%
copy from kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/DiscoveryClientProvider.scala
copy to kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala
index be908e2fc..a251b1aec 100644
--- a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/DiscoveryClientProvider.scala
+++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/util/ClassUtils.scala
@@ -15,31 +15,29 @@
  * limitations under the License.
  */
 
-package org.apache.kyuubi.ha.client
+package org.apache.kyuubi.util
 
-import java.io.IOException
-
-import org.apache.kyuubi.Logging
 import org.apache.kyuubi.config.KyuubiConf
-import org.apache.kyuubi.ha.client.zookeeper.ZookeeperDiscoveryClient
 
-object DiscoveryClientProvider extends Logging {
+object ClassUtils {
 
   /**
-   * Creates a zookeeper client before calling `f` and close it after calling `f`.
+   * Create an object instance with given [[KyuubiConf]].
+   * @param clazz object class
+   * @param conf configuration ([[KyuubiConf]])
+   * @tparam T object instance type to create
+   * @return
    */
-  def withDiscoveryClient[T](conf: KyuubiConf)(f: DiscoveryClient => T): T = {
-    val discoveryClient = new ZookeeperDiscoveryClient(conf)
-    try {
-      discoveryClient.createClient()
-      f(discoveryClient)
-    } finally {
-      try {
-        discoveryClient.closeClient()
-      } catch {
-        case e: IOException => error("Failed to release the zkClient", e)
-      }
+  def createInstance[T](clazz: Class[_], conf: KyuubiConf): T = {
+    val confConstructor = clazz.getConstructors.exists(p => {
+      val params = p.getParameterTypes
+      params.length == 1 && classOf[KyuubiConf].isAssignableFrom(params(0))
+    })
+    if (confConstructor) {
+      clazz.getConstructor(classOf[KyuubiConf]).newInstance(conf)
+        .asInstanceOf[T]
+    } else {
+      clazz.newInstance().asInstanceOf[T]
     }
   }
-
 }
diff --git a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/HighAvailabilityConf.scala b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/HighAvailabilityConf.scala
index c6bd587b3..151f9a823 100644
--- a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/HighAvailabilityConf.scala
+++ b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/HighAvailabilityConf.scala
@@ -158,4 +158,12 @@ object HighAvailabilityConf {
       .version("1.5.0")
       .stringConf
       .createOptional
+
+  val HA_DISCOVERY_CLIENT_CLASS: ConfigEntry[String] =
+    buildConf("kyuubi.ha.service.discovery.client.class")
+      .doc("Class name for service discovery client.")
+      .version("1.6.0")
+      .stringConf
+      .checkValue(_.nonEmpty, "must not be empty")
+      .createWithDefault("org.apache.kyuubi.ha.client.zookeeper.ZookeeperDiscoveryClient")
 }
diff --git a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/DiscoveryClientProvider.scala b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/DiscoveryClientProvider.scala
index be908e2fc..54a96d5fc 100644
--- a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/DiscoveryClientProvider.scala
+++ b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/DiscoveryClientProvider.scala
@@ -19,9 +19,10 @@ package org.apache.kyuubi.ha.client
 
 import java.io.IOException
 
-import org.apache.kyuubi.Logging
+import org.apache.kyuubi.{KyuubiException, Logging}
 import org.apache.kyuubi.config.KyuubiConf
-import org.apache.kyuubi.ha.client.zookeeper.ZookeeperDiscoveryClient
+import org.apache.kyuubi.ha.HighAvailabilityConf
+import org.apache.kyuubi.util.ClassUtils
 
 object DiscoveryClientProvider extends Logging {
 
@@ -29,7 +30,7 @@ object DiscoveryClientProvider extends Logging {
    * Creates a zookeeper client before calling `f` and close it after calling `f`.
    */
   def withDiscoveryClient[T](conf: KyuubiConf)(f: DiscoveryClient => T): T = {
-    val discoveryClient = new ZookeeperDiscoveryClient(conf)
+    val discoveryClient = createDiscoveryClient(conf)
     try {
       discoveryClient.createClient()
       f(discoveryClient)
@@ -42,4 +43,19 @@ object DiscoveryClientProvider extends Logging {
     }
   }
 
+  def createDiscoveryClient(conf: KyuubiConf): DiscoveryClient = {
+    val classLoader = Thread.currentThread.getContextClassLoader
+    val className = conf.get(HighAvailabilityConf.HA_DISCOVERY_CLIENT_CLASS)
+    if (className.isEmpty) {
+      throw new KyuubiException(
+        s"${HighAvailabilityConf.HA_DISCOVERY_CLIENT_CLASS.key} cannot be empty.")
+    }
+    val cls = Class.forName(className, true, classLoader)
+    cls match {
+      case c if classOf[DiscoveryClient].isAssignableFrom(cls) =>
+        ClassUtils.createInstance[DiscoveryClient](c, conf)
+      case _ => throw new KyuubiException(
+          s"$className must extend of ${DiscoveryClient.getClass.getName}")
+    }
+  }
 }
diff --git a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/ServiceDiscovery.scala b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/ServiceDiscovery.scala
index c65a4aa91..5dc50dc1f 100644
--- a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/ServiceDiscovery.scala
+++ b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/ServiceDiscovery.scala
@@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicBoolean
 import org.apache.kyuubi.Logging
 import org.apache.kyuubi.config.KyuubiConf
 import org.apache.kyuubi.ha.HighAvailabilityConf._
-import org.apache.kyuubi.ha.client.zookeeper.ZookeeperDiscoveryClient
 import org.apache.kyuubi.service.{AbstractService, FrontendService}
 
 /**
@@ -50,7 +49,7 @@ abstract class ServiceDiscovery(
     this.conf = conf
 
     _namespace = conf.get(HA_ZK_NAMESPACE)
-    _discoveryClient = new ZookeeperDiscoveryClient(conf)
+    _discoveryClient = DiscoveryClientProvider.createDiscoveryClient(conf)
     discoveryClient.monitorState(this)
     discoveryClient.createClient()