Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction support #2465

Merged
merged 14 commits into from
Nov 27, 2022
Merged

Transaction support #2465

merged 14 commits into from
Nov 27, 2022

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Nov 14, 2022

Note: contains changes from #2462 folded into a single commit; I'd prefer merging that PR before taking on this one.

This takes the work started by @klinson in #1904 and addresses some of the feedback left in that PR:

  • Transactions can't be nested. This was done as MongoDB itself can't nest transactions. Running transactions in parallel instead of nested changes the outcome of nested operations that are partially rolled back, so this is a can of worms we better not open.
  • Session options are no longer hard coded. Users can specify them when calling DB::beginTransaction or DB::transaction if they need to.
  • The DB::transaction method will respect the $attempts argument and not invoke the callback more often, but may also abort before invoking the callback the specified number of times. This is due to the comparable logic in the MongoDB driver aborting after two minutes. I believe this is preferable to retrying for longer, especially in a web context (where two minutes is already quite long).

Note that I have not touched the docker-compose logic. Transaction tests are skipped unless run against a deployment that supports them, and I don't think it's necessary to set up a local cluster for quick tests. People who need to debug transaction tests locally should set up their own deployment and change the connection URL via the MONGODB_URI environment variable when running tests.

This PR was made possible by the contributions of @klinson and @levon80999 who've worked on this in the past. Commits retain co-authorship to reflect this.

alcaeus and others added 3 commits November 14, 2022 12:52
Co-authored-by: klinson <klinson@163.com>
Co-authored-by: levon80999 <levonb@ucraft.com>
Co-authored-by: levon80999 <levonb@ucraft.com>
The faster connection and server selection timeouts ensure we don't spend too much time waiting for the inevitable as we're expecting fast connections on CI systems

Co-authored-by: levon80999 <levonb@ucraft.com>
.github/workflows/build-ci.yml Show resolved Hide resolved
.github/workflows/build-ci.yml Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
tests/TransactionTest.php Outdated Show resolved Hide resolved
tests/config/database.php Outdated Show resolved Hide resolved
tests/config/database.php Outdated Show resolved Hide resolved
tests/TransactionTest.php Outdated Show resolved Hide resolved
@@ -59,6 +55,13 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Create MongoDB Replica Set
run: |
docker run --name mongodb -p 27017:27017 -e MONGO_INITDB_DATABASE=unittest --detach mongo:${{ matrix.mongodb }} mongod --replSet rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to use --setParameter transactionLifetimeLimitSeconds=5 (or a lower value) to minimize deadlock time in CI.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- name: Create MongoDB Replica Set
run: |
docker run --name mongodb -p 27017:27017 -e MONGO_INITDB_DATABASE=unittest --detach mongo:${{ matrix.mongodb }} mongod --replSet rs
until docker exec --tty mongodb mongo 127.0.0.1:27017 --eval "db.runCommand({ ping: 1 })"; do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looking at the CI output. I'm not certain, but if this line is the only thing responsible for printing the exact MongoDB server version then perhaps we should use buildInfo here instead of ping. serverStatus was definitely TMI but it'd be helpful to have some details about the server (vs. just trusting the shell verison).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - I added an additional build step for this to avoid people having to look through the entire setup output for this.

@Smolevich Smolevich self-requested a review November 20, 2022 09:01
@alcaeus alcaeus force-pushed the feature/transactions branch 2 times, most recently from c88dcab to 5b07c78 Compare November 22, 2022 08:31
This partially reverts commit 203160e. The simplified call unfortunately breaks tests.
@codecov-commenter
Copy link

codecov-commenter commented Nov 24, 2022

Codecov Report

Base: 87.83% // Head: 88.21% // Increases project coverage by +0.38% 🎉

Coverage data is based on head (df3a53a) compared to base (0606fc0).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2465      +/-   ##
============================================
+ Coverage     87.83%   88.21%   +0.38%     
- Complexity      677      696      +19     
============================================
  Files            31       32       +1     
  Lines          1553     1595      +42     
============================================
+ Hits           1364     1407      +43     
+ Misses          189      188       -1     
Impacted Files Coverage Δ
src/Connection.php 87.32% <ø> (ø)
src/Concerns/ManagesTransactions.php 100.00% <100.00%> (ø)
src/Query/Builder.php 91.15% <100.00%> (+0.49%) ⬆️
src/Schema/Builder.php 61.90% <0.00%> (-1.74%) ⬇️
src/Relations/BelongsToMany.php 85.85% <0.00%> (-0.88%) ⬇️
src/Eloquent/Builder.php 85.07% <0.00%> (-0.22%) ⬇️
src/Helpers/QueriesRelationships.php 91.07% <0.00%> (-0.16%) ⬇️
src/Queue/MongoQueue.php 100.00% <0.00%> (ø)
src/Relations/EmbedsOneOrMany.php 98.13% <0.00%> (ø)
... and 1 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@alcaeus
Copy link
Member Author

alcaeus commented Nov 24, 2022

@Smolevich thanks for your patience! This PR is now ready for review. I'll make a couple of minor changes to the CI setup to ensure that any aborted or stale transaction doesn't block the CI build for an unnecessary period of time (30 seconds by default), but the functionality is in place and ready to go!

This ensures that hung transactions don't block any subsequent operations for an unnecessary period of time.
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
Copy link
Contributor

@divine divine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@divine divine removed the Needs work label Nov 27, 2022
@divine divine mentioned this pull request Nov 27, 2022
@divine divine merged commit e5e9193 into mongodb:master Nov 27, 2022
@axgalache

This comment was marked as off-topic.

@alcaeus alcaeus deleted the feature/transactions branch November 29, 2022 13:13
@mongodb mongodb locked as resolved and limited conversation to collaborators Nov 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants