[WIP] using db-pool library to create a pool of databases#5846
[WIP] using db-pool library to create a pool of databases#5846momentary-lapse wants to merge 94 commits intoLemmyNet:mainfrom
Conversation
| .await; | ||
|
|
||
| // TODO make compatible with ActualDbPool | ||
| db_pool.pull_immutable().await |
There was a problem hiding this comment.
I created this WIP PR to share the progress and the issue I'm stuck with currently. The crate I use operates with its own structure wrapping connection pools: code
And we have our own ActualDbPool. They are kinda same, but it's not obvious for me how to correctly convert one to another.
I had an idea to make ActualDbPool a enum with two possible values: RegularPool and ReusablePool, but stuck on trying to adapt stuff like LemmyContext, which also requires pool struct to be clone-able (and ReusablePool is not). And it seems a lot of changes to the main codebase for purely test changes.
Do you folks have any ideas how to manage that? Or should I stick to the initial plan without using this library?
There was a problem hiding this comment.
Our ActualDbPool is just a type alias for deadpool Pool<AsyncPgConnection>.
Their crate should be able to work with deadpool pools, but I'm not familiar with how to plug that into their crate... you'll have to ask them.
There was a problem hiding this comment.
Yeah, I see. I returned to this issue today after a week of a break. I'm in contact with the db-pool author and they're helping to understand a lot of moments and really willing to collaborate, so i think we'll make this work.
I'd like to clarify one moment: do we want build_db_pool_for_tests to return still ActualDbPool? db-pool has its own wrapper ReusableConnectionPool which works like a deadpool Pool, but a bit different and needs adaptation. And it might be easier to adapt tests for working with ReusableConnectionPool than converting ReusableConnectionPool to ActualDbPool
There was a problem hiding this comment.
The return type of build_db_pool_for_tests may be changed. Also, a DbPool variant may be added if needed.
| #[derive(Clone)] | ||
| pub struct LemmyContext { | ||
| pool: ActualDbPool, | ||
| pool: ContextPool, |
There was a problem hiding this comment.
This is the moment which currently blocks me, and i think it's better to consult with you again. LemmyContext structure must be cloneable, therefore all the fields, therefore the pool. But unfortunately, reusable pool from db-pool crate is not, and i don't have access to its fields to implement the trait here.
But before asking db-pool developer, i'd like to be sure we really need this pool cloning stuff, especially for the tests. Cloning the pool seems a bit strange to me, but i may miss something. I'm looking at the code now, but maybe you folks already have some insights on this
There was a problem hiding this comment.
Wrap it in Arc for now.
|
Update: I'm working on the topic; cannot devote much time for it, but it slowly going forward, and i keep the code in the branch up-to-date. I connected |
|
Okay, for now i simply ignored cleanup errors in db-pool, and tests are passing now. Tried to disable There's a small speed improvement comparing to main build (~4 min). But i suspect that's because one heavy test is still ignored ( |
How many tests are currently running at the same time? Not sure where that is defined, but try to change the number higher or lower and see if it helps.
It might be possible to pass the port number as parameter to make them parallel. But there are only a few tests in that crate so it can be changed later in another PR. |
I hardcoded the pool size for tests to 30 for now. But both pool sizes (restricted and privileged) don't seem to actually affect the test speed at all.
Honestly, I tried to set up each test to its own port (changed it here and here) and run them together, but they started failing and hanging. I spent some time, but then switched to other stuff. |
|
Tried it locally and the tests are passing now, good job! Still needs And you need to use a proper release version of the db-pool crate in Edit: Locally tests complete in 104s with this change, compared to 203s for main branch. So only half the time! |
|
|
||
| #[tokio::test] | ||
| #[serial] | ||
| #[tokio_shared_rt::test(shared = true, flavor = "multi_thread")] |
There was a problem hiding this comment.
| #[tokio_shared_rt::test(shared = true, flavor = "multi_thread")] | |
| #[tokio_shared_rt::test(shared, flavor = "multi_thread")] |
Can be shortened like this (with search and replace on all files).
https://docs.rs/tokio-shared-rt/latest/tokio_shared_rt/attr.test.html
| }, | ||
| ) | ||
| .await | ||
| .expect("diesel postgres backend"); |
There was a problem hiding this comment.
Dont use so many expect, instead simply use ? and return LemmyResult from this closure.
|
|
||
| let backend = DieselAsyncPostgresBackend::new( | ||
| config, | ||
| |manager| Pool::builder(manager).max_size(30), //TODO use some env var |
There was a problem hiding this comment.
Leftover todo, anyway this is probably fine to keep hardcoded.
|
|
||
| #[test] | ||
| #[serial] | ||
| #[ignore] |
There was a problem hiding this comment.
Need to enable this test again (it doesnt have to run in parallel).
| @@ -0,0 +1 @@ | |||
|
|
|||
There was a problem hiding this comment.
Delete this folder (it was removed from main branch but came back somehow).
| let pool_arc = data.pool(); | ||
| let pool_ref = &***pool_arc; | ||
| let pool = &mut pool_ref.into(); |
There was a problem hiding this comment.
| let pool_arc = data.pool(); | |
| let pool_ref = &***pool_arc; | |
| let pool = &mut pool_ref.into(); | |
| let pool = &build_db_pool_for_tests().await; | |
| let pool = &mut pool.into(); |
Simplify it like the other tests. In this file its all written in this overly complicated way.
|
Thanks for the review, I'll try to address remarks soon-ish and contact with db-pool owner to release my forked changes as a new version. |
|
To fix the task which is currently failing in CI (lemmy_api_common_works_with_wasm) you need to change |
Addresses: #4979