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

Use diesel-async database pools #9161

Merged
merged 8 commits into from
Jul 29, 2024
Merged

Conversation

Turbo87
Copy link
Member

@Turbo87 Turbo87 commented Jul 24, 2024

This PR moves our codebase from deadpool database pools to deadpool database pools... essentially... well... it's complicated.

The codebase currently uses the database pools from deadpool_diesel. These database pools are async, but they still return a sync database connection that can be used with regular diesel queries. With this PR we are migrating to using database pools from diesel-async in their deadpool variant. These are returning an async connection, which can be wrapped in an AsyncConnectionWrapper to make them work with sync diesel queries (in combination with spawn_blocking()).

In other words: this is just an intermediate step that allows us to use async diesel queries (enabled by diesel-async), but a lot of the code is still using sync queries with the wrapper. I've started to port over some of the simpler endpoints to use async queries, but that migration is far from complete. It should however serve as a starting point to demonstrate the end goal of removing all the spawn_blocking() calls.

Note that I had to introduce a Conn trait to abstract over the PgConnection and AsyncConnectionWrapper structs. Both implement LoadConnection<Backend = Pg>, but since trait aliases are not stabilized yet and I didn't want to repeat that signature everywhere I decided to extract the Conn trait. After that I essentially searched for &mut PgConnection and replaced it with &mut impl Conn, which is equivalent, but also allows us to pass in AsyncConnectionWrapper instead.

I would recommend reviewing this commit-by-commit... 😅

@Turbo87 Turbo87 added C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear A-backend ⚙️ labels Jul 24, 2024
@Turbo87 Turbo87 requested a review from a team July 24, 2024 12:22
@Turbo87 Turbo87 force-pushed the diesel-async branch 2 times, most recently from 619c86a to e8bce5e Compare July 24, 2024 13:24
Copy link

codecov bot commented Jul 24, 2024

Codecov Report

Attention: Patch coverage is 93.31797% with 29 lines in your changes missing coverage. Please review.

Project coverage is 89.17%. Comparing base (5b6cca9) to head (4795d51).

Files Patch % Lines
...worker/jobs/downloads/clean_processed_log_files.rs 16.66% 5 Missing ⚠️
src/worker/jobs/downloads/update_metadata.rs 37.50% 5 Missing ⚠️
src/controllers/user/session.rs 20.00% 4 Missing ⚠️
src/controllers/user/me.rs 83.33% 3 Missing ⚠️
src/worker/jobs/daily_db_maintenance.rs 0.00% 3 Missing ⚠️
src/worker/jobs/expiry_notification.rs 50.00% 3 Missing ⚠️
src/bin/background-worker.rs 0.00% 2 Missing ⚠️
src/controllers/team.rs 0.00% 2 Missing ⚠️
src/controllers/krate/downloads.rs 97.61% 1 Missing ⚠️
src/controllers/user/other.rs 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9161      +/-   ##
==========================================
- Coverage   89.17%   89.17%   -0.01%     
==========================================
  Files         282      282              
  Lines       28534    28583      +49     
==========================================
+ Hits        25445    25488      +43     
- Misses       3089     3095       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@eth3lbert eth3lbert left a comment

Choose a reason for hiding this comment

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

I've reviewed the commits one by one with hide whitespace turned on, and it looks good to me! Plus, the CI passed, nice works 👍

@eth3lbert
Copy link
Contributor

In other words: this is just an intermediate step that allows us to use async diesel queries (enabled by diesel-async), but a lot of the code is still using sync queries with the wrapper. I've started to port over some of the simpler endpoints to use async queries, but that migration is far from complete. It should however serve as a starting point to demonstrate the end goal of removing all the spawn_blocking() calls.

I also see that there's still a lot of work to do for the migration, especially for all the models.

Let me know if you need more help.

@Turbo87
Copy link
Member Author

Turbo87 commented Jul 26, 2024

yeah, it's far from finished but seemed like a good first step in hopefully the right direction. I haven't found a good way yet to incrementally migrate the rest of the code. Feel free to take a look while I'm on vacation :)

@bors
Copy link
Contributor

bors commented Jul 29, 2024

☔ The latest upstream changes (presumably 0d4928b) made this pull request unmergeable. Please resolve the merge conflicts.

Turbo87 added 8 commits July 29, 2024 11:12
This allows us to use different implementations of `LoadConnection` (e.g. from `diesel-async`)
This allows us to use different implementations of `LoadConnection` (e.g. from `diesel-async`)
Using the `current_thread` runtime can cause deadlocks under certain conditions. This is especially visible when migrating the library to `diesel-async`.
@Turbo87 Turbo87 merged commit e0bb004 into rust-lang:main Jul 29, 2024
10 checks passed
@Turbo87 Turbo87 deleted the diesel-async branch July 29, 2024 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-backend ⚙️ C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants