Skip to content

Commit f65a985

Browse files
authored
balancer: reorganize monolith selection (#1648)
1 parent 808b884 commit f65a985

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

crates/ott-balancer/src/balancer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use tracing::{debug, error, info, instrument, trace, warn};
1717

1818
use crate::balancer::collector::ClientState;
1919
use crate::client::ClientLink;
20-
use crate::config::{BalancerConfig, MonolithSelectionStrategy};
20+
use crate::config::BalancerConfig;
2121
use crate::connection::BALANCER_ID;
2222
use crate::monolith::Room;
2323
use crate::room::RoomLocator;
24-
use crate::selection::MonolithSelection;
24+
use crate::selection::{MonolithSelection, MonolithSelectionStrategy};
2525
use crate::{
2626
client::{BalancerClient, NewClient},
2727
messages::*,

crates/ott-balancer/src/config.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
use std::{borrow::BorrowMut, path::PathBuf, sync::Once};
22

33
use clap::{Parser, ValueEnum};
4-
use enum_dispatch::enum_dispatch;
54
use figment::providers::Format;
65
use serde::Deserialize;
76

87
use ott_common::discovery::DiscoveryConfig;
98

10-
use crate::selection::MinRoomsSelector;
9+
use crate::selection::MonolithSelectionStrategy;
1110

1211
static mut CONFIG: Option<BalancerConfig> = None;
1312

1413
static CONFIG_INIT: Once = Once::new();
1514

16-
#[derive(Debug, Deserialize, Copy, Clone)]
17-
#[enum_dispatch]
18-
pub enum MonolithSelectionStrategy {
19-
MinRooms(MinRoomsSelector),
20-
}
21-
22-
impl Default for MonolithSelectionStrategy {
23-
fn default() -> Self {
24-
MonolithSelectionStrategy::MinRooms(MinRoomsSelector)
25-
}
26-
}
2715
#[derive(Debug, Deserialize)]
2816
#[serde(default)]
2917
pub struct BalancerConfig {

crates/ott-balancer/src/selection.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,57 @@
1-
use crate::config::MonolithSelectionStrategy;
21
use crate::monolith::BalancerMonolith;
32
use enum_dispatch::enum_dispatch;
43
use rand::seq::IteratorRandom;
54
use serde::Deserialize;
65

7-
#[derive(Debug, Default, Deserialize, Copy, Clone)]
8-
pub struct MinRoomsSelector;
96
#[enum_dispatch(MonolithSelectionStrategy)]
107
pub trait MonolithSelection: std::fmt::Debug {
118
fn select_monolith<'a>(
129
&'a self,
13-
monolith: Vec<&'a BalancerMonolith>,
10+
monoliths: Vec<&'a BalancerMonolith>,
1411
) -> anyhow::Result<&BalancerMonolith>;
1512

1613
fn random_monolith<'a>(
1714
&'a self,
18-
monolith: Vec<&'a BalancerMonolith>,
19-
) -> anyhow::Result<&BalancerMonolith>;
15+
monoliths: Vec<&'a BalancerMonolith>,
16+
) -> anyhow::Result<&BalancerMonolith> {
17+
let selected = monoliths
18+
.iter()
19+
.choose(&mut rand::thread_rng())
20+
.ok_or_else(|| anyhow::anyhow!("no monoliths available"))?;
21+
Ok(selected)
22+
}
23+
}
24+
25+
#[derive(Debug, Deserialize, Copy, Clone)]
26+
#[enum_dispatch]
27+
pub enum MonolithSelectionStrategy {
28+
MinRooms(MinRoomsSelector),
29+
}
30+
31+
impl Default for MonolithSelectionStrategy {
32+
fn default() -> Self {
33+
MonolithSelectionStrategy::MinRooms(MinRoomsSelector)
34+
}
2035
}
2136

37+
#[derive(Debug, Default, Deserialize, Copy, Clone)]
38+
pub struct MinRoomsSelector;
39+
2240
impl MonolithSelection for MinRoomsSelector {
2341
fn select_monolith<'a>(
2442
&'a self,
25-
monolith: Vec<&'a BalancerMonolith>,
43+
monoliths: Vec<&'a BalancerMonolith>,
2644
) -> anyhow::Result<&BalancerMonolith> {
2745
fn cmp(x: &BalancerMonolith, y: &BalancerMonolith) -> std::cmp::Ordering {
2846
x.rooms().len().cmp(&y.rooms().len())
2947
}
3048

31-
let selected = monolith.iter().min_by(|x, y| cmp(x, y));
49+
let selected = monoliths.iter().min_by(|x, y| cmp(x, y));
3250
match selected {
3351
Some(s) => Ok(s),
3452
None => anyhow::bail!("no monoliths available"),
3553
}
3654
}
37-
38-
fn random_monolith<'a>(
39-
&'a self,
40-
monolith: Vec<&'a BalancerMonolith>,
41-
) -> anyhow::Result<&BalancerMonolith> {
42-
let selected = monolith
43-
.iter()
44-
.choose(&mut rand::thread_rng())
45-
.ok_or_else(|| anyhow::anyhow!("no monoliths available"))?;
46-
Ok(selected)
47-
}
4855
}
4956

5057
#[cfg(test)]

0 commit comments

Comments
 (0)