From c0802836719e24c4e53b6e208f9925f4415d3d21 Mon Sep 17 00:00:00 2001 From: timothy65535 Date: Sun, 12 Sep 2021 22:43:27 +0800 Subject: [PATCH] [KYUUBI #1080] Move stringifyException to Utils ### _Why are the changes needed?_ Move stringifyException from KyuubiSQLException to Utils, make it more general. ### _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 - [ ] [Run test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #1081 from timothy65535/ky-1080. Closes #1080 ce3015d5 [timothy65535] [KYUUBI #1080] Move stringifyException to Utils Authored-by: timothy65535 Signed-off-by: Cheng Pan --- .../engine/spark/operation/SparkOperation.scala | 4 ++-- .../spark/kyuubi/SparkSQLEngineListener.scala | 3 +-- .../org/apache/kyuubi/KyuubiSQLException.scala | 11 ++--------- .../src/main/scala/org/apache/kyuubi/Utils.scala | 13 ++++++++++++- .../KyuubiOperationPerConnectionSuite.scala | 5 +++-- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala index cc945f7ff..3a16ee408 100644 --- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala +++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala @@ -25,7 +25,7 @@ import org.apache.hive.service.rpc.thrift.{TRowSet, TTableSchema} import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.types.StructType -import org.apache.kyuubi.KyuubiSQLException +import org.apache.kyuubi.{KyuubiSQLException, Utils} import org.apache.kyuubi.engine.spark.FetchIterator import org.apache.kyuubi.engine.spark.operation.SparkOperation.TIMEZONE_KEY import org.apache.kyuubi.operation.{AbstractOperation, OperationState} @@ -85,7 +85,7 @@ abstract class SparkOperation(spark: SparkSession, opType: OperationType, sessio case e: Throwable => if (cancel && !spark.sparkContext.isStopped) spark.sparkContext.cancelJobGroup(statementId) state.synchronized { - val errMsg = KyuubiSQLException.stringifyException(e) + val errMsg = Utils.stringifyException(e) if (state == OperationState.TIMEOUT) { val ke = KyuubiSQLException(s"Timeout operating $opType: $errMsg") setOperationException(ke) diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/SparkSQLEngineListener.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/SparkSQLEngineListener.scala index 099b605da..102930b7e 100644 --- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/SparkSQLEngineListener.scala +++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/SparkSQLEngineListener.scala @@ -27,8 +27,8 @@ import org.apache.spark.SparkException import org.apache.spark.scheduler._ import org.apache.kyuubi.KyuubiSparkUtils.KYUUBI_STATEMENT_ID_KEY -import org.apache.kyuubi.KyuubiSQLException import org.apache.kyuubi.Logging +import org.apache.kyuubi.Utils.stringifyException import org.apache.kyuubi.config.KyuubiConf._ import org.apache.kyuubi.engine.spark.monitor.KyuubiStatementMonitor import org.apache.kyuubi.engine.spark.monitor.entity.KyuubiJobInfo @@ -41,7 +41,6 @@ import org.apache.kyuubi.service.{Serverable, ServiceState} * @param server the corresponding engine */ class SparkSQLEngineListener(server: Serverable) extends SparkListener with Logging { - import KyuubiSQLException.stringifyException // the conf of server is null before initialized, use lazy val here private lazy val deregisterExceptions: Seq[String] = diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/KyuubiSQLException.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/KyuubiSQLException.scala index ec662ca3e..8136997cd 100644 --- a/kyuubi-common/src/main/scala/org/apache/kyuubi/KyuubiSQLException.scala +++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/KyuubiSQLException.scala @@ -17,7 +17,6 @@ package org.apache.kyuubi -import java.io.{PrintWriter, StringWriter} import java.lang.reflect.{InvocationTargetException, UndeclaredThrowableException} import java.sql.SQLException @@ -26,6 +25,8 @@ import scala.collection.JavaConverters._ import org.apache.hive.service.rpc.thrift.{TStatus, TStatusCode} +import org.apache.kyuubi.Utils.stringifyException + /** * @param reason a description of the exception * @param sqlState an XOPEN or SQL:2003 code identifying the exception @@ -175,14 +176,6 @@ object KyuubiSQLException { ex } - def stringifyException(e: Throwable): String = { - val stm = new StringWriter - val wrt = new PrintWriter(stm) - e.printStackTrace(wrt) - wrt.close() - stm.toString - } - @tailrec def findCause(t: Throwable): Throwable = t match { case e @ (_: UndeclaredThrowableException | _: InvocationTargetException) diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala index 09e32c801..c581f9791 100644 --- a/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala +++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/Utils.scala @@ -17,7 +17,7 @@ package org.apache.kyuubi -import java.io.{File, InputStreamReader, IOException} +import java.io.{File, InputStreamReader, IOException, PrintWriter, StringWriter} import java.net.{Inet4Address, InetAddress, NetworkInterface} import java.nio.charset.StandardCharsets import java.nio.file.{Files, Path, Paths} @@ -229,4 +229,15 @@ object Utils extends Logging { def getDateFromTimestamp(time: Long): String = { DateFormatUtils.format(time, "yyyyMMdd", TimeZone.getDefault) } + + /** + * Make a string representation of the exception. + */ + def stringifyException(e: Throwable): String = { + val stm = new StringWriter + val wrt = new PrintWriter(stm) + e.printStackTrace(wrt) + wrt.close() + stm.toString + } } diff --git a/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/KyuubiOperationPerConnectionSuite.scala b/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/KyuubiOperationPerConnectionSuite.scala index 6ada31b64..b0fc125e0 100644 --- a/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/KyuubiOperationPerConnectionSuite.scala +++ b/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/KyuubiOperationPerConnectionSuite.scala @@ -22,7 +22,8 @@ import java.sql.SQLException import org.apache.hive.service.rpc.thrift.{TExecuteStatementReq, TGetOperationStatusReq, TOperationState, TStatusCode} import org.scalatest.time.SpanSugar.convertIntToGrainOfTime -import org.apache.kyuubi.{KyuubiSQLException, WithKyuubiServer} +import org.apache.kyuubi.Utils +import org.apache.kyuubi.WithKyuubiServer import org.apache.kyuubi.config.KyuubiConf /** @@ -60,7 +61,7 @@ class KyuubiOperationPerConnectionSuite extends WithKyuubiServer with JDBCTestUt withJdbcStatement() { statement => // no-op } } - val verboseMessage = KyuubiSQLException.stringifyException(exception) + val verboseMessage = Utils.stringifyException(exception) assert(verboseMessage.contains("Failed to detect the root cause")) assert(verboseMessage.contains("The last line log")) }