rargo 0.7.10

The rargo client: syncs a worktree and its environment to a rargo build server and brings back the results
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
409
410
411
412
413
414
415
416
use rargo_net::io::{self, UnboundedReceiver, UnboundedSender, unbounded_channel};
use rargo::link_watch::{self, Tick, Watch};
use rargo_net::link::Link;
use rargo_proto::{CONTROL, ClientMsg, Digest, Frame, Reorder, ServerMsg};
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

const WINDOW: usize = 32 << 20;
const WATCH_STEP: Duration = Duration::from_millis(200);

const _: () = assert!(WINDOW <= u32::MAX as usize, "a window's permits fit acquire_many");

fn dead_limit(kind: &str) -> Duration {
    let set = std::env::var("RARGO_LINK_DEAD_SECS").ok().and_then(|v| v.parse::<u64>().ok()).filter(|n| *n >= 1).map(Duration::from_secs);
    match (set, kind) {
        (Some(base), "udp") => base,
        (Some(base), _) => base * 8,
        (None, "udp") => link_watch::SILENT_AFTER,
        (None, _) => link_watch::SILENT_AFTER_TCP,
    }
}

struct Out {
    seq: u64,
    tx: UnboundedSender<(Vec<u8>, usize)>,
    urgent: UnboundedSender<Vec<u8>>,
}

type Missing = (Vec<Digest>, Vec<Digest>);

struct MuxState<S, M> {
    streams: BTreeMap<u32, S>,
    missing: BTreeMap<u64, M>,
    order: Reorder<ServerMsg>,
    next_stream: u32,
    next_id: u64,
    dead: bool,
}

enum Route<S, M> {
    Stream(S, ServerMsg),
    Missing(M, Missing),
    Drop,
}

impl<S: Clone, M> MuxState<S, M> {
    fn new() -> Self {
        Self { streams: BTreeMap::new(), missing: BTreeMap::new(), order: Reorder::new(true), next_stream: 1, next_id: 1, dead: false }
    }

    fn open(&mut self, dest: S) -> (u32, Option<S>) {
        let stream = self.next_stream;
        assert_ne!(stream, CONTROL);
        assert_ne!(stream, rargo_proto::KEEPALIVE);
        self.next_stream += 1;
        if self.dead {
            return (stream, Some(dest));
        }
        assert!(self.streams.insert(stream, dest).is_none());
        (stream, None)
    }

    fn close(&mut self, stream: u32) -> Option<S> {
        self.order.end(stream);
        self.streams.remove(&stream)
    }

    fn incoming(&mut self, frame: Frame<ServerMsg>) -> Vec<(u32, ServerMsg)> {
        if self.dead || (frame.stream != CONTROL && !self.streams.contains_key(&frame.stream)) {
            Vec::new()
        } else {
            self.order.push(frame)
        }
    }

    fn route(&mut self, stream: u32, msg: ServerMsg) -> Route<S, M> {
        if stream == CONTROL {
            if let ServerMsg::Missing { id, trees, blobs } = msg
                && let Some(dest) = self.missing.remove(&id)
            {
                return Route::Missing(dest, (trees, blobs));
            }
            return Route::Drop;
        }
        let dest = if matches!(msg, ServerMsg::Exit(_) | ServerMsg::Exec(_) | ServerMsg::Fetched { .. }) {
            self.close(stream)
        } else {
            self.streams.get(&stream).cloned()
        };
        match dest {
            Some(dest) => Route::Stream(dest, msg),
            None => Route::Drop,
        }
    }

    fn next_id(&mut self) -> u64 {
        let id = self.next_id;
        self.next_id += 1;
        id
    }

    fn expect_missing(&mut self, id: u64, dest: M) -> Option<M> {
        if self.dead {
            return Some(dest);
        }
        assert!(self.missing.insert(id, dest).is_none(), "offer {id} awaited twice");
        None
    }

    fn fail(&mut self) -> Option<(BTreeMap<u32, S>, BTreeMap<u64, M>)> {
        if self.dead {
            return None;
        }
        self.dead = true;
        self.order = Reorder::new(true);
        Some((std::mem::take(&mut self.streams), std::mem::take(&mut self.missing)))
    }

    #[cfg(test)]
    fn invariants(&self) -> bool {
        self.streams.keys().all(|&id| id > CONTROL && id < self.next_stream && id != rargo_proto::KEEPALIVE)
            && (!self.dead || (self.streams.is_empty() && self.missing.is_empty()))
    }
}

type StreamSender = UnboundedSender<ServerMsg>;
type WireState = MuxState<StreamSender, io::oneshot::Sender<Missing>>;

enum Outgoing {
    Urgent(Option<Vec<u8>>),
    Bulk(Option<(Vec<u8>, usize)>),
}

enum DeadWait {
    Gone,
    Tick,
}

pub struct Mux {
    link: Arc<Link>,
    out: Mutex<Out>,
    window: Arc<io::Semaphore>,
    state: Mutex<WireState>,
    pub gone: io::Notify,
    started: Instant,
    watch: Mutex<Watch>,
    pub bytes_out: AtomicU64,
    pub bytes_in: AtomicU64,
}

