mongodb_driver_transcation

module
v0.0.0-...-69156e0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2022 License: Apache-2.0

README

中文文档

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:

Transaction/Transaction_test.go

Upper-level use logic:

Transaction/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.

Now that the real transaction id of mongodb is directly exposed, there may be security risks in passing between different service nodes.

Specific implementation
  1. Activate a transaction, generate a transaction uuid (mongodb driver provides the generation method), Transaction/Transaction.go: StartTransaction
  2. Activate a session through the uuid of the transaction and join a transaction of the mongodb server, Transaction/Transaction.go: ReloadSession
  3. Bind the transaction to the session, get the SessionContext, mongo/session_exposer.go:TxnContextWithSession
  4. Perform curl operations
Solution implementation

Use project:

BlueKing Configuration Platform

Drainage

bson decode register

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).

Directories

Path Synopsis
Package bson is a library for reading, writing, and manipulating BSON.
Package bson is a library for reading, writing, and manipulating BSON.
bsoncodec
Package bsoncodec provides a system for encoding values to BSON representations and decoding values from BSON representations.
Package bsoncodec provides a system for encoding values to BSON representations and decoding values from BSON representations.
bsonrw
Package bsonrw contains abstractions for reading and writing BSON and BSON like types from sources.
Package bsonrw contains abstractions for reading and writing BSON and BSON like types from sources.
bsontype
Package bsontype is a utility package that contains types for each BSON type and the a stringifier for the Type to enable easier debugging when working with BSON.
Package bsontype is a utility package that contains types for each BSON type and the a stringifier for the Type to enable easier debugging when working with BSON.
primitive
Package primitive contains types similar to Go primitives for BSON types can do not have direct Go primitive representations.
Package primitive contains types similar to Go primitives for BSON types can do not have direct Go primitive representations.
cmd
examples
testutil/israce
Package israce reports if the Go race detector is enabled.
Package israce reports if the Go race detector is enabled.
Package mongo provides a MongoDB Driver API for Go.
Package mongo provides a MongoDB Driver API for Go.
x
bsonx/bsoncore
Package bsoncore contains functions that can be used to encode and decode BSON elements and values to or from a slice of bytes.
Package bsoncore contains functions that can be used to encode and decode BSON elements and values to or from a slice of bytes.
mongo/driver/auth
Package auth is not for public use.
Package auth is not for public use.
mongo/driver/topology
Package topology contains types that handles the discovery, monitoring, and selection of servers.
Package topology contains types that handles the discovery, monitoring, and selection of servers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL