hya-core 0.2.2

I/O-free multi-source download scheduler: interval algebra, divergence-triggered repair, liveness
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Reproduce the repair storm against the shipped scheduler, and measure what
//! each fix is worth.
//!
//! Run: `cargo run --release -p hya-core --example storm -- [out.csv]`
//!
//! The existing simulator does not reproduce it, and this is why: its origin
//! model gives every connection an independent rate and charges nothing for a
//! request. Reality gives neither.
//!
//!  * **Shared bottleneck.** n connections to one origin do not have n
//!    independent capacities; they share one. Moving bytes from connection A to
//!    connection B cannot make B faster except by making A slower, so the
//!    per-connection rate divergence the repair rule fires on is not evidence
//!    of anything repairable.
//!  * **Setup cost and slow start.** A repair hands the taker a range it must
//!    open a request for. That costs `delta`, and the new flow starts with a
//!    small congestion window, so for several RTTs it delivers well below the
//!    share the scheduler priced the repair against.
//!
//! Model: capacity `C` split across connections in proportion to their ramp
//! factor `1 - exp(-age/tau)`, renormalised so the aggregate is exactly `C`
//! (stationary transfer: the split jitters, the total does not). A connection
//! that has just been given a range delivers nothing for `delta` and then
//! restarts its ramp.
//!
//! With that model the correct repair count on a stationary transfer is zero.

use hya_core::{Action, Scheduler, Source};

/// Deterministic PRNG, so a run is reproducible.
struct Rng(u64);
impl Rng {
    fn next_f64(&mut self) -> f64 {
        // xorshift64*
        let mut x = self.0;
        x ^= x >> 12;
        x ^= x << 25;
        x ^= x >> 27;
        self.0 = x;
        (x.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 11) as f64 / (1u64 << 53) as f64
    }
    /// Approximately standard normal (sum of 4 uniforms, Irwin-Hall).
    fn normal(&mut self) -> f64 {
        let s: f64 = (0..4).map(|_| self.next_f64()).sum();
        (s - 2.0) * 1.732_050_8
    }
}

#[derive(Clone, Copy)]
struct Flow {
    /// Wall clock at which this flow's current request finishes setting up.
    ready_at: f64,
    /// Whether the flow currently holds a request at all.
    live: bool,
    /// The `hi` the running fetch task captured at spawn. `fetch_range` loops
    /// `while off < hi` on THIS value, so a scheduler-side shrink is invisible
    /// to it: the victim keeps pulling bytes the taker is now also pulling.
    task_hi: u64,
    /// Where the victim's socket has actually got to.
    task_off: u64,
    /// Persistent share weight. TCP flows sharing one bottleneck do NOT settle
    /// at equal rates: the share goes as roughly 1/RTT, and cwnd history makes
    /// the asymmetry outlive any single round trip. This is the term the
    /// existing simulator omits, and it is a property of the PATH, so a repair
    /// cannot move it — handing a laggard's bytes to a fast flow does not make
    /// the laggard faster, it just costs one more setup.
    weight: f64,
}

struct Params {
    size: u64,
    n: usize,
    /// Aggregate bottleneck capacity, bytes/s.
    capacity: f64,
    /// Per-request setup cost, seconds.
    delta: f64,
    /// Slow-start time constant, seconds.
    tau: f64,
    /// Per-connection multiplicative jitter, as a fraction.
    sigma: f64,
    /// Lognormal spread of the PERSISTENT per-flow share weight.
    sigma_w: f64,
    theta_scale: f64,
    seed: u64,
    /// At this fraction of the fluid optimum, connection 0's share weight is cut
    /// by `collapse_to`. This is the case repair EXISTS for, and the guard rails
    /// added for the shared-bottleneck case must not suppress it: a scheduler that
    /// never repairs is trivially free of repair storms and useless against a
    /// mirror that dies mid-transfer.
    collapse_at: Option<f64>,
    collapse_to: f64,
}

struct Outcome {
    makespan: f64,
    requests: u64,
    repairs: u64,
    /// Fraction of the object already held when each repair fired, averaged.
    /// A value near 1.0 means repairs cluster in the endgame.
    repair_progress: Vec<f64>,
    /// theta at the moment of each repair, seconds.
    repair_theta: Vec<f64>,
    /// Connection-seconds spent in setup rather than receiving.
    setup_waste: f64,
    /// Bytes that crossed the wire twice because a shrunk victim kept sending.
    dup_bytes: u64,
}

