apavlo / py-tpcc

Python implementation of TPC-C
142 stars 91 forks source link

Problems about MongoDB Transaction API #15

Closed yoyolicoris closed 3 years ago

yoyolicoris commented 3 years ago

Hi, I'm interesting in benchmarking MongoDB Transaction performances, and I found your project seems to fit my requirement.

But I found that there is no api that support transaction in the document of pymongo 2.7.2,and I cannot find any similar function calls like the following (taken from https://docs.mongodb.com/manual/core/transactions/) in your code:

# For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
# uriString = 'mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl'
# For a sharded cluster, connect to the mongos instances; e.g.
# uriString = 'mongodb://mongos0.example.com:27017,mongos1.example.com:27017/'

client = MongoClient(uriString)
wc_majority = WriteConcern("majority", wtimeout=1000)

# Prereq: Create collections.
client.get_database(
    "mydb1", write_concern=wc_majority).foo.insert_one({'abc': 0})
client.get_database(
    "mydb2", write_concern=wc_majority).bar.insert_one({'xyz': 0})

# Step 1: Define the callback that specifies the sequence of operations to perform inside the transactions.
def callback(session):
    collection_one = session.client.mydb1.foo
    collection_two = session.client.mydb2.bar

    # Important:: You must pass the session to the operations.
    collection_one.insert_one({'abc': 1}, session=session)
    collection_two.insert_one({'xyz': 999}, session=session)

# Step 2: Start a client session.
with client.start_session() as session:
    # Step 3: Use with_transaction to start a transaction, execute the callback, and commit (or abort on error).
    session.with_transaction(
        callback, read_concern=ReadConcern('local'),
        write_concern=wc_majority,
        read_preference=ReadPreference.PRIMARY)

Did I misunderstand anything ? How do you implement the atomic transaction operations for MongoDB? Thanks~

yoyolicoris commented 3 years ago

sorry, wrong repo (I should look at https://github.com/mongodb-labs/py-tpcc).