@@ -501,7 +501,13 @@ public VisibleStateWaiter commitPreparedTransaction(long transactionId) throws S
501501 Span unprotectedCommitSpan = TraceManager .startSpan ("unprotectedCommitPreparedTransaction" , txnSpan );
502502
503503 // transaction state transform
504- boolean txnOperated = unprotectedCommitPreparedTransaction (copiedState , db );
504+ boolean txnOperated ;
505+ writeLock ();
506+ try {
507+ txnOperated = unprotectedCommitPreparedTransaction (copiedState , db );
508+ } finally {
509+ writeUnlock ();
510+ }
505511 if (!txnOperated ) {
506512 return null ;
507513 }
@@ -1115,7 +1121,7 @@ public boolean canTxnFinished(TransactionState txn, Set<Long> errReplicas, Set<L
11151121 return true ;
11161122 }
11171123
1118- public void finishTransaction (long transactionId , Set <Long > errorReplicaIds , long lockTimeoutMs )
1124+ public TransactionState finishTransaction (long transactionId , Set <Long > errorReplicaIds , long lockTimeoutMs )
11191125 throws StarRocksException {
11201126 TransactionState transactionState = getTransactionState (transactionId );
11211127 // add all commit errors and publish errors to a single set
@@ -1145,7 +1151,7 @@ public void finishTransaction(long transactionId, Set<Long> errorReplicaIds, lon
11451151 }
11461152 });
11471153
1148- return ;
1154+ return copiedState ;
11491155 } finally {
11501156 transactionState .writeUnlock ();
11511157 }
@@ -1224,8 +1230,10 @@ public void finishTransaction(long transactionId, Set<Long> errorReplicaIds, lon
12241230 "wait for publishing partition %d version %d. self version: %d. table %d" ,
12251231 physicalPartitionId , physicalPartition .getVisibleVersion () + 1 ,
12261232 partitionCommitInfo .getVersion (), tableId );
1227- copiedState .setErrorMsg (errMsg );
1228- return ;
1233+ // set errMsg to transactionState instead of copiedState,
1234+ // because copiedState will not be upserted in this case.
1235+ transactionState .setErrorMsg (errMsg );
1236+ return transactionState ;
12291237 }
12301238
12311239 if (table .isCloudNativeTableOrMaterializedView ()) {
@@ -1309,7 +1317,9 @@ public void finishTransaction(long transactionId, Set<Long> errorReplicaIds, lon
13091317 tablet .getId (), healthReplicaNum , quorumReplicaNum , tableId ,
13101318 physicalPartitionId ,
13111319 physicalPartition .getVisibleVersion () + 1 );
1312- copiedState .setErrorMsg (errMsg );
1320+ // set errMsg to transactionState instead of copiedState,
1321+ // because copiedState will not be upserted in this case.
1322+ transactionState .setErrorMsg (errMsg );
13131323 hasError = true ;
13141324 }
13151325
@@ -1344,7 +1354,7 @@ public void finishTransaction(long transactionId, Set<Long> errorReplicaIds, lon
13441354 "version not equal to partition commit version or commit version - 1 if it's not a " +
13451355 "upgrade stage, its a fatal error. " ,
13461356 copiedState );
1347- return ;
1357+ return transactionState ;
13481358 }
13491359 copiedState .setErrorReplicas (errorReplicaIds );
13501360 copiedState .setFinishTime (System .currentTimeMillis ());
@@ -1397,6 +1407,7 @@ public void finishTransaction(long transactionId, Set<Long> errorReplicaIds, lon
13971407 GlobalStateMgr .getCurrentState ().getLocalMetastore ().handleMVRepair (copiedState );
13981408 LOG .info ("finish transaction {} successfully" , copiedState );
13991409 updateTransactionMetrics (copiedState );
1410+ return copiedState ;
14001411 }
14011412
14021413 protected boolean unprotectedCommitPreparedTransaction (TransactionState transactionState , Database db ) {
@@ -1405,7 +1416,7 @@ protected boolean unprotectedCommitPreparedTransaction(TransactionState transact
14051416 return false ;
14061417 }
14071418 // commit timestamps needs to be strictly monotonically increasing
1408- long commitTs = Math . max ( System . currentTimeMillis (), maxCommitTs + 1 );
1419+ long commitTs = reserveCommitTs ( );
14091420 transactionState .setCommitTime (commitTs );
14101421 // update transaction state version
14111422 transactionState .setTransactionStatus (TransactionStatus .COMMITTED );
@@ -1518,6 +1529,13 @@ protected boolean unprotectedCommitPreparedTransaction(TransactionState transact
15181529 return true ;
15191530 }
15201531
1532+ private long reserveCommitTs () {
1533+ Preconditions .checkState (transactionLock .isWriteLockedByCurrentThread ());
1534+ long commitTs = Math .max (System .currentTimeMillis (), maxCommitTs + 1 );
1535+ maxCommitTs = commitTs ;
1536+ return commitTs ;
1537+ }
1538+
15211539 // for add/update/delete TransactionState
15221540 protected void unprotectUpsertTransactionState (TransactionState transactionState ) {
15231541 // it's OK if getCommitTime() returns -1
@@ -2052,7 +2070,7 @@ GlobalStateMgr getGlobalStateMgr() {
20522070 return globalStateMgr ;
20532071 }
20542072
2055- public void finishTransactionNew (TransactionState transactionState , Set <Long > publishErrorReplicas )
2073+ public TransactionState finishTransactionNew (TransactionState transactionState , Set <Long > publishErrorReplicas )
20562074 throws StarRocksException {
20572075 Database db = globalStateMgr .getLocalMetastore ().getDb (transactionState .getDbId ());
20582076 if (db == null ) {
@@ -2072,7 +2090,7 @@ public void finishTransactionNew(TransactionState transactionState, Set<Long> pu
20722090 }
20732091 });
20742092
2075- return ;
2093+ return copiedState ;
20762094 } finally {
20772095 transactionState .writeUnlock ();
20782096 }
@@ -2133,6 +2151,7 @@ public void finishTransactionNew(TransactionState transactionState, Set<Long> pu
21332151 GlobalStateMgr .getCurrentState ().getLocalMetastore ().handleMVRepair (copiedState );
21342152 LOG .info ("finish transaction {} successfully" , copiedState );
21352153 updateTransactionMetrics (copiedState );
2154+ return copiedState ;
21362155 }
21372156
21382157 // only for test
@@ -2194,7 +2213,7 @@ private boolean isTxnStateBatchConsistent(Database db, TransactionStateBatch sta
21942213 }
21952214
21962215
2197- public void finishTransactionBatch (TransactionStateBatch stateBatch , Set <Long > errorReplicaIds ) {
2216+ public TransactionStateBatch finishTransactionBatch (TransactionStateBatch stateBatch , Set <Long > errorReplicaIds ) {
21982217 Database db = globalStateMgr .getLocalMetastore ().getDb (stateBatch .getDbId ());
21992218 if (db == null ) {
22002219 stateBatch .writeLock ();
@@ -2211,7 +2230,7 @@ public void finishTransactionBatch(TransactionStateBatch stateBatch, Set<Long> e
22112230 writeUnlock ();
22122231 }
22132232 });
2214- return ;
2233+ return copiedStateBatch ;
22152234 } finally {
22162235 stateBatch .writeUnlock ();
22172236 }
@@ -2231,7 +2250,7 @@ public void finishTransactionBatch(TransactionStateBatch stateBatch, Set<Long> e
22312250 copiedStateBatch = new TransactionStateBatch (stateBatch );
22322251 // check whether version is consistent
22332252 if (!isTxnStateBatchConsistent (db , copiedStateBatch )) {
2234- return ;
2253+ return stateBatch ;
22352254 }
22362255
22372256 copiedStateBatch .setTransactionVisibleInfo ();
@@ -2264,6 +2283,7 @@ public void finishTransactionBatch(TransactionStateBatch stateBatch, Set<Long> e
22642283 for (TransactionState transactionState : copiedStateBatch .getTransactionStates ()) {
22652284 updateTransactionMetrics (transactionState );
22662285 }
2286+ return copiedStateBatch ;
22672287 }
22682288
22692289 private void updateTransactionMetrics (TransactionState txnState ) {
0 commit comments