kunobi-daemon 0.10.1

Process ownership, request draining, and verified upgrades for local daemons
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Typed health and drain observations.
#![cfg(feature = "wire-async")]
use kunobi_daemon::{
    Lifecycle, ServiceIdentity,
    control::ControlService,
    wire::{self, capability, operation},
};
use std::sync::Arc;
use tokio::time::{Duration, Instant};

fn offer() -> wire::Hello {
    let id = ServiceIdentity::new([0x51; 16], "test-service", "test", "one").unwrap();
    wire::Hello::new(
        &id,
        capability::HEALTH | capability::HEALTH_DETAILS | capability::DRAIN,
        capability::HEALTH | capability::HEALTH_DETAILS,
    )
}
fn request(operation: u32) -> wire::Control {
    wire::Control {
        operation,
        request_id: 81,
        ..Default::default()
    }
}

#[tokio::test(start_paused = true)]
async fn health_observes_initialization_and_hours_of_drain_without_waiting_for_work() {
    let lifecycle = Arc::new(Lifecycle::default());
    let service = ControlService::new(Arc::clone(&lifecycle), 22, "test-build".into(), 123);
    let health = request(operation::HEALTH);
    let initial = wire::Health::from_response(&service.handle(&health).unwrap(), &health).unwrap();
    assert!(!initial.ready);
    assert!(!initial.draining);
    assert_eq!(initial.process_id, std::process::id());
    assert_eq!(initial.generation, 22);
    service.mark_ready();
    let pending = lifecycle.begin().unwrap();
    let drain = request(operation::DRAIN);
    let reply = wire::Health::from_response(&service.handle(&drain).unwrap(), &drain).unwrap();
    assert!(reply.draining);
    assert!(!reply.ready);
    assert_eq!(reply.active, 1);
    assert!(lifecycle.begin().is_none());
    tokio::time::advance(Duration::from_secs(7200)).await;
    service.mark_ready();
    assert!(!service.snapshot().ready, "drain cannot be reopened");
    assert_eq!(service.snapshot().active, 1);
    drop(pending);
    assert_eq!(service.snapshot().active, 0);
}

#[tokio::test]
async fn binary_control_uses_negotiated_identity_and_correlated_typed_responses() {
    let lifecycle = Arc::new(Lifecycle::default());
    let service = Arc::new(ControlService::new(lifecycle, 4, "test".into(), 8));
    service.mark_ready();
    let (client, server) = tokio::io::duplex(128);
    let task = tokio::spawn(async move {
        service
            .serve(server, &offer(), Instant::now() + Duration::from_secs(1))
            .await
    });
    let mut session = wire::AsyncSession::connect(client, &offer()).await.unwrap();
    let request = request(operation::HEALTH);
    session.send(&request).await.unwrap();
    let reply = session.receive().await.unwrap();
    let health = wire::Health::from_response(&reply, &request).unwrap();
    assert!(health.ready);
    assert_eq!(health.build, "test");
    assert_eq!(health.revision, 8);
    let mut wrong = request;
    wrong.request_id += 1;
    assert!(wire::Health::from_response(&reply, &wrong).is_err());
    task.await.unwrap().unwrap();
}

#[tokio::test(start_paused = true)]
async fn stalled_handshake_is_bounded_without_starting_drain() {
    let lifecycle = Arc::new(Lifecycle::default());
    let service = ControlService::new(Arc::clone(&lifecycle), 0, "test".into(), 0);
    let (_client, server) = tokio::io::duplex(64);
    let error = service
        .serve(server, &offer(), Instant::now() + Duration::from_secs(2))
        .await
        .unwrap_err();
    assert_eq!(error.kind(), std::io::ErrorKind::TimedOut);
    assert!(lifecycle.accepting_calls());
    let mut wrong = request(operation::DRAIN);
    wrong.generation = 3;
    assert!(service.handle(&wrong).is_err());
    wrong.generation = 0;
    wrong.payload.push(1);
    assert!(service.handle(&wrong).is_err());
    wrong.payload.clear();
    wrong.kind = wire::MessageKind::Application.into();
    assert!(service.handle(&wrong).is_err());
    assert!(lifecycle.accepting_calls());
}

