Skip to content

Commit

Permalink
[Refactor] Modify the parameter in Locker from database object to dat…
Browse files Browse the repository at this point in the history
…abase id (#50687)

Signed-off-by: HangyuanLiu <[email protected]>
  • Loading branch information
HangyuanLiu authored Sep 6, 2024
1 parent 2a31794 commit 519d0f3
Show file tree
Hide file tree
Showing 124 changed files with 822 additions and 789 deletions.
46 changes: 23 additions & 23 deletions fe/fe-core/src/main/java/com/starrocks/alter/AlterJobExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,29 @@ public Void visitAlterTableStatement(AlterTableStmt statement, ConnectContext co

if (statement.hasSchemaChangeOp()) {
Locker locker = new Locker();
locker.lockTableWithIntensiveDbLock(db, table.getId(), LockType.WRITE);
locker.lockTableWithIntensiveDbLock(db.getId(), table.getId(), LockType.WRITE);
try {
SchemaChangeHandler schemaChangeHandler = GlobalStateMgr.getCurrentState().getSchemaChangeHandler();
assert table instanceof OlapTable;
schemaChangeHandler.process(statement.getAlterClauseList(), db, (OlapTable) table);
} catch (UserException e) {
throw new AlterJobException(e.getMessage());
} finally {
locker.unLockTableWithIntensiveDbLock(db, table, LockType.WRITE);
locker.unLockTableWithIntensiveDbLock(db.getId(), table.getId(), LockType.WRITE);
}

isSynchronous = false;
} else if (statement.hasRollupOp()) {
Locker locker = new Locker();
locker.lockTableWithIntensiveDbLock(db, table.getId(), LockType.WRITE);
locker.lockTableWithIntensiveDbLock(db.getId(), table.getId(), LockType.WRITE);
try {
MaterializedViewHandler materializedViewHandler = GlobalStateMgr.getCurrentState().getAlterJobMgr()
.getMaterializedViewHandler();
Preconditions.checkState(table instanceof OlapTable);
ErrorReport.wrapWithRuntimeException(() ->
materializedViewHandler.process(statement.getAlterClauseList(), db, (OlapTable) table));
} finally {
locker.unLockTableWithIntensiveDbLock(db, table, LockType.WRITE);
locker.unLockTableWithIntensiveDbLock(db.getId(), table.getId(), LockType.WRITE);
}

isSynchronous = false;
Expand Down Expand Up @@ -246,7 +246,7 @@ public Void visitAlterMaterializedViewStatement(AlterMaterializedViewStmt stmt,
GlobalStateMgr.getCurrentState().getMaterializedViewMgr().rebuildMaintainMV(materializedView);
return null;
} finally {
locker.unLockDatabase(db, LockType.WRITE);
locker.unLockDatabase(db.getId(), LockType.WRITE);
}
}

Expand All @@ -267,25 +267,25 @@ public Void visitDropIndexClause(DropIndexClause clause, ConnectContext context)
@Override
public Void visitTableRenameClause(TableRenameClause clause, ConnectContext context) {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
ErrorReport.wrapWithRuntimeException(() ->
GlobalStateMgr.getCurrentState().getLocalMetastore().renameTable(db, table, clause));
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
return null;
}

@Override
public Void visitAlterTableCommentClause(AlterTableCommentClause clause, ConnectContext context) {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
ErrorReport.wrapWithRuntimeException(() -> GlobalStateMgr.getCurrentState().getLocalMetastore()
.alterTableComment(db, table, clause));
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
return null;
}
Expand All @@ -294,7 +294,7 @@ public Void visitAlterTableCommentClause(AlterTableCommentClause clause, Connect
public Void visitSwapTableClause(SwapTableClause clause, ConnectContext context) {
// must hold db write lock
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
OlapTable origTable = (OlapTable) table;
String origTblName = origTable.getName();
Expand Down Expand Up @@ -333,7 +333,7 @@ public Void visitSwapTableClause(SwapTableClause clause, ConnectContext context)
throw new AlterJobException(e.getMessage(), e);
}
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
}

Expand All @@ -352,11 +352,11 @@ public Void visitModifyTablePropertiesClause(ModifyTablePropertiesClause clause,
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_ENABLE_PERSISTENT_INDEX)) {
if (table.isCloudNativeTable()) {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
schemaChangeHandler.processLakeTableAlterMeta(clause, db, (OlapTable) table);
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}

isSynchronous = false;
Expand Down Expand Up @@ -389,7 +389,7 @@ public Void visitModifyTablePropertiesClause(ModifyTablePropertiesClause clause,
schemaChangeHandler.updateTableConstraint(db, tableName.getTbl(), properties);
} else {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
OlapTable olapTable = (OlapTable) table;
if (properties.containsKey(PropertyAnalyzer.PROPERTIES_COLOCATE_WITH)) {
Expand Down Expand Up @@ -442,7 +442,7 @@ public Void visitModifyTablePropertiesClause(ModifyTablePropertiesClause clause,
schemaChangeHandler.process(Lists.newArrayList(clause), db, olapTable);
}
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}

isSynchronous = false;
Expand Down Expand Up @@ -487,15 +487,15 @@ public Void visitModifyColumnClause(ModifyColumnClause clause, ConnectContext co
public Void visitColumnRenameClause(ColumnRenameClause clause, ConnectContext context) {
SchemaChangeHandler schemaChangeHandler = GlobalStateMgr.getCurrentState().getSchemaChangeHandler();
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
Set<String> modifiedColumns = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
modifiedColumns.add(clause.getColName());
ErrorReport.wrapWithRuntimeException(() ->
schemaChangeHandler.checkModifiedColumWithMaterializedViews((OlapTable) table, modifiedColumns));
GlobalStateMgr.getCurrentState().getLocalMetastore().renameColumn(db, table, clause);
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
return null;
}
Expand All @@ -521,12 +521,12 @@ public Void visitDropRollupClause(DropRollupClause clause, ConnectContext contex
@Override
public Void visitRollupRenameClause(RollupRenameClause clause, ConnectContext context) {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
ErrorReport.wrapWithRuntimeException(() ->
GlobalStateMgr.getCurrentState().getLocalMetastore().renameRollup(db, (OlapTable) table, clause));
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
return null;
}
Expand Down Expand Up @@ -609,7 +609,7 @@ public Void visitReplacePartitionClause(ReplacePartitionClause clause, ConnectCo
@Override
public Void visitPartitionRenameClause(PartitionRenameClause clause, ConnectContext context) {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
if (clause.getPartitionName().startsWith(ExpressionRangePartitionInfo.SHADOW_PARTITION_PREFIX)) {
throw new AlterJobException("Rename of shadow partitions is not allowed");
Expand All @@ -618,7 +618,7 @@ public Void visitPartitionRenameClause(PartitionRenameClause clause, ConnectCont
ErrorReport.wrapWithRuntimeException(() ->
GlobalStateMgr.getCurrentState().getLocalMetastore().renamePartition(db, table, clause));
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
return null;
}
Expand Down Expand Up @@ -646,11 +646,11 @@ public Void visitModifyPartitionClause(ModifyPartitionClause clause, ConnectCont
}

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
modifyPartitionsProperty(db, (OlapTable) table, partitionNames, properties);
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(table.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
} catch (DdlException | AnalysisException e) {
throw new AlterJobException(e.getMessage());
Expand Down
28 changes: 14 additions & 14 deletions fe/fe-core/src/main/java/com/starrocks/alter/AlterJobMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void processDropMaterializedView(DropMaterializedViewStmt stmt) throws Dd
throw e;
}
} finally {
locker.unLockDatabase(db, LockType.WRITE);
locker.unLockDatabase(db.getId(), LockType.WRITE);
}
}

Expand Down Expand Up @@ -278,14 +278,14 @@ public void replayAlterMaterializedViewBaseTableInfos(AlterMaterializedViewBaseT
}

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(mv.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(mv.getId()), LockType.WRITE);
try {
mv.replayAlterMaterializedViewBaseTableInfos(log);
} catch (Throwable e) {
LOG.warn("replay alter materialized-view status failed: {}", mv.getName(), e);
mv.setInactiveAndReason("replay alter status failed: " + e.getMessage());
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(mv.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(mv.getId()), LockType.WRITE);
}
}

Expand All @@ -300,14 +300,14 @@ public void replayAlterMaterializedViewStatus(AlterMaterializedViewStatusLog log
}

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(mv.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(mv.getId()), LockType.WRITE);
try {
alterMaterializedViewStatus(mv, log.getStatus(), true);
} catch (Throwable e) {
LOG.warn("replay alter materialized-view status failed: {}", mv.getName(), e);
mv.setInactiveAndReason("replay alter status failed: " + e.getMessage());
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(mv.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(mv.getId()), LockType.WRITE);
}
}

Expand All @@ -319,7 +319,7 @@ public void replayRenameMaterializedView(RenameMaterializedViewLog log) {
MaterializedView oldMaterializedView = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore()
.getTable(db.getId(), materializedViewId);
if (oldMaterializedView != null) {
try (AutoCloseableLock ignore = new AutoCloseableLock(new Locker(), db,
try (AutoCloseableLock ignore = new AutoCloseableLock(new Locker(), db.getId(),
Lists.newArrayList(oldMaterializedView.getId()), LockType.WRITE)) {
db.dropTable(oldMaterializedView.getName());
oldMaterializedView.setName(newMaterializedViewName);
Expand Down Expand Up @@ -359,7 +359,7 @@ public void replayChangeMaterializedViewRefreshScheme(ChangeMaterializedViewRefr
}

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(oldMaterializedView.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(oldMaterializedView.getId()), LockType.WRITE);
try {
final MaterializedView.MvRefreshScheme newMvRefreshScheme = new MaterializedView.MvRefreshScheme();
final MaterializedView.MvRefreshScheme oldRefreshScheme = oldMaterializedView.getRefreshScheme();
Expand Down Expand Up @@ -387,7 +387,7 @@ public void replayChangeMaterializedViewRefreshScheme(ChangeMaterializedViewRefr
LOG.warn("replay change materialized-view refresh scheme failed: {}",
oldMaterializedView.getName(), e);
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(oldMaterializedView.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(oldMaterializedView.getId()), LockType.WRITE);
}
}

Expand All @@ -405,7 +405,7 @@ public void replayAlterMaterializedViewProperties(short opCode, ModifyTablePrope
}

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(mv.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(mv.getId()), LockType.WRITE);
try {
TableProperty tableProperty = mv.getTableProperty();
if (tableProperty == null) {
Expand All @@ -419,7 +419,7 @@ public void replayAlterMaterializedViewProperties(short opCode, ModifyTablePrope
mv.setInactiveAndReason("replay failed: " + e.getMessage());
LOG.warn("replay alter materialized-view properties failed: {}", mv.getName(), e);
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(mv.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(mv.getId()), LockType.WRITE);
}
}

Expand Down Expand Up @@ -487,7 +487,7 @@ public void alterView(AlterViewInfo alterViewInfo) {
View view = (View) GlobalStateMgr.getCurrentState().getLocalMetastore().getTable(db.getId(), tableId);

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(view.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(view.getId()), LockType.WRITE);
try {
String viewName = view.getName();
view.setInlineViewDefWithSqlMode(inlineViewDef, alterViewInfo.getSqlMode());
Expand All @@ -505,7 +505,7 @@ public void alterView(AlterViewInfo alterViewInfo) {

LOG.info("replay modify view[{}] definition to {}", viewName, inlineViewDef);
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(view.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(view.getId()), LockType.WRITE);
}
}

Expand All @@ -515,7 +515,7 @@ public void replayModifyPartition(ModifyPartitionInfo info) {
.getTable(db.getId(), info.getTableId());

Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db, Lists.newArrayList(olapTable.getId()), LockType.WRITE);
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(olapTable.getId()), LockType.WRITE);
try {
PartitionInfo partitionInfo = olapTable.getPartitionInfo();
if (info.getDataProperty() != null) {
Expand All @@ -531,7 +531,7 @@ public void replayModifyPartition(ModifyPartitionInfo info) {
}
partitionInfo.setIsInMemory(info.getPartitionId(), info.isInMemory());
} finally {
locker.unLockTablesWithIntensiveDbLock(db, Lists.newArrayList(olapTable.getId()), LockType.WRITE);
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(olapTable.getId()), LockType.WRITE);
}
}

Expand Down
Loading

0 comments on commit 519d0f3

Please sign in to comment.