EasyTransaction major source analysis

EasyTransaction is a full-featured framework for distributed transaction, the following features excerpt from its home page: https://github.com/QNJR-GROUP/EasyTransaction

  • A frame comprising a plurality of transaction forms, to get a frame of all types of transaction
  • A variety of forms can be mixed Affairs
  • High-performance, most of the business system bottlenecks in the database business, if power and other functions enable the framework of the additional consumption of business database only write 25 bytes of row
  • Optional frame comes idempotent order to achieve and call processing disorder, significantly reduce the workload of business development, but also enabled the increase in power of a business database and other control lines
  • Business code can be completely without intrusion
  • Support nested transactions
  • No additional deployment coordinator, different APP service coordination initiated its own affairs, but also to avoid a single point of failure
  • Implementation of a distributed transaction ID may be associated with a distributed transaction service ID, service type, APPID, to facilitate monitoring of each service

In this paper, we share the role of EasyTransaction core of each package of its main achievement.

Read Seata architecture than to think https://www.cnblogs.com/skyesx/p/10674700.html , combined with debugging code, and demo to see this, to see directly if a point here too fragmented

A, context pack

The main class

LogProcessContext

ET transaction for storing context information. In the open transaction ET (ET first long-distance call, or active call startSoftTrans method), will create an instance of this class and bind it to the local context Spring's transaction, by:

TransactionSynchronizationManager.bindResource()

To perform the binding. When the need to obtain ET context, by

TransactionSynchronizationManager.getResource()

Achieved.

The main contents ET context included are:

  • The final state of affairs
  • All of the global transaction log
  • Not Flush to external global transaction log
  • Contents of the transaction ID, etc.

Second, the core package

This package is mainly class

EasyTransFacade
TransactionHook
ConsistentGuardian
ExecuteCacheManager

Class EasyTransFacade

It defines the interface of business caller, contains only two:

public void startEasyTrans(String busCode,long trxId);

public <P extends EasyTransRequest<R,E>,E extends EasyTransExecutor, R extends Serializable> Future<R> execute(P params);

The first is used to enable global transaction, the main operations are:

  • Spring mount TransactionHook local to the current transaction, so that the code can be embedded in the ET key nodes (e.g. local transaction commits before, the local transaction is rolled back, etc.)
  • The LogProcessContext bound to the current Spring local affairs, so that the state can obtain ET ET global transactions in the current Spring local transaction at any time.
  • In the current is turned on local affairs, recorded a write transaction to execute business library, which plays a key role in identifying global transaction when the state Crash recovery

The second represents the implementation of a remote transaction method.

  • Object parameters by calling the corresponding Class obtain a corresponding processor (e.g., processor TCC, reliable messaging processor, etc.) and execute the call, the specific form of the subsequent section dedicated to continue the call

Based annotation interface calls are made by encapsulating these two methods.

Class TransactionHook

The junction point of the main frame ET native code Spring transaction, ET TransactionSynchronization defined by the method, during the execution of a local transaction Spring, extended support global transactions. Major expansion of the following two methods

beforeCommit(boolean readOnly)
afterCompletion(int status)

beforeCommit method will

  • Spring before the transaction commits all local global transaction log does not fall off the disc tray
  • And perform all remote calls are not executed (ET will try to delay the accumulation of this global transaction and batch execution)
  • If unsuccessful global transaction, an exception is thrown, roll back the transaction (including local and global)

afterCompletion method will

  • Gets the local affairs of the final result (commit / rollback / unknown) and the status of ET parent transaction (commit / rollback / unknown) to the decisions of the final state of affairs ET (commit / rollback / unknown)
  • After the final stage of this state of affairs ET, asynchronous execution of a final agreement process (call consistentGuardian.process)

类ConsistentGuardian

This class is used to process the final ET same global transaction, such as sending a message to TCC Conifrim / Cancel, reliable message.

The final matching process is usually thrown in synchronous operation (TCC TRY-like) corresponding to a local transaction execution is completed asynchronous execution thread pool, but failed, then there will reveal all the details of compensation (Recovery package), then the subsequent detailed account

The main mechanism of the class based on the global transaction log is written before, corresponding to the log acquisition processor (e.g., acquired from the processor corresponding to the log TCC TCC transaction log), thereby

  • Analyzing the final state of the current transaction ET (ET if the current state of affairs if not yet determined)
  • A typical example of the process of passing the final state to the transaction log handler ET, corresponding to the transaction log are sequentially processed, the processing of:
    • When the corresponding log exists method TRY
    • TRY and can not find the corresponding CONFRIM / CANCEL log
    • According to the final ET transaction state, call the corresponding CONFIRM / CANCEL method

Class ExecuteCacheManager

This class mainly serves the following expectations ET

  • ET write batch transaction log (to reduce IO)
  • Batch concurrent execution of remote service invocation (to reduce the corresponding remote serial waiting time)

Its main achievement is that

  • For each incoming Calleble objects are returned through a rewrite of the object Futrure
  • When a Futrue before any get methods have not been called, all passed before Callable object will not be executed.
  • When any one of the Future of get is called, all will be callable batch execution, written to the log here include batch and batch remote execution of concurrent calls

Third, the package datasource