fn run(p: &Params, zombies_bite: bool) -> Outcome {
    let dt = 0.010;
    let sources = vec![Source {
        gamma_est: p.capacity / p.n as f64,
        delta_est: p.delta,
        ..Default::default()
    }];
    let mut sched = Scheduler::new(p.size, sources, &[p.n]).with_theta_scale(p.theta_scale);
    let mut rng = Rng(p.seed | 1);
    let mut flows: Vec<Flow> = (0..p.n)
        .map(|_| Flow {
            ready_at: 0.0,
            live: false,
            weight: (p.sigma_w * rng.normal()).exp(),
            task_hi: 0,
            task_off: 0,
        })
        .collect();
    let mut setup_waste = 0.0;
    let mut dup_bytes = 0u64;
    let mut collapsed = false;
    let mut repair_progress: Vec<f64> = Vec::new();
    let mut repair_theta: Vec<f64> = Vec::new();
    let mut last_repairs = 0u64;
    let mut now = 0.0;

    // Generous horizon: 100x the fluid optimum.
    let horizon = 100.0 * p.size as f64 / p.capacity;
    while now < horizon {
        for act in sched.tick(now) {
            match act {
                Action::Request { conn, range } => {
                    // A new request means a new flow: pay setup, restart the ramp.
                    flows[conn].ready_at = now + p.delta;
                    flows[conn].live = true;
                    flows[conn].task_hi = range.hi;
                    flows[conn].task_off = range.lo;
                }
                Action::Cancel { conn } => flows[conn].live = false,
                // The fix under test. `zombies_bite == false` is the transport
                // honouring it: the victim's loop stops at the new far end. Left
                // unhandled (`true`) it models the old behaviour, where the
                // scheduler shrank its own copy and the socket never learned.
                Action::Shrink { conn, hi } => {
                    if !zombies_bite {
                        flows[conn].task_hi = hi;
                    }
                }
            }
        }
        if sched.stats.repairs > last_repairs {
            let k = (sched.stats.repairs - last_repairs) as usize;
            let frac = sched.bytes_held() as f64 / p.size as f64;
            let th = sched.theta_now(now);
            for _ in 0..k {
                repair_progress.push(frac);
                repair_theta.push(th);
            }
            last_repairs = sched.stats.repairs;
        }
        if sched.is_complete() {
            break;
        }

        // Adversary: one connection's share of the bottleneck collapses.
        if let Some(frac) = p.collapse_at {
            let at = frac * p.size as f64 / p.capacity;
            if now >= at && !collapsed {
                flows[0].weight *= p.collapse_to;
                collapsed = true;
            }
        }

        // Who can receive right now, and with how much of the window open.
        //
        // A "zombie" is a connection whose scheduler-side range was shrunk by a
        // repair while its socket keeps streaming to the `hi` it captured. It
        // still competes for the bottleneck, and none of what it delivers is
        // credited. Modelling it is the difference between reproducing the storm
        // and not.
        let mut ramp = vec![0.0f64; p.n];
        let mut zombie = vec![false; p.n];
        for j in 0..p.n {
            let sched_hi = sched.conn_range(j).map(|(_, _, hi)| hi);
            if zombies_bite {
                let live_socket = flows[j].live && flows[j].task_off < flows[j].task_hi;
                let shrunk = match sched_hi {
                    Some(hi) => hi < flows[j].task_hi,
                    None => true,
                };
                if live_socket && shrunk && now >= flows[j].ready_at {
                    let age = now - flows[j].ready_at;
                    let base = 1.0 - (-age / p.tau).exp();
                    ramp[j] = (base * flows[j].weight).max(0.0);
                    zombie[j] = true;
                    continue;
                }
            }
            let has_range = sched.conn_range(j).is_some();
            if has_range && flows[j].live && now >= flows[j].ready_at {
                let age = now - flows[j].ready_at;
                let base = 1.0 - (-age / p.tau).exp();
                let noise = (p.sigma * rng.normal()).exp();
                ramp[j] = (base * noise * flows[j].weight).max(0.0);
            } else if has_range && flows[j].live {
                setup_waste += dt;
            }
        }
        let total: f64 = ramp.iter().sum();
        if total > 0.0 {
            for j in 0..p.n {
                if ramp[j] <= 0.0 {
                    continue;
                }
                // Renormalise: the SPLIT jitters, the aggregate is conserved.
                let rate = p.capacity * ramp[j] / total;
                let bytes = (rate * dt) as u64;
                if bytes == 0 {
                    continue;
                }
                if zombie[j] {
                    // Bytes burned on a span the taker is fetching too.
                    let room = flows[j].task_hi.saturating_sub(flows[j].task_off);
                    let step = bytes.min(room);
                    flows[j].task_off += step;
                    dup_bytes += step;
                    if flows[j].task_off >= flows[j].task_hi {
                        flows[j].live = false;
                    }
                    continue;
                }
                if let Some((_, pos, _)) = sched.conn_range(j) {
                    sched.on_bytes_at(j, pos, bytes, now, dt);
                    flows[j].task_off = pos + bytes;
                }
            }
        }
        now += dt;
    }

    Outcome {
        makespan: now,
        requests: sched.stats.requests,
        repairs: sched.stats.repairs,
        setup_waste,
        dup_bytes,
        repair_progress,
        repair_theta,
    }
}

