Skip to content

Commit 5e86076

Browse files
committed
balancer: add room arg to monolith selection
1 parent 48a38dc commit 5e86076

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

crates/ott-balancer/src/balancer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,9 @@ impl BalancerContext {
386386
self.monoliths.values().collect()
387387
}
388388

389-
pub fn select_monolith(&self) -> anyhow::Result<&BalancerMonolith> {
389+
pub fn select_monolith(&self, room: &RoomName) -> anyhow::Result<&BalancerMonolith> {
390390
let filtered = self.filter_monoliths();
391-
return self.monolith_selection.select_monolith(filtered);
391+
return self.monolith_selection.select_monolith(room, filtered);
392392
}
393393

394394
pub fn random_monolith(&self) -> anyhow::Result<&BalancerMonolith> {
@@ -453,7 +453,7 @@ pub async fn join_client(
453453
}
454454
None => {
455455
// the room is not loaded, randomly select a monolith
456-
let selected = ctx_write.select_monolith()?;
456+
let selected = ctx_write.select_monolith(&new_client.room)?;
457457
debug!(
458458
"room is not loaded, selected monolith: {:?} (region: {:?})",
459459
selected.id(),

crates/ott-balancer/src/selection.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use crate::monolith::BalancerMonolith;
22
use enum_dispatch::enum_dispatch;
3+
use ott_balancer_protocol::RoomName;
34
use rand::seq::IteratorRandom;
45
use serde::Deserialize;
56

67
#[enum_dispatch(MonolithSelectionStrategy)]
78
pub trait MonolithSelection: std::fmt::Debug {
89
fn select_monolith<'a>(
910
&'a self,
11+
room: &RoomName,
1012
monoliths: Vec<&'a BalancerMonolith>,
1113
) -> anyhow::Result<&BalancerMonolith>;
1214

@@ -40,6 +42,7 @@ pub struct MinRoomsSelector;
4042
impl MonolithSelection for MinRoomsSelector {
4143
fn select_monolith<'a>(
4244
&'a self,
45+
_room: &RoomName,
4346
monoliths: Vec<&'a BalancerMonolith>,
4447
) -> anyhow::Result<&BalancerMonolith> {
4548
fn cmp(x: &BalancerMonolith, y: &BalancerMonolith) -> std::cmp::Ordering {
@@ -121,8 +124,9 @@ mod test {
121124

122125
let monoliths: Vec<&BalancerMonolith> = vec![&monolith_one, &monolith_two];
123126

127+
let room: RoomName = "foo".into();
124128
let selected = MinRoomsSelector
125-
.select_monolith(monoliths)
129+
.select_monolith(&room, monoliths)
126130
.expect("failed to select monolith");
127131

128132
assert_eq!(selected.id(), monolith_two.id())

crates/ott-balancer/src/service.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl Service<Request<IncomingBody>> for BalancerService {
242242
);
243243
ctx_read.monoliths.get(&locator.monolith_id())
244244
} else {
245-
ctx_read.select_monolith().ok()
245+
ctx_read.select_monolith(&room_name).ok()
246246
};
247247
if let Some(monolith) = monolith {
248248
info!("proxying request to monolith {}", monolith.id());

0 commit comments

Comments
 (0)