Skip to content

Commit c148ba0

Browse files
authored
Merge branch 'main' into cal/tanstack-project-page-optimisations
2 parents f7a4770 + 1cf782c commit c148ba0

10 files changed

+63
-104
lines changed

apps/labrinth/.sqlx/query-1adbd24d815107e13bc1440c7a8f4eeff66ab4165a9f4980032e114db4dc1286.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

apps/labrinth/.sqlx/query-b30d0365bd116fceee5de03fb9e3087a587633783894a5041889b856d47a4ed5.json renamed to apps/labrinth/.sqlx/query-702a2826d5857dc51b1a7a79c9043ae8987441bb5e89c9ea62d347e47899e3c2.json

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-b92b5bb7d179c4fcdbc45600ccfd2402f52fea71e27b08e7926fcc2a9e62c0f3.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

apps/labrinth/.sqlx/query-cd5ccd618fb3cc41646a6de86f9afedb074492b4ec7f2457c14113f5fd13aa02.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

apps/labrinth/.sqlx/query-cec4240c7c848988b3dfd13e3f8e5c93783c7641b019fdb698a1ec0be1393606.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

apps/labrinth/src/background_task.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ impl BackgroundTask {
3131
#[allow(clippy::too_many_arguments)]
3232
pub async fn run(
3333
self,
34-
pool: sqlx::Pool<Postgres>,
34+
pool: sqlx::PgPool,
35+
ro_pool: sqlx::PgPool,
3536
redis_pool: RedisPool,
3637
search_config: search::SearchConfig,
3738
clickhouse: clickhouse::Client,
@@ -43,7 +44,9 @@ impl BackgroundTask {
4344
use BackgroundTask::*;
4445
match self {
4546
Migrations => run_migrations().await,
46-
IndexSearch => index_search(pool, redis_pool, search_config).await,
47+
IndexSearch => {
48+
index_search(ro_pool, redis_pool, search_config).await
49+
}
4750
ReleaseScheduled => release_scheduled(pool).await,
4851
UpdateVersions => update_versions(pool, redis_pool).await,
4952
Payouts => payouts(pool, clickhouse, redis_pool).await,
@@ -117,12 +120,12 @@ pub async fn run_migrations() {
117120
}
118121

119122
pub async fn index_search(
120-
pool: sqlx::Pool<Postgres>,
123+
ro_pool: sqlx::PgPool,
121124
redis_pool: RedisPool,
122125
search_config: search::SearchConfig,
123126
) {
124127
info!("Indexing local database");
125-
let result = index_projects(pool, redis_pool, &search_config).await;
128+
let result = index_projects(ro_pool, redis_pool, &search_config).await;
126129
if let Err(e) = result {
127130
warn!("Local project indexing failed: {:?}", e);
128131
}

apps/labrinth/src/database/postgres_database.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ impl From<PgPool> for ReadOnlyPgPool {
1717
}
1818
}
1919

20+
impl ReadOnlyPgPool {
21+
pub fn into_inner(self) -> PgPool {
22+
self.0
23+
}
24+
}
25+
2026
impl Deref for ReadOnlyPgPool {
2127
type Target = PgPool;
2228

apps/labrinth/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ async fn app() -> std::io::Result<()> {
176176
info!("Running task {task:?} and exiting");
177177
task.run(
178178
pool,
179+
ro_pool.into_inner(),
179180
redis_pool,
180181
search_config,
181182
clickhouse,

apps/labrinth/src/search/indexing/local_import.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use sqlx::postgres::PgPool;
2222

2323
pub async fn index_local(
2424
pool: &PgPool,
25-
) -> Result<Vec<UploadSearchProject>, IndexingError> {
25+
cursor: i64,
26+
limit: i64,
27+
) -> Result<(Vec<UploadSearchProject>, i64), IndexingError> {
2628
info!("Indexing local projects!");
2729

2830
// todo: loaders, project type, game versions
@@ -45,13 +47,17 @@ pub async fn index_local(
4547
SELECT m.id id, m.name name, m.summary summary, m.downloads downloads, m.follows follows,
4648
m.icon_url icon_url, m.updated updated, m.approved approved, m.published, m.license license, m.slug slug, m.color
4749
FROM mods m
48-
WHERE m.status = ANY($1)
49-
GROUP BY m.id;
50+
WHERE m.status = ANY($1) AND m.id > $3
51+
GROUP BY m.id
52+
ORDER BY m.id ASC
53+
LIMIT $2;
5054
",
5155
&*crate::models::projects::ProjectStatus::iterator()
5256
.filter(|x| x.is_searchable())
5357
.map(|x| x.to_string())
5458
.collect::<Vec<String>>(),
59+
limit,
60+
cursor,
5561
)
5662
.fetch(pool)
5763
.map_ok(|m| {
@@ -74,6 +80,10 @@ pub async fn index_local(
7480

7581
let project_ids = db_projects.iter().map(|x| x.id.0).collect::<Vec<i64>>();
7682

83+
let Some(largest) = project_ids.iter().max() else {
84+
return Ok((vec![], i64::MAX));
85+
};
86+
7787
struct PartialGallery {
7888
url: String,
7989
featured: bool,
@@ -415,7 +425,7 @@ pub async fn index_local(
415425
}
416426
}
417427

418-
Ok(uploads)
428+
Ok((uploads, *largest))
419429
}
420430

421431
struct PartialVersion {

apps/labrinth/src/search/indexing/mod.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub async fn remove_documents(
8686
}
8787

8888
pub async fn index_projects(
89-
pool: PgPool,
89+
ro_pool: PgPool,
9090
redis: RedisPool,
9191
config: &SearchConfig,
9292
) -> Result<(), IndexingError> {
@@ -111,7 +111,7 @@ pub async fn index_projects(
111111

112112
let all_loader_fields =
113113
crate::database::models::loader_fields::LoaderField::get_fields_all(
114-
&pool, &redis,
114+
&ro_pool, &redis,
115115
)
116116
.await?
117117
.into_iter()
@@ -120,17 +120,35 @@ pub async fn index_projects(
120120

121121
info!("Gathering local projects");
122122

123-
let uploads = index_local(&pool).await?;
123+
let mut cursor = 0;
124+
let mut idx = 0;
125+
let mut total = 0;
124126

125-
info!("Adding projects to index");
127+
loop {
128+
info!("Gathering index data chunk {idx}");
129+
idx += 1;
126130

127-
add_projects_batch_client(
128-
&indices,
129-
uploads,
130-
all_loader_fields.clone(),
131-
config,
132-
)
133-
.await?;
131+
let (uploads, next_cursor) =
132+
index_local(&ro_pool, cursor, 10000).await?;
133+
total += uploads.len();
134+
135+
if uploads.is_empty() {
136+
info!(
137+
"No more projects to index, indexed {total} projects after {idx} chunks"
138+
);
139+
break;
140+
}
141+
142+
cursor = next_cursor;
143+
144+
add_projects_batch_client(
145+
&indices,
146+
uploads,
147+
all_loader_fields.clone(),
148+
config,
149+
)
150+
.await?;
151+
}
134152

135153
info!("Swapping indexes");
136154

@@ -326,7 +344,7 @@ async fn add_to_index(
326344
monitor_task(
327345
client,
328346
task,
329-
Duration::from_secs(60 * 10), // Timeout after 10 minutes
347+
Duration::from_secs(60 * 5), // Timeout after 10 minutes
330348
Some(Duration::from_secs(1)), // Poll once every second
331349
)
332350
.await?;

0 commit comments

Comments
 (0)