actordb 0.11.1

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
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
// Copyright (c) Sean Lawlor
// Licensed under the MIT license in the repository root.

//! Manual release diagnostic for #190. It exercises the production snapshot
//! helper without exposing private Node internals to an external benchmark.
//! Lookup cases use real sender handles cloned into synthetic unrelated slots;
//! Node cases use real fragment actors, senders, TCP replication and ACKs.

use super::*;
use futures::future::join_all;
use std::hint::black_box;
use std::time::Instant;

fn bench_option(name: &str, default: usize) -> usize {
    std::env::var(name).map_or(default, |value| {
        value.parse().expect("invalid benchmark option")
    })
}

async fn sender_setup_state(source: &Node, replica: &Node) -> (usize, usize) {
    let mut complete = 0;
    let mut total = 0;
    for node in [source, replica] {
        let reports: Vec<_> = node.sender_reports.lock().await.values().cloned().collect();
        for report in reports {
            total += 1;
            let report = report.lock().await;
            complete += usize::from(
                report.state == "connected"
                    && report.reconcile_rounds > 0
                    && report.progress.reconciliation_stage.is_none()
                    && report.buffered == 0
                    && report.in_flight == 0,
            );
        }
    }
    (complete, total)
}

fn key_for_fragment(fragment_count: usize, target: usize, seed: usize) -> String {
    for nonce in 0u64.. {
        let key = format!("sender-index-{target}-{seed}-{nonce}");
        if fragment_of(&key, fragment_count) == FragmentId(target as u32) {
            return key;
        }
    }
    unreachable!()
}

async fn lookup_sweep() {
    let node = Node::start(
        "lookup".into(),
        "127.0.0.1:0".parse().unwrap(),
        NodeConfig::builder()
            .fragment_map(HashMap::from([(
                FragmentId(0),
                vec!["lookup".into(), "b".into(), "c".into()],
            )]))
            .build(),
    )
    .await
    .unwrap();
    let handles = snapshot_senders_for_fragment(&node.senders, FragmentId(0)).await;
    assert_eq!(handles.len(), 2);
    for fragment_count in [1usize, 64, 1024, 8192] {
        let registry: SenderMap = Arc::new(Mutex::new(Default::default()));
        for fragment in 0..fragment_count {
            for (peer, sender) in handles.iter().enumerate() {
                registry.lock().await.insert(
                    (FragmentId(fragment as u32), format!("peer-{peer}")),
                    sender.clone(),
                );
            }
        }
        for concurrency in [1usize, 32] {
            let iterations = 1_000_000 / concurrency;
            let barrier = Arc::new(tokio::sync::Barrier::new(concurrency + 1));
            let tasks: Vec<_> = (0..concurrency)
                .map(|writer| {
                    let registry = registry.clone();
                    let barrier = barrier.clone();
                    tokio::spawn(async move {
                        barrier.wait().await;
                        for iteration in 0..iterations {
                            let fragment =
                                FragmentId(((writer + iteration) % fragment_count) as u32);
                            let targets = snapshot_senders_for_fragment(&registry, fragment).await;
                            assert_eq!(targets.len(), 2);
                            black_box(targets);
                        }
                    })
                })
                .collect();
            let started = Instant::now();
            barrier.wait().await;
            for task in tasks {
                task.await.unwrap();
            }
            let nanos = started.elapsed().as_nanos() as f64;
            let operations = (iterations * concurrency) as f64;
            println!(
                "SENDER_BENCH,lookup,{fragment_count},2,{concurrency},0,{:.3},{:.3},0,0",
                operations * 1e9 / nanos,
                nanos / operations,
            );
        }
    }
    node.shutdown().await.unwrap();
}

