Skip to content

Commit

Permalink
[enhance](mtmv)Optimize MTMV lock logic (apache#44967) (apache#45076)
Browse files Browse the repository at this point in the history
  • Loading branch information
zddr authored Dec 6, 2024
1 parent ebb21ef commit 4f45fef
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 57 deletions.
46 changes: 27 additions & 19 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -3217,25 +3217,33 @@ public static void getSyncedDdlStmt(TableIf table, List<String> createTableStmt,
hidePassword, false, specificVersion, false, true);
}

public static String getMTMVDdl(MTMV mtmv) {
StringBuilder sb = new StringBuilder("CREATE MATERIALIZED VIEW ");
sb.append(mtmv.getName());
addMTMVCols(mtmv, sb);
sb.append("\n");
sb.append(mtmv.getRefreshInfo());
addMTMVKeyInfo(mtmv, sb);
addTableComment(mtmv, sb);
addMTMVPartitionInfo(mtmv, sb);
DistributionInfo distributionInfo = mtmv.getDefaultDistributionInfo();
sb.append("\n").append(distributionInfo.toSql());
// properties
sb.append("\nPROPERTIES (\n");
addOlapTablePropertyInfo(mtmv, sb, false, false, null);
addMTMVPropertyInfo(mtmv, sb);
sb.append("\n)");
sb.append("\nAS ");
sb.append(mtmv.getQuerySql());
return sb.toString();
public static String getMTMVDdl(MTMV mtmv) throws AnalysisException {
if (!mtmv.tryReadLock(1, TimeUnit.MINUTES)) {
throw new AnalysisException(
"get table read lock timeout, database=" + mtmv.getQualifiedDbName() + ",table=" + mtmv.getName());
}
try {
StringBuilder sb = new StringBuilder("CREATE MATERIALIZED VIEW ");
sb.append(mtmv.getName());
addMTMVCols(mtmv, sb);
sb.append("\n");
sb.append(mtmv.getRefreshInfo());
addMTMVKeyInfo(mtmv, sb);
addTableComment(mtmv, sb);
addMTMVPartitionInfo(mtmv, sb);
DistributionInfo distributionInfo = mtmv.getDefaultDistributionInfo();
sb.append("\n").append(distributionInfo.toSql());
// properties
sb.append("\nPROPERTIES (\n");
addOlapTablePropertyInfo(mtmv, sb, false, false, null);
addMTMVPropertyInfo(mtmv, sb);
sb.append("\n)");
sb.append("\nAS ");
sb.append(mtmv.getQuerySql());
return sb.toString();
} finally {
mtmv.readUnlock();
}
}

private static void addMTMVKeyInfo(MTMV mtmv, StringBuilder sb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public Pair<Map<String, Set<String>>, Map<String, String>> calculateDoublyPartit
Map<String, String> baseToMv = Maps.newHashMap();
Map<PartitionKeyDesc, Set<String>> relatedPartitionDescs = MTMVPartitionUtil
.generateRelatedPartitionDescs(mvPartitionInfo, mvProperties);
Map<String, PartitionItem> mvPartitionItems = getAndCopyPartitionItems();
Map<String, PartitionItem> mvPartitionItems = getAndCopyPartitionItemsWithoutLock();
for (Entry<String, PartitionItem> entry : mvPartitionItems.entrySet()) {
Set<String> basePartitionNames = relatedPartitionDescs.getOrDefault(entry.getValue().toPartitionKeyDesc(),
Sets.newHashSet());
Expand Down
20 changes: 12 additions & 8 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/OlapTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3029,19 +3029,23 @@ public Map<String, PartitionItem> getAndCopyPartitionItems() throws AnalysisExce
"get table read lock timeout, database=" + getQualifiedDbName() + ",table=" + getName());
}
try {
Map<String, PartitionItem> res = Maps.newHashMap();
for (Entry<Long, PartitionItem> entry : getPartitionInfo().getIdToItem(false).entrySet()) {
Partition partition = idToPartition.get(entry.getKey());
if (partition != null) {
res.put(partition.getName(), entry.getValue());
}
}
return res;
return getAndCopyPartitionItemsWithoutLock();
} finally {
readUnlock();
}
}

public Map<String, PartitionItem> getAndCopyPartitionItemsWithoutLock() throws AnalysisException {
Map<String, PartitionItem> res = Maps.newHashMap();
for (Entry<Long, PartitionItem> entry : getPartitionInfo().getIdToItem(false).entrySet()) {
Partition partition = idToPartition.get(entry.getKey());
if (partition != null) {
res.put(partition.getName(), entry.getValue());
}
}
return res;
}

@Override
public List<Column> getPartitionColumns(Optional<MvccSnapshot> snapshot) {
return getPartitionColumns();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,12 @@ public static Map<PartitionKeyDesc, Set<String>> generateRelatedPartitionDescs(M
}

public static List<Long> getPartitionsIdsByNames(MTMV mtmv, List<String> partitions) throws AnalysisException {
mtmv.readLock();
try {
List<Long> res = Lists.newArrayList();
for (String partitionName : partitions) {
Partition partition = mtmv.getPartitionOrAnalysisException(partitionName);
res.add(partition.getId());
}
return res;
} finally {
mtmv.readUnlock();
List<Long> res = Lists.newArrayList();
for (String partitionName : partitions) {
Partition partition = mtmv.getPartitionOrAnalysisException(partitionName);
res.add(partition.getId());
}

return res;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,13 @@ public void analyze(ConnectContext ctx) {
* @throws DdlException DdlException
* @throws IOException IOException
*/
public void run(StmtExecutor executor) throws DdlException, IOException {
public void run(StmtExecutor executor) throws DdlException, IOException, org.apache.doris.common.AnalysisException {
List<List<String>> rows = Lists.newArrayList();
Database db = Env.getCurrentInternalCatalog().getDbOrDdlException(mvName.getDb());
MTMV mtmv = (MTMV) db.getTableOrDdlException(mvName.getTbl());
mtmv.readLock();
try {
String mtmvDdl = Env.getMTMVDdl(mtmv);
rows.add(Lists.newArrayList(mtmv.getName(), mtmvDdl));
executor.handleShowCreateMTMVStmt(rows);
} finally {
mtmv.readUnlock();
}
String mtmvDdl = Env.getMTMVDdl(mtmv);
rows.add(Lists.newArrayList(mtmv.getName(), mtmvDdl));
executor.handleShowCreateMTMVStmt(rows);
}

/**
Expand Down
12 changes: 3 additions & 9 deletions fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1080,15 +1080,9 @@ private void handleShowCreateMTMV() throws AnalysisException {
.getDbOrAnalysisException(showStmt.getDb());
MTMV mtmv = (MTMV) db.getTableOrAnalysisException(showStmt.getTable());
List<List<String>> rows = Lists.newArrayList();

mtmv.readLock();
try {
String mtmvDdl = Env.getMTMVDdl(mtmv);
rows.add(Lists.newArrayList(mtmv.getName(), mtmvDdl));
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
} finally {
mtmv.readUnlock();
}
String mtmvDdl = Env.getMTMVDdl(mtmv);
rows.add(Lists.newArrayList(mtmv.getName(), mtmvDdl));
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
}

// Describe statement
Expand Down

0 comments on commit 4f45fef

Please sign in to comment.