[feature](analyze_cmd) add show-tablets-belong stmt for analyzing a batch of tablet-ids (#27994)

This commit is contained in:
Yulei-Yang
2023-12-06 15:59:00 +08:00
committed by GitHub
parent 994c5c6f6e
commit fa5096f510
7 changed files with 312 additions and 1 deletions

View File

@ -0,0 +1,94 @@
// 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.analysis;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.UserException;
import org.apache.doris.qe.ShowResultSetMetaData;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* ShowTabletsBelongStmt is used to show information of tables which tablets are belonged to
* syntax:
* SHOW TABLETS BELONG tablet_ids
*/
public class ShowTabletsBelongStmt extends ShowStmt {
private List<Long> tabletIds;
private static final ImmutableList<String> TITLE_NAMES = new ImmutableList.Builder<String>()
.add("DbName")
.add("TableName")
.add("TableSize")
.add("PartitionNum")
.add("BucketNum")
.add("ReplicaCount")
.add("TabletIds")
.build();
public ShowTabletsBelongStmt(List<Long> tabletIds) {
this.tabletIds = tabletIds;
}
public List<Long> getTabletIds() {
return tabletIds;
}
@Override
public void analyze(Analyzer analyzer) throws UserException {
if (tabletIds == null || tabletIds.isEmpty()) {
throw new UserException("Please supply at least one tablet id");
}
}
@Override
public ShowResultSetMetaData getMetaData() {
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
for (String title : TITLE_NAMES) {
builder.addColumn(new Column(title, ScalarType.createVarchar(128)));
}
return builder.build();
}
@Override
public RedirectStatus getRedirectStatus() {
return RedirectStatus.FORWARD_NO_SYNC;
}
@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("SHOW TABLETS BELONG ");
for (long tabletId : tabletIds) {
sb.append(tabletId);
sb.append(", ");
}
String tmp = sb.toString();
return tmp.substring(tmp.length() - 1);
}
@Override
public String toString() {
return toSql();
}
}

View File

@ -97,6 +97,7 @@ import org.apache.doris.analysis.ShowTableStatsStmt;
import org.apache.doris.analysis.ShowTableStatusStmt;
import org.apache.doris.analysis.ShowTableStmt;
import org.apache.doris.analysis.ShowTabletStmt;
import org.apache.doris.analysis.ShowTabletsBelongStmt;
import org.apache.doris.analysis.ShowTransactionStmt;
import org.apache.doris.analysis.ShowTrashDiskStmt;
import org.apache.doris.analysis.ShowTrashStmt;
@ -168,6 +169,7 @@ import org.apache.doris.common.proc.TrashProcDir;
import org.apache.doris.common.proc.TrashProcNode;
import org.apache.doris.common.profile.ProfileTreeNode;
import org.apache.doris.common.profile.ProfileTreePrinter;
import org.apache.doris.common.util.DebugUtil;
import org.apache.doris.common.util.ListComparator;
import org.apache.doris.common.util.LogBuilder;
import org.apache.doris.common.util.LogKey;
@ -237,6 +239,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -417,6 +420,8 @@ public class ShowExecutor {
handleShowCreateCatalog();
} else if (stmt instanceof ShowAnalyzeStmt) {
handleShowAnalyze();
} else if (stmt instanceof ShowTabletsBelongStmt) {
handleShowTabletsBelong();
} else if (stmt instanceof AdminCopyTabletStmt) {
handleCopyTablet();
} else if (stmt instanceof ShowCatalogRecycleBinStmt) {
@ -2704,6 +2709,62 @@ public class ShowExecutor {
resultSet = new ShowResultSet(showStmt.getMetaData(), resultRows);
}
private void handleShowTabletsBelong() {
ShowTabletsBelongStmt showStmt = (ShowTabletsBelongStmt) stmt;
List<List<String>> rows = new ArrayList<>();
Env env = Env.getCurrentEnv();
TabletInvertedIndex invertedIndex = Env.getCurrentInvertedIndex();
Map<Long, HashSet<Long>> tableToTabletIdsMap = new HashMap<>();
for (long tabletId : showStmt.getTabletIds()) {
TabletMeta tabletMeta = invertedIndex.getTabletMeta(tabletId);
if (tabletMeta == null) {
continue;
}
Database db = env.getInternalCatalog().getDbNullable(tabletMeta.getDbId());
if (db == null) {
continue;
}
long tableId = tabletMeta.getTableId();
Table table = db.getTableNullable(tableId);
if (table == null) {
continue;
}
if (!tableToTabletIdsMap.containsKey(tableId)) {
tableToTabletIdsMap.put(tableId, new HashSet<>());
}
tableToTabletIdsMap.get(tableId).add(tabletId);
}
for (long tableId : tableToTabletIdsMap.keySet()) {
Table table = env.getInternalCatalog().getTableByTableId(tableId);
List<String> line = new ArrayList<>();
line.add(table.getDatabase().getFullName());
line.add(table.getName());
OlapTable olapTable = (OlapTable) table;
Pair<Double, String> tableSizePair = DebugUtil.getByteUint((long) olapTable.getDataSize());
String readableSize = DebugUtil.DECIMAL_FORMAT_SCALE_3.format(tableSizePair.first) + " "
+ tableSizePair.second;
line.add(readableSize);
line.add(new Long(olapTable.getPartitionNum()).toString());
int totalBucketNum = 0;
Set<String> partitionNamesSet = table.getPartitionNames();
for (String partitionName : partitionNamesSet) {
totalBucketNum += table.getPartition(partitionName).getDistributionInfo().getBucketNum();
}
line.add(new Long(totalBucketNum).toString());
line.add(new Long(olapTable.getReplicaCount()).toString());
line.add(tableToTabletIdsMap.get(tableId).toString());
rows.add(line);
}
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
}
private void handleCopyTablet() throws AnalysisException {
AdminCopyTabletStmt copyStmt = (AdminCopyTabletStmt) stmt;
long tabletId = copyStmt.getTabletId();