From cd33a9bd32220b5a09eaa2d93dd0979959f63aae Mon Sep 17 00:00:00 2001 From: Rijesh Kunhi Parambattu Date: Mon, 25 Nov 2024 20:51:15 +0530 Subject: [PATCH] [Enhancement] (nereids)implement adminCheckTabletsCommand in nereids --- .../org/apache/doris/nereids/DorisParser.g4 | 2 +- .../nereids/parser/LogicalPlanBuilder.java | 17 +++ .../doris/nereids/trees/plans/PlanType.java | 1 + .../commands/AdminCheckTabletsCommand.java | 103 ++++++++++++++++++ .../trees/plans/visitor/CommandVisitor.java | 5 + 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminCheckTabletsCommand.java diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index cee972a63d210b5..0b0418b65d76fb4 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -473,6 +473,7 @@ unsupportedCancelStatement supportedAdminStatement : ADMIN SHOW REPLICA DISTRIBUTION FROM baseTableRef #adminShowReplicaDistribution + | ADMIN CHECK tabletList properties=propertyClause? #adminCheckTablets ; supportedRecoverStatement @@ -492,7 +493,6 @@ unsupportedAdminStatement | ADMIN COMPACT TABLE baseTableRef wildWhere? #adminCompactTable | ADMIN SET (FRONTEND | (ALL FRONTENDS)) CONFIG (LEFT_PAREN propertyItemList RIGHT_PAREN)? ALL? #adminSetFrontendConfig - | ADMIN CHECK tabletList properties=propertyClause? #adminCheckTablets | ADMIN REBALANCE DISK (ON LEFT_PAREN backends+=STRING_LITERAL (COMMA backends+=STRING_LITERAL) RIGHT_PAREN)? #adminRebalanceDisk | ADMIN CANCEL REBALANCE DISK (ON LEFT_PAREN backends+=STRING_LITERAL diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 1a49c7ba65fa56b..705c64ffb08db86 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -49,6 +49,7 @@ import org.apache.doris.mtmv.MTMVRefreshTriggerInfo; import org.apache.doris.nereids.DorisParser; import org.apache.doris.nereids.DorisParser.AddConstraintContext; +import org.apache.doris.nereids.DorisParser.AdminCheckTabletsContext; import org.apache.doris.nereids.DorisParser.AdminShowReplicaDistributionContext; import org.apache.doris.nereids.DorisParser.AggClauseContext; import org.apache.doris.nereids.DorisParser.AggStateDataTypeContext; @@ -450,6 +451,7 @@ import org.apache.doris.nereids.trees.plans.algebra.Aggregate; import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; import org.apache.doris.nereids.trees.plans.commands.AddConstraintCommand; +import org.apache.doris.nereids.trees.plans.commands.AdminCheckTabletsCommand; import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand; import org.apache.doris.nereids.trees.plans.commands.AlterStorageVaultCommand; @@ -4688,4 +4690,19 @@ public LogicalPlan visitShowTabletsBelong(ShowTabletsBelongContext ctx) { }); return new ShowTabletsBelongCommand(tabletIdLists); } + + @Override + public LogicalPlan visitAdminCheckTablets(AdminCheckTabletsContext ctx) { + List tabletIdLists = new ArrayList<>(); + if (ctx.tabletList() != null) { + ctx.tabletList().tabletIdList.stream().forEach(tabletToken -> { + tabletIdLists.add(Long.parseLong(tabletToken.getText())); + }); + } + Map properties = ctx.properties != null + ? Maps.newHashMap(visitPropertyClause(ctx.properties)) + : Maps.newHashMap(); + return new AdminCheckTabletsCommand(tabletIdLists, properties); + } } + diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java index 20d46d612d02ac5..27239084dacfe8f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java @@ -132,6 +132,7 @@ public enum PlanType { PHYSICAL_WINDOW, // commands + ADMIN_CHECK_TABLETS_COMMAND, CREATE_POLICY_COMMAND, CREATE_TABLE_COMMAND, DELETE_COMMAND, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminCheckTabletsCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminCheckTabletsCommand.java new file mode 100644 index 000000000000000..994dbfc37998428 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminCheckTabletsCommand.java @@ -0,0 +1,103 @@ +// 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.common.AnalysisException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.util.PropertyAnalyzer; +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.commons.collections.CollectionUtils; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * drop user command + */ +public class AdminCheckTabletsCommand extends Command { + private final List tabletIds; + private final Map properties; + + /** + * Check Type in Properties. + * At present only CONSISTENCY is supported + */ + public enum CheckType { + CONSISTENCY; // check the consistency of replicas of tablet + public static CheckType getTypeFromString(String str) throws AnalysisException { + try { + return CheckType.valueOf(str.toUpperCase()); + } catch (Exception e) { + throw new AnalysisException("Unknown check type: " + str); + } + } + } + + /** + * constructor + */ + // ADMIN CHECK TABLET (id1, id2, ...) PROPERTIES ("type" = "check_consistency"); + public AdminCheckTabletsCommand(List tabletIds, Map properties) { + super(PlanType.ADMIN_CHECK_TABLETS_COMMAND); + this.tabletIds = tabletIds; + this.properties = properties; + } + + public List getTabletIds() { + return tabletIds; + } + + @Override + public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { + // check auth + if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN"); + } + + if (CollectionUtils.isEmpty(tabletIds)) { + throw new AnalysisException("Tablet id list is empty"); + } + + String typeStr = PropertyAnalyzer.analyzeType(properties); + if (typeStr == null) { + throw new AnalysisException("Should specify 'type' property"); + } + CheckType checkType = CheckType.getTypeFromString(typeStr); + + if (properties != null && !properties.isEmpty()) { + throw new AnalysisException("Unknown properties: " + properties.keySet()); + } + + if (Objects.requireNonNull(checkType) == CheckType.CONSISTENCY) { + Env.getCurrentEnv().getConsistencyChecker().addTabletsToCheck(getTabletIds()); + } + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitAdminCheckTabletsCommand(this, context); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java index 0af9ce4dc9c4ccf..f6c813359b9ec99 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java @@ -18,6 +18,7 @@ package org.apache.doris.nereids.trees.plans.visitor; import org.apache.doris.nereids.trees.plans.commands.AddConstraintCommand; +import org.apache.doris.nereids.trees.plans.commands.AdminCheckTabletsCommand; import org.apache.doris.nereids.trees.plans.commands.AlterJobStatusCommand; import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand; @@ -489,4 +490,8 @@ default R visitShowTabletsBelongCommand(ShowTabletsBelongCommand showTabletBelon default R visitCreateRoutineLoadCommand(CreateRoutineLoadCommand createRoutineLoadCommand, C context) { return visitCommand(createRoutineLoadCommand, context); } + + default R visitAdminCheckTabletsCommand(AdminCheckTabletsCommand adminCheckTabletsCommand, C context) { + return visitCommand(adminCheckTabletsCommand, context); + } }