durable-actors 0.7.14

Standalone regional durable-actors control plane, host, and durability runtime
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
use super::*;

#[tokio::test]
async fn generic_executor_connects_before_code_and_hydrates_after_assignment() -> Result<()> {
    let root = tempfile::tempdir_in("/tmp")?;
    let path = root.path().join("executor.sock");
    let listener = ActorExecutorListener::bind(&path).await?;
    let peer = tokio::spawn(async move {
        let mut socket = BufReader::new(tokio::net::UnixStream::connect(path).await?);
        write_json_line(&mut socket, &json!({"type":"warm","protocol":24})).await?;
        let load = read_json_line(&mut socket).await?;
        assert_eq!(load["entrypoint"], "/customer/actors.mjs");
        assert_eq!(load["environment"]["CUSTOMER_KEY"], "value");
        write_json_line(
            &mut socket,
            &json!({"type":"attach","protocol":24,"actor_names":["counter"]}),
        )
        .await?;
        assert_eq!(read_json_line(&mut socket).await?["type"], "attached");
        let hydrate = read_json_line(&mut socket).await?;
        assert_eq!(hydrate["command"]["type"], "hydrate");
        assert_eq!(hydrate["command"]["sqlite"]["txid"], 41);
        write_json_line(
            &mut socket,
            &json!({"type":"reply","message_id":hydrate["message_id"],"reply":{"type":"hydrated"}}),
        )
        .await?;
        anyhow::Ok(())
    });
    let warm = listener.accept_warm().await?;
    let connection = warm
        .load(
            "/customer/actors.mjs",
            &HashMap::from([("CUSTOMER_KEY".into(), "value".into())]),
        )
        .await?;
    connection
        .mark_ready(
            None,
            None,
            Arc::new(crate::litestream::Litestream::default()),
        )
        .await?;
    connection
        .executor()
        .hydrate(
            ActorKey {
                project_id: "default".into(),
                actor_name: "counter".into(),
                actor_id: "one".into(),
            },
            Some(Arc::new(ActorState {
                sqlite: crate::actor::SqliteState::position(41),
            })),
        )
        .await?;
    peer.await??;
    Ok(())
}
use serde_json::json;
use tempfile::TempDir;
use tokio::{
    io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
    net::UnixStream,
    time::{Duration, timeout},
};

#[tokio::test]
async fn residency_reports_are_separate_from_invocation_cache_hints() -> Result<()> {
    let root = TempDir::new_in("/tmp")?;
    let socket = root.path().join("residency.sock");
    let listener = ActorExecutorListener::bind(&socket).await?;
    let mut peer = BufReader::new(UnixStream::connect(&socket).await?);
    write_json_line(
        &mut peer,
        &json!({"type":"attach", "protocol":24, "actor_names":["Room"]}),
    )
    .await?;
    let connection = listener.accept().await?;
    connection
        .mark_ready(
            None,
            None,
            Arc::new(crate::litestream::Litestream::default()),
        )
        .await?;
    let executor = connection.executor();
    assert!(executor.resident_actors().is_none());
    let mut changes = executor
        .residency_changes()
        .expect("residency notifications");
    let actor = ActorKey {
        project_id: "default".into(),
        actor_name: "Room".into(),
        actor_id: "one".into(),
    };
    for actors in [vec![actor], vec![]] {
        write_json_line(&mut peer, &json!({"type":"residency", "actors":actors})).await?;
        timeout(Duration::from_secs(1), changes.changed()).await??;
        timeout(Duration::from_secs(1), async {
            while executor.resident_actors() != Some(actors.clone()) {
                tokio::task::yield_now().await;
            }
        })
        .await?;
    }
    Ok(())
}

