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
//! Lease decisions are ordered with workflow batches in the same VSR log.
//! A session starts at its first control op. This gives it a unique persistent
//! identity even when the same process or member becomes primary twice.
use super::{Proposal, Shared, Slot, other};
use crate::limits::{Grant, LEASE_MS, LimitWire, RemoteLimits, ServerLimits, op};
use fusee::limits::{Acquire, LimitBackend, LimitSpec, Priority};
use fusee_core::record::Payload;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::io;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock, Weak};
use std::task::Waker;

// The first varint of an engine batch is its positive first LSN.
const PREFIX: &[u8] = b"\0FSL\x01";

#[derive(Serialize, Deserialize)]
pub(super) struct Command {
    operation: u8,
    name: String,
    ticket: u64,
    concurrent: u32,
    per_second: f64,
    /// An acquire's place in the wait queue; absent for arrival order.
    #[serde(default, skip_serializing_if = "Priority::is_default")]
    priority: Priority,
}

pub(super) enum Request {
    Embedded(Command),
    Claim,
    Probe,
    Worker(u64, Command),
}

impl Command {
    pub(super) fn new(
        operation: u8,
        name: String,
        ticket: u64,
        concurrent: u32,
        per_second: f64,
    ) -> Self {
        Self {
            operation,
            name,
            ticket,
            concurrent,
            per_second,
            priority: Priority::default(),
        }
    }

    pub(super) fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = priority;
        self
    }

    pub(super) fn renew() -> Self {
        Self::new(op::RENEW, String::new(), 0, 0, 0.0)
    }
}

#[derive(Serialize, Deserialize)]
pub(super) struct Record {
    session: u64,
    time: u64,
    command: Command,
}

pub(super) fn is_control(body: &[u8]) -> bool {
    body.first() == Some(&0)
}

pub(super) fn encode(command: Command, session: u64, time: u64) -> io::Result<Payload> {
    let mut bytes = PREFIX.to_vec();
    serde_json::to_writer(
        &mut bytes,
        &Record {
            session,
            time,
            command,
        },
    )?;
    Ok(Arc::from(bytes))
}

#[derive(Default)]
pub(super) struct Ledger {
    table: ServerLimits,
    session: u64,
    pub(super) time: u64,
}

impl Ledger {
    pub(super) fn snapshot(&self) -> io::Result<Vec<u8>> {
        Ok(serde_json::to_vec(&(
            self.session,
            self.time,
            self.table.snapshot()?,
        ))?)
    }

    pub(super) fn restore(bytes: &[u8]) -> io::Result<Self> {
        let (session, time, limits) = serde_json::from_slice(bytes)?;
        Ok(Self {
            session,
            time,
            table: ServerLimits::from_snapshot(limits)?,
        })
    }

    pub(super) fn apply(&mut self, body: &[u8]) -> io::Result<u64> {
        let record: Record = serde_json::from_slice(
            body.strip_prefix(PREFIX)
                .ok_or_else(|| other("unknown replicated control record"))?,
        )?;
        if record.session < self.session {
            return Err(other("lease session moved backwards"));
        }
        if record.session != self.session {
            self.table.disconnected(self.session);
            self.session = record.session;
        }
        // Logical time advances only by observed elapsed time on one primary.
        // A new primary starts from the last committed time. Old grants can
        // therefore live longer during failover, but can never expire early.
        self.time = self.time.max(record.time);
        let c = record.command;
        match c.operation {
            op::DEFINE => self.table.define(
                &c.name,
                LimitSpec {
                    concurrent: c.concurrent,
                    per_second: c.per_second,
                },
            ),
            op::ACQUIRE => {
                use fusee::limits::answer;
                let grant = self.table.acquire(
                    &c.name,
                    self.session,
                    c.ticket,
                    &c.priority,
                    self.time,
                    LEASE_MS,
                );
                return Ok(match grant {
                    // Another ticket of the same client: within 2^40.
                    Grant::Granted(t) => answer::granted(c.ticket, t),
                    Grant::Queued => answer::QUEUED,
                    Grant::RetryAt(at) => answer::retry_after(at.saturating_sub(self.time)),
                });
            }
            op::RELEASE => self.table.release(&c.name, self.session, c.ticket),
            op::RENEW => self.table.renew(self.session, self.time, LEASE_MS),
            _ => return Err(other("unknown replicated lease operation")),
        }
        Ok(0)
    }
}

struct Wire {
    shared: Arc<Shared>,
    /// The primary epoch the engine runs in (0 until a standby engine's
    /// shards are promoted).
    epoch: Arc<AtomicU64>,
    client: OnceLock<Weak<RemoteLimits>>,
    acquiring: Arc<Mutex<HashSet<u64>>>,
}