impl Mux {
    pub fn new(link: Link) -> Arc<Mux> {
        let link = Arc::new(link);
        let (tx, mut rx) = unbounded_channel::<(Vec<u8>, usize)>();
        let (urgent, mut urgent_rx) = unbounded_channel::<Vec<u8>>();
        let mux = Arc::new(Mux {
            link: link.clone(),
            out: Mutex::new(Out { seq: 0, tx, urgent }),
            window: Arc::new(io::Semaphore::new(WINDOW as u32)),
            state: Mutex::new(MuxState::new()),
            gone: io::Notify::new(),
            started: Instant::now(),
            watch: Mutex::new(Watch::new(Duration::ZERO, dead_limit(link.kind()))),
            bytes_out: AtomicU64::new(0),
            bytes_in: AtomicU64::new(0),
        });
        let m = mux.clone();
        io::spawn(move || {
            loop {
                let got = io::race_biased(vec![
                    Box::new(|| Outgoing::Urgent(urgent_rx.recv())),
                    Box::new(|| Outgoing::Bulk(rx.recv())),
                ]);
                let (frame, bulk) = match got {
                    Outgoing::Urgent(Some(frame)) => (frame, 0),
                    Outgoing::Bulk(Some(frame)) => frame,
                    Outgoing::Urgent(None) | Outgoing::Bulk(None) => break,
                };
                m.bytes_out.fetch_add(frame.len() as u64, Ordering::Relaxed);
                let sent = m.link.send(&frame);
                if bulk > 0 {
                    m.window.add_permits(bulk as u32);
                }
                if sent.is_err() {
                    break;
                }
            }
            m.die();
        });
        let m = mux.clone();
        io::spawn(move || {
            while let Some(raw) = m.link.recv() {
                m.bytes_in.fetch_add(raw.len() as u64, Ordering::Relaxed);
                let now = m.started.elapsed();
                m.watch.lock().unwrap().heard(now);
                let Ok(frame) = rargo_proto::decode::<Frame<ServerMsg>>(&raw) else { break };
                let ordered = m.state.lock().unwrap().incoming(frame);
                for (stream, msg) in ordered {
                    let msg = match msg {
                        ServerMsg::Packed(z) => match rargo_proto::unpack(&z).and_then(|b| rargo_proto::decode(&b)) {
                            Ok(m) => m,
                            Err(_) => continue,
                        },
                        m => m,
                    };
                    let action = m.state.lock().unwrap().route(stream, msg);
                    match action {
                        Route::Stream(dest, msg) => { let _ = dest.send(msg); }
                        Route::Missing(dest, missing) => { let _ = dest.send(missing); }
                        Route::Drop => {}
                    }
                }
            }
            m.die();
        });
        let m = mux.clone();
        io::spawn(move || {
            loop {
                io::sleep(WATCH_STEP);
                if m.is_dead() {
                    break;
                }
                let now = m.started.elapsed();
                let tick = {
                    let mut watch = m.watch.lock().unwrap();
                    if m.link.kind() == "udp" {
                        watch.heard(now.saturating_sub(m.link.idle()));
                    }
                    watch.tick(now)
                };
                match tick {
                    Tick::Wait => {}
                    Tick::Ping(n) => {
                        let ping = rargo_proto::encode(&Frame { stream: rargo_proto::KEEPALIVE, seq: 0, msg: ClientMsg::Ping(n) });
                        let _ = m.out.lock().unwrap().urgent.send(ping);
                    }
                    Tick::Silent(_) => {
                        m.die();
                        break;
                    }
                }
            }
        });
        mux
    }

