🐛 Fix using the shared session when updating a document#227
🐛 Fix using the shared session when updating a document#227art049 merged 12 commits intoart049:masterfrom
Conversation
…enable transactions
b63eddf to
f5ce01a
Compare
Codecov Report
@@ Coverage Diff @@
## master #227 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 38 38
Lines 2725 2749 +24
Branches 413 416 +3
=========================================
+ Hits 2725 2749 +24
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
|
Hey thanks for the PR! @Olegt0rr started to work something out a while ago in #89, I was planning on merging it but as you mentioned the outcome with automatic server type detection makes the whole thing a bit flaky depending on the versions. Probably it would be wiser to postpone this automatic detection to later including maybe a higher-level session object which would be more developer-friendly.
Very interesting finding, I wasn't aware of this at all. I really wonder about the performance outcome of stripping away the
LGTM, anyway it's nice to have to be able to handle sessions/transactions outside and have some flexibility. I think we just lack a tiny bit of docs, probably "Working with transactions" in /docs/engine.md with a small example, and then we're good to go! |
|
I'm glad to see that project is not abandoned :) |
|
Thanks for the feedback @art049 and @Olegt0rr ! 🚀 And thanks for all the points and ideas @art049. 🤓 About removing I just added a couple of examples to the docs! 📝 One just with a session, and one with a session and a transaction. ✔️ |
|
Awesome @tiangolo, thank you! Can't wait to see the bulk writes, for sure this will improve the overall ODM performances! |
🐛 Fix using the shared session when updating a document
Currently, the code creates a session and transaction and passes down the session in the parameters through all the function calls, but it's not passed in the last point, which would make motor use the session.
The original intention of this PR was just to pass that
session, a one-line/argument change, but I found a couple of bugs and caveats along the way and the PR got bigger.I'm not sure I should add tests for this, I was trying to add them but I saw that I would have to add a bunch of mocks for the internals, or add strange tricks to break the save in the middle and see that the session didn't finish... but then any of those things seemed like a very complex trick to enable a test that would mostly test the trick, and not really the session. Not sure if I should do anything else. Let me know!Edit 2022-06-23 Transactions in Standalone
I see that transactions are only supported in MongoDB clusters with shards or replicas. And there's no simple way to detect the type of cluster in code. When using a transaction on a standalone MongoDB I get this error:
That error is currently not being thrown because sessions are currently not used (which is what this intends to fix).
Detecting if the current cluster is replicated, sharded, or standalone (unsupported) would require a lot of complex and fragile logic, as even though transactions are not supported in standalone, there's no way to ask the cluster what's the current type of deployment.
To solve that, I moved the transactions away from the internal code (they were not used anyway), and added support for passing a session to
engine.save()andengine.save_all(), this way, a user could create a session outside, create a transaction (after confirming they have a supported deployment) and then pass the session to these methods.Edit 2022-06-23 B Same session concurrently
I'm seeing that the same session is not expected to be used concurrently 😔
https://motor.readthedocs.io/en/3.0.0/api-asyncio/asyncio_motor_client.html#motor.motor_asyncio.AsyncIOMotorClient.start_session
So using
asyncio.gather()should not be used as the document saves are expected to share the same session.Edit 2022-06-24 Tests for transactions
I added a couple of tests to confirm that external transactions work with
engine.save()andengine.save_all(). Because transactions require a cluster with replicas, those tests are only run on the replica version.