一、引子
分布式事务组件seata最近社区很活跃,刚好公司有对接seata的计划。刚好借此机会,彻底了解下seata的价值。其中有一个比较特殊的模式叫SAGA模式,听起来就很懵逼,按照官网的介绍起源于一篇1987年的论文:
于是,决定翻译一下原文,附上论文链接如下:Princeton University Report ID: TR-070-87(如下载速度过慢,请点此博客园文件链接)
二、读后总结
2.1 摘要
ABSTRACT
Long lived transactions (LLTS) hold on to database resources for relatively long periods of time, significantly delaying the termination of shorter and more common transactions. To alleviate these problems we propose the notion of a saga. A LLT is a saga if it can be written as a sequence of transactions that can be interleaved with other transactions. The database management system guarantees that either all the transactions in a saga are successfully completed or compensating transactions are run to amend a partial execution. Both the concept of saga and its implementation are relatively simple but they have the potential to improve performance significantly. We analyze the various implementation issues related to sagas, including how they can be run on an existing system that does not directly support them. We also discuss techniques for database and LLT design that make it feasible to break up LLTs into sagas.
January 7, 1987
翻译:
一个长时间事务会在相对较长的时间内占用数据库资源,明显的阻碍了较短的和公用的其他事务完成。为了缓解这些问题, 我们提出一个 saga的概念。它是由多个有序的事务组成、并且与其他事务可以交错的一个长时间事务(LLT),数据库管理系统保证成功完成 saga 中的所有事务, 或对部分进行事务补偿。saga的概念和它的实施相对简单, 但它们有可能显著提高性能。我们分析了与 sagas 相关的各种实施问题,包括如何在不直接支持它们的现有系统上运行它们。我们进行了数据库和 LLT技术讨论, 使 sagas成为LLT解决方案的可能。
1987年1月7日
2.2 核心概念
1.SAGA的两种场景
Once compensating transactions C1, C2, ..., Cn-1 are defined for saga T1, T2, ..., Tn, then the system can make the following guarantee. Either the sequence T1, T2, ..., Tn (which is the preferable one) or the sequence T1, T2, ..., Tj, Cj, ..., C2, C1 for some 0 ≤ j < n will be executed.
翻译:
一旦将 saga 的 T1, T2, ..., Tn 子事务的取消补偿事务定义为 C1, C2, ..., Cn-1 ,那么 这个系统将会作出如下保证,任何一个序列
【T1, T2, ..., Tn】
(最好是一个)或者【这个序列
T1, T2, ..., Tj, Cj, ..., C2, C1
对于0 ≤ j < n 将被执行】(为啥j=n不行?需要补偿时,至少有一个失败,否则全部成功就是T1~Tn了。这也是只有Cn-1的原因)。
说的狠懵逼,其实就是
- 1.正向全部执行(T1, T2, ..., Tn)
- 2.执行到第J个事务时(T1, T2, ..., Tj)出错,逆向从J回退(Cj, ..., C2, C1)补偿一遍。
2.SAGA事务特性
Note that the notion of a saga is related to that of a nested transaction [Garc83a, Lync83a]. However there are two important differences:
(a)A saga only permits two levels of nesting the top level saga and simple transactions, and
(b)At the outer level full atomicity is not provided. That is, sagas may view the partial results of other sagas.
翻译:
请注意,saga的概念与嵌套事务的概念有关[Garc83a, Lync83a]。但是, 有两个重要的区别:
- (a)一个 saga 嵌套只允许有2层,顶级的 saga 第一层,里面的简单事务为第二层。
- (b)在外部层面看不提供完全的原子性。也就是说,某个saga可能看到其他saga的部分结果。(违反了事务的隔离性)
3.恢复模式
逆向恢复:补偿事务。
正向恢复:系统需要保存点。
混合恢复:T1、T2(保存点S1)、T3、T4时奔溃。先执行逆向恢复到S1,再执行正向恢复T3、T4.
4.如何实现SAGA
Also recall that pure forward recovery does not require compensating transactions (see Section 5). So if compensating transactions are hard to write,
then one has the choice of tailoring the application so that LLTs do not have user initiated aborts. Without these aborts, pure forward recovery is feasible and compensation is never needed.
翻译:
LLT设计补偿事务是一个非常普遍的难题。
1)如果这种补偿机制很难写的话,考虑业务上正向补偿:正向补救措施。
As has become clear from our discussion, the structure of the database plays an important role in the design of sagas. Thus, it is best not to study each LLT in isolation,
but to design the entire database with LLTs and sagas in mind. That is, if the database can be laid out into a set of loosely-coupled components (with few and simple inter-component consistency constraints,
then it is likely that the LLT will naturally break up into sub-transactions that can be interleaved.
2)DB的结构在saga设计中扮演着关键角色,因此最好不要孤立地研究每种LLT,而是在设计数据库时就把LLT和saga考虑进去。如果DB能被设计成一套松耦合的组件(少且简单的一致性约束),那么LLT就能自然地拆解为多个子事务编织而成。
================引用===============================
1.《Sagas.pdf》论文原文。
2.https://github.com/mltds/sagas-report 翻译。
如果你觉得本文对你有点帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!