reifydb-runtime 0.10.1

Runtime infrastructure for ReifyDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

#![allow(clippy::disallowed_types)]

mod pool;

use std::{
	any::Any,
	error, fmt,
	fmt::{Debug, Formatter},
	mem,
	sync::{Arc, OnceLock, Weak},
	time,
	time::Duration,
};

use crossbeam_channel::{Receiver, RecvTimeoutError as CcRecvTimeoutError};

use crate::{
	actor::{
		context::CancellationToken, system::host::pool::PoolActorHandle, timers::scheduler::SchedulerHandle,
		traits::Actor,
	},
	context::clock::Clock,
	pool::{
		PoolConfig, Pools,
		actor_pool::{EPHEMERAL_BATCH_SIZE, Schedule},
	},
	sync::mutex::Mutex,
};

static TESTING_ROOT: OnceLock<ActorSystem> = OnceLock::new();

struct ActorSystemInner {
	cancel: CancellationToken,
	scheduler: SchedulerHandle,
	clock: Clock,
	pools: Pools,
	wakers: Mutex<Vec<Arc<dyn Fn() + Send + Sync>>>,
	keepalive: Mutex<Vec<Box<dyn Any + Send + Sync>>>,
	done_rxs: Mutex<Vec<Receiver<Result<(), String>>>>,
	children: Mutex<Vec<ActorSystem>>,
	parent: Weak<ActorSystemInner>,
}

#[derive(Clone)]
pub struct ActorSystem {
	inner: Arc<ActorSystemInner>,
}

impl ActorSystem {
	pub fn new(pools: Pools, clock: Clock) -> Self {
		let scheduler = SchedulerHandle::new();

		Self {
			inner: Arc::new(ActorSystemInner {
				cancel: CancellationToken::new(),
				scheduler,
				clock,
				pools,
				wakers: Mutex::new(Vec::new()),
				keepalive: Mutex::new(Vec::new()),
				done_rxs: Mutex::new(Vec::new()),
				children: Mutex::new(Vec::new()),
				parent: Weak::new(),
			}),
		}
	}

	pub fn testing(clock: Clock) -> Self {
		TESTING_ROOT.get_or_init(|| Self::new(Pools::new(PoolConfig::default()), Clock::Real)).scope_with(clock)
	}

	pub fn scope(&self) -> Self {
		self.scope_with(self.inner.clock.clone())
	}

	fn scope_with(&self, clock: Clock) -> Self {
		let cancel = self.inner.cancel.child_token();
		let child = Self {
			inner: Arc::new(ActorSystemInner {
				scheduler: self.inner.scheduler.shared(cancel.clone()),
				cancel,
				clock,
				pools: self.inner.pools.clone(),
				wakers: Mutex::new(Vec::new()),
				keepalive: Mutex::new(Vec::new()),
				done_rxs: Mutex::new(Vec::new()),
				children: Mutex::new(Vec::new()),
				parent: Arc::downgrade(&self.inner),
			}),
		};
		self.inner.children.lock().push(child.clone());
		child
	}

	pub fn pools(&self) -> Pools {
		self.inner.pools.clone()
	}

	pub fn spawner(&self) -> ActorSpawner {
		ActorSpawner {
			inner: Arc::downgrade(&self.inner),
			clock: self.inner.clock.clone(),
		}
	}

	pub fn cancellation_token(&self) -> CancellationToken {
		self.inner.cancel.clone()
	}

	pub fn is_cancelled(&self) -> bool {
		self.inner.cancel.is_cancelled()
	}

	pub fn shutdown(&self) {
		self.inner.cancel.cancel();

		let children = mem::take(&mut *self.inner.children.lock());
		for child in &children {
			child.shutdown();
		}
		drop(children);

		let wakers = mem::take(&mut *self.inner.wakers.lock());
		for waker in &wakers {
			waker();
		}
		drop(wakers);

		self.inner.scheduler.shutdown();

		self.inner.keepalive.lock().clear();

		if let Some(parent) = self.inner.parent.upgrade() {
			parent.children.lock().retain(|child| !Arc::ptr_eq(&child.inner, &self.inner));
		}
	}

	pub(crate) fn register_waker(&self, f: Arc<dyn Fn() + Send + Sync>) {
		self.inner.wakers.lock().push(f);
	}

	pub(crate) fn register_keepalive(&self, cell: Box<dyn Any + Send + Sync>) {
		self.inner.keepalive.lock().push(cell);
	}