async fn replicated_pair(fragment_count: usize) -> (Node, Node) {
    let fragment_map = (0..fragment_count)
        .map(|fragment| (FragmentId(fragment as u32), vec!["a".into(), "b".into()]))
        .collect::<HashMap<_, _>>();
    // Fixed workers, runtime threads, fan-out and active-fragment count.
    let pool = || PoolConfig::builder().workers(1).queue_cap(100_000).build();
    let config = || {
        NodeConfig::builder()
            .default_pool(pool())
            .high_pool(pool())
            .background_pool(pool())
            .fragment_count(fragment_count)
            .fragment_map(fragment_map.clone())
            .replication(
                SenderConfig::builder()
                    .batch_interval(Duration::from_millis(bench_option(
                        "ACTORDB_SENDER_BENCH_TICK_MS",
                        1,
                    ) as u64))
                    .consistency_interval(Duration::from_secs(3600))
                    .max_in_flight(64)
                    .buffer_capacity(100_000)
                    .build(),
            )
            .build()
    };
    let options = NodeRuntimeOptions::builder()
        .peer_queue_frame_capacity(4096)
        .peer_queue_byte_capacity(256 * 1024 * 1024)
        .build();
    let a = Node::start_with_options(
        "a".into(),
        "127.0.0.1:0".parse().unwrap(),
        config(),
        options.clone(),
    )
    .await
    .unwrap();
    let b = Node::start_with_options(
        "b".into(),
        "127.0.0.1:0".parse().unwrap(),
        config(),
        options,
    )
    .await
    .unwrap();
    a.connect(b.addr()).await.unwrap();
    (a, b)
}

