🧮 refuse a mark on an exam that no longer exists
Browse files at this commitbb774eba29a03316f6f13b07ed13ff0f1e3dab18Parent: 755f4ab- fix
- 🧮 refuse a mark on an exam that no longer exists
2 changed files +140 -42
- src/domain/exam.rs +36 -15
- src/domain/exam_result.rs +104 -27
src/domain/exam.rsmodified+36 -15
| @@ -892,8 +892,14 @@ mod tests { | ||
| 892 | 892 | /// The racer is a mark: [`ExamResult::grade`] claims the exam's own |
| 893 | 893 | /// `result_count` (and the kind's reference) before writing, so it contends |
| 894 | 894 | /// with the `DELETE $ex` and with the kind_ref decrement inside the same |
| 895 | - /// transaction. The two stored counters assert the delete landed *inside* | |
| 896 | - /// the burst rather than before or after it. | |
| 895 | + /// transaction. A round where *some* of the six grades 404 and the rest | |
| 896 | + /// succeed is the witness that the delete landed inside the burst: the 404 | |
| 897 | + /// comes from `write_mark`'s existence gate, so it can only be answered by a | |
| 898 | + /// grade that reached the store after the row was gone, and its siblings' | |
| 899 | + /// success says the same burst also had grades that got there first. Stored | |
| 900 | + /// state cannot say this any more — the gate is what stops a mark outliving | |
| 901 | + /// its exam, so the sweep now finds nothing to leave behind in *every* | |
| 902 | + /// round, which is asserted below as a fact rather than read as a signal. | |
| 897 | 903 | /// |
| 898 | 904 | /// It does not prove the retry either: measured at 0 conflicts in 100 raced |
| 899 | 905 | /// rounds, and green with |
| @@ -908,7 +914,7 @@ mod tests { | ||
| 908 | 914 | use crate::domain::exam_result::{ExamResult, Mark}; |
| 909 | 915 | let (db, _serialized) = crate::database::init_test_server("exam_delete_race").await; |
| 910 | 916 | let (mut delete_500, mut grade_500) = (0, 0); |
| 911 | - let (mut marked, mut swept) = (0, 0); | |
| 917 | + let (mut split, mut swept) = (0, 0); | |
| 912 | 918 | let (mut last_delete, mut last_grade) = (String::new(), String::new()); |
| 913 | 919 | for round in 0..20 { |
| 914 | 920 | let exam = published(&db).await; |
| @@ -953,34 +959,49 @@ mod tests { | ||
| 953 | 959 | delete_500 += 1; |
| 954 | 960 | last_delete = format!("{drop_it:?}"); |
| 955 | 961 | } |
| 962 | + let mut refused = 0; | |
| 956 | 963 | for mark in marks { |
| 957 | 964 | let mark = mark.await.unwrap(); |
| 958 | - if matches!(mark, Err(AppError::Db(_))) { | |
| 959 | - grade_500 += 1; | |
| 960 | - last_grade = format!("{mark:?}"); | |
| 965 | + match mark { | |
| 966 | + // The existence gate: this grade reached the store after | |
| 967 | + // the row was gone. | |
| 968 | + Err(AppError::NotFound) => refused += 1, | |
| 969 | + Err(AppError::Db(_)) => { | |
| 970 | + grade_500 += 1; | |
| 971 | + last_grade = format!("{mark:?}"); | |
| 972 | + } | |
| 973 | + _ => {} | |
| 961 | 974 | } |
| 962 | 975 | } |
| 963 | - // Stored state, and *both* outcomes are needed: marks surviving | |
| 964 | - // means the grades ran past the cascade, no marks left means the | |
| 965 | - // cascade ran past the grades. Seeing only one of the two is a run | |
| 966 | - // where the delete never landed inside the burst at all. | |
| 976 | + // Neither end of the burst: some grades beat the delete and some | |
| 977 | + // lost to it, so the delete landed *between* them. A round that is | |
| 978 | + // all-refused or all-through is one where it landed outside. | |
| 979 | + if (1..6).contains(&refused) { | |
| 980 | + split += 1; | |
| 981 | + } | |
| 967 | 982 | if ExamResult::list_for_exam(exam.get_id(), &db) |
| 968 | 983 | .await |
| 969 | 984 | .unwrap() |
| 970 | 985 | .is_empty() |
| 971 | 986 | { |
| 972 | 987 | swept += 1; |
| 973 | - } else { | |
| 974 | - marked += 1; | |
| 975 | 988 | } |
| 976 | 989 | } |
| 977 | 990 | eprintln!( |
| 978 | 991 | "Exam::delete raced: {delete_500}/20 delete 500s, {grade_500} grade 500s, \ |
| 979 | - {marked} rounds with marks left / {swept} swept" | |
| 992 | + {split} rounds split by the delete / {swept} swept clean" | |
| 980 | 993 | ); |
| 981 | 994 | assert!( |
| 982 | - marked > 0 && swept > 0, | |
| 983 | - "the delete never landed inside the burst ({marked} left / {swept} swept)" | |
| 995 | + split > 0, | |
| 996 | + "the delete never landed inside the burst (0/20 rounds split)" | |
| 997 | + ); | |
| 998 | + // Not a race signal, an invariant: the gate refuses a mark for an exam | |
| 999 | + // that is gone, so no round can leave one behind for the next reader. | |
| 1000 | + assert_eq!( | |
| 1001 | + swept, | |
| 1002 | + 20, | |
| 1003 | + "a mark outlived its exam in {} rounds", | |
| 1004 | + 20 - swept | |
| 984 | 1005 | ); |
| 985 | 1006 | assert_eq!( |
| 986 | 1007 | delete_500, 0, |
src/domain/exam_result.rsmodified+104 -27
| @@ -5,7 +5,7 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue}; | ||
| 5 | 5 | use crate::constant::{ |
| 6 | 6 | EXAM_RESULT_COUNT_FIELD, EXAM_RESULT_TABLE, KIND_REF_TABLE, REF_COUNT_FIELD, |
| 7 | 7 | }; |
| 8 | -use crate::database::Database; | |
| 8 | +use crate::database::{Database, transaction_with_retry}; | |
| 9 | 9 | use crate::domain::cap; |
| 10 | 10 | use crate::domain::course::CourseId; |
| 11 | 11 | use crate::domain::exam::ExamId; |
| @@ -242,33 +242,55 @@ impl ExamResult { | ||
| 242 | 242 | mark, |
| 243 | 243 | graded_by: graded_by.clone(), |
| 244 | 244 | }; |
| 245 | - // The "not a draft" gate rides in the same transaction as the mark, the | |
| 246 | - // mirror of the re-draft gate on `Exam::update_if_unchanged`: between | |
| 247 | - // them, a mark and a re-draft racing each other can only ever leave one | |
| 248 | - // of the two applied, whichever process either ran in. The caller's | |
| 249 | - // pre-flight check answers the same 409 one round trip earlier. | |
| 245 | + // The "exists" and "not a draft" gates ride in the same transaction as | |
| 246 | + // the mark, the mirror of the re-draft gate on | |
| 247 | + // `Exam::update_if_unchanged`: between them, a mark and a re-draft | |
| 248 | + // racing each other can only ever leave one of the two applied, | |
| 249 | + // whichever process either ran in. The existence gate is not | |
| 250 | + // redundant with the draft one — a deleted exam reads NONE, which is | |
| 251 | + // *falsy*, so the draft gate alone waved a mark onto an exam that no | |
| 252 | + // longer existed. The caller's pre-flight check answers the same 409 | |
| 253 | + // one round trip earlier. | |
| 250 | 254 | // Whether the row was already there rides out of the transaction with |
| 251 | 255 | // the mark: the answer decides if this grade owes the kind a reference, |
| 252 | 256 | // and read anywhere else it would be a guess about a row two graders |
| 253 | 257 | // may be writing at once. |
| 254 | - let mut written = db | |
| 255 | - .query( | |
| 256 | - "BEGIN TRANSACTION; | |
| 258 | + // | |
| 259 | + // Re-sent while the store answers "conflict, retry": the gates read a | |
| 260 | + // column an exam PATCH writes, so the two contend on one record by | |
| 261 | + // design. Sound to re-send because the only write is an UPSERT on a | |
| 262 | + // deterministic id — `key::sitting(exam, user, seq)` *is* the | |
| 263 | + // `exam_result_exam_user_seq` unique tuple — so it can never answer | |
| 264 | + // "already exists", the one thing a retry cannot fix. | |
| 265 | + let (mut written, mut errors) = transaction_with_retry( | |
| 266 | + db, | |
| 267 | + "BEGIN TRANSACTION; | |
| 268 | + IF (SELECT VALUE id FROM ONLY $exam) IS NONE { THROW 'exam_missing' }; | |
| 257 | 269 | IF (SELECT VALUE draft FROM ONLY $exam) { THROW 'exam_draft' }; |
| 258 | 270 | LET $before = (SELECT VALUE id FROM ONLY $id); |
| 259 | 271 | LET $after = (UPSERT $id CONTENT $result RETURN AFTER); |
| 260 | 272 | RETURN { existed: $before != NONE, result: $after[0] }; |
| 261 | 273 | COMMIT TRANSACTION;", |
| 262 | - ) | |
| 263 | - .bind(("exam", exam.record())) | |
| 264 | - .bind(("id", result.id.record())) | |
| 265 | - .bind(("result", result)) | |
| 266 | - .await?; | |
| 267 | - let mut errors = written.take_errors(); | |
| 268 | - if errors | |
| 269 | - .values() | |
| 270 | - .any(|error| error.to_string().contains("exam_draft")) | |
| 271 | - { | |
| 274 | + &[ | |
| 275 | + ("exam".into(), exam.record().into_value()), | |
| 276 | + ("id".into(), result.id.record().into_value()), | |
| 277 | + ("result".into(), result.into_value()), | |
| 278 | + ], | |
| 279 | + &["exam_missing", "exam_draft"], | |
| 280 | + ) | |
| 281 | + .await?; | |
| 282 | + // An aborted transaction errors every slot and only the THROW's own | |
| 283 | + // slot names the marker, so a refusal is read by marker while a lost | |
| 284 | + // round was already re-sent — never reported as a 500. | |
| 285 | + let thrown = |marker: &str| { | |
| 286 | + errors | |
| 287 | + .values() | |
| 288 | + .any(|error| error.to_string().contains(marker)) | |
| 289 | + }; | |
| 290 | + if thrown("exam_missing") { | |
| 291 | + return Err(AppError::NotFound); | |
| 292 | + } | |
| 293 | + if thrown("exam_draft") { | |
| 272 | 294 | return Err(draft_error()); |
| 273 | 295 | } |
| 274 | 296 | if let Some(error) = errors.drain().map(|(_, error)| error).next() { |
| @@ -348,8 +370,18 @@ impl ExamResult { | ||
| 348 | 370 | // release them a second time, and on a kind another exam still grades |
| 349 | 371 | // under, one release too many reads as one mark too few — a kind |
| 350 | 372 | // wrongly free to leave the settings. |
| 351 | - let mut result = db | |
| 352 | - .query(format!( | |
| 373 | + // | |
| 374 | + // Re-sent while the store answers "conflict, retry": the counters this | |
| 375 | + // touches are the ones a concurrent grade claims, so a lost round is | |
| 376 | + // routine and aborts having written nothing. `check()` cannot be used | |
| 377 | + // to read the outcome — it takes the *lowest*-slot error, and an | |
| 378 | + // aborted transaction's generic "not executed" sibling masked the real | |
| 379 | + // conflict, the exact defect deleted from `Exam::delete`. No THROW of | |
| 380 | + // its own, and only `DELETE`/`UPDATE` inside, so nothing here can | |
| 381 | + // answer "already exists" and make a retry unsound. | |
| 382 | + let (mut result, mut errors) = transaction_with_retry( | |
| 383 | + db, | |
| 384 | + &format!( | |
| 353 | 385 | "BEGIN TRANSACTION; |
| 354 | 386 | LET $gone = (DELETE exam_result WHERE exam = $ex AND user = $usr RETURN BEFORE); |
| 355 | 387 | IF array::len($gone) > 0 {{ |
| @@ -360,12 +392,18 @@ impl ExamResult { | ||
| 360 | 392 | }}; |
| 361 | 393 | RETURN $gone; |
| 362 | 394 | COMMIT TRANSACTION;" |
| 363 | - )) | |
| 364 | - .bind(("ex", exam.record())) | |
| 365 | - .bind(("usr", user.record())) | |
| 366 | - .bind(("kind", kind.to_string())) | |
| 367 | - .await? | |
| 368 | - .check()?; | |
| 395 | + ), | |
| 396 | + &[ | |
| 397 | + ("ex".into(), exam.record().into_value()), | |
| 398 | + ("usr".into(), user.record().into_value()), | |
| 399 | + ("kind".into(), kind.to_string().into_value()), | |
| 400 | + ], | |
| 401 | + &[], | |
| 402 | + ) | |
| 403 | + .await?; | |
| 404 | + if let Some(error) = errors.drain().map(|(_, error)| error).next() { | |
| 405 | + return Err(error.into()); | |
| 406 | + } | |
| 369 | 407 | // `RETURN` is the last statement before `COMMIT`; its slot follows the |
| 370 | 408 | // statement count, as in `Exam::delete`. |
| 371 | 409 | let slot = result.num_statements().saturating_sub(2); |
| @@ -393,6 +431,17 @@ mod tests { | ||
| 393 | 431 | let exam = ExamId::from_key("01TESTEXAMAAAAAAAAAAAAAAAA"); |
| 394 | 432 | let user = UserId::from_key("01TESTSTUDENTAAAAAAAAAAAAA"); |
| 395 | 433 | let teacher = UserId::from_key("01TESTTEACHERAAAAAAAAAAAAA"); |
| 434 | + // The exam has to be real: a mark is refused on one that isn't. | |
| 435 | + db.query( | |
| 436 | + "CREATE $ex SET creator = $t, course = course:c, title = 't', | |
| 437 | + description = '', kind = 'midterm'", | |
| 438 | + ) | |
| 439 | + .bind(("ex", exam.record())) | |
| 440 | + .bind(("t", teacher.record())) | |
| 441 | + .await | |
| 442 | + .unwrap() | |
| 443 | + .check() | |
| 444 | + .unwrap(); | |
| 396 | 445 | |
| 397 | 446 | // Grade sitting #1, then a retake as sitting #2 — two rows, not one. |
| 398 | 447 | ExamResult::grade( |
| @@ -455,4 +504,32 @@ mod tests { | ||
| 455 | 504 | .is_empty() |
| 456 | 505 | ); |
| 457 | 506 | } |
| 507 | + | |
| 508 | + /// A deleted exam reads NONE, and NONE is falsy — the draft gate alone let | |
| 509 | + /// a mark land on an exam that no longer existed. | |
| 510 | + #[tokio::test] | |
| 511 | + async fn a_mark_is_refused_on_an_exam_that_no_longer_exists() { | |
| 512 | + let db = init_mem().await.unwrap(); | |
| 513 | + let exam = ExamId::from_key("01TESTGONEEXAMAAAAAAAAAAAA"); | |
| 514 | + let user = UserId::from_key("01TESTSTUDENTAAAAAAAAAAAAA"); | |
| 515 | + let teacher = UserId::from_key("01TESTTEACHERAAAAAAAAAAAAA"); | |
| 516 | + | |
| 517 | + let refused = ExamResult::grade( | |
| 518 | + &exam, | |
| 519 | + &user, | |
| 520 | + 1, | |
| 521 | + Mark::try_new(40).unwrap(), | |
| 522 | + &teacher, | |
| 523 | + "midterm", | |
| 524 | + &db, | |
| 525 | + ) | |
| 526 | + .await; | |
| 527 | + assert!(matches!(refused, Err(AppError::NotFound)), "{refused:?}"); | |
| 528 | + assert!( | |
| 529 | + ExamResult::list_all_for_exam_user(&exam, &user, &db) | |
| 530 | + .await | |
| 531 | + .unwrap() | |
| 532 | + .is_empty() | |
| 533 | + ); | |
| 534 | + } | |
| 458 | 535 | } |