Skip to content

Commit a2ebc11

Browse files
author
chengliefeng
committed
feature: add TCC three-phase hooks (apache#6731)
1 parent b358e22 commit a2ebc11

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/fence/hook/TccHook.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,37 @@
1717
package org.apache.seata.integration.tx.api.fence.hook;
1818

1919

20+
import org.apache.seata.rm.tcc.api.BusinessActionContext;
21+
2022
public interface TccHook {
2123

2224
/**
2325
* before tcc prepare
2426
*/
25-
void beforeTccPrepare(String xid, Long branchId, String actionName);
27+
void beforeTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);
2628

2729
/**
2830
* after tcc prepare
2931
*/
30-
void afterTccPrepare(String xid, Long branchId, String actionName);
32+
void afterTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);
3133

3234
/**
3335
* before tcc commit
3436
*/
35-
void beforeTccCommit(String xid, Long branchId, String actionName);
37+
void beforeTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);
3638

3739
/**
3840
* after tcc commit
3941
*/
40-
void afterTccCommit(String xid, Long branchId, String actionName);
42+
void afterTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);
4143

4244
/**
4345
* before tcc rollback
4446
*/
45-
void beforeTccRollback(String xid, Long branchId, String actionName);
47+
void beforeTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);
4648

4749
/**
4850
* after tcc rollback
4951
*/
50-
void afterTccRollback(String xid, Long branchId, String actionName);
52+
void afterTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);
5153
}

integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/ActionInterceptorHandler.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
9090
try {
9191
//share actionContext implicitly
9292
BusinessActionContextUtil.setContext(actionContext);
93-
doBeforeTccPrepare(xid, branchId, actionName);
93+
doBeforeTccPrepare(xid, branchId, actionName, actionContext);
9494
if (businessActionParam.getUseCommonFence()) {
9595
try {
9696
// Use common Fence, and return the business result
@@ -108,7 +108,7 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
108108
}
109109
} finally {
110110
try {
111-
doAfterTccPrepare(xid, branchId, actionName);
111+
doAfterTccPrepare(xid, branchId, actionName, actionContext);
112112
//to report business action context finally if the actionContext.getUpdated() is true
113113
BusinessActionContextUtil.reportContext(actionContext);
114114
} finally {
@@ -128,15 +128,16 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
128128
* @param xid the xid
129129
* @param branchId the branchId
130130
* @param actionName the actionName
131+
* @param context the business action context
131132
*/
132-
private void doBeforeTccPrepare(String xid, String branchId, String actionName) {
133+
private void doBeforeTccPrepare(String xid, String branchId, String actionName, BusinessActionContext context) {
133134
List<TccHook> hooks = TccHookManager.getHooks();
134135
if (hooks.isEmpty()) {
135136
return;
136137
}
137138
for (TccHook hook : hooks) {
138139
try {
139-
hook.beforeTccPrepare(xid, Long.valueOf(branchId), actionName);
140+
hook.beforeTccPrepare(xid, Long.valueOf(branchId), actionName, context);
140141
} catch (Exception e) {
141142
LOGGER.error("Failed execute beforeTccPrepare in hook {}", e.getMessage(), e);
142143
}
@@ -148,15 +149,16 @@ private void doBeforeTccPrepare(String xid, String branchId, String actionName)
148149
* @param xid the xid
149150
* @param branchId the branchId
150151
* @param actionName the actionName
152+
* @param context the business action context
151153
*/
152-
private void doAfterTccPrepare(String xid, String branchId, String actionName) {
154+
private void doAfterTccPrepare(String xid, String branchId, String actionName, BusinessActionContext context) {
153155
List<TccHook> hooks = TccHookManager.getHooks();
154156
if (hooks.isEmpty()) {
155157
return;
156158
}
157159
for (TccHook hook : hooks) {
158160
try {
159-
hook.afterTccPrepare(xid, Long.valueOf(branchId), actionName);
161+
hook.afterTccPrepare(xid, Long.valueOf(branchId), actionName, context);
160162
} catch (Exception e) {
161163
LOGGER.error("Failed execute afterTccPrepare in hook {}", e.getMessage(), e);
162164
}

tcc/src/main/java/org/apache/seata/rm/tcc/TCCResourceManager.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,16 @@ public BranchStatus branchCommit(BranchType branchType, String xid, long branchI
116116
if (targetTCCBean == null || commitMethod == null) {
117117
throw new ShouldNeverHappenException(String.format("TCC resource is not available, resourceId: %s", resourceId));
118118
}
119+
BusinessActionContext businessActionContext = null;
119120
try {
120121
//BusinessActionContext
121-
BusinessActionContext businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
122+
businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
122123
applicationData);
123124

124125
Object[] args = this.getTwoPhaseCommitArgs(tccResource, businessActionContext);
125126
//share actionContext implicitly
126127
BusinessActionContextUtil.setContext(businessActionContext);
127-
doBeforeTccCommit(xid, branchId, tccResource.getActionName());
128+
doBeforeTccCommit(xid, branchId, tccResource.getActionName(), businessActionContext);
128129
Object ret;
129130
boolean result;
130131
// add idempotent and anti hanging
@@ -153,7 +154,7 @@ public BranchStatus branchCommit(BranchType branchType, String xid, long branchI
153154
LOGGER.error(msg, ExceptionUtil.unwrap(t));
154155
return BranchStatus.PhaseTwo_CommitFailed_Retryable;
155156
} finally {
156-
doAfterTccCommit(xid, branchId, tccResource.getActionName());
157+
doAfterTccCommit(xid, branchId, tccResource.getActionName(), businessActionContext);
157158
// clear the action context
158159
BusinessActionContextUtil.clear();
159160
}
@@ -182,14 +183,15 @@ public BranchStatus branchRollback(BranchType branchType, String xid, long branc
182183
if (targetTCCBean == null || rollbackMethod == null) {
183184
throw new ShouldNeverHappenException(String.format("TCC resource is not available, resourceId: %s", resourceId));
184185
}
186+
BusinessActionContext businessActionContext = null;
185187
try {
186188
//BusinessActionContext
187-
BusinessActionContext businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
189+
businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
188190
applicationData);
189191
Object[] args = this.getTwoPhaseRollbackArgs(tccResource, businessActionContext);
190192
//share actionContext implicitly
191193
BusinessActionContextUtil.setContext(businessActionContext);
192-
doBeforeTccRollback(xid, branchId, tccResource.getActionName());
194+
doBeforeTccRollback(xid, branchId, tccResource.getActionName(), businessActionContext);
193195
Object ret;
194196
boolean result;
195197
// add idempotent and anti hanging
@@ -219,7 +221,7 @@ public BranchStatus branchRollback(BranchType branchType, String xid, long branc
219221
LOGGER.error(msg, ExceptionUtil.unwrap(t));
220222
return BranchStatus.PhaseTwo_RollbackFailed_Retryable;
221223
} finally {
222-
doAfterTccRollback(xid, branchId, tccResource.getActionName());
224+
doAfterTccRollback(xid, branchId, tccResource.getActionName(), businessActionContext);
223225
// clear the action context
224226
BusinessActionContextUtil.clear();
225227
}
@@ -230,15 +232,16 @@ public BranchStatus branchRollback(BranchType branchType, String xid, long branc
230232
* @param xid the xid
231233
* @param branchId the branchId
232234
* @param actionName the actionName
235+
* @param context the business action context
233236
*/
234-
private void doBeforeTccRollback(String xid, long branchId, String actionName) {
237+
private void doBeforeTccRollback(String xid, long branchId, String actionName, BusinessActionContext context) {
235238
List<TccHook> hooks = TccHookManager.getHooks();
236239
if (hooks.isEmpty()) {
237240
return;
238241
}
239242
for (TccHook hook : hooks) {
240243
try {
241-
hook.beforeTccRollback(xid, branchId, actionName);
244+
hook.beforeTccRollback(xid, branchId, actionName, context);
242245
} catch (Exception e) {
243246
LOGGER.error("Failed execute beforeTccRollback in hook {}", e.getMessage(), e);
244247
}
@@ -250,15 +253,16 @@ private void doBeforeTccRollback(String xid, long branchId, String actionName) {
250253
* @param xid the xid
251254
* @param branchId the branchId
252255
* @param actionName the actionName
256+
* @param context the business action context
253257
*/
254-
private void doAfterTccRollback(String xid, long branchId, String actionName) {
258+
private void doAfterTccRollback(String xid, long branchId, String actionName, BusinessActionContext context) {
255259
List<TccHook> hooks = TccHookManager.getHooks();
256260
if (hooks.isEmpty()) {
257261
return;
258262
}
259263
for (TccHook hook : hooks) {
260264
try {
261-
hook.afterTccRollback(xid, branchId, actionName);
265+
hook.afterTccRollback(xid, branchId, actionName, context);
262266
} catch (Exception e) {
263267
LOGGER.error("Failed execute afterTccRollback in hook {}", e.getMessage(), e);
264268
}
@@ -270,15 +274,16 @@ private void doAfterTccRollback(String xid, long branchId, String actionName) {
270274
* @param xid the xid
271275
* @param branchId the branchId
272276
* @param actionName the actionName
277+
* @param context the business action context
273278
*/
274-
private void doBeforeTccCommit(String xid, long branchId, String actionName) {
279+
private void doBeforeTccCommit(String xid, long branchId, String actionName, BusinessActionContext context) {
275280
List<TccHook> hooks = TccHookManager.getHooks();
276281
if (hooks.isEmpty()) {
277282
return;
278283
}
279284
for (TccHook hook : hooks) {
280285
try {
281-
hook.beforeTccCommit(xid, branchId, actionName);
286+
hook.beforeTccCommit(xid, branchId, actionName, context);
282287
} catch (Exception e) {
283288
LOGGER.error("Failed execute beforeTccCommit in hook {}", e.getMessage(), e);
284289
}
@@ -290,15 +295,16 @@ private void doBeforeTccCommit(String xid, long branchId, String actionName) {
290295
* @param xid the xid
291296
* @param branchId the branchId
292297
* @param actionName the actionName
298+
* @param context the business action context
293299
*/
294-
private void doAfterTccCommit(String xid, long branchId, String actionName) {
300+
private void doAfterTccCommit(String xid, long branchId, String actionName, BusinessActionContext context) {
295301
List<TccHook> hooks = TccHookManager.getHooks();
296302
if (hooks.isEmpty()) {
297303
return;
298304
}
299305
for (TccHook hook : hooks) {
300306
try {
301-
hook.afterTccCommit(xid, branchId, actionName);
307+
hook.afterTccCommit(xid, branchId, actionName, context);
302308
} catch (Exception e) {
303309
LOGGER.error("Failed execute afterTccCommit in hook {}", e.getMessage(), e);
304310
}

0 commit comments

Comments
 (0)