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
// 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.

//! Public low-level fragment worker and its request handlers.

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

use ractor::{Actor, ActorProcessingErr, ActorRef, RpcReplyPort};

use super::fragment_state::FragmentState;
use super::pool::release_pending;
use super::protocol::{ApplyError, BatchReadEntry, BatchReadPage, KvMessage};
use crate::wire::{batch_get_result_encoded_len, RepeatedFieldSizer};
/// A fragment's worker: applies ops and serves reads against the shared
/// [`FragmentState`]. One worker's mailbox is FIFO, and sticky dispatch
/// sends each key to one worker within a priority pool. Cross-pool LWW merges
/// are serialized by the shared state while different keys execute
/// concurrently.
pub struct KvWorker;

/// Arguments to spawn a [KvWorker].
#[derive(Clone, bon::Builder)]
pub struct KvWorkerArgs {
    /// The fragment state this worker shares with its siblings.
    pub shared: Arc<FragmentState>,
    /// Pending-op counter for the pool (decremented after each op).
    /// `None` for unsupervised test workers.
    pub pending: Option<Arc<AtomicU64>>,
}

impl Actor for KvWorker {
    type Msg = KvMessage;
    type State = (Arc<FragmentState>, Option<Arc<AtomicU64>>);
    type Arguments = KvWorkerArgs;

    async fn pre_start(
        &self,
        _myself: ActorRef<Self::Msg>,
        args: Self::Arguments,
    ) -> Result<Self::State, ActorProcessingErr> {
        Ok((args.shared, args.pending))
    }

    async fn handle(
        &self,
        _myself: ActorRef<Self::Msg>,
        message: Self::Msg,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        let result = handle_worker_message(&state.0, message).await;
        // Release the optional caller-owned pending slot. Production pools
        // use `PoolWorker`, which also tracks the exact worker slot so
        // supervision can reclaim queued work discarded by a failed worker.
        if let Some(pending) = &state.1 {
            release_pending(pending, 1);
        }
        result
    }
}

async fn serve_worker_batch(
    shared: &FragmentState,
    keys: Vec<String>,
    response_base_payload_len: usize,
    response_soft_limit: usize,
    reply: RpcReplyPort<BatchReadPage>,
) {
    let mut entries = Vec::with_capacity(keys.len().min(256));
    let mut response_sizing = RepeatedFieldSizer::new(17, 2, response_base_payload_len);
    for key in keys {
        let value = shared.get(&key).await;
        response_sizing.push(batch_get_result_encoded_len(&key, value.as_ref(), false));
        entries.push(BatchReadEntry {
            key,
            result: Ok(value),
        });
        if response_sizing.encoded_len() > response_soft_limit {
            break;
        }
    }
    let _ = reply.send(BatchReadPage { entries });
}

