Skip to content

Commit 831d2c1

Browse files
authored
branch-2.1:[enhance](mtmv)cache table snapshot in refresh context (#50855) (#51493)
pick: #50855
1 parent 14cb4c9 commit 831d2c1

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionUtil.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,34 @@ private static boolean isSyncWithBaseTable(MTMVRefreshContext context, String mt
487487
if (!baseTable.needAutoRefresh()) {
488488
return true;
489489
}
490-
MTMVSnapshotIf baseTableCurrentSnapshot = baseTable.getTableSnapshot(context, Optional.empty());
490+
MTMVSnapshotIf baseTableCurrentSnapshot = getTableSnapshotFromContext(baseTable, context);
491491
return mtmv.getRefreshSnapshot()
492492
.equalsWithBaseTable(mtmvPartitionName, new BaseTableInfo(baseTable), baseTableCurrentSnapshot);
493493
}
494494

495+
/**
496+
* Try context first, then load via getTableSnapshot and cache
497+
*
498+
* @param mtmvRelatedTableIf Base table of materialized views
499+
* @param context The context data persists for the duration of either a refresh task
500+
* or a transparent rewrite operation
501+
* @return The snapshot information of the MTMV
502+
* @throws AnalysisException
503+
*/
504+
public static MTMVSnapshotIf getTableSnapshotFromContext(MTMVRelatedTableIf mtmvRelatedTableIf,
505+
MTMVRefreshContext context)
506+
throws AnalysisException {
507+
BaseTableInfo baseTableInfo = new BaseTableInfo(mtmvRelatedTableIf);
508+
Map<BaseTableInfo, MTMVSnapshotIf> baseTableSnapshotCache = context.getBaseTableSnapshotCache();
509+
if (baseTableSnapshotCache.containsKey(baseTableInfo)) {
510+
return baseTableSnapshotCache.get(baseTableInfo);
511+
}
512+
MTMVSnapshotIf baseTableCurrentSnapshot = mtmvRelatedTableIf.getTableSnapshot(context,
513+
Optional.empty());
514+
baseTableSnapshotCache.put(baseTableInfo, baseTableCurrentSnapshot);
515+
return baseTableCurrentSnapshot;
516+
}
517+
495518
/**
496519
* Generate updated snapshots of partitions to determine if they are synchronized
497520
*
@@ -538,7 +561,7 @@ private static MTMVRefreshPartitionSnapshot generatePartitionSnapshot(MTMVRefres
538561
continue;
539562
}
540563
refreshPartitionSnapshot.addTableSnapshot(baseTableInfo,
541-
((MTMVRelatedTableIf) table).getTableSnapshot(context, Optional.empty()));
564+
getTableSnapshotFromContext((MTMVRelatedTableIf) table, context));
542565
}
543566
return refreshPartitionSnapshot;
544567
}

fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRefreshContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
import org.apache.doris.catalog.MTMV;
2121
import org.apache.doris.common.AnalysisException;
2222

23+
import com.google.common.collect.Maps;
24+
2325
import java.util.Map;
2426
import java.util.Set;
2527

2628
public class MTMVRefreshContext {
2729
private MTMV mtmv;
2830
private Map<String, Set<String>> partitionMappings;
2931
private MTMVBaseVersions baseVersions;
32+
// Within the same context, repeated fetches of the same table's snapshot must return consistent values.
33+
// Hence, the results are cached at this stage.
34+
// The value is loaded/cached on the first fetch
35+
private Map<BaseTableInfo, MTMVSnapshotIf> baseTableSnapshotCache = Maps.newHashMap();
3036

3137
public MTMVRefreshContext(MTMV mtmv) {
3238
this.mtmv = mtmv;
@@ -44,6 +50,10 @@ public MTMVBaseVersions getBaseVersions() {
4450
return baseVersions;
4551
}
4652

53+
public Map<BaseTableInfo, MTMVSnapshotIf> getBaseTableSnapshotCache() {
54+
return baseTableSnapshotCache;
55+
}
56+
4757
public static MTMVRefreshContext buildContext(MTMV mtmv) throws AnalysisException {
4858
MTMVRefreshContext context = new MTMVRefreshContext(mtmv);
4959
context.partitionMappings = mtmv.calculatePartitionMappings();

fe/fe-core/src/test/java/org/apache/doris/mtmv/MTMVPartitionUtilTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.junit.Test;
3939

4040
import java.util.List;
41+
import java.util.Map;
4142
import java.util.Optional;
4243
import java.util.Set;
4344

@@ -92,6 +93,10 @@ public void setUp() throws NoSuchMethodException, SecurityException, AnalysisExc
9293
minTimes = 0;
9394
result = versions;
9495

96+
context.getBaseTableSnapshotCache();
97+
minTimes = 0;
98+
result = Maps.newHashMap();
99+
95100
mtmv.getPartitions();
96101
minTimes = 0;
97102
result = Lists.newArrayList(p1);
@@ -296,4 +301,20 @@ public void testIsTableNamelike() {
296301
Assert.assertFalse(MTMVPartitionUtil.isTableNamelike(new TableName("db1"), tableNameToCheck));
297302
Assert.assertFalse(MTMVPartitionUtil.isTableNamelike(new TableName("ctl1"), tableNameToCheck));
298303
}
304+
305+
@Test
306+
public void testGetTableSnapshotFromContext() throws AnalysisException {
307+
Map<BaseTableInfo, MTMVSnapshotIf> cache = Maps.newHashMap();
308+
new Expectations() {
309+
{
310+
context.getBaseTableSnapshotCache();
311+
minTimes = 0;
312+
result = cache;
313+
}
314+
};
315+
Assert.assertTrue(cache.isEmpty());
316+
MTMVPartitionUtil.getTableSnapshotFromContext(baseOlapTable, context);
317+
Assert.assertEquals(1, cache.size());
318+
Assert.assertEquals(baseSnapshotIf, cache.values().iterator().next());
319+
}
299320
}

0 commit comments

Comments
 (0)