bombay-entity 0.1.0

Local entity activation, lifecycle management, and stable typed routing.
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
//! Concurrent storage for local entity lifecycle machines.

#[cfg(loom)]
use loom::sync::atomic::{AtomicU64, Ordering};
#[cfg(loom)]
use loom::sync::{Arc, Mutex};
use std::collections::HashMap;
use std::hash::{BuildHasher, Hash, RandomState};
use std::num::{NonZeroU64, NonZeroUsize};
#[cfg(not(loom))]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(loom))]
use std::sync::{Arc, Mutex};

use crate::{
    ActivationId, DispatchId, DrainFailure, EntityId, LifecycleMachine, LifecycleOutput, Refusal,
    RetirementMode, SlotEffect, SlotEffectBatch, SlotEvent, TransitionEvidence, lifecycle_machine,
};
use bombay_machine_executor::{LinearizedExecutor, OutputEvidence};
use bombay_transition::Machine;

/// Fixed sizing and admission limits for a local directory.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DirectoryConfig {
    /// Number of independently locked map shards; must be a power of two.
    pub shards: NonZeroUsize,
    /// Maximum commands retained by one in-flight activation.
    pub activation_waiters: NonZeroUsize,
}

impl Default for DirectoryConfig {
    fn default() -> Self {
        Self {
            shards: NonZeroUsize::new(64).expect("64 is non-zero"),
            activation_waiters: NonZeroUsize::new(64).expect("64 is non-zero"),
        }
    }
}

/// Failure before a command can be submitted to a lifecycle machine.
#[derive(Debug, thiserror::Error)]
pub enum DirectoryError<C> {
    /// The shard count was not a power of two.
    #[error("shard count was not a power of two")]
    InvalidShardCount,
    /// The monotonically increasing activation namespace is exhausted.
    #[error("activation identity namespace is exhausted")]
    ActivationIdsExhausted(C),
    /// The monotonically increasing dispatch namespace is exhausted.
    #[error("dispatch identity namespace is exhausted")]
    DispatchIdsExhausted(C),
}

/// Runtime capabilities used to interpret lifecycle effects.
///
/// Methods run without directory or slot synchronization held. Asynchronous
/// implementations should schedule owned work and later submit its typed fact
/// through the directory. Calls for one slot occur in declared effect order.
pub trait EffectInterpreter<I, C, E, L> {
    /// Start one directory-owned activation attempt.
    fn start_activation(&self, entity_id: EntityId<I>, activation_id: ActivationId);
    /// Start delivery to one exact-incarnation endpoint.
    fn deliver(
        &self,
        entity_id: EntityId<I>,
        activation_id: ActivationId,
        dispatch_id: DispatchId,
        endpoint: E,
        command: C,
    );
    /// Return a command that was not admitted or delivered.
    fn reject(&self, dispatch_id: DispatchId, command: C, reason: Refusal);
    /// Enqueue an ordered processing fence for one exact incarnation.
    fn enqueue_fence(&self, entity_id: EntityId<I>, activation_id: ActivationId, endpoint: E);
    /// Exercise exact-incarnation retirement authority.
    fn retire(
        &self,
        entity_id: EntityId<I>,
        activation_id: ActivationId,
        lease: L,
        retirement: RetirementMode,
    );
}

/// One installed lifecycle decision awaiting effect interpretation.
#[must_use = "installed lifecycle decision awaits effect interpretation via LocalDirectory::interpret"]
pub struct DirectoryOutput<I, C, E, L> {
    /// Checked evidence for the installed lifecycle decision.
    pub evidence: TransitionEvidence,
    pub(crate) activation_id: Option<ActivationId>,
    entity_id: EntityId<I>,
    target: OutputTarget<C, E, L>,
}

/// Where an installed decision's effects await interpretation.
enum OutputTarget<C, E, L> {
    /// A directory-mapped slot with queued effects.
    Mapped(Arc<Slot<C, E, L>>),
    /// Effects already resolved for an event addressed to an absent entry.
    /// Boxed: the cold variant stays off the mapped output's move path
    /// (measured: inline costs the active dispatch path ~8%).
    Transient(Box<SlotEffectBatch<C, E, L>>),
}

/// The installed decision for one dispatched command, with its guaranteed
/// correlation identity.
#[must_use = "installed lifecycle decision awaits effect interpretation via LocalDirectory::interpret"]
pub struct DispatchOutput<I, C, E, L> {
    /// Correlation identity allocated for the dispatched command.
    pub dispatch_id: DispatchId,
    /// Installed decision awaiting effect interpretation.
    pub output: DirectoryOutput<I, C, E, L>,
}

struct Slot<C, E, L> {
    lifecycle: LinearizedExecutor<LifecycleMachine<C, E, L>>,
}

struct Installed {
    evidence: TransitionEvidence,
    activation_id: Option<ActivationId>,
}

impl<C, E: Clone, L> Slot<C, E, L> {
    fn new() -> Self {
        Self {
            lifecycle: LinearizedExecutor::new(lifecycle_machine()),
        }
    }

    fn submit(&self, event: SlotEvent<C, E, L>) -> Installed {
        let (evidence, activation_id) = self.lifecycle.submit(event);
        Installed {
            evidence,
            activation_id,
        }
    }

    fn activation_id(&self) -> Option<ActivationId> {
        self.lifecycle.evidence().and_then(|evidence| evidence.1)
    }
}

type Shard<I, C, E, L> = Mutex<HashMap<EntityId<I>, Arc<Slot<C, E, L>>>>;

/// Sharded local storage for authoritative per-entity lifecycle machines.
///
/// Hashing first selects a shard before locking; [`Hash`] and [`Eq`] for `I`
/// then run during table operations while that shard lock is held and therefore
/// must not reenter this directory. Lifecycle effect callbacks run only after
/// directory synchronization is released.
pub struct LocalDirectory<I, C, E, L, S = RandomState> {
    shards: Box<[Shard<I, C, E, L>]>,
    mask: u64,
    hash_builder: S,
    waiter_limit: NonZeroUsize,
    next_activation: AtomicU64,
    next_dispatch: AtomicU64,
}

impl<I, C, E, L> LocalDirectory<I, C, E, L>
where
    I: Eq + Hash + Clone,
    E: Clone,
{
    /// Construct a directory using the standard randomized hash builder.
    ///
    /// # Errors
    ///
    /// Returns [`DirectoryError::InvalidShardCount`] unless the configured
    /// shard count is a power of two.
    pub fn new(config: DirectoryConfig) -> Result<Self, DirectoryError<C>> {
        Self::with_hasher(config, RandomState::new())
    }
}

impl<I, C, E, L, S> LocalDirectory<I, C, E, L, S>
where
    I: Eq + Hash + Clone,
    E: Clone,
    S: BuildHasher,
{
    /// Construct a directory with an explicit hash builder.
    ///
    /// # Errors
    ///
    /// Returns [`DirectoryError::InvalidShardCount`] unless the configured
    /// shard count is a power of two.
    pub fn with_hasher(
        config: DirectoryConfig,
        hash_builder: S,
    ) -> Result<Self, DirectoryError<C>> {
        if !config.shards.get().is_power_of_two() {
            return Err(DirectoryError::InvalidShardCount);
        }
        let mask = config
            .shards
            .get()
            .checked_sub(1)
            .and_then(|count| u64::try_from(count).ok())
            .ok_or(DirectoryError::InvalidShardCount)?;
        let shards = (0..config.shards.get())
            .map(|_| Mutex::new(HashMap::new()))
            .collect();
        Ok(Self {
            shards,
            mask,
            hash_builder,
            waiter_limit: config.activation_waiters,
            next_activation: AtomicU64::new(1),
            next_dispatch: AtomicU64::new(1),
        })
    }

    /// Dispatch a command through the stable slot for `entity_id`.
    ///
    /// The first caller atomically installs an activating machine. Concurrent
    /// callers use that same slot and bounded activation attempt.
    ///
    /// # Errors
    ///
    /// Returns the original command if either identity namespace is exhausted.
    ///
    /// # Panics
    ///
    /// Panics after a synchronization poison, which cannot be recovered without
    /// risking lifecycle corruption.
    pub fn dispatch(
        &self,
        entity_id: EntityId<I>,
        command: C,
    ) -> Result<DispatchOutput<I, C, E, L>, DirectoryError<C>> {
        let Some(dispatch_sequence) = allocate(&self.next_dispatch) else {
            return Err(DirectoryError::DispatchIdsExhausted(command));
        };
        let dispatch_id = DispatchId::new(dispatch_sequence);
        let shard = &self.shards[self.shard_index(&entity_id)];
        let mut entries = shard.lock().expect("directory shard lock poisoned");
        if let Some(slot) = entries.get(&entity_id) {
            let slot = Arc::clone(slot);
            drop(entries);
            let installed = slot.submit(SlotEvent::Dispatch {
                dispatch_id,
                command,
            });
            return Ok(DispatchOutput {
                dispatch_id,
                output: directory_output(
                    entity_id,
                    installed.evidence,
                    installed.activation_id,
                    OutputTarget::Mapped(slot),
                ),
            });
        }
        let Some(activation_sequence) = allocate(&self.next_activation) else {
            return Err(DirectoryError::ActivationIdsExhausted(command));
        };
        let activation_id = ActivationId::new(activation_sequence);
        let slot = Arc::new(Slot::new());
        let installed = slot.submit(SlotEvent::ClaimActivation {
            activation_id,
            dispatch_id,
            command,
            waiter_limit: self.waiter_limit,
        });
        entries.insert(entity_id.clone(), Arc::clone(&slot));
        drop(entries);
        Ok(DispatchOutput {
            dispatch_id,
            output: directory_output(
                entity_id,
                installed.evidence,
                installed.activation_id,
                OutputTarget::Mapped(slot),
            ),
        })
    }

    /// Submit successful exact-incarnation activation to the represented slot.
    pub fn activation_succeeded(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
        endpoint: E,
        lease: L,
    ) -> DirectoryOutput<I, C, E, L> {
        let event = SlotEvent::ActivationSucceeded {
            activation_id,
            endpoint,
            lease,
        };
        self.submit_or_inactive(entity_id, event)
    }

    /// Submit a failed activation attempt to the represented slot.
    pub fn activation_failed(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
    ) -> DirectoryOutput<I, C, E, L> {
        self.submit_or_inactive(entity_id, SlotEvent::ActivationFailed { activation_id })
    }

    /// Cancel one bounded activation waiter.
    pub fn cancel_waiter(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
        dispatch_id: DispatchId,
    ) -> DirectoryOutput<I, C, E, L> {
        self.submit_or_inactive(
            entity_id,
            SlotEvent::CancelWaiter {
                activation_id,
                dispatch_id,
            },
        )
    }

    /// Resolve one previously admitted exact-incarnation delivery.
    pub fn delivery_resolved(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
        failure: Option<(DispatchId, C)>,
    ) -> DirectoryOutput<I, C, E, L> {
        self.submit_or_inactive(
            entity_id,
            SlotEvent::DeliveryResolved {
                activation_id,
                failure,
            },
        )
    }

    /// Atomically close admission for an exact active incarnation.
    pub fn begin_drain(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
    ) -> DirectoryOutput<I, C, E, L> {
        self.submit_or_inactive(entity_id, SlotEvent::BeginDrain { activation_id })
    }

    /// Submit acknowledgement of the ordered processing fence.
    pub fn fence_acknowledged(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
    ) -> DirectoryOutput<I, C, E, L> {
        self.submit_or_inactive(entity_id, SlotEvent::FenceAcknowledged { activation_id })
    }

    /// Force a bounded drain to retirement with its exact failure stage.
    pub fn force_drain(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
        failure: DrainFailure,
    ) -> DirectoryOutput<I, C, E, L> {
        self.submit_or_inactive(
            entity_id,
            SlotEvent::ForceDrain {
                activation_id,
                failure,
            },
        )
    }

    /// Submit an exact-incarnation termination observation.
    pub fn terminated(
        &self,
        entity_id: &EntityId<I>,
        activation_id: ActivationId,
    ) -> DirectoryOutput<I, C, E, L> {
        self.submit_or_inactive(entity_id, SlotEvent::Terminated { activation_id })
    }

    /// Return the number of represented stable routing slots.
    ///
    /// # Panics
    ///
    /// Panics if a directory shard lock was poisoned.
    #[must_use]
    pub fn len(&self) -> usize {
        // The sum cannot overflow: every counted entry is a live heap
        // allocation, so the total is bounded far below usize::MAX.
        self.shards
            .iter()
            .map(|shard| shard.lock().expect("directory shard lock poisoned").len())
            .sum()
    }

    /// Return whether no stable routing slots are represented.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub(crate) fn current_activation(&self, entity_id: &EntityId<I>) -> Option<ActivationId> {
        self.shards[self.shard_index(entity_id)]
            .lock()
            .expect("directory shard lock poisoned")
            .get(entity_id)
            .cloned()
            .and_then(|slot| slot.activation_id())
    }

    fn submit_or_inactive(
        &self,
        entity_id: &EntityId<I>,
        event: SlotEvent<C, E, L>,
    ) -> DirectoryOutput<I, C, E, L> {
        let slot = self.shards[self.shard_index(entity_id)]
            .lock()
            .expect("directory shard lock poisoned")
            .get(entity_id)
            .cloned();
        if let Some(slot) = slot {
            let installed = slot.submit(event);
            return directory_output(
                entity_id.clone(),
                installed.evidence,
                installed.activation_id,
                OutputTarget::Mapped(slot),
            );
        }
        // No entry: reduce the event on a stack-resident machine instead of
        // allocating a slot that would be discarded after interpretation.
        submit_absent(entity_id.clone(), event)
    }

    /// Interpret all effects queued for the output's stable slot.
    ///
    /// A concurrent or reentrant call only contributes its already-queued
    /// effects; the current interpreter retains ownership until the queue is
    /// empty. Runtime callbacks execute without directory synchronization held.
    pub fn interpret<R>(&self, output: DirectoryOutput<I, C, E, L>, runtime: &R)
    where
        R: EffectInterpreter<I, C, E, L>,
    {
        let DirectoryOutput {
            entity_id, target, ..
        } = output;
        match target {
            OutputTarget::Mapped(slot) => {
                slot.lifecycle
                    .dispatch_pending(&|output: LifecycleOutput<C, E, L>| {
                        output.effects.for_each(|effect| {
                            self.apply_effect(&entity_id, Some(&slot), effect, runtime);
                        });
                    });
            }
            OutputTarget::Transient(effects) => {
                effects.for_each(|effect| self.apply_effect(&entity_id, None, effect, runtime));
            }
        }
    }

    #[inline]
    fn apply_effect<R>(
        &self,
        entity_id: &EntityId<I>,
        slot: Option<&Arc<Slot<C, E, L>>>,
        effect: SlotEffect<C, E, L>,
        runtime: &R,
    ) where
        R: EffectInterpreter<I, C, E, L>,
    {
        match effect {
            SlotEffect::StartActivation { activation_id } => {
                runtime.start_activation(entity_id.clone(), activation_id);
            }
            SlotEffect::Deliver {
                activation_id,
                dispatch_id,
                endpoint,
                command,
            } => runtime.deliver(
                entity_id.clone(),
                activation_id,
                dispatch_id,
                endpoint,
                command,
            ),
            SlotEffect::Reject {
                dispatch_id,
                command,
                reason,
            } => runtime.reject(dispatch_id, command, reason),
            SlotEffect::EnqueueFence {
                activation_id,
                endpoint,
            } => runtime.enqueue_fence(entity_id.clone(), activation_id, endpoint),
            SlotEffect::Retire {
                activation_id,
                lease,
                retirement,
            } => runtime.retire(entity_id.clone(), activation_id, lease, retirement),
            // A mapped slot never acquires a second activation, so pointer
            // identity is the exact removal authority. A transient decision
            // belongs to no mapped slot, so its Remove cannot match anything.
            SlotEffect::Remove { .. } => {
                if let Some(slot) = slot {
                    self.remove_matching(entity_id, slot);
                }
            }
        }
    }

    fn remove_matching(&self, entity_id: &EntityId<I>, slot: &Arc<Slot<C, E, L>>) {
        let mut entries = self.shards[self.shard_index(entity_id)]
            .lock()
            .expect("directory shard lock poisoned");
        if entries
            .get(entity_id)
            .is_some_and(|stored| Arc::ptr_eq(stored, slot))
        {
            entries.remove(entity_id);
        }
    }

    fn shard_index(&self, entity_id: &EntityId<I>) -> usize {
        // The mask derives from the shard count, so the masked hash always
        // fits the pointer width that allocated the shards.
        usize::try_from(self.hash_builder.hash_one(entity_id) & self.mask)
            .expect("masked hash fits usize")
    }
}

