Skip to content

Commit 57a1b3d

Browse files
committed
[fix](ivm) Match the changed base partition by incarnation and read the sync limit twice
Two ways the selection still picked the wrong partitions, both by reading less than what decides it. RECOVER PARTITION reports the recycled partition under the name it had, and a partition added after the drop can be live under that name again by then, with a different range. The membership check matched names only, so it accepted that replacement: the barrier went to the MV partitions of the replacement's range, and the recovered range -- whose rows no row binlog can repair -- was left without one, so the MV partition that partition sync then adds for it stays empty. The partition_sync_limit decides whether the mapping may be trusted, but was read only after the mapping was built. MV properties are mutated in between (ALTER MATERIALIZED VIEW ... SET is not generation guarded), so a limit cleared while the mapping was built read as no limit at all, and a change outside the window was answered with "no MV partition reads it": no barrier, no version bump. Key changes: - Require each changed base partition to be the live partition the caller supplied, by id, not merely a partition carrying the same name; a missing or mismatched incarnation rebuilds the whole MV, which is what the missing-name case already did. Only RECOVER can supply an incarnation that is not live: DROP, TRUNCATE and strict REPLACE all pass the id of the partition they are changing - Read the limit on both sides of the mapping construction, so a limit in effect either before or after it makes the mapping untrustworthy; a limit that is gone by the time the guard runs still describes the mapping it produced - End the recent partition of both sync-window tests two years out: the cutoff is now() truncated to the year and a partition is kept while its upper bound is after it, so a year rollover between building the DDL and marking the change used to put its upper bound exactly on the cutoff, drop it from the mapping, and let the test pass through the answer it exists to rule out Unit Test: - IvmBaselineRebuildTest.testRecoveredPartitionWhoseNameWasReusedRebuildsTheWholeMv: DROP p202001, add a different-range p202001, then RECOVER ... AS p202003. Without the id check it fails with expected: <true> but was: <false> -- no barrier at all - IvmBaselineRebuildTest.testTheSyncLimitIsReadOnBothSidesOfTheMapping pins both reads; with a single read it fails with "Wanted but not invoked ... exactly 1 interaction". The interleaving itself is not staged: the mapping is built with no injection point between the reads - Regression: mtmv_p0/ivm test_ivm_baseline_marker_scope, test_ivm_partition_baseline_rebuild, test_ivm_partition_baseline_rebuild_dup_keys, test_ivm_drop_referenced_column_baseline_rebuild, test_ivm_partition_drop_live_delta, test_ivm_replace_table_reconcile, test_ivm_replace_stream_cleanup and test_ivm_drop_column_fallback_reason green on a local cluster built from this head; IvmBaselineRebuildTest 36/36; checkstyle clean
1 parent 892b20c commit 57a1b3d

2 files changed

Lines changed: 111 additions & 16 deletions

