Skip to content

Commit fb1050e

Browse files
GeometricallyJai Agrawal
andauthored
Implement redis clustering (#5189)
Co-authored-by: Jai Agrawal <[email protected]>
1 parent 5c29a8c commit fb1050e

File tree

13 files changed

+200
-124
lines changed

13 files changed

+200
-124
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const_format = "0.2.34"
6060
daedalus = { path = "packages/daedalus" }
6161
dashmap = "6.1.0"
6262
data-url = "0.3.2"
63-
deadpool-redis = "0.22.0"
63+
deadpool-redis = { version ="0.22.0", features = ["cluster-async"] }
6464
derive_more = "2.0.1"
6565
directories = "6.0.0"
6666
dirs = "6.0.0"

apps/labrinth/src/database/models/notification_item.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,10 @@ impl DBNotification {
557557
let mut redis = redis.connect().await?;
558558

559559
redis
560-
.delete_many(user_ids.into_iter().map(|id| {
561-
(USER_NOTIFICATIONS_NAMESPACE, Some(id.0.to_string()))
562-
}))
560+
.delete_many(
561+
USER_NOTIFICATIONS_NAMESPACE,
562+
user_ids.into_iter().map(|id| Some(id.0.to_string())),
563+
)
563564
.await?;
564565

565566
Ok(())

apps/labrinth/src/database/models/organization_item.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,16 @@ impl DBOrganization {
256256
) -> Result<(), super::DatabaseError> {
257257
let mut redis = redis.connect().await?;
258258

259+
if let Some(slug) = slug {
260+
redis
261+
.delete(ORGANIZATIONS_TITLES_NAMESPACE, slug.to_lowercase())
262+
.await?;
263+
}
264+
259265
redis
260-
.delete_many([
261-
(ORGANIZATIONS_NAMESPACE, Some(id.0.to_string())),
262-
(
263-
ORGANIZATIONS_TITLES_NAMESPACE,
264-
slug.map(|x| x.to_lowercase()),
265-
),
266-
])
266+
.delete(ORGANIZATIONS_NAMESPACE, id.0.to_string())
267267
.await?;
268+
268269
Ok(())
269270
}
270271
}

apps/labrinth/src/database/models/pat_item.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,26 @@ impl DBPersonalAccessToken {
209209
}
210210

211211
redis
212-
.delete_many(clear_pats.into_iter().flat_map(
213-
|(id, token, user_id)| {
214-
[
215-
(PATS_NAMESPACE, id.map(|i| i.0.to_string())),
216-
(PATS_TOKENS_NAMESPACE, token),
217-
(
218-
PATS_USERS_NAMESPACE,
219-
user_id.map(|i| i.0.to_string()),
220-
),
221-
]
222-
},
223-
))
212+
.delete_many(
213+
PATS_NAMESPACE,
214+
clear_pats
215+
.iter()
216+
.map(|(x, _, _)| x.map(|i| i.0.to_string())),
217+
)
218+
.await?;
219+
redis
220+
.delete_many(
221+
PATS_TOKENS_NAMESPACE,
222+
clear_pats.iter().map(|(_, token, _)| token.clone()),
223+
)
224+
.await?;
225+
redis
226+
.delete_many(
227+
PATS_USERS_NAMESPACE,
228+
clear_pats
229+
.iter()
230+
.map(|(_, _, x)| x.map(|i| i.0.to_string())),
231+
)
224232
.await?;
225233

226234
Ok(())

apps/labrinth/src/database/models/project_item.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -953,20 +953,20 @@ impl DBProject {
953953
) -> Result<(), DatabaseError> {
954954
let mut redis = redis.connect().await?;
955955

956-
redis
957-
.delete_many([
958-
(PROJECTS_NAMESPACE, Some(id.0.to_string())),
959-
(PROJECTS_SLUGS_NAMESPACE, slug.map(|x| x.to_lowercase())),
960-
(
961-
PROJECTS_DEPENDENCIES_NAMESPACE,
962-
if clear_dependencies.unwrap_or(false) {
963-
Some(id.0.to_string())
964-
} else {
965-
None
966-
},
967-
),
968-
])
969-
.await?;
956+
redis.delete(PROJECTS_NAMESPACE, id.0.to_string()).await?;
957+
958+
if let Some(slug) = slug {
959+
redis
960+
.delete(PROJECTS_SLUGS_NAMESPACE, slug.to_lowercase())
961+
.await?;
962+
}
963+
964+
if clear_dependencies.unwrap_or(false) {
965+
redis
966+
.delete(PROJECTS_DEPENDENCIES_NAMESPACE, id.0.to_string())
967+
.await?;
968+
}
969+
970970
Ok(())
971971
}
972972
}

