diff --git a/src/backend/mod.rs b/src/backend/mod.rs deleted file mode 100644 index 4bfd49c..0000000 --- a/src/backend/mod.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::collections::HashMap; -use std::net::SocketAddr; -use std::sync::RwLock; -use std::sync::Arc; -use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; - -pub struct BackendPool { - pub backends: Arc>>>, -} - -#[derive(Debug)] -pub struct Backend { - pub id: String, - pub address: SocketAddr, - pub is_healthy: AtomicBool, // no clue how this should work, for now - pub current_load: AtomicUsize, // no clue how this should work, for now -} - -impl BackendPool { - pub fn new(initial_backends: Vec>) -> Self { - let mut map = HashMap::new(); - for backend in initial_backends { - map.insert(backend.id.clone(), backend); - } - - Self { - backends: Arc::new(RwLock::new(map)), - } - } - - pub fn add_backend(&self, backend: Arc) { - let mut backends_guard = self.backends - .write() - .expect("BackendPool lock poisoned"); - // let backends_guard = self.backends.read().unwrap_or_else(|poisoned| poisoned.into_inner()); - backends_guard.insert(backend.id.clone(), backend); - } - - pub fn get_backend(&self, id: &str) -> Option> { - let backends_guard = self.backends - .read() - .expect("BackendPool lock poisoned"); - // let backends_guard = self.backends.read().unwrap_or_else(|poisoned| poisoned.into_inner()); - backends_guard.get(id).cloned() - } - - pub fn bruh_amogus_sus(&self) { - for k in self.backends.read().unwrap().keys() { - self.backends.write().unwrap().get(k).unwrap().increment_current_load(); - } - } -} - -impl Backend { - pub fn new(id: String, address: SocketAddr) -> Self { - Self { - id: id, - address: address, - is_healthy: AtomicBool::new(false), - current_load: AtomicUsize::new(0), - } - } - - pub fn increment_current_load(&self) { - self.current_load.fetch_add(1, Ordering::SeqCst); - } - - pub fn decrement_current_load(&self) { - self.current_load.fetch_sub(1, Ordering::SeqCst); - } -} \ No newline at end of file diff --git a/src/balancer/adaptive_weight.rs b/src/balancer/adaptive_weight.rs index c74b888..46b0e47 100644 --- a/src/balancer/adaptive_weight.rs +++ b/src/balancer/adaptive_weight.rs @@ -19,7 +19,7 @@ impl ServerMetrics { ServerMetrics { cpu: 0.0, mem: 0.0, net: 0.0, io: 0.0 } } - pub fn update_ema(&mut self, cpu: f64, mem: f64, net: f64, io: f64, alpha: f64) { + pub fn update(&mut self, cpu: f64, mem: f64, net: f64, io: f64, alpha: f64) { self.cpu = cpu; self.mem = mem; self.net = net; @@ -40,29 +40,21 @@ impl ServerState { } } -/// This implementation keeps an EMA of reported resource usage (cpu/mem/net/io), -/// computes a composite load L = sum(coeff_i * metric_i) and converts that to -/// a selection weight. Lower load -> higher weight. Selection is weighted-random. pub struct AdaptiveBalancer { servers: Vec, // resource coefficients (cpu, mem, net, io) - sum to 1.0 coeffs: [f64; 4], - // EMA smoothing factor (alpha) alpha: f64, rng: SmallRng, } impl AdaptiveBalancer { - /// Create a new balancer from a list of backends. - /// `coeffs` are the importance weights for cpu,mem,net,io respectively. - /// `alpha` controls EMA smoothing (0..1). Typical alpha ~0.2-0.5. pub fn new(backends: Vec, coeffs: [f64; 4], alpha: f64) -> Self { let servers = backends.into_iter().map(ServerState::new).collect(); let rng = SmallRng::from_entropy(); AdaptiveBalancer { servers, coeffs, alpha, rng } } - /// Add a backend at runtime. pub fn add_backend(&mut self, backend: Backend) { self.servers.push(ServerState::new(backend)); } @@ -72,7 +64,7 @@ impl AdaptiveBalancer { pub fn update_metrics(&mut self, backend_addr: &str, cpu: f64, mem: f64, net: f64, io: f64) { for s in &mut self.servers { if s.backend.to_string() == backend_addr { - s.metrics.update_ema(cpu, mem, net, io, self.alpha); + s.metrics.update(cpu, mem, net, io, self.alpha); return; } }