@@ -512,22 +512,35 @@ private void commitTransaction(long dbId, List<Table> tableList, long transactio
512
512
}
513
513
514
514
final CommitTxnRequest commitTxnRequest = builder .build ();
515
+ boolean txnOperated = false ;
516
+ TransactionState txnState = null ;
517
+ TxnStateChangeCallback cb = null ;
518
+ long callbackId = 0L ;
515
519
try {
516
- commitTxn (commitTxnRequest , transactionId , is2PC , dbId , tableList );
517
- } catch (UserException e ) {
518
- // For routine load, it is necessary to release the write lock when commit transaction fails,
519
- // otherwise it will cause the lock added in beforeCommitted to not be released.
520
+ txnState = commitTxn (commitTxnRequest , transactionId , is2PC , dbId , tableList );
521
+ txnOperated = true ;
522
+ } finally {
520
523
if (txnCommitAttachment != null && txnCommitAttachment instanceof RLTaskTxnCommitAttachment ) {
521
524
RLTaskTxnCommitAttachment rlTaskTxnCommitAttachment = (RLTaskTxnCommitAttachment ) txnCommitAttachment ;
522
- Env .getCurrentEnv ().getRoutineLoadManager ().getJob (rlTaskTxnCommitAttachment .getJobId ()).writeUnlock ();
525
+ callbackId = rlTaskTxnCommitAttachment .getJobId ();
526
+ } else if (txnState != null ) {
527
+ callbackId = txnState .getCallbackId ();
528
+ }
529
+
530
+ cb = callbackFactory .getCallback (callbackId );
531
+ if (cb != null ) {
532
+ LOG .info ("commitTxn, run txn callback, transactionId:{} callbackId:{}, txnState:{}" ,
533
+ transactionId , callbackId , txnState );
534
+ cb .afterCommitted (txnState , txnOperated );
535
+ cb .afterVisible (txnState , txnOperated );
523
536
}
524
- throw e ;
525
537
}
526
538
}
527
539
528
- private void commitTxn (CommitTxnRequest commitTxnRequest , long transactionId , boolean is2PC , long dbId ,
540
+ private TransactionState commitTxn (CommitTxnRequest commitTxnRequest , long transactionId , boolean is2PC , long dbId ,
529
541
List <Table > tableList ) throws UserException {
530
542
CommitTxnResponse commitTxnResponse = null ;
543
+ TransactionState txnState = null ;
531
544
int retryTime = 0 ;
532
545
533
546
try {
@@ -578,19 +591,13 @@ private void commitTxn(CommitTxnRequest commitTxnRequest, long transactionId, bo
578
591
throw new UserException ("internal error, " + internalMsgBuilder .toString ());
579
592
}
580
593
581
- TransactionState txnState = TxnUtil .transactionStateFromPb (commitTxnResponse .getTxnInfo ());
582
- TxnStateChangeCallback cb = callbackFactory .getCallback (txnState .getCallbackId ());
583
- if (cb != null ) {
584
- LOG .info ("commitTxn, run txn callback, transactionId:{} callbackId:{}, txnState:{}" ,
585
- txnState .getTransactionId (), txnState .getCallbackId (), txnState );
586
- cb .afterCommitted (txnState , true );
587
- cb .afterVisible (txnState , true );
588
- }
594
+ txnState = TxnUtil .transactionStateFromPb (commitTxnResponse .getTxnInfo ());
589
595
if (MetricRepo .isInit ) {
590
596
MetricRepo .COUNTER_TXN_SUCCESS .increase (1L );
591
597
MetricRepo .HISTO_TXN_EXEC_LATENCY .update (txnState .getCommitTime () - txnState .getPrepareTime ());
592
598
}
593
599
afterCommitTxnResp (commitTxnResponse );
600
+ return txnState ;
594
601
}
595
602
596
603
private List <OlapTable > getMowTableList (List <Table > tableList ) {
@@ -990,9 +997,24 @@ public boolean commitAndPublishTransaction(DatabaseIf db, long transactionId,
990
997
}
991
998
992
999
final CommitTxnRequest commitTxnRequest = builder .build ();
993
- commitTxn (commitTxnRequest , transactionId , false , db .getId (),
994
- subTransactionStates .stream ().map (SubTransactionState ::getTable )
1000
+ TransactionState txnState = null ;
1001
+ boolean txnOperated = false ;
1002
+ try {
1003
+ txnState = commitTxn (commitTxnRequest , transactionId , false , db .getId (),
1004
+ subTransactionStates .stream ().map (SubTransactionState ::getTable )
995
1005
.collect (Collectors .toList ()));
1006
+ txnOperated = true ;
1007
+ } finally {
1008
+ if (txnState != null ) {
1009
+ TxnStateChangeCallback cb = callbackFactory .getCallback (txnState .getCallbackId ());
1010
+ if (cb != null ) {
1011
+ LOG .info ("commitTxn, run txn callback, transactionId:{} callbackId:{}, txnState:{}" ,
1012
+ txnState .getTransactionId (), txnState .getCallbackId (), txnState );
1013
+ cb .afterCommitted (txnState , txnOperated );
1014
+ cb .afterVisible (txnState , txnOperated );
1015
+ }
1016
+ }
1017
+ }
996
1018
return true ;
997
1019
}
998
1020
@@ -1042,8 +1064,6 @@ public void abortTransaction(Long dbId, Long transactionId, String reason) throw
1042
1064
@ Override
1043
1065
public void abortTransaction (Long dbId , Long transactionId , String reason ,
1044
1066
TxnCommitAttachment txnCommitAttachment , List <Table > tableList ) throws UserException {
1045
- LOG .info ("try to abort transaction, dbId:{}, transactionId:{}" , dbId , transactionId );
1046
-
1047
1067
if (txnCommitAttachment != null ) {
1048
1068
if (txnCommitAttachment instanceof RLTaskTxnCommitAttachment ) {
1049
1069
RLTaskTxnCommitAttachment rlTaskTxnCommitAttachment = (RLTaskTxnCommitAttachment ) txnCommitAttachment ;
@@ -1058,6 +1078,18 @@ public void abortTransaction(Long dbId, Long transactionId, String reason,
1058
1078
}
1059
1079
}
1060
1080
1081
+ AbortTxnResponse abortTxnResponse = null ;
1082
+ try {
1083
+ abortTxnResponse = abortTransactionImpl (dbId , transactionId , reason , null , null );
1084
+ } finally {
1085
+ handleAfterAbort (abortTxnResponse , txnCommitAttachment , transactionId );
1086
+ }
1087
+ }
1088
+
1089
+ private AbortTxnResponse abortTransactionImpl (Long dbId , Long transactionId , String reason ,
1090
+ TxnCommitAttachment txnCommitAttachment , List <Table > tableList ) throws UserException {
1091
+ LOG .info ("try to abort transaction, dbId:{}, transactionId:{}" , dbId , transactionId );
1092
+
1061
1093
AbortTxnRequest .Builder builder = AbortTxnRequest .newBuilder ();
1062
1094
builder .setDbId (dbId );
1063
1095
builder .setTxnId (transactionId );
@@ -1089,27 +1121,43 @@ public void abortTransaction(Long dbId, Long transactionId, String reason,
1089
1121
Preconditions .checkNotNull (abortTxnResponse .getStatus ());
1090
1122
} catch (RpcException e ) {
1091
1123
LOG .warn ("abortTxn failed, transactionId:{}, Exception" , transactionId , e );
1092
- // For routine load, it is necessary to release the write lock when abort transaction fails,
1093
- // otherwise it will cause the lock added in beforeAborted to not be released.
1094
- if (txnCommitAttachment != null && txnCommitAttachment instanceof RLTaskTxnCommitAttachment ) {
1095
- RLTaskTxnCommitAttachment rlTaskTxnCommitAttachment = (RLTaskTxnCommitAttachment ) txnCommitAttachment ;
1096
- Env .getCurrentEnv ().getRoutineLoadManager ().getJob (rlTaskTxnCommitAttachment .getJobId ()).writeUnlock ();
1097
- }
1098
1124
throw new UserException ("abortTxn failed, errMsg:" + e .getMessage ());
1099
1125
}
1100
1126
afterAbortTxnResp (abortTxnResponse , String .valueOf (transactionId ), txnCommitAttachment );
1127
+ return abortTxnResponse ;
1128
+ }
1129
+
1130
+ private void handleAfterAbort (AbortTxnResponse abortTxnResponse , TxnCommitAttachment txnCommitAttachment ,
1131
+ long transactionId ) throws UserException {
1132
+ TransactionState txnState = new TransactionState ();
1133
+ boolean txnOperated = false ;
1134
+ long callbackId = 0L ;
1135
+ TxnStateChangeCallback cb = null ;
1136
+ String abortReason = "" ;
1137
+
1138
+ if (abortTxnResponse != null ) {
1139
+ txnState = TxnUtil .transactionStateFromPb (abortTxnResponse .getTxnInfo ());
1140
+ txnOperated = abortTxnResponse .getStatus ().getCode () == MetaServiceCode .OK ;
1141
+ callbackId = txnState .getCallbackId ();
1142
+ abortReason = txnState .getReason ();
1143
+ }
1144
+ if (txnCommitAttachment != null && txnCommitAttachment instanceof RLTaskTxnCommitAttachment ) {
1145
+ RLTaskTxnCommitAttachment rlTaskTxnCommitAttachment = (RLTaskTxnCommitAttachment ) txnCommitAttachment ;
1146
+ callbackId = rlTaskTxnCommitAttachment .getJobId ();
1147
+ }
1148
+
1149
+ cb = callbackFactory .getCallback (callbackId );
1150
+ if (cb != null ) {
1151
+ LOG .info ("run txn callback, txnId:{} callbackId:{}, txnState:{}" ,
1152
+ transactionId , callbackId , txnState );
1153
+ cb .afterAborted (txnState , txnOperated , abortReason );
1154
+ }
1101
1155
}
1102
1156
1103
1157
private void afterAbortTxnResp (AbortTxnResponse abortTxnResponse , String txnIdOrLabel ,
1104
1158
TxnCommitAttachment txnCommitAttachment ) throws UserException {
1105
1159
if (abortTxnResponse .getStatus ().getCode () != MetaServiceCode .OK ) {
1106
1160
LOG .warn ("abortTxn failed, transaction:{}, response:{}" , txnIdOrLabel , abortTxnResponse );
1107
- // For routine load, it is necessary to release the write lock when abort transaction fails,
1108
- // otherwise it will cause the lock added in beforeAborted to not be released.
1109
- if (txnCommitAttachment != null && txnCommitAttachment instanceof RLTaskTxnCommitAttachment ) {
1110
- RLTaskTxnCommitAttachment rlTaskTxnCommitAttachment = (RLTaskTxnCommitAttachment ) txnCommitAttachment ;
1111
- Env .getCurrentEnv ().getRoutineLoadManager ().getJob (rlTaskTxnCommitAttachment .getJobId ()).writeUnlock ();
1112
- }
1113
1161
switch (abortTxnResponse .getStatus ().getCode ()) {
1114
1162
case TXN_ID_NOT_FOUND :
1115
1163
case TXN_LABEL_NOT_FOUND :
@@ -1125,13 +1173,6 @@ private void afterAbortTxnResp(AbortTxnResponse abortTxnResponse, String txnIdOr
1125
1173
}
1126
1174
}
1127
1175
1128
- TransactionState txnState = TxnUtil .transactionStateFromPb (abortTxnResponse .getTxnInfo ());
1129
- TxnStateChangeCallback cb = callbackFactory .getCallback (txnState .getCallbackId ());
1130
- if (cb != null ) {
1131
- LOG .info ("run txn callback, txnId:{} callbackId:{}" , txnState .getTransactionId (),
1132
- txnState .getCallbackId ());
1133
- cb .afterAborted (txnState , true , txnState .getReason ());
1134
- }
1135
1176
if (MetricRepo .isInit ) {
1136
1177
MetricRepo .COUNTER_TXN_FAILED .increase (1L );
1137
1178
}
0 commit comments