Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于saga模式的问题(没有全局提交) #5424

Closed
412165137lzh opened this issue Mar 10, 2023 · 11 comments
Closed

关于saga模式的问题(没有全局提交) #5424

412165137lzh opened this issue Mar 10, 2023 · 11 comments

Comments

@412165137lzh
Copy link

目前我们项目组使用1seata.5.2版本,并且是基于spingboot+springcloud基础的,在分析saga源码的时候发现一些问题,请指教

前言描述:
按理说saga模式是不需要@GlobalTransactional注解的,因为开启状态机时DbAndReportTcStateLogStore.recordStateMachineStarted就会注册全局事务。

问题:
状态机成功执行完毕后,居然不会调用TC的globalCommit接口,就直接响应成功信息给调用方了。
此时TC端的global_table中还存在一条status=begin的数据,等到全局事务超时后(默认30分钟),TC就会调用“开启状态机”的服务进行branchRollback,回滚方法居然是直接调用各个serviceTask节点的补偿方法。
请问这里是否设计有问题,还是我的理解有偏差? 调用方收到成功消息了(比如转账成功),30分钟后居然回滚了?

@wt-better
Copy link
Contributor

我也看了下哈,说下理解,在状态机执行完毕时,会调用DbAndReportTcStateLogStore#recordStateMachineFinished方法主动更新全局事务的状态,理论上会更新全局事务状态到目标比如成功或者失败,除非在End状态全局事务超时或者是状态机状态更新异常,否则应该不会出现上面的情况。

@412165137lzh
Copy link
Author

我也看了下哈,说下理解,在状态机执行完毕时,会调用DbAndReportTcStateLogStore#recordStateMachineFinished方法主动更新全局事务的状态,理论上会更新全局事务状态到目标比如成功或者失败,除非在End状态全局事务超时或者是状态机状态更新异常,否则应该不会出现上面的情况。

并不是你说这样的哦,这块我之前也同样分析过了,你仔细看DbAndReportTcStateLogStore#recordStateMachineFinished方法,这个方法调用的是TC的globalReport接口,该接口并不会将全局事务状态修改为committed,只会删除所有的branch而已,所以此时全局状态还是begin,这个问题还是存在的,我写demo测了很多案例的,都是这个结果

TC的globalReport接口源码
@OverRide
public void doGlobalReport(GlobalSession globalSession, String xid, GlobalStatus globalStatus) throws TransactionException {
if (GlobalStatus.Committed.equals(globalStatus)) {
SessionHelper.removeAllBranch(globalSession, false);
SessionHelper.endCommitted(globalSession,false);
LOGGER.info("Global[{}] committed", globalSession.getXid());
} else if (GlobalStatus.Rollbacked.equals(globalStatus)
|| GlobalStatus.Finished.equals(globalStatus)) {
SessionHelper.removeAllBranch(globalSession, false);
SessionHelper.endRollbacked(globalSession, false);
LOGGER.info("Global[{}] rollbacked", globalSession.getXid());
} else {
globalSession.changeGlobalStatus(globalStatus);
LOGGER.info("Global[{}] reporting is successfully done. status[{}]", globalSession.getXid(), globalSession.getStatus());

        if (GlobalStatus.RollbackRetrying.equals(globalStatus)
                || GlobalStatus.TimeoutRollbackRetrying.equals(globalStatus)
                || GlobalStatus.UnKnown.equals(globalStatus)) {
            globalSession.queueToRetryRollback();
            LOGGER.info("Global[{}] will retry rollback", globalSession.getXid());
        } else if (GlobalStatus.CommitRetrying.equals(globalStatus)) {
            globalSession.queueToRetryCommit();
            LOGGER.info("Global[{}] will retry commit", globalSession.getXid());
        }
    }
}

@liyy653787585
Copy link

同问,前几天在看seata源码时,我也遇到了这个问题,不过我是直接基于官方提供的例子程序跑的,暂时还没理解这样设计的用意

@wt-better
Copy link
Contributor

wt-better commented Mar 13, 2023

#5050
saga Template没有走commit分支,看起来是没完全fix掉。

@wt-better
Copy link
Contributor

老版本是ok的,看起来是延迟删除branch feature带来的问题

@liyy653787585
Copy link

#5050 saga Template没有走commit分支,看起来是没完全fix掉。

是的,saga走的是globalReport分支,而不是commit分支,目前知道这是一个已知的问题就可以了,谢谢老哥的回答哈

@wt-better 对了,老哥可以帮忙看下我提的这个问题吗?感谢感谢 #5433
我在saga的流程中没看到处理幂等、空回滚、悬挂的相关逻辑,可能是我刚开始看saga源码,希望老哥指点下

@liyy653787585
Copy link

@wt-better 老哥,我看到这个问题在 #5145 中的修改方案没有被merge,请问这个问题大概会在什么时候解决?因为业务上准备使用saga模式了

@wt-better
Copy link
Contributor

@wt-better 老哥,我看到这个问题在 #5145 中的修改方案没有被merge,请问这个问题大概会在什么时候解决?因为业务上准备使用saga模式了

我跟进看下

@liyy653787585
Copy link

好的,目前我参考 #5145 中的思路先行修复了,等后边官方修复了,我再看下有没有漏考虑什么,感谢老哥

@zhangam
Copy link

zhangam commented Jul 31, 2023

v1.7.0好像问题还在,加了@GlobalTransactional就可以提交,但是走了AT模式和Saga两种模式,不加的话就一直是Begin

@funky-eyes
Copy link
Contributor

v1.7.0好像问题还在,加了@GlobalTransactional就可以提交,但是走了AT模式和Saga两种模式,不加的话就一直是Begin

本来就允许at和saga混用,只能选一种

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants