sim_lib_journal/admission.rs
1// SPDX-License-Identifier: MPL-2.0
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
6//! Verified admission and post-commit continuation policy.
7
8use crate::{
9 Admission, Journal, JournalBackend, JournalEntry, JournalError, JournalHead, JournalObject,
10 Lease,
11};
12pub use sim_storage_port::CommitAction;
13
14/// Whether this admission invoked its post-commit continuation.
15#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16pub enum AdmissionDisposition {
17 /// A new batch committed and its continuation was invoked under exclusion.
18 CommittedActionInvoked,
19 /// The exact batch is committed; no continuation was invoked by this call.
20 AlreadyCommittedActionNotInvoked,
21}
22
23/// A committed head and this call's continuation disposition.
24///
25/// This is not proof that a domain-specific external effect succeeded. The
26/// action's caller owns that result and reconciliation of uncertain effects.
27#[derive(Clone, Debug, Eq, PartialEq)]
28pub struct GuardedAdmission {
29 /// Current committed head returned by the admission owner.
30 pub head: JournalHead,
31 /// Whether this call invoked the continuation.
32 pub disposition: AdmissionDisposition,
33}
34
35impl<B: JournalBackend> Journal<B> {
36 /// Verifies and admits a batch, invoking a short action under the same fence.
37 ///
38 /// Objects become durable before the head commit. The action runs after a
39 /// new head commit while competing head/fence mutations remain excluded.
40 /// Exact redelivery never invokes it again. Reads may be reentrant; the
41 /// action must not mutate this journal or wait for payload completion.
42 /// Any error after commit requires reconciliation, not external-effect replay.
43 pub fn publish_then(
44 &self,
45 lease: &Lease,
46 expected: Option<&JournalHead>,
47 objects: Vec<JournalObject>,
48 entries: Vec<JournalEntry>,
49 action: &mut dyn CommitAction,
50 ) -> Result<GuardedAdmission, JournalError> {
51 let admission = self.verified_admission(lease, expected, objects, entries)?;
52 self.backend.admit_then(admission, action)
53 }
54
55 pub(crate) fn verified_admission(
56 &self,
57 lease: &Lease,
58 expected: Option<&JournalHead>,
59 objects: Vec<JournalObject>,
60 entries: Vec<JournalEntry>,
61 ) -> Result<Admission, JournalError> {
62 if entries.is_empty() {
63 return Err(JournalError::EmptyBatch);
64 }
65 let before = self.backend.read_state()?;
66 crate::verify::verify_state(&before)?;
67 crate::verify::verify_batch(&before, expected, &objects, &entries)?;
68 Ok(Admission {
69 fence: lease.fence,
70 expected: expected.cloned(),
71 objects,
72 entries,
73 })
74 }
75}