This is an automated email from the ASF dual-hosted git repository.

dkuzmenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new b240eb3266d HIVE-28999: Fix Flaky Test: 
TestReExecuteKilledTezAMQueryPlugin.testKillQueryById (#5854)
b240eb3266d is described below

commit b240eb3266d4736424678d6c71c3c6f6a6fdbf38
Author: Indhumathi <[email protected]>
AuthorDate: Thu Jun 12 13:17:42 2025 +0530

    HIVE-28999: Fix Flaky Test: 
TestReExecuteKilledTezAMQueryPlugin.testKillQueryById (#5854)
---
 .../TestReExecuteKilledTezAMQueryPlugin.java       | 44 +++++++++++++++++++---
 1 file changed, 39 insertions(+), 5 deletions(-)

diff --git 
a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/reexec/TestReExecuteKilledTezAMQueryPlugin.java
 
b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/reexec/TestReExecuteKilledTezAMQueryPlugin.java
index 2dfe5069f99..6083fd01a5c 100644
--- 
a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/reexec/TestReExecuteKilledTezAMQueryPlugin.java
+++ 
b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/reexec/TestReExecuteKilledTezAMQueryPlugin.java
@@ -34,6 +34,7 @@
 import java.net.URL;
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.HashMap;
@@ -48,6 +49,7 @@ public class TestReExecuteKilledTezAMQueryPlugin {
   private static String dataFileDir;
   private static final String testDbName = "testKillTezAmDb";
   private static HiveConf conf;
+  private static YarnClient yarnClient;
 
   private static class ExceptionHolder {
     Throwable throwable;
@@ -96,6 +98,11 @@ public static void beforeTest() throws Exception {
 
     stmt.close();
     conDefault.close();
+
+    // start yarn client
+    yarnClient = YarnClient.createYarnClient();
+    yarnClient.init(conf);
+    yarnClient.start();
   }
 
   @AfterClass
@@ -103,6 +110,9 @@ public static void afterTest() {
     if (miniHS2 != null && miniHS2.isStarted()) {
       miniHS2.stop();
     }
+    if (yarnClient != null) {
+      yarnClient.stop();
+    }
   }
 
   /**
@@ -160,7 +170,7 @@ public void run() {
     int count = 0;
     while (++count <= 10) {
       Thread.sleep(2000);
-      if (stmtQueryId.length() != 0) {
+      if (stmtQueryId.length() != 0 && isQueryRunning(stmtQueryId.toString(), 
con1)) {
         String queryId = stmtQueryId.toString();
         System.out.println("Killing query: " + queryId);
         killAMForQueryId(queryId);
@@ -180,10 +190,6 @@ public void run() {
   }
 
   private void killAMForQueryId(String queryId) throws Exception {
-    YarnClient yarnClient = YarnClient.createYarnClient();
-    yarnClient.init(conf);
-    yarnClient.start();
-
     List<ApplicationReport> applicationReports = yarnClient.getApplications();
     String diagnosticsMessage = "AM Container for %s exited with exitCode: 
-100";
     for (ApplicationReport ar : applicationReports) {
@@ -195,4 +201,32 @@ private void killAMForQueryId(String queryId) throws 
Exception {
       }
     }
   }
+
+  boolean isQueryRunning(String queryId, Connection conn) throws Exception {
+    boolean queryRunningInYarn = false;
+    boolean hiveRunning = false;
+
+    // Check YARN state
+    List<ApplicationReport> applicationReports = yarnClient.getApplications();
+    for (ApplicationReport ar : applicationReports) {
+      if (ar.getApplicationTags().contains(queryId) &&
+              ar.getYarnApplicationState() == YarnApplicationState.RUNNING) {
+        queryRunningInYarn = true;
+        break;
+      }
+    }
+    // check if the DAG is also started
+    if (queryRunningInYarn) {
+      try (Statement s = conn.createStatement();
+           ResultSet rs = s.executeQuery(
+                   "SELECT STATE FROM SYS.QUERY WHERE QUERY_ID='" + queryId + 
"'")) {
+        if (rs.next()) {
+          hiveRunning = "RUNNING".equalsIgnoreCase(rs.getString(1));
+        }
+      } catch (Exception e) {
+        LOG.warn("Exception when checking hive state", e);
+      }
+    }
+    return hiveRunning;
+  }
 }
\ No newline at end of file

Reply via email to