pub(super) async fn handle_worker_message(
    shared: &Arc<FragmentState>,
    message: KvMessage,
) -> Result<(), ActorProcessingErr> {
    match message {
        KvMessage::ApplyOp {
            op,
            priority: _,
            is_replication: _,
            reply,
        } => {
            // Cache merge and durable enqueue are one ordered operation.
            // A DB error fails the actor loudly rather than diverging
            // silently.
            match shared.apply(&op).await {
                Ok(outcome) => {
                    if let Some(reply) = reply {
                        let _ = reply.send(Ok(outcome));
                    }
                    Ok(())
                }
                Err(error) => {
                    let fatal = match &error {
                        ApplyError::Storage(message) => {
                            Some(ActorProcessingErr::from(message.clone()))
                        }
                        ApplyError::Backpressure | ApplyError::InvalidInput(_) => None,
                    };
                    if let Some(reply) = reply {
                        let _ = reply.send(Err(error));
                    }
                    match fatal {
                        Some(error) => Err(error),
                        None => Ok(()),
                    }
                }
            }
        }
        KvMessage::Get {
            key,
            reply,
            priority: _,
        } => {
            // The receiver may be gone (e.g. timed-out caller); that's fine.
            let _ = reply.send(Ok(shared.get(&key).await));
            Ok(())
        }
        KvMessage::Scan { spec, reply } => {
            // Worker-executed (unlike the dispatcher-served GetAll family
            // below): the scan runs against shared state inside the worker's
            // message handler. The receiver may be gone; that's fine.
            let _ = reply.send(Ok(shared.scan(&spec).await));
            Ok(())
        }
        KvMessage::ScanRetained {
            spec,
            retention,
            reply,
        } => {
            let _retention = retention;
            if !reply.is_closed() {
                let _ = reply.send(Ok(shared.scan(&spec).await));
            }
            Ok(())
        }
        KvMessage::QueryPage { work, reply } => {
            if !reply.is_closed() {
                let _retention = &work.retention;
                let _ = reply.send(shared.query_page(&work).await);
            }
            Ok(())
        }
        KvMessage::GetBatch {
            keys,
            response_base_payload_len,
            response_soft_limit,
            reply,
            priority: _,
        } => {
            // `KvWorker` remains a public low-level actor. It has no managed
            // pool admission, so serve the same ordered byte-bounded shape
            // directly. Production `KvActor` routes one admitted key at a
            // time through `ContinueBatchRead` below.
            serve_worker_batch(
                shared,
                keys,
                response_base_payload_len,
                response_soft_limit,
                reply,
            )
            .await;
            Ok(())
        }
        KvMessage::GetBatchRetained(request) => {
            let _retention = request.retention;
            serve_worker_batch(
                shared,
                request.keys,
                request.response_base_payload_len,
                request.response_soft_limit,
                request.reply,
            )
            .await;
            Ok(())
        }
        KvMessage::ContinueBatchRead(mut work) => {
            let Some(key) = work.current_key() else {
                work.finish();
                return Ok(());
            };
            let value = shared.get(key).await;
            work.push(Ok(value));
            work.return_to_dispatcher();
            Ok(())
        }
        // Flush fences are handled by the dispatcher against the shared
        // state. They are never routed through a worker pool.
        KvMessage::Flush { reply } | KvMessage::FlushStable { reply } => {
            let _ = reply.send(Err(ApplyError::Storage(
                "flush fence was routed to a worker".to_string(),
            )));
            Ok(())
        }
        // Snapshot queries are served by the dispatcher straight from
        // shared state.
        KvMessage::GetAll { .. }
        | KvMessage::GetSnapshot { .. }
        | KvMessage::GetSummaryPage { .. }
        | KvMessage::GetVersioned { .. }
        | KvMessage::GetVersionedPage { .. }
        | KvMessage::GetRevision { .. } => Ok(()),
        // Shed counts are a dispatcher-level query; workers don't track them.
        KvMessage::GetShedCounts { reply } => {
            let _ = reply.send((0, 0, 0));
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    // 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.

    //! Low-level worker APIs, retained reads, and input rejection.

    use super::*;
    use crate::actor::test_support::{test_op, test_state};
    use crate::actor::{ApplyOutcome, OpPriority, ReplicatedOp, RetainedBatchRead, VersionedValue};
    use ractor::rpc::CallResult;
    use std::collections::HashMap;
    use std::time::Duration;
    #[tokio::test]
    async fn worker_without_pending_counter_serves_operations() {
        let shared = Arc::new(test_state(HashMap::new()));
        let (worker, handle) = KvWorker::spawn(
            None,
            KvWorker,
            KvWorkerArgs::builder().shared(shared.clone()).build(),
        )
        .await
        .unwrap();
        let value = VersionedValue::new(b"built".to_vec(), 10, "writer".into());
        assert_eq!(
            worker
                .call(
                    |reply| KvMessage::ApplyOp {
                        op: ReplicatedOp {
                            key: "built".into(),
                            value: value.clone(),
                            seq: 1,
                            boot_id: 1
                        },
                        priority: OpPriority::Default,
                        is_replication: false,
                        reply: Some(reply),
                    },
                    Some(Duration::from_secs(2))
                )
                .await
                .unwrap(),
            CallResult::Success(Ok(ApplyOutcome::Applied))
        );
        assert_eq!(shared.get("built").await, Some(value));
        worker.drain().unwrap();
        handle.await.unwrap();
    }
    #[tokio::test]
    async fn direct_worker_batch_read_preserves_positions_and_exact_response_boundary() {
        use crate::proto::{BatchGetResponse, BatchKeyValue, Envelope, EnvelopeMsg};
        use prost::Message;

        let value = VersionedValue::new(vec![7; 128], 128, "n".to_string());
        let shared = Arc::new(test_state(HashMap::from([(
            "present".to_string(),
            value.clone(),
        )])));
        let (worker, handle) = KvWorker::spawn(
            None,
            KvWorker,
            KvWorkerArgs {
                shared,
                pending: None,
            },
        )
        .await
        .unwrap();

        // Measure with Prost, including the nonzero request id and nested
        // envelope, rather than the actor's incremental sizing helper.
        let response = BatchGetResponse {
            req_id: u64::MAX,
            results: Vec::new(),
        };
        let response_base_payload_len = response.encoded_len();
        let mut singleton = response;
        singleton.results.push(BatchKeyValue {
            key: "present".to_string(),
            found: true,
            value: value.value.clone(),
            timestamp_ms: value.timestamp_ms,
            node_id: value.node_id.clone(),
            ..BatchKeyValue::default()
        });
        let singleton_response_len = Envelope {
            msg: Some(EnvelopeMsg::BatchGetResponse(singleton)),
        }
        .encoded_len();
        let keys = vec![
            "present".to_string(),
            "m".to_string(),
            "present".to_string(),
            "m".to_string(),
        ];
        let read = |keys: Vec<String>, response_soft_limit| {
            worker.call(
                move |reply| KvMessage::GetBatch {
                    keys,
                    response_base_payload_len,
                    response_soft_limit,
                    reply,
                    priority: OpPriority::Background,
                },
                Some(Duration::from_secs(2)),
            )
        };

        let present = BatchReadEntry {
            key: "present".to_string(),
            result: Ok(Some(value)),
        };
        let missing = BatchReadEntry {
            key: "m".to_string(),
            result: Ok(None),
        };
        let CallResult::Success(below_boundary) = read(keys.clone(), singleton_response_len - 1)
            .await
            .unwrap()
        else {
            panic!("direct worker did not return the crossing singleton");
        };
        assert_eq!(below_boundary.entries, vec![present.clone()]);

        // Equality does not end the page: the next miss crosses the target
        // and is included once. The duplicate positions remain in the suffix.
        let CallResult::Success(first) = read(keys.clone(), singleton_response_len).await.unwrap()
        else {
            panic!("direct worker did not return the first prefix");
        };
        assert_eq!(first.entries, vec![present.clone(), missing.clone()]);
        let suffix = keys.into_iter().skip(first.entries.len()).collect();
        let CallResult::Success(second) = read(suffix, singleton_response_len).await.unwrap()
        else {
            panic!("direct worker did not return the resumed suffix");
        };
        assert_eq!(second.entries, vec![present.clone(), missing]);

        let retention = Arc::new(());
        let released = Arc::downgrade(&retention);
        let retained = worker
            .call(
                move |reply| {
                    KvMessage::GetBatchRetained(RetainedBatchRead::new(
                        vec!["present".to_string(), "m".to_string()],
                        response_base_payload_len,
                        singleton_response_len - 1,
                        reply,
                        OpPriority::Background,
                        retention,
                    ))
                },
                Some(Duration::from_secs(2)),
            )
            .await
            .unwrap();
        assert_eq!(
            retained,
            CallResult::Success(BatchReadPage {
                entries: vec![present]
            })
        );

        let empty = read(Vec::new(), 0).await.unwrap();
        assert_eq!(
            empty,
            CallResult::Success(BatchReadPage {
                entries: Vec::new()
            })
        );
        assert!(
            released.upgrade().is_none(),
            "low-level worker retained a completed request lease"
        );
        worker.stop(None);
        handle.await.unwrap();
    }

    #[tokio::test]
    async fn invalid_wire_record_apply_is_nonfatal_to_worker() {
        let shared = Arc::new(test_state(HashMap::new()));
        let (worker, handle) = KvWorker::spawn(
            None,
            KvWorker,
            KvWorkerArgs {
                shared: shared.clone(),
                pending: None,
            },
        )
        .await
        .unwrap();

        let oversized_key = "k".repeat(crate::wire::MAX_FRAME_BYTES / 2);
        let invalid = worker
            .call(
                |reply| KvMessage::ApplyOp {
                    op: test_op(
                        &oversized_key,
                        VersionedValue::new(Vec::new(), 1, "node".to_string()),
                    ),
                    priority: OpPriority::Default,
                    is_replication: false,
                    reply: Some(reply),
                },
                None,
            )
            .await
            .unwrap();
        assert!(matches!(
            invalid,
            CallResult::Success(Err(ApplyError::InvalidInput(_)))
        ));

        for node_id in [
            String::new(),
            "n".repeat(crate::wire::MAX_WIRE_NODE_ID_BYTES + 1),
        ] {
            let invalid = worker
                .call(
                    move |reply| KvMessage::ApplyOp {
                        op: test_op(
                            "invalid-provenance",
                            VersionedValue::new(Vec::new(), 2, node_id),
                        ),
                        priority: OpPriority::Default,
                        is_replication: false,
                        reply: Some(reply),
                    },
                    None,
                )
                .await
                .unwrap();
            assert!(matches!(
                invalid,
                CallResult::Success(Err(ApplyError::InvalidInput(_)))
            ));
            assert!(shared.get("invalid-provenance").await.is_none());
        }

        let valid = worker
            .call(
                |reply| KvMessage::ApplyOp {
                    op: test_op(
                        "healthy",
                        VersionedValue::new(b"value".to_vec(), 3, "node".to_string()),
                    ),
                    priority: OpPriority::Default,
                    is_replication: false,
                    reply: Some(reply),
                },
                None,
            )
            .await
            .unwrap();
        assert_eq!(valid, CallResult::Success(Ok(ApplyOutcome::Applied)));
        assert_eq!(
            shared.get("healthy").await.unwrap().value,
            b"value".to_vec()
        );

        worker.stop(None);
        handle.await.unwrap();
    }
}