Skip to main content

ff_backend_sqlite/queries/
lease.rs

1//! SQLite dialect-forked queries for lease lifecycle (renew / reclaim).
2//!
3//! Populated in Phase 2a.3 per RFC-023 ยง4.1. The SQL strings are
4//! module-level `const`s so `backend.rs` call sites reference them
5//! by name and cross-dialect review lines them up against the PG
6//! reference at `ff-backend-postgres/src/attempt.rs` statement-by-
7//! statement.
8//!
9//! # Fence-triple contract
10//!
11//! Same as `queries/attempt.rs`: `BEGIN IMMEDIATE` on SQLite escalates
12//! the txn to RESERVED for the full read-modify-write window; the
13//! fence CAS is a plain SELECT of `ff_attempt.lease_epoch` (see
14//! `queries/attempt.rs::SELECT_ATTEMPT_EPOCH_SQL`).
15
16/// Advance the lease expiry for a live attempt. Caller has already
17/// fence-checked the handle's epoch against the row. The epoch is NOT
18/// bumped โ€” renewal keeps the same epoch per the PG reference at
19/// `ff-backend-postgres/src/attempt.rs:446-461`.
20pub(crate) const UPDATE_ATTEMPT_RENEW_SQL: &str = r#"
21    UPDATE ff_attempt
22       SET lease_expires_at_ms = ?1
23     WHERE partition_key = ?2 AND execution_id = ?3 AND attempt_index = ?4
24"#;
25
26/// Look up the latest attempt row for a reclaim. Returns
27/// `(attempt_index, lease_epoch, lease_expires_at_ms)`; the
28/// caller checks expiry (NULL or `<= now`) before minting a fresh
29/// handle. Mirror of PG at
30/// `ff-backend-postgres/src/attempt.rs:294-308` โ€” without
31/// `FOR UPDATE` because `BEGIN IMMEDIATE` already holds RESERVED.
32pub(crate) const SELECT_LATEST_ATTEMPT_FOR_RECLAIM_SQL: &str = r#"
33    SELECT attempt_index, lease_epoch, lease_expires_at_ms
34      FROM ff_attempt
35     WHERE partition_key = ?1 AND execution_id = ?2
36     ORDER BY attempt_index DESC
37     LIMIT 1
38"#;
39
40/// Bump the epoch, rotate worker identity, install a fresh lease
41/// expiry, and clear the outcome so the attempt is live again.
42/// Mirror of PG at `ff-backend-postgres/src/attempt.rs:332-353`.
43pub(crate) const UPDATE_ATTEMPT_RECLAIM_SQL: &str = r#"
44    UPDATE ff_attempt
45       SET worker_id = ?1,
46           worker_instance_id = ?2,
47           lease_epoch = lease_epoch + 1,
48           lease_expires_at_ms = ?3,
49           started_at_ms = ?4,
50           outcome = NULL
51     WHERE partition_key = ?5 AND execution_id = ?6 AND attempt_index = ?7
52"#;
53
54/// Flip `ff_exec_core` back to active/leased for a reclaimed attempt.
55/// Mirror of PG at `ff-backend-postgres/src/attempt.rs:355-369`.
56pub(crate) const UPDATE_EXEC_CORE_RECLAIM_SQL: &str = r#"
57    UPDATE ff_exec_core
58       SET lifecycle_phase = 'active',
59           ownership_state = 'leased',
60           eligibility_state = 'not_applicable',
61           attempt_state = 'running_attempt'
62     WHERE partition_key = ?1 AND execution_id = ?2
63"#;