Seata源码——TCC模式解析03

TCC模式常见问题

在分布式系统中,随时随地都需要面对网络超时,网络重发和服务器宕机等问题。所以分布式事务框架作为搭载在分布式系统之上的一个框架型应用也绕不开这些问题。具体而言,有以下常见问题:
1.幂等处理
2.空回滚
3.资源悬挂
参考 :https://developer.aliyun.com/article/1053736#

Seata如何解决Tcc常见问题的

使用

在LocalTCC模式下,可以选择开启useTCCFence=true,通过seata框架内置的tcc分支事务状态表解决TCC的三大问题
@TwoPhaseBusinessAction(name = “local-tcc-action”, useTCCFence = true // 是否启用TCCFence,由SeataTCC框架处理TCC三大问题(幂等、悬挂、空回滚)

原理解析

在执行prepare、commit、rollback业务方法之前,用tccFenceLog事务状态表拦截非法请求。
在try阶段,TCCFenceHandler首先开启事务保证与业务在同一个事务中提交,然后插入一条tcc_fence_log状态为STATUS_TRIED,最后执行业务try方法。

Try阶段

TCCFenceHandler

public static Object prepareFence(String xid, Long branchId, String actionName, Callback<Object> targetCallback) {
    // 开启事务
    return transactionTemplate.execute(status -> {
        try {
            Connection conn = DataSourceUtils.getConnection(dataSource);
            // 插入tcc_fence_log status = STATUS_TRIED
            boolean result = insertTCCFenceLog(conn, xid, branchId, actionName, TCCFenceConstant.STATUS_TRIED);
            LOGGER.info("TCC fence prepare result: {}. xid: {}, branchId: {}", result, xid, branchId);
            if (result) {
                // 业务try方法
                return targetCallback.execute();
            } else {
                throw new TCCFenceException(String.format("Insert tcc fence record error, prepare fence failed. xid= %s, branchId= %s", xid, branchId),
                        FrameworkErrorCode.InsertRecordError);
            }
        } catch (TCCFenceException e) {
            if (e.getErrcode() == FrameworkErrorCode.DuplicateKeyException) {
                LOGGER.error("Branch transaction has already rollbacked before,prepare fence failed. xid= {},branchId = {}", xid, branchId);
                addToLogCleanQueue(xid, branchId);
            }
            status.setRollbackOnly();
            throw new SkipCallbackWrapperException(e);
        } catch (Throwable t) {
            status.setRollbackOnly();
            throw new SkipCallbackWrapperException(t);
        }
    });
}

Commit阶段

在二阶段commit方法执行前,先查询tcc_fence_log(select for update)中是否有记录,如果有则做幂等状态校验,只有当状态为STATUS_TRIED时,才执行二阶段业务commit方法。
TCCFenceHandler

public static boolean commitFence(Method commitMethod, Object targetTCCBean,
                                  String xid, Long branchId, Object[] args) {
    // 开启事务
    return transactionTemplate.execute(status -> {
        try {
            Connection conn = DataSourceUtils.getConnection(dataSource);
            // select for update
            TCCFenceDO tccFenceDO = TCC_FENCE_DAO.queryTCCFenceDO(conn, xid, branchId);
            if (tccFenceDO == null) {
                throw new TCCFenceException(String.format("TCC fence record not exists, commit fence method failed. xid= %s, branchId= %s", xid, branchId),
                        FrameworkErrorCode.RecordAlreadyExists);
            }
            // (1)幂等
            if (TCCFenceConstant.STATUS_COMMITTED == tccFenceDO.getStatus()) {
                return true;
            }
            // 之前已经收到二阶段回滚请求
            if (TCCFenceConstant.STATUS_ROLLBACKED == tccFenceDO.getStatus() || TCCFenceConstant.STATUS_SUSPENDED == tccFenceDO.getStatus()) {
                return false;
            }
            // 通过幂等校验后,更新状态STATUS_COMMITTED并执行目标方法
            return updateStatusAndInvokeTargetMethod(conn, commitMethod, targetTCCBean, xid, branchId, TCCFenceConstant.STATUS_COMMITTED, status, args);
        } catch (Throwable t) {
            status.setRollbackOnly();
            throw new SkipCallbackWrapperException(t);
        }
    });
}

rollback阶段

TCCFenceHandler
开启事务后查询tcc_fence_log(select for update)中是否有记录。
如果有记录,做幂等校验后,执行业务rollback方法。
如果无记录,代表这是一次空回滚,那么尝试插入一条tcc_fence_log记录,且status=STATUS_SUSPENDED:
1.如果插入成功,代表二阶段回滚执行完成,直接返回;如果后续收到try请求,在prepareFence中直接抛出异常(唯一约束xid+branchId),不执行一阶段try业务方法,阻止资源悬挂;
2.如果插入失败(唯一约束xid+branchId),可能发生了在rollback阶段收到了一阶段try请求,此时抛出异常,等待后续TC执行二阶段回滚重试,解决资源悬挂;

public static boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,
                                    String xid, Long branchId, Object[] args, String actionName) {
    // 开启事务
    return transactionTemplate.execute(status -> {
        try {
            Connection conn = DataSourceUtils.getConnection(dataSource);
            // select for update
            TCCFenceDO tccFenceDO = TCC_FENCE_DAO.queryTCCFenceDO(conn, xid, branchId);
            // (2) 资源悬挂,插入tcc_fence_log status=STATUS_SUSPENDED,防止一阶段try请求在回滚之后到达
            // (3) 空回滚,如果当前没有tcc_fence_log,代表是一次空回滚,不执行二阶段rollback方法
            if (tccFenceDO == null) {
                boolean result = insertTCCFenceLog(conn, xid, branchId, actionName, TCCFenceConstant.STATUS_SUSPENDED);
                if (!result) {
                    throw new TCCFenceException(String.format("Insert tcc fence record error, rollback fence method failed. xid= %s, branchId= %s", xid, branchId),
                            FrameworkErrorCode.InsertRecordError);
                }
                return true;
            } else {
                // (1) 幂等
                if (TCCFenceConstant.STATUS_ROLLBACKED == tccFenceDO.getStatus() || TCCFenceConstant.STATUS_SUSPENDED == tccFenceDO.getStatus()) {
                    return true;
                }
                if (TCCFenceConstant.STATUS_COMMITTED == tccFenceDO.getStatus()) {
                    return false;
                }
            }
            // 通过幂等和空回滚校验后,更新状态并执行目标方法
            return updateStatusAndInvokeTargetMethod(conn, rollbackMethod, targetTCCBean, xid, branchId, TCCFenceConstant.STATUS_ROLLBACKED, status, args);
        } catch (Throwable t) {
            status.setRollbackOnly();
            throw new SkipCallbackWrapperException(t);
        }
    });
}

你可能感兴趣的:(Seata,java)