juliuszsompolski commented on a change in pull request #25611: 
[SPARK-28901][SQL] SparkThriftServer's Cancel SQL Operation show it in JDBC Tab 
UI
URL: https://github.com/apache/spark/pull/25611#discussion_r319170864
 
 

 ##########
 File path: 
sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/HiveThriftServer2.scala
 ##########
 @@ -239,6 +239,15 @@ object HiveThriftServer2 extends Logging {
       executionList(id).state = ExecutionState.COMPILED
     }
 
+    def onStatementCanceled(id: String): Unit = {
+      synchronized {
+        executionList(id).finishTimestamp = System.currentTimeMillis
+        executionList(id).state = ExecutionState.CANCELED
+        totalRunning -= 1
 
 Review comment:
   It can happen that `cancel()` will race with query finishing, and then both 
`onStatementCanceled()` and `onStatementFinish()` will do `totalRunning -= 1`, 
resulting in negative count.
   
   Instead of keeping a counter, for sessions and queries, it is better to do 
something like:
   ```
       def getOnlineSessionNum: Int = synchronized {
         sessionList.count(_._2.finishTimestamp == 0)
       }
   
       def isExecutionActive(execInfo: ExecutionInfo): Boolean = {
         !(execInfo.state == ExecutionState.FAILED ||
           execInfo.state == ExecutionState.CANCELED ||
           execInfo.closeTimestamp != 0)
       }
   
       /**
        * When an error or a cancellation occurs, we set the finishTimestamp of 
the statement.
        * Therefore, when we count the number of running statements, we need to 
exclude errors and
        * cancellations and count all statements that have not been closed so 
far.
        */
       def getTotalRunning: Int = synchronized {
         executionList.count {
           case (_, v) => isExecutionActive(v)
         }
       }
   ```
   (Note that we should also consider statements that are CLOSED instead of 
FINISHED as not running anymore, because a FINISHED statement is still fetching 
results.)
   (Same for sessions counters - though quite an obscure, there can be a race 
between a session timeout and a client closing session)

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to