#[tokio::test]
async fn connection_lookup_is_scoped_to_its_invocation_and_does_not_block_other_actors()
-> Result<()> {
    struct Source {
        requested: mpsc::UnboundedSender<ActorKey>,
        release: Arc<tokio::sync::Semaphore>,
    }
    #[async_trait]
    impl ActorSocketSource for Source {
        async fn query(
            &self,
            actor: &ActorKey,
            query: crate::actor::SocketQuery,
        ) -> Result<crate::actor::SocketLookup> {
            self.requested.send(actor.clone())?;
            self.release.acquire().await?.forget();
            if query.count_only {
                return Ok(crate::actor::SocketLookup::Count(1));
            }
            Ok(crate::actor::SocketLookup::Connections(vec![
                ActorSocketConnection {
                    id: actor.actor_id.clone(),
                    metadata: json!({}),
                    tags: vec![],
                },
            ]))
        }
    }
    let (host, customer) = UnixStream::pair()?;
    let (reader, writer) = host.into_split();
    let (executor, running) =
        JsActorExecutor::start(BufReader::new(reader), writer, vec!["counter".into()]);
    let (requested, mut requests) = mpsc::unbounded_channel();
    let release = Arc::new(tokio::sync::Semaphore::new(0));
    executor
        .mark_ready(
            None,
            Some(Arc::new(Source {
                requested,
                release: release.clone(),
            })),
            Arc::new(crate::litestream::Litestream::default()),
        )
        .await?;
    let peer = async {
        let mut customer = BufReader::new(customer);
        assert_eq!(read_json_line(&mut customer).await?["type"], "attached");
        let slow = read_json_line(&mut customer).await?;
        write_json_line(&mut customer, &json!({"type":"get_connections", "message_id":slow["message_id"], "actor": {"project_id": "default", "actor_name":"forged","actor_id":"forged"}})).await?;
        let fast = read_json_line(&mut customer).await?;
        assert_eq!(fast["command"]["actor"]["actor_id"], "fast");
        write_json_line(&mut customer, &json!({"type":"reply", "message_id":fast["message_id"], "reply":{"type":"invoked", "result":42, "sqlite":{"txid":1}}})).await?;
        let loaded = read_json_line(&mut customer).await?;
        assert_eq!(loaded["type"], "socket_connections");
        assert_eq!(loaded["message_id"], slow["message_id"]);
        assert_eq!(loaded["connections"][0]["id"], "slow");
        write_json_line(&mut customer, &json!({"type":"reply", "message_id":slow["message_id"], "reply":{"type":"invoked", "result":1, "sqlite":{"txid":1}}})).await?;
        anyhow::Ok(())
    };
    let invoke = |id: &str| {
        executor.invoke(
            ActorMethodInvocation {
                request_id: id.into(),
                actor: ActorKey {
                    project_id: "default".into(),
                    actor_name: "counter".into(),
                    actor_id: id.into(),
                },
                method: "run".into(),
                args: vec![],
            },
            None,
        )
    };
    let callers = async {
        let fast = async {
            let actor = requests.recv().await.context("no lookup")?;
            assert_eq!(actor.actor_name, "counter");
            assert_eq!(actor.actor_id, "slow");
            assert!(
                matches!(invoke("fast").await?, ActorMethodOutcome::Completed { result, .. } if result == json!(42))
            );
            release.add_permits(1);
            anyhow::Ok(())
        };
        let (slow, ()) = tokio::try_join!(invoke("slow"), fast)?;
        assert!(matches!(slow, ActorMethodOutcome::Completed { result, .. } if result == json!(1)));
        anyhow::Ok(())
    };
    let result = timeout(Duration::from_secs(2), async {
        tokio::try_join!(peer, callers)
    })
    .await;
    running.abort();
    result??;
    Ok(())
}

#[tokio::test]
async fn admission_is_a_single_permission_from_an_active_invocation() -> Result<()> {
    let (host, customer) = UnixStream::pair()?;
    let (reader, writer) = host.into_split();
    let (executor, running) =
        JsActorExecutor::start(BufReader::new(reader), writer, vec!["counter".into()]);
    let mut admission = executor.invocation_admission().unwrap();
    assert!(!admission.has_changed()?);
    let caller = tokio::spawn(async move {
        executor
            .invoke(
                ActorMethodInvocation {
                    request_id: "request".into(),
                    actor: ActorKey {
                        project_id: "default".into(),
                        actor_name: "counter".into(),
                        actor_id: "one".into(),
                    },
                    method: "hold".into(),
                    args: vec![],
                },
                None,
            )
            .await
    });
    let mut customer = BufReader::new(customer);
    let command = timeout(Duration::from_secs(1), read_json_line(&mut customer)).await??;
    assert!(!admission.has_changed()?);
    let permission = json!({"type": "ready_for_invocation", "message_id": command["message_id"]});
    write_json_line(&mut customer, &permission).await?;
    timeout(Duration::from_secs(1), admission.changed()).await??;
    write_json_line(&mut customer, &permission).await?;
    let error = timeout(Duration::from_secs(1), running)
        .await??
        .unwrap_err();
    assert!(error.to_string().contains("already granted"));
    assert!(caller.await?.is_err());
    Ok(())
}

