当前位置:网站首页>Analysis of distributed transaction solution Seata golang
Analysis of distributed transaction solution Seata golang
2022-06-28 04:48:00 【May Hacker】
List of articles
Preface
Seata It's easy to use , High performance 、 Open source one stop distributed transaction solution , Open source by Alibaba .
2020 year 4 month , Liuxiaomin started to base on Seata Start multilingual golang project , After a year of development ,seata-golang Released 1.0.0 edition .
Seata framework

● TC:Transaction coordinator, It's a distributed transaction coordinator .
● TM:Transaction manager, It's a transaction manager , Responsible for opening global transactions 、 Commit and roll back .
● RM:Resource Manager, It manages branch transaction resources , After it joins the global transaction group , towards TC Report the execution status of branch transactions .
● XID:TM When opening a global transaction , Will be in TC Create a GlobalSession,GlobalSession The globally unique ID of is XID.
● BranchID:RM towards TC After registering branch transactions , stay TC The side generates a BranchSession,BranchID Globally unique identification of this BranchSession.
When RM towards TC When reporting branch execution failure ,TC Will mark this BranchSession The status of is failure , then TM When a rollback is initiated ,TC according to XID Find all successfully executed transaction branches , Tell them to roll back .
The following is a complete process for ordering and purchasing 
The user sends an order placing request to the aggregation layer
- Polymeric layer TM towards TC Request to start a global transaction ,TC establish
GlobalSession, A session representing a global transaction - TC Back to the aggregation layer TM One Xid, Used to mark this global transaction , Then the polymerization layer TM To obtain the Xid To order service and inventory service respectively
- Order service to TC Registration transaction branch ,TC Will create a
BranchSession, And added to the global transaction - TC Return to the order service BranchId, Used to uniquely represent this branch transaction
- Inventory service to TC Registration transaction branch ,TC Will create a
BranchSession, And added to the global transaction - TC Return to the inventory service
BranchId, Used to uniquely represent this branch transaction - If the inventory service fails to execute the local logic , Will send to TC The report , launch
BranchReportRequest; And will return err to TM, - TM After perceiving that the inventory service local branch transaction execution fails ,TM Will send to TC Initiate a global rollback , namely
GlobalRollbackRequest - TC Consider the global transaction failed , Notify the... Of each service RM, Give Way RM Initiate rollback at the local transaction branch
Refer to the... Involved in the above process seata Methods are abstracted :
You can ignore SAGA Pattern ,seata-golang Not implemented in 
Seata Golang Supported patterns
Seata-Golang Support AT and TCC Two modes .
In a nutshell :
AT Depending on the local database transaction characteristics , Including rollback 、 Submission, etc
TCC The pattern relies on the user-defined implementation to rollback and commit transactions , Abstract out three methods :Try、Confirm、Cancel, That's why it's called TCC
AT Pattern
AT The rollback of the pattern depends on the compensation for the operation performed ,seata-golang be based on TIDB Of Parser To identify what the user has done , Constructed as Undo Log Then rollback compensation is performed for these operations .
TCC Pattern
Rely on developers to implement seata Of TCC Interface , That is, customize Try、Confirm、Cancel Methods
There are many constraints on developers
Seata Source analyses
1.TC Global transaction coordinator
type TransactionCoordinator struct {
sync.Mutex
maxCommitRetryTimeout int64
maxRollbackRetryTimeout int64
rollbackDeadSeconds int64
rollbackRetryTimeoutUnlockEnable bool
asyncCommittingRetryPeriod time.Duration
committingRetryPeriod time.Duration
rollingBackRetryPeriod time.Duration
timeoutRetryPeriod time.Duration
streamMessageTimeout time.Duration
holder holder.SessionHolderInterface
resourceDataLocker lock.LockManagerInterface
locker GlobalSessionLocker
idGenerator *atomic.Uint64
futures *sync.Map
activeApplications *sync.Map
callBackMessages *sync.Map
}
Begin() Method to start a global transaction
func (tc *TransactionCoordinator) Begin(ctx context.Context, request *apis.GlobalBeginRequest) (*apis.GlobalBeginResponse, error) {
// Generate XID
transactionID := uuid.NextID()
xid := common.GenerateXID(request.Addressing, transactionID)
// Global transaction
gt := model.GlobalTransaction{
// The global session
GlobalSession: &apis.GlobalSession{
Addressing: request.Addressing,
XID: xid,
TransactionID: transactionID,
TransactionName: request.TransactionName,
Timeout: request.Timeout,
},
}
// Open global transaction , In fact, the state of the global transaction is modified
gt.Begin()
// Add global transactions to holder in
err := tc.holder.AddGlobalSession(gt.GlobalSession)
if err != nil {
return &apis.GlobalBeginResponse{
ResultCode: apis.ResultCodeFailed,
ExceptionCode: apis.BeginFailed,
Message: err.Error(),
}, nil
}
// Start the coroutines , Receive global transaction events
runtime.GoWithRecover(func() {
evt := event.NewGlobalTransactionEvent(gt.TransactionID, event.RoleTC, gt.TransactionName, gt.BeginTime, 0, gt.Status)
event.EventBus.GlobalTransactionEventChannel <- evt
}, nil)
log.Infof("successfully begin global transaction xid = {}", gt.XID)
return &apis.GlobalBeginResponse{
ResultCode: apis.ResultCodeSuccess,
XID: xid,
}, nil
}
TC The global transaction coordinator has three concepts
1.1 LockManager
AT In mode , Lock the data on the branch transaction to TC On , Ensure the isolation of transaction data 
1.2 SessionManager
management BranchSession, Record transaction execution status 、 Persistent transaction data
Now there are two ways to do it :
- FileBasedSessionManager: The transaction data is saved to memory , And persist to local files
- DBBasedSessionManager: The transaction data is persisted to the database , In this way ,TC High availability
You may consider ETCD Or right TC do Raft, It is the issue that the follow-up community will consider .
1.3 Transaction Manager
namely TM, Transaction manager , We'll talk about that
2.TM Transaction manager
According to the previous description ,TM Responsible for some global operations , Including the request TC Open global transaction 、 Global commit 、 Global rollback and other operations
type TransactionManagerInterface interface {
// GlobalStatus_Begin a new global transaction.
Begin(ctx context.Context, name string, timeout int32) (string, error)
// Global commit.
Commit(ctx context.Context, xid string) (apis.GlobalSession_GlobalStatus, error)
// Global rollback.
Rollback(ctx context.Context, xid string) (apis.GlobalSession_GlobalStatus, error)
// Get current status of the give transaction.
GetStatus(ctx context.Context, xid string) (apis.GlobalSession_GlobalStatus, error)
// Global report.
GlobalReport(ctx context.Context, xid string, globalStatus apis.GlobalSession_GlobalStatus) (apis.GlobalSession_GlobalStatus, error)
}
It is worth noting that ,TransactionManagerInterface The method in , It's a notice TC Do the corresponding operation
With Rollback Methods as an example , The concrete implementation is the request TC Roll back global transactions ,TC We'll inform everyone RM Rollback the local branch transaction .
func (manager *TransactionManager) Rollback(ctx context.Context, xid string) (apis.GlobalSession_GlobalStatus, error) {
// RPC request TC, Let it roll back the global transaction
request := &apis.GlobalRollbackRequest{
XID: xid}
resp, err := manager.rpcClient.Rollback(ctx, request)
if err != nil {
return 0, err
}
if resp.ResultCode == apis.ResultCodeSuccess {
return resp.GlobalStatus, nil
}
return 0, &exception.TransactionException{
Code: resp.GetExceptionCode(),
Message: resp.GetMessage(),
}
}
3.RM Explorer
RM Mainly responsible for the management of local branch affairs
type ResourceManagerInterface interface {
BranchCommit(ctx context.Context, request *apis.BranchCommitRequest) (*apis.BranchCommitResponse, error)
BranchRollback(ctx context.Context, request *apis.BranchRollbackRequest) (*apis.BranchRollbackResponse, error)
// RegisterResource Register a Resource to be managed by Resource Manager.
RegisterResource(resource model.Resource)
// UnregisterResource Unregister a Resource from the Resource Manager.
UnregisterResource(resource model.Resource)
// GetBranchType ...
GetBranchType() apis.BranchSession_BranchType
}
besides ,RM It is also responsible for starting the global transaction , towards TC Registration transaction branch , therefore seata-golang Extract three notifications TC The interface of
type ResourceManagerOutbound interface {
// BranchRegister towards TC Registration transaction branch
BranchRegister(ctx context.Context, xid string, resourceID string, branchType apis.BranchSession_BranchType,
applicationData []byte, lockKeys string) (int64, error)
// BranchReport Report the execution status of branch transactions
BranchReport(ctx context.Context, xid string, branchID int64, branchType apis.BranchSession_BranchType,
status apis.BranchSession_BranchStatus, applicationData []byte) error
// LockQuery lock resource by lockKeys.
LockQuery(ctx context.Context, xid string, resourceID string, branchType apis.BranchSession_BranchType, lockKeys string) (bool, error)
}
边栏推荐
- Resolve cross domain
- Ppt production tips
- Establishment of SSH Framework (Part 2)
- Severe tire damage: the first rock band in the world to broadcast live on the Internet
- [csp-j2020] excellent splitting
- [applet] solution document using font awesome Font Icon (picture and text)
- Project practice! Teach you JMeter performance test hand in hand
- 请问下,mysqlcdc设置多并行度的话,增量数据是不是会重复?
- The coming wave of Web3
- 2022年中國音頻市場年度綜合分析
猜你喜欢

