The second chapter of distributed learning framework seata something (parse source (server-side))

Seen before the start of commit and rollback of things tm AT mode end code Then take a look at the requests of a place to go server. This is used to determine matters in the end is a commit or rollback stuff while also undo log records (raw data before data manipulation)
start talking about the first open interface to the transaction
Here Insert Picture Description
can see an abstract way to look at him implementation class
Here Insert Picture Description

You can see only one implementation class is DefaultCoordinator
look at this method does what

Here Insert Picture Description
You can see the begin method calls the object's core and set xid. Here you can know xid is generated here
to see the core of the begin method did what
Here Insert Picture Description
first is a new session sessionmanager out and then add a listener and then call the begin method
Here Insert Picture Description
after here onbegin method calls the listener's point of discovery into It is the
Here Insert Picture Description
Here Insert Picture Description
following method writesession

Here Insert Picture Description
We can see there is a file or write to db session where you can note that if you are using a distributed design seata when the server can not write to the file you want to use the database but excluding stand-alone (no one should use it stand-alone )

Here Insert Picture Description
Here is a way to go in eventbus post to see what made
this publication an event. By @Subscribe find his subscribers (with no framework for the spring so useless spring subscription model release Personally, I prefer the spring)

Here Insert Picture Description
Here Insert Picture Description
Obtaining a corresponding method map because begin. That is to call first begin

Here Insert Picture Description
点进去之后 是 一个map 维护的计数器
Here Insert Picture Description
看到使用的是 atomiclong。不过 阿里开发手册 建议 用longadder 至于两者区别 自己看去吧 简单说一下 在并发下 longadder 性能要比 前者好 实现原理 自己看去

最后是就 begin 的结尾了 返回 xid 到此 begin 就结束了 接下来 看看 commit

Here Insert Picture Description
假设 现在 我们的业务代码 已经 开启 然后 全部执行成功 接下来 就是 执行 commit 方法了
Here Insert Picture Description
Here Insert Picture Description
这里 首先 通过 xid 去寻找 session。这里肯定能找到(按照正常流程)
Here Insert Picture Description
这里就看到了 如果 你使用文件存储 不使用数据库 在分布式模式下 极可能出现session 为null的情况 我感觉 这个地方 少了校验。 应该 在请求来的时候 带上 存储类型 如果 是文件 还是分布式 那应该报错 (谨代表个人观点) 但是 你要是 改造一下 使用 流去读取 远程文件。也是可以的
Here Insert Picture Description
如果是 begin 则 将状态 改为 commiting 并返回true

Here Insert Picture Description
这里尝试 去 异步 commit 进去看看做了啥

Here Insert Picture Description
可以看到 如果是 tcc模式 返回false 就是 tcc模式 不能 异步 这里埋个疑惑 为啥 tcc不能呢
Here Insert Picture Description
上面就是 执行 commit 的方法了

Here Insert Picture Description
这里不说了 应该还是 计数

Here Insert Picture Description
这里判断是否是 saga模式 即长事物模式
Here Insert Picture Description
这里看到 如果是第一阶段 失败 则 继续循环 不继续执行
这里是继续执行的代码 开始 发送请求 到 rm端了
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
是异步的发送请求出去
Here Insert Picture Description
下面的代码 是 判断 返回的内容是否成功

Here Insert Picture Description
Here is successful removal of this session
Here Insert Picture Description
here is the content after sending when the return is not retried
we look after treatment failure
Here Insert Picture Description
Here Insert Picture Description
with the next session of the state is about change and failed to save
us look at this approach
Here Insert Picture Description
see the name should be put in the retry queue to which

Here Insert Picture Description
Here Insert Picture Description
Into which the listener down status to retry

Finally, look at the code following the real success commit

Here Insert Picture Description
Here Insert Picture Description
The session was changed successfully re-written to db

At this point the commit method is over the next look. Rollback methods

Rollback
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Here you can see the first call the above method has a close passage

Here Insert Picture Description

Means that the first closed session. Then the other branches can not be registered to this session will come up because you want to roll back a thing
Here Insert Picture Description
or put the listener inside
Here Insert Picture Description
here when it is determined that the state can begin to roll back the time representation

Here Insert Picture Description
The above is a specific method of rollback.
Here Insert Picture DescriptionWith submission method similar interface is just not the same as a call to retry the other will not say but this method has the following passage
Here Insert Picture Description
means that when things in db rollback (here that is stored in the session db) can lead to data inconsistencies It will happen (probability event is considered to be small does not mean it will not happen)
well, so far over the server-side code analysis makers really good intimate manufacturers

Released three original articles · won praise 0 · Views 84

Guess you like

Origin blog.csdn.net/haha12312wqe/article/details/104640369