pan3793 commented on code in PR #7029: URL: https://github.com/apache/kyuubi/pull/7029#discussion_r2055452685
########## kyuubi-server/src/main/scala/org/apache/kyuubi/server/metadata/jdbc/JDBCMetadataStore.scala: ########## @@ -419,6 +419,142 @@ class JDBCMetadataStore(conf: KyuubiConf) extends MetadataStore with Logging { } } + private def insertKubernetesEngineInfo(engineInfo: KubernetesEngineInfo): Unit = { + val insertQuery = + s""" + |INSERT INTO $KUBERNETES_ENGINE_INFO_TABLE( + |identifier, + |context, + |namespace, + |pod_name, + |pod_state, + |container_state, + |engine_id, + |engine_name, + |engine_state, + |engine_error, + |create_time, + |update_time + |) + |SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,? + |WHERE NOT EXISTS ( + | SELECT 1 FROM $KUBERNETES_ENGINE_INFO_TABLE WHERE identifier = ? + |) + |""".stripMargin + JdbcUtils.withConnection { connection => + val currentTime = System.currentTimeMillis() + execute( + connection, + insertQuery, + engineInfo.identifier, + engineInfo.context.orNull, + engineInfo.namespace.orNull, + engineInfo.podName, + engineInfo.podState, + engineInfo.containerState, + engineInfo.engineId, + engineInfo.engineName, + engineInfo.engineState, + engineInfo.engineError.orNull, + currentTime, + currentTime, + engineInfo.identifier) + } + } + + private def updateKubernetesEngineInfo(engineInfo: KubernetesEngineInfo): Unit = { + val queryBuilder = new StringBuilder + val params = ListBuffer[Any]() + + queryBuilder.append(s"UPDATE $KUBERNETES_ENGINE_INFO_TABLE") + val setClauses = ListBuffer[String]() + engineInfo.context.foreach { context => + setClauses += "context = ?" + params += context + } + engineInfo.namespace.foreach { namespace => + setClauses += "namespace = ?" + params += namespace + } + Option(engineInfo.podName).foreach { pod => + setClauses += "pod_name = ?" + params += pod + } + Option(engineInfo.podState).foreach { podState => + setClauses += "pod_state = ?" + params += podState + } + Option(engineInfo.containerState).foreach { containerState => + setClauses += "container_state = ?" + params += containerState + } + Option(engineInfo.engineId).foreach { appId => + setClauses += "engine_id = ?" + params += appId + } + Option(engineInfo.engineName).foreach { appName => + setClauses += "engine_name = ?" + params += appName + } + Option(engineInfo.engineState).foreach { appState => + setClauses += "engine_state = ?" + params += appState + } + engineInfo.engineError.foreach { appError => + setClauses += "engine_error = ?" + params += appError + } + setClauses += "update_time = ?" + params += System.currentTimeMillis() + + queryBuilder.append(setClauses.mkString(" SET ", ", ", "")) + queryBuilder.append(" WHERE identifier = ?") + params += engineInfo.identifier + + val query = queryBuilder.toString() + JdbcUtils.withConnection { connection => + withUpdateCount(connection, query, params.toSeq: _*) { updateCount => + if (updateCount == 0) { + throw new KyuubiException( + s"Error updating kubernetes engine info for ${engineInfo.identifier} by SQL: $query, " + + s"with params: ${params.mkString(", ")}") + } + } + } + + } + + override def upsertKubernetesEngineInfo(metadata: KubernetesEngineInfo): Unit = { + insertKubernetesEngineInfo(metadata) + updateKubernetesEngineInfo(metadata) Review Comment: MySQL has native support for `INSERT ... ON DUPLICATE KEY UPDATE`, it's the simplest and most efficient way, and if we use DB-specific implementations, we don't need to care about the multi-instance issue for SQLite. ########## kyuubi-server/src/main/scala/org/apache/kyuubi/server/metadata/jdbc/JDBCMetadataStore.scala: ########## @@ -419,6 +419,142 @@ class JDBCMetadataStore(conf: KyuubiConf) extends MetadataStore with Logging { } } + private def insertKubernetesEngineInfo(engineInfo: KubernetesEngineInfo): Unit = { + val insertQuery = + s""" + |INSERT INTO $KUBERNETES_ENGINE_INFO_TABLE( + |identifier, + |context, + |namespace, + |pod_name, + |pod_state, + |container_state, + |engine_id, + |engine_name, + |engine_state, + |engine_error, + |create_time, + |update_time + |) + |SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,? Review Comment: nit: spaces ########## kyuubi-server/src/main/scala/org/apache/kyuubi/server/metadata/jdbc/JDBCMetadataStore.scala: ########## @@ -419,6 +419,142 @@ class JDBCMetadataStore(conf: KyuubiConf) extends MetadataStore with Logging { } } + private def insertKubernetesEngineInfo(engineInfo: KubernetesEngineInfo): Unit = { + val insertQuery = + s""" + |INSERT INTO $KUBERNETES_ENGINE_INFO_TABLE( + |identifier, + |context, + |namespace, + |pod_name, + |pod_state, + |container_state, + |engine_id, + |engine_name, + |engine_state, + |engine_error, + |create_time, + |update_time + |) + |SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,? + |WHERE NOT EXISTS ( Review Comment: not sure if it's better to use the DB-specific dialect, e.g. MySQL supports `INSERT IGNORE INTO` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@kyuubi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@kyuubi.apache.org For additional commands, e-mail: notifications-h...@kyuubi.apache.org