Use mongodb driver to solve distributed transactions
Use mongodb driver to solve distributed transactions
Solving distributed transaction scenarios
This project can only solve distributed transactions in multiple tables in the same database in the same replica-set in mongodb.
Main principles
The mongodb server does not strictly bind the transaction in which the transaction is in, and the mongodb driver can initiate transactions to achieve this. Changing this solution will extend the mongodb golang driver code, It can be used for verification in mongodb driver 1.1.2 release and mongodb 4.0.x-4.2.x replica-set mode
mongodb golang driver code extension content
Extension code:
mongo/session_exposer.go
x/mongo/driver/session/session_ext.go
The extension code is mainly the underlying logic, used to activate the transaction and bind the transaction. Does not contain business logic
Related test cases:
transcation/transaction_test.go
Upper-level use logic:
transcation/transaction.go
Encapsulate business logic, realize business-level management interfaces such as opening, committing, and rolling back transactions, and provide an operation interface for the transaction uuid and the cursor id record operation of the statement execution within the transaction. In actual use, the transaction uuid and in-service statement execution cursor id need to be stored centrally, and all service instances need to be available and used. You can use redis and mysql as central storage
Now that the real transaction id of mongodb is directly exposed, there may be security risks in passing between different service nodes.
Specific implementation
- Activate a transaction, generate a transaction uuid (mongodb driver provides the generation method), transaction/transaction.go: StartTransaction
- Activate a session through the uuid of the transaction and join a transaction of the mongodb server, transaction/transaction.go: ReloadSession
- Bind the transaction to the session, get the SessionContext, mongo/session_exposer.go:TxnContextWithSession
- Perform curl operations
Solution implementation
Use items:
Blueking Configuration Platform
Drainage
Bson decoding interface priority uses the actual type as the final object, such as slice, map In the mongodb golang official driver, when bson uses interface at the top level, it puts slice and map in an object called primitive.D (golang []interface type).