Skip to content

Commit

Permalink
feature: add TCC three-phase hooks (apache#6731)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengliefeng committed Aug 26, 2024
1 parent b358e22 commit a2ebc11
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@
package org.apache.seata.integration.tx.api.fence.hook;


import org.apache.seata.rm.tcc.api.BusinessActionContext;

public interface TccHook {

/**
* before tcc prepare
*/
void beforeTccPrepare(String xid, Long branchId, String actionName);
void beforeTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc prepare
*/
void afterTccPrepare(String xid, Long branchId, String actionName);
void afterTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* before tcc commit
*/
void beforeTccCommit(String xid, Long branchId, String actionName);
void beforeTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc commit
*/
void afterTccCommit(String xid, Long branchId, String actionName);
void afterTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* before tcc rollback
*/
void beforeTccRollback(String xid, Long branchId, String actionName);
void beforeTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc rollback
*/
void afterTccRollback(String xid, Long branchId, String actionName);
void afterTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
try {
//share actionContext implicitly
BusinessActionContextUtil.setContext(actionContext);
doBeforeTccPrepare(xid, branchId, actionName);
doBeforeTccPrepare(xid, branchId, actionName, actionContext);
if (businessActionParam.getUseCommonFence()) {
try {
// Use common Fence, and return the business result
Expand All @@ -108,7 +108,7 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
}
} finally {
try {
doAfterTccPrepare(xid, branchId, actionName);
doAfterTccPrepare(xid, branchId, actionName, actionContext);
//to report business action context finally if the actionContext.getUpdated() is true
BusinessActionContextUtil.reportContext(actionContext);
} finally {
Expand All @@ -128,15 +128,16 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doBeforeTccPrepare(String xid, String branchId, String actionName) {
private void doBeforeTccPrepare(String xid, String branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
try {
hook.beforeTccPrepare(xid, Long.valueOf(branchId), actionName);
hook.beforeTccPrepare(xid, Long.valueOf(branchId), actionName, context);
} catch (Exception e) {
LOGGER.error("Failed execute beforeTccPrepare in hook {}", e.getMessage(), e);
}
Expand All @@ -148,15 +149,16 @@ private void doBeforeTccPrepare(String xid, String branchId, String actionName)
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doAfterTccPrepare(String xid, String branchId, String actionName) {
private void doAfterTccPrepare(String xid, String branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
try {
hook.afterTccPrepare(xid, Long.valueOf(branchId), actionName);
hook.afterTccPrepare(xid, Long.valueOf(branchId), actionName, context);
} catch (Exception e) {
LOGGER.error("Failed execute afterTccPrepare in hook {}", e.getMessage(), e);
}
Expand Down
34 changes: 20 additions & 14 deletions tcc/src/main/java/org/apache/seata/rm/tcc/TCCResourceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ public BranchStatus branchCommit(BranchType branchType, String xid, long branchI
if (targetTCCBean == null || commitMethod == null) {
throw new ShouldNeverHappenException(String.format("TCC resource is not available, resourceId: %s", resourceId));
}
BusinessActionContext businessActionContext = null;
try {
//BusinessActionContext
BusinessActionContext businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
applicationData);

Object[] args = this.getTwoPhaseCommitArgs(tccResource, businessActionContext);
//share actionContext implicitly
BusinessActionContextUtil.setContext(businessActionContext);
doBeforeTccCommit(xid, branchId, tccResource.getActionName());
doBeforeTccCommit(xid, branchId, tccResource.getActionName(), businessActionContext);
Object ret;
boolean result;
// add idempotent and anti hanging
Expand Down Expand Up @@ -153,7 +154,7 @@ public BranchStatus branchCommit(BranchType branchType, String xid, long branchI
LOGGER.error(msg, ExceptionUtil.unwrap(t));
return BranchStatus.PhaseTwo_CommitFailed_Retryable;
} finally {
doAfterTccCommit(xid, branchId, tccResource.getActionName());
doAfterTccCommit(xid, branchId, tccResource.getActionName(), businessActionContext);
// clear the action context
BusinessActionContextUtil.clear();
}
Expand Down Expand Up @@ -182,14 +183,15 @@ public BranchStatus branchRollback(BranchType branchType, String xid, long branc
if (targetTCCBean == null || rollbackMethod == null) {
throw new ShouldNeverHappenException(String.format("TCC resource is not available, resourceId: %s", resourceId));
}
BusinessActionContext businessActionContext = null;
try {
//BusinessActionContext
BusinessActionContext businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
businessActionContext = BusinessActionContextUtil.getBusinessActionContext(xid, branchId, resourceId,
applicationData);
Object[] args = this.getTwoPhaseRollbackArgs(tccResource, businessActionContext);
//share actionContext implicitly
BusinessActionContextUtil.setContext(businessActionContext);
doBeforeTccRollback(xid, branchId, tccResource.getActionName());
doBeforeTccRollback(xid, branchId, tccResource.getActionName(), businessActionContext);
Object ret;
boolean result;
// add idempotent and anti hanging
Expand Down Expand Up @@ -219,7 +221,7 @@ public BranchStatus branchRollback(BranchType branchType, String xid, long branc
LOGGER.error(msg, ExceptionUtil.unwrap(t));
return BranchStatus.PhaseTwo_RollbackFailed_Retryable;
} finally {
doAfterTccRollback(xid, branchId, tccResource.getActionName());
doAfterTccRollback(xid, branchId, tccResource.getActionName(), businessActionContext);
// clear the action context
BusinessActionContextUtil.clear();
}
Expand All @@ -230,15 +232,16 @@ public BranchStatus branchRollback(BranchType branchType, String xid, long branc
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doBeforeTccRollback(String xid, long branchId, String actionName) {
private void doBeforeTccRollback(String xid, long branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
try {
hook.beforeTccRollback(xid, branchId, actionName);
hook.beforeTccRollback(xid, branchId, actionName, context);
} catch (Exception e) {
LOGGER.error("Failed execute beforeTccRollback in hook {}", e.getMessage(), e);
}
Expand All @@ -250,15 +253,16 @@ private void doBeforeTccRollback(String xid, long branchId, String actionName) {
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doAfterTccRollback(String xid, long branchId, String actionName) {
private void doAfterTccRollback(String xid, long branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
try {
hook.afterTccRollback(xid, branchId, actionName);
hook.afterTccRollback(xid, branchId, actionName, context);
} catch (Exception e) {
LOGGER.error("Failed execute afterTccRollback in hook {}", e.getMessage(), e);
}
Expand All @@ -270,15 +274,16 @@ private void doAfterTccRollback(String xid, long branchId, String actionName) {
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doBeforeTccCommit(String xid, long branchId, String actionName) {
private void doBeforeTccCommit(String xid, long branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
try {
hook.beforeTccCommit(xid, branchId, actionName);
hook.beforeTccCommit(xid, branchId, actionName, context);
} catch (Exception e) {
LOGGER.error("Failed execute beforeTccCommit in hook {}", e.getMessage(), e);
}
Expand All @@ -290,15 +295,16 @@ private void doBeforeTccCommit(String xid, long branchId, String actionName) {
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doAfterTccCommit(String xid, long branchId, String actionName) {
private void doAfterTccCommit(String xid, long branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
try {
hook.afterTccCommit(xid, branchId, actionName);
hook.afterTccCommit(xid, branchId, actionName, context);
} catch (Exception e) {
LOGGER.error("Failed execute afterTccCommit in hook {}", e.getMessage(), e);
}
Expand Down

0 comments on commit a2ebc11

Please sign in to comment.