actordb 0.4.0

An actor-first distributed database
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
// Copyright (c) Sean Lawlor
//
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree.

//! Per-fragment dispatcher and supervision of priority worker pools.

use std::panic::AssertUnwindSafe;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;

use futures::FutureExt;
use ractor::{Actor, ActorProcessingErr, ActorRef, ActorStatus, SupervisionEvent};

use super::fragment_state::FragmentState;
use super::pool::{PoolConfig, WorkerPool};
#[cfg(test)]
use super::pool_worker::take_worker_startup_gate;
use super::pool_worker::{PoolWorker, PoolWorkerArgs};
use super::protocol::{ApplyError, BatchReadContinuation, KvMessage, OpPriority};
use crate::shard::FragmentId;
/// Arguments to spawn a [KvActor].
/// State, fragment identity and the unique worker prefix remain required;
/// omitted pool settings match [`crate::node::NodeConfig`] defaults.
#[derive(Clone, bon::Builder)]
pub struct KvArgs {
    /// The fragment's shared state, created by the supervisor (which
    /// also hands it to the cleaner and snapshotter).
    pub shared: Arc<FragmentState>,
    /// Owning node id, for unique worker naming.
    /// Which fragment this actor serves. Used for debug output.
    pub fragment: FragmentId,
    /// Prefix for worker actor names (must be unique per dispatcher
    /// in the process; the supervisor generates it).
    #[builder(into)]
    pub worker_prefix: String,
    /// Worker pool configs by priority. Keys are sticky-routed by
    /// `hash(key) % pool.workers` within the selected pool.
    #[builder(default = PoolConfig::high_priority_defaults())]
    pub high_pool: PoolConfig,
    #[builder(default = PoolConfig::default())]
    pub default_pool: PoolConfig,
    #[builder(default = PoolConfig::background_defaults())]
    pub background_pool: PoolConfig,
}

/// The per-fragment dispatcher. One runs per fragment this node serves:
/// it owns the [`FragmentState`] and an internal managed worker pool, and
/// routes every operation to a worker by sticky key hash. `GetAll` is served
/// directly from shared state.
pub struct KvActor;

/// The dispatcher's state: the worker pool and the state they share.
pub struct KvState {
    high_pool: WorkerPool,
    default_pool: WorkerPool,
    background_pool: WorkerPool,
    shared: Arc<FragmentState>,
    worker_prefix: String,
}

impl KvState {
    fn pool(&self, priority: OpPriority) -> &WorkerPool {
        match priority {
            OpPriority::High => &self.high_pool,
            OpPriority::Default => &self.default_pool,
            OpPriority::Background => &self.background_pool,
        }
    }
}

async fn route_batch_read(mut work: BatchReadContinuation, state: &KvState) {
    loop {
        if work.reply.is_closed() {
            return;
        }
        if work.is_complete()
            || (!work.results.is_empty() && work.dispatcher.get_status() >= ActorStatus::Draining)
        {
            work.finish();
            return;
        }
        let pool = state.pool(work.priority);
        let reservation = {
            let key = work
                .current_key()
                .expect("incomplete batch continuation must have a key");
            pool.try_reserve(key).await
        };
        let Some(reservation) = reservation else {
            work.push(Err("shed: pool at queue cap or rate limit".to_string()));
            continue;
        };
        reservation.send(KvMessage::ContinueBatchRead(work));
        return;
    }
}

impl Actor for KvActor {
    type Msg = KvMessage;
    type State = KvState;
    type Arguments = KvArgs;

