Consider a 4 member MongoDB replica set. Consider also that we use writeconcern(w:4) during insertions to achieve consistency. Then, if one of the replica set members is off, the insertion will "fail". However, the insertion is actually done in the three active members of the replica.
What I would like is that, if there is a writeconcern error no insertion is performed. I have tried with transactions but it is not working. Here is the code in pymongo:
from pymongo import MongoClient
from pymongo.write_concern import WriteConcern
from pymongo.read_concern import ReadConcern
from pymongo import ReadPreference
# one of the servers is down
uriString = "127.0.0.1:27001,127.0.0.1:27002,127.0.0.1:27003,127.0.0.1:27004"
client = MongoClient(uriString)
wc_rule = WriteConcern(4, wtimeout=1000)
# Prereq: Create collections.
db1 = client.get_database("mydb1")
db2 = client.get_database("mydb2")
db1.foo.drop()
db2.bar.drop()
db1.foo.insert_one({"_id":0, "abc": 0})
db2.bar.insert_one({"_id":0,"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
# estas operaciones se revertirán si no se pueden hacer juntas
collection_one.insert_one({"_id":1,"abc": 1}, session=session)
collection_two.insert_one({"_id":1,"xyz": 999}, session=session) # cambiar 0 por 1 para que no dé error
# 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_rule,
read_preference=ReadPreference.PRIMARY,
)
I expected that the insertions in the callback are not performed, because the insertion cannot be done in the 4 members of the replcia set, but its working and the result is:
db1.foo
{'_id': 0, 'abc': 0}
{'_id': 1, 'abc': 1}
db2.bar
{'_id': 0, 'xyz': 0}
{'_id': 1, 'xyz': 999}
What am I doing wrong? Can these insertions be removed in some way when the write concer fails?