hello-stephen commented on code in PR #71199:
URL: https://github.com/apache/doris/pull/71199#discussion_r4755411804


##########
fe/fe-core/src/main/java/org/apache/doris/tso/TSOTransactionTracker.java:
##########
@@ -146,6 +146,57 @@ long candidateCommittedTso(long currentTso, long 
durableCommittedTso) {
         return candidate;
     }
 
+    Map<Pair<Long, Long>, Long> liveCommittedTsos(long currentTso, 
Collection<Pair<Long, Long>> dbTablePairs) {
+        Preconditions.checkState(lock.isHeldByCurrentThread());
+        if (dbTablePairs.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        Map<Pair<Long, Long>, Long> result = new HashMap<>(dbTablePairs.size() 
* 2);
+        if (pendingByTso.isEmpty()) {
+            for (Pair<Long, Long> pair : dbTablePairs) {
+                result.put(pair, currentTso);
+            }
+            return result;
+        }
+        Map<Long, Set<Long>> remainingByDb = new HashMap<>();
+        for (Pair<Long, Long> pair : dbTablePairs) {
+            remainingByDb.computeIfAbsent(pair.first, k -> new 
HashSet<>()).add(pair.second);
+        }
+        for (PendingTransaction pending : pendingByTso.values()) {
+            if (pending.tso > currentTso || remainingByDb.isEmpty()) {
+                break;
+            }
+            Set<Long> tables = remainingByDb.get(pending.identity.first);
+            if (tables == null) {
+                continue;
+            }
+            long boundTso = pending.tso - 1;
+            List<Long> matched = null;
+            for (Long tableId : tables) {

Review Comment:
   [P2] Remove the nested table scan from the global TSO lock
   
   For each pending transaction, this scans every remaining requested table in 
the same database, even when the transaction touches only one unrelated table. 
In that case the requested set never shrinks. 
`TSOService.getLiveCommittedTsos()` holds the global allocator lock throughout, 
and the collector repeats the lookup for every chunk. For example, 10,000 
outstanding one-table commits on other tables plus 1,000 monitored tables 
require 10 million membership checks in one uninterrupted critical section; 
further chunks repeat that work while commit TSO allocation, transaction 
completion, and clock/status access wait on the same lock. Iterate each pending 
transaction's table IDs against the requested set, or maintain a per-table 
pending index under the lock, to preserve the watermark proof without this 
product scan.



##########
fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java:
##########
@@ -1950,6 +1965,57 @@ protected void 
unprotectUpdateInMemoryState(TransactionState transactionState, b
         if (isReplay) {
             updateTxnLabels(transactionState);
         }
+        updateCommittedTableIndex(transactionState);
+    }
+
+    private void updateCommittedTableIndex(TransactionState transactionState) {
+        long txnId = transactionState.getTransactionId();
+        Set<Long> prevTableIds = committedTableIdsByTxn.remove(txnId);
+        if (prevTableIds != null) {
+            for (Long tableId : prevTableIds) {
+                Set<Long> txnIds = committedTxnIdsByTable.get(tableId);
+                if (txnIds != null) {
+                    txnIds.remove(txnId);
+                    if (txnIds.isEmpty()) {
+                        committedTxnIdsByTable.remove(tableId);
+                    }
+                }
+            }
+        }
+        if (transactionState.getTransactionStatus() != 
TransactionStatus.COMMITTED) {
+            return;
+        }
+        Set<Long> tableIds = collectCommittedTableIds(transactionState);
+        if (tableIds.isEmpty()) {
+            return;
+        }
+        committedTableIdsByTxn.put(txnId, tableIds);
+        for (Long tableId : tableIds) {
+            committedTxnIdsByTable.computeIfAbsent(tableId, k -> new 
HashSet<>()).add(txnId);

Review Comment:
   [P1] Synchronize index writes on the actual commit and publish paths
   
   The new indexes are plain `HashMap`s, but their writers do not hold the 
transaction-manager write lock named in the comment. `commitTransaction()` 
releases its initial read lock before calling this code under 
`synchronized(transactionState)`; `finishTransaction()` does likewise. Two 
loads on different tables in the same database therefore hold different table 
locks and transaction monitors while modifying these shared maps. Concurrent 
structural changes can make `computeIfAbsent()` throw 
`ConcurrentModificationException` or lose entries. The commit path has already 
set COMMITTED at this point and has yet to enqueue/persist its journal entry or 
update catalog versions, so this can break ordinary writes even with monitoring 
disabled; missing entries also invalidate the collector's caught-up proof. 
Protect both index reads and writes with common synchronization, and cover 
concurrent commits/publication on different tables.



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