Skip to content

Commit

Permalink
[Enhancement] support show catalog recycle bin
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Satardekar <[email protected]>
  • Loading branch information
rohitrs1983 committed Sep 14, 2024
1 parent 68f7e63 commit c29ecac
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.starrocks.common.io.Text;
import com.starrocks.common.io.Writable;
import com.starrocks.common.util.FrontendDaemon;
import com.starrocks.common.util.TimeUtils;
import com.starrocks.persist.ImageWriter;
import com.starrocks.persist.RecoverInfo;
import com.starrocks.persist.gson.IForwardCompatibleObject;
Expand All @@ -68,12 +69,14 @@

import java.io.DataOutput;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;

Expand Down Expand Up @@ -903,6 +906,82 @@ protected void runAfterCatalogReady() {
}
}

public synchronized List<List<String>> getCatalogRecycleBinInfo() {
Map<Long, Long> dbToDataSize = Maps.newHashMap();
List<List<String>> tableInfos = Lists.newArrayList();
for (Map<Long, RecycleTableInfo> tableEntry : idToTableInfo.rowMap().values()) {
for (Map.Entry<Long, RecycleTableInfo> entry : tableEntry.entrySet()) {
List<String> info = Lists.newArrayList();
info.add("Table");
RecycleTableInfo tableInfo = entry.getValue();
Table table = tableInfo.getTable();
info.add(table.getName());
info.add(String.valueOf(tableInfo.getDbId()));
info.add(String.valueOf(entry.getKey()));
info.add("");
info.add(TimeUtils.longToTimeString(idToRecycleTime.get(entry.getKey())));
tableInfos.add(info);
}
}
// sort by Name, DropTime
tableInfos.sort((x, y) -> {
int nameRet = x.get(1).compareTo(y.get(1));
if (nameRet == 0) {
return x.get(5).compareTo(y.get(5));
} else {
return nameRet;
}
});

List<List<String>> partitionInfos = Lists.newArrayList();
for (Map.Entry<Long, RecyclePartitionInfo> entry : idToPartition.entrySet()) {
List<String> info = Lists.newArrayList();
info.add("Partition");
RecyclePartitionInfo partitionInfo = entry.getValue();
Partition partition = partitionInfo.getPartition();
info.add(partition.getName());
info.add(String.valueOf(partitionInfo.getDbId()));
info.add(String.valueOf(partitionInfo.getTableId()));
info.add(String.valueOf(entry.getKey()));
info.add(TimeUtils.longToTimeString(idToRecycleTime.get(entry.getKey())));
partitionInfos.add(info);
}
// sort by Name, DropTime
partitionInfos.sort((x, y) -> {
int nameRet = x.get(1).compareTo(y.get(1));
if (nameRet == 0) {
return x.get(5).compareTo(y.get(5));
} else {
return nameRet;
}
});

List<List<String>> dbInfos = Lists.newArrayList();
for (Map.Entry<Long, RecycleDatabaseInfo> entry : idToDatabase.entrySet()) {
List<String> info = Lists.newArrayList();
info.add("Database");
RecycleDatabaseInfo dbInfo = entry.getValue();
Database db = dbInfo.getDb();
info.add(db.getFullName());
info.add(String.valueOf(entry.getKey()));
info.add("");
info.add("");
info.add(TimeUtils.longToTimeString(idToRecycleTime.get(entry.getKey())));
dbInfos.add(info);
}
// sort by Name, DropTime
dbInfos.sort((x, y) -> {
int nameRet = x.get(1).compareTo(y.get(1));
if (nameRet == 0) {
return x.get(5).compareTo(y.get(5));
} else {
return nameRet;
}
});

return Stream.of(dbInfos, tableInfos, partitionInfos).flatMap(Collection::stream).collect(Collectors.toList());
}

