yikf commented on a change in pull request #32222:
URL: https://github.com/apache/spark/pull/32222#discussion_r615367709
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCRDD.scala
##########
@@ -301,10 +306,44 @@ private[jdbc] class JDBCRDD(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(options.fetchSize)
stmt.setQueryTimeout(options.queryTimeout)
- rs = stmt.executeQuery()
+
+ val isFinished = new AtomicBoolean(false)
+ startTheJobInterruptListenerThread(context, stmt, isFinished)
+ try{
+ rs = stmt.executeQuery()
+ } finally {
+ isFinished.set(true)
+ }
val rowsIterator = JdbcUtils.resultSetToSparkInternalRows(rs, schema,
inputMetrics)
CompletionIterator[InternalRow, Iterator[InternalRow]](
new InterruptibleIterator(context, rowsIterator), close())
}
+
+ /**
+ * Start the job interruption listener thread, if the job is interrupted and
+ * stmt.executeQuery() is not completed, initiate a stmt.cancel() request.
+ */
+ private def startTheJobInterruptListenerThread(context: TaskContext, stmt:
PreparedStatement
+ , isFinished: AtomicBoolean):
Unit = {
+ val thread = new Thread(() => {
+ val waitInterval = 100
+ // Always wait for job interruption or stmt.executeQuery() execution to
complete
+ while (!context.isInterrupted() && !isFinished.get()) {
+ Thread.sleep(waitInterval)
+ }
+
+ // Is interrupted and stmt.executeQuery() is not completed, then
initiate a cancel request
+ if (context.isInterrupted() && !isFinished.get()) {
+ logInfo(s"try to cancel query ${context.getKillReason()}")
+ try {
+ stmt.cancel()
+ } catch {
+ case e: Exception => logWarning("Exception cancel statement", e)
Review comment:
if cancel occur a error, `stmt.cancel()` will throw SQLException only,
rigth?
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCRDD.scala
##########
@@ -301,10 +306,44 @@ private[jdbc] class JDBCRDD(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(options.fetchSize)
stmt.setQueryTimeout(options.queryTimeout)
- rs = stmt.executeQuery()
+
+ val isFinished = new AtomicBoolean(false)
Review comment:
Could you add test suite for this code?
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCRDD.scala
##########
@@ -301,10 +306,44 @@ private[jdbc] class JDBCRDD(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(options.fetchSize)
stmt.setQueryTimeout(options.queryTimeout)
- rs = stmt.executeQuery()
+
+ val isFinished = new AtomicBoolean(false)
+ startTheJobInterruptListenerThread(context, stmt, isFinished)
+ try{
+ rs = stmt.executeQuery()
+ } finally {
+ isFinished.set(true)
+ }
val rowsIterator = JdbcUtils.resultSetToSparkInternalRows(rs, schema,
inputMetrics)
CompletionIterator[InternalRow, Iterator[InternalRow]](
new InterruptibleIterator(context, rowsIterator), close())
}
+
+ /**
+ * Start the job interruption listener thread, if the job is interrupted and
+ * stmt.executeQuery() is not completed, initiate a stmt.cancel() request.
+ */
+ private def startTheJobInterruptListenerThread(context: TaskContext, stmt:
PreparedStatement
+ , isFinished: AtomicBoolean):
Unit = {
+ val thread = new Thread(() => {
+ val waitInterval = 100
Review comment:
why is the period set to 100 millis?
--
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]