async fn node_sweep(isolated_registry: bool) {
    let mut counts = if isolated_registry {
        vec![8usize, 64, 1024, 8192]
    } else {
        vec![1, 64, 512]
    };
    if let Ok(value) = std::env::var("ACTORDB_SENDER_BENCH_FRAGMENTS") {
        counts = vec![value.parse().expect("invalid fragment count")];
    }
    for registry_fragment_count in counts {
        let fragment_count = if isolated_registry {
            8
        } else {
            registry_fragment_count
        };
        let setup_started = Instant::now();
        let (source, replica) = replicated_pair(fragment_count).await;
        if bench_option("ACTORDB_SENDER_BENCH_SETTLE", 0) != 0 {
            tokio::time::timeout(Duration::from_secs(30), async {
                loop {
                    let (complete, total) = sender_setup_state(&source, &replica).await;
                    if complete == total && total == 2 * fragment_count {
                        break;
                    }
                    tokio::time::sleep(Duration::from_millis(2)).await;
                }
            })
            .await
            .expect("initial reconciliation did not finish");
        }
        println!(
            "SENDER_SETUP,{fragment_count},ready_seconds,{:.6}",
            setup_started.elapsed().as_secs_f64()
        );
        if isolated_registry {
            // Finish both Hello handlers before injecting aliases: connection
            // setup enumerates all peer senders, which would otherwise notify
            // the same task repeatedly and contaminate this lookup experiment.
            tokio::time::timeout(Duration::from_secs(30), async {
                loop {
                    let mut ready = true;
                    for fragment in 0..fragment_count {
                        for (node, peer) in [(&source, "b"), (&replica, "a")] {
                            ready &= node
                                .sender_report(FragmentId(fragment as u32), peer)
                                .await
                                .unwrap()
                                .state
                                == "connected";
                        }
                    }
                    if ready {
                        break;
                    }
                    tokio::time::sleep(Duration::from_millis(1)).await;
                }
            })
            .await
            .expect("replication sessions did not become ready");
            // Synthetic unrelated slots isolate the map scan from actor count
            // and idle sender timers. Only the eight real fragments receive
            // mutations. Aliased handles are never offered unrelated writes.
            let handle = snapshot_senders_for_fragment(&source.senders, FragmentId(0))
                .await
                .pop()
                .unwrap();
            let mut registry = source.senders.lock().await;
            for fragment in fragment_count..registry_fragment_count {
                registry.insert((FragmentId(fragment as u32), "b".into()), handle.clone());
            }
        }
        for (value_bytes, concurrency) in [(100usize, 1usize), (100, 32), (16_384, 32)] {
            if std::env::var("ACTORDB_SENDER_BENCH_CONCURRENCY").is_ok()
                && concurrency != bench_option("ACTORDB_SENDER_BENCH_CONCURRENCY", 32)
            {
                continue;
            }
            if std::env::var("ACTORDB_SENDER_BENCH_BYTES").is_ok()
                && value_bytes != bench_option("ACTORDB_SENDER_BENCH_BYTES", 100)
            {
                continue;
            }
            if value_bytes != 100
                && if isolated_registry {
                    registry_fragment_count != 8 && registry_fragment_count != 8192
                } else {
                    fragment_count == 64
                }
            {
                continue;
            }
            let keys: Vec<_> = (0..concurrency)
                .map(|index| key_for_fragment(fragment_count, index % fragment_count.min(8), index))
                .collect();
            let sample_count = bench_option("ACTORDB_SENDER_BENCH_SAMPLES", 400);
            let warmup = bench_option("ACTORDB_SENDER_BENCH_WARMUP", 50);
            let mut samples = Vec::with_capacity(sample_count);
            for generation in 0u64..(sample_count + warmup) as u64 {
                if generation == warmup as u64 {
                    let (complete, total) = sender_setup_state(&source, &replica).await;
                    println!("SENDER_SETUP,{fragment_count},{value_bytes},{concurrency},reconciled,{complete},{total}");
                }
                let mut value = vec![0x5a; value_bytes];
                value[..8].copy_from_slice(&generation.to_be_bytes());
                let writes: Vec<_> = keys
                    .iter()
                    .map(|key| (key.clone(), value.clone()))
                    .collect();
                let started = Instant::now();
                let results = join_all(
                    writes
                        .into_iter()
                        .map(|(key, value)| source.put(key, value)),
                )
                .await;
                let elapsed = started.elapsed().as_nanos() as f64;
                assert!(results.iter().all(Result::is_ok));
                if generation >= warmup as u64 {
                    samples.push(elapsed);
                }
                // Exact-generation replica visibility is outside the timer.
                // Drain before the next batch so bounded-ingress overflow and
                // reset cannot inflate measured source throughput.
                let deadline = Instant::now() + Duration::from_secs(30);
                loop {
                    let visible = replica.get_multi(keys.clone()).await;
                    if visible.iter().all(
                        |result| matches!(result, Ok(Some(versioned)) if versioned.value == value),
                    ) {
                        break;
                    }
                    assert!(Instant::now() < deadline, "replication did not catch up");
                    tokio::time::sleep(Duration::from_millis(1)).await;
                }
            }
            for fragment in 0..concurrency.min(fragment_count.min(8)) {
                let report = source
                    .sender_report(FragmentId(fragment as u32), "b")
                    .await
                    .unwrap();
                assert_ne!(
                    report.state, "buffer_reset",
                    "benchmark overflowed sender ingress"
                );
            }
            let mean = samples.iter().sum::<f64>() / samples.len() as f64;
            if let Ok(path) = std::env::var("ACTORDB_SENDER_BENCH_SAMPLES_FILE") {
                std::fs::write(
                    path,
                    samples
                        .iter()
                        .map(|sample| format!("{sample:.0}\n"))
                        .collect::<String>(),
                )
                .unwrap();
            }
            samples.sort_by(f64::total_cmp);
            let kind = if isolated_registry {
                "node-isolated"
            } else {
                "node"
            };
            println!(
                "SENDER_BENCH,{kind},{registry_fragment_count},1,{concurrency},{value_bytes},{:.3},{:.3},{:.3},{:.3}",
                concurrency as f64 * 1e9 / mean, mean,
                samples[samples.len()/2], samples[samples.len()*95/100],
            );
        }
        source.shutdown().await.unwrap();
        replica.shutdown().await.unwrap();
    }
}

