use osproxy_core::{ClusterId, Epoch, IndexName};
use crate::rules::InjectedField;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Placement {
DedicatedCluster {
cluster: ClusterId,
},
DedicatedIndex {
cluster: ClusterId,
index: IndexName,
},
SharedIndex {
cluster: ClusterId,
index: IndexName,
inject: Vec<InjectedField>,
},
}
impl Placement {
#[must_use]
pub fn cluster(&self) -> &ClusterId {
match self {
Self::DedicatedCluster { cluster }
| Self::DedicatedIndex { cluster, .. }
| Self::SharedIndex { cluster, .. } => cluster,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum MigrationPhase {
#[default]
Settled,
Draining,
Cutover,
}
impl MigrationPhase {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Settled => "settled",
Self::Draining => "draining",
Self::Cutover => "cutover",
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PlacementAt {
pub placement: Placement,
pub epoch: Epoch,
pub phase: MigrationPhase,
pub endpoint: Option<String>,
}
impl PlacementAt {
#[must_use]
pub fn new(placement: Placement, epoch: Epoch) -> Self {
Self {
placement,
epoch,
phase: MigrationPhase::Settled,
endpoint: None,
}
}
#[must_use]
pub fn with_phase(mut self, phase: MigrationPhase) -> Self {
self.phase = phase;
self
}
#[must_use]
pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.endpoint = Some(endpoint.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cluster_is_extracted_for_every_mode() {
let dc = Placement::DedicatedCluster {
cluster: ClusterId::from("c1"),
};
let di = Placement::DedicatedIndex {
cluster: ClusterId::from("c2"),
index: IndexName::from("i"),
};
let si = Placement::SharedIndex {
cluster: ClusterId::from("c3"),
index: IndexName::from("shared"),
inject: Vec::new(),
};
assert_eq!(dc.cluster().as_str(), "c1");
assert_eq!(di.cluster().as_str(), "c2");
assert_eq!(si.cluster().as_str(), "c3");
}
#[test]
fn placement_at_pairs_epoch() {
let at = PlacementAt::new(
Placement::DedicatedCluster {
cluster: ClusterId::from("c"),
},
Epoch::new(5),
);
assert_eq!(at.epoch, Epoch::new(5));
}
}