Add week 8 materials

This commit is contained in:
osukhoroslov committed 2026-11-04 19:23:39 +03:00
1 parent a987b82834
commit 0bf71ed374
38 files changed
+2908

No files matched your search

@@ -0,0 +1,284 @@
use std::boxed::Box;
use std::cell::RefCell;
use std::rc::Rc;
use rand::prelude::*;
use rand_pcg::Pcg64;
use simcore::cast;
use simcore::context::SimulationContext;
use simcore::event::Event;
use simcore::handler::EventHandler;
use crate::events::{RequestArrivalEvent, SyncEvent};
use crate::host::Host;
#[derive(Copy, Clone)]
pub struct HostSnapshot {
pub id: usize,
pub requests: usize,
}
pub struct LoadBalancer {
state: Vec<HostSnapshot>,
hosts: Vec<Rc<RefCell<Host>>>,
selector: Box<dyn HostSelector>,
sync_interval: f64,
sync_until: f64,
ctx: Rc<RefCell<SimulationContext>>,
}
impl LoadBalancer {
pub fn new(
hosts: Vec<Rc<RefCell<Host>>>,
selector: Box<dyn HostSelector>,
sync_interval: f64,
sync_until: f64,
ctx: Rc<RefCell<SimulationContext>>,
) -> Self {
Self {
state: (0..hosts.len())
.map(|i| HostSnapshot { id: i, requests: 0 })
.collect(),
hosts,
selector,
sync_interval,
sync_until,
ctx,
}
}
}
impl EventHandler for LoadBalancer {
fn on(&mut self, event: Event) {
cast!(match event.data {
RequestArrivalEvent {
processing_time,
sender,
} => {
let host = self.selector.select(&self.state, &self.hosts, sender);
self.hosts[host.id]
.borrow_mut()
.on_new_request(processing_time, event.time);
}
SyncEvent {} => {
for (i, host) in self.hosts.iter().enumerate() {
self.state[i].requests = host.borrow().requests.len();
}
if event.time + self.sync_interval < self.sync_until {
self.ctx
.borrow_mut()
.emit_self(SyncEvent {}, self.sync_interval);
}
}
});
}
}
pub trait HostSelector {
fn select(
&mut self,
states: &[HostSnapshot],
hosts: &[Rc<RefCell<Host>>],
sender: usize,
) -> HostSnapshot;
}
pub struct RoundRobinSelector {
pub ptr: usize,
}
impl HostSelector for RoundRobinSelector {
fn select(
&mut self,
states: &[HostSnapshot],
hosts: &[Rc<RefCell<Host>>],
_sender: usize,
) -> HostSnapshot {
let id = self.ptr;
self.ptr += 1;
if self.ptr == hosts.len() {
self.ptr = 0;
}
states[id]
}
}
pub struct RandomSelector {
rng: Pcg64,
}
impl RandomSelector {
fn new(seed: u64) -> Self {
Self {
rng: Pcg64::seed_from_u64(seed),
}
}
}
impl HostSelector for RandomSelector {
fn select(
&mut self,
states: &[HostSnapshot],
_hosts: &[Rc<RefCell<Host>>],
_sender: usize,
) -> HostSnapshot {
*states.choose(&mut self.rng).unwrap()
}
}
pub struct LeastLoadedSelector {}
impl HostSelector for LeastLoadedSelector {
fn select(
&mut self,
states: &[HostSnapshot],
_hosts: &[Rc<RefCell<Host>>],
_sender: usize,
) -> HostSnapshot {
*states.iter().min_by_key(|s| s.requests).unwrap()
}
}
pub struct PowerOfKSelector {
k: usize,
rng: Pcg64,
}
impl PowerOfKSelector {
pub fn new(k: usize, seed: u64) -> Self {
Self {
k,
rng: Pcg64::seed_from_u64(seed),
}
}
}
impl HostSelector for PowerOfKSelector {
fn select(
&mut self,
states: &[HostSnapshot],
_hosts: &[Rc<RefCell<Host>>],
_sender: usize,
) -> HostSnapshot {
**states
.iter()
.choose_multiple(&mut self.rng, self.k)
.iter()
.min_by_key(|s| s.requests)
.unwrap()
}
}
pub struct PowerOfKSelectorWithUpdates {
k: usize,
rng: Pcg64,
}
impl PowerOfKSelectorWithUpdates {
pub fn new(k: usize, seed: u64) -> Self {
Self {
k,
rng: Pcg64::seed_from_u64(seed),
}
}
}
impl HostSelector for PowerOfKSelectorWithUpdates {
fn select(
&mut self,
states: &[HostSnapshot],
hosts: &[Rc<RefCell<Host>>],
_sender: usize,
) -> HostSnapshot {
**states
.iter()
.choose_multiple(&mut self.rng, self.k)
.iter()
.min_by_key(|s| hosts[s.id].borrow().requests.len())
.unwrap()
}
}
pub struct HashSelector {}
impl HostSelector for HashSelector {
fn select(
&mut self,
states: &[HostSnapshot],
_hosts: &[Rc<RefCell<Host>>],
sender: usize,
) -> HostSnapshot {
states[sender % states.len()]
}
}
pub fn create_selector_by_name(name: &str, seed: u64, num_hosts: usize) -> Box<dyn HostSelector> {
match name {
"RoundRobin" => Box::new(RoundRobinSelector {
ptr: Pcg64::seed_from_u64(seed).gen_range(0..num_hosts),
}),
"Random" => Box::new(RandomSelector::new(seed)),
"LeastLoaded" => Box::new(LeastLoadedSelector {}),
"Hash" => Box::new(HashSelector {}),
_ => {
if name.starts_with("PowerOfK[k=") && name.ends_with("]") {
let k = name[11..name.len() - 1].parse::<usize>().unwrap();
assert!(
(1..=num_hosts).contains(&k),
"k must be between 1 and the number of servers"
);
Box::new(PowerOfKSelector::new(k, seed))
} else if name.starts_with("PowerOfKWithUpdates[k=") && name.ends_with("]") {
let k = name[22..name.len() - 1].parse::<usize>().unwrap();
assert!(
(1..=num_hosts).contains(&k),
"k must be between 1 and the number of servers"
);
Box::new(PowerOfKSelectorWithUpdates::new(k, seed))
} else {
panic!("Unknown host selector: {name}");
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use simcore::simulation::Simulation;
fn choices(name: &str, seed: u64) -> Vec<usize> {
let mut sim = Simulation::new(0);
let log = Rc::new(RefCell::new(Vec::new()));
let hosts: Vec<_> = (0..100)
.map(|id| {
Rc::new(RefCell::new(Host::new(
id,
1.0,
log.clone(),
Rc::new(RefCell::new(sim.create_context(format!("host_{id}")))),
)))
})
.collect();
let states: Vec<_> = (0..hosts.len())
.map(|id| HostSnapshot { id, requests: 0 })
.collect();
let mut selector = create_selector_by_name(name, seed, hosts.len());
(0..200)
.map(|_| selector.select(&states, &hosts, 0).id)
.collect()
}
#[test]
fn selector_streams_are_reproducible_and_depend_on_seed() {
for name in [
"Random",
"RoundRobin",
"PowerOfK[k=2]",
"PowerOfKWithUpdates[k=2]",
] {
assert_eq!(choices(name, 111), choices(name, 111), "{name}");
assert_ne!(choices(name, 111), choices(name, 222), "{name}");
}
}
}
@@ -0,0 +1,8 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Config {
pub balancer: String,
pub n_balancers: u32,
pub sync_interval: f64,
}
@@ -0,0 +1,13 @@
use serde::Serialize;
#[derive(Clone, Serialize)]
pub struct SyncEvent {}
#[derive(Clone, Serialize)]
pub struct RequestArrivalEvent {
pub processing_time: f64,
pub sender: usize,
}
#[derive(Clone, Serialize)]
pub struct RequestEndEvent {}
@@ -0,0 +1,60 @@
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use simcore::cast;
use simcore::context::SimulationContext;
use simcore::event::Event;
use simcore::handler::EventHandler;
use crate::events::RequestEndEvent;
use crate::log::EventLog;
pub struct Host {
#[allow(dead_code)]
pub id: usize,
pub requests: VecDeque<(f64, f64)>,
slowdown: f64,
log: Rc<RefCell<EventLog>>,
ctx: Rc<RefCell<SimulationContext>>,
}
impl Host {
pub fn new(
id: usize,
slowdown: f64,
log: Rc<RefCell<EventLog>>,
ctx: Rc<RefCell<SimulationContext>>,
) -> Self {
Self {
id,
requests: Default::default(),
slowdown,
log,
ctx,
}
}
pub fn on_new_request(&mut self, duration: f64, time: f64) {
self.requests.push_back((duration * self.slowdown, time));
if self.requests.len() == 1 {
self.ctx
.borrow_mut()
.emit_self(RequestEndEvent {}, duration * self.slowdown);
}
}
}
impl EventHandler for Host {
fn on(&mut self, event: Event) {
cast!(match event.data {
RequestEndEvent {} => {
let (_, old) = self.requests.pop_front().unwrap();
self.log.borrow_mut().push((event.time, old));
if let Some((delay, _)) = self.requests.front() {
self.ctx.borrow_mut().emit_self(RequestEndEvent {}, *delay);
}
}
});
}
}
@@ -0,0 +1 @@
pub type EventLog = Vec<(f64, f64)>;
@@ -0,0 +1,224 @@
use std::cell::RefCell;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufWriter;
use std::path::Path;
use std::rc::Rc;
use clap::Parser;
use rand::prelude::*;
use rand_distr::Exp;
use rand_pcg::Pcg64;
use simcore::simulation::Simulation;
mod balancer;
mod config;
mod events;
mod host;
mod log;
mod metrics;
use crate::balancer::{create_selector_by_name, LoadBalancer};
use crate::config::Config;
use crate::events::{RequestArrivalEvent, SyncEvent};
use crate::host::Host;
use crate::log::EventLog;
use crate::metrics::{windowed_p99, WINDOW};
#[derive(Parser, Debug)]
#[clap(about, long_about = None)]
struct Args {
/// Results dump path.
#[clap(long)]
dump: String,
/// Configuration JSON file.
#[clap(long)]
config: String,
/// Number of servers.
#[clap(long, default_value = "100")]
servers: u32,
/// Last arrival time shown on the graph (seconds).
#[clap(long, default_value = "3600")]
time_limit: f64,
/// Number of senders generating requests.
#[clap(long, default_value = "200")]
senders: u32,
/// Load distribution preset (balanced/skewed).
#[clap(long, default_value = "balanced")]
preset: String,
/// Adds slower servers to simulate heterogenous cluster.
#[clap(long)]
different_servers: bool,
/// Random seed.
#[clap(long, default_value = "123")]
seed: u64,
/// Reproduce correlated selector streams for the teaching experiment.
#[clap(long)]
correlated_selectors: bool,
}
fn run_simulation(args: &Args, config: &Config) -> Vec<(f64, f64)> {
assert!(
args.servers > 0 && args.senders > 0,
"servers and senders must be positive"
);
assert!(
args.time_limit.is_finite() && args.time_limit > 0.0,
"time-limit must be finite and positive"
);
assert!(config.n_balancers > 0, "n_balancers must be positive");
assert!(
config.sync_interval.is_finite() && config.sync_interval > 0.0,
"sync_interval must be finite and positive"
);
// Generate all arrivals needed by the last complete measurement window.
let arrivals_end = args.time_limit + WINDOW;
let mut sim = Simulation::new(args.seed);
let ctx = sim.create_context("entry point");
let log = Rc::new(RefCell::new(EventLog::new()));
let mut hosts = Vec::with_capacity(args.servers as usize);
for i in 0..args.servers {
let slowdown = if args.different_servers && i < args.servers / 20 {
5.0
} else {
1.0
};
hosts.push(Rc::new(RefCell::new(Host::new(
i as usize,
slowdown,
log.clone(),
Rc::new(RefCell::new(sim.create_context(format!("host_{i}")))),
))));
sim.add_handler(format!("host_{i}"), hosts[i as usize].clone());
}
let mut balancers = Vec::with_capacity(config.n_balancers as usize);
// Keep selector randomness separate from the workload generator.
let mut selector_seeds = Pcg64::seed_from_u64(args.seed);
for i in 0..config.n_balancers {
let seed = if args.correlated_selectors {
111
} else {
selector_seeds.gen()
};
balancers.push(Rc::new(RefCell::new(LoadBalancer::new(
hosts.clone(),
create_selector_by_name(&config.balancer, seed, hosts.len()),
config.sync_interval,
arrivals_end,
Rc::new(RefCell::new(sim.create_context(format!("balancer_{i}")))),
))));
sim.add_handler(format!("balancer_{i}"), balancers[i as usize].clone());
ctx.emit(SyncEvent {}, sim.lookup_id(&format!("balancer_{i}")), 0.0);
}
let mut rng = Pcg64::seed_from_u64(args.seed);
let mut routing_rng = Pcg64::seed_from_u64(args.seed.wrapping_add(1));
for sender in 0..(args.senders as usize) {
let distr = Exp::new(if args.preset == "skewed" && sender == 0 {
50.0
} else {
1.0
})
.unwrap();
let mut t = 0f64;
loop {
t += distr.sample(&mut rng) * 5.;
if t >= arrivals_end {
break;
}
let processing_time = rng.gen_range(0.5..2.5);
ctx.emit(
RequestArrivalEvent {
processing_time,
sender,
},
sim.lookup_id(&format!(
"balancer_{}",
routing_rng.gen_range(0..balancers.len())
)),
t,
);
}
}
// Periodic updates stop at arrivals_end. Drain every queued request so that
// slow responses are measured in full, not truncated at a time cutoff.
sim.step_until_no_events();
let requests = log
.borrow()
.iter()
.map(|&(end, start)| (start, end - start))
.collect();
windowed_p99(requests, args.time_limit)
}
fn main() {
let args = Args::parse();
let configs: Vec<Config> =
serde_json::from_reader(File::open(Path::new(&args.config)).unwrap()).unwrap();
let mut out = BufWriter::new(File::create(&args.dump).unwrap());
for config in configs.iter() {
let result = run_simulation(&args, config);
out.write_all(config.balancer.as_bytes()).unwrap();
out.write_all(b"\n").unwrap();
out.write_all(
result
.iter()
.map(|x| format!("{:.3}", x.0))
.collect::<Vec<_>>()
.join(",")
.as_bytes(),
)
.unwrap();
out.write_all(b"\n").unwrap();
out.write_all(
result
.iter()
.map(|x| format!("{}", x.1))
.collect::<Vec<_>>()
.join(",")
.as_bytes(),
)
.unwrap();
out.write_all(b"\n").unwrap();
}
out.flush().unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn overloaded_server_finishes_requests_after_arrival_horizon() {
let args = Args::parse_from([
"lab",
"--dump",
"unused",
"--config",
"unused",
"--servers",
"1",
"--senders",
"100",
"--time-limit",
"1",
]);
let config = Config {
balancer: "Hash".into(),
n_balancers: 1,
sync_interval: 0.01,
};
let result = run_simulation(&args, &config);
assert!(!result.is_empty());
assert!(result.iter().all(|(t, _)| *t <= args.time_limit));
assert!(result[0].1 > args.time_limit + WINDOW);
assert_eq!(result, run_simulation(&args, &config));
}
}
@@ -0,0 +1,132 @@
use std::cmp::Ordering;
use std::collections::BTreeSet;
pub const WINDOW: f64 = 60.0;
#[derive(Copy, Clone, PartialEq)]
struct RequestTime {
time: f64,
id: usize,
}
impl Eq for RequestTime {}
impl Ord for RequestTime {
fn cmp(&self, other: &Self) -> Ordering {
self.time
.total_cmp(&other.time)
.then(self.id.cmp(&other.id))
}
}
impl PartialOrd for RequestTime {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/// Nearest-rank p99 of response times for requests arriving in [t, t + WINDOW).
/// Emit one point per distinct arrival time t <= time_limit.
pub fn windowed_p99(mut requests: Vec<(f64, f64)>, time_limit: f64) -> Vec<(f64, f64)> {
requests.sort_by(|a, b| a.0.total_cmp(&b.0));
let mut result = Vec::new();
let mut items = BTreeSet::new();
let mut end = 0;
let mut start = 0;
while start < requests.len() && requests[start].0 <= time_limit {
let time = requests[start].0;
while end < requests.len() && requests[end].0 < time + WINDOW {
items.insert(RequestTime {
time: requests[end].1,
id: end,
});
end += 1;
}
// ceil(0.99 * n), computed exactly, without floating-point rounding.
let rank = items.len() - items.len() / 100;
let p99 = items.iter().rev().nth(items.len() - rank).unwrap().time;
result.push((time, p99));
// Equal response times remain distinct observations thanks to id.
// Remove the entire arrival-time group before advancing the window.
while start < requests.len() && requests[start].0 == time {
items.remove(&RequestTime {
time: requests[start].1,
id: start,
});
start += 1;
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn expires_old_requests_and_excludes_right_boundary() {
let requests = vec![
(0.0, 100.0),
(1.0, 10.0),
(61.0, 1.0),
(62.0, 1.0),
(123.0, 3.0),
];
assert_eq!(
windowed_p99(requests, 123.0),
vec![
(0.0, 100.0),
(1.0, 10.0),
(61.0, 1.0),
(62.0, 1.0),
(123.0, 3.0)
]
);
}
#[test]
fn uses_nearest_rank_and_counts_equal_response_times() {
let mut requests = vec![(0.0, 1.0); 99];
requests.push((0.0, 100.0));
assert_eq!(windowed_p99(requests, 0.0), vec![(0.0, 1.0)]);
let requests = (1..=100).map(|n| (0.0, n as f64)).collect();
assert_eq!(windowed_p99(requests, 0.0), vec![(0.0, 99.0)]);
}
#[test]
fn sorts_arrivals_and_limits_reported_times() {
assert_eq!(
windowed_p99(vec![(120.0, 8.0), (60.0, 2.0), (0.0, 4.0)], 60.0),
vec![(0.0, 4.0), (60.0, 2.0)]
);
assert!(windowed_p99(Vec::new(), 60.0).is_empty());
}
#[test]
fn matches_independent_sort_per_window_reference() {
let requests: Vec<_> = (0..400)
.map(|i| (((i * 37) % 241) as f64, ((i * 17) % 31 + 1) as f64))
.collect();
let mut times: Vec<_> = requests
.iter()
.map(|r| r.0)
.filter(|t| *t <= 180.0)
.collect();
times.sort_by(f64::total_cmp);
times.dedup();
let expected: Vec<_> = times
.into_iter()
.map(|t| {
let mut values: Vec<_> = requests
.iter()
.filter(|r| r.0 >= t && r.0 < t + WINDOW)
.map(|r| r.1)
.collect();
values.sort_by(f64::total_cmp);
let rank = (0.99 * values.len() as f64).ceil() as usize;
(t, values[rank - 1])
})
.collect();
assert_eq!(windowed_p99(requests, 180.0), expected);
}
}