TsukiokaKogane commented on code in PR #63850:
URL: https://github.com/apache/doris/pull/63850#discussion_r3340700172


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/stream/TableStreamManager.java:
##########
@@ -97,7 +128,129 @@ public Set<Long> getTableStreamIds(DatabaseIf db) {
         return result;
     }
 
-    public void fillTableStreamValuesMetadataResult(List<TRow> dataBatch) {
+    public void cleanupStalePartitionOffsets() {
+        List<Long> staleDbIds = new ArrayList<>();
+        List<Pair<Long, Long>> staleStreamIds = new ArrayList<>();
+        List<PruneTableStreamPartitionOffsetInfo.Entry> pruneEntries = new 
ArrayList<>();
+        for (Map.Entry<Long, Set<Long>> entry : copyDbStreamMap().entrySet()) {
+            Optional<Database> db = 
Env.getCurrentInternalCatalog().getDb(entry.getKey());
+            if (!db.isPresent()) {
+                staleDbIds.add(entry.getKey());
+                continue;
+            }
+            for (Long tableId : entry.getValue()) {
+                Optional<Table> table = db.get().getTable(tableId);
+                if (!table.isPresent()) {
+                    staleStreamIds.add(Pair.of(db.get().getId(), tableId));
+                    continue;
+                }
+                if (!(table.get() instanceof OlapTableStream)) {
+                    staleStreamIds.add(Pair.of(db.get().getId(), tableId));
+                    continue;
+                }
+                cleanupStalePartitionOffsets((OlapTableStream) 
table.get()).ifPresent(pruneEntries::add);
+            }
+        }
+        removeStaleDbAndStream(staleDbIds, staleStreamIds);
+        if (!pruneEntries.isEmpty()) {
+            
Env.getCurrentEnv().getEditLog().logPruneTableStreamPartitionOffsets(
+                    new PruneTableStreamPartitionOffsetInfo(pruneEntries));
+        }
+    }
+
+    private Optional<PruneTableStreamPartitionOffsetInfo.Entry> 
cleanupStalePartitionOffsets(OlapTableStream stream) {
+        if (stream.isDisabled() || stream.isStale()) {
+            return Optional.empty();
+        }
+        OlapTable baseTable = stream.getBaseTableNullable();
+        if (baseTable == null) {
+            return Optional.empty();
+        }
+        Set<Long> validPartitionIds;
+        if (!baseTable.tryReadLock(Table.TRY_LOCK_TIMEOUT_MS, 
TimeUnit.MILLISECONDS)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("skip cleaning stream {} because base table {} read 
lock is busy",
+                        stream.getName(), baseTable.getName());
+            }
+            return Optional.empty();
+        }
+        try {
+            if (baseTable.isDropped) {
+                return Optional.empty();
+            }
+            validPartitionIds = new HashSet<>(baseTable.getPartitionIds());
+        } finally {
+            baseTable.readUnlock();
+        }
+        if (!stream.tryWriteLockIfExist(Table.TRY_LOCK_TIMEOUT_MS, 
TimeUnit.MILLISECONDS)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("skip cleaning stream {} because stream write lock 
is busy", stream.getName());
+            }
+            return Optional.empty();
+        }
+        try {
+            if (stream.isDisabled() || stream.isStale()) {
+                return Optional.empty();
+            }
+            Set<Long> stalePartitionIds = 
stream.unprotectedCollectStalePartitionOffsetIds(validPartitionIds);
+            if (stalePartitionIds.isEmpty()) {
+                return Optional.empty();
+            }
+            int removedPartitionCount = 
stream.unprotectedPrunePartitionOffsets(stalePartitionIds);
+            if (removedPartitionCount > 0) {
+                LOG.info("cleaned {} stale partition offset entries from 
stream {}.{} ({})",
+                        removedPartitionCount, 
stream.getDatabase().getFullName(), stream.getName(), stream.getId());
+            }
+            return Optional.of(new PruneTableStreamPartitionOffsetInfo.Entry(
+                    stream.getDatabase().getId(), stream.getId(), 
stalePartitionIds));
+        } finally {
+            stream.writeUnlock();
+        }
+    }
+
+    public void 
replayPruneTableStreamPartitionOffsets(PruneTableStreamPartitionOffsetInfo 
info) {
+        if (info == null || info.getEntries() == null || 
info.getEntries().isEmpty()) {
+            return;
+        }
+        for (PruneTableStreamPartitionOffsetInfo.Entry entry : 
info.getEntries()) {
+            replayPruneTableStreamPartitionOffsets(entry);
+        }
+    }
+
+    private void 
replayPruneTableStreamPartitionOffsets(PruneTableStreamPartitionOffsetInfo.Entry
 entry) {
+        if (entry == null || entry.getPartitionIds() == null || 
entry.getPartitionIds().isEmpty()) {
+            return;
+        }
+        Optional<Database> db = 
Env.getCurrentInternalCatalog().getDb(entry.getDbId());
+        if (!db.isPresent()) {
+            LOG.info("skip replay pruning partition offsets because db {} does 
not exist", entry.getDbId());
+            return;
+        }
+        Optional<Table> table = db.get().getTable(entry.getStreamId());
+        if (!table.isPresent()) {
+            LOG.info("skip replay pruning partition offsets because stream 
{}.{} does not exist",
+                    entry.getDbId(), entry.getStreamId());
+            return;
+        }
+        if (!(table.get() instanceof OlapTableStream)) {
+            LOG.info("skip replay pruning partition offsets because table 
{}.{} is not an olap table stream",
+                    entry.getDbId(), entry.getStreamId());
+            return;
+        }
+        OlapTableStream stream = (OlapTableStream) table.get();
+        if (!stream.tryWriteLockIfExist(Table.TRY_LOCK_TIMEOUT_MS, 
TimeUnit.MILLISECONDS)) {

Review Comment:
   我理解这里的不一致是可以接受的 因为本身删的就是stale partition 这个没删掉也不会有比较大的影响



-- 
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