impl std::fmt::Debug for Wire {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ReplicaLeaseWire")
            .field("epoch", &self.epoch)
            .finish()
    }
}

impl LimitWire for Wire {
    fn send_limit(
        &self,
        operation: u8,
        name: &str,
        ticket: u64,
        concurrent: u32,
        per_second: f64,
        priority: &Priority,
    ) -> io::Result<()> {
        let epoch = self.epoch.load(Ordering::Acquire);
        if epoch != self.shared.epoch.load(Ordering::Acquire) {
            return Err(other("worker lost its primary role"));
        }
        let mut slot = Slot::default();
        if operation == op::ACQUIRE {
            let acquiring = self.acquiring.clone();
            if !acquiring
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .insert(ticket)
            {
                return Ok(());
            }
            let client = self.client.get().cloned().unwrap_or_default();
            slot.callback = Some(Box::new(move |result| {
                acquiring
                    .lock()
                    .unwrap_or_else(|e| e.into_inner())
                    .remove(&ticket);
                if let Ok(answer) = result
                    && let Some(client) = client.upgrade()
                {
                    use fusee::limits::answer::{Answer, decode};
                    match decode(ticket, answer) {
                        Answer::Granted(t) => client.answered(t, true, 0),
                        Answer::RetryAfter(d) => client.answered(
                            ticket,
                            false,
                            fusee::local::unix_ms().saturating_add(d),
                        ),
                        Answer::Queued => client.answered(ticket, false, 0),
                    }
                }
            }));
        }
        let proposal = Proposal {
            queued: self.shared.traffic.timings.start(),
            body: Arc::from([]),
            last_lsn: 0,
            epoch,
            slot: Arc::new(Mutex::new(slot)),
            control: Some(Request::Embedded(
                Command::new(operation, name.into(), ticket, concurrent, per_second)
                    .with_priority(priority.clone()),
            )),
        };
        if self.shared.proposals.send(proposal).is_err() {
            self.acquiring
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .remove(&ticket);
            return Err(other("replica stopped"));
        }
        Ok(())
    }
}

/// Ordinary workflows allocate no lease worker and produce no control records.
pub(super) struct Backend {
    shared: Arc<Shared>,
    /// The primary epoch the engine runs in; 0 for a standby engine not yet
    /// promoted (its shards, or this backend, set it once the member is the
    /// ready primary).
    epoch: Arc<AtomicU64>,
    client: OnceLock<Arc<RemoteLimits>>,
    /// Limits a standby engine defined before its promotion, sent first
    /// once it runs.
    pending: Mutex<Vec<(String, LimitSpec)>>,
}

impl std::fmt::Debug for Backend {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ReplicatedLimits")
            .field("epoch", &self.epoch)
            .finish()
    }
}

impl Backend {
    pub(super) fn new(shared: Arc<Shared>, epoch: u64) -> Self {
        Self::standby(shared, Arc::new(AtomicU64::new(epoch)))
    }

    /// A standby engine's: its epoch is set when its shards are promoted.
    pub(super) fn standby(shared: Arc<Shared>, epoch: Arc<AtomicU64>) -> Self {
        Self {
            shared,
            epoch,
            client: OnceLock::new(),
            pending: Mutex::new(Vec::new()),
        }
    }

    /// Whether the engine's epoch is known: a standby engine's is set here
    /// if this member is now the ready primary (the shards agree on it, or
    /// fail). Then sends the limits defined while it stood by.
    fn running(&self) -> bool {
        if self.epoch.load(Ordering::Acquire) == 0 {
            if !self.shared.status.borrow().ready {
                return false;
            }
            let now = self.shared.epoch.load(Ordering::Acquire);
            let _ = self
                .epoch
                .compare_exchange(0, now, Ordering::AcqRel, Ordering::Acquire);
        }
        // Held while sending, so no acquire overtakes a definition.
        let mut pending = self.pending.lock().unwrap_or_else(|e| e.into_inner());
        for (name, spec) in pending.drain(..) {
            self.client().define(&name, spec);
        }
        true
    }
    fn client(&self) -> &Arc<RemoteLimits> {
        self.client.get_or_init(|| {
            let wire = Arc::new(Wire {
                shared: self.shared.clone(),
                epoch: self.epoch.clone(),
                client: OnceLock::new(),
                acquiring: Arc::default(),
            });
            let client = RemoteLimits::new(wire.clone(), 0);
            let _ = wire.client.set(Arc::downgrade(&client));
            client
        })
    }
}
impl LimitBackend for Backend {
    fn define(&self, name: &str, spec: LimitSpec) {
        if !self.running() {
            self.pending
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .push((name.to_owned(), spec));
            return;
        }
        self.client().define(name, spec);
    }
    fn ticket(&self) -> u64 {
        self.shared.next_ticket.fetch_add(1, Ordering::Relaxed)
    }
    fn acquire_in(
        &self,
        name: &str,
        ticket: u64,
        priority: &Priority,
        now: u64,
        waker: Option<&Waker>,
    ) -> Acquire {
        if !self.running()
            || self.epoch.load(Ordering::Acquire) != self.shared.epoch.load(Ordering::Acquire)
        {
            return Acquire::Wait { retry_at_ms: None };
        }
        self.client().acquire_in(name, ticket, priority, now, waker)
    }
    fn release(&self, name: &str, ticket: u64) {
        if let Some(client) = self.client.get() {
            client.release(name, ticket);
        }
    }
    fn in_use(&self, name: &str) -> u32 {
        self.client.get().map_or(0, |c| c.in_use(name))
    }
}