@VisibleForTesting
synchronized boolean isContainedInidToRecycleTime(long id) {
return idToRecycleTime.get(id) != null;
Expand Down
8 changes: 8 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/qe/ShowExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
import com.starrocks.sql.ast.ShowBackupStmt;
import com.starrocks.sql.ast.ShowBasicStatsMetaStmt;
import com.starrocks.sql.ast.ShowBrokerStmt;
import com.starrocks.sql.ast.ShowCatalogRecycleBinStmt;
import com.starrocks.sql.ast.ShowCatalogsStmt;
import com.starrocks.sql.ast.ShowCharsetStmt;
import com.starrocks.sql.ast.ShowCollationStmt;
Expand Down Expand Up @@ -2675,6 +2676,13 @@ public ShowResultSet visitShowBackendBlackListStatement(ShowBackendBlackListStmt
return new ShowResultSet(statement.getMetaData(), rows);
}

@Override
public ShowResultSet visitShowCatalogRecycleBinStatement(ShowCatalogRecycleBinStmt statement, ConnectContext context) {
List<List<String>> rowSet = GlobalStateMgr.getCurrentState().getRecycleBin().getCatalogRecycleBinInfo().stream()
.sorted(Comparator.comparing(o -> o.get(0))).collect(Collectors.toList());
return new ShowResultSet(statement.getMetaData(), rowSet);
}

private List<List<String>> doPredicate(ShowStmt showStmt,
ShowResultSetMetaData showResultSetMetaData,
List<List<String>> rows) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
import com.starrocks.sql.ast.ShowBackupStmt;
import com.starrocks.sql.ast.ShowBasicStatsMetaStmt;
import com.starrocks.sql.ast.ShowBrokerStmt;
import com.starrocks.sql.ast.ShowCatalogRecycleBinStmt;
import com.starrocks.sql.ast.ShowCatalogsStmt;
import com.starrocks.sql.ast.ShowColumnStmt;
import com.starrocks.sql.ast.ShowComputeNodesStmt;
Expand Down Expand Up @@ -2518,6 +2519,24 @@ public Void visitCancelCompactionStatement(CancelCompactionStmt statement, Conne
return null;
}

@Override
public Void visitShowCatalogRecycleBinStatement(ShowCatalogRecycleBinStmt statement, ConnectContext context) {
try {
Authorizer.checkSystemAction(context.getCurrentUserIdentity(), context.getCurrentRoleIds(), PrivilegeType.OPERATE);
} catch (AccessDeniedException e) {
try {
Authorizer.checkSystemAction(context.getCurrentUserIdentity(), context.getCurrentRoleIds(), PrivilegeType.NODE);
} catch (AccessDeniedException e2) {
AccessDeniedException.reportAccessDenied(
InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME,
context.getCurrentUserIdentity(), context.getCurrentRoleIds(),
PrivilegeType.OPERATE.name() + " OR " + PrivilegeType.NODE.name(),
ObjectType.SYSTEM.name(), null);
}
}
return null;
}

private String getTableNameByRoutineLoadLabel(ConnectContext context,
String dbName, String labelName) {
RoutineLoadJob job = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,4 +1450,8 @@ default R visitSetVarHint(SetVarHint node, C context) {
default R visitUserVariableHint(UserVariableHint node, C context) {
return visitNode(node, context);
}

default R visitShowCatalogRecycleBinStatement(ShowCatalogRecycleBinStmt statement, C context) {
return visitShowStatement(statement, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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 com.starrocks.sql.ast;

import com.starrocks.catalog.Column;
import com.starrocks.catalog.ScalarType;
import com.starrocks.qe.ShowResultSetMetaData;
import com.starrocks.sql.parser.NodePosition;

public class ShowCatalogRecycleBinStmt extends ShowStmt {

private static final ShowResultSetMetaData META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column("Type", ScalarType.createVarchar(20)))
.addColumn(new Column("Name", ScalarType.createVarchar(256)))
.addColumn(new Column("DbId", ScalarType.createVarchar(30)))
.addColumn(new Column("TableId", ScalarType.createVarchar(30)))
.addColumn(new Column("PartitionId", ScalarType.createVarchar(30)))
.addColumn(new Column("DropTime", ScalarType.createVarchar(100)))
.build();

public ShowCatalogRecycleBinStmt() {
this(NodePosition.ZERO);
}

public ShowCatalogRecycleBinStmt(NodePosition pos) {
super(pos);
}

@Override
public ShowResultSetMetaData getMetaData() {
return META_DATA;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitShowCatalogRecycleBinStatement(this, context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@
import com.starrocks.sql.ast.ShowBackupStmt;
import com.starrocks.sql.ast.ShowBasicStatsMetaStmt;
import com.starrocks.sql.ast.ShowBrokerStmt;
import com.starrocks.sql.ast.ShowCatalogRecycleBinStmt;
import com.starrocks.sql.ast.ShowCatalogsStmt;
import com.starrocks.sql.ast.ShowCharsetStmt;
import com.starrocks.sql.ast.ShowCollationStmt;
Expand Down Expand Up @@ -7597,6 +7598,11 @@ public ParseNode visitCancelDisableDiskClause(StarRocksParser.CancelDisableDiskC

// ------------------------------------------- Util Functions -------------------------------------------

@Override
public ParseNode visitShowCatalogRecycleBinStatement(StarRocksParser.ShowCatalogRecycleBinStatementContext context) {
return new ShowCatalogRecycleBinStmt(createPos(context));
}

protected <T> List<T> visit(List<? extends ParserRuleContext> contexts, Class<T> clazz) {
return contexts.stream()
.map(this::visit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ statement
| showUserPropertyStatement
| showVariablesStatement
| showWarningStatement
| showCatalogRecycleBinStatement
| helpStatement

// authz Statement
Expand Down Expand Up @@ -1556,6 +1557,10 @@ showWarningStatement
: SHOW (WARNINGS | ERRORS) (limitElement)?
;

showCatalogRecycleBinStatement
: SHOW CATALOG RECYCLE BIN
;

helpStatement
: HELP identifierOrString
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ BASE: 'BASE';
BEGIN: 'BEGIN';
BETWEEN: 'BETWEEN';
BIGINT: 'BIGINT';
BIN: 'BIN';
BINARY: 'BINARY';
BITMAP: 'BITMAP';
BITMAP_UNION: 'BITMAP_UNION';
Expand Down Expand Up @@ -346,6 +347,7 @@ RANK: 'RANK';
READ: 'READ';
REASON: 'REASON';
RECOVER: 'RECOVER';
RECYCLE: 'RECYCLE';
REFRESH: 'REFRESH';
REWRITE: 'REWRITE';
REGEXP: 'REGEXP';
Expand Down Expand Up @@ -602,4 +604,4 @@ WS

ATTACHMENT
: DOUBLE_DOLLAR .*? DOUBLE_DOLLAR
;
;
Loading

0 comments on commit c29ecac

Please sign in to comment.