Skip to content

Commit 3ffa78a

Browse files
authored
Allow many Meilisearch write addrs (#5102)
* Write to many Meilisearch write addrs * Keep client results ordered * Attach Read Meilisearch client to actix data * Load balanced meilisearch Compose profile * Nginx config (round_robin) * Fix nginx * Meilisearch + nginx in same net * Fix env vars example * Fix env example again * Fix env again * Use try_collect with FuturesOrdered * maybe fix remove_documents * Clippy
1 parent 7dba9cb commit 3ffa78a

File tree

9 files changed

+327
-99
lines changed

9 files changed

+327
-99
lines changed

apps/labrinth/.env.docker-compose

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ DATABASE_URL=postgresql://labrinth:labrinth@labrinth-postgres/labrinth
1212
DATABASE_MIN_CONNECTIONS=0
1313
DATABASE_MAX_CONNECTIONS=16
1414

15-
MEILISEARCH_ADDR=http://labrinth-meilisearch:7700
15+
MEILISEARCH_READ_ADDR=http://localhost:7700
16+
MEILISEARCH_WRITE_ADDRS=http://localhost:7700
1617
MEILISEARCH_KEY=modrinth
1718

1819
REDIS_URL=redis://labrinth-redis

apps/labrinth/.env.local

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ DATABASE_URL=postgresql://labrinth:labrinth@localhost/labrinth
1313
DATABASE_MIN_CONNECTIONS=0
1414
DATABASE_MAX_CONNECTIONS=16
1515

16-
MEILISEARCH_ADDR=http://localhost:7700
16+
MEILISEARCH_READ_ADDR=http://localhost:7700
17+
MEILISEARCH_WRITE_ADDRS=http://localhost:7700
18+
19+
# # For a sharded Meilisearch setup (sharded-meilisearch docker compose profile)
20+
# MEILISEARCH_READ_ADDR=http://localhost:7710
21+
# MEILISEARCH_WRITE_ADDRS=http://localhost:7700,http://localhost:7701
22+
1723
MEILISEARCH_KEY=modrinth
1824

1925
REDIS_URL=redis://localhost

apps/labrinth/nginx/meili-lb.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
upstream meilisearch_upstream {
2+
server meilisearch0:7700;
3+
server meilisearch1:7700;
4+
}
5+
6+
server {
7+
listen 80;
8+
9+
location / {
10+
proxy_pass http://meilisearch_upstream;
11+
proxy_set_header Host $host;
12+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
13+
}
14+
}

apps/labrinth/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::background_task::update_versions;
1919
use crate::database::ReadOnlyPgPool;
2020
use crate::queue::billing::{index_billing, index_subscriptions};
2121
use crate::queue::moderation::AutomatedModerationQueue;
22+
use crate::search::MeilisearchReadClient;
2223
use crate::util::anrok;
2324
use crate::util::archon::ArchonClient;
2425
use crate::util::env::{parse_strings_from_var, parse_var};
@@ -68,6 +69,7 @@ pub struct LabrinthConfig {
6869
pub email_queue: web::Data<EmailQueue>,
6970
pub archon_client: web::Data<ArchonClient>,
7071
pub gotenberg_client: GotenbergClient,
72+
pub search_read_client: web::Data<MeilisearchReadClient>,
7173
}
7274

7375
#[allow(clippy::too_many_arguments)]
@@ -274,6 +276,11 @@ pub fn app_setup(
274276
file_host,
275277
scheduler: Arc::new(scheduler),
276278
ip_salt,
279+
search_read_client: web::Data::new(
280+
search_config.make_loadbalanced_read_client().expect(
281+
"Failed to make Meilisearch client for read operations",
282+
),
283+
),
277284
search_config,
278285
session_queue,
279286
payouts_queue: web::Data::new(PayoutsQueue::new()),
@@ -325,6 +332,7 @@ pub fn app_config(
325332
.app_data(labrinth_config.archon_client.clone())
326333
.app_data(web::Data::new(labrinth_config.stripe_client.clone()))
327334
.app_data(web::Data::new(labrinth_config.anrok_client.clone()))
335+
.app_data(labrinth_config.search_read_client.clone())
328336
.app_data(labrinth_config.rate_limiter.clone())
329337
.configure({
330338
#[cfg(target_os = "linux")]
@@ -373,7 +381,8 @@ pub fn check_env_vars() -> bool {
373381
failed |= check_var::<String>("LABRINTH_EXTERNAL_NOTIFICATION_KEY");
374382
failed |= check_var::<String>("RATE_LIMIT_IGNORE_KEY");
375383
failed |= check_var::<String>("DATABASE_URL");
376-
failed |= check_var::<String>("MEILISEARCH_ADDR");
384+
failed |= check_var::<String>("MEILISEARCH_READ_ADDR");
385+
failed |= check_var::<String>("MEILISEARCH_WRITE_ADDRS");
377386
failed |= check_var::<String>("MEILISEARCH_KEY");
378387
failed |= check_var::<String>("REDIS_URL");
379388
failed |= check_var::<String>("BIND_ADDR");

apps/labrinth/src/routes/v2/projects.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use crate::queue::moderation::AutomatedModerationQueue;
1313
use crate::queue::session::AuthQueue;
1414
use crate::routes::v3::projects::ProjectIds;
1515
use crate::routes::{ApiError, v2_reroute, v3};
16-
use crate::search::{SearchConfig, SearchError, search_for_project};
16+
use crate::search::{
17+
MeilisearchReadClient, SearchConfig, SearchError, search_for_project,
18+
};
1719
use actix_web::{HttpRequest, HttpResponse, delete, get, patch, post, web};
1820
use serde::{Deserialize, Serialize};
1921
use sqlx::PgPool;
@@ -54,6 +56,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
5456
pub async fn project_search(
5557
web::Query(info): web::Query<SearchRequest>,
5658
config: web::Data<SearchConfig>,
59+
read_client: web::Data<MeilisearchReadClient>,
5760
) -> Result<HttpResponse, SearchError> {
5861
// Search now uses loader_fields instead of explicit 'client_side' and 'server_side' fields
5962
// While the backend for this has changed, it doesnt affect much
@@ -99,7 +102,7 @@ pub async fn project_search(
99102
..info
100103
};
101104

102-
let results = search_for_project(&info, &config).await?;
105+
let results = search_for_project(&info, &config, &read_client).await?;
103106

104107
let results = LegacySearchResults::from(results);
105108

apps/labrinth/src/routes/v3/projects.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use crate::queue::moderation::AutomatedModerationQueue;
2727
use crate::queue::session::AuthQueue;
2828
use crate::routes::ApiError;
2929
use crate::search::indexing::remove_documents;
30-
use crate::search::{SearchConfig, SearchError, search_for_project};
30+
use crate::search::{
31+
MeilisearchReadClient, SearchConfig, SearchError, search_for_project,
32+
};
3133
use crate::util::img;
3234
use crate::util::img::{delete_old_images, upload_image_optimized};
3335
use crate::util::routes::read_limited_from_payload;
@@ -1037,8 +1039,9 @@ pub async fn edit_project_categories(
10371039
pub async fn project_search(
10381040
web::Query(info): web::Query<SearchRequest>,
10391041
config: web::Data<SearchConfig>,
1042+
read_client: web::Data<MeilisearchReadClient>,
10401043
) -> Result<HttpResponse, SearchError> {
1041-
let results = search_for_project(&info, &config).await?;
1044+
let results = search_for_project(&info, &config, &read_client).await?;
10421045

10431046
// TODO: add this back
10441047
// let results = ReturnSearchResults {

0 commit comments

Comments
 (0)