apps/labrinth/src/database/models/session_item.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,28 @@ impl DBSession {
268268
}
269269

270270
redis
271-
.delete_many(clear_sessions.into_iter().flat_map(
272-
|(id, session, user_id)| {
273-
[
274-
(SESSIONS_NAMESPACE, id.map(|i| i.0.to_string())),
275-
(SESSIONS_IDS_NAMESPACE, session),
276-
(
277-
SESSIONS_USERS_NAMESPACE,
278-
user_id.map(|i| i.0.to_string()),
279-
),
280-
]
281-
},
282-
))
271+
.delete_many(
272+
SESSIONS_NAMESPACE,
273+
clear_sessions
274+
.iter()
275+
.map(|(x, _, _)| x.map(|x| x.0.to_string())),
276+
)
277+
.await?;
278+
redis
279+
.delete_many(
280+
SESSIONS_IDS_NAMESPACE,
281+
clear_sessions.iter().map(|(_, session, _)| session.clone()),
282+
)
283283
.await?;
284+
redis
285+
.delete_many(
286+
SESSIONS_USERS_NAMESPACE,
287+
clear_sessions
288+
.iter()
289+
.map(|(_, _, x)| x.map(|x| x.0.to_string())),
290+
)
291+
.await?;
292+
284293
Ok(())
285294
}
286295

apps/labrinth/src/database/models/user_item.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,16 @@ impl DBUser {
470470
let mut redis = redis.connect().await?;
471471

472472
redis
473-
.delete_many(user_ids.iter().flat_map(|(id, username)| {
474-
[
475-
(USERS_NAMESPACE, Some(id.0.to_string())),
476-
(
477-
USER_USERNAMES_NAMESPACE,
478-
username.clone().map(|i| i.to_lowercase()),
479-
),
480-
]
481-
}))
473+
.delete_many(
474+
USERS_NAMESPACE,
475+
user_ids.iter().map(|(id, _)| Some(id.0.to_string())),
476+
)
477+
.await?;
478+
redis
479+
.delete_many(
480+
USER_USERNAMES_NAMESPACE,
481+
user_ids.iter().map(|(_, username)| username.clone()),
482+
)
482483
.await?;
483484
Ok(())
484485
}
@@ -491,9 +492,8 @@ impl DBUser {
491492

492493
redis
493494
.delete_many(
494-
user_ids.iter().map(|id| {
495-
(USERS_PROJECTS_NAMESPACE, Some(id.0.to_string()))
496-
}),
495+
USERS_PROJECTS_NAMESPACE,
496+
user_ids.iter().map(|id| Some(id.0.to_string())),
497497
)
498498
.await?;
499499

apps/labrinth/src/database/models/version_item.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use itertools::Itertools;
1414
use serde::{Deserialize, Serialize};
1515
use std::cmp::Ordering;
1616
use std::collections::HashMap;
17-
use std::iter;
1817

1918
pub const VERSIONS_NAMESPACE: &str = "versions";
2019
const VERSION_FILES_NAMESPACE: &str = "versions_files";
@@ -914,24 +913,21 @@ impl DBVersion {
914913
) -> Result<(), DatabaseError> {
915914
let mut redis = redis.connect().await?;
916915

916+
redis
917+
.delete(VERSIONS_NAMESPACE, version.inner.id.0.to_string())
918+
.await?;
919+
917920
redis
918921
.delete_many(
919-
iter::once((
920-
VERSIONS_NAMESPACE,
921-
Some(version.inner.id.0.to_string()),
922-
))
923-
.chain(version.files.iter().flat_map(
924-
|file| {
925-
file.hashes.iter().map(|(algo, hash)| {
926-
(
927-
VERSION_FILES_NAMESPACE,
928-
Some(format!("{algo}_{hash}")),
929-
)
930-
})
931-
},
932-
)),
922+
VERSION_FILES_NAMESPACE,
923+
version.files.iter().flat_map(|file| {
924+
file.hashes
925+
.iter()
926+
.map(|(algo, hash)| Some(format!("{algo}_{hash}")))
927+
}),
933928
)
934929
.await?;
930+
935931
Ok(())
936932
}
937933
}

0 commit comments

Comments
 (0)