diff --git a/src/constant.rs b/src/constant.rs index f0819d6..6fd3269 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -231,6 +231,18 @@ pub const MAX_LEDGER_AMOUNT_MINOR: i64 = 10_000_000; pub const MAX_LEDGER_METHOD_LEN: usize = 50; pub const MAX_LEDGER_NOTE_LEN: usize = 500; +/// How many times one seat may be taken on one menu: the first booking plus +/// nine re-bookings after a cancel. +/// +/// A cap on *storage*, not on indecision. Every cycle appends two permanent, +/// undeletable ledger lines — the charge and its reversal, both keyed to the +/// attempt — and nothing else bounded them: at the API's own request ceiling +/// one account could grow its statement by hundreds of thousands of rows a +/// day, which every later balance read then answers for. Ten is far past what +/// a family changing its mind about lunch needs, and a menu whose seat really +/// must move again is the canteen's to cancel. +pub const MAX_MEAL_BOOKING_ATTEMPTS: i64 = 10; + /// Ceiling on the settings knob that closes booking (and cancelling) ahead of /// a meal — one week. The knob itself is optional: absent means no cutoff. pub const MAX_MEAL_CANCEL_CUTOFF_MINUTES: i64 = 7 * 24 * 60; @@ -241,8 +253,10 @@ pub const MAX_MEAL_CANCEL_CUTOFF_MINUTES: i64 = 7 * 24 * 60; /// /// **The clock is UTC.** This backend deliberately stores no school timezone /// (rejected feature), so staff enter the serving time in UTC: a UTC+3 school -/// types `540` (09:00) to mean noon locally. Optional per slot — a slot -/// without one keeps the old behaviour, midnight UTC of the menu's date. +/// types `540` (09:00) to mean noon locally. Optional per slot — and a slot +/// without one has **no cutoff at all**, since there is no instant to count a +/// deadline back from; the cutoff knob starts binding that slot the day the +/// school sets its hour. pub const MAX_MEAL_SERVING_MINUTE: i64 = 24 * 60 - 1; /// The only accepted course kinds. `course`: a regular class (ders). `study`: @@ -298,11 +312,27 @@ pub const MAX_FEE_PLAN_NAME_LEN: usize = 120; pub const MAX_FEE_PLAN_INSTALLMENTS: usize = 60; /// How many students one `POST /payments/plans/{id}/assignments` may name. -/// Bulk placement is a whole class at a time, not the whole school: each named -/// student appends every installment of the plan, so this is what bounds one -/// request's writes. +/// Bulk placement is a whole class at a time, not the whole school. pub const MAX_FEE_PLAN_ASSIGN_STUDENTS: usize = 200; +/// How many charge lines one assignment request may append: the students it +/// names times the plan's installments. The student cap alone cannot see the +/// schedule — 200 students on a 60-installment plan is 12 000 sequential +/// writes in one HTTP request, and nothing in the stack times a request out. +/// Three thousand leaves the ordinary shapes whole (200 students up to a +/// 15-installment plan, 50 students on the largest plan there is) and the +/// refusal tells the caller to split the batch. +pub const MAX_FEE_PLAN_ASSIGN_WRITES: usize = 3_000; + +/// How many lines may be applied to one ledger line — the payments under a +/// charge, the refunds under a payment, and every reversal among them. The +/// over-payment cap folds that whole subtree one query per line while holding +/// the process-global payment lock, so an unbounded subtree is an unbounded +/// stall for every other payment in the school: 2 000 one-kuruş payments make +/// the next one issue 2 001 queries with the lock held. Twenty pieces is +/// already a pathological way to settle a single installment. +pub const MAX_LEDGER_APPLIED_LINES: usize = 20; + /// Ceiling on the client-chosen `request_key` that makes a credit or a refund /// retry-safe: it becomes part of the ledger line's record id, so it is bounded /// and drawn from `[A-Za-z0-9-]` — `_` joins the parts of a ledger id, so a key @@ -851,6 +881,14 @@ pub const FEE_PLAN_UNASSIGNED_GUARD: &str = "(assignment_count ?? 0) = 0"; pub const NOTE_FILE_COUNT_FIELD: &str = "file_count"; pub const SUBMISSION_FILE_COUNT_FIELD: &str = "file_count"; pub const CHATBOT_THREAD_COUNT_FIELD: &str = "chatbot_thread_count"; +/// The column a *grant* claim moves and puts back, so its transaction writes +/// the holder's own `user` record — the one key +/// [`crate::domain::user::User::set_role`] writes (see +/// [`crate::domain::cap::role_claim`]). Any `option` on the row would do: +/// the claim nets zero and never reads it, so this is an alias rather than a +/// column of its own — a new one would mean a migration on a SCHEMAFULL table +/// for a value nothing ever observes. +pub const USER_ROLE_CLAIM_FIELD: &str = CHATBOT_THREAD_COUNT_FIELD; /// How many boards this user created, on the user row — the same per-user shape /// as `CHATBOT_THREAD_COUNT_FIELD`, capped at `MAX_BOARDS_PER_CREATOR`. It is /// what closes the "open another board" way around the two board counters @@ -865,9 +903,13 @@ pub const USER_BOARD_COUNT_FIELD: &str = "board_count"; /// submit/delete/submit), and a teacher un-grading gives back /// `MARKS_GIVEN` — with `HIGH_MARK` on the exam side, credited to the student /// by the same act (else one exam becomes fifty by grade/ungrade/regrade). -/// The exam and pomodoro totals never decrease at all: there is no delete -/// behind them but a teacher's, so sitting an exam or finishing a stint is a -/// lifetime fact. Nothing else decrements: the cascades (an exam, a homework or +/// The exam and pomodoro totals never decrease at all, and each earns that by +/// counting something a repeat cannot re-earn rather than by anyone's restraint: +/// `EXAM_SAT_TOTAL` counts *exams sat*, moving only on a student's first sitting +/// of an exam (`seq == 1`), because retakes are a loop the student drives alone — +/// an open exam with unlimited attempts is start/finish/start, no teacher in it — +/// and a stint is a span of time that has to be lived through to be finished. +/// Nothing else decrements: the cascades (an exam, a homework or /// a course delete, which do take the attempt and result rows with them) leave /// them alone, since history a teacher erased is still history the student /// lived. A badge already earned is never taken back either, whatever a counter diff --git a/src/domain/cap.rs b/src/domain/cap.rs index bea97b9..93d9101 100644 --- a/src/domain/cap.rs +++ b/src/domain/cap.rs @@ -22,16 +22,19 @@ //! (every parent's `.content()` write is a create of a fresh row). // // ponytail: a counter can only drift if a future delete path forgets its -// decrement, and the boot backfill seeds counters once rather than recomputing -// them (recomputing on every boot would clobber a live peer's increments). If -// drift is ever observed, the repair is the same GROUP BY count the backfill -// runs, issued by hand with the field's `= NONE` guard dropped. +// decrement, and the boot backfill seeds most counters once rather than +// recomputing them. `enrollment_count` is the one that already drifted (the boot +// sweep of promoted users' rows kept their seats), so it is recomputed on every +// boot instead — see the repair in `migration_sql::BACKFILL`. That is the shape +// to copy for any other counter drift observed: a counter whose meaning is +// exactly "the live child rows" converges on a recompute, a lifetime tally does +// not and must stay seeded once. use surrealdb::types::{RecordId, SurrealValue}; use tokio::sync::Mutex; -use crate::constant::{CAP_WRITE_TRIES, REF_COUNT_FIELD, REF_RETIRED_FIELD}; -use crate::database::{Database, backoff, lost_the_race}; +use crate::constant::{CAP_WRITE_TRIES, REF_COUNT_FIELD, REF_RETIRED_FIELD, USER_ROLE_CLAIM_FIELD}; +use crate::database::{Database, backoff, lost_the_race, transaction_with_retry}; use crate::error::AppError; /// One counter writer at a time, over every counter. @@ -160,7 +163,34 @@ pub(crate) async fn claim_and_create( content: &T, db: &Database, ) -> Result, AppError> { - claim_at_and_create(parent, field, "$num", cap, "", id, content, db).await + claim_at_and_create(parent, field, "$num", cap, "", 1, None, id, content, db).await +} + +/// [`claim_and_create`] for a counter only *some* of a parent's children move: +/// the row lands either way, and `counts` decides whether the tally follows it. +/// +/// The one live user is `exam_sat_total`, which counts exams sat rather than +/// sittings — a retake writes a row and moves nothing. Splitting that into +/// "count and create" versus a bare create outside this module would put the +/// uncounted write on a different statement to the counted one, and a +/// deterministic child id needs the duplicate abort either way: whichever +/// branch runs, exactly one writer's `CREATE` commits, and only that one's +/// increment survives with it. `counts` false still writes the seat statement +/// (`+ 0`), so a missing parent row is refused as [`Claimed::Full`] on both +/// paths rather than silently writing a child of nobody. +pub(crate) async fn create_counting( + parent: &RecordId, + field: &str, + counts: bool, + id: &RecordId, + content: &T, + db: &Database, +) -> Result, AppError> { + let bump = i64::from(counts); + claim_at_and_create( + parent, field, "$num", UNLIMITED, "", bump, None, id, content, db, + ) + .await } /// [`claim_and_create`] against a cap the *statement* reads rather than one the @@ -177,16 +207,27 @@ pub(crate) async fn claim_and_create( /// the write that lowered it and the write that reads it contend on one record. /// /// `cap_expr` is always an in-crate SQL literal, never text from a client. +/// +/// `holder` is the child's *other* parent when that parent is a user whose live +/// role decides whether the row may exist at all: `(the user record, the role +/// it may not carry)`, claimed by [`role_claim`] in this same transaction. A +/// refusal from it arrives as [`Claimed::Full`], which already means "full, or +/// the guard failed, or the parent is gone" — the caller re-reads to pick the +/// message, and only on that path. pub(crate) async fn claim_live_and_create( parent: &RecordId, field: &str, cap_expr: &str, fallback: i64, + holder: Option<(&RecordId, &str)>, id: &RecordId, content: &T, db: &Database, ) -> Result, AppError> { - claim_at_and_create(parent, field, cap_expr, fallback, "", id, content, db).await + claim_at_and_create( + parent, field, cap_expr, fallback, "", 1, holder, id, content, db, + ) + .await } /// [`claim_and_create`], but only while `guard` — an extra predicate on that @@ -212,7 +253,7 @@ pub(crate) async fn claim_when_and_create( content: &T, db: &Database, ) -> Result, AppError> { - claim_at_and_create(parent, field, "$num", cap, guard, id, content, db).await + claim_at_and_create(parent, field, "$num", cap, guard, 1, None, id, content, db).await } #[allow(clippy::too_many_arguments)] @@ -222,6 +263,8 @@ async fn claim_at_and_create( cap_expr: &str, cap: i64, extra: &str, + bump: i64, + holder: Option<(&RecordId, &str)>, id: &RecordId, content: &T, db: &Database, @@ -231,11 +274,23 @@ async fn claim_at_and_create( } else { format!(" AND ({extra})") }; + // The holder's claim rides between the duplicate gate and the seat, so a + // caller whose row a rival placed is still told "you already have it", and + // a demotion outranks a full cap: `Full` on a list with room left sends + // nobody looking for a seat that is not the problem. + let held_by = holder.map_or_else(Vec::new, |(_, unfit)| { + role_claim("holder", unfit, FULL_MARK) + }); + let hold = if held_by.is_empty() { + String::new() + } else { + format!("{};\n", held_by.join(";\n")) + }; let sql = format!( "BEGIN TRANSACTION; LET $held = (SELECT VALUE id FROM $id); IF array::len($held) > 0 {{ THROW '{HELD_MARK}' }}; - LET $seat = (UPDATE $parent SET {field} = ({field} ?? 0) + 1 \ + {hold}LET $seat = (UPDATE $parent SET {field} = ({field} ?? 0) + $bump \ WHERE ({field} ?? 0) < ({cap_expr}){extra} RETURN VALUE id); IF array::len($seat) = 0 {{ THROW '{FULL_MARK}' }}; CREATE $id CONTENT $row; @@ -245,13 +300,17 @@ async fn claim_at_and_create( let mut last = None; for attempt in 0..CAP_WRITE_TRIES { backoff(attempt).await; - let attempted = db + let mut query = db .query(sql.as_str()) .bind(("parent", parent.clone())) .bind(("num", cap)) + .bind(("bump", bump)) .bind(("id", id.clone())) - .bind(("row", content.clone())) - .await; + .bind(("row", content.clone())); + if let Some((record, _)) = holder { + query = query.bind(("holder", record.clone())); + } + let attempted = query.await; let mut result = match attempted { Ok(result) => result, Err(err) if lost_the_race(&err) => { @@ -289,9 +348,13 @@ async fn claim_at_and_create( if let Some(error) = errors.drain().map(|(_, error)| error).next() { return Err(error.into()); } - // Slots count BEGIN, two LETs and two IFs: the CREATE is slot 5. + // Slots count BEGIN, two LETs and two IFs: the CREATE is slot 5, plus + // the holder claim's own statements when one is carried — counted, not + // tallied by hand, so a statement added there cannot read back the + // wrong result. + let slot = 5 + held_by.len(); return result - .take::>(5)? + .take::>(slot)? .into_iter() .next() .map(Claimed::Made) @@ -302,6 +365,134 @@ async fn claim_at_and_create( .unwrap_or_else(|| AppError::Internal("cap counter write never ran".into()))) } +/// The marker [`touch_and_create`] aborts with: the parent row is gone. +const GONE_MARK: &str = "cap_gone"; + +/// Write a child of `parent` while *colliding* with anything that removes the +/// parent — no seat spent, only the proof. `Ok(None)` = the parent is gone and +/// nothing was written. +/// +/// Reading the parent first and then inserting cannot promise this, and neither +/// can a `SELECT` inside the transaction: SurrealDB 3.2.3 conflict-checks write +/// sets, not read sets, so a create landing inside a parent delete's window +/// reads a row that is still there (removed, uncommitted) while the delete's +/// `DELETE WHERE parent = $parent` sweep already ran on a snapshot +/// without this row — both commit, and the child outlives its parent +/// unreachably, because every route to it goes through the parent. +/// +/// So the proof is a *write* on the parent's own record, which is the key the +/// delete writes. `field` is moved — bumped and put back by captured value, +/// `NONE` included, so the row is byte-identical afterwards — because an +/// `UPDATE` that leaves the document unchanged is elided and never enters the +/// write set: `SET x = x` sits on no key at all and collides with nothing +/// (measured on 3.2.3, and the reason +/// [`crate::domain::class_pump::Axis::pivot_claim`] and +/// [`crate::domain::exam_answer::ExamAnswer::save`] have the same shape). Both +/// statements are inside the transaction, so an abort between them cannot leave +/// the counter up. +/// +/// It lives here because it is [`claim_and_create`] with the cap removed: the +/// same single-record conditional write on the parent, asked "are you still +/// there?" instead of "is there room?". No [`CLAIM_LOCK`] either — the counter +/// nets zero, so no cap depends on the order these arrive in. +/// +/// Admissible for [`crate::database::transaction_with_retry`]: `UPDATE`s and a +/// `CREATE` whose id is a freshly minted ULID no rival can aim at, so no +/// statement can legitimately answer "already exists" and a lost round wrote +/// nothing. +pub(crate) async fn touch_and_create( + parent: &RecordId, + field: &str, + id: &RecordId, + content: &T, + db: &Database, +) -> Result, AppError> { + let sql = format!( + "BEGIN TRANSACTION; + LET $was = (SELECT VALUE {field} FROM ONLY $parent); + LET $alive = (UPDATE $parent SET {field} = ({field} ?? 0) + 1 RETURN VALUE id); + IF array::len($alive) = 0 {{ THROW '{GONE_MARK}' }}; + UPDATE $parent SET {field} = $was; + LET $made = (CREATE $id CONTENT $row RETURN AFTER); + RETURN $made[0]; + COMMIT TRANSACTION;" + ); + let (mut result, mut errors) = transaction_with_retry( + db, + &sql, + &[ + ("parent".into(), parent.clone().into_value()), + ("id".into(), id.clone().into_value()), + ("row".into(), content.clone().into_value()), + ], + &[GONE_MARK], + ) + .await?; + // An aborted transaction errors *every* slot, most with a generic "not + // executed" — only the THROW's own slot names the marker. + if errors + .values() + .any(|error| error.to_string().contains(GONE_MARK)) + { + return Ok(None); + } + if let Some(error) = errors.drain().map(|(_, error)| error).next() { + return Err(error.into()); + } + // The trailing `RETURN` is the last statement before `COMMIT`, so its slot + // follows the statement count rather than a hand-kept number; + // `num_statements` counts BEGIN and COMMIT. + let slot = result.num_statements().saturating_sub(2); + result + .take::>(slot)? + .into_iter() + .next() + .map(Some) + .ok_or_else(|| AppError::Internal("the parent touch wrote no row".into())) +} + +/// The statements that put a transaction's write on a **user's own record** +/// while the role that row still carries may hold the grant being written — +/// [`touch_and_create`]'s trick pointed at the one key +/// [`crate::domain::user::User::set_role`] writes. +/// +/// WHY: a demotion sheds every grant the old role implied, and it does that in +/// the role write's own transaction — but its sweeps are *snapshots* +/// (`DELETE WHERE user = $usr`), and SurrealDB 3.2.3 conflict-checks +/// write sets, not read sets. A seat, an enrollment or a class membership +/// created after that snapshot therefore survives the sweep, and nothing ever +/// re-sweeps: the grant stands forever under a role that may not hold it. Every +/// one of those creates writes its *other* parent (the event, the course, the +/// class), so it shares no key with the demotion and the store has nothing to +/// settle. +/// +/// So the counter named by [`USER_ROLE_CLAIM_FIELD`] is **moved** — bumped and +/// put back by captured value, `NONE` included, so the row is byte-identical +/// afterwards — because re-stating a value claims nothing: an `UPDATE` that +/// leaves the document unchanged is elided and enters no write set. The same +/// `UPDATE` hands back the role it found, so one statement is both the write +/// that collides and the read that decides. Either the sweep sees this row, or +/// this statement sees the demotion; and the caller that *loses* the conflict +/// re-sends into a fresh transaction that reads the new role, which is what +/// keeps the retry from turning a refusal into a success. +/// +/// A user row that is not there claims nothing and refuses nothing: the +/// `UPDATE` matches no key, and every caller has already answered a missing +/// target its own way. `holder` names the binding the user record is bound to, +/// and `unfit` is an in-crate comparison against the role that may *not* hold +/// this grant — never text from a client. +pub(crate) fn role_claim(holder: &str, unfit: &str, mark: &str) -> Vec { + vec![ + format!("LET $role_was = (SELECT VALUE {USER_ROLE_CLAIM_FIELD} FROM ONLY ${holder})"), + format!( + "LET $role_now = (UPDATE ${holder} SET {USER_ROLE_CLAIM_FIELD} = \ + ({USER_ROLE_CLAIM_FIELD} ?? 0) + 1 RETURN VALUE role)" + ), + format!("IF array::len($role_now) > 0 AND $role_now[0] {unfit} {{ THROW '{mark}' }}"), + format!("UPDATE ${holder} SET {USER_ROLE_CLAIM_FIELD} = $role_was"), + ] +} + /// What [`claim_two_when_and_create`] settled. pub(crate) enum ClaimedTwo { /// Both seats and the row committed together. @@ -1046,6 +1237,7 @@ mod tests { BOARD_EPOCH_STROKE_COUNT_FIELD, "epoch ?? $num", UNLIMITED, + None, &RecordId::new("board_stroke", "s1"), &a_stroke(), &db, @@ -1072,6 +1264,7 @@ mod tests { BOARD_EPOCH_STROKE_COUNT_FIELD, "capacity ?? $num", UNLIMITED, + None, &RecordId::new("board_stroke", "s1"), &a_stroke(), &db,