saga.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //! Legend saga step adapters for the ledger.
  2. //!
  3. //! Provides [`Step`] implementations so the ledger can participate
  4. //! in multi-resource saga workflows, with automatic LIFO compensation across
  5. //! resource boundaries.
  6. //!
  7. //! # Envelope pipeline saga
  8. //!
  9. //! A commit is two saga steps over a pre-resolved [`Envelope`] (resolution runs
  10. //! before the saga, in `Ledger::commit`):
  11. //!
  12. //! 1. **ReservePostingsStep** -- `reserve_postings`: Active → PendingInactive, stamped with the saga's `ReservationId`; interprets the count via `verify_postings`.
  13. //! 2. **FinalizeTransferStep** -- delegates to `Ledger::finalize_envelope`, which re-validates against current state (the last-step floor / freeze-close guard), marks the saga `Finalizing`, then runs the dumb primitives (`deactivate_postings` → `insert_postings` → `store_transfer` → `append_event`) verifying every end-state.
  14. //!
  15. //! The `EnvelopeSaga` is defined via `legend!` in `ledger.rs` and driven by
  16. //! `commit_envelope()`. Crash recovery (`Ledger::recover`) re-completes a
  17. //! persisted saga using its persisted phase: a `Reserving` saga is re-run
  18. //! (re-validating); a `Finalizing` saga is rolled forward through the same
  19. //! verified `finalize_envelope`.
  20. //!
  21. //! # High-level composition
  22. //!
  23. //! High-level steps (`PayMovementStep` and `DepositMovementStep`) compose over
  24. //! the intent-layer API and can be combined into multi-transfer sagas via `legend!`.
  25. use std::sync::Arc;
  26. use async_trait::async_trait;
  27. use legend::step::{CompensationOutcome, RetryPolicy, Step, StepOutcome};
  28. use serde::{Deserialize, Serialize};
  29. use tracing::Instrument;
  30. use kuatia_core::{
  31. AccountId, AssetId, Cent, Envelope, Posting, PostingId, PostingStatus, Receipt, ReservationId,
  32. TransferBuilder,
  33. };
  34. use crate::error::LedgerError;
  35. use crate::ledger::Ledger;
  36. use kuatia_storage::store::Store;
  37. /// Interpret a dumb primitive's affected-row `count` against the `ids` it
  38. /// targeted. `count == ids.len()` is success. A short count is acceptable only if
  39. /// the shortfall is already in the desired end-state — a prior attempt (or this
  40. /// saga, replayed by recovery) already applied it — verified by reading the
  41. /// postings and checking `ok`. Otherwise it is a genuine failure (contended or
  42. /// concurrently modified) and the saga compensates.
  43. async fn verify_postings(
  44. store: &dyn Store,
  45. ids: &[PostingId],
  46. count: u64,
  47. ok: impl Fn(&Posting) -> bool,
  48. what: &str,
  49. ) -> Result<(), SagaError> {
  50. if count == ids.len() as u64 {
  51. return Ok(());
  52. }
  53. let postings = store
  54. .get_postings(ids)
  55. .await
  56. .map_err(|e| SagaError::from(LedgerError::Store(e)))?;
  57. if postings.len() == ids.len() && postings.iter().all(&ok) {
  58. return Ok(());
  59. }
  60. Err(SagaError {
  61. message: format!(
  62. "{what}: storage applied {count}/{} rows and the end-state is not satisfied",
  63. ids.len()
  64. ),
  65. })
  66. }
  67. // ---------------------------------------------------------------------------
  68. // Saga error -- serializable + cloneable wrapper
  69. // ---------------------------------------------------------------------------
  70. /// Serializable error wrapper used across saga steps.
  71. #[derive(Debug, Clone, Serialize, Deserialize)]
  72. pub struct SagaError {
  73. /// Human-readable error description.
  74. pub message: String,
  75. }
  76. impl std::fmt::Display for SagaError {
  77. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  78. write!(f, "{}", self.message)
  79. }
  80. }
  81. impl std::error::Error for SagaError {}
  82. impl From<LedgerError> for SagaError {
  83. fn from(e: LedgerError) -> Self {
  84. Self {
  85. message: e.to_string(),
  86. }
  87. }
  88. }
  89. // ---------------------------------------------------------------------------
  90. // Saga context -- carries the ledger handle + state between steps
  91. // ---------------------------------------------------------------------------
  92. /// Saga context that wraps a ledger and tracks state across steps.
  93. ///
  94. /// The ledger handle is `#[serde(skip)]` -- after deserializing a paused
  95. /// execution you must call [`inject_ledger`](LedgerCtx::inject_ledger)
  96. /// before resuming.
  97. #[derive(Clone, Serialize, Deserialize)]
  98. pub struct LedgerCtx {
  99. /// Receipts collected from completed steps.
  100. pub receipts: Vec<Receipt>,
  101. /// Posting ids reserved so far (for compensation).
  102. pub reserved_postings: Vec<PostingId>,
  103. /// Resolved envelope produced by the resolve step.
  104. pub envelope: Option<Envelope>,
  105. /// Reservation owner token for this saga's reserved postings. Serialized so
  106. /// it survives pause/recovery, keeping ownership stable across restarts.
  107. pub reservation: ReservationId,
  108. #[serde(skip)]
  109. ledger: Option<Arc<Ledger>>,
  110. }
  111. impl std::fmt::Debug for LedgerCtx {
  112. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  113. f.debug_struct("LedgerCtx")
  114. .field("receipts", &self.receipts)
  115. .field("reserved_postings", &self.reserved_postings.len())
  116. .field("has_envelope", &self.envelope.is_some())
  117. .field("ledger_present", &self.ledger.is_some())
  118. .finish()
  119. }
  120. }
  121. impl LedgerCtx {
  122. /// Create a new context wrapping the given ledger.
  123. pub fn new(ledger: Arc<Ledger>) -> Self {
  124. Self {
  125. receipts: Vec::new(),
  126. reserved_postings: Vec::new(),
  127. envelope: None,
  128. reservation: ReservationId::default(),
  129. ledger: Some(ledger),
  130. }
  131. }
  132. /// Create a context for the envelope pipeline (reserve → finalize; finalize re-validates)
  133. /// with a pre-resolved envelope and an explicit reservation.
  134. pub fn for_envelope(
  135. ledger: Arc<Ledger>,
  136. envelope: Envelope,
  137. reservation: ReservationId,
  138. ) -> Self {
  139. Self {
  140. receipts: Vec::new(),
  141. reserved_postings: Vec::new(),
  142. envelope: Some(envelope),
  143. reservation,
  144. ledger: Some(ledger),
  145. }
  146. }
  147. /// Re-inject the ledger handle after deserializing a paused execution.
  148. pub fn inject_ledger(&mut self, ledger: Arc<Ledger>) {
  149. self.ledger = Some(ledger);
  150. }
  151. /// Borrow the ledger, returning an error if not injected.
  152. pub fn ledger(&self) -> Result<&Ledger, SagaError> {
  153. self.ledger.as_ref().map(|l| l.as_ref()).ok_or(SagaError {
  154. message: "ledger not injected -- call inject_ledger() after deserializing".into(),
  155. })
  156. }
  157. /// Clone the ledger `Arc`, returning an error if not injected.
  158. pub fn ledger_arc(&self) -> Result<Arc<Ledger>, SagaError> {
  159. self.ledger.clone().ok_or(SagaError {
  160. message: "ledger not injected -- call inject_ledger() after deserializing".into(),
  161. })
  162. }
  163. }
  164. // ===========================================================================
  165. // Envelope pipeline steps (reserve -> finalize; resolve runs before the saga, validate inside finalize)
  166. // ===========================================================================
  167. // ---------------------------------------------------------------------------
  168. // Step 1: ReservePostingsStep
  169. // ---------------------------------------------------------------------------
  170. /// Input for the reserve step (posting ids come from ctx.envelope).
  171. #[derive(Debug, Clone, Serialize, Deserialize)]
  172. pub struct ReserveInput;
  173. /// Reserves consumed postings by CAS: Active to PendingInactive.
  174. ///
  175. /// Gets the posting ids from the resolved envelope in the context.
  176. /// Compensation releases all reserved postings back to Active.
  177. pub struct ReservePostingsStep;
  178. #[async_trait]
  179. impl Step<LedgerCtx, SagaError> for ReservePostingsStep {
  180. type Input = ReserveInput;
  181. async fn execute(ctx: &mut LedgerCtx, _input: &ReserveInput) -> Result<StepOutcome, SagaError> {
  182. async {
  183. let posting_ids: Vec<PostingId> = ctx
  184. .envelope
  185. .as_ref()
  186. .ok_or(SagaError {
  187. message: "no envelope in context -- resolve step must run first".into(),
  188. })?
  189. .consumes()
  190. .to_vec();
  191. let rid = ctx.reservation;
  192. let ledger = ctx.ledger_arc()?;
  193. let store = ledger.store();
  194. let reserved = store
  195. .reserve_postings(&posting_ids, rid)
  196. .await
  197. .map_err(|e| SagaError::from(LedgerError::Store(e)))?;
  198. // Storage reports the count; the saga decides. A short count is fine
  199. // only if the shortfall is already reserved by us (idempotent replay).
  200. verify_postings(
  201. store,
  202. &posting_ids,
  203. reserved,
  204. |p| p.status == PostingStatus::PendingInactive && p.reservation == Some(rid),
  205. "reserve",
  206. )
  207. .await?;
  208. ctx.reserved_postings.extend_from_slice(&posting_ids);
  209. Ok(StepOutcome::Continue)
  210. }
  211. .instrument(tracing::info_span!("saga_step", step = "reserve"))
  212. .await
  213. }
  214. async fn compensate(
  215. ctx: &mut LedgerCtx,
  216. _input: &ReserveInput,
  217. ) -> Result<CompensationOutcome, SagaError> {
  218. ctx.ledger()?
  219. .store()
  220. .release_postings(&ctx.reserved_postings, ctx.reservation)
  221. .await
  222. .map_err(|e| SagaError::from(LedgerError::Store(e)))?;
  223. ctx.reserved_postings.clear();
  224. Ok(CompensationOutcome::Completed)
  225. }
  226. fn retry_policy() -> RetryPolicy {
  227. RetryPolicy::retries(3)
  228. }
  229. }
  230. // ---------------------------------------------------------------------------
  231. // Step 2: FinalizeTransferStep
  232. // ---------------------------------------------------------------------------
  233. /// Input for the finalize step (envelope comes from ctx).
  234. #[derive(Debug, Clone, Serialize, Deserialize)]
  235. pub struct FinalizeInput;
  236. /// Re-validates against current state (the last-step floor / freeze-close guard),
  237. /// then drives the verified, idempotent commit via `Ledger::finalize_envelope`.
  238. ///
  239. /// Compensation reverses the finalized envelope (only relevant once committed).
  240. pub struct FinalizeTransferStep;
  241. #[async_trait]
  242. impl Step<LedgerCtx, SagaError> for FinalizeTransferStep {
  243. type Input = FinalizeInput;
  244. async fn execute(
  245. ctx: &mut LedgerCtx,
  246. _input: &FinalizeInput,
  247. ) -> Result<StepOutcome, SagaError> {
  248. async {
  249. let envelope = ctx.envelope.clone().ok_or(SagaError {
  250. message: "no envelope in context -- resolve step must run first".into(),
  251. })?;
  252. let rid = ctx.reservation;
  253. let ledger = ctx.ledger_arc()?;
  254. // All commit work (re-validate, mark Finalizing, deactivate/insert/
  255. // store/event with end-state verification) lives in `finalize_envelope`
  256. // so recovery uses exactly the same path.
  257. let receipt = ledger
  258. .finalize_envelope(&envelope, rid)
  259. .await
  260. .map_err(SagaError::from)?;
  261. ctx.receipts.push(receipt);
  262. ctx.reserved_postings.clear();
  263. Ok(StepOutcome::Continue)
  264. }
  265. .instrument(tracing::info_span!("saga_step", step = "finalize"))
  266. .await
  267. }
  268. async fn compensate(
  269. ctx: &mut LedgerCtx,
  270. _input: &FinalizeInput,
  271. ) -> Result<CompensationOutcome, SagaError> {
  272. if let Some(receipt) = ctx.receipts.pop() {
  273. ctx.ledger_arc()?.reverse(&receipt.transfer_id).await?;
  274. }
  275. Ok(CompensationOutcome::Completed)
  276. }
  277. fn retry_policy() -> RetryPolicy {
  278. RetryPolicy::retries(3)
  279. }
  280. }
  281. // ===========================================================================
  282. // High-level steps (pay / deposit movement steps)
  283. // ===========================================================================
  284. /// Input for the pay movement saga step.
  285. #[derive(Debug, Clone, Serialize, Deserialize)]
  286. pub struct PayInput {
  287. /// Source account.
  288. pub from: AccountId,
  289. /// Destination account.
  290. pub to: AccountId,
  291. /// Asset to transfer.
  292. pub asset: AssetId,
  293. /// Amount to transfer.
  294. pub amount: Cent,
  295. }
  296. /// Input for the deposit movement saga step.
  297. #[derive(Debug, Clone, Serialize, Deserialize)]
  298. pub struct DepositInput {
  299. /// Account receiving the deposit.
  300. pub to: AccountId,
  301. /// Asset being deposited.
  302. pub asset: AssetId,
  303. /// Amount to deposit.
  304. pub amount: Cent,
  305. /// External account funding the deposit.
  306. pub external: AccountId,
  307. }
  308. // ---------------------------------------------------------------------------
  309. // Helpers
  310. // ---------------------------------------------------------------------------
  311. async fn compensate_last_receipt(ctx: &mut LedgerCtx) -> Result<CompensationOutcome, SagaError> {
  312. let receipt = ctx.receipts.pop().ok_or(SagaError {
  313. message: "no receipt to compensate".into(),
  314. })?;
  315. ctx.ledger_arc()?.reverse(&receipt.transfer_id).await?;
  316. Ok(CompensationOutcome::Completed)
  317. }
  318. // ---------------------------------------------------------------------------
  319. // Steps
  320. // ---------------------------------------------------------------------------
  321. /// Saga step: pay between two accounts via a single-movement transfer.
  322. pub struct PayMovementStep;
  323. #[async_trait]
  324. impl Step<LedgerCtx, SagaError> for PayMovementStep {
  325. type Input = PayInput;
  326. async fn execute(ctx: &mut LedgerCtx, input: &PayInput) -> Result<StepOutcome, SagaError> {
  327. let ledger = ctx.ledger_arc()?;
  328. let transfer = TransferBuilder::new()
  329. .pay(input.from, input.to, input.asset, input.amount)
  330. .build();
  331. let receipt = ledger.commit(transfer).await?;
  332. ctx.receipts.push(receipt);
  333. Ok(StepOutcome::Continue)
  334. }
  335. async fn compensate(
  336. ctx: &mut LedgerCtx,
  337. _input: &PayInput,
  338. ) -> Result<CompensationOutcome, SagaError> {
  339. compensate_last_receipt(ctx).await
  340. }
  341. }
  342. /// Saga step: deposit value from an external account via a single-movement transfer.
  343. pub struct DepositMovementStep;
  344. #[async_trait]
  345. impl Step<LedgerCtx, SagaError> for DepositMovementStep {
  346. type Input = DepositInput;
  347. async fn execute(ctx: &mut LedgerCtx, input: &DepositInput) -> Result<StepOutcome, SagaError> {
  348. let ledger = ctx.ledger_arc()?;
  349. let transfer = TransferBuilder::new()
  350. .deposit(input.to, input.asset, input.amount, input.external)
  351. .map_err(|e| SagaError::from(LedgerError::from(e)))?
  352. .build();
  353. let receipt = ledger.commit(transfer).await?;
  354. ctx.receipts.push(receipt);
  355. Ok(StepOutcome::Continue)
  356. }
  357. async fn compensate(
  358. ctx: &mut LedgerCtx,
  359. _input: &DepositInput,
  360. ) -> Result<CompensationOutcome, SagaError> {
  361. compensate_last_receipt(ctx).await
  362. }
  363. }