You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ul...@apache.org on 2022/08/24 01:40:17 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #3089] [FEATURE] Support to load custom event handlers by ServiceLoader

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

ulyssesyou 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 fae9883ca [KYUUBI #3089] [FEATURE] Support to load custom event handlers by ServiceLoader
fae9883ca is described below

commit fae9883ca3acdada0a28621828a7d7e9a17da9a3
Author: Min <zh...@163.com>
AuthorDate: Wed Aug 24 09:40:05 2022 +0800

    [KYUUBI #3089] [FEATURE] Support to load custom event handlers by ServiceLoader
    
    ### _Why are the changes needed?_
    
    close https://github.com/apache/incubator-kyuubi/issues/3089#issue-1306657936
    ### _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
    
    - [ ] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #3261 from zhaomin1423/custom_event.
    
    Closes #3089
    
    a329f4d9 [Min] fix test
    165834be [Min] add comment
    e216022d [Min] catch exception when provider create event handler
    3d452bc2 [Min] fix review
    4e7c4b4b [Min] [KYUUBI #3089] [FEATURE] Support to load custom event handlers by ServiceLoader
    
    Authored-by: Min <zh...@163.com>
    Signed-off-by: ulysses-you <ul...@apache.org>
---
 .../kyuubi/events/EventHandlerRegister.scala       | 16 +++---
 .../handler/CustomEventHandlerProvider.scala       | 36 +++++++++++++
 .../kyuubi/events/handler/EventHandlerLoader.scala | 55 +++++++++++++++++++
 ...yuubi.events.handler.CustomEventHandlerProvider | 20 +++++++
 .../kyuubi/events/CustomEventHandlerSuite.scala    | 62 ++++++++++++++++++++++
 5 files changed, 183 insertions(+), 6 deletions(-)

diff --git a/kyuubi-events/src/main/scala/org/apache/kyuubi/events/EventHandlerRegister.scala b/kyuubi-events/src/main/scala/org/apache/kyuubi/events/EventHandlerRegister.scala
index 64ce9d0f3..f16a48964 100644
--- a/kyuubi-events/src/main/scala/org/apache/kyuubi/events/EventHandlerRegister.scala
+++ b/kyuubi-events/src/main/scala/org/apache/kyuubi/events/EventHandlerRegister.scala
@@ -20,7 +20,7 @@ import org.apache.kyuubi.{KyuubiException, Logging}
 import org.apache.kyuubi.config.KyuubiConf
 import org.apache.kyuubi.config.KyuubiConf.{ENGINE_EVENT_LOGGERS, SERVER_EVENT_LOGGERS}
 import org.apache.kyuubi.events.EventLoggerType.EventLoggerType
-import org.apache.kyuubi.events.handler.EventHandler
+import org.apache.kyuubi.events.handler.{EventHandler, EventHandlerLoader}
 
 trait EventHandlerRegister extends Logging {
 
@@ -38,7 +38,8 @@ trait EventHandlerRegister extends Logging {
     loggers
       .map(EventLoggerType.withName)
       .foreach { logger =>
-        EventBus.register(loadEventHandler(logger, conf))
+        val handlers = loadEventHandler(logger, conf)
+        handlers.foreach(EventBus.register)
       }
   }
 
@@ -56,16 +57,19 @@ trait EventHandlerRegister extends Logging {
 
   private def loadEventHandler(
       eventLoggerType: EventLoggerType,
-      kyuubiConf: KyuubiConf): EventHandler[KyuubiEvent] = {
+      kyuubiConf: KyuubiConf): Seq[EventHandler[KyuubiEvent]] = {
     eventLoggerType match {
       case EventLoggerType.SPARK =>
-        createSparkEventHandler(kyuubiConf)
+        createSparkEventHandler(kyuubiConf) :: Nil
 
       case EventLoggerType.JSON =>
-        createJsonEventHandler(kyuubiConf)
+        createJsonEventHandler(kyuubiConf) :: Nil
 
       case EventLoggerType.JDBC =>
-        createJdbcEventHandler(kyuubiConf)
+        createJdbcEventHandler(kyuubiConf) :: Nil
+
+      case EventLoggerType.CUSTOM =>
+        EventHandlerLoader.loadCustom(kyuubiConf)
 
       case other =>
         throw new KyuubiException(s"Unsupported event logger: ${other.toString}")
diff --git a/kyuubi-events/src/main/scala/org/apache/kyuubi/events/handler/CustomEventHandlerProvider.scala b/kyuubi-events/src/main/scala/org/apache/kyuubi/events/handler/CustomEventHandlerProvider.scala
new file mode 100644
index 000000000..06ef416dc
--- /dev/null
+++ b/kyuubi-events/src/main/scala/org/apache/kyuubi/events/handler/CustomEventHandlerProvider.scala
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kyuubi.events.handler
+
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.events.KyuubiEvent
+
+/**
+ * Custom EventHandler provider. We can implement it to provide a custom EventHandler.
+ * The implementation will be loaded by ``Service Provider Interface``.
+ */
+trait CustomEventHandlerProvider {
+
+  /**
+   * The create method is called to create a custom event handler
+   * when this implementation is loaded.
+   *
+   * @param kyuubiConf The conf can be used to read some configs.
+   * @return A custom handler to handle KyuubiEvent.
+   */
+  def create(kyuubiConf: KyuubiConf): EventHandler[KyuubiEvent]
+}
diff --git a/kyuubi-events/src/main/scala/org/apache/kyuubi/events/handler/EventHandlerLoader.scala b/kyuubi-events/src/main/scala/org/apache/kyuubi/events/handler/EventHandlerLoader.scala
new file mode 100644
index 000000000..c81dcfb9b
--- /dev/null
+++ b/kyuubi-events/src/main/scala/org/apache/kyuubi/events/handler/EventHandlerLoader.scala
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kyuubi.events.handler
+
+import java.util.ServiceLoader
+
+import scala.collection.JavaConverters._
+import scala.collection.mutable.ArrayBuffer
+import scala.util.{Failure, Success, Try}
+
+import org.apache.kyuubi.{Logging, Utils}
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.events.KyuubiEvent
+
+object EventHandlerLoader extends Logging {
+
+  def loadCustom(kyuubiConf: KyuubiConf): Seq[EventHandler[KyuubiEvent]] = {
+    val providers = ArrayBuffer[CustomEventHandlerProvider]()
+    ServiceLoader.load(
+      classOf[CustomEventHandlerProvider],
+      Utils.getContextOrKyuubiClassLoader)
+      .iterator()
+      .asScala
+      .foreach(provider => providers += provider)
+
+    providers.map { provider =>
+      Try {
+        provider.create(kyuubiConf)
+      } match {
+        case Success(value) =>
+          value
+        case Failure(exception) =>
+          warn(
+            s"Failed to create an EventHandler by the ${provider.getClass.getName}," +
+              s" it will be ignored.",
+            exception)
+          null
+      }
+    }.filter(_ != null)
+  }
+}
diff --git a/kyuubi-events/src/test/resources/META-INF/services/org.apache.kyuubi.events.handler.CustomEventHandlerProvider b/kyuubi-events/src/test/resources/META-INF/services/org.apache.kyuubi.events.handler.CustomEventHandlerProvider
new file mode 100644
index 000000000..d6c4892ae
--- /dev/null
+++ b/kyuubi-events/src/test/resources/META-INF/services/org.apache.kyuubi.events.handler.CustomEventHandlerProvider
@@ -0,0 +1,20 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.kyuubi.events.Fake1EventHandlerProvider
+org.apache.kyuubi.events.Fake2EventHandlerProvider
+org.apache.kyuubi.events.ExceptionEventHandlerProvider
\ No newline at end of file
diff --git a/kyuubi-events/src/test/scala/org/apache/kyuubi/events/CustomEventHandlerSuite.scala b/kyuubi-events/src/test/scala/org/apache/kyuubi/events/CustomEventHandlerSuite.scala
new file mode 100644
index 000000000..452f0e297
--- /dev/null
+++ b/kyuubi-events/src/test/scala/org/apache/kyuubi/events/CustomEventHandlerSuite.scala
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kyuubi.events
+
+import org.apache.kyuubi.{KyuubiException, KyuubiFunSuite}
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.config.KyuubiConf.ENGINE_EVENT_LOGGERS
+import org.apache.kyuubi.events.handler.{CustomEventHandlerProvider, EventHandler, EventHandlerLoader}
+
+class CustomEventHandlerSuite extends KyuubiFunSuite {
+
+  test("load custom event handler") {
+    val kyuubiConf = new KyuubiConf()
+    kyuubiConf.set(ENGINE_EVENT_LOGGERS.key, "custom")
+    val providers = EventHandlerLoader.loadCustom(kyuubiConf)
+    assert(providers.head.getClass === classOf[Fake1EventHandler])
+    assert(providers(1).getClass === classOf[Fake2EventHandler])
+    assert(providers.size == 2)
+  }
+}
+
+class Fake1EventHandlerProvider extends CustomEventHandlerProvider {
+  override def create(kyuubiConf: KyuubiConf): EventHandler[KyuubiEvent] = {
+    new Fake1EventHandler(kyuubiConf)
+  }
+}
+
+class Fake1EventHandler(kyuubiConf: KyuubiConf) extends EventHandler[KyuubiEvent] {
+
+  override def apply(kyuubiEvent: KyuubiEvent): Unit = {}
+}
+
+class Fake2EventHandlerProvider extends CustomEventHandlerProvider {
+  override def create(kyuubiConf: KyuubiConf): EventHandler[KyuubiEvent] = {
+    new Fake2EventHandler(kyuubiConf)
+  }
+}
+
+class Fake2EventHandler(kyuubiConf: KyuubiConf) extends EventHandler[KyuubiEvent] {
+
+  override def apply(kyuubiEvent: KyuubiEvent): Unit = {}
+}
+
+class ExceptionEventHandlerProvider extends CustomEventHandlerProvider {
+  override def create(kyuubiConf: KyuubiConf): EventHandler[KyuubiEvent] = {
+    throw new KyuubiException("Testing exception.")
+  }
+}