#[cfg(test)]
mod snapshot_tests {
    use super::*;
    #[test]
    fn checkpoint_preserves_lease_sessions_rate_balances_and_waiter_order() {
        let mut ledger = Ledger::default();
        let command = |op, ticket, session, time| {
            encode(
                Command::new(op, "api".into(), ticket, 1, 4.0),
                session,
                time,
            )
            .unwrap()
        };
        ledger.apply(&command(op::DEFINE, 0, 4, 100)).unwrap();
        assert_eq!(ledger.apply(&command(op::ACQUIRE, 2, 4, 100)).unwrap(), 1);
        assert_eq!(ledger.apply(&command(op::ACQUIRE, 3, 4, 150)).unwrap(), 0);
        assert_eq!(ledger.apply(&command(op::ACQUIRE, 4, 4, 165)).unwrap(), 0);
        let saved = ledger.snapshot().unwrap();
        let mut copy = Ledger::restore(&saved).unwrap();
        assert_eq!(saved, copy.snapshot().unwrap());
        for msg in [
            command(op::RELEASE, 2, 4, 170),
            command(op::ACQUIRE, 4, 4, 200),
            command(op::ACQUIRE, 3, 4, 200),
            command(op::RENEW, 0, 4, 1000),
            command(op::ACQUIRE, 1, 9, 1000),
            command(op::ACQUIRE, 1, 9, 4001),
        ] {
            assert_eq!(ledger.apply(&msg).unwrap(), copy.apply(&msg).unwrap());
        }
        assert_eq!(ledger.snapshot().unwrap(), copy.snapshot().unwrap());
    }

    #[test]
    fn priorities_replicate_and_grants_name_the_ticket_served() {
        use fusee::limits::answer::{Answer, decode};
        let mut ledger = Ledger::default();
        let at = |level| Priority {
            level,
            ..Priority::default()
        };
        let command = |op, ticket, p: Priority, time| {
            encode(
                Command::new(op, "one".into(), ticket, 1, 0.0).with_priority(p),
                4,
                time,
            )
            .unwrap()
        };
        ledger
            .apply(&command(op::DEFINE, 0, Priority::default(), 1))
            .unwrap();
        assert_eq!(
            ledger
                .apply(&command(op::ACQUIRE, 1, Priority::default(), 1))
                .unwrap(),
            1
        );
        assert_eq!(ledger.apply(&command(op::ACQUIRE, 2, at(0), 2)).unwrap(), 0);
        assert_eq!(ledger.apply(&command(op::ACQUIRE, 3, at(9), 3)).unwrap(), 0);
        // The checkpoint carries the queue with its levels.
        let saved = ledger.snapshot().unwrap();
        let mut copy = Ledger::restore(&saved).unwrap();
        assert_eq!(saved, copy.snapshot().unwrap());
        let mut answers = Vec::new();
        for msg in [
            command(op::RELEASE, 1, Priority::default(), 4),
            // Ticket 2 rings; ticket 3, queued later at a higher level, is
            // the one granted, and the answer names it.
            command(op::ACQUIRE, 2, at(0), 5),
            command(op::RELEASE, 3, Priority::default(), 6),
            command(op::ACQUIRE, 2, at(0), 7),
        ] {
            let a = ledger.apply(&msg).unwrap();
            assert_eq!(a, copy.apply(&msg).unwrap());
            answers.push(a);
        }
        assert_eq!(decode(2, answers[1]), Answer::Granted(3));
        assert_eq!(decode(2, answers[3]), Answer::Granted(2));
        assert_eq!(ledger.snapshot().unwrap(), copy.snapshot().unwrap());
    }
}