Skip to content

Commit c9273eb

Browse files
zhenjiaguoulysses-you
authored andcommitted
[KYUUBI #1050] Add KyuubiServerInfoEvent
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html 2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'. 3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'. --> ### _Why are the changes needed?_ <!-- Please clarify why the changes are needed. For instance, 1. If you add a feature, you can talk about the use case of it. 2. If you fix a bug, you can clarify why it is a bug. --> Add KyuubiServerStartEvent in KyuubiServer so we can get the server level info: - server config - server version - server environment ### _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.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #1378 from zhenjiaguo/add-kyuubi-server-event. Closes #1050 d97e1d0 [zhenjiaguo] optimize import 56939c1 [zhenjiaguo] change import way 7632363 [zhenjiaguo] rebase master 4455915 [zhenjiaguo] add more info in kyuubi server event beacc88 [zhenjiaguo] add sparksql case-sensitive setting b35875f [zhenjiaguo] add kyuubi server start event test d316168 [zhenjiaguo] add KyuubiServerEvent Authored-by: zhenjiaguo <zhenjia_guo@163.com> Signed-off-by: ulysses-you <ulyssesyou@apache.org>
1 parent b7e94ff commit c9273eb

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.kyuubi.events
19+
20+
import org.apache.kyuubi.{BUILD_DATE => P_BUILD_DATE, BUILD_USER => P_BUILD_USER, REPO_URL => P_REPO_URL, _}
21+
import org.apache.kyuubi.server.KyuubiServer
22+
import org.apache.kyuubi.service.ServiceState
23+
24+
/**
25+
* A [[KyuubiServerInfoEvent]] is used to get server level info when KyuubiServer start or stop
26+
*
27+
* @param serverName the server name
28+
* @param startTime the time when the server started
29+
* @param eventTime the time when the event made
30+
* @param state the server states
31+
* @param serverIP the server ip
32+
* @param serverConf the server config
33+
* @param serverEnv the server environment
34+
*/
35+
case class KyuubiServerInfoEvent private (
36+
serverName: String,
37+
startTime: Long,
38+
eventTime: Long,
39+
state: String,
40+
serverIP: String,
41+
serverConf: Map[String, String],
42+
serverEnv: Map[String, String]) extends KyuubiServerEvent {
43+
44+
val BUILD_USER: String = P_BUILD_USER
45+
val BUILD_DATE: String = P_BUILD_DATE
46+
val REPO_URL: String = P_REPO_URL
47+
48+
val VERSION_INFO = Map(
49+
"KYUUBI_VERSION" -> KYUUBI_VERSION,
50+
"JAVA_COMPILE_VERSION" -> JAVA_COMPILE_VERSION,
51+
"SCALA_COMPILE_VERSION" -> SCALA_COMPILE_VERSION,
52+
"SPARK_COMPILE_VERSION" -> SPARK_COMPILE_VERSION,
53+
"HIVE_COMPILE_VERSION" -> HIVE_COMPILE_VERSION,
54+
"HADOOP_COMPILE_VERSION" -> HADOOP_COMPILE_VERSION)
55+
56+
override def partitions: Seq[(String, String)] =
57+
("day", Utils.getDateFromTimestamp(startTime)) :: Nil
58+
}
59+
60+
object KyuubiServerInfoEvent {
61+
62+
def apply(
63+
server: KyuubiServer,
64+
state: ServiceState.ServiceState): Option[KyuubiServerInfoEvent] = {
65+
server.getServiceState match {
66+
// Only server is started that we can log event
67+
case ServiceState.STARTED =>
68+
Some(KyuubiServerInfoEvent(
69+
server.getName,
70+
server.getStartTime,
71+
System.currentTimeMillis(),
72+
state.toString,
73+
server.frontendServices.head.connectionUrl,
74+
server.getConf.getAll,
75+
server.getConf.getEnvs))
76+
case _ => None
77+
}
78+
}
79+
}

kyuubi-server/src/main/scala/org/apache/kyuubi/server/KyuubiServer.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import org.apache.kyuubi._
3131
import org.apache.kyuubi.config.KyuubiConf
3232
import org.apache.kyuubi.config.KyuubiConf.{FRONTEND_PROTOCOLS, FrontendProtocols}
3333
import org.apache.kyuubi.config.KyuubiConf.FrontendProtocols._
34+
import org.apache.kyuubi.events.KyuubiServerInfoEvent
3435
import org.apache.kyuubi.ha.HighAvailabilityConf._
3536
import org.apache.kyuubi.ha.client.{ServiceDiscovery, ZooKeeperAuthTypes}
3637
import org.apache.kyuubi.ha.client.ZooKeeperClientProvider._
3738
import org.apache.kyuubi.metrics.{MetricsConf, MetricsSystem}
38-
import org.apache.kyuubi.service.{AbstractBackendService, AbstractFrontendService, Serverable}
39+
import org.apache.kyuubi.service.{AbstractBackendService, AbstractFrontendService, Serverable, ServiceState}
3940
import org.apache.kyuubi.util.{KyuubiHadoopUtils, SignalRegister}
4041
import org.apache.kyuubi.zookeeper.EmbeddedZookeeper
4142

@@ -157,6 +158,12 @@ class KyuubiServer(name: String) extends Serverable(name) {
157158
override def start(): Unit = {
158159
super.start()
159160
KyuubiServer.kyuubiServer = this
161+
KyuubiServerInfoEvent(this, ServiceState.STARTED).foreach(EventLoggingService.onEvent)
162+
}
163+
164+
override def stop(): Unit = {
165+
KyuubiServerInfoEvent(this, ServiceState.STOPPED).foreach(EventLoggingService.onEvent)
166+
super.stop()
160167
}
161168

162169
override protected def stopServer(): Unit = {}

kyuubi-server/src/test/scala/org/apache/kyuubi/events/EventLoggingServiceSuite.scala

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import java.net.InetAddress
2121
import java.nio.file.Paths
2222
import java.util.UUID
2323

24+
import com.fasterxml.jackson.databind.ObjectMapper
2425
import org.apache.hadoop.conf.Configuration
2526
import org.apache.hadoop.fs.{FileSystem, Path}
2627

27-
import org.apache.kyuubi.{Utils, WithKyuubiServer}
28+
import org.apache.kyuubi._
2829
import org.apache.kyuubi.config.KyuubiConf
2930
import org.apache.kyuubi.operation.HiveJDBCTestHelper
3031
import org.apache.kyuubi.operation.OperationState._
32+
import org.apache.kyuubi.server.KyuubiServer
33+
import org.apache.kyuubi.service.ServiceState
3134

3235
class EventLoggingServiceSuite extends WithKyuubiServer with HiveJDBCTestHelper {
3336

@@ -151,4 +154,64 @@ class EventLoggingServiceSuite extends WithKyuubiServer with HiveJDBCTestHelper
151154
}
152155
}
153156
}
157+
158+
test("test Kyuubi server info event") {
159+
val confKv = List(("awesome.kyuubi", "true"), ("awesome.kyuubi.server", "yeah"))
160+
confKv.foreach(kv => conf.set(kv._1, kv._2))
161+
162+
val name = "KyuubiServerInfoTest"
163+
val server = new KyuubiServer(name)
164+
server.initialize(conf)
165+
server.start()
166+
server.stop()
167+
168+
val hostName = InetAddress.getLocalHost.getCanonicalHostName
169+
val kyuubiServerInfoPath =
170+
Paths.get(serverLogRoot, "kyuubi_server_info", s"day=$currentDate", s"server-$hostName.json")
171+
172+
withJdbcStatement() { statement =>
173+
statement.executeQuery("set spark.sql.caseSensitive=true")
174+
val res = statement.executeQuery(
175+
s"SELECT * FROM `json`.`$kyuubiServerInfoPath` where serverName = '$name'")
176+
177+
res.next()
178+
assert(res.getString("serverName") == name)
179+
assert(res.getLong("startTime") > 0)
180+
181+
val startEventTime = res.getLong("eventTime")
182+
assert(res.getLong("startTime") <= startEventTime)
183+
assert(res.getString("state") == ServiceState.STARTED.toString)
184+
185+
val serverIP = res.getString("serverIP")
186+
assert(serverIP != null && !"".equals(serverIP))
187+
188+
val objMapper = new ObjectMapper()
189+
val confMap = objMapper.readTree(res.getString("serverConf"))
190+
confKv.foreach(kv => assert(confMap.get(kv._1).asText() == kv._2))
191+
192+
val USER = "USER"
193+
val envMap = objMapper.readTree(res.getString("serverEnv"))
194+
val envUser = if (envMap.has(USER)) envMap.get(USER).asText() else ""
195+
assert(envUser == sys.env.getOrElse(USER, ""))
196+
197+
assert(res.getString("BUILD_USER") == BUILD_USER)
198+
assert(res.getString("BUILD_DATE") == BUILD_DATE)
199+
assert(res.getString("REPO_URL") == REPO_URL)
200+
201+
val versionInfoMap = objMapper.readTree(res.getString("VERSION_INFO"))
202+
assert(versionInfoMap.get("KYUUBI_VERSION").asText() == KYUUBI_VERSION)
203+
assert(versionInfoMap.get("JAVA_COMPILE_VERSION").asText() == JAVA_COMPILE_VERSION)
204+
assert(versionInfoMap.get("SCALA_COMPILE_VERSION").asText() == SCALA_COMPILE_VERSION)
205+
assert(versionInfoMap.get("HIVE_COMPILE_VERSION").asText() == HIVE_COMPILE_VERSION)
206+
assert(versionInfoMap.get("HADOOP_COMPILE_VERSION").asText() == HADOOP_COMPILE_VERSION)
207+
assert(res.getString("eventType") == "kyuubi_server_info")
208+
209+
res.next()
210+
assert(res.getString("serverName") == name)
211+
assert(startEventTime <= res.getLong("eventTime"))
212+
assert(res.getString("state") == ServiceState.STOPPED.toString)
213+
assert(res.getString("BUILD_USER") == BUILD_USER)
214+
assert(res.getString("eventType") == "kyuubi_server_info")
215+
}
216+
}
154217
}

0 commit comments

Comments
 (0)