Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

balancer: add room arg to monolith selection #1649

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/ott-balancer/src/balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ impl BalancerContext {
self.monoliths.values().collect()
}

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

pub fn random_monolith(&self) -> anyhow::Result<&BalancerMonolith> {
Expand Down Expand Up @@ -453,7 +453,7 @@ pub async fn join_client(
}
None => {
// the room is not loaded, randomly select a monolith
let selected = ctx_write.select_monolith()?;
let selected = ctx_write.select_monolith(&new_client.room)?;
debug!(
"room is not loaded, selected monolith: {:?} (region: {:?})",
selected.id(),
Expand Down
6 changes: 5 additions & 1 deletion crates/ott-balancer/src/selection.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::monolith::BalancerMonolith;
use enum_dispatch::enum_dispatch;
use ott_balancer_protocol::RoomName;
use rand::seq::IteratorRandom;
use serde::Deserialize;

#[enum_dispatch(MonolithSelectionStrategy)]
pub trait MonolithSelection: std::fmt::Debug {
fn select_monolith<'a>(
&'a self,
room: &RoomName,
monoliths: Vec<&'a BalancerMonolith>,
) -> anyhow::Result<&BalancerMonolith>;

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

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

let room: RoomName = "foo".into();
let selected = MinRoomsSelector
.select_monolith(monoliths)
.select_monolith(&room, monoliths)
.expect("failed to select monolith");

assert_eq!(selected.id(), monolith_two.id())
Expand Down
2 changes: 1 addition & 1 deletion crates/ott-balancer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl Service<Request<IncomingBody>> for BalancerService {
);
ctx_read.monoliths.get(&locator.monolith_id())
} else {
ctx_read.select_monolith().ok()
ctx_read.select_monolith(&room_name).ok()
};
if let Some(monolith) = monolith {
info!("proxying request to monolith {}", monolith.id());
Expand Down
Loading