fn main() {
    let out = std::env::args().nth(1);
    let size = 5_300_000u64;
    let capacity = 1_400_000.0; // ~1.4 MB/s, the CRAN measurements' ballpark
    let oracle = size as f64 / capacity;
    let seeds = 12u64;

    let mut csv = String::from(
        "n,zombie,seed,makespan_s,oracle_ratio,requests,repairs,dup_bytes,setup_waste_s\n",
    );

    println!(
        "object {} bytes, shared capacity {:.0} B/s, fluid oracle {:.2}s, {} seeds",
        size, capacity, oracle, seeds
    );
    println!("stationary transfer: the correct repair count is 0.");
    println!("zombie=yes is the SHIPPED transport: a repair shrinks the victim's range");
    println!("scheduler-side but emits no Cancel, and fetch_range loops on the `hi` it");
    println!("captured at spawn, so the stolen span crosses the wire twice.\n");
    println!(
        "{:>3}  {:>7}  {:>9}  {:>7}  {:>8}  {:>8}  {:>9}",
        "n", "zombie", "makespan", "ratio", "requests", "repairs", "dup_MB"
    );

    for &n in &[1usize, 2, 4, 8, 16] {
        for &z in &[true, false] {
            let (mut ms, mut rq, mut rp, mut db) = (0.0, 0u64, 0u64, 0u64);
            let mut all_prog: Vec<f64> = Vec::new();
            let mut all_theta: Vec<f64> = Vec::new();
            for sd in 0..seeds {
                let p = Params {
                    size,
                    n,
                    capacity,
                    delta: 0.12,
                    tau: 0.35,
                    sigma: 0.10,
                    sigma_w: 0.45,
                    theta_scale: 1.0,
                    seed: 0x5EED + sd * 7919,
                    collapse_at: None,
                    collapse_to: 1.0,
                };
                let o = run(&p, z);
                csv.push_str(&format!(
                    "{},{},{},{:.4},{:.4},{},{},{},{:.4}\n",
                    n,
                    if z { 1 } else { 0 },
                    p.seed,
                    o.makespan,
                    o.makespan / oracle,
                    o.requests,
                    o.repairs,
                    o.dup_bytes,
                    o.setup_waste
                ));
                all_prog.extend(o.repair_progress.iter().copied());
                all_theta.extend(o.repair_theta.iter().copied());
                ms += o.makespan;
                rq += o.requests;
                rp += o.repairs;
                db += o.dup_bytes;
            }
            // Where in the transfer do the surviving repairs fire, and how wide was
            // the deadband when they did? theta = scale*sqrt(delta*T_rem/n) goes to
            // zero as the remaining time does, so the deadband is narrowest exactly
            // when a repair has the least time left to pay itself back.
            if !z && !all_prog.is_empty() {
                let nr = all_prog.len() as f64;
                let mean_p = all_prog.iter().sum::<f64>() / nr;
                let late = all_prog.iter().filter(|x| **x > 0.9).count();
                let mean_th = all_theta.iter().sum::<f64>() / nr;
                let min_th = all_theta.iter().cloned().fold(f64::INFINITY, f64::min);
                eprintln!(
                    "    n={n}: {} repairs | mean progress at repair {:.2} | {} fired past 90% \
                     | theta mean {:.3}s min {:.4}s",
                    all_prog.len(),
                    mean_p,
                    late,
                    mean_th,
                    min_th
                );
            }
            let f = seeds as f64;
            println!(
                "{:>3}  {:>7}  {:>8.2}s  {:>7.3}  {:>8.1}  {:>8.1}  {:>8.2}",
                n,
                if z { "yes" } else { "no" },
                ms / f,
                (ms / f) / oracle,
                rq as f64 / f,
                rp as f64 / f,
                db as f64 / f / 1e6
            );
        }
    }

    // ---- the case repair exists for -------------------------------------------
    //
    // Everything above is a STATIONARY transfer, where the correct repair count is
    // zero and every repair is waste. That makes it the right test for a storm and
    // the wrong test for the mechanism: a scheduler that simply never repaired
    // would score perfectly on it. So run the opposite scenario too — one
    // connection's share of the bottleneck collapses to 5% at 30% of the way
    // through — and check that repairs still fire and still help.
    println!("\ncollapse arm: connection 0 drops to 5% of its share at 30% progress");
    println!(
        "{:>3}  {:>9}  {:>7}  {:>8}  {:>8}",
        "n", "makespan", "ratio", "requests", "repairs"
    );
    for &n in &[2usize, 4, 8] {
        let (mut ms, mut rq, mut rp) = (0.0, 0u64, 0u64);
        for sd in 0..seeds {
            let o = run(
                &Params {
                    size,
                    n,
                    capacity,
                    delta: 0.12,
                    tau: 0.35,
                    sigma: 0.10,
                    sigma_w: 0.45,
                    theta_scale: 1.0,
                    seed: 0x5EED + sd * 7919,
                    collapse_at: Some(0.30),
                    collapse_to: 0.05,
                },
                false,
            );
            ms += o.makespan;
            rq += o.requests;
            rp += o.repairs;
        }
        let f = seeds as f64;
        println!(
            "{:>3}  {:>8.2}s  {:>7.3}  {:>8.1}  {:>8.1}",
            n,
            ms / f,
            (ms / f) / oracle,
            rq as f64 / f,
            rp as f64 / f
        );
    }

    if let Some(path) = out {
        std::fs::write(&path, csv).expect("write csv");
        println!("\nwrote {}", path);
    }
}