TCC transaction model of distributed transactions

text

We first set into a business scenario, as shown in FIG.
TCC transaction model of distributed transactions

Page payment button that point, calling the payment service, then we want to achieve the following three steps back
[1] Order service - modify the order status
[2] Account Services - deduct money
[3] Inventory Service - deduct inventory
reached affairs effect, either succeed together or fail together! TCC is necessary to take a distributed transaction program!

concept

TCC stands (Try-Confirm-Cancel). As shown below

TCC transaction model of distributed transactions

ps: TCC and can be called a two-phase compensation matters, only the first stage try to reserve resources, the second stage to clearly tell the service provider, the resource you want to do in the end, corresponding to the second stage of the confirm / cancel, with to remove the effects of the first phase, so called compensation matters.

Another analogy, TCC says it is too large, say RM of prepare, commit, rollback interfaces, always know it. The analogy can be so understanding
TCC transaction model of distributed transactions

That difference where it?
ROLLBACK, the commit, PREPARE, standing developer level is imperceptible, database resources to help you do the operation!
And try, confirm, cancel, standing developer level is able to perceive, business logic of these three methods, namely the operation of resources, the developer is to themselves to achieve!
Well, here we set the scene, how to do it. For example, you order the service had only one interface

//修改代码状态
orderClient.updateStatus();

It is split into three interfaces, namely

orderClient.tryUpateStatus();
orderClient.confirmUpateStatus();
orderClient.cancelUpateStatus();

Attention : If the interviewer asks you, TCC What are the disadvantages? This is a very serious drawback code *** large! Each service logic, should the try press (resource request), Confirm (operation resources), Cancel (canceled resources), split into three interfaces!

具体每个阶段,每个服务业务逻辑是什么样的呢?
假设,库存数量本来是50,那么可销售库存也是50。账户余额为50,可用余额也为50。用户下单,买了1个单价为1元的商品。流程如下:
Try阶段
订单服务:修改订单的状态为支付中
账户服务:账户余额不变,可用余额减1,然后将1这个数字冻结在一个单独的字段里
库存服务:库存数量不变,可销售库存减1,然后将1这个数字冻结在一个单独的字段里
confirm阶段
订单服务:修改订单的状态为支付完成
账户服务:账户余额变为(当前值减冻结字段的值),可用余额不变(Try阶段减过了),冻结字段清0。
库存服务:库存变为(当前值减冻结字段的值),可销售库存不变(Try阶段减过了),冻结字段清0。
cancel阶段
订单服务:修改订单的状态为未支付
账户服务:账户余额不变,可用余额变为(当前值加冻结字段的值),冻结字段清0。
库存服务:库存不变,可销售库存变为(当前值加冻结字段的值),冻结字段清0。

伪代码

接下来从代码程序来说明,为了便于演示,将入参略去。
本来,你支付服务的代码是长下面这样的

TCC transaction model of distributed transactions

那么,用上TCC模型后,代码变成下面这样

TCC transaction model of distributed transactions

注意了,这种写法其实严格上来说,不是不行。看你业务场景,因为存在一些瑕疵,看你自己有没办法接受
(1)cancel或者confirm出现异常了,你怎么处理?
例如在cancel阶段执行如下三行代码

orderClient.cancelUpdateStatus();
accountClient.cancelDecrease();
repositoryClient.cancelDecrease();

Your second line is abnormal, and the third line did not run to quit, how to do? To compensate for this you conduct business!
(2) a large number of duplicate logic
you see, ah, our execution architecture fact, this is

try{
    xxclient.try();
}catch(Throwable t){
    xxclient.cancel();
    throw t;
}
xxclient.confirm();

There is no way to make this shelf to the framework to perform, we tell the framework, which method you want to perform like at each stage!

Therefore, it is necessary to introduce TCC distributed transaction framework, Try, Confirm, Cancel three state of affairs to the framework to perceive! You just tell the framework, Try To do what, Confirm To do what, Cancel to execute what! Cancel the process if an exception occurs, the frame has an internal remedies to restore your data!
Tcc distributed framework hmily example, if an exception cancel or confirm an abnormal situation arises, the stage will try keep a log, Hmily have built-in scheduler thread pool to recover, do not worry.
That hmily, how states perceive it? Is also very simple, it is the programming section, the core logic of the following lines

TCC transaction model of distributed transactions

We use, as long as the annotation tells framework confirm what method of execution by @Tcc, cancel method performs what can be! The other to the frame to help you deal with!
Well, not much to say, say anything hmily source is resolved, we have time to get to know yourself!

Digging

If you hit that, calls between different platforms, how would you ensure that the transaction? For example, my bank transfer service to the interface, you think there might allow banks to take your tcc frame it? Or let the bank take your news columns? Do you think it realistic? Of course, some people will say: "One http direct tune." Ah, teenagers have an idea!
ok, so the industry for such service calls related to third-party interfaces, and how to ensure consistency? We ponder.

Guess you like

Origin blog.51cto.com/14230003/2427322