	pub(crate) fn register_done_rx(&self, rx: Receiver<Result<(), String>>) {
		self.inner.done_rxs.lock().push(rx);
	}

	pub fn join(&self) -> Result<(), JoinError> {
		self.join_timeout(Duration::from_secs(5))
	}

	#[allow(clippy::disallowed_methods)]
	pub fn join_timeout(&self, timeout: Duration) -> Result<(), JoinError> {
		let deadline = time::Instant::now() + timeout;
		let rxs: Vec<_> = mem::take(&mut *self.inner.done_rxs.lock());
		for rx in rxs {
			let remaining = deadline.saturating_duration_since(time::Instant::now());
			match rx.recv_timeout(remaining) {
				Ok(Ok(())) => {}
				Ok(Err(name)) => {
					return Err(JoinError::new(format!("actor {name} panicked")));
				}
				Err(CcRecvTimeoutError::Disconnected) => {}
				Err(CcRecvTimeoutError::Timeout) => {
					return Err(JoinError::new("timed out waiting for actors to stop"));
				}
			}
		}
		Ok(())
	}

	pub fn scheduler(&self) -> &SchedulerHandle {
		&self.inner.scheduler
	}

	pub fn clock(&self) -> &Clock {
		&self.inner.clock
	}

	pub fn spawn_coordination<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		let group = self.inner.pools.actor_pool().coordination();
		pool::spawn_on_schedule(self, name, actor, Schedule::Pinned(group.assign()), group.batch_size())
	}

	pub fn spawn_flow<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		let group = self.inner.pools.actor_pool().flow();
		pool::spawn_on_schedule(self, name, actor, Schedule::Pinned(group.assign()), group.batch_size())
	}

	pub fn spawn_maintenance<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		let group = self.inner.pools.actor_pool().maintenance();
		pool::spawn_on_schedule(self, name, actor, Schedule::Pinned(group.assign()), group.batch_size())
	}

	pub fn spawn_ephemeral<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		pool::spawn_on_schedule(self, name, actor, self.inner.pools.task_injector(), EPHEMERAL_BATCH_SIZE)
	}
}

impl Debug for ActorSystem {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		f.debug_struct("ActorSystem").field("cancelled", &self.is_cancelled()).finish_non_exhaustive()
	}
}

#[derive(Clone)]
pub struct ActorSpawner {
	inner: Weak<ActorSystemInner>,
	clock: Clock,
}

impl ActorSpawner {
	fn system(&self) -> ActorSystem {
		ActorSystem {
			inner: self.inner.upgrade().expect("runtime already shut down: cannot spawn actor"),
		}
	}

	pub fn clock(&self) -> &Clock {
		&self.clock
	}

	pub fn pools(&self) -> Pools {
		self.system().pools()
	}

	pub fn is_alive(&self) -> bool {
		self.inner.strong_count() > 0
	}

	pub fn cancellation_token(&self) -> Option<CancellationToken> {
		self.inner.upgrade().map(|inner| inner.cancel.clone())
	}

	pub fn scope(&self) -> ActorSpawner {
		self.system().scope().spawner()
	}

	pub fn shutdown(&self) {
		if let Some(inner) = self.inner.upgrade() {
			ActorSystem {
				inner,
			}
			.shutdown();
		}
	}

	pub fn spawn_coordination<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		self.system().spawn_coordination(name, actor)
	}

	pub fn spawn_flow<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		self.system().spawn_flow(name, actor)
	}

	pub fn spawn_maintenance<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		self.system().spawn_maintenance(name, actor)
	}

	pub fn spawn_ephemeral<A: Actor>(&self, name: &str, actor: A) -> ActorHandle<A::Message>
	where
		A::State: Send,
	{
		self.system().spawn_ephemeral(name, actor)
	}
}

impl Debug for ActorSpawner {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		f.debug_struct("ActorSpawner").field("alive", &self.is_alive()).finish_non_exhaustive()
	}
}

pub type ActorHandle<M> = PoolActorHandle<M>;

#[derive(Debug)]
pub struct JoinError {
	message: String,
}

impl JoinError {
	pub fn new(message: impl Into<String>) -> Self {
		Self {
			message: message.into(),
		}
	}
}

impl fmt::Display for JoinError {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		write!(f, "actor join failed: {}", self.message)
	}
}

impl error::Error for JoinError {}

#[cfg(test)]
mod tests {
	use std::sync;

