Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the seata source code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the seata source code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Seata source code reading notes

This article does not have the use of seata, how to use seata can refer to the official example, very detailed.

This article is based on v0.8.0, and there is no code posted in this article.

There are three important parts of seata:

TC: the transaction coordinator, which maintains the state of global and branch transactions, drives global commit or rollback, is the server side of seata.

TM: transaction manager, start the global transaction, commit or roll back the global transaction.

RM: resource manager that manages resources for branch transactions that are being processed, registers and reports the status of branch transactions with TC, and drives the commit or rollback of branch transactions.

Initialization of seata

TC start

RM sends three parameters, jdbcUrl, applicationId and transactionServiceGroup, to TC to register and establish a connection. The entry is when the DataSourceProxy is instantiated

TM sends two parameters, applicationId and transactionServiceGroup, to TC to register and establish a connection.

TM- handles global transactions

The initiator of the global transaction is the way we annotate @ GlobalTransactional, and seata will represent our method and complete the global transaction through the following steps.

Send global transaction start request to TC,TC and return xid (global transaction unique id) to bind xid to the current thread

Execute our business logic

The business logic is successful. Send a global transaction commit request to TC. If it fails, try again.

Business logic exception (configurable rollback rules, that is, which exception rollback), send a global transaction rollback request to TC, and try again if it fails

Clean up resources, transaction end

Source code reading entry: io.seata.spring.annotation.GlobalTransactionalInterceptor#invoke

RM- handles branch transactions

Rm needs to proxy the data sources in our project. In this step, we need to modify our own code, as follows:

@ Configurationpublic class DataSourceConfig {@ Bean @ ConfigurationProperties (prefix = "spring.datasource") public DataSource druidDataSource () {return new DruidDataSource ();} / * you need to set DataSourceProxy as the primary data source, otherwise the transaction cannot be rolled back * / @ Primary @ Bean ("dataSource") public DataSource dataSource (DataSource druidDataSource) {return new io.seata.rm.datasource.DataSourceProxy (druidDataSource);}

The main purpose of the proxy data source is to proxy the database connection so that you can control the commit and rollback of the branch transaction.

Seate's rm-datasoure module has these proxy classes DataSourceProxy, ConnectionProxy, and StatementProxy. By proxying these classes in jdbc, let our sql execute through ExecuteTemplate. This class is the entrance for us to understand how seata controls branch transactions. Let's look at the processing steps of the branch transaction:

Bind xid to the current database connection (where did xid come from? In the back)

Get the data record beforeImage in the database table before sql execution (just the affected data and fields, which is done by parsing our sql)

Execute our business sql

Get the data record afterImage in the database table after sql execution

Use the primary key value of the changed data as the lockKey and save it to the current database connection

Convert beforeImage and afterImage to undo log and save to the current database connection

When a global lock request is sent to TC (with xid and lockKey), TC returns branchId, which means that the global lock has been acquired. If it is not successful, it will be retried (default is 30 times, with an interval of 10ms each time). This global lock is designed to prevent multiple users from modifying the same data at the same time, resulting in data errors during the rollback operation via undo_log. The lock will be released by TC after the PhaseTwo is completed.

Save undoLog to the undo_log table in the database

Commit database transaction

Report the result (failure or success) of the current branch transaction commit to TC, and retry if it is unsuccessful (default 5 times)

After the commit succeeds, clean up the xid bound to the current database connection

After the commit fails, an exception is thrown up to let tm know that the global transaction is rolled back

This is the whole process of PhaseOne. Let's look at PhaseTwo:

After receiving the global transaction commit or rollback request from tm, tc will notify all branch transactions in the global transaction. After receiving the notification, rm will roll back or commit.

Rollback: remove database undo_log for data compensation and restore. Delete undo_log; submission after success: delete undo_log directly

Return the result of tc processing

Source code reading entry: io.seata.rm.datasource.StatementProxy#execute

Transitive dubbo of XID in Service Link

Using dubbo's filter to achieve, source code: io.seata.integration.dubbo.TransactionPropagationFilter

The principle is that the upstream puts the xid into the RpcContext in the filter, and the downstream gets the xid from RpcContext.

RestTemplate and Feign

Support for RestTemplate and Feign is not in seata-all, but in spring-cloud-alibaba-seata

Source entry:

Com.alibaba.cloud.seata.rest.SeataRestTemplateAutoConfigurationcom.alibaba.cloud.seata.feign.SeataFeignClientAutoConfigurationcom.alibaba.cloud.seata.web.SeataHandlerInterceptorConfiguration

The principle is that the upstream puts the xid into the requested header through the interceptor, and the downstream gets the xid from the header through the interceptor.

Seata also supports many RPC frameworks, such as sofa-rpc, motan, etc. We can get seata to support our own rpc framework in a similar way.

GlobalLock comments

If it is a local business method decorated with GlobalLock, although the method is not a branch transaction under a global transaction, its operation on the data resource also needs to query the global lock first, and if there are other Seata global transactions being modified, the method also needs to wait. Therefore, if you want the database not to be modified by other transactions during the execution of the Seata global transaction, this method needs to force the addition of GlobalLock annotations to bring it into the management scope of the Seata distributed transaction.

The function is somewhat similar to the @ Transactional annotation of Spring. If you want to open the transaction, you must add the annotation. If you do not add it, then the transaction function will not take effect, and so may the business BUG;Seata. If you want a SQL operation that is not under the global transaction to not affect the AT distributed transaction, you must add the GlobalLock annotation.

High availability Design of TC

The seata server supports zk, nacos, eureka and so on as service discovery. Data sharing is realized through the database. Global transaction Session information, branch transaction Session information and global lock information are all placed in the database.

TCC mode

The difference between TCC and AT is mainly reflected in the RM side, TC and TM are the same.

TCC's RM will not proxy our data source, but we will specify the rollback and commit logic ourselves. In the second phase, when TM initiates a global transaction commit rollback, RM only needs to execute the rollback and commit methods we specified.

This model means that there are more changes in our business code and it is more efficient than the AT model.

RM one-stage source code entry: io.seata.spring.tcc.TccActionInterceptor#invoke

RM two-phase source code entry: io.seata.rm.tcc.TCCResourceManager#branchCommit#branchRollback

This is the end of the content of "what is the seata source code". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report