    async fn pre_start(
        &self,
        myself: ActorRef<Self::Msg>,
        args: Self::Arguments,
    ) -> Result<Self::State, ActorProcessingErr> {
        // Workers are linked to the dispatcher: a dead worker raises a
        // supervision event here and gets replaced in-place.
        // Phase 7: three isolated pools by priority.
        let mut pools = Vec::with_capacity(3);
        let startup = AssertUnwindSafe(async {
            pools.push(
                WorkerPool::spawn_linked(
                    args.high_pool.clone(),
                    args.shared.clone(),
                    myself.get_cell(),
                    &format!("{}-high", args.worker_prefix),
                )
                .await?,
            );
            pools.push(
                WorkerPool::spawn_linked(
                    args.default_pool.clone(),
                    args.shared.clone(),
                    myself.get_cell(),
                    &format!("{}-default", args.worker_prefix),
                )
                .await?,
            );
            pools.push(
                WorkerPool::spawn_linked(
                    args.background_pool.clone(),
                    args.shared.clone(),
                    myself.get_cell(),
                    &format!("{}-bg", args.worker_prefix),
                )
                .await?,
            );
            Ok::<(), ActorProcessingErr>(())
        })
        .catch_unwind()
        .await;
        match startup {
            Ok(Ok(())) => {}
            Ok(Err(error)) => {
                for pool in &pools {
                    pool.begin_shutdown();
                }
                for pool in &pools {
                    pool.join().await;
                }
                return Err(error);
            }
            Err(panic) => {
                for pool in &pools {
                    pool.begin_shutdown();
                }
                for pool in &pools {
                    pool.join().await;
                }
                std::panic::resume_unwind(panic);
            }
        }
        let mut pools = pools.into_iter();
        let high_pool = pools.next().expect("high pool was started");
        let default_pool = pools.next().expect("default pool was started");
        let background_pool = pools.next().expect("background pool was started");
        Ok(KvState {
            high_pool,
            default_pool,
            background_pool,
            shared: args.shared,
            worker_prefix: args.worker_prefix.clone(),
        })
    }

    async fn post_stop(
        &self,
        _myself: ActorRef<Self::Msg>,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        // Standalone KvActor users still receive the actor's historical
        // flush-on-stop contract. A supervising fragment keeps another Arc to
        // the shared state and performs an observable second fence, so a
        // failed attempt here retains its batch for the supervisor to retry.
        state.high_pool.shutdown().await;
        state.default_pool.shutdown().await;
        state.background_pool.shutdown().await;
        state.shared.flush().await
    }

