This is an automated email from the ASF dual-hosted git repository. wangbo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push: new 7b5bc731efc [Improvement](executor)Support kill query in be (#35602) 7b5bc731efc is described below commit 7b5bc731efc36485a6041ccedf8daa122daf02b9 Author: wangbo <wan...@apache.org> AuthorDate: Fri May 31 10:46:23 2024 +0800 [Improvement](executor)Support kill query in be (#35602) --- .../org/apache/doris/common/util/DebugUtil.java | 24 +++++++++++ .../apache/doris/common/util/ProfileManager.java | 7 +++- .../java/org/apache/doris/common/util/Util.java | 24 ----------- .../java/org/apache/doris/qe/StmtExecutor.java | 31 ++++++++++++--- .../apache/doris/common/util/DebugUtilTest.java | 45 +++++++++++++++++++++ .../org/apache/doris/common/util/UtilTest.java | 46 ---------------------- 6 files changed, 101 insertions(+), 76 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/util/DebugUtil.java b/fe/fe-core/src/main/java/org/apache/doris/common/util/DebugUtil.java index 75fb331347e..2a52420a96d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/common/util/DebugUtil.java +++ b/fe/fe-core/src/main/java/org/apache/doris/common/util/DebugUtil.java @@ -21,6 +21,8 @@ import org.apache.doris.common.Pair; import org.apache.doris.proto.Types; import org.apache.doris.thrift.TUniqueId; +import com.google.common.base.Strings; + import java.io.PrintWriter; import java.io.StringWriter; import java.text.DecimalFormat; @@ -135,6 +137,28 @@ public class DebugUtil { return builder.toString(); } + // id is a String generated by DebugUtil.printId(TUniqueId) + public static TUniqueId parseTUniqueIdFromString(String id) { + if (Strings.isNullOrEmpty(id)) { + throw new NumberFormatException("invalid query id"); + } + + String[] parts = id.split("-"); + if (parts.length != 2) { + throw new NumberFormatException("invalid query id"); + } + + TUniqueId uniqueId = new TUniqueId(); + try { + uniqueId.setHi(Long.parseUnsignedLong(parts[0], 16)); + uniqueId.setLo(Long.parseUnsignedLong(parts[1], 16)); + } catch (NumberFormatException e) { + throw new NumberFormatException("invalid query id:" + e.getMessage()); + } + + return uniqueId; + } + public static String printId(final UUID id) { TUniqueId tUniqueId = new TUniqueId(id.getMostSignificantBits(), id.getLeastSignificantBits()); StringBuilder builder = new StringBuilder(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/util/ProfileManager.java b/fe/fe-core/src/main/java/org/apache/doris/common/util/ProfileManager.java index 6d5724665af..8fbcbce27cf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/common/util/ProfileManager.java +++ b/fe/fe-core/src/main/java/org/apache/doris/common/util/ProfileManager.java @@ -336,7 +336,12 @@ public class ProfileManager { } List<Future<TGetRealtimeExecStatusResponse>> futures = Lists.newArrayList(); - TUniqueId queryId = Util.parseTUniqueIdFromString(id); + TUniqueId queryId = null; + try { + queryId = DebugUtil.parseTUniqueIdFromString(id); + } catch (NumberFormatException e) { + LOG.warn("Failed to parse TUniqueId from string {} when fetch profile", id, e); + } List<QueryIdAndAddress> involvedBackends = Lists.newArrayList(); if (queryId != null) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java b/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java index a32b9f9b8cd..4e4c6a94735 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java +++ b/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java @@ -26,7 +26,6 @@ import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.qe.ConnectContext; import org.apache.doris.thrift.TFileCompressType; import org.apache.doris.thrift.TFileFormatType; -import org.apache.doris.thrift.TUniqueId; import com.google.common.base.Preconditions; import com.google.common.base.Strings; @@ -671,29 +670,6 @@ public class Util { return sw.toString(); } - // id is a String generated by DebugUtil.printId(TUniqueId) - public static TUniqueId parseTUniqueIdFromString(String id) { - if (Strings.isNullOrEmpty(id)) { - return null; - } - - String[] parts = id.split("-"); - if (parts.length != 2) { - return null; - } - - TUniqueId uniqueId = new TUniqueId(); - try { - uniqueId.setHi(Long.parseUnsignedLong(parts[0], 16)); - uniqueId.setLo(Long.parseUnsignedLong(parts[1], 16)); - } catch (Exception e) { - LOG.warn("Failed to parse TUniqueId from string {}", id, e); - return null; - } - - return uniqueId; - } - public static long sha256long(String str) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); 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 90bde60d06c..db14ff1d8e6 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 @@ -234,6 +234,7 @@ import java.io.IOException; import java.io.StringReader; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -1565,16 +1566,15 @@ public class StmtExecutor { } // Handle kill statement. - private void handleKill() throws DdlException { + private void handleKill() throws UserException { KillStmt killStmt = (KillStmt) parsedStmt; ConnectContext killCtx = null; int id = killStmt.getConnectionId(); String queryId = killStmt.getQueryId(); if (id == -1) { + // when killCtx == null, this means the query not in FE, + // then we just send kill signal to BE 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) { @@ -1582,7 +1582,28 @@ public class StmtExecutor { } } - if (context == killCtx) { + if (killCtx == null) { + TUniqueId tQueryId = null; + try { + tQueryId = DebugUtil.parseTUniqueIdFromString(queryId); + } catch (NumberFormatException e) { + throw new UserException(e.getMessage()); + } + LOG.info("kill query {}", queryId); + Collection<Backend> nodesToPublish = Env.getCurrentSystemInfo().getIdToBackend().values(); + for (Backend be : nodesToPublish) { + if (be.isAlive()) { + try { + Status cancelReason = new Status(TStatusCode.CANCELLED, "user kill query"); + BackendServiceProxy.getInstance() + .cancelPipelineXPlanFragmentAsync(be.getBrpcAddress(), tQueryId, + cancelReason); + } catch (Throwable t) { + LOG.info("send kill query {} rpc to be {} failed", queryId, be); + } + } + } + } else if (context == killCtx) { // Suicide context.setKilled(); } else { diff --git a/fe/fe-core/src/test/java/org/apache/doris/common/util/DebugUtilTest.java b/fe/fe-core/src/test/java/org/apache/doris/common/util/DebugUtilTest.java index bebe65cd2e0..54a3f4c388b 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/common/util/DebugUtilTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/common/util/DebugUtilTest.java @@ -19,10 +19,13 @@ package org.apache.doris.common.util; import org.apache.doris.common.DdlException; import org.apache.doris.common.Pair; +import org.apache.doris.thrift.TUniqueId; import org.junit.Assert; import org.junit.Test; +import java.util.UUID; + public class DebugUtilTest { @Test public void testGetUint() { @@ -97,4 +100,46 @@ public class DebugUtilTest { .contains("org.apache.doris.common.DdlException: errCode = 2, detailMessage = only one exception")); Assert.assertEquals("unknown", Util.getRootCauseStack(null)); } + + @Test + public void testParseIdFromString() { + // test null + TUniqueId nullTUniqueId = null; + try { + nullTUniqueId = DebugUtil.parseTUniqueIdFromString(null); + } catch (NumberFormatException e) { + Assert.assertTrue("invalid query id".equals(e.getMessage())); + } + Assert.assertTrue(nullTUniqueId == null); + + + try { + nullTUniqueId = DebugUtil.parseTUniqueIdFromString(""); + } catch (NumberFormatException e) { + Assert.assertTrue("invalid query id".equals(e.getMessage())); + } + Assert.assertTrue(nullTUniqueId == null); + + Assert.assertEquals(new TUniqueId(), DebugUtil.parseTUniqueIdFromString("0-0")); + + try { + nullTUniqueId = DebugUtil.parseTUniqueIdFromString("INVALID-STRING"); + } catch (NumberFormatException e) { + Assert.assertTrue(e.getMessage().contains("For input string")); + } + Assert.assertTrue(nullTUniqueId == null); + + for (int i = 0; i < 100; i++) { + UUID uuid = UUID.randomUUID(); + TUniqueId originTQueryId = new TUniqueId(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); + String originStrQueryId = DebugUtil.printId(originTQueryId); + + TUniqueId convertedTQueryId = DebugUtil.parseTUniqueIdFromString(originStrQueryId); + String convertedStrQueryId = DebugUtil.printId(convertedTQueryId); + + Assert.assertTrue(originTQueryId.hi == convertedTQueryId.hi); + Assert.assertTrue(originTQueryId.lo == convertedTQueryId.lo); + Assert.assertTrue(originStrQueryId.equals(convertedStrQueryId)); + } + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java b/fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java deleted file mode 100644 index 888f40fec0f..00000000000 --- a/fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java +++ /dev/null @@ -1,46 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package org.apache.doris.common.util; - -import org.apache.doris.thrift.TUniqueId; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.UUID; - -public class UtilTest { - @Test - public void testParseTUniqueIdFromString() { - // test normal - for (int i = 0; i < 10; i++) { - UUID uuid = UUID.randomUUID(); - TUniqueId tUID = new TUniqueId(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); - String strUID = DebugUtil.printId(tUID); - TUniqueId parsedTUniqueId = Util.parseTUniqueIdFromString(strUID); - Assert.assertEquals(tUID, parsedTUniqueId); - } - - // test null - Assert.assertNull(Util.parseTUniqueIdFromString(null)); - Assert.assertNull(Util.parseTUniqueIdFromString("")); - Assert.assertEquals(new TUniqueId(), Util.parseTUniqueIdFromString("0-0")); - Assert.assertNull(Util.parseTUniqueIdFromString("INVALID-STRING")); - Assert.assertNull(Util.parseTUniqueIdFromString("INVALID")); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org