fusee-server 1.0.0

Fusee server mode: the server holds the log, workflow code runs in the application
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
//! One thread per shard of a lease.
//!
//! The thread owns the shard's storage, handles that shard's requests in the
//! order they arrived, does all of its blocking file I/O (flushes, run-store
//! reads and writes, recovery reads, compaction) and writes the replies to
//! the socket itself. Shards therefore flush in parallel, a slow shard holds
//! up no other shard, and the connection task only reads, routes and keeps
//! the lease alive. Each shard's replies leave in the order its requests
//! were handled, which the worker's run-store offset check relies on.

use super::storage::WorkerDisk;
use super::{Server, SharedLog};
use crate::proto::{self, req, resp};
use crate::spin::{self, Signal};
use fusee::local::{LocalEnv, LocalOptions};
use fusee_core::wire::{Reader, put_bytes, put_u32, put_u64, put_varint};
use fusee_core::{Lsn, frame};
use std::collections::VecDeque;
use std::io;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::oneshot;

/// A shard stops taking requests off the socket for itself once this many
/// request bytes wait for its thread; the connection then stops reading.
const QUEUE_LIMIT: usize = 32 << 20;
/// The longest a shard thread spins for its next request.
const SPIN_CAP: Duration = Duration::from_micros(100);

pub(super) type TryWriteFn = dyn Fn(&[u8]) -> io::Result<usize> + Send + Sync;

/// Replies to the lease holder, from the connection task and every shard
/// thread. Frames are written whole and in the order they were sent; what
/// the socket does not take at once waits here for the connection task.
pub(super) struct Outbox {
    state: Mutex<Out>,
    write: Box<TryWriteFn>,
    /// Tells the connection task that output is waiting for the socket.
    pub(super) queued: tokio::sync::Notify,
}

struct Out {
    buf: Vec<u8>,
    /// When the waiting output last made progress.
    since: Instant,
    failed: Option<String>,
}

impl Outbox {
    pub(super) fn new(write: Box<TryWriteFn>) -> Self {
        Self {
            state: Mutex::new(Out {
                buf: Vec::new(),
                since: Instant::now(),
                failed: None,
            }),
            write,
            queued: tokio::sync::Notify::new(),
        }
    }

    pub(super) fn send(&self, frames: &[u8]) {
        if frames.is_empty() {
            return;
        }
        let mut s = self.state.lock().unwrap_or_else(|e| e.into_inner());
        if s.failed.is_some() {
            return;
        }
        let mut rest = frames;
        if s.buf.is_empty() {
            match self.write_some(rest) {
                Ok(n) => rest = &rest[n..],
                Err(e) => {
                    s.failed = Some(e.to_string());
                    drop(s);
                    self.queued.notify_one();
                    return;
                }
            }
            if rest.is_empty() {
                return;
            }
            s.since = Instant::now();
        }
        s.buf.extend_from_slice(rest);
        drop(s);
        self.queued.notify_one();
    }

