Skip to content

Commit f8ed33f

Browse files
authored
[fix](ivm) Refresh the surviving partitions after an IVM baseline rebuild (apache#67802)
### What problem does this PR solve? Dropping a base-table partition marks the IVM baseline as broken, because those rows disappear through metadata rather than through row binlog entries. Partition sync then removes the very MV partitions that barrier named, so `handlePendingIvmBaselineRebuild` intersected the barrier with the current MV partitions and always got an empty set. The refresh reported SUCCESS with refresh mode `NOT_REFRESH` and cleared the barrier without refreshing anything, leaving the delta that had accumulated on the surviving partitions unapplied: the task claimed the MV was up to date while the MV was missing rows. Reproduction: drop a base partition, insert a row into a surviving partition, then run `REFRESH MATERIALIZED VIEW ... INCREMENTAL FALLBACK`. The task succeeds, the expired MV partition is dropped, and the new row never reaches the MV. ### What changed - `MTMVTask.handlePendingIvmBaselineRebuild` is a pre-step now instead of a terminal branch. It rebuilds only the barrier partitions that still exist, releases the barrier, and then lets the normal attempt list run, so the surviving partitions catch up in the same task. Partitions the barrier named that partition sync already dropped need no rebuild: the partition and its IVM offsets are both gone, which is what satisfies those entries. - The attempt list is rewritten in place. A lone `COMPLETE` attempt rebuilds everything anyway and needs no pre-rebuild; a complete baseline rebuild rewrites the list to `COMPLETE` instead of executing it inline. - `MTMV.releaseIvmBaselineRebuild` releases the barrier under a `schemaChangeVersion` compare-and-clear and journals the new state immediately, like the other `ivmInfo` mutations. Without the release the IVM attempt that follows would be rejected by `validateIvmRefreshStart`; without the compare-and-clear a concurrent base-table change would lose the barrier entry it had just recorded. - `MTMVRelationManager.markIvmBaselineRebuild` takes an explicit all-partitions-changed flag instead of inferring it from an empty partition map. The COMPLETE paths are unchanged. They end the task, so the existing `addTaskResult` cleanup releases the barrier once the refresh succeeds. A strict `REFRESH ... INCREMENTAL`, and a scheduled refresh of an MV declared without `FALLBACK`, still fail while the barrier is pending, because that check runs before partition sync. Relaxing it is left to a follow-up PR. ### Test - `regression-test/suites/mtmv_p0/ivm/test_ivm_partition_drop_live_delta.groovy` (new): partitions added and dropped by hand with literal dates and no dynamic-partition scheduler, so the case is deterministic. Asserts the MV matches the base table right after `INCREMENTAL FALLBACK`. The scenario was reproduced on the pre-fix build, where the fallback task reported SUCCESS while the MV was missing the row written to the surviving partition. - `regression-test/suites/mtmv_p0/ivm/test_ivm_partition_baseline_rebuild_dup_keys.groovy` (new): covers the `TRUNCATE` path, where the affected MV partition survives and the pre-step really has something to rebuild. Runs on a duplicate-key MV with repeated identical rows, so a delta applied twice would show up as extra rows instead of hiding behind a unique key. - `test_ivm_partition_baseline_rebuild`, `test_ivm_partition_sync_retry`, `test_ivm_partition_unique_key`, `test_ivm_partition_window_limit`, `test_ivm_partition_window_remove`, `test_ivm_one_row_relation_partitioned`, `test_ivm_strict_failure_partition_atomicity` Trace issue: apache#65418
1 parent fe39f5b commit f8ed33f

8 files changed

Lines changed: 452 additions & 23 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,41 @@ public void invalidateIvmBaseline(BaseTableInfo baseTableInfo, Map<String, Long>
645645
editLogItem.await();
646646
}
647647

648+
/**
649+
* Release the IVM baseline barrier after the partitions it named have been rebuilt, or after
650+
* partition sync removed them (a dropped partition resolves its own entry: the partition and its
651+
* IVM offsets are both gone).
652+
*
653+
* <p>Guarded by schemaChangeVersion, like {@link #persistIvmBaselineGuard}: a base-table change
654+
* landing while the rebuild runs carries its own barrier entry, and a blind clear would swallow
655+
* it. Failing instead preserves that entry -- the next refresh rebuilds it together with the
656+
* partitions this task handled.
657+
*
658+
* <p>Journals the new state right away, like every other ivmInfo mutation here. A task that dies
659+
* before {@link #addTaskResult} would otherwise leave the release in memory only, and a restart
660+
* would resurrect the barrier from disk.
661+
*/
662+
public void releaseIvmBaselineRebuild(long expectedSchemaChangeVersion) throws JobException {
663+
EditLogItem editLogItem;
664+
writeMvLock();
665+
try {
666+
if (ivmInfo == null || !ivmInfo.isBaselineRebuildRequired()) {
667+
// Nothing to release: skip both the mutation and the journal entry. Any base-table
668+
// change that raced us in is still caught by validateIvmRefreshStart() below.
669+
return;
670+
}
671+
if (schemaChangeVersion != expectedSchemaChangeVersion) {
672+
throw new JobException("Base table metadata changed before IVM baseline refresh, mv="
673+
+ getName());
674+
}
675+
ivmInfo.clearBaselineRebuild();
676+
editLogItem = submitIvmInfoChange();
677+
} finally {
678+
writeMvUnlock();
679+
}
680+
editLogItem.await();
681+
}
682+
648683
public void persistIvmBaselineGuard(RefreshMode refreshMode, Set<String> baselinePartitions,
649684
long expectedSchemaChangeVersion) throws JobException {
650685
EditLogItem editLogItem;

‎fe/fe-core/src/main/java/org/apache/doris/job/extensions/mtmv/MTMVTask.java‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,7 @@ public void run() throws JobException {
319319
throw new JobException(e.getMessage(), e);
320320
}
321321
MTMVRefreshContext refreshContext = buildRefreshContext(tableIfs);
322-
if (handlePendingIvmBaselineRebuild(refreshContext, request, ctx)) {
323-
return;
324-
}
322+
handlePendingIvmBaselineRebuild(refreshContext, request, ctx, attempts);
325323
boolean disablePartitionRefresh = false;
326324
for (RefreshAttemptType attemptType : attempts) {
327325
switch (attemptType) {
@@ -558,28 +556,56 @@ private void executeCompleteAttempt(MTMVRefreshContext context, ConnectContext c
558556
executePartitionBasedRefresh(context, RefreshMode.COMPLETE, ctx);
559557
}
560558

561-
private boolean handlePendingIvmBaselineRebuild(MTMVRefreshContext context, RefreshRequest request,
562-
ConnectContext ctx)
559+
/**
560+
* Rebuild the MV partitions whose IVM baseline is broken, before the normal refresh runs.
561+
*
562+
* <p>This is a pre-step, not a terminal branch: the caller keeps running {@code attempts}
563+
* afterwards, so a broken baseline no longer skips the refresh entirely. The list is rewritten
564+
* in place when the baseline demands a different set of attempts.
565+
*
566+
* <p>Partition sync drops the MV partitions whose base partition disappeared, which is exactly
567+
* what the barrier recorded when that base partition was dropped. Those partitions are resolved
568+
* by the drop itself (the partition and its IVM offsets are both gone), so only the partitions
569+
* that still exist need a rebuild. The barrier is released either way, otherwise the IVM attempt
570+
* that follows would be rejected by {@link MTMV#validateIvmRefreshStart}.
571+
*/
572+
private void handlePendingIvmBaselineRebuild(MTMVRefreshContext context,
573+
RefreshRequest request, ConnectContext ctx, List<RefreshAttemptType> attempts)
563574
throws JobException, AnalysisException {
564575
if (!mtmv.isIvm() || request.refreshMode == RefreshMode.COMPLETE
565576
|| !mtmv.getIvmInfo().isBaselineRebuildRequired()) {
566-
return false;
577+
return;
567578
}
568579
ivmFallbackReason = IvmFailureReason.BINLOG_BROKEN.name();
569580
IvmInfo ivmInfo = mtmv.getIvmInfo();
581+
// A lone COMPLETE attempt rebuilds every partition anyway, so a partial pre-rebuild here
582+
// would be redundant; it also releases the barrier by itself once it succeeds.
583+
if (attempts.size() == 1 && attempts.get(0) == RefreshAttemptType.COMPLETE) {
584+
LOG.info("IVM baseline barrier is covered by the pending COMPLETE attempt, mv={}, taskId={}",
585+
mtmv.getName(), getTaskId());
586+
return;
587+
}
570588
if (ivmInfo.requiresCompleteBaselineRebuild()) {
571-
executeCompleteAttempt(context, ctx);
572-
return true;
589+
LOG.warn("IVM baseline requires a complete rebuild, mv={}, taskId={}. "
590+
+ "Continuing with COMPLETE refresh.", mtmv.getName(), getTaskId());
591+
attempts.clear();
592+
attempts.add(RefreshAttemptType.COMPLETE);
593+
return;
573594
}
574-
this.needRefreshPartitions = Lists.newArrayList(Sets.intersection(
595+
List<String> baselinePartitions = Lists.newArrayList(Sets.intersection(
575596
ivmInfo.getPendingBaselineRebuildPartitions(), mtmv.getPartitionNames()));
576-
this.needRefreshPartitions.sort(String::compareTo);
577-
this.refreshMode = generateRefreshMode(needRefreshPartitions);
578-
if (refreshMode == MTMVTaskRefreshMode.NOT_REFRESH) {
579-
return true;
597+
if (baselinePartitions.isEmpty()) {
598+
// Partition sync has already dropped every partition the barrier named, so there is
599+
// nothing left to rebuild. The surviving partitions are picked up by the attempts below.
600+
LOG.info("IVM baseline partitions were removed by partition sync, mv={}, taskId={}",
601+
mtmv.getName(), getTaskId());
602+
} else {
603+
baselinePartitions.sort(String::compareTo);
604+
this.needRefreshPartitions = baselinePartitions;
605+
this.refreshMode = generateRefreshMode(baselinePartitions);
606+
executePartitionBasedRefresh(context, RefreshMode.PARTITIONS, ctx);
580607
}
581-
executePartitionBasedRefresh(context, RefreshMode.PARTITIONS, ctx);
582-
return true;
608+
mtmv.releaseIvmBaselineRebuild(mtmvSchemaChangeVersion);
583609
}
584610

585611
private void validateIvmBaselineBeforePartitionSync(RefreshRequest request) throws JobException {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ public Set<BaseTableInfo> getMtmvsByBaseTableOneLevelAndFromView(BaseTableInfo t
8686
}
8787

8888
public void markIvmBaselineRebuild(BaseTableInfo baseTableInfo, String reason) {
89-
markIvmBaselineRebuild(baseTableInfo, Collections.emptyMap(), reason);
89+
markIvmBaselineRebuild(baseTableInfo, true, Collections.emptyMap(), reason);
9090
}
9191

9292
public void markIvmBaselineRebuildForPartitionChange(BaseTableInfo baseTableInfo,
9393
Map<String, Long> changedPartitions, String reason) {
9494
Preconditions.checkArgument(!changedPartitions.isEmpty(), "changed partitions can not be empty");
95-
markIvmBaselineRebuild(baseTableInfo, changedPartitions, reason);
95+
markIvmBaselineRebuild(baseTableInfo, false, changedPartitions, reason);
9696
}
9797

98-
private void markIvmBaselineRebuild(BaseTableInfo baseTableInfo,
98+
private void markIvmBaselineRebuild(BaseTableInfo baseTableInfo, boolean allPartitionsChanged,
9999
Map<String, Long> changedPartitions, String reason) {
100100
TableNameInfo baseTableName = new TableNameInfo(baseTableInfo.getCtlName(),
101101
baseTableInfo.getDbName(), baseTableInfo.getTableName());
@@ -115,7 +115,7 @@ private void markIvmBaselineRebuild(BaseTableInfo baseTableInfo,
115115
if (MTMVPartitionUtil.isTableExcluded(mtmv.getExcludedTriggerTables(), baseTableName)) {
116116
continue;
117117
}
118-
if (changedPartitions.isEmpty()) {
118+
if (allPartitionsChanged) {
119119
mtmv.invalidateIvmBaseline();
120120
} else {
121121
mtmv.invalidateIvmBaseline(baseTableInfo, changedPartitions);

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,46 @@ public void testPartitionsFallbackRebuildsPendingBaselineWithComplete() throws E
816816
Object request = Deencapsulation.invoke(task, "resolveRefreshRequest");
817817

818818
Deencapsulation.invoke(task, "validateIvmBaselineBeforePartitionSync", request);
819-
Assertions.assertTrue((Boolean) Deencapsulation.invoke(task, "handlePendingIvmBaselineRebuild",
820-
Mockito.mock(MTMVRefreshContext.class), request, new ConnectContext()));
821-
Assertions.assertEquals(MTMVTask.MTMVTaskRefreshMode.NOT_REFRESH,
822-
Deencapsulation.getField(task, "refreshMode"));
819+
List<Object> attempts = Lists.newArrayList();
820+
attempts.addAll(Deencapsulation.invoke(task, "buildAttempts", request, false));
821+
Assertions.assertEquals("[PARTITIONS, COMPLETE]", attempts.toString());
822+
823+
Deencapsulation.invoke(task, "handlePendingIvmBaselineRebuild",
824+
Mockito.mock(MTMVRefreshContext.class), request, new ConnectContext(), attempts);
825+
826+
// A pending COMPLETE rebuild reshapes the attempt list instead of rebuilding inline, so
827+
// PARTITIONS FALLBACK rebuilds the whole MV through the COMPLETE attempt it keeps.
828+
Assertions.assertEquals("[COMPLETE]", attempts.toString());
829+
Assertions.assertEquals(IvmFailureReason.BINLOG_BROKEN.name(),
830+
Deencapsulation.getField(task, "ivmFallbackReason"));
831+
// The barrier is released by the caller once the reshaped attempts have run.
832+
Mockito.verify(mtmv, Mockito.never()).releaseIvmBaselineRebuild(Mockito.anyLong());
833+
}
834+
835+
@Test
836+
public void testDroppedBaselinePartitionsReleaseBarrierWithoutRebuild() throws Exception {
837+
Mockito.when(mtmv.isIvm()).thenReturn(true);
838+
IvmInfo ivmInfo = new IvmInfo();
839+
ivmInfo.addPendingBaselineRebuildPartitions(Sets.newHashSet(poneName));
840+
Mockito.when(mtmv.getIvmInfo()).thenReturn(ivmInfo);
841+
// Partition sync already dropped the partition the barrier named, so nothing is left to
842+
// pre-rebuild and the surviving partitions catch up through the attempts themselves.
843+
Mockito.when(mtmv.getPartitionNames()).thenReturn(Sets.newHashSet(ptwoName));
844+
MTMVTask task = new MTMVTask(mtmv, relation, MTMVTaskContext.of(
845+
MTMVTaskTriggerMode.MANUAL, null, RefreshMode.PARTITIONS, true, null));
846+
Deencapsulation.setField(task, "mtmvSchemaChangeVersion", 7L);
847+
Object request = Deencapsulation.invoke(task, "resolveRefreshRequest");
848+
849+
List<Object> attempts = Lists.newArrayList();
850+
attempts.addAll(Deencapsulation.invoke(task, "buildAttempts", request, false));
851+
Deencapsulation.invoke(task, "handlePendingIvmBaselineRebuild",
852+
Mockito.mock(MTMVRefreshContext.class), request, new ConnectContext(), attempts);
853+
854+
Assertions.assertEquals("[PARTITIONS, COMPLETE]", attempts.toString());
855+
Assertions.assertNull(Deencapsulation.getField(task, "refreshMode"));
823856
Assertions.assertEquals(IvmFailureReason.BINLOG_BROKEN.name(),
824857
Deencapsulation.getField(task, "ivmFallbackReason"));
858+
Mockito.verify(mtmv).releaseIvmBaselineRebuild(7L);
825859
}
826860

827861
@Test
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !baseline_task --
3+
SUCCESS NONE NONE
4+
5+
-- !baseline_base --
6+
2026-01-10 1 10
7+
2026-01-10 1 10
8+
2026-02-10 3 30
9+
2026-02-10 3 30
10+
11+
-- !baseline_mv --
12+
2026-01-10 1 10
13+
2026-01-10 1 10
14+
2026-02-10 3 30
15+
2026-02-10 3 30
16+
17+
-- !strict_task --
18+
FAILED NOT_REFRESH BINLOG_BROKEN
19+
20+
-- !fallback_task --
21+
SUCCESS PARTIAL BINLOG_BROKEN
22+
23+
-- !fallback_base --
24+
2026-02-10 3 30
25+
2026-02-10 3 30
26+
2026-02-15 4 40
27+
28+
-- !fallback_mv --
29+
2026-02-10 3 30
30+
2026-02-10 3 30
31+
2026-02-15 4 40
32+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !baseline_task --
3+
SUCCESS NONE
4+
5+
-- !baseline_base --
6+
2026-01-10 1 10
7+
2026-02-10 2 20
8+
2026-03-10 3 30
9+
10+
-- !baseline_mv --
11+
2026-01-10 1 10
12+
2026-02-10 2 20
13+
2026-03-10 3 30
14+
15+
-- !strict_task --
16+
FAILED BINLOG_BROKEN
17+
18+
-- !fallback_task --
19+
SUCCESS BINLOG_BROKEN
20+
21+
-- !fallback_base --
22+
2026-02-10 2 20
23+
2026-02-15 4 40
24+
2026-03-10 3 30
25+
26+
-- !fallback_mv --
27+
2026-02-10 2 20
28+
2026-02-15 4 40
29+
2026-03-10 3 30
30+
31+
-- !resumed_task --
32+
SUCCESS NONE
33+
34+
-- !resumed_base --
35+
2026-02-10 2 20
36+
2026-02-15 4 40
37+
2026-03-10 3 30
38+
2026-03-15 5 50
39+
40+
-- !resumed_mv --
41+
2026-02-10 2 20
42+
2026-02-15 4 40
43+
2026-03-10 3 30
44+
2026-03-15 5 50
45+

0 commit comments

Comments
 (0)