    async fn handle(
        &self,
        myself: ActorRef<Self::Msg>,
        message: Self::Msg,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        match message {
            KvMessage::ApplyOp {
                op,
                priority,
                is_replication,
                reply,
            } => {
                let pool = match priority {
                    OpPriority::High => &state.high_pool,
                    OpPriority::Default => &state.default_pool,
                    OpPriority::Background => &state.background_pool,
                };
                // Phase 7: admission control. Shed explicitly if the
                // pool is over its queue cap or rate limit — unless this
                // is a replicated op: the replication sender is already
                // bounded, and shedding here while the sender acks would
                // silently lose acknowledged writes (adversarial #2).
                // Bypassed ops still reserve their exact sticky worker so
                // completion, immediate send failure, or worker death can
                // release the same accounting exactly once.
                let reservation = if is_replication {
                    Some(pool.reserve_unchecked(&op.key))
                } else {
                    pool.try_reserve(&op.key).await
                };
                let Some(reservation) = reservation else {
                    // Shed: counted in pool.shed_count and returned to callers
                    // that requested an exact apply outcome.
                    if let Some(reply) = reply {
                        let _ = reply.send(Err(ApplyError::Backpressure));
                    }
                    return Ok(());
                };
                reservation.send(KvMessage::ApplyOp {
                    op,
                    priority,
                    is_replication,
                    reply,
                });
                Ok(())
            }
            KvMessage::Get {
                key,
                reply,
                priority,
            } => {
                // Reads sticky-route within their selected priority pool.
                // Completed writes are already visible in the shared cache,
                // regardless of which pool applied them. The worker replies
                // through the caller's channel directly; on worker
                // timeout/error it is dropped and the caller sees a
                // SenderError.
                let pool = match priority {
                    OpPriority::High => &state.high_pool,
                    OpPriority::Default => &state.default_pool,
                    OpPriority::Background => &state.background_pool,
                };
                let Some(reservation) = pool.try_reserve(&key).await else {
                    // Shed: explicit error, not "not found". The caller
                    // sees NodeError::Backpressure, distinct from Ok(None).
                    let _ = reply.send(Err("shed: pool at queue cap or rate limit".to_string()));
                    return Ok(());
                };
                // Hand the caller's reply port directly to the worker. The
                // dispatcher remains free to serve other pools, health, and
                // supervision while this read waits behind sticky-key work.
                reservation.send(KvMessage::Get {
                    key,
                    reply,
                    priority,
                });
                Ok(())
            }
            KvMessage::Scan { spec, reply } => {
                // Scans ride the background pool ("bulk/scan traffic") so
                // they can never starve latency-sensitive ops. Non-sticky
                // routing: a scan has no key, so spread concurrent scans
                // round-robin across background workers. Shed explicitly
                // under backpressure — never confused with "no matches".
                let pool = &state.background_pool;
                let Some(reservation) = pool.try_reserve_round_robin().await else {
                    let _ = reply.send(Err("shed: pool at queue cap or rate limit".to_string()));
                    return Ok(());
                };
                reservation.send(KvMessage::Scan { spec, reply });
                Ok(())
            }
            KvMessage::ScanRetained {
                spec,
                retention,
                reply,
            } => {
                if reply.is_closed() {
                    return Ok(());
                }
                let Some(reservation) = state.background_pool.try_reserve_round_robin().await
                else {
                    let _ = reply.send(Err("shed: pool at queue cap or rate limit".into()));
                    return Ok(());
                };
                reservation.send(KvMessage::ScanRetained {
                    spec,
                    retention,
                    reply,
                });
                Ok(())
            }
            KvMessage::QueryPage { work, reply } => {
                if reply.is_closed() {
                    return Ok(());
                }
                let Some(reservation) = state.background_pool.try_reserve_round_robin().await
                else {
                    let _ = reply.send(crate::query::page_error(
                        &work.request,
                        crate::proto::QueryPageStatus::Backpressure,
                        "background pool at queue cap or rate limit",
                    ));
                    return Ok(());
                };
                reservation.send(KvMessage::QueryPage { work, reply });
                Ok(())
            }
            KvMessage::GetBatch {
                keys,
                response_base_payload_len,
                response_soft_limit,
                reply,
                priority,
            } => {
                route_batch_read(
                    BatchReadContinuation::new(
                        keys,
                        response_base_payload_len,
                        response_soft_limit,
                        priority,
                        reply,
                        myself,
                    ),
                    state,
                )
                .await;
                Ok(())
            }
            KvMessage::ContinueBatchRead(work) => {
                route_batch_read(work, state).await;
                Ok(())
            }
            KvMessage::GetBatchRetained(request) => {
                route_batch_read(
                    BatchReadContinuation::new(
                        request.keys,
                        request.response_base_payload_len,
                        request.response_soft_limit,
                        request.priority,
                        request.reply,
                        myself,
                    )
                    .with_retention(request.retention),
                    state,
                )
                .await;
                Ok(())
            }
            KvMessage::Flush { reply } => {
                let result = state
                    .shared
                    .flush()
                    .await
                    .map(|()| state.shared.durability())
                    .map_err(|error| ApplyError::Storage(error.to_string()));
                let _ = reply.send(result);
                // A failed fence keeps its in-flight buffer for retry. Report
                // the error to the protocol caller without killing the stable
                // dispatcher reference.
                Ok(())
            }
            KvMessage::FlushStable { reply } => {
                let result = state
                    .shared
                    .flush_stable()
                    .await
                    .map_err(|error| ApplyError::Storage(error.to_string()));
                let _ = reply.send(result);
                // As with the ordinary flush fence, keep the stable dispatcher
                // reference alive so a backend failure can be retried.
                Ok(())
            }
            KvMessage::GetAll { reply } => {
                let _ = reply.send(state.shared.get_all().await);
                Ok(())
            }
            KvMessage::GetSnapshot { reply } => {
                let _ = reply.send(state.shared.snapshot_with_revision().await);
                Ok(())
            }
            KvMessage::GetSummaryPage {
                after_key,
                expected_revision,
                snapshot_time_ms,
                limit,
                reply,
            } => {
                let page = state
                    .shared
                    .summary_page(
                        after_key.as_deref(),
                        expected_revision,
                        snapshot_time_ms,
                        limit,
                    )
                    .await;
                let _ = reply.send(page);
                Ok(())
            }
            KvMessage::GetVersioned {
                key,
                snapshot_time_ms,
                reply,
            } => {
                let _ = reply.send(state.shared.get_versioned(&key, snapshot_time_ms).await);
                Ok(())
            }
            KvMessage::GetVersionedPage {
                keys,
                start_index,
                snapshot_time_ms,
                reply,
            } => {
                let _ = reply.send(
                    state
                        .shared
                        .get_versioned_page(&keys, start_index, snapshot_time_ms)
                        .await,
                );
                Ok(())
            }
            KvMessage::GetRevision { reply } => {
                let _ = reply.send(state.shared.current_revision().await);
                Ok(())
            }
            KvMessage::GetShedCounts { reply } => {
                let counts = (
                    state.high_pool.shed_count(),
                    state.default_pool.shed_count(),
                    state.background_pool.shed_count(),
                );
                let _ = reply.send(counts);
                Ok(())
            }
        }
    }