Mainly it includes the following two interfaces, which acts on the main service data source.

DataSourceSelector
TransStatusLogger

Class DataSourceSelector

This class is used to acquire the current transaction / request corresponding to the data source and the transaction manager, if a plurality of application service data sources is required to achieve the corresponding data source selector itself, the method mainly includes the following

DataSource selectDataSource(String appId,String busCode,long trxId);
DataSource selectDataSource(String appId,String busCode,EasyTransRequest<?, ?> request);

The first method is to select the corresponding opening when ET transaction data source

The second method is selected when the corresponding data source receives the caller's request (for idempotent, anti-suspension process, if need be ignored)

This interface contains a default implementation, when only a single data source can be directly implemented by the

SingleDataSourceSelector

Class TransStatusLogger

This class is mainly used to record write transaction status is determined for ET, the recording will be turned on when the ET transaction, write the current database table, with the corresponding business transaction (the Spring local transaction) and submitted to commit, rollback rollback.

More details, please see the direct implementation

Fourth, the package executor

The package is stored in the transaction initiator (remote caller to the service) position-related processing classes, different types of transactions (TCC, reliable transactions, etc.) have different Executor, TCC example to explain to other types of transaction implementation are similar.

TccMethodExecutor

This class implements three interfaces

  • EasyTransExecutor
  • LogProcessor
  • DemiLogEventHandler

EasyTransExecutor interface defines the methods

    <P extends EasyTransRequest<R,E>,E extends EasyTransExecutor,R  extends Serializable> Future<R> execute(Integer sameBusinessCallSeq, P params);

The method used for EasyTransFacade.execute class, which corresponds in the execution TCC TRY method, specifically, it

  • The method calls corresponding TRY RPC request class packaged Runnable
  • Construction of a global transaction log corresponding to this call (mainly containing the specific parameters of this call, the corresponding remote method)
  • Then pass ExecuteCacheManager class method mentioned in the above sections

LogProcessor interface defines how to handle the transaction log, which contains a main method

    boolean logProcess(LogProcessContext ctx, Content currentContent) 
    

The method will determine if the incoming log type is PreTccCallContent (TCC TRY log corresponding to the request), it will monitor the final log pairing information (category ConsistentGuardian will after processing the current ET transaction log, sending a message to inform All matched results need to pair the logs), if

  • Overheard successfully paired (or CANCEL find the corresponding log CONFIRM) message, the subsequent process do not
  • Pairing failure monitor (corresponding to the absence of the CONFIRM / CANCEL log) message, executes the corresponding operation CONFIRM or CANCEL ET transaction based on the current state and the corresponding log record

Implement other form of transaction is similar, not repeat them

Fifth, the recovery package

To reveal all the details to recover a transaction achieve final agreement.

Code is not complicated, you can view on their own.

Six Pack Filter

This package is mainly used for ET corresponding Filter, which acts on the called side Filter. We can be invoked by Filter extension implementation ET function ports, such as processing power, etc., handle nested transactions, an increase of processing the call context, and so on.

Seven, pack idempotent

Achieve idempotent side corresponding packet processing such as suspension

And other major powers and the principle of anti-suspension process:

  • When the remote call over, write the log to the current call service opening log, and records the corresponding call ID, call parameters corresponding MD5
  • The results will be returned when the update result is stored in the log
  • When the repeat request over checks record exists corresponding to the ID, whether it is checked if there MD5 consistent parameter, store the results if they are consistent before return
  • Anti suspension similarly, in the log, it will record what method is invoked, as
    • When the log corresponding to the request can not be found, but to cancel the current operation, it will directly return a successful frame
    • After the above-mentioned cancel been successfully performed, the time came to try the method again, found that cancel has been executed, an error is returned directly to try

Eight, package idgen

For generating a distributed transaction ID in the ET, when formulated its own ID, corresponding to the method of the package will not be called. When not specified ID, it will automatically generate a.

Nine, the package log

ET defined transaction log corresponding to Class, and its read-write interface.
The transaction log has been mentioned in TccExecutor chapters before, not repeat them.

We need to expand the transaction log storage to achieve, to directly implement the following interface

TransactionLogReader
TransactionLogWritter

(ET transaction log does not need to read and write and business transactions in the same transaction, nor in the same transaction)

Ten, package master

For selecting as a final fallback master consistent compensated for in the same package in a appId.

In fact, choose not too precise, any instance can a appId under, can have multiple simultaneous master exist (but does not make sense, also a waste of performance)

XI packet monitor

ET instance state for providing packages, the Dashboard for, monitoring the use of extended

XII packet protocol

For customers to directly define a distributed transaction service package, which includes the parent class of some customers directly, the configuration interface, configuration notes, etc.

XIII package provider.factory

Acquired and stored from the corresponding Spring bean implemented so as to quickly and easily by the corresponding definition ET acquires the corresponding bean

Fourth, the package queue

To add extensions corresponding message queue implementation, the package is achieved corresponding interface

XV rpc package

Ditto

Sixteen, package serialization

ET frame serialized form to be used, self-expansion

XVII package stingcodec

A compression string, the replacement string is digital id, to improve storage efficiency

Guess you like

Origin www.cnblogs.com/skyesx/p/11111726.html