fn watch_offer() -> wire::Hello {
    let mut offer = offer();
    offer.supported |= capability::WATCH | capability::APPLICATION;
    offer
}

#[tokio::test(start_paused = true)]
async fn watch_streams_ready_committed_selection_and_drain_without_idle_polling() {
    use kunobi_daemon::control::WatchClient;
    use wire::LifecycleChange;
    let lifecycle = Arc::new(Lifecycle::default());
    let service = Arc::new(ControlService::new(
        Arc::clone(&lifecycle),
        4,
        "old".into(),
        8,
    ));
    // A staged candidate must report the incumbent, not imply it was selected.
    assert!(
        service
            .selection_committed(4, "wrong-build".into())
            .is_err()
    );
    service.selection_committed(3, "incumbent".into()).unwrap();
    service.selection_committed(3, "incumbent".into()).unwrap();
    let (client, server) = tokio::io::duplex(1024);
    let serving = Arc::clone(&service);
    let task = tokio::spawn(async move {
        serving
            .serve(
                server,
                &watch_offer(),
                Instant::now() + Duration::from_secs(1),
            )
            .await
    });
    let mut client = WatchClient::connect(
        client,
        &watch_offer(),
        Instant::now() + Duration::from_secs(1),
    )
    .await
    .unwrap()
    .unwrap();
    assert_eq!(client.snapshot().change, LifecycleChange::Snapshot);
    assert_eq!(client.snapshot().selected_generation, Some(3));
    assert!(!client.snapshot().ready);
    // The setup deadline does not become a subscription lifetime or heartbeat.
    tokio::time::advance(Duration::from_secs(3600)).await;
    assert!(!task.is_finished());
    service.mark_ready();
    assert_eq!(
        client.changed().await.unwrap().change,
        LifecycleChange::Ready
    );
    service.selection_committed(5, "new".into()).unwrap();
    let event = client.changed().await.unwrap();
    assert_eq!(event.change, LifecycleChange::SelectionChanged);
    assert_eq!(event.selected_generation, Some(5));
    assert_eq!(event.selected_build, "new");
    assert_eq!(event.generation, 4);
    assert!(service.selection_committed(3, "stale".into()).is_err());
    assert!(service.selection_committed(5, "different".into()).is_err());
    lifecycle.start_drain();
    let event = client.changed().await.unwrap();
    assert_eq!(event.change, LifecycleChange::Draining);
    assert!(!event.ready);
    service.mark_retiring();
    assert_eq!(
        client.changed().await.unwrap().change,
        LifecycleChange::Retiring
    );
    drop(client);
    task.await.unwrap().unwrap();
}

#[tokio::test]
async fn late_watchers_get_current_state_and_slow_watchers_get_coalesced_updates() {
    use kunobi_daemon::control::WatchClient;
    let service = Arc::new(ControlService::new(
        Arc::new(Lifecycle::default()),
        1,
        "first".into(),
        0,
    ));
    service.mark_ready();
    service.selection_committed(2, "second".into()).unwrap();
    let (client, server) = tokio::io::duplex(128);
    let serving = Arc::clone(&service);
    let task = tokio::spawn(async move {
        serving
            .serve(
                server,
                &watch_offer(),
                Instant::now() + Duration::from_secs(2),
            )
            .await
    });
    let mut client = WatchClient::connect(
        client,
        &watch_offer(),
        Instant::now() + Duration::from_secs(2),
    )
    .await
    .unwrap()
    .unwrap();
    assert!(client.snapshot().ready);
    assert_eq!(client.snapshot().selected_generation, Some(2));
    // No task can run between these publications. Only the last selection
    // needs to be retained; WATCH is current state, not an audit log.
    for generation in 3..1000 {
        service
            .selection_committed(generation, generation.to_string())
            .unwrap();
    }
    service.mark_retiring();
    let event = client.changed().await.unwrap();
    assert_eq!(event.selected_generation, Some(999));
    assert_eq!(event.selected_build, "999");
    assert!(event.retiring && event.draining && !event.ready);
    drop(client);
    task.await.unwrap().unwrap();
}