    fn write_some(&self, mut data: &[u8]) -> io::Result<usize> {
        let total = data.len();
        while !data.is_empty() {
            match (self.write)(data) {
                Ok(0) => return Err(io::ErrorKind::WriteZero.into()),
                Ok(n) => data = &data[n..],
                Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
                Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(total - data.len())
    }

    pub(super) fn has_queued(&self) -> bool {
        let s = self.state.lock().unwrap_or_else(|e| e.into_inner());
        !s.buf.is_empty() || s.failed.is_some()
    }

    /// Writes what the socket takes now. Returns how long the output still
    /// waiting has made no progress, if any is waiting.
    pub(super) fn write_queued(&self) -> io::Result<Option<Duration>> {
        let mut s = self.state.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(e) = &s.failed {
            return Err(io::Error::other(e.clone()));
        }
        if s.buf.is_empty() {
            return Ok(None);
        }
        let n = match self.write_some(&s.buf) {
            Ok(n) => n,
            Err(e) => {
                s.failed = Some(e.to_string());
                return Err(e);
            }
        };
        if n > 0 {
            s.buf.drain(..n);
            s.since = Instant::now();
        }
        Ok((!s.buf.is_empty()).then(|| s.since.elapsed()))
    }
}

/// Requests waiting for one shard's thread.
pub(super) struct Inbox {
    jobs: Mutex<Jobs>,
    signal: Signal,
    bytes: AtomicUsize,
    /// Shared by all shards of a lease: a queue fell below its limit.
    drained: Arc<tokio::sync::Notify>,
    /// The shard thread stopped the shard: it accepts no more writes.
    stopped: AtomicBool,
}

#[derive(Default)]
struct Jobs {
    queue: VecDeque<(u8, Vec<u8>)>,
    closed: bool,
    /// The thread took jobs and has not finished them (or sent their
    /// replies) yet.
    busy: bool,
}

impl Inbox {
    fn new(drained: Arc<tokio::sync::Notify>) -> Self {
        Self {
            jobs: Mutex::new(Jobs::default()),
            signal: Signal::default(),
            bytes: AtomicUsize::new(0),
            drained,
            stopped: AtomicBool::new(false),
        }
    }

    /// Runs `f` if every request routed to this shard so far has been
    /// handled and answered and the shard accepts writes: a request `f`
    /// handles then keeps its place in the shard's order. Returns `None`
    /// (without running `f`) otherwise.
    pub(super) fn if_idle<T>(&self, f: impl FnOnce() -> T) -> Option<T> {
        let j = self.jobs.lock().unwrap_or_else(|e| e.into_inner());
        if j.busy || !j.queue.is_empty() || j.closed || self.stopped.load(Ordering::Acquire) {
            return None;
        }
        // Held across `f`: the thread cannot take a job meanwhile, and only
        // the connection task, which is running `f`, pushes them.
        Some(f())
    }

    pub(super) fn push(&self, kind: u8, body: Vec<u8>) {
        self.bytes.fetch_add(body.len(), Ordering::AcqRel);
        self.jobs
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .queue
            .push_back((kind, body));
        self.signal.raise();
    }

    /// Background I/O finished: the thread polls its storage.
    fn wake(&self) {
        self.signal.raise();
    }

    fn close(&self) {
        self.jobs.lock().unwrap_or_else(|e| e.into_inner()).closed = true;
        self.signal.raise();
    }

    /// The shard thread stopped the shard: it accepts no more writes.
    pub(super) fn is_stopped(&self) -> bool {
        self.stopped.load(Ordering::Acquire)
    }

    pub(super) fn over_limit(&self) -> bool {
        self.bytes.load(Ordering::Acquire) > QUEUE_LIMIT
    }

    fn take(&self) -> (VecDeque<(u8, Vec<u8>)>, bool) {
        self.signal.take();
        let mut j = self.jobs.lock().unwrap_or_else(|e| e.into_inner());
        j.busy = !j.queue.is_empty();
        (std::mem::take(&mut j.queue), j.closed)
    }

    /// The jobs taken last are handled and their replies sent.
    fn done(&self) {
        self.jobs.lock().unwrap_or_else(|e| e.into_inner()).busy = false;
    }

    fn handled(&self, len: usize) {
        let before = self.bytes.fetch_sub(len, Ordering::AcqRel);
        if before > QUEUE_LIMIT && before - len <= QUEUE_LIMIT {
            self.drained.notify_one();
        }
    }
}

/// Where a shard thread gets its storage. With `log`, the shard's records
/// go to the server's shared log (log format 3) and its directory keeps
/// only the run store and checkpoints.
pub(super) enum Disk {
    Open {
        dir: PathBuf,
        options: LocalOptions,
        log: Option<Arc<SharedLog>>,
    },
    Ready(WorkerDisk),
}

/// One shard's view of the shared log: which shard, and what its two
/// newest durable checkpoints cover (the log keeps every record after the
/// older one).
struct LogShard {
    log: Arc<SharedLog>,
    /// The shard's directory (its checkpoint files).
    dir: PathBuf,
    /// Checkpoints sent to the disk and not yet reported, by LSN.
    pending: Vec<Lsn>,
    retained: Vec<Lsn>,
    /// Sections of an unfinished `RECOVER_LOG`, with the shard's last LSN.
    recovering: Option<(Vec<fusee::wal::RawSection>, Lsn)>,
}

impl LogShard {
    fn report(&self, shard: u32) -> io::Result<()> {
        match self.retained.iter().copied().min() {
            Some(min) => self.log.set_retain(shard, min),
            None => Ok(()),
        }
    }
}

/// What the two newest valid checkpoint files of a shard directory cover,
/// oldest first; `[0]` when it has none (the log keeps all its records).
pub(super) fn retained_checkpoints(dir: &std::path::Path) -> io::Result<Vec<Lsn>> {
    let mut files: Vec<PathBuf> = match std::fs::read_dir(dir) {
        Ok(d) => d
            .filter_map(|e| e.ok().map(|e| e.path()))
            .filter(|p| p.extension().is_some_and(|x| x == "fck"))
            .collect(),
        Err(e) if e.kind() == io::ErrorKind::NotFound => Vec::new(),
        Err(e) => return Err(e),
    };
    files.sort();
    let mut out = Vec::new();
    for path in files.iter().rev() {
        if out.len() == 2 {
            break;
        }
        if let Ok(cp) = fusee_core::store::Checkpoint::decode(&std::fs::read(path)?) {
            out.push(cp.lsn);
        }
    }
    out.reverse();
    if out.is_empty() {
        out.push(0);
    }
    Ok(out)
}

/// The shard threads of one lease.
#[derive(Default)]
pub(super) struct Shards {
    threads: Mutex<Vec<(Arc<Inbox>, oneshot::Receiver<()>)>>,
    /// Set when any of them ends, including by a panic.
    exited: Arc<AtomicBool>,
    /// Shards of the shared log granted to this lease: `(shard, epoch)`.
    granted: Mutex<Vec<(u32, u64)>>,
    /// The lease's shared memory, when its worker uses it.
    pub(super) shm: Mutex<Option<Arc<super::ring::ShmConn>>>,
}

/// Marks a shard thread's end and wakes the connection task.
struct Exit {
    exited: Arc<AtomicBool>,
    outbox: Arc<Outbox>,
}

impl Drop for Exit {
    fn drop(&mut self) {
        self.exited.store(true, Ordering::Release);
        self.outbox.queued.notify_one();
    }
}

impl Shards {
    /// Starts one shard's thread. Resolves once its storage is open, with
    /// the first segment's size, so the lease can be granted.
    pub(super) fn start(
        &self,
        server: Arc<Server>,
        shard: u32,
        epoch: u64,
        disk: Disk,
        outbox: Arc<Outbox>,
        drained: Arc<tokio::sync::Notify>,
    ) -> io::Result<(Arc<Inbox>, oneshot::Receiver<io::Result<u64>>)> {
        let inbox = Arc::new(Inbox::new(drained));
        let (opened, ready) = oneshot::channel();
        let (done, finished) = oneshot::channel();
        let thread_inbox = inbox.clone();
        let exit = Exit {
            exited: self.exited.clone(),
            outbox: outbox.clone(),
        };
        std::thread::Builder::new()
            .name(format!("fusee-server-shard-{shard}"))
            .spawn(move || {
                let _exit = exit;
                let mut log = None;
                let mut disk = match disk {
                    Disk::Ready(d) => d,
                    Disk::Open {
                        dir,
                        options,
                        log: shared,
                    } => {
                        log = shared.map(|log| LogShard {
                            log,
                            dir: dir.clone(),
                            pending: Vec::new(),
                            retained: Vec::new(),
                            recovering: None,
                        });
                        match LocalEnv::open(&dir, options) {
                            Ok(env) => WorkerDisk::Local(Box::new(env)),
                            Err(e) => {
                                let _ = opened.send(Err(e));
                                return;
                            }
                        }
                    }
                };
                let wake = thread_inbox.clone();
                disk.set_notify(Arc::new(move || wake.wake()));
                let _ = opened.send(Ok(disk.first_segment_bytes(u64::MAX)));
                let state = ShardState {
                    server,
                    shard,
                    epoch,
                    disk,
                    log,
                    inflight: None,
                    ckpt: None,
                    stopped: None,
                };
                state.run(&thread_inbox, &outbox);
                // The storage is closed by now; the next lease may open it.
                let _ = done.send(());
            })?;
        self.threads
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .push((inbox.clone(), finished));
        Ok((inbox, ready))
    }

    pub(super) fn granted(&self, shard: u32, epoch: u64) {
        self.granted
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .push((shard, epoch));
    }

    pub(super) fn take_granted(&self) -> Vec<(u32, u64)> {
        std::mem::take(&mut *self.granted.lock().unwrap_or_else(|e| e.into_inner()))
    }

    /// Whether a shard thread has ended. While the lease lasts, that is a
    /// failure: the shard's requests would go unanswered.
    pub(super) fn exited(&self) -> bool {
        self.exited.load(Ordering::Acquire)
    }

    /// Stops every shard thread and waits until each has closed its storage.
    /// Requests still queued are dropped unanswered.
    pub(super) async fn stop(&self) {
        let threads = std::mem::take(&mut *self.threads.lock().unwrap_or_else(|e| e.into_inner()));
        for (inbox, _) in &threads {
            inbox.close();
        }
        for (_, finished) in threads {
            let _ = finished.await;
        }
    }
}

struct ShardState {
    server: Arc<Server>,
    shard: u32,
    epoch: u64,
    disk: WorkerDisk,
    /// Log format 3: the shard's records go to the shared log.
    log: Option<LogShard>,
    /// The request whose flush runs in the background.
    inflight: Option<u64>,
    /// The request whose checkpoint runs in the background.
    ckpt: Option<u64>,
    /// Set once a run-store append failed: the shard accepts no further
    /// writes in this lease, since a later record could reference the
    /// missing frame.
    stopped: Option<String>,
}

impl ShardState {
    fn run(mut self, inbox: &Inbox, outbox: &Outbox) {
        let mut out = Vec::new();
        let mut gap = Duration::ZERO;
        loop {
            let (jobs, closed) = inbox.take();
            if closed {
                return;
            }
            for (kind, body) in jobs {
                // A shared-log write may publish the shard's position from
                // inside `handle`: every earlier reply leaves first, so the
                // worker checks each run-store append before it sees a
                // flush that follows it.
                if kind == req::APPEND && !out.is_empty() {
                    outbox.send(&out);
                    out.clear();
                }
                self.handle(kind, &body, &mut out);
                if self.stopped.is_some() {
                    inbox.stopped.store(true, Ordering::Release);
                }
                inbox.handled(body.len());
                // A flush acknowledgment is what the worker is waiting for.
                if kind == req::SUBMIT {
                    outbox.send(&out);
                    out.clear();
                }
            }
            self.poll(&mut out);
            outbox.send(&out);
            out.clear();
            inbox.done();
            // Background work notifies when it ends; poll as well while some
            // is outstanding, as storage may also fail without a word.
            let nap = if self.inflight.is_some() || self.ckpt.is_some() {
                Duration::from_millis(50)
            } else {
                Duration::from_secs(3600)
            };
            let idle = Instant::now();
            inbox.signal.wait(spin::budget(gap, SPIN_CAP), nap, None);
            gap = idle.elapsed();
        }
    }

    /// Reports background flushes and checkpoints that finished.
    fn poll(&mut self, out: &mut Vec<u8>) {
        if let Some(res) = self.disk.poll_flushed() {
            let id = self.inflight.take().unwrap_or(0);
            let mut b = Vec::new();
            put_u64(&mut b, id);
            put_u32(&mut b, self.shard);
            match res {
                Ok(lsn) => {
                    put_u64(&mut b, lsn);
                    frame::encode(resp::FLUSHED, &b, out);
                }
                Err(e) => {
                    put_bytes(&mut b, e.to_string().as_bytes());
                    frame::encode(resp::IO_FAILED, &b, out);
                }
            }
        }
        if let Some(mut res) = self.disk.poll_checkpoint() {
            let id = self.ckpt.take().unwrap_or(0);
            // Log format 3: the log keeps every record after the older of
            // the shard's two newest checkpoints.
            if let (Ok(lsn), Some(log)) = (&res, &mut self.log)
                && let Some(i) = log.pending.iter().position(|l| l == lsn)
            {
                log.pending.remove(i);
                log.retained.push(*lsn);
                if log.retained.len() > 2 {
                    log.retained.remove(0);
                }
                if let Err(e) = log.report(self.shard) {
                    res = Err(e);
                }
            }
            let mut b = Vec::new();
            put_u64(&mut b, id);
            put_u32(&mut b, self.shard);
            match res {
                Ok(lsn) => {
                    put_u64(&mut b, lsn);
                    frame::encode(resp::CHECKPOINTED, &b, out);
                }
                Err(e) => {
                    put_bytes(&mut b, e.to_string().as_bytes());
                    frame::encode(resp::IO_FAILED, &b, out);
                }
            }
        }
    }

    fn handle(&mut self, kind: u8, body: &[u8], out: &mut Vec<u8>) {
        let mut r = Reader::new(body);
        let Ok(h) = proto::read_head(&mut r) else {
            return;
        };
        let reply = |out: &mut Vec<u8>, res: io::Result<Vec<u8>>| {
            let mut b = Vec::new();
            put_u64(&mut b, h.id);
            match res {
                Ok(data) => {
                    b.extend_from_slice(&data);
                    frame::encode(resp::OK, &b, out);
                }
                Err(e) => {
                    put_bytes(&mut b, e.to_string().as_bytes());
                    frame::encode(resp::ERR, &b, out);
                }
            }
        };
        if h.epoch != self.epoch
            || h.shard != self.shard
            || h.epoch != self.server.shard_epoch(self.shard)
        {
            reply(
                out,
                Err(io::Error::other(
                    "lease epoch is stale: another worker holds the shards",
                )),
            );
            return;
        }
        let env = &mut self.disk;
        let bad = |e: fusee_core::wire::DecodeError| io::Error::other(e.to_string());
        // Asynchronous writes report failure as `IO_FAILED`, which stops the
        // worker's shard; any later acknowledgment for it becomes an error.
        let io_failed = |out: &mut Vec<u8>, message: &str| {
            let mut b = Vec::new();
            put_u64(&mut b, h.id);
            put_u32(&mut b, h.shard);
            put_bytes(&mut b, message.as_bytes());
            frame::encode(resp::IO_FAILED, &b, out);
        };
        if let Some(reason) = &self.stopped {
            match kind {
                req::SUBMIT
                | req::APPEND
                | req::STORE_APPEND
                | req::STORE_APPEND_AT
                | req::CHECKPOINT => {
                    io_failed(out, reason);
                    return;
                }
                req::COMPACT_STORE | req::SYNC_RECOVERED | req::DISCARD | req::OPEN_LOG => {
                    reply(out, Err(io::Error::other(reason.clone())));
                    return;
                }
                _ => {}
            }
        }
        if let Some(log) = &mut self.log {
            // Log format 3: the shard's records live in the shared log.
            match kind {
                req::APPEND => {
                    let res = proto::decode_section(&mut r)
                        .map_err(bad)
                        .and_then(|batch| log.log.append(self.shard, self.epoch, batch, true));
                    if let Err(e) = res {
                        let message = format!("shard {}: {e}", self.shard);
                        io_failed(out, &message);
                        self.stopped = Some(message);
                    }
                    return;
                }
                req::RECOVER_LOG => {
                    let res = (|| -> io::Result<Vec<u8>> {
                        let after = r.u64().map_err(bad)?;
                        let start = r.u32().map_err(bad)? as usize;
                        if start == 0 && log.recovering.is_none() {
                            log.recovering = Some(log.log.recover(self.shard, self.epoch, after)?);
                        }
                        let Some((sections, last)) = &log.recovering else {
                            return Err(io::Error::other("no log recovery in progress"));
                        };
                        let total = sections.len();
                        let mut end = start.min(total);
                        let mut bytes = 0;
                        while end < total && (end == start || bytes < proto::RECOVERY_PAGE) {
                            bytes += sections[end].2.len() + 16;
                            end += 1;
                        }
                        let mut b = Vec::with_capacity(bytes + 32);
                        put_u64(&mut b, *last);
                        put_u32(&mut b, total as u32);
                        put_u32(&mut b, (end - start) as u32);
                        proto::encode_sections(&mut b, &sections[start..end]);
                        if end == total {
                            log.recovering = None;
                        }
                        Ok(b)
                    })();
                    reply(out, res);
                    return;
                }
                req::SUBMIT => {
                    reply(
                        out,
                        Err(io::Error::other(
                            "this server keeps one shared log: send sections, not segment batches",
                        )),
                    );
                    return;
                }
                req::LOAD_CHECKPOINT => {
                    let res = (|| -> io::Result<Vec<u8>> {
                        let cp = env.load_checkpoint()?;
                        log.retained = retained_checkpoints(&log.dir)?;
                        if cp.is_none() {
                            log.retained = vec![0];
                        }
                        log.report(self.shard)?;
                        let mut b = Vec::new();
                        match cp {
                            Some(c) => {
                                b.push(1);
                                put_bytes(&mut b, &c.encode());
                            }
                            None => b.push(0),
                        }
                        Ok(b)
                    })();
                    reply(out, res);
                    return;
                }
                req::CHECKPOINT => {
                    let res = (|| -> io::Result<()> {
                        let cp = fusee_core::store::Checkpoint::decode(r.blob().map_err(bad)?)
                            .map_err(bad)?;
                        // Segments of the shared log are retired by what
                        // every shard's retained checkpoints cover.
                        log.pending.push(cp.lsn);
                        self.ckpt = Some(h.id);
                        env.checkpoint(cp, None);
                        Ok(())
                    })();
                    if let Err(e) = res {
                        reply(out, Err(e));
                    }
                    return;
                }
                req::SEGMENTS => {
                    reply(out, Ok(vec![0]));
                    return;
                }
                req::SYNC_RECOVERED | req::DISCARD | req::OPEN_LOG => {
                    reply(out, Ok(Vec::new()));
                    return;
                }
                req::READ_SEGMENT | req::SEGMENT_EXTENT | req::SEGMENT_RANGE => {
                    reply(
                        out,
                        Err(io::Error::other("a shared log has no per-shard segments")),
                    );
                    return;
                }
                _ => {}
            }
        }
        match kind {
            req::SUBMIT => match proto::decode_batch(&mut r) {
                Ok(batch) => {
                    // On a fast disk, flush on this thread: two handoffs
                    // fewer per step. On a slow one, keep this thread free
                    // for the shard's reads and use the flusher thread.
                    if env.supports_inline() && env.last_flush_us() <= 200 {
                        let mut b = Vec::new();
                        put_u64(&mut b, h.id);
                        put_u32(&mut b, h.shard);
                        match env.flush_inline(batch) {
                            Ok(lsn) => {
                                put_u64(&mut b, lsn);
                                frame::encode(resp::FLUSHED, &b, out);
                            }
                            Err(e) => {
                                put_bytes(&mut b, e.to_string().as_bytes());
                                frame::encode(resp::IO_FAILED, &b, out);
                            }
                        }
                    } else {
                        self.inflight = Some(h.id);
                        env.submit(batch);
                    }
                }
                Err(e) => reply(out, Err(bad(e))),
            },
            req::STORE_APPEND | req::STORE_APPEND_AT => {
                let mismatch = |expected: u64, actual: u64| {
                    io::Error::other(format!(
                        "run-store append offset mismatch on shard {}: the worker expected offset {expected}, the server's store is at {actual}; the shard accepts no more writes in this lease",
                        h.shard
                    ))
                };
                let res = (|| {
                    let expected = match kind {
                        req::STORE_APPEND_AT => Some(r.u64().map_err(bad)?),
                        _ => None,
                    };
                    let frame = r.blob().map_err(bad)?;
                    // Never write a frame where the worker's reference
                    // would not find it.
                    if let Some(expected) = expected
                        && env.store_len() != expected
                    {
                        return Err(mismatch(expected, env.store_len()));
                    }
                    let offset = if r.is_empty() {
                        env.store_append(frame)?
                    } else {
                        let run = r.u64().map_err(bad)?;
                        let lsn = r.u64().map_err(bad)?;
                        env.store_append_history(frame, run, lsn)?
                    };
                    match expected {
                        Some(expected) if expected != offset => Err(mismatch(expected, offset)),
                        _ => Ok(offset),
                    }
                })();
                match res {
                    // Verified appends report the offset written; the
                    // original kind reports only failures.
                    Ok(offset) if kind == req::STORE_APPEND_AT => {
                        let mut b = Vec::with_capacity(20);
                        put_u64(&mut b, h.id);
                        put_u32(&mut b, h.shard);
                        put_u64(&mut b, offset);
                        frame::encode(resp::STORE_APPENDED, &b, out);
                    }
                    Ok(_) => {}
                    Err(e) => {
                        let message = e.to_string();
                        io_failed(out, &message);
                        self.stopped = Some(message);
                    }
                }
            }
            req::STORE_READ => {
                let res = (|| -> io::Result<Vec<u8>> {
                    let off = r.u64().map_err(bad)?;
                    let len = r.u32().map_err(bad)?;
                    let data = env.store_read(off, len)?;
                    let mut b = Vec::new();
                    put_bytes(&mut b, &data);
                    Ok(b)
                })();
                reply(out, res);
            }
            req::CHECKPOINT => {
                let res = (|| -> io::Result<()> {
                    let cp = fusee_core::store::Checkpoint::decode(r.blob().map_err(bad)?)
                        .map_err(bad)?;
                    let del = r.u64().map_err(bad)?;
                    self.ckpt = Some(h.id);
                    env.checkpoint(cp, (del > 0).then_some(del));
                    Ok(())
                })();
                if let Err(e) = res {
                    reply(out, Err(e));
                }
            }
            req::LOAD_CHECKPOINT => {
                let res = env.load_checkpoint().map(|cp| {
                    let mut b = Vec::new();
                    match cp {
                        Some(c) => {
                            b.push(1);
                            put_bytes(&mut b, &c.encode());
                        }
                        None => b.push(0),
                    }
                    b
                });
                reply(out, res);
            }
            req::LOAD_STORE => {
                let res = (|| -> io::Result<Vec<u8>> {
                    let generation = r.u64().map_err(bad)?;
                    let len = r.u64().map_err(bad)?;
                    let mut b = Vec::new();
                    if !r.is_empty() && r.u8().map_err(bad)? == 1 {
                        env.files().open_store_prefix(generation, len)?;
                        put_u64(&mut b, u64::MAX);
                        put_u64(&mut b, env.store_generation());
                        put_u64(&mut b, env.store_len());
                        b.push(7); // paged compaction, history archival and visibility index
                        return Ok(b);
                    }
                    let data = env.load_store(generation, len)?;
                    put_u64(&mut b, env.store_generation());
                    put_bytes(&mut b, &data);
                    Ok(b)
                })();
                reply(out, res);
            }
            req::COMPACT_STORE => {
                let res = (|| -> io::Result<Vec<u8>> {
                    let mut result = Vec::new();
                    match r.u8().map_err(bad)? {
                        0 => env.files().begin_store_rewrite()?,
                        1 if r.rest().len() <= 4096 * 20 => {
                            while !r.is_empty() {
                                let run = r.u64().map_err(bad)?;
                                let offset = r.u64().map_err(bad)?;
                                let len = r.u32().map_err(bad)?;
                                env.files().copy_store_frame(
                                    run,
                                    fusee_core::shard::BlobRef { offset, len },
                                )?;
                            }
                        }
                        2 => {
                            env.files().finish_store_rewrite()?;
                            put_u64(&mut result, env.store_generation());
                            put_u64(&mut result, env.store_len());
                        }
                        _ => return Err(io::Error::other("bad run-store compaction frame")),
                    }
                    Ok(result)
                })();
                reply(out, res);
            }
            req::VISIBILITY => {
                let res = (|| -> io::Result<Vec<u8>> {
                    let filter = serde_json::from_slice(r.bytes().map_err(bad)?)?;
                    let limit = r.u32().map_err(bad)?.min(4096) as usize;
                    serde_json::to_vec(&env.visibility_candidates(&filter, limit))
                        .map_err(io::Error::other)
                })();
                reply(out, res);
            }
            req::SEGMENTS => {
                let res = env.segments().map(|segs| {
                    let mut b = Vec::new();
                    put_varint(&mut b, segs.len() as u64);
                    for s in segs {
                        put_varint(&mut b, s);
                    }
                    b
                });
                reply(out, res);
            }
            req::SEGMENT_EXTENT => {
                let res = r
                    .u64()
                    .map_err(bad)
                    .and_then(|id| env.files().recovery_segment_extent(id))
                    .map(|(length, prefix)| {
                        let mut b = Vec::with_capacity(16);
                        put_u64(&mut b, length);
                        put_u64(&mut b, prefix);
                        b
                    });
                reply(out, res);
            }
            req::SEGMENT_RANGE => {
                let res = (|| -> io::Result<Vec<u8>> {
                    let id = r.u64().map_err(bad)?;
                    let offset = r.u64().map_err(bad)?;
                    let size = r.u32().map_err(bad)? as usize;
                    if size > proto::RECOVERY_PAGE {
                        return Err(io::Error::other("recovery page exceeds bound"));
                    }
                    env.files().read_recovery_range(id, offset, size)
                })();
                reply(out, res);
            }
            req::READ_SEGMENT => {
                let res = r
                    .u64()
                    .map_err(bad)
                    .and_then(|s| env.read_segment(s))
                    .map(|data| {
                        let mut b = Vec::new();
                        put_bytes(&mut b, &data);
                        b
                    });
                reply(out, res);
            }
            req::SYNC_RECOVERED => reply(out, env.sync_recovered().map(|()| Vec::new())),
            req::DISCARD => reply(
                out,
                r.u64()
                    .map_err(bad)
                    .and_then(|s| env.discard_segment(s))
                    .map(|()| Vec::new()),
            ),
            req::OPEN_LOG => reply(
                out,
                r.u64()
                    .map_err(bad)
                    .and_then(|s| env.open_log(s))
                    .map(|()| Vec::new()),
            ),
            _ => reply(out, Err(io::Error::other("unknown request"))),
        }
    }
}