Design a stack with getmin function

Matlab exercises -- exercises related to symbolic operation

Play with double pointer
![[CSP-J2020] 优秀的拆分](/img/05/90f9cf71791b3cdc37073eaf5db989.png)
[CSP-J2020] 优秀的拆分
![[early knowledge of activities] list of recent activities of livevideostack](/img/aa/8b14f9863cd675a7be06a0c3855a93.png)
[early knowledge of activities] list of recent activities of livevideostack

Google Earth engine (GEE) - global flood database V1 (2000-2018)

云厂商为什么都在冲这个KPI?

Detailed reading of the thesis: implementing volume models for handowriting text recognition

Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation

With the transformation of automatic empowerment, Feihe dairy accelerates its move towards digitalization!
随机推荐
A doctor's 22 years in Huawei (full of dry goods)
100+数据科学面试问题和答案总结 - 机器学习和深度学习
JVM I: introduction to JVM and understanding of class files
Mask's miserable and inspirational childhood, who is introverted by campus violence
inherit
How can we speed up the chunksplitter when CDC extracts MySQL data in full?
Sum of squares of each bit of a number
filinCdc 的sql,多表的时候总报这个错,请问下该怎么解决呀
Matlab exercises -- exercises related to symbolic operation
[matlab traffic light identification] traffic light identification [including GUI source code 1908]
Principle and implementation of SSD for target detection
10:00面试,10:02就出来了 ,问的实在是太...
Go language -select statement
The development of the Internet has promoted the emergence of a series of new models such as unbounded retail, digital retail and instant retail
If mysqlcdc sets multiple parallelism, will the incremental data repeat?
[NOIP2002 普及组] 过河卒
Idle interrupt cannot be cleared
RT thread bidirectional linked list (learning notes)
Where does the storm go? Whose pot is the weather forecast wrong?
Severe tire damage: the first rock band in the world to broadcast live on the Internet