    pub fn kind(&self) -> &'static str {
        self.link.kind()
    }

    /// How long the server had been silent when this link gave up on it, if that is why it closed.
    pub fn silent(&self) -> Option<Duration> {
        self.watch.lock().unwrap().silent()
    }

    pub fn is_dead(&self) -> bool {
        self.state.lock().unwrap().dead || self.link.is_closed()
    }

    /// Resolves when the link is closed. A missed wake is caught on the next check.
    pub fn until_dead(&self) {
        loop {
            if self.is_dead() {
                return;
            }
            match io::race_biased(vec![
                Box::new(|| { self.gone.notified(); DeadWait::Gone }),
                Box::new(|| { io::sleep(Duration::from_millis(200)); DeadWait::Tick }),
            ]) {
                DeadWait::Gone => return,
                DeadWait::Tick => {}
            }
        }
    }

    pub fn die(&self) {
        let drained = self.state.lock().unwrap().fail();
        let Some(endpoints) = drained else { return };
        self.link.close();
        drop(endpoints);
        self.window.close();
        self.gone.notify_waiters();
    }

    fn queue(&self, stream: u32, msg: &ClientMsg, bulk: usize) {
        let mut encoded_msg = None;
        if matches!(msg, ClientMsg::Trees(_) | ClientMsg::Env(_) | ClientMsg::Run(_) | ClientMsg::Fetch { .. }) {
            if let Some(z) = rargo_proto::pack(&rargo_proto::encode(msg)) {
                encoded_msg = Some(ClientMsg::Packed(z));
            }
        }
        let mut out = self.out.lock().unwrap();
        let frame = match &encoded_msg {
            Some(packed) => rargo_proto::encode(&Frame { stream, seq: out.seq, msg: packed }),
            None => rargo_proto::encode(&Frame { stream, seq: out.seq, msg }),
        };
        out.seq += 1;
        let _ = out.tx.send((frame, bulk));
    }

    pub fn post(&self, stream: u32, msg: &ClientMsg) {
        self.queue(stream, msg, 0);
    }

    pub fn post_bulk(&self, stream: u32, msg: &ClientMsg, bytes: usize) -> std::io::Result<()> {
        let n = bytes.clamp(1, WINDOW);
        assert!(n >= 1);
        assert!(n <= WINDOW);
        let permit = self.window.acquire_many(n as u32)?;
        permit.forget();
        self.queue(stream, msg, n);
        Ok(())
    }

    pub fn open(&self) -> (u32, UnboundedSender<ServerMsg>, UnboundedReceiver<ServerMsg>) {
        let (tx, rx) = unbounded_channel();
        let (id, rejected) = self.state.lock().unwrap().open(tx.clone());
        drop(rejected);
        (id, tx, rx)
    }

    pub fn cancel(&self, stream: u32) {
        let closed = self.state.lock().unwrap().close(stream);
        if closed.is_some() {
            self.post(stream, &ClientMsg::Cancel);
        }
    }

    pub fn next_id(&self) -> u64 {
        self.state.lock().unwrap().next_id()
    }

    pub fn expect_missing(&self, id: u64) -> io::oneshot::Receiver<Missing> {
        let (tx, rx) = io::oneshot::channel();
        let rejected = self.state.lock().unwrap().expect_missing(id, tx);
        drop(rejected);
        rx
    }

    pub fn fetch(&self, hashes: Vec<Digest>, to: &UnboundedSender<ServerMsg>) -> u64 {
        self.ask(to, |id| ClientMsg::Fetch { id, hashes })
    }

    pub fn ask(&self, to: &UnboundedSender<ServerMsg>, question: impl FnOnce(u64) -> ClientMsg) -> u64 {
        let (stream, rejected) = self.state.lock().unwrap().open(to.clone());
        drop(rejected);
        let id = stream as u64;
        self.post(stream, &question(id));
        id
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reorder_and_terminal_frames_stay_with_their_own_stream() {
        let mut state = MuxState::<_, ()>::new();
        let (first, _) = state.open("first");
        assert!(state.invariants());
        let (second, _) = state.open("second");
        assert!(state.invariants());

        let stdout = |byte| ServerMsg::Stdout(vec![byte]);
        assert!(state.incoming(Frame { stream: first, seq: 1, msg: ServerMsg::Exit(0) }).is_empty());
        assert!(state.invariants());
        let msgs = state.incoming(Frame { stream: second, seq: 0, msg: stdout(2) });
        assert!(state.invariants());
        let [(stream, msg)] = msgs.try_into().ok().unwrap();
        assert!(matches!(state.route(stream, msg), Route::Stream("second", ServerMsg::Stdout(bytes)) if bytes == [2]));
        assert!(state.invariants());

        let msgs = state.incoming(Frame { stream: first, seq: 0, msg: stdout(1) });
        assert!(state.invariants());
        let [(stream, msg), (last_stream, last)] = msgs.try_into().ok().unwrap();
        assert!(matches!(state.route(stream, msg), Route::Stream("first", ServerMsg::Stdout(bytes)) if bytes == [1]));
        assert!(state.invariants());
        assert!(matches!(state.route(last_stream, last), Route::Stream("first", ServerMsg::Exit(0))));
        assert!(state.invariants());
        assert!(state.incoming(Frame { stream: first, seq: 2, msg: stdout(9) }).is_empty());
        assert!(state.invariants());
        assert!(matches!(state.route(first, stdout(9)), Route::Drop));
        assert!(state.invariants());
    }

    #[test]
    fn failure_drains_every_open_registration_once() {
        let mut state = MuxState::new();
        let (closed, _) = state.open("cancelled");
        assert!(state.invariants());
        let (a, _) = state.open("first");
        assert!(state.invariants());
        let (b, _) = state.open("second");
        assert!(state.invariants());
        assert_eq!(state.close(closed), Some("cancelled"));
        assert!(state.invariants());
        let id = state.next_id();
        assert!(state.invariants());
        assert_eq!(state.expect_missing(id, "offer"), None);
        assert!(state.invariants());
        let (streams, offers) = state.fail().unwrap();
        assert!(state.invariants());
        assert_eq!(streams, BTreeMap::from([(a, "first"), (b, "second")]));
        assert_eq!(offers, BTreeMap::from([(id, "offer")]));
        assert!(state.fail().is_none());
        assert!(state.invariants());
        assert_eq!(state.open("late").1, Some("late"));
        assert!(state.invariants());
        assert_eq!(state.expect_missing(id, "late offer"), Some("late offer"));
        assert!(state.invariants());
    }
}