#[tokio::test]
async fn admission_without_an_active_invocation_is_rejected() -> Result<()> {
    let (host, customer) = UnixStream::pair()?;
    let (reader, writer) = host.into_split();
    let (_executor, running) =
        JsActorExecutor::start(BufReader::new(reader), writer, vec!["counter".into()]);
    let mut customer = BufReader::new(customer);
    write_json_line(
        &mut customer,
        &json!({"type":"ready_for_invocation", "message_id": 99}),
    )
    .await?;
    let error = timeout(Duration::from_secs(1), running)
        .await??
        .unwrap_err();
    assert!(error.to_string().contains("no active call"));
    Ok(())
}

#[tokio::test]
async fn multiplexes_out_of_order_replies_before_peer_disconnect() -> Result<()> {
    let (host, customer) = UnixStream::pair()?;
    let (reader, writer) = host.into_split();
    let (executor, running) =
        JsActorExecutor::start(BufReader::new(reader), writer, vec!["counter".into()]);
    let peer = tokio::spawn(async move {
        let mut customer = BufReader::new(customer);
        let first = read_json_line(&mut customer).await?;
        let second = read_json_line(&mut customer).await?;
        for command in [second, first] {
            write_json_line(&mut customer, &json!({
                    "type": "reply", "message_id": command["message_id"],
                    "reply": {"type": "invoked", "result": command["command"]["request_id"], "sqlite": {"txid": 1}}
                })).await?;
        }
        anyhow::Ok(())
    });
    let invoke = |id: &str| {
        executor.invoke(
            ActorMethodInvocation {
                request_id: id.into(),
                actor: ActorKey {
                    project_id: "default".into(),
                    actor_name: "counter".into(),
                    actor_id: id.into(),
                },
                method: "get".into(),
                args: Vec::new(),
            },
            None,
        )
    };
    let replies = timeout(Duration::from_secs(2), async {
        tokio::try_join!(invoke("first"), invoke("second"))
    })
    .await?;
    peer.await??;
    let _ = running.await;
    let (first, second) = replies?;
    assert!(
        matches!(first, ActorMethodOutcome::Completed { result, .. } if result == json!("first"))
    );
    assert!(
        matches!(second, ActorMethodOutcome::Completed { result, .. } if result == json!("second"))
    );
    Ok(())
}

#[tokio::test]
async fn shutdown_does_not_wait_for_a_peer_that_stopped_reading() -> Result<()> {
    let root = TempDir::new_in("/tmp")?;
    let socket = root.path().join("executor.sock");
    let listener = ActorExecutorListener::bind(&socket).await?;
    let customer = tokio::spawn(async move {
        let stream = UnixStream::connect(socket).await?;
        let mut stream = BufReader::new(stream);
        write_json_line(
            &mut stream,
            &json!({"type":"attach", "protocol":24, "actor_names":["counter"]}),
        )
        .await?;
        let _ = read_json_line(&mut stream).await?;
        std::future::pending::<Result<()>>().await
    });
    let connection = listener.accept().await?;
    connection
        .mark_ready(
            None,
            None,
            Arc::new(crate::litestream::Litestream::default()),
        )
        .await?;
    let executor = connection.executor();
    let shutdown = CancellationToken::new();
    let mut running = tokio::spawn(connection.run(shutdown.clone()));
    let mut call = tokio::spawn(async move {
        executor
            .invoke(
                ActorMethodInvocation {
                    request_id: "blocked-write".into(),
                    actor: ActorKey {
                        project_id: "default".into(),
                        actor_name: "counter".into(),
                        actor_id: "one".into(),
                    },
                    method: "accept".into(),
                    args: vec![json!("x".repeat(8 * 1024 * 1024))],
                },
                None,
            )
            .await
    });
    assert!(timeout(Duration::from_millis(30), &mut call).await.is_err());
    shutdown.cancel();
    let stopped = timeout(Duration::from_millis(200), &mut running).await;
    running.abort();
    call.abort();
    customer.abort();
    stopped.context("executor shutdown waited for a blocked socket writer")???;
    Ok(())
}

