github-actions[bot] commented on code in PR #68180:
URL: https://github.com/apache/doris/pull/68180#discussion_r4061183239


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java:
##########
@@ -682,33 +687,207 @@ public void invalidateIvmBaseline() {
         editLogItem.await();
     }
 
-    public void invalidateIvmBaseline(BaseTableInfo baseTableInfo, Map<String, 
Long> changedPartitions) {
+    /**
+     * Mark the MV partitions that may hold rows read from the changed base 
table partitions as needing a
+     * rebuild. When those partitions cannot be determined, the whole MV is 
marked instead.
+     */
+    /**
+     * @return whether a barrier was recorded. The caller reports the two 
outcomes differently: a change
+     *         that no MV partition reads leaves nothing to rebuild and must 
not be logged as one.
+     */
+    public boolean invalidateIvmBaseline(BaseTableInfo baseTableInfo, 
Map<String, Long> changedPartitions) {
+        // Computed before the MV lock is taken, not inside it: the mapping 
reads the partition items of the
+        // MV and of every PCT table, so it takes those tables' locks, and the 
MV lock has to stay a leaf
+        // (nothing may be acquired under it) the way the rest of this class 
assumes. The selection does not
+        // need to be atomic with the barrier it produces: the barrier is 
recorded under the lock below, and
+        // the names it carries are intersected with the live partition names 
when they are consumed
+        // (MTMVTask).
+        Optional<Set<String>> affectedMvPartitions = 
selectAffectedMvPartitions(baseTableInfo,
+                changedPartitions);
+        if (affectedMvPartitions.isPresent() && 
affectedMvPartitions.get().isEmpty()) {
+            // No MV partition reads any of the changed base partitions, so 
this change cannot leave
+            // anything behind here: there is no barrier to persist, and 
skipping the version bump
+            // keeps it from discarding the result of a task that is already 
running.
+            LOG.debug("No MV partition is affected by changed base partitions, 
mv={}, baseTable={}, "
+                    + "changedPartitions={}", name, baseTableInfo, 
changedPartitions);
+            return false;
+        }
         EditLogItem editLogItem;
         writeMvLock();
         try {
             if (ivmInfo == null) {
                 ivmInfo = new IvmInfo();
             }
-            if (mvPartitionInfo.getPartitionType() != 
MTMVPartitionType.SELF_MANAGE
-                    && mvPartitionInfo.getPctInfos().stream()
-                    .anyMatch(pctInfo -> 
pctInfo.getTableInfo().equals(baseTableInfo))) {
-                Optional<Set<String>> mvPartitionNames = 
refreshSnapshot.getMvPartitionNames(baseTableInfo,
-                        changedPartitions);
-                if (mvPartitionNames.isPresent()) {
-                    
ivmInfo.addPendingBaselineRebuildPartitions(mvPartitionNames.get());
-                } else {
-                    // Without a snapshot for every changed base partition, a 
PARTITIONS rebuild is unsafe.
-                    ivmInfo.requireCompleteBaselineRebuild();
-                }
-            } else {
+            if (!affectedMvPartitions.isPresent()) {
+                // A narrower rebuild could leave a partition holding rows of 
the changed base partition
+                // untouched, and those rows cannot be repaired later: the 
change emitted no row binlog.
                 ivmInfo.requireCompleteBaselineRebuild();
+            } else {
+                
ivmInfo.addPendingBaselineRebuildPartitions(affectedMvPartitions.get());
             }
             schemaChangeVersion++;
             editLogItem = submitIvmInfoChange();
         } finally {
             writeMvUnlock();
         }
         editLogItem.await();
+        return true;
+    }
+
+    /**
+     * Select the MV partitions that may hold rows read from the changed base 
table partitions.
+     *
+     * <p>This asks which MV partitions read the changed base partitions at 
all, instead of (as the
+     * refresh snapshot based selection did) which of them had already seen 
them. The snapshot is a lower
+     * bound that is allowed to lag: a base partition that was added after the 
snapshot was captured never
+     * appears in it, so it can report "this partition never read the changed 
base partition" about a
+     * partition that does hold its rows. Missing a partition here is not 
repaired by a later refresh --
+     * dropping or truncating a base partition emits no row binlog, so the 
incremental path never learns
+     * about those orphan rows and they stay in the MV forever.
+     *
+     * <p>Three cases have no answer in the mapping, and each of them must 
rebuild the whole MV instead:
+     * a SELF_MANAGE MV (the mapping API answers nothing for it, although its 
single partition reads every
+     * base partition); a base table that is not one of the MV's PCT tables 
(the mapping is seeded from
+     * {@code getPctTables()} and never gains a table later, so a joined 
partition table that the MV's
+     * partition column does not reach is not described at all); and a changed 
partition that is not in
+     * the base table's metadata right now, which is how RECOVER PARTITION 
arrives here -- it marks before
+     * the partition is added back, so at this point the partition is still in 
the recycle bin.
+     *
+     * <p>Locking is the fourth way to end up rebuilding everything, but it is 
contention rather than a
+     * property of the MV: the tables whose partition items the mapping reads 
are locked with a bounded
+     * tryLock, and the MV is rebuilt only while one of them is being written. 
See the comment at that
+     * loop.
+     *
+     * <p>An empty result is meaningful, on the other hand: the mapping lists 
every base partition read by
+     * the MV, so a changed base partition that no MV partition maps to is 
read by none of them.
+     *
+     * @param changedBasePartitions base partition name to partition id, never 
empty
+     * @return {@link Optional#empty()} when the affected MV partitions cannot 
be determined, otherwise the
+     *         (possibly empty) set of MV partition names that must be rebuilt
+     */
+    private Optional<Set<String>> selectAffectedMvPartitions(BaseTableInfo 
baseTableInfo,
+            Map<String, Long> changedBasePartitions) {
+        if (mvPartitionInfo.getPartitionType() == 
MTMVPartitionType.SELF_MANAGE) {
+            return Optional.empty();
+        }
+        MTMVRelatedTableIf pctTable = findPctTable(baseTableInfo);
+        if (pctTable == null) {
+            return Optional.empty();
+        }
+        // Computing the mapping reads the partition items of the MV and of 
every PCT table, which means
+        // taking their read locks. The caller already holds the changed 
table's write lock (a partition DDL
+        // marks before it releases it), so these other reads must not block: 
two partition DDLs on two PCT
+        // tables of this MV would otherwise each hold the write lock the 
other one needs, and acquiring in
+        // id order cannot break a cycle whose first lock is already held. 
They are taken with a bounded
+        // tryLock instead, the way the stream cleanup treats a busy table: a 
busy table means a writer is
+        // involved, and then the whole MV is rebuilt. The list is still 
sorted by id so that the acquisition
+        // order matches the rest of the code base.
+        List<TableIf> tablesToRead = 
Lists.newArrayListWithCapacity(mvPartitionInfo.getPctInfos().size() + 1);
+        tablesToRead.add(this);
+        for (BaseColInfo pctInfo : mvPartitionInfo.getPctInfos()) {
+            if (pctInfo.getTableInfo().equals(baseTableInfo)) {
+                continue;
+            }
+            try {
+                tablesToRead.add(MTMVUtil.getTable(pctInfo.getTableInfo()));
+            } catch (Exception e) {
+                LOG.warn("Failed to resolve PCT table {}, rebuild the whole 
MV. mv={}",
+                        pctInfo.getTableInfo(), name, e);
+                return Optional.empty();
+            }
+        }
+        tablesToRead.sort(Comparator.comparing(TableIf::getId));
+        if (!MetaLockUtils.tryReadLockTables(tablesToRead, 
Table.TRY_LOCK_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
+            LOG.warn("A PCT table is busy, rebuild the whole MV {} instead of 
selecting part of it", name);
+            return Optional.empty();
+        }
+        try {
+            // A partition that is missing from the metadata here is invisible 
to the mapping as well, so
+            // an empty answer below would be indistinguishable from "no MV 
partition reads it". The match
+            // is deliberately exact: the mapping is keyed by the metadata's 
spelling, so a name that only
+            // differs in case must take the whole-MV path too, or the lookup 
below would quietly select
+            // nothing for a partition that some MV partition does read. Base 
tables that do not implement
+            // getPartitionNames -- the external ones -- report no partition 
at all, so a partition change
+            // on them always rebuilds the whole MV. That matches what the 
refresh-snapshot selection
+            // answered for them, and the mapping has never been exercised for 
external tables (IVM does
+            // not support them as base tables yet): revisit before taking the 
narrow path for them.
+            if 
(!pctTable.getPartitionNames().containsAll(changedBasePartitions.keySet())) {
+                return Optional.empty();
+            }
+            Map<String, Map<MTMVRelatedTableIf, Set<String>>> 
partitionMappings =
+                    calculatePartitionMappings(Maps.newHashMap());
+            Set<String> res = Sets.newHashSet();
+            boolean pctTableMapped = false;
+            for (Entry<String, Map<MTMVRelatedTableIf, Set<String>>> mapping : 
partitionMappings.entrySet()) {
+                for (Entry<MTMVRelatedTableIf, Set<String>> tableMapping : 
mapping.getValue().entrySet()) {
+                    if (!tableMapping.getKey().equals(pctTable)) {
+                        continue;
+                    }
+                    pctTableMapped = true;
+                    if (!Collections.disjoint(tableMapping.getValue(), 
changedBasePartitions.keySet())) {
+                        res.add(mapping.getKey());
+                    }
+                }
+            }
+            // The mapping does not describe this base table at all. That 
contradicts the PCT check above,
+            // so it is safer to rebuild everything than to trust a selection 
that never saw the table --
+            // unless the MV has no partition of its own yet, which is the one 
shape where the missing
+            // entries are not a surprise: an MV without partitions holds no 
rows.
+            if (!pctTableMapped) {
+                if (getPartitionNames().isEmpty()) {
+                    LOG.info("MV has no partition yet, nothing can hold the 
changed base partitions. "
+                            + "baseTable={}, mv={}", baseTableInfo, name);
+                    return Optional.of(Sets.newHashSet());
+                }
+                LOG.warn("Base table is not described by the partition 
mapping, rebuild the whole MV. "
+                        + "baseTable={}, mv={}", baseTableInfo, name);
+                return Optional.empty();
+            }
+            // An empty selection is only trustworthy while the mapping covers 
every base partition. With a
+            // partition_sync_limit in effect it does not: the window leaves 
out the partitions it dropped,

Review Comment:
   [P1] Handle mixed in-window and out-of-window changes. When 
`partition_sync_limit` is active, this check only falls back when the aggregate 
`res` is empty. A multi-partition TRUNCATE/REPLACE can pass both an in-window 
and an out-of-window partition: the in-window one makes `res` non-empty, while 
the filtered old partition contributes no MV name. The barrier then covers only 
the newer MV partition. If the window is widened before refresh, partition sync 
retains the old MV partition and its stale rows, and no incremental delta 
repairs them. Track whether every changed partition was represented (or 
conservatively require a complete rebuild when any is missing), and add a 
mixed-window regression test. This is distinct from the existing single 
changed-partition case, which is now covered by the empty-selection fallback.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to