async fn write_chunk(
    source: &Node,
    replica: &Node,
    keys: &[String],
    value_bytes: usize,
    generation: &mut u64,
    batches: usize,
) -> Vec<f64> {
    let mut prepared = Vec::with_capacity(batches);
    let mut expected = Vec::new();
    for _ in 0..batches {
        *generation += 1;
        let mut value = vec![0x5a; value_bytes];
        value[..8].copy_from_slice(&generation.to_be_bytes());
        prepared.push(
            keys.iter()
                .map(|key| (key.clone(), value.clone()))
                .collect::<Vec<_>>(),
        );
        expected = value;
    }
    let mut samples = Vec::with_capacity(batches);
    for writes in prepared {
        let started = Instant::now();
        let results = join_all(
            writes
                .into_iter()
                .map(|(key, value)| source.put(key, value)),
        )
        .await;
        samples.push(started.elapsed().as_nanos() as f64);
        assert!(results.iter().all(Result::is_ok));
    }
    // Catch up once per block. The next timed batch is not coupled to every
    // preceding replication timer/ACK wave. Exact visibility plus idle sender
    // ingress/deques and zero cumulative overflow certify this workload.
    tokio::time::timeout(Duration::from_secs(30), async {
        loop {
            let visible = replica.get_multi(keys.to_vec()).await;
            let mut idle = visible
                .iter()
                .all(|result| matches!(result, Ok(Some(versioned)) if versioned.value == expected));
            // All active fragments must have drained before the next mode.
            let active: HashSet<_> = keys
                .iter()
                .map(|key| fragment_of(key, source.router.fragment_count))
                .collect();
            for fragment in active {
                let handles = snapshot_senders_for_fragment(&source.senders, fragment).await;
                for sender in handles {
                    assert_eq!(sender.ingress_overflow_counts_for_test(), (0, 0));
                    idle &= sender.ingress_is_idle();
                }
                let report = source.sender_report(fragment, "b").await.unwrap();
                idle &= report.buffered == 0 && report.in_flight == 0;
                assert_ne!(report.state, "buffer_reset");
            }
            if idle {
                break;
            }
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
    })
    .await
    .expect("replication did not drain after continuous writes");
    samples
}

/// Switch only registry lookup inside one running node pair. Both layouts
/// retain exactly the same task handles; actor/session/timer state is fixed.
async fn paired_comparison() {
    let fragment_count = bench_option("ACTORDB_SENDER_BENCH_FRAGMENTS", 512);
    let concurrency = bench_option("ACTORDB_SENDER_BENCH_CONCURRENCY", 32);
    let value_bytes = bench_option("ACTORDB_SENDER_BENCH_BYTES", 100);
    let samples_per_block = bench_option("ACTORDB_SENDER_BENCH_SAMPLES", 128);
    let warmup = bench_option("ACTORDB_SENDER_BENCH_WARMUP", 16);
    let control = bench_option("ACTORDB_SENDER_BENCH_CONTROL", 0) != 0;
    let continuous = bench_option("ACTORDB_SENDER_BENCH_CONTINUOUS", 1) != 0;
    for trial in 0..bench_option("ACTORDB_SENDER_BENCH_TRIALS", 4) {
        let (source, replica) = replicated_pair(fragment_count).await;
        tokio::time::timeout(Duration::from_secs(30), async {
            loop {
                let (complete, total) = sender_setup_state(&source, &replica).await;
                if complete == total && total == 2 * fragment_count {
                    break;
                }
                tokio::time::sleep(Duration::from_millis(2)).await;
            }
        })
        .await
        .expect("initial reconciliation did not finish");
        let keys: Vec<_> = (0..concurrency)
            .map(|index| key_for_fragment(fragment_count, index % fragment_count.min(8), index))
            .collect();
        let mut generation = 0u64;
        let warmup_started = Instant::now();
        let mut warmup_chunks = 0usize;
        while warmup_started.elapsed()
            < Duration::from_millis(bench_option("ACTORDB_SENDER_BENCH_PREROLL_MS", 2000) as u64)
        {
            for node in [&source, &replica] {
                node.senders
                    .lock()
                    .await
                    .benchmark_flat_lookup(warmup_chunks.is_multiple_of(2) && !control);
            }
            write_chunk(
                &source,
                &replica,
                &keys,
                value_bytes,
                &mut generation,
                warmup + samples_per_block,
            )
            .await;
            warmup_chunks += 1;
        }
        // ABBA then BAAB: balance order both within and across each trial.
        for (block, flat_label) in [true, false, false, true, false, true, true, false]
            .into_iter()
            .enumerate()
        {
            let flat_label = if trial.is_multiple_of(2) {
                flat_label
            } else {
                !flat_label
            };
            for node in [&source, &replica] {
                node.senders
                    .lock()
                    .await
                    .benchmark_flat_lookup(flat_label && !control);
            }
            let mut samples = Vec::with_capacity(samples_per_block);
            if continuous {
                samples = write_chunk(
                    &source,
                    &replica,
                    &keys,
                    value_bytes,
                    &mut generation,
                    warmup + samples_per_block,
                )
                .await;
                samples.drain(..warmup);
            } else {
                for iteration in 0..(warmup + samples_per_block) {
                    generation += 1;
                    let mut value = vec![0x5a; value_bytes];
                    value[..8].copy_from_slice(&generation.to_be_bytes());
                    let writes: Vec<_> = keys
                        .iter()
                        .map(|key| (key.clone(), value.clone()))
                        .collect();
                    let started = Instant::now();
                    let results = join_all(
                        writes
                            .into_iter()
                            .map(|(key, value)| source.put(key, value)),
                    )
                    .await;
                    let nanos = started.elapsed().as_nanos() as f64;
                    assert!(results.iter().all(Result::is_ok));
                    if iteration >= warmup {
                        samples.push(nanos);
                    }
                    let deadline = Instant::now() + Duration::from_secs(30);
                    loop {
                        let visible = replica.get_multi(keys.clone()).await;
                        if visible.iter().all(
                        |result| matches!(result, Ok(Some(versioned)) if versioned.value == value),
                    ) {
                        break;
                    }
                        assert!(Instant::now() < deadline, "replication did not catch up");
                        tokio::time::sleep(Duration::from_millis(1)).await;
                    }
                }
            }
            let mean = samples.iter().sum::<f64>() / samples.len() as f64;
            let layout = if flat_label { "flat" } else { "index" };
            let kind = if control { "control" } else { "paired" };
            println!("SENDER_PAIR,{kind},{trial},{block},{layout},{fragment_count},{concurrency},{value_bytes},{:.3},{:.3}", concurrency as f64 * 1e9 / mean, mean);
            if let Ok(directory) = std::env::var("ACTORDB_SENDER_BENCH_SAMPLES_DIR") {
                let path = std::path::Path::new(&directory)
                    .join(format!("{kind}-{trial}-{block}-{layout}.samples"));
                std::fs::write(
                    path,
                    samples
                        .iter()
                        .map(|sample| format!("{sample:.0}\n"))
                        .collect::<String>(),
                )
                .unwrap();
            }
        }
        for node in [&source, &replica] {
            for fragment in 0..fragment_count.min(8) {
                assert_ne!(
                    node.sender_report(
                        FragmentId(fragment as u32),
                        if std::ptr::eq(node, &source) {
                            "b"
                        } else {
                            "a"
                        }
                    )
                    .await
                    .unwrap()
                    .state,
                    "buffer_reset"
                );
            }
            node.shutdown().await.unwrap();
        }
    }
}

#[test]
#[ignore = "manual release performance diagnostic; see benches/README.md"]
fn sender_registry_scaling() {
    if cfg!(debug_assertions) {
        panic!("run this diagnostic with --release");
    }
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(bench_option("ACTORDB_SENDER_BENCH_RT_THREADS", 4))
        .enable_all()
        .build()
        .unwrap();
    runtime.block_on(async {
        println!("SENDER_BENCH,kind,fragments,fanout,concurrency,value_bytes,ops_per_sec,mean_ns,p50_batch_ns,p95_batch_ns");
        match std::env::var("ACTORDB_SENDER_BENCH_MODE").as_deref() {
            Ok("lookup") => lookup_sweep().await,
            Ok("node") => node_sweep(false).await,
            Ok("isolated") => node_sweep(true).await,
            Ok("paired") => paired_comparison().await,
            _ => {
                lookup_sweep().await;
                node_sweep(false).await;
                node_sweep(true).await;
            }
        }
    });
}