Skip to content

Commit

Permalink
[Refactor] Unify the getTable interface to LocalMetastore (#50206)
Browse files Browse the repository at this point in the history
Signed-off-by: HangyuanLiu <[email protected]>
  • Loading branch information
HangyuanLiu committed Aug 29, 2024
1 parent 5c03743 commit c610121
Show file tree
Hide file tree
Showing 377 changed files with 9,264 additions and 8,523 deletions.
27 changes: 19 additions & 8 deletions fe/fe-core/src/main/java/com/starrocks/alter/AlterJobExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
import com.starrocks.sql.ast.TableRenameClause;
import com.starrocks.sql.ast.TruncatePartitionClause;
import com.starrocks.sql.ast.TruncateTableStmt;
import com.starrocks.sql.common.MetaUtils;
import com.starrocks.thrift.TStorageMedium;
import com.starrocks.thrift.TTabletMetaType;
import com.starrocks.thrift.TTabletType;
Expand Down Expand Up @@ -136,8 +135,11 @@ public Void visitAlterTableStatement(AlterTableStmt statement, ConnectContext co
TableName tableName = statement.getTbl();
this.tableName = tableName;

Database db = MetaUtils.getDatabase(context, tableName);
Table table = MetaUtils.getTable(tableName);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(tableName.getDb());
Table table = GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(tableName.getDb(), tableName.getTbl());
if (table == null) {
throw new SemanticException("Table %s is not found", tableName);
}

if (!(table.isOlapOrCloudNativeTable() || table.isMaterializedView())) {
throw new SemanticException("Do not support alter non-native table/materialized-view[" + tableName + "]");
Expand Down Expand Up @@ -190,8 +192,11 @@ public Void visitAlterTableStatement(AlterTableStmt statement, ConnectContext co
@Override
public Void visitAlterViewStatement(AlterViewStmt statement, ConnectContext context) {
TableName tableName = statement.getTableName();
Database db = MetaUtils.getDatabase(context, tableName);
Table table = MetaUtils.getTable(tableName);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(tableName.getDb());
Table table = GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(tableName.getDb(), tableName.getTbl());
if (table == null) {
throw new SemanticException("Table %s is not found", tableName);
}

if (table.getType() != Table.TableType.VIEW) {
throw new SemanticException("The specified table [" + tableName + "] is not a view");
Expand All @@ -208,15 +213,21 @@ public Void visitAlterViewStatement(AlterViewStmt statement, ConnectContext cont
public Void visitAlterMaterializedViewStatement(AlterMaterializedViewStmt stmt, ConnectContext context) {
// check db
final TableName mvName = stmt.getMvName();
Database db = MetaUtils.getDatabase(context, mvName);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(mvName.getDb());
if (db == null) {
throw new SemanticException("Database %s is not found", mvName.getCatalogAndDb());
}

Locker locker = new Locker();
if (!locker.lockDatabaseAndCheckExist(db, LockType.WRITE)) {
throw new AlterJobException("alter materialized failed. database:" + db.getFullName() + " not exist");
}

try {
Table table = MetaUtils.getTable(mvName);
Table table = GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(mvName.getDb(), mvName.getTbl());
if (table == null) {
throw new SemanticException("Table %s is not found", mvName);
}
if (!table.isMaterializedView()) {
throw new SemanticException("The specified table [" + mvName + "] is not a view");
}
Expand Down Expand Up @@ -288,7 +299,7 @@ public Void visitSwapTableClause(SwapTableClause clause, ConnectContext context)
OlapTable origTable = (OlapTable) table;
String origTblName = origTable.getName();
String newTblName = clause.getTblName();
Table newTbl = db.getTable(newTblName);
Table newTbl = GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getFullName(), newTblName);
if (newTbl == null || !(newTbl.isOlapOrCloudNativeTable() || newTbl.isMaterializedView())) {
throw new AlterJobException("Table " + newTblName + " does not exist or is not OLAP/LAKE table");
}
Expand Down
51 changes: 28 additions & 23 deletions fe/fe-core/src/main/java/com/starrocks/alter/AlterJobMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void stop() {
public void processDropMaterializedView(DropMaterializedViewStmt stmt) throws DdlException, MetaNotFoundException {
// check db
String dbName = stmt.getDbName();
Database db = GlobalStateMgr.getCurrentState().getDb(dbName);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbName);
if (db == null) {
ErrorReport.reportDdlException(ErrorCode.ERR_BAD_DB_ERROR, dbName);
}
Expand All @@ -144,7 +144,7 @@ public void processDropMaterializedView(DropMaterializedViewStmt stmt) throws Dd
try {
Table table = null;
boolean hasfindTable = false;
for (Table t : db.getTables()) {
for (Table t : GlobalStateMgr.getCurrentState().getLocalMetastore().getTables(db.getId())) {
if (t instanceof OlapTable) {
OlapTable olapTable = (OlapTable) t;
for (MaterializedIndexMeta mvMeta : olapTable.getVisibleIndexMetas()) {
Expand Down Expand Up @@ -231,7 +231,7 @@ public static QueryStatement recreateMVQuery(MaterializedView materializedView,
String createMvSql) {
// If we could parse the MV sql successfully, and the schema of mv does not change,
// we could reuse the existing MV
Optional<Database> mayDb = GlobalStateMgr.getCurrentState().mayGetDb(materializedView.getDbId());
Optional<Database> mayDb = GlobalStateMgr.getCurrentState().getLocalMetastore().mayGetDb(materializedView.getDbId());

// check database existing
String dbName = mayDb.orElseThrow(() ->
Expand Down Expand Up @@ -271,8 +271,8 @@ public static QueryStatement recreateMVQuery(MaterializedView materializedView,
public void replayAlterMaterializedViewBaseTableInfos(AlterMaterializedViewBaseTableInfosLog log) {
long dbId = log.getDbId();
long mvId = log.getMvId();
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
MaterializedView mv = (MaterializedView) db.getTable(mvId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
MaterializedView mv = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), mvId);
if (mv == null) {
return;
}
Expand All @@ -292,8 +292,9 @@ public void replayAlterMaterializedViewBaseTableInfos(AlterMaterializedViewBaseT
public void replayAlterMaterializedViewStatus(AlterMaterializedViewStatusLog log) {
long dbId = log.getDbId();
long tableId = log.getTableId();
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
MaterializedView mv = (MaterializedView) db.getTable(tableId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
MaterializedView mv = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore()
.getTable(db.getId(), tableId);
if (mv == null) {
return;
}
Expand All @@ -314,8 +315,9 @@ public void replayRenameMaterializedView(RenameMaterializedViewLog log) {
long dbId = log.getDbId();
long materializedViewId = log.getId();
String newMaterializedViewName = log.getNewMaterializedViewName();
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
MaterializedView oldMaterializedView = (MaterializedView) db.getTable(materializedViewId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
MaterializedView oldMaterializedView = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore()
.getTable(db.getId(), materializedViewId);
if (oldMaterializedView != null) {
try (AutoCloseableLock ignore = new AutoCloseableLock(new Locker(), db,
Lists.newArrayList(oldMaterializedView.getId()), LockType.WRITE)) {
Expand Down Expand Up @@ -344,12 +346,13 @@ private void updateTaskDefinition(MaterializedView materializedView) {
public void replayChangeMaterializedViewRefreshScheme(ChangeMaterializedViewRefreshSchemeLog log) {
long dbId = log.getDbId();
long id = log.getId();
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
if (db == null) {
return;
}

MaterializedView oldMaterializedView = (MaterializedView) db.getTable(id);
MaterializedView oldMaterializedView = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore()
.getTable(db.getId(), id);
if (oldMaterializedView == null) {
LOG.warn("Ignore change materialized view refresh scheme log because table:" + id + "is null");
return;
Expand Down Expand Up @@ -393,8 +396,9 @@ public void replayAlterMaterializedViewProperties(short opCode, ModifyTablePrope
long tableId = log.getTableId();
Map<String, String> properties = log.getProperties();

Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
MaterializedView mv = (MaterializedView) db.getTable(tableId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
MaterializedView mv = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore()
.getTable(db.getId(), tableId);
if (mv == null) {
LOG.warn("Ignore change materialized view properties og because table:" + tableId + "is null");
return;
Expand Down Expand Up @@ -428,9 +432,9 @@ public void replaySwapTable(SwapTableOperationLog log) {
long dbId = log.getDbId();
long origTblId = log.getOrigTblId();
long newTblId = log.getNewTblId();
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
OlapTable origTable = (OlapTable) db.getTable(origTblId);
OlapTable newTbl = (OlapTable) db.getTable(newTblId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
OlapTable origTable = (OlapTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), origTblId);
OlapTable newTbl = (OlapTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), newTblId);
LOG.debug("finish replay swap table {}-{} with table {}-{}", origTblId, origTable.getName(), newTblId,
newTbl.getName());
}
Expand All @@ -444,9 +448,9 @@ public void swapTableInternal(SwapTableOperationLog log) throws DdlException {
long dbId = log.getDbId();
long origTblId = log.getOrigTblId();
long newTblId = log.getNewTblId();
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
OlapTable origTable = (OlapTable) db.getTable(origTblId);
OlapTable newTbl = (OlapTable) db.getTable(newTblId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
OlapTable origTable = (OlapTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), origTblId);
OlapTable newTbl = (OlapTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), newTblId);

String origTblName = origTable.getName();
String newTblName = newTbl.getName();
Expand Down Expand Up @@ -479,8 +483,8 @@ public void alterView(AlterViewInfo alterViewInfo) {
List<Column> newFullSchema = alterViewInfo.getNewFullSchema();
String comment = alterViewInfo.getComment();

Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
View view = (View) db.getTable(tableId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
View view = (View) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(view.getId()), LockType.WRITE);
Expand All @@ -506,8 +510,9 @@ public void alterView(AlterViewInfo alterViewInfo) {
}

public void replayModifyPartition(ModifyPartitionInfo info) {
Database db = GlobalStateMgr.getCurrentState().getDb(info.getDbId());
OlapTable olapTable = (OlapTable) db.getTable(info.getTableId());
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(info.getDbId());
OlapTable olapTable = (OlapTable) GlobalStateMgr.getCurrentState().getLocalMetastore()
.getTable(db.getId(), info.getTableId());

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(olapTable.getId()), LockType.WRITE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public boolean cancel(String errMsg) {
*/
protected boolean checkTableStable(Database db) throws AlterCancelException {
long unHealthyTabletId = TabletInvertedIndex.NOT_EXIST_VALUE;
OlapTable tbl = (OlapTable) db.getTable(tableId);
OlapTable tbl = (OlapTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);
if (tbl == null) {
throw new AlterCancelException("Table " + tableId + " does not exist");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Void visitTableRenameClause(TableRenameClause clause, ConnectContext cont
String newMvName = clause.getNewTableName();
String oldMvName = table.getName();

if (db.getTable(newMvName) != null) {
if (GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getFullName(), newMvName) != null) {
throw new SemanticException("Materialized view [" + newMvName + "] is already used");
}
table.setName(newMvName);
Expand Down Expand Up @@ -349,7 +349,7 @@ public Void visitRefreshSchemeClause(RefreshSchemeClause refreshSchemeDesc, Conn
}
try {
// check
Table mv = db.getTable(materializedView.getId());
Table mv = GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), materializedView.getId());
if (mv == null) {
throw new DmlException(
"update meta failed. materialized view:" + materializedView.getName() + " not exist");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ protected void runPendingJob() throws AlterCancelException {
// send task to be
GlobalStateMgr globalStateMgr = GlobalStateMgr.getCurrentState();
List<Partition> partitions = Lists.newArrayList();
Database db = globalStateMgr.getDb(dbId);
Database db = globalStateMgr.getLocalMetastore().getDb(dbId);

if (db == null) {
throw new AlterCancelException("database does not exist, dbId:" + dbId);
}

LakeTable table = (LakeTable) db.getTable(tableName);
LakeTable table = (LakeTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getFullName(), tableName);
if (table == null) {
throw new AlterCancelException("table does not exist, tableName:" + tableName);
}
Expand Down Expand Up @@ -135,13 +135,13 @@ protected void runWaitingTxnJob() throws AlterCancelException {

@Override
protected void runRunningJob() throws AlterCancelException {
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
if (db == null) {
// database has been dropped
throw new AlterCancelException("database does not exist, dbId:" + dbId);
}

LakeTable table = (LakeTable) db.getTable(tableId);
LakeTable table = (LakeTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);
if (table == null) {
// table has been dropped
throw new AlterCancelException("table does not exist, tableId:" + tableId);
Expand Down Expand Up @@ -187,14 +187,14 @@ protected void runFinishedRewritingJob() throws AlterCancelException {
return;
}

Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
if (db == null) {
// database has been dropped
LOG.warn("database does not exist, dbId:" + dbId);
throw new AlterCancelException("database does not exist, dbId:" + dbId);
}

LakeTable table = (LakeTable) db.getTable(tableId);
LakeTable table = (LakeTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);
if (table == null) {
// table has been dropped
LOG.warn("table does not exist, tableId:" + tableId);
Expand All @@ -221,12 +221,12 @@ protected void runFinishedRewritingJob() throws AlterCancelException {
}

boolean readyToPublishVersion() throws AlterCancelException {
Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
if (db == null) {
// database has been dropped
throw new AlterCancelException("database does not exist, dbId:" + dbId);
}
LakeTable table = (LakeTable) db.getTable(tableId);
LakeTable table = (LakeTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);
if (table == null) {
// table has been dropped
throw new AlterCancelException("table does not exist, tableId:" + tableId);
Expand Down Expand Up @@ -422,9 +422,9 @@ protected boolean cancelImpl(String errMsg) {
return false;
}

Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
if (db != null) {
LakeTable table = (LakeTable) db.getTable(tableId);
LakeTable table = (LakeTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);
if (table != null) {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
Expand Down Expand Up @@ -480,14 +480,14 @@ public void replay(AlterJobV2 replayedJob) {
restoreState(other);
}

Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbId);
if (db == null) {
// database has been dropped
LOG.warn("database does not exist, dbId:" + dbId);
return;
}

LakeTable table = (LakeTable) db.getTable(tableId);
LakeTable table = (LakeTable) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);
if (table == null) {
return;
}
Expand Down
Loading

0 comments on commit c610121

Please sign in to comment.