#[tokio::test]
async fn one_javascript_executor_runs_until_host_shutdown() -> Result<()> {
    let root = TempDir::new_in("/tmp")?;
    let socket = root.path().join("actor-executor.sock");
    let host = ActorExecutorListener::bind(&socket).await?;
    let customer = tokio::spawn(run_incrementing_customer(socket.clone()));
    let connection = host.accept().await?;
    let executor = connection.executor();
    connection
        .mark_ready(
            None,
            None,
            Arc::new(crate::litestream::Litestream::default()),
        )
        .await?;
    assert!(executor.supports("counter"));

    let shutdown = CancellationToken::new();
    let connection_task = tokio::spawn(connection.run(shutdown.clone()));
    let outcome = executor
        .invoke(
            ActorMethodInvocation {
                request_id: "request-1".into(),
                actor: ActorKey {
                    project_id: "default".into(),
                    actor_name: "counter".into(),
                    actor_id: "counter-1".into(),
                },
                method: "increment".into(),
                args: vec![json!(2)],
            },
            None,
        )
        .await?;
    assert_eq!(
        outcome,
        ActorMethodOutcome::Completed {
            result: json!(2),
            state: ActorState {
                sqlite: crate::actor::SqliteState::position(2)
            },
            effects: Vec::new(),
        }
    );
    let socket_outcome = executor
        .handle_socket(
            ActorSocketInvocation {
                request_id: "socket-request-1".into(),
                actor: ActorKey {
                    project_id: "default".into(),
                    actor_name: "counter".into(),
                    actor_id: "counter-1".into(),
                },
                event: ActorSocketEvent::Connect {
                    connection: ActorSocketConnection {
                        id: "socket-1".into(),
                        metadata: json!({ "userId": "user-1" }),
                        tags: Vec::new(),
                    },
                },
                connections: vec![ActorSocketConnection {
                    id: "socket-1".into(),
                    metadata: json!({ "userId": "user-1" }),
                    tags: Vec::new(),
                }],
            },
            Some(&ActorState {
                sqlite: crate::actor::SqliteState::position(2),
            }),
        )
        .await?;
    assert_eq!(
        socket_outcome,
        ActorSocketOutcome::Handled {
            state: ActorState {
                sqlite: crate::actor::SqliteState::position(3)
            },
            effects: vec![ActorSocketEffect::Send {
                connection_id: "socket-1".into(),
                message: ActorSocketMessage::Text {
                    data: "ready".into()
                },
            }],
        }
    );
    shutdown.cancel();
    connection_task.await??;
    customer.await??;
    Ok(())
}

#[tokio::test]
async fn resident_commands_omit_state_and_retry_only_an_explicit_hydration_request() -> Result<()> {
    let (host, customer) = UnixStream::pair()?;
    let (reader, writer) = host.into_split();
    let (executor, running) =
        JsActorExecutor::start(BufReader::new(reader), writer, vec!["counter".into()]);
    let mut reader = BufReader::new(customer);
    let customer = async {
        let first = read_json_line(&mut reader).await?;
        assert_eq!(first["command"]["sqlite"], json!({"txid": 9}));
        write_json_line(&mut reader, &json!({"type":"reply", "message_id":first["message_id"], "reply":json!({"type":"invoked", "result":10,"sqlite": {"txid": 10}})})).await?;
        let warm = read_json_line(&mut reader).await?;
        assert!(warm["command"].get("sqlite").is_none());
        assert_eq!(warm["command"]["resident_only"], true);
        write_json_line(&mut reader, &json!({"type":"reply", "message_id":warm["message_id"], "reply":json!({"type":"state_required"})})).await?;
        let retry = read_json_line(&mut reader).await?;
        assert_eq!(
            retry["command"]["request_id"],
            warm["command"]["request_id"]
        );
        assert_eq!(retry["command"]["sqlite"], json!({"txid": 10}));
        assert_eq!(retry["command"]["resident_only"], false);
        write_json_line(&mut reader, &json!({"type":"reply", "message_id":retry["message_id"], "reply":json!({"type":"invoked", "result":11,"sqlite": {"txid": 11}})})).await?;
        anyhow::Ok(())
    };
    let invoke = async {
        for count in [9, 10] {
            let outcome = executor
                .invoke(
                    ActorMethodInvocation {
                        request_id: format!("request-{count}"),
                        actor: ActorKey {
                            project_id: "default".into(),
                            actor_name: "counter".into(),
                            actor_id: "one".into(),
                        },
                        method: "increment".into(),
                        args: vec![],
                    },
                    Some(&ActorState {
                        sqlite: crate::actor::SqliteState::position(count),
                    }),
                )
                .await?;
            assert!(
                matches!(outcome, ActorMethodOutcome::Completed {result, ..} if result == json!(count + 1))
            );
        }
        anyhow::Ok(())
    };
    tokio::try_join!(customer, invoke)?;
    running.abort();
    Ok(())
}

#[tokio::test]
async fn executor_receives_the_shared_database_path_for_host_managed_commits() -> Result<()> {
    let state = ActorState {
        sqlite: crate::actor::SqliteState {
            txid: 1,
            path: Some("/tmp/actor.sqlite".into()),
        },
    };
    let (host, customer) = UnixStream::pair()?;
    let (reader, writer) = host.into_split();
    let (executor, running) =
        JsActorExecutor::start(BufReader::new(reader), writer, vec!["counter".into()]);
    let customer = async {
        let mut reader = BufReader::new(customer);
        let message = read_json_line(&mut reader).await?;
        assert_eq!(
            message["command"]["sqlite"],
            serde_json::to_value(&state.sqlite)?
        );
        write_json_line(
            &mut reader,
            &json!({
                "type": "reply", "message_id": message["message_id"],
                "reply": {"type": "invoked", "result": 1, "sqlite": state.sqlite}
            }),
        )
        .await
    };
    let invoke = async {
        let outcome = executor
            .invoke(
                ActorMethodInvocation {
                    request_id: "large".into(),
                    actor: ActorKey {
                        project_id: "default".into(),
                        actor_name: "counter".into(),
                        actor_id: "one".into(),
                    },
                    method: "read".into(),
                    args: vec![],
                },
                Some(&state),
            )
            .await?;
        assert_eq!(
            outcome,
            ActorMethodOutcome::Completed {
                result: json!(1),
                state: state.clone(),
                effects: vec![],
            }
        );
        anyhow::Ok(())
    };
    let result = timeout(Duration::from_secs(30), async {
        tokio::try_join!(customer, invoke)
    })
    .await;
    running.abort();
    result??;
    Ok(())
}

async fn run_incrementing_customer(socket: PathBuf) -> Result<()> {
    let stream = UnixStream::connect(socket).await?;
    let (reader, mut writer) = stream.into_split();
    let mut reader = BufReader::new(reader);
    writer
        .write_all(b"{\"type\":\"attach\",\"protocol\":24,\"actor_names\":[\"counter\"]}\n")
        .await?;
    ensure!(
        read_json_line(&mut reader).await?
            == json!({ "type": "attached", "protocol": 24, "supports_residency": true })
    );

    let invocation = read_json_line(&mut reader).await?;
    let invocation_id = invocation["message_id"]
        .as_u64()
        .context("invocation message ID")?;
    ensure!(invocation["command"]["type"] == "invoke");
    ensure!(invocation["command"].get("timeout_ms").is_none());
    write_json_line(
        &mut writer,
        &json!({
            "type": "reply",
            "message_id": invocation_id,
            "reply": {
                "type": "invoked",
                "result": 2,
                "sqlite": {"txid": 2}
            }
        }),
    )
    .await?;

    let socket_event = read_json_line(&mut reader).await?;
    let socket_event_id = socket_event["message_id"]
        .as_u64()
        .context("socket event message ID")?;
    ensure!(socket_event["command"]["type"] == "websocket_event");
    ensure!(socket_event["command"]["event"]["type"] == "connect");
    write_json_line(
        &mut writer,
        &json!({
            "type": "reply",
            "message_id": socket_event_id,
            "reply": {
                "type": "websocket_handled",
                "sqlite": {"txid": 3},
                "effects": [{
                    "type": "send",
                    "connection_id": "socket-1",
                    "message": { "type": "text", "data": "ready" }
                }]
            }
        }),
    )
    .await?;

    let mut trailing = String::new();
    ensure!(
        reader.read_line(&mut trailing).await? == 0,
        "expected Rust host to close the actor executor"
    );
    Ok(())
}

async fn read_json_line<R>(reader: &mut R) -> Result<Value>
where
    R: tokio::io::AsyncBufRead + Unpin,
{
    let mut line = String::new();
    ensure!(reader.read_line(&mut line).await? > 0, "expected JSON line");
    Ok(serde_json::from_str(line.trim_end())?)
}

async fn write_json_line<W>(writer: &mut W, value: &Value) -> Result<()>
where
    W: tokio::io::AsyncWrite + Unpin,
{
    writer
        .write_all(serde_json::to_string(value)?.as_bytes())
        .await?;
    writer.write_all(b"\n").await?;
    Ok(())
}