-
Notifications
You must be signed in to change notification settings - Fork 649
Reuse existing connection when executing each background job #5837
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
Conversation
I'm not sure I fully understand the implications of this PR yet. Do I read this correctly that all the background jobs now share a single database connection? Doesn't that prevent us from running jobs in parallel? 🤔 |
It will still be possible to run jobs in parallel. The previous design used 2 connections per task that it completed. One connection selects and locks a job row, and it then sits idle while the job (if needed) obtains its own connection to do its work. That is what was causing an issue with our test framework. We were using a reentrant mutex so that each of these connections were actually the same The new design is that each connection locks a job and then enters an inner transaction so that the job can reuse the database connection. Worst case the job only needs access to the deserialized JSON and this connection still sits idle while the task is executed. The risk, addressed in the 3rd commit, is that we need to take care not to return a connection to the pool while it is still within a transaction. That could happen if the background job panics, so we manually do enough rollbacks to get the connection into the expected state. (Before, if the job panicked, we still had the untouched original connection holding the row locked so that we can update the failure count. And if the job had its own connection, then during a panic I assume the |
To be clear here: Diesel 1.4 will happily return a connection from a panicked thread or with an open transaction to the connection pool. Diesel 2.0 introduced several checks to prevent this behavior. |
☔ The latest upstream changes (presumably #5859) made this pull request unmergeable. Please resolve the merge conflicts. |
2e3637d
to
e5ee424
Compare
@jtgeibel I've rebased the PR to fix the merge conflicts. any thoughts on the "infinite loop" comment above? |
☔ The latest upstream changes (presumably #5558) made this pull request unmergeable. Please resolve the merge conflicts. |
a3ba69b
to
ac700fa
Compare
As discussed in today's meeting I looked into |
This commit is the initial backport from my `diesel2` branch.
It should be possible to handle this much better in diesel 2.0.
ac700fa
to
0888c89
Compare
removed the need for the |
Looks good, merging. One thing your PRs got me thinking about, is that due to the implicit update of the token's I checked and there is only one place where we pass a read-only connection to a default auth check. That occurs when passing a |
Indeed, that's a good point! 👍 |
Revert "Merge pull request #5837 from rust-lang/share-background-worker-connection"
…er-changes" This reverts commit 838ac1d, reversing changes made to a732c1f. This reintroduces changes that were originally landed in rust-lang#5837 and reverted in rust-lang#5909.
This is a backport of some work done in #4892. Once this lands it should be easy to finally land the port to diesel 2.0!
The 2nd commit includes 2 quick fixes to avoid obtaining 2 connections at the same time (which is now necessary to pass tests). We could do some refactoring to reuse an existing connection instead of dropping and reobtaining one, but these endpoints aren't performance critical so I'm not too worried about it.