use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Clone, Debug, Default)]
pub enum PromotionPolicy {
#[default]
Eager,
Lazy,
SampleRate(u64),
OnlyTo(usize),
}
#[derive(Debug, Clone, Copy)]
pub struct PromotionContext {
pub source_tier: usize,
pub total_tiers: usize,
}
#[derive(Default)]
pub struct PromotionGate {
counter: AtomicU64,
policy: PromotionPolicy,
}
impl PromotionGate {
#[must_use]
pub fn new(policy: PromotionPolicy) -> Self {
Self {
counter: AtomicU64::new(0),
policy,
}
}
#[must_use]
pub fn arc(policy: PromotionPolicy) -> Arc<Self> {
Arc::new(Self::new(policy))
}
pub fn decide(&self, ctx: PromotionContext) -> Option<usize> {
if ctx.source_tier == 0 {
return None; }
match self.policy {
PromotionPolicy::Eager => Some(ctx.source_tier),
PromotionPolicy::Lazy => None,
PromotionPolicy::SampleRate(rate) => {
if rate <= 1 {
return Some(ctx.source_tier);
}
let n = self.counter.fetch_add(1, Ordering::Relaxed);
if n.is_multiple_of(rate) {
Some(ctx.source_tier)
} else {
None
}
}
PromotionPolicy::OnlyTo(max) => {
let target = ctx.source_tier.min(max + 1);
if target == 0 { None } else { Some(target) }
}
}
}
#[must_use]
pub fn policy(&self) -> &PromotionPolicy {
&self.policy
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ctx(source: usize, total: usize) -> PromotionContext {
PromotionContext {
source_tier: source,
total_tiers: total,
}
}
#[test]
fn eager_promotes_to_source_tier() {
let g = PromotionGate::new(PromotionPolicy::Eager);
assert_eq!(g.decide(ctx(0, 3)), None); assert_eq!(g.decide(ctx(1, 3)), Some(1));
assert_eq!(g.decide(ctx(2, 3)), Some(2));
}
#[test]
fn lazy_never_promotes() {
let g = PromotionGate::new(PromotionPolicy::Lazy);
for s in 0..5 {
assert_eq!(g.decide(ctx(s, 5)), None);
}
}
#[test]
fn sample_rate_one_acts_like_eager() {
let g = PromotionGate::new(PromotionPolicy::SampleRate(1));
assert_eq!(g.decide(ctx(1, 3)), Some(1));
assert_eq!(g.decide(ctx(2, 3)), Some(2));
}
#[test]
fn sample_rate_zero_acts_like_eager() {
let g = PromotionGate::new(PromotionPolicy::SampleRate(0));
assert_eq!(g.decide(ctx(1, 3)), Some(1));
}
#[test]
fn sample_rate_three_promotes_one_in_three() {
let g = PromotionGate::new(PromotionPolicy::SampleRate(3));
let mut promoted = 0;
for _ in 0..9 {
if g.decide(ctx(1, 2)).is_some() {
promoted += 1;
}
}
assert_eq!(promoted, 3);
}
#[test]
fn source_tier_zero_never_promotes() {
for policy in [
PromotionPolicy::Eager,
PromotionPolicy::Lazy,
PromotionPolicy::SampleRate(2),
PromotionPolicy::OnlyTo(5),
] {
let g = PromotionGate::new(policy);
assert_eq!(g.decide(ctx(0, 3)), None);
}
}
#[test]
fn only_to_caps_promotion_target() {
let g = PromotionGate::new(PromotionPolicy::OnlyTo(0));
assert_eq!(g.decide(ctx(2, 3)), Some(1));
}
#[test]
fn only_to_above_source_uses_source() {
let g = PromotionGate::new(PromotionPolicy::OnlyTo(5));
assert_eq!(g.decide(ctx(1, 3)), Some(1));
}
#[test]
fn default_is_eager() {
let g = PromotionGate::default();
assert!(matches!(g.policy(), PromotionPolicy::Eager));
}
#[test]
fn arc_constructor_wraps_policy() {
let g = PromotionGate::arc(PromotionPolicy::Lazy);
assert!(matches!(g.policy(), PromotionPolicy::Lazy));
}
#[test]
fn policy_round_trips_via_arc() {
let p = PromotionPolicy::SampleRate(10);
let g = PromotionGate::new(p);
if let PromotionPolicy::SampleRate(n) = g.policy() {
assert_eq!(*n, 10);
} else {
panic!("policy mismatch");
}
}
}