-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Enhancement] (nereids)implement kill connection and query command in nereids #46882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
starocean999
merged 26 commits into
apache:master
from
yx-keith:kill-connection-and-query-in-nereids
Apr 3, 2025
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
43e23ee
implement kill connection and query command in nereids
yx-keith e1c629e
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith da6e1ed
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith b7e9418
Update CommandVisitor.java
yx-keith 05b09dd
Update LogicalPlanBuilder.java
yx-keith 645d63f
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith f27d40b
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith df512d7
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith be72daa
optimize checkstyle error
yx-keith 618c1b8
delete unsupportedDescribeStatement in DorisParser.g4
yx-keith 6210f85
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith 4c38019
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith 74c5f6b
add regression test for kill command
yx-keith 9c26c2c
optimize import
yx-keith 32f4667
optimize code
yx-keith a33d4a3
optimize kill query
yx-keith df904df
optimize regression test
yx-keith 24d1011
add UT for killConnection/QueryCommand
yx-keith 4b8867c
optimize code style
yx-keith 0803290
optimize UT
yx-keith cc13f4c
optimize UT test for killQueryCommand
yx-keith ea40aa8
optimize UT test
yx-keith 2e60ab9
optimize import
yx-keith 040c185
optimize UT test for KillConnectionCommand
yx-keith 159ba94
optimize code style
yx-keith aa4585b
Merge branch 'master' into kill-connection-and-query-in-nereids
yx-keith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...re/src/main/java/org/apache/doris/nereids/trees/plans/commands/KillConnectionCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // 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.nereids.trees.plans.commands; | ||
|
|
||
| import org.apache.doris.analysis.RedirectStatus; | ||
| import org.apache.doris.catalog.Env; | ||
| import org.apache.doris.common.ErrorCode; | ||
| import org.apache.doris.common.ErrorReport; | ||
| import org.apache.doris.mysql.privilege.PrivPredicate; | ||
| import org.apache.doris.nereids.trees.plans.PlanType; | ||
| import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; | ||
| import org.apache.doris.qe.ConnectContext; | ||
| import org.apache.doris.qe.StmtExecutor; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| /** | ||
| * kill connection command | ||
| */ | ||
|
|
||
| public class KillConnectionCommand extends KillCommand { | ||
| private static final Logger LOG = LogManager.getLogger(KillQueryCommand.class); | ||
| private final Integer connectionId; | ||
|
|
||
| public KillConnectionCommand(Integer connectionId) { | ||
| super(PlanType.KILL_CONNECTION_COMMAND); | ||
| this.connectionId = connectionId; | ||
| } | ||
|
|
||
| @Override | ||
| public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { | ||
| ConnectContext killCtx = ctx.getConnectScheduler().getContext(connectionId); | ||
| if (killCtx == null) { | ||
| ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_THREAD, id); | ||
| } | ||
|
|
||
| if (ctx == killCtx) { | ||
| // Suicide | ||
| ctx.setKilled(); | ||
| } else { | ||
| // Check auth | ||
| // Only user itself and user with admin priv can kill connection | ||
| if (!killCtx.getQualifiedUser().equals(ConnectContext.get().getQualifiedUser()) | ||
| && !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), | ||
| PrivPredicate.ADMIN)) { | ||
| ErrorReport.reportDdlException(ErrorCode.ERR_KILL_DENIED_ERROR, id); | ||
| } | ||
| killCtx.kill(true); | ||
| } | ||
yx-keith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ctx.getState().setOk(); | ||
| } | ||
|
|
||
| @Override | ||
| public <R, C> R accept(PlanVisitor<R, C> visitor, C context) { | ||
| return visitor.visitKillConnectionCommand(this, context); | ||
| } | ||
|
|
||
| @Override | ||
| public RedirectStatus toRedirectStatus() { | ||
| return RedirectStatus.NO_FORWARD; | ||
| } | ||
| } | ||
107 changes: 107 additions & 0 deletions
107
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/KillQueryCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // 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.nereids.trees.plans.commands; | ||
|
|
||
| import org.apache.doris.analysis.RedirectStatus; | ||
| import org.apache.doris.catalog.Env; | ||
| import org.apache.doris.common.ErrorCode; | ||
| import org.apache.doris.common.ErrorReport; | ||
| import org.apache.doris.common.Status; | ||
| import org.apache.doris.common.UserException; | ||
| import org.apache.doris.common.util.DebugUtil; | ||
| import org.apache.doris.mysql.privilege.PrivPredicate; | ||
| import org.apache.doris.nereids.trees.plans.PlanType; | ||
| import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; | ||
| import org.apache.doris.qe.ConnectContext; | ||
| import org.apache.doris.qe.StmtExecutor; | ||
| import org.apache.doris.rpc.BackendServiceProxy; | ||
| import org.apache.doris.system.Backend; | ||
| import org.apache.doris.thrift.TStatusCode; | ||
| import org.apache.doris.thrift.TUniqueId; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Collection; | ||
| /** | ||
| * kill query command | ||
| */ | ||
|
|
||
| public class KillQueryCommand extends KillCommand { | ||
| private static final Logger LOG = LogManager.getLogger(KillQueryCommand.class); | ||
| private final String queryId; | ||
|
|
||
| public KillQueryCommand(String queryId) { | ||
| super(PlanType.KILL_QUERY_COMMAND); | ||
| this.queryId = queryId; | ||
| } | ||
|
|
||
yx-keith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { | ||
| ConnectContext killCtx = ctx.getConnectScheduler().getContextWithQueryId(queryId); | ||
| // when killCtx == null, this means the query not in FE, | ||
| // then we just send kill signal to BE | ||
| 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() | ||
| .getAllBackendsByAllCluster().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 (ctx == killCtx) { | ||
| // Suicide | ||
| ctx.setKilled(); | ||
| } else { | ||
| // Check auth | ||
| // Only user itself and user with admin priv can kill connection | ||
| if (!killCtx.getQualifiedUser().equals(ConnectContext.get().getQualifiedUser()) | ||
| && !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), | ||
| PrivPredicate.ADMIN)) { | ||
| ErrorReport.reportDdlException(ErrorCode.ERR_KILL_DENIED_ERROR, id); | ||
| } | ||
| killCtx.kill(false); | ||
| } | ||
yx-keith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ctx.getState().setOk(); | ||
| } | ||
|
|
||
| @Override | ||
| public <R, C> R accept(PlanVisitor<R, C> visitor, C context) { | ||
| return visitor.visitKillQueryCommand(this, context); | ||
| } | ||
|
|
||
| @Override | ||
| public RedirectStatus toRedirectStatus() { | ||
| return RedirectStatus.NO_FORWARD; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...rc/test/java/org/apache/doris/nereids/trees/plans/commands/KillConnectionCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // 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.nereids.trees.plans.commands; | ||
|
|
||
| import org.apache.doris.catalog.Env; | ||
| import org.apache.doris.mysql.privilege.AccessControllerManager; | ||
| import org.apache.doris.mysql.privilege.PrivPredicate; | ||
| import org.apache.doris.qe.ConnectContext; | ||
| import org.apache.doris.qe.ConnectScheduler; | ||
| import org.apache.doris.qe.QueryState; | ||
| import org.apache.doris.qe.StmtExecutor; | ||
| import org.apache.doris.utframe.TestWithFeService; | ||
|
|
||
| import com.google.common.collect.Lists; | ||
| import mockit.Expectations; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class KillConnectionCommandTest extends TestWithFeService { | ||
| private ConnectContext connectContext; | ||
| private Env env; | ||
| private AccessControllerManager accessControllerManager; | ||
|
|
||
| private void runBefore() throws IOException { | ||
| connectContext = createDefaultCtx(); | ||
| env = Env.getCurrentEnv(); | ||
| accessControllerManager = env.getAccessManager(); | ||
| ConnectScheduler scheduler = new ConnectScheduler(10); | ||
| connectContext.setQualifiedUser("root"); | ||
| new Expectations() { | ||
| { | ||
| accessControllerManager.checkGlobalPriv(connectContext, PrivPredicate.ADMIN); | ||
| minTimes = 0; | ||
| result = true; | ||
|
|
||
| scheduler.listConnection("root", anyBoolean); | ||
| minTimes = 0; | ||
| result = Lists.newArrayList(connectContext.toThreadInfo(false)); | ||
| } | ||
| }; | ||
| connectContext.setConnectScheduler(scheduler); | ||
| scheduler.registerConnection(connectContext); | ||
| } | ||
|
|
||
| @Test | ||
| public void testKillConnection() throws Exception { | ||
| runBefore(); | ||
| StmtExecutor stmtExecutor = new StmtExecutor(connectContext, "select 1"); | ||
| stmtExecutor.execute(); | ||
| KillConnectionCommand command = new KillConnectionCommand(stmtExecutor.getContext().getConnectionId()); | ||
| Assertions.assertDoesNotThrow(() -> command.doRun(connectContext, stmtExecutor)); | ||
| Assertions.assertEquals(connectContext.getState().getStateType(), QueryState.MysqlStateType.OK); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.