#[tokio::test]
async fn watch_falls_back_only_when_the_peer_does_not_negotiate_it() {
    use kunobi_daemon::control::WatchClient;
    let (client, server) = tokio::io::duplex(1024);
    let task = tokio::spawn(async move {
        let mut session = wire::AsyncSession::accept(server, &offer()).await.unwrap();
        assert!(
            session.receive().await.is_err(),
            "client sent WATCH to a legacy peer"
        );
    });
    assert!(
        WatchClient::connect(
            client,
            &watch_offer(),
            Instant::now() + Duration::from_secs(1)
        )
        .await
        .unwrap()
        .is_none()
    );
    task.await.unwrap();

    let (client, server) = tokio::io::duplex(1024);
    let task = tokio::spawn(async move {
        let mut session = wire::AsyncSession::accept(server, &watch_offer())
            .await
            .unwrap();
        let request = session.receive().await.unwrap();
        assert_eq!(request.operation, operation::WATCH);
        let reply = wire::Control {
            operation: operation::WATCH,
            request_id: request.request_id,
            payload: vec![0xff],
            ..Default::default()
        };
        session.send(&reply).await.unwrap();
    });
    assert!(
        WatchClient::connect(
            client,
            &watch_offer(),
            Instant::now() + Duration::from_secs(1)
        )
        .await
        .is_err(),
        "an advertised malformed stream must not downgrade"
    );
    task.await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn watch_releases_a_subscriber_that_stops_reading() {
    let service = Arc::new(ControlService::new(
        Arc::new(Lifecycle::default()),
        1,
        "large-build-id".repeat(512),
        0,
    ));
    let (client, server) = tokio::io::duplex(128);
    let task = tokio::spawn(async move {
        service
            .serve(
                server,
                &watch_offer(),
                Instant::now() + Duration::from_secs(1),
            )
            .await
    });
    let mut client = wire::AsyncSession::connect(client, &watch_offer())
        .await
        .unwrap();
    client.send(&request(operation::WATCH)).await.unwrap();
    let error = tokio::time::timeout(Duration::from_secs(6), task)
        .await
        .unwrap()
        .unwrap()
        .unwrap_err();
    assert_eq!(error.kind(), std::io::ErrorKind::TimedOut);
    drop(client);
}

#[tokio::test]
async fn invalid_watch_events_are_terminal_and_cannot_resume_the_stream() {
    use buffa::Message;
    use kunobi_daemon::control::WatchClient;
    for fault in 0..21 {
        let (client, server) = tokio::io::duplex(4096);
        let task = tokio::spawn(async move {
            let mut session = wire::AsyncSession::accept(server, &watch_offer())
                .await
                .unwrap();
            let request = session.receive().await.unwrap();
            let mut event = wire::LifecycleEvent {
                sequence: 1,
                process_id: 123,
                generation: 4,
                build: "own".into(),
                selected_generation: Some(5),
                selected_build: "selected".into(),
                ..Default::default()
            };
            if fault == 18 {
                event.selected_generation = None;
                event.selected_build.clear();
            }
            if fault == 19 || fault == 20 {
                event.draining = true;
                event.retiring = fault == 20;
            }
            let mut reply = wire::Control {
                operation: operation::WATCH,
                request_id: request.request_id,
                generation: 4,
                payload: event.encode_to_vec(),
                ..Default::default()
            };
            session.send(&reply).await.unwrap();
            event.sequence = 2;
            match fault {
                0 => event.sequence = 3,
                1 => event.process_id += 1,
                2 => event.build = "other".into(),
                3 => event.selected_generation = Some(4),
                4 => event.selected_build = "replaced-at-same-epoch".into(),
                5 => {
                    event.ready = true;
                    event.draining = true;
                }
                6 => event.retiring = true,
                7 => event.change = 999.into(),
                8 => reply.request_id += 1,
                9 => reply.token.push(1),
                10 => {
                    event.selected_generation = None;
                    event.selected_build.clear();
                }
                11 => reply.generation += 1,
                12 => reply.kind = wire::MessageKind::Application.into(),
                13 => reply.operation = operation::HEALTH,
                14 => reply.offset = Some(0),
                15 => event.change = wire::LifecycleChange::Ready.into(),
                16 => event.change = wire::LifecycleChange::Draining.into(),
                17 => event.change = wire::LifecycleChange::Retiring.into(),
                18 => {
                    event.change = wire::LifecycleChange::SelectionChanged.into();
                    event.selected_generation = None;
                    event.selected_build.clear();
                }
                19 => event.draining = false,
                20 => event.retiring = false,
                _ => unreachable!(),
            }
            reply.payload = event.encode_to_vec();
            session.send(&reply).await.unwrap();
        });
        let mut client = WatchClient::connect(
            client,
            &watch_offer(),
            Instant::now() + Duration::from_secs(2),
        )
        .await
        .unwrap()
        .unwrap();
        assert!(
            client.changed().await.is_err(),
            "accepted invalid event {fault}"
        );
        assert!(
            client
                .changed()
                .await
                .unwrap_err()
                .to_string()
                .contains("WATCH failed")
        );
        task.await.unwrap();
    }
}

#[tokio::test]
async fn watch_rejects_requests_with_payloads_or_followup_bytes() {
    for payload in [true, false] {
        let service = ControlService::new(Arc::new(Lifecycle::default()), 1, "test".into(), 0);
        let (client, server) = tokio::io::duplex(4096);
        let task = tokio::spawn(async move {
            service
                .serve(
                    server,
                    &watch_offer(),
                    Instant::now() + Duration::from_secs(1),
                )
                .await
        });
        let mut client = wire::AsyncSession::connect(client, &watch_offer())
            .await
            .unwrap();
        let mut subscribe = request(operation::WATCH);
        if payload {
            subscribe.payload.push(1);
        }
        client.send(&subscribe).await.unwrap();
        if !payload {
            client.receive().await.unwrap();
            client.send(&request(operation::HEALTH)).await.unwrap();
        }
        assert_eq!(
            task.await.unwrap().unwrap_err().kind(),
            std::io::ErrorKind::InvalidData
        );
    }
}

#[tokio::test(start_paused = true)]
async fn cancelling_a_watch_read_requires_reconnection() {
    use kunobi_daemon::control::WatchClient;
    let service = Arc::new(ControlService::new(
        Arc::new(Lifecycle::default()),
        4,
        "test".into(),
        8,
    ));
    let (client, server) = tokio::io::duplex(1024);
    let serving = Arc::clone(&service);
    let task = tokio::spawn(async move {
        serving
            .serve(
                server,
                &watch_offer(),
                Instant::now() + Duration::from_secs(1),
            )
            .await
    });
    let mut client = WatchClient::connect(
        client,
        &watch_offer(),
        Instant::now() + Duration::from_secs(1),
    )
    .await
    .unwrap()
    .unwrap();
    assert_eq!(client.snapshot().selected_generation, None);
    assert!(
        tokio::time::timeout(Duration::from_secs(1), client.changed())
            .await
            .is_err()
    );
    service.mark_ready();
    assert!(
        client
            .changed()
            .await
            .unwrap_err()
            .to_string()
            .contains("reconnect")
    );
    drop(client);
    // The peer may observe either EOF or a broken pipe if readiness was in flight.
    let _ = task.await.unwrap();
}

#[tokio::test]
async fn watch_initial_snapshot_has_coherent_identity_and_selection() {
    use buffa::Message;
    use kunobi_daemon::control::WatchClient;
    for fault in 0..6 {
        let (client, server) = tokio::io::duplex(4096);
        let task = tokio::spawn(async move {
            let mut session = wire::AsyncSession::accept(server, &watch_offer())
                .await
                .unwrap();
            let request = session.receive().await.unwrap();
            let mut event = wire::LifecycleEvent {
                sequence: 1,
                process_id: 123,
                generation: 4,
                build: "own".into(),
                selected_generation: Some(4),
                selected_build: "own".into(),
                ..Default::default()
            };
            match fault {
                0 => event.sequence = 2,
                1 => {
                    event.change = wire::LifecycleChange::Ready.into();
                    event.ready = true;
                }
                2 => event.process_id = 0,
                3 => event.selected_generation = None,
                4 => event.selected_build = "wrong-build".into(),
                _ => {} // The selected serving generation must also be accepted.
            }
            session
                .send(&wire::Control {
                    operation: operation::WATCH,
                    request_id: request.request_id,
                    generation: 4,
                    payload: event.encode_to_vec(),
                    ..Default::default()
                })
                .await
                .unwrap();
        });
        let result = WatchClient::connect(
            client,
            &watch_offer(),
            Instant::now() + Duration::from_secs(2),
        )
        .await;
        if fault == 5 {
            assert!(result.unwrap().is_some());
        } else {
            assert!(result.is_err(), "accepted invalid initial snapshot {fault}");
        }
        task.await.unwrap();
    }
}

#[tokio::test]
async fn watch_checks_its_own_offer_and_needs_no_unrelated_capabilities() {
    use kunobi_daemon::control::WatchClient;
    let (client, _server) = tokio::io::duplex(128);
    let result =
        WatchClient::connect(client, &offer(), Instant::now() + Duration::from_millis(20)).await;
    assert_eq!(
        result.err().unwrap().kind(),
        std::io::ErrorKind::InvalidData
    );
    let mut only_watch = watch_offer();
    only_watch.supported = capability::WATCH;
    only_watch.required = capability::WATCH;
    let server_offer = only_watch.clone();
    let (client, server) = tokio::io::duplex(1024);
    let task = tokio::spawn(async move {
        ControlService::new(Arc::new(Lifecycle::default()), 4, "own".into(), 0)
            .serve(
                server,
                &server_offer,
                Instant::now() + Duration::from_secs(1),
            )
            .await
    });
    let client = WatchClient::connect(client, &only_watch, Instant::now() + Duration::from_secs(1))
        .await
        .unwrap()
        .unwrap();
    drop(client);
    task.await.unwrap().unwrap();
}

#[tokio::test]
async fn watch_request_fields_are_validated_before_subscribing() {
    for case in 0..5 {
        let (client, server) = tokio::io::duplex(1024);
        let task = tokio::spawn(async move {
            ControlService::new(Arc::new(Lifecycle::default()), 4, "own".into(), 0)
                .serve(
                    server,
                    &watch_offer(),
                    Instant::now() + Duration::from_secs(1),
                )
                .await
        });
        let mut session = wire::AsyncSession::connect(client, &watch_offer())
            .await
            .unwrap();
        let mut request = request(operation::WATCH);
        match case {
            0 => request.token.push(1),
            1 => request.offset = Some(0),
            2 => request.generation = 3,
            3 => request.generation = 4,
            _ => {}
        }
        session.send(&request).await.unwrap();
        if case >= 3 {
            let reply = session.receive().await.unwrap();
            assert_eq!(reply.generation, 4);
            assert_eq!(reply.request_id, request.request_id);
            drop(session);
            task.await.unwrap().unwrap();
        } else {
            assert_eq!(
                task.await.unwrap().unwrap_err().kind(),
                std::io::ErrorKind::InvalidData
            );
        }
    }
}