|
1 |
| -use crate::config::MonolithSelectionStrategy; |
2 | 1 | use crate::monolith::BalancerMonolith;
|
3 | 2 | use enum_dispatch::enum_dispatch;
|
4 | 3 | use rand::seq::IteratorRandom;
|
5 | 4 | use serde::Deserialize;
|
6 | 5 |
|
7 |
| -#[derive(Debug, Default, Deserialize, Copy, Clone)] |
8 |
| -pub struct MinRoomsSelector; |
9 | 6 | #[enum_dispatch(MonolithSelectionStrategy)]
|
10 | 7 | pub trait MonolithSelection: std::fmt::Debug {
|
11 | 8 | fn select_monolith<'a>(
|
12 | 9 | &'a self,
|
13 |
| - monolith: Vec<&'a BalancerMonolith>, |
| 10 | + monoliths: Vec<&'a BalancerMonolith>, |
14 | 11 | ) -> anyhow::Result<&BalancerMonolith>;
|
15 | 12 |
|
16 | 13 | fn random_monolith<'a>(
|
17 | 14 | &'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 | + } |
20 | 35 | }
|
21 | 36 |
|
| 37 | +#[derive(Debug, Default, Deserialize, Copy, Clone)] |
| 38 | +pub struct MinRoomsSelector; |
| 39 | + |
22 | 40 | impl MonolithSelection for MinRoomsSelector {
|
23 | 41 | fn select_monolith<'a>(
|
24 | 42 | &'a self,
|
25 |
| - monolith: Vec<&'a BalancerMonolith>, |
| 43 | + monoliths: Vec<&'a BalancerMonolith>, |
26 | 44 | ) -> anyhow::Result<&BalancerMonolith> {
|
27 | 45 | fn cmp(x: &BalancerMonolith, y: &BalancerMonolith) -> std::cmp::Ordering {
|
28 | 46 | x.rooms().len().cmp(&y.rooms().len())
|
29 | 47 | }
|
30 | 48 |
|
31 |
| - let selected = monolith.iter().min_by(|x, y| cmp(x, y)); |
| 49 | + let selected = monoliths.iter().min_by(|x, y| cmp(x, y)); |
32 | 50 | match selected {
|
33 | 51 | Some(s) => Ok(s),
|
34 | 52 | None => anyhow::bail!("no monoliths available"),
|
35 | 53 | }
|
36 | 54 | }
|
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 |
| - } |
48 | 55 | }
|
49 | 56 |
|
50 | 57 | #[cfg(test)]
|
|
0 commit comments