From caaa96e4cb59c9b8e57c7abf3fe3f72aa5b33258 Mon Sep 17 00:00:00 2001 From: nanfeng1999 Date: Sun, 17 Dec 2023 16:38:04 +0800 Subject: [PATCH 1/3] support kill query by queryId --- fe/fe-core/src/main/cup/sql_parser.cup | 4 ++++ .../java/org/apache/doris/analysis/KillStmt.java | 12 ++++++++++++ .../java/org/apache/doris/common/ErrorCode.java | 1 + .../org/apache/doris/qe/ConnectScheduler.java | 9 +++++++++ .../java/org/apache/doris/qe/StmtExecutor.java | 16 +++++++++++++--- 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup index fbd657dbccbebb..bd6cb92fc562e1 100644 --- a/fe/fe-core/src/main/cup/sql_parser.cup +++ b/fe/fe-core/src/main/cup/sql_parser.cup @@ -4906,6 +4906,10 @@ kill_stmt ::= {: RESULT = new KillStmt(false, value.intValue()); :} + | KW_KILL KW_QUERY STRING_LITERAL:value + {: + RESULT = new KillStmt(value); + :} ; kill_analysis_job_stmt ::= diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/KillStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/KillStmt.java index 56f4f24d2312da..77cbafe4a8fb60 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/KillStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/KillStmt.java @@ -25,10 +25,18 @@ public class KillStmt extends StatementBase { private final boolean isConnectionKill; private final int connectionId; + private final String queryId; public KillStmt(boolean isConnectionKill, int connectionId) { this.isConnectionKill = isConnectionKill; this.connectionId = connectionId; + this.queryId = ""; + } + + public KillStmt(String queryId) { + this.isConnectionKill = false; + this.connectionId = -1; + this.queryId = queryId; } public boolean isConnectionKill() { @@ -39,6 +47,10 @@ public int getConnectionId() { return connectionId; } + public String getQueryId() { + return queryId; + } + @Override public void analyze(Analyzer analyzer) { // No operation. diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/ErrorCode.java b/fe/fe-core/src/main/java/org/apache/doris/common/ErrorCode.java index 09af833628e8ed..b8b3d7e08696ed 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/common/ErrorCode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/common/ErrorCode.java @@ -43,6 +43,7 @@ public enum ErrorCode { ERR_NO_SUCH_THREAD(1094, new byte[]{'H', 'Y', '0', '0', '0'}, "Unknown thread id: %d"), ERR_KILL_DENIED_ERROR(1095, new byte[]{'H', 'Y', '0', '0', '0'}, "You are not owner of thread %d"), ERR_NO_TABLES_USED(1096, new byte[]{'H', 'Y', '0', '0', '0'}, "No tables used"), + ERR_NO_SUCH_QUERY(1097, new byte[]{'H', 'Y', '0', '0', '0'}, "Unknown query id: %s"), ERR_WRONG_DB_NAME(1102, new byte[]{'4', '2', '0', '0', '0'}, "Incorrect database name '%s'"), ERR_WRONG_TABLE_NAME(1103, new byte[]{'4', '2', '0', '0', '0'}, "Incorrect table name '%s'. Table name regex is '%s'"), ERR_TOO_BIG_SELECT(1104, new byte[]{'4', '2', '0', '0', '0'}, "The SELECT would examine more than MAX_JOIN_SIZE " diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectScheduler.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectScheduler.java index 5be4c330e0aec2..53925ce5e0fc34 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectScheduler.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectScheduler.java @@ -126,6 +126,15 @@ public ConnectContext getContext(int connectionId) { return connectionMap.get(connectionId); } + public ConnectContext getContextWithQueryId(String queryId) { + for (ConnectContext context : connectionMap.values()) { + if (queryId.equals(DebugUtil.printId(context.queryId))) { + return context; + } + } + return null; + } + public ConnectContext getContext(String flightToken) { if (flightToken2ConnectionId.containsKey(flightToken)) { int connectionId = flightToken2ConnectionId.get(flightToken); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index ff837283056e2c..0e32d27b4d1abe 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -1242,11 +1242,21 @@ public void cancel() { // Handle kill statement. private void handleKill() throws DdlException { KillStmt killStmt = (KillStmt) parsedStmt; + ConnectContext killCtx = null; int id = killStmt.getConnectionId(); - ConnectContext killCtx = context.getConnectScheduler().getContext(id); - if (killCtx == null) { - ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_THREAD, id); + String queryId = killStmt.getQueryId(); + if (id == -1) { + killCtx = context.getConnectScheduler().getContextWithQueryId(queryId); + if (killCtx == null) { + ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_QUERY, queryId); + } + } else { + killCtx = context.getConnectScheduler().getContext(id); + if (killCtx == null) { + ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_THREAD, id); + } } + if (context == killCtx) { // Suicide context.setKilled(); From b93c6c48cbedfae871fd2fdd1e2ff08016cddba1 Mon Sep 17 00:00:00 2001 From: nanfeng <42513321+nanfeng1999@users.noreply.github.com> Date: Sun, 17 Dec 2023 17:42:59 +0800 Subject: [PATCH 2/3] Update KILL.md add description about kill query by query_id --- .../Database-Administration-Statements/KILL.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/zh-CN/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md b/docs/zh-CN/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md index c671abcc5e707c..d35cdfa3145839 100644 --- a/docs/zh-CN/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md +++ b/docs/zh-CN/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md @@ -39,9 +39,19 @@ KILL 语法: ```sql -KILL [CONNECTION | QUERY] processlist_id +KILL [CONNECTION] processlist_id ``` +除此之外,您还可以使用 processlist_id 或者 query_id 终止正在执行的查询命令 + +语法: + +```sql +KILL QUERY processlist_id | query_id +``` + + + ### Example ### Keywords From f92732b0e5c7d6496911a879beadcdc4df7a029c Mon Sep 17 00:00:00 2001 From: nanfeng <42513321+nanfeng1999@users.noreply.github.com> Date: Sun, 17 Dec 2023 17:46:49 +0800 Subject: [PATCH 3/3] Update KILL.md add description about kill query by query_id --- .../Database-Administration-Statements/KILL.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/en/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md b/docs/en/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md index 04106a48f1f5ed..67f948c964078b 100644 --- a/docs/en/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md +++ b/docs/en/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md @@ -39,9 +39,18 @@ The thread process list identifier can be determined from the ID column of the I grammar: ```sql -KILL [CONNECTION | QUERY] processlist_id +KILL [CONNECTION] processlist_id ```` +In addition, you can also use processlist_id or query_id terminates the executing query command + +grammar: + +```sql +KILL QUERY processlist_id | query_id +```` + + ### Example ### Keywords