[KYUUBI #1080] Move stringifyException to Utils

<!--
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.
-->

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 <timothy65535@163.com>
Signed-off-by: Cheng Pan <chengpan@apache.org>
This commit is contained in:
timothy65535 2021-09-12 22:43:27 +08:00 committed by Cheng Pan
parent 95be93cd7a
commit c080283671
No known key found for this signature in database
GPG Key ID: 8001952629BCC75D
5 changed files with 20 additions and 16 deletions

View File

@ -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)

View File

@ -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] =

View File

@ -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)

View File

@ -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
}
}

View File

@ -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"))
}