impl<C, E, L> OutputEvidence for LifecycleOutput<C, E, L> {
    type Evidence = (TransitionEvidence, Option<ActivationId>);

    fn evidence(&self) -> Self::Evidence {
        (self.evidence, self.activation_id)
    }
}

/// Allocate the next non-zero identity from a monotonic sequence.
///
/// `Relaxed` suffices by structural argument: the only requirement is
/// uniqueness, which the atomic read-modify-write modification order already
/// guarantees for any ordering. No non-atomic state is published through these
/// counters — every identity is an opaque token, and all slot state it later
/// names is synchronized by the shard and executor mutexes instead.
fn allocate(sequence: &AtomicU64) -> Option<NonZeroU64> {
    sequence
        .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
            current.checked_add(1)
        })
        .ok()
        .and_then(NonZeroU64::new)
}

/// Reduce an event addressed to an absent entry on a stack-resident machine.
#[cold]
#[inline(never)]
fn submit_absent<I, C, E: Clone, L>(
    entity_id: EntityId<I>,
    event: SlotEvent<C, E, L>,
) -> DirectoryOutput<I, C, E, L> {
    let (output, _) = lifecycle_machine().step(event);
    directory_output(
        entity_id,
        output.evidence,
        output.activation_id,
        OutputTarget::Transient(Box::new(output.effects)),
    )
}

fn directory_output<I, C, E, L>(
    entity_id: EntityId<I>,
    evidence: TransitionEvidence,
    activation_id: Option<ActivationId>,
    target: OutputTarget<C, E, L>,
) -> DirectoryOutput<I, C, E, L> {
    DirectoryOutput {
        evidence,
        activation_id,
        entity_id,
        target,
    }
}