|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.nereids.trees.plans.commands; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.Column; |
| 21 | +import org.apache.doris.catalog.Env; |
| 22 | +import org.apache.doris.catalog.ScalarType; |
| 23 | +import org.apache.doris.common.ErrorCode; |
| 24 | +import org.apache.doris.common.ErrorReport; |
| 25 | +import org.apache.doris.common.proc.TrashProcDir; |
| 26 | +import org.apache.doris.common.util.NetUtils; |
| 27 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
| 28 | +import org.apache.doris.nereids.trees.plans.PlanType; |
| 29 | +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; |
| 30 | +import org.apache.doris.qe.ConnectContext; |
| 31 | +import org.apache.doris.qe.ShowResultSet; |
| 32 | +import org.apache.doris.qe.ShowResultSetMetaData; |
| 33 | +import org.apache.doris.qe.StmtExecutor; |
| 34 | +import org.apache.doris.system.Backend; |
| 35 | + |
| 36 | +import com.google.common.collect.ImmutableMap; |
| 37 | +import com.google.common.collect.Lists; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +/** |
| 42 | + * show trash command |
| 43 | + */ |
| 44 | +public class ShowTrashCommand extends ShowCommand { |
| 45 | + private List<Backend> backends = Lists.newArrayList(); |
| 46 | + private String backendQuery; |
| 47 | + |
| 48 | + public ShowTrashCommand() { |
| 49 | + super(PlanType.SHOW_TRASH_COMMAND); |
| 50 | + } |
| 51 | + |
| 52 | + public ShowTrashCommand(String backendQuery) { |
| 53 | + super(PlanType.SHOW_TRASH_COMMAND); |
| 54 | + this.backendQuery = backendQuery; |
| 55 | + } |
| 56 | + |
| 57 | + public List<Backend> getBackends() { |
| 58 | + return backends; |
| 59 | + } |
| 60 | + |
| 61 | + public String getBackend() { |
| 62 | + return backendQuery; |
| 63 | + } |
| 64 | + |
| 65 | + public ShowResultSetMetaData getMetaData() { |
| 66 | + ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder(); |
| 67 | + for (String title : TrashProcDir.TITLE_NAMES) { |
| 68 | + builder.addColumn(new Column(title, ScalarType.createVarchar(30))); |
| 69 | + } |
| 70 | + return builder.build(); |
| 71 | + } |
| 72 | + |
| 73 | + private ShowResultSet handleShowTrash(String backendQuery) throws Exception { |
| 74 | + if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN) |
| 75 | + && !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), |
| 76 | + PrivPredicate.OPERATOR)) { |
| 77 | + ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN/OPERATOR"); |
| 78 | + } |
| 79 | + ImmutableMap<Long, Backend> backendsInfo = Env.getCurrentSystemInfo().getAllBackendsByAllCluster(); |
| 80 | + if (backendQuery == null || backendQuery.isEmpty()) { |
| 81 | + for (Backend backend : backendsInfo.values()) { |
| 82 | + this.backends.add(backend); |
| 83 | + } |
| 84 | + } else { |
| 85 | + for (Backend backend : backendsInfo.values()) { |
| 86 | + String backendStr = NetUtils.getHostPortInAccessibleFormat(backend.getHost(), |
| 87 | + backend.getHeartbeatPort()); |
| 88 | + if (backendQuery.equals(backendStr)) { |
| 89 | + this.backends.add(backend); |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + List<List<String>> infos = Lists.newArrayList(); |
| 95 | + TrashProcDir.getTrashInfo(backends, infos); |
| 96 | + return new ShowResultSet(getMetaData(), infos); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public <R, C> R accept(PlanVisitor<R, C> visitor, C context) { |
| 101 | + return visitor.visitShowTrashCommand(this, context); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { |
| 106 | + return handleShowTrash(backendQuery); |
| 107 | + } |
| 108 | +} |
| 109 | + |
0 commit comments