    async fn handle_supervisor_evt(
        &self,
        myself: ActorRef<Self::Msg>,
        message: SupervisionEvent,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        // A dead worker is replaced in-place: the pool slot keeps its
        // index, so sticky routing is unaffected and no keys move.
        let dead = match &message {
            SupervisionEvent::ActorFailed(cell, _) => Some(cell.clone()),
            SupervisionEvent::ActorTerminated(cell, _, _) => Some(cell.clone()),
            _ => None,
        };
        if let Some(cell) = dead {
            // Check all three pools for the dead worker.
            let mut found = None;
            for (pool, suffix) in [
                (&mut state.high_pool, "high"),
                (&mut state.default_pool, "default"),
                (&mut state.background_pool, "bg"),
            ] {
                if let Some(slot) = pool.slot_of(&cell) {
                    found = Some((pool, slot, suffix));
                    break;
                }
            }
            if let Some((pool, slot, suffix)) = found {
                pool.reclaim_slot(slot);
                // Planned worker drains emit the same termination events as
                // failures. Retire any remaining accounting above, but do not
                // link a replacement to a draining parent.
                if myself.get_status() >= ActorStatus::Draining {
                    return Ok(());
                }
                let name = format!("{}-{suffix}-worker-{slot}", state.worker_prefix);
                let slot_pending = Arc::new(AtomicU64::new(0));
                #[cfg(test)]
                let startup_gate = take_worker_startup_gate(&name);
                let replacement = PoolWorker::spawn_linked(
                    Some(name),
                    PoolWorker,
                    PoolWorkerArgs {
                        shared: state.shared.clone(),
                        pool_pending: pool.pending.clone(),
                        slot_pending: slot_pending.clone(),
                        #[cfg(test)]
                        startup_gate,
                    },
                    myself.get_cell(),
                )
                .await;
                match replacement {
                    Ok((worker, _handle)) => pool.replace(slot, worker, slot_pending),
                    // `drain()` changes the parent status immediately. It can
                    // race the await above, causing ractor to reject the new
                    // link after the earlier status check. That is an orderly
                    // retirement, not a dispatcher failure: returning an
                    // error would skip `post_stop` and kill sibling workers
                    // before their accepted mailboxes drain.
                    Err(_) if myself.get_status() >= ActorStatus::Draining => return Ok(()),
                    Err(error) => return Err(ActorProcessingErr::from(error.to_string())),
                }
            }
        }
        Ok(())
    }
}