File tree

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -803,19 +803,38 @@ private Optional<Set<String>> selectAffectedMvPartitions(BaseTableInfo baseTable
803803
}
804804
try {
805805
// A partition that is missing from the metadata here is invisible to the mapping as well, so
806-
// an empty answer below would be indistinguishable from "no MV partition reads it". The match
807-
// is deliberately exact: the mapping is keyed by the metadata's spelling, so a name that only
808-
// differs in case must take the whole-MV path too, or the lookup below would quietly select
809-
// nothing for a partition that some MV partition does read. Base tables that do not implement
810-
// getPartitionNames -- the external ones -- report no partition at all, so a partition change
811-
// on them always rebuilds the whole MV. That matches what the refresh-snapshot selection
812-
// answered for them, and the mapping has never been exercised for external tables (IVM does
813-
// not support them as base tables yet): revisit before taking the narrow path for them.
814-
if (!pctTable.getPartitionNames().containsAll(changedBasePartitions.keySet())) {
815-
return Optional.empty();
806+
// an empty answer below would be indistinguishable from "no MV partition reads it". It has to
807+
// be the partition the caller described, not merely one carrying the same name: RECOVER
808+
// PARTITION reports the recycled partition under its old name, and a partition added after
809+
// the drop may be live under that name again, with a different range. Matching on the name
810+
// alone would accept that replacement, select the MV partitions of its range, and leave the
811+
// recovered range -- whose rows no row binlog can repair -- without a barrier. The lookup is
812+
// by exact name, so a name that only differs in case takes the whole-MV path too. Base tables
813+
// that do not implement getPartition -- the external ones -- answer null for every name, so a
814+
// partition change on them always rebuilds the whole MV. That matches what the
815+
// refresh-snapshot selection answered for them, and the mapping has never been exercised for
816+
// external tables (IVM does not support them as base tables yet): revisit before taking the
817+
// narrow path for them.
818+
for (Entry<String, Long> changedBasePartition : changedBasePartitions.entrySet()) {
819+
Partition livePartition = pctTable.getPartition(changedBasePartition.getKey());
820+
if (livePartition == null || livePartition.getId() != changedBasePartition.getValue()) {
821+
return Optional.empty();
822+
}
816823
}
824+
// Whether a partition_sync_limit is in effect decides whether the mapping built below may be
825+
// trusted, and it is read on both sides of that construction. It has to be: the property is
826+
// mutable (ALTER MATERIALIZED VIEW ... SET is not generation guarded) and the mapping is built
827+
// from it, so a read taken on one side only can be the stale one. Reading it after the mapping
828+
// alone misses a limit cleared while the mapping was built -- the mapping is then the windowed
829+
// one and would be trusted; reading it before alone misses a limit set in that same window, for
830+
// the opposite reason. The two reads bracket exactly the construction, and a limit in effect on
831+
// either of them means the mapping that came out of it may carry a window.
832+
boolean partitionSyncLimitActiveBeforeMapping =
833+
MTMVPartitionUtil.isPartitionSyncLimitActive(mvProperties);
817834
Map<String, Map<MTMVRelatedTableIf, Set<String>>> partitionMappings =
818835
calculatePartitionMappings(Maps.newHashMap());
836+
boolean partitionSyncLimitActiveAfterMapping =
837+
MTMVPartitionUtil.isPartitionSyncLimitActive(mvProperties);
819838
Set<String> res = Sets.newHashSet();
820839
boolean pctTableMapped = false;
821840
// Every base partition this table's part of the mapping describes, which is what the selection
@@ -856,8 +875,10 @@ private Optional<Set<String>> selectAffectedMvPartitions(BaseTableInfo baseTable
856875
// change to be described, rather than only a non-empty selection, is what covers a change
857876
// that mixes a partition inside the window with one outside it: the inside half would
858877
// otherwise fill the selection and hide the missing half. Without a limit the mapping is
859-
// complete, and a partition it leaves out really is one no MV partition reads.
860-
if (MTMVPartitionUtil.isPartitionSyncLimitActive(mvProperties)
878+
// complete, and a partition it leaves out really is one no MV partition reads. Either of the
879+
// two reads above counts: a limit that was in effect while the mapping was built leaves it
880+
// incomplete even if the limit is gone by now.
881+
if ((partitionSyncLimitActiveBeforeMapping || partitionSyncLimitActiveAfterMapping)
861882
&& !mappedBasePartitions.containsAll(changedBasePartitions.keySet())) {
862883
LOG.info("Changed base partitions are outside the partition_sync_limit window and the MV may "
863884
+ "still hold their rows, rebuild the whole MV. baseTable={}, changedPartitions={}, "

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

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import org.apache.doris.job.extensions.mtmv.MTMVTask;
3434
import org.apache.doris.job.extensions.mtmv.MTMVTask.MTMVTaskTriggerMode;
3535
import org.apache.doris.job.extensions.mtmv.MTMVTaskContext;
36+
import org.apache.doris.mtmv.BaseTableInfo;
3637
import org.apache.doris.mtmv.MTMVAlterOpType;
38+
import org.apache.doris.mtmv.MTMVPartitionUtil;
3739
import org.apache.doris.mtmv.MTMVPlanUtil;
3840
import org.apache.doris.mtmv.MTMVRelation;
3941
import org.apache.doris.persist.AlterMTMV;
@@ -47,6 +49,8 @@
4749
import com.google.common.collect.Sets;
4850
import org.junit.jupiter.api.Assertions;
4951
import org.junit.jupiter.api.Test;
52+
import org.mockito.MockedStatic;
53+
import org.mockito.Mockito;
5054

5155
import java.time.LocalDate;
5256
import java.util.Collections;
@@ -178,7 +182,12 @@ public void testDropPartitionOutsideMvPartitionsMarksNothing() throws Exception
178182
public void testChangedPartitionOutsideTheSyncWindowRebuildsTheWholeMv() throws Exception {
179183
String db = "ivm_baseline_sync_window";
180184
String thisYear = LocalDate.now().withDayOfYear(1).toString();
181-
String nextYear = LocalDate.now().withDayOfYear(1).plusYears(1).toString();
185+
// The cutoff is now() truncated to the year, read when the marker runs, and a partition is kept
186+
// while its upper bound is after it. The recent partition therefore ends more than one year out:
187+
// a year boundary falling between building this DDL and marking the change would otherwise put
188+
// its upper bound exactly on the cutoff, drop it from the mapping, and let this test pass through
189+
// the "nothing was selected" answer it exists to rule out.
190+
String recentEnd = LocalDate.now().withDayOfYear(1).plusYears(2).toString();
182191
createDatabaseAndUse(db);
183192
createTable("CREATE TABLE " + db + ".ivm_base (\n"
184193
+ " dt date NOT NULL,\n"
@@ -189,7 +198,7 @@ public void testChangedPartitionOutsideTheSyncWindowRebuildsTheWholeMv() throws
189198
+ "PARTITION BY RANGE(dt) (\n"
190199
+ " PARTITION p202001 VALUES [('2020-01-01'), ('2020-02-01')),\n"
191200
+ " PARTITION p202002 VALUES [('2020-02-01'), ('2020-03-01')),\n"
192-
+ " PARTITION pThisYear VALUES [('" + thisYear + "'), ('" + nextYear + "'))\n"
201+
+ " PARTITION pThisYear VALUES [('" + thisYear + "'), ('" + recentEnd + "'))\n"
193202
+ ")\n"
194203
+ "DISTRIBUTED BY HASH(k1) BUCKETS 1\n"
195204
+ "PROPERTIES ('replication_num' = '1', 'binlog.enable' = 'true', 'binlog.format' = 'ROW')");
@@ -223,7 +232,12 @@ public void testChangedPartitionOutsideTheSyncWindowRebuildsTheWholeMv() throws
223232
public void testChangeThatMixesInWindowAndOutOfWindowPartitionsRebuildsTheWholeMv() throws Exception {
224233
String db = "ivm_baseline_sync_window_mixed";
225234
String thisYear = LocalDate.now().withDayOfYear(1).toString();
226-
String nextYear = LocalDate.now().withDayOfYear(1).plusYears(1).toString();
235+
// The cutoff is now() truncated to the year, read when the marker runs, and a partition is kept
236+
// while its upper bound is after it. The recent partition therefore ends more than one year out:
237+
// a year boundary falling between building this DDL and marking the change would otherwise put
238+
// its upper bound exactly on the cutoff, drop it from the mapping, and let this test pass through
239+
// the "nothing was selected" answer it exists to rule out.
240+
String recentEnd = LocalDate.now().withDayOfYear(1).plusYears(2).toString();
227241
createDatabaseAndUse(db);
228242
createTable("CREATE TABLE " + db + ".ivm_base (\n"
229243
+ " dt date NOT NULL,\n"
@@ -234,7 +248,7 @@ public void testChangeThatMixesInWindowAndOutOfWindowPartitionsRebuildsTheWholeM
234248
+ "PARTITION BY RANGE(dt) (\n"
235249
+ " PARTITION p202001 VALUES [('2020-01-01'), ('2020-02-01')),\n"
236250
+ " PARTITION p202002 VALUES [('2020-02-01'), ('2020-03-01')),\n"
237-
+ " PARTITION pThisYear VALUES [('" + thisYear + "'), ('" + nextYear + "'))\n"
251+
+ " PARTITION pThisYear VALUES [('" + thisYear + "'), ('" + recentEnd + "'))\n"
238252
+ ")\n"
239253
+ "DISTRIBUTED BY HASH(k1) BUCKETS 1\n"
240254
+ "PROPERTIES ('replication_num' = '1', 'binlog.enable' = 'true', 'binlog.format' = 'ROW')");
@@ -256,6 +270,40 @@ public void testChangeThatMixesInWindowAndOutOfWindowPartitionsRebuildsTheWholeM
256270
Assertions.assertTrue(mtmv.getIvmInfo().requiresCompleteBaselineRebuild());
257271
}
258272

273+
/**
274+
* The limit is read on both sides of the mapping the selection judges: the mapping is built under
275+
* whatever window the properties hold at that moment, and MV properties are mutable in between --
276+
* <code>ALTER MATERIALIZED VIEW ... SET</code> is not generation guarded, so a limit can be cleared
277+
* while the mapping is built. A read that happens only afterwards then sees no limit and trusts a
278+
* windowed mapping, and a change outside that window is answered with "no MV partition reads it",
279+
* which records no barrier at all. The read taken before the mapping is the one that cannot be
280+
* reconstructed afterwards, so this pins that the selection takes both.
281+
*
282+
* <p>The interleaving itself is not staged: the mapping is built with no injection point between the
283+
* two reads, so the test pins that both reads happen rather than a racy outcome.
284+
*/
285+
@Test
286+
public void testTheSyncLimitIsReadOnBothSidesOfTheMapping() throws Exception {
287+
String db = "ivm_baseline_sync_limit_both_reads";
288+
createPartitionedIvmTableAndPartitionedMv(db);
289+
MTMV mtmv = getMtmv(db);
290+
OlapTable baseTable = getBaseTable(db);
291+
executeSql("ALTER MATERIALIZED VIEW ivm_mv SET ('partition_sync_limit' = '1',"
292+
+ " 'partition_sync_time_unit' = 'YEAR')");
293+
294+
try (MockedStatic<MTMVPartitionUtil> partitionUtil = Mockito.mockStatic(MTMVPartitionUtil.class,
295+
Mockito.CALLS_REAL_METHODS)) {
296+
Assertions.assertTrue(mtmv.invalidateIvmBaseline(new BaseTableInfo(baseTable),
297+
Collections.singletonMap("p202001", baseTable.getPartition("p202001").getId())));
298+
partitionUtil.verify(() -> MTMVPartitionUtil.isPartitionSyncLimitActive(Mockito.any()),
299+
Mockito.times(2));
300+
}
301+
302+
// p202001 is outside the window while it is in effect, so its rows are described by no mapping
303+
// entry and only the limit can tell that apart from "no MV partition reads it".
304+
Assertions.assertTrue(mtmv.getIvmInfo().requiresCompleteBaselineRebuild());
305+
}
306+
259307
/**
260308
* The partition mapping is built from the MV's PCT tables only. A changed partition of a joined table
261309
* the MV's partition column does not reach is invisible to it, and missing such a change leaves rows
@@ -447,6 +495,32 @@ public void testRecoverAndDropKeepGlobalBrokenState() throws Exception {
447495
Assertions.assertTrue(mtmv.getIvmInfo().isBaselineRebuildRequired());
448496
}
449497

498+
/**
499+
* RECOVER reports the recycled partition under the name it had, and a partition added after the drop
500+
* can be live under that name again by then, with a different range. The change is then not the one
501+
* the mapping describes: the recovered range is the one whose rows have to come back, and its MV
502+
* partition -- which partition sync adds when the recovered partition returns -- is read from a base
503+
* partition that the replacement does not describe at all. Narrowing to the replacement's MV
504+
* partitions would leave that one out, and recovery emits no row binlog to fill it later, so the
505+
* whole MV has to be rebuilt.
506+
*/
507+
@Test
508+
public void testRecoveredPartitionWhoseNameWasReusedRebuildsTheWholeMv() throws Exception {
509+
String db = "ivm_recover_partition_name_reused";
510+
createPartitionedIvmTableAndPartitionedMv(db);
511+
MTMV mtmv = getMtmv(db);
512+
513+
executeSql("ALTER TABLE ivm_base DROP PARTITION p202001");
514+
clearBaselineRebuild(mtmv);
515+
// Live again under the dropped name, with a range no MV partition covers: the RECOVER below is
516+
// still about the recycled partition, not about this one.
517+
executeSql("ALTER TABLE ivm_base ADD PARTITION p202001 VALUES [('2020-04-01'), ('2020-05-01'))");
518+
519+
executeSql("RECOVER PARTITION p202001 AS p202003 FROM ivm_base");
520+
521+
Assertions.assertTrue(mtmv.getIvmInfo().requiresCompleteBaselineRebuild());
522+
}
523+
450524
@Test
451525
public void testAddPartitionDoesNotMarkBaselineRebuild() throws Exception {
452526
String db = "ivm_broken_add_partition";

0 commit comments

Comments
 (0)