	use super::*;
	use crate::{
		actor::{context::Context, mailbox::ActorRef, system::ActorConfig, traits::Directive},
		pool::{PoolConfig, Pools},
	};

	fn test_system() -> ActorSystem {
		let pools = Pools::new(PoolConfig::default());
		ActorSystem::new(pools, Clock::Real)
	}

	struct CounterActor;

	#[derive(Debug)]
	enum CounterMessage {
		Inc,
		Get(sync::mpsc::Sender<i64>),
		Stop,
	}

	impl Actor for CounterActor {
		type State = i64;
		type Message = CounterMessage;

		fn init(&self, _ctx: &Context<Self::Message>) -> Self::State {
			0
		}

		fn handle(
			&self,
			state: &mut Self::State,
			msg: Self::Message,
			_ctx: &Context<Self::Message>,
		) -> Directive {
			match msg {
				CounterMessage::Inc => *state += 1,
				CounterMessage::Get(tx) => {
					let _ = tx.send(*state);
				}
				CounterMessage::Stop => return Directive::Stop,
			}
			Directive::Continue
		}
	}

	#[test]
	fn test_spawn_and_send() {
		let system = test_system();
		let handle = system.spawn_coordination("counter", CounterActor);

		let actor_ref = handle.actor_ref().clone();
		actor_ref.send(CounterMessage::Inc).unwrap();
		actor_ref.send(CounterMessage::Inc).unwrap();
		actor_ref.send(CounterMessage::Inc).unwrap();

		let (tx, rx) = sync::mpsc::channel();
		actor_ref.send(CounterMessage::Get(tx)).unwrap();

		let value = rx.recv().unwrap();
		assert_eq!(value, 3);

		actor_ref.send(CounterMessage::Stop).unwrap();
		handle.join().unwrap();
	}

	#[test]
	fn test_shutdown_join() {
		let system = test_system();

		for i in 0..5 {
			system.spawn_coordination(&format!("counter-{i}"), CounterActor);
		}

		// join() must not return before every actor has finished, or shutdown races teardown.
		system.shutdown();
		system.join().unwrap();
	}

	#[test]
	fn test_shutdown_stops_the_timer_scheduler() {
		// Timers must be dead before the cells are released, otherwise a callback owns the last system ref.
		let system = test_system();
		system.shutdown();

		let (tx, rx) = sync::mpsc::channel();
		system.scheduler().schedule_once(Duration::from_millis(5), move || {
			let _ = tx.send(());
		});

		assert!(rx.recv_timeout(Duration::from_millis(200)).is_err());
	}

	#[test]
	fn a_scope_that_shuts_down_leaves_its_parent_but_a_live_scope_still_stops_with_it() {
		// A parent that keeps every stopped scope grows by one system per open and close, forever.
		let root = test_system();
		let stopped = root.scope();
		let live = root.scope();
		assert_eq!(root.inner.children.lock().len(), 2, "precondition: both scopes are registered");

		stopped.shutdown();
		let children = root.inner.children.lock().clone();
		assert_eq!(children.len(), 1, "a stopped scope must leave its parent's list");
		assert!(Arc::ptr_eq(&children[0].inner, &live.inner), "only the stopped scope may leave");
		drop(children);

		root.shutdown();
		assert!(live.is_cancelled(), "a parent shutdown must still stop every live scope");
		assert!(root.inner.children.lock().is_empty(), "the parent must hold no scope after its shutdown");
	}
	struct TurnActor {
		log: Arc<sync::Mutex<Vec<&'static str>>>,
		name: &'static str,
		batch: Option<usize>,
	}

	#[derive(Debug)]
	enum TurnMessage {
		Hold(sync::mpsc::Sender<()>, sync::mpsc::Receiver<()>),
		Kick(ActorRef<TurnMessage>),
		Log,
		Done(sync::mpsc::Sender<()>),
	}

	impl Actor for TurnActor {
		type State = ();
		type Message = TurnMessage;

		fn init(&self, _ctx: &Context<Self::Message>) -> Self::State {}

		fn handle(
			&self,
			_state: &mut Self::State,
			msg: Self::Message,
			_ctx: &Context<Self::Message>,
		) -> Directive {
			match msg {
				TurnMessage::Hold(started, release) => {
					let _ = started.send(());
					let _ = release.recv();
				}
				TurnMessage::Kick(other) => {
					let _ = other.send(TurnMessage::Log);
				}
				TurnMessage::Log => self.log.lock().unwrap().push(self.name),
				TurnMessage::Done(tx) => {
					let _ = tx.send(());
				}
			}
			Directive::Continue
		}

		fn config(&self) -> ActorConfig {
			match self.batch {
				Some(batch) => ActorConfig::new().batch_size(batch),
				None => ActorConfig::new(),
			}
		}
	}

	fn turn_order(batch: Option<usize>) -> Vec<&'static str> {
		let system = ActorSystem::new(
			Pools::new(PoolConfig {
				flow_threads: 1,
				..PoolConfig::default()
			}),
			Clock::Real,
		);
		let log = Arc::new(sync::Mutex::new(Vec::new()));
		let actor = |name, batch| TurnActor {
			log: Arc::clone(&log),
			name,
			batch,
		};
		let holder = system.spawn_flow("holder", actor("holder", None));
		let busy = system.spawn_flow("busy", actor("busy", batch));
		let other = system.spawn_flow("other", actor("other", None));

		for handle in [&busy, &other] {
			let (tx, rx) = sync::mpsc::channel();
			handle.actor_ref().send(TurnMessage::Done(tx)).unwrap();
			rx.recv().unwrap();
		}

		let (started_tx, started_rx) = sync::mpsc::channel();
		let (release_tx, release_rx) = sync::mpsc::channel();
		holder.actor_ref().send(TurnMessage::Hold(started_tx, release_rx)).unwrap();
		started_rx.recv().unwrap();

		busy.actor_ref().send(TurnMessage::Kick(other.actor_ref().clone())).unwrap();
		for _ in 0..20 {
			busy.actor_ref().send(TurnMessage::Log).unwrap();
		}
		let (busy_done_tx, busy_done_rx) = sync::mpsc::channel();
		busy.actor_ref().send(TurnMessage::Done(busy_done_tx)).unwrap();
		release_tx.send(()).unwrap();
		busy_done_rx.recv().unwrap();

		let (other_done_tx, other_done_rx) = sync::mpsc::channel();
		other.actor_ref().send(TurnMessage::Done(other_done_tx)).unwrap();
		other_done_rx.recv().unwrap();

		system.shutdown();
		system.join().unwrap();
		log.lock().unwrap().clone()
	}

	#[test]
	fn an_actor_batch_size_holds_its_turn_for_that_many_messages() {
		// A shared queue that yields after the pool default makes every sender wait behind the whole pool.
		let own = turn_order(Some(64));
		assert_eq!(
			own.iter().position(|name| *name == "other"),
			Some(20),
			"a batch of 64 must finish all 20 queued messages before a newly woken actor runs: {own:?}"
		);

		let pooled = turn_order(None);
		assert_eq!(
			pooled.iter().position(|name| *name == "other"),
			Some(7),
			"without its own batch size the actor must yield after the flow pool default of 8: {pooled:?}"
		);
	}

	struct PanicActor;

	impl Actor for PanicActor {
		type State = ();
		type Message = sync::mpsc::Sender<()>;

		fn init(&self, _ctx: &Context<Self::Message>) -> Self::State {}

		fn handle(
			&self,
			_state: &mut Self::State,
			reached: Self::Message,
			_ctx: &Context<Self::Message>,
		) -> Directive {
			let _ = reached.send(());
			panic!("a pool actor crashed while handling a message");
		}
	}

	#[test]
	fn a_pool_actor_that_panics_fails_the_join_instead_of_passing_shutdown_as_clean() {
		// A join that reads a dropped done signal as a clean stop hides a crashed actor behind a clean
		// shutdown.
		let system = test_system();
		let crashed = system.spawn_flow("crashed", PanicActor);
		let (reached_tx, reached_rx) = sync::mpsc::channel();
		crashed.actor_ref().send(reached_tx).unwrap();
		reached_rx.recv().unwrap();

		system.shutdown();

		assert!(system.join().is_err(), "a crashed pool actor must fail the join");
	}

	#[test]
	fn a_pool_actor_that_panics_fails_its_handle_join_after_shutdown() {
		// Subsystems join each actor handle on shutdown; a crashed actor read as joined would report a clean
		// stop.
		let system = test_system();
		let crashed = system.spawn_flow("crashed", PanicActor);
		let (reached_tx, reached_rx) = sync::mpsc::channel();
		crashed.actor_ref().send(reached_tx).unwrap();
		reached_rx.recv().unwrap();

		system.shutdown();

		assert!(crashed.join().is_err(), "a crashed pool actor must fail its handle join");
	}
}