aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-harness / tests / provisional_commit.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Headstart M3 (#537): the `Committing` state. A journaled `BeginCommit` |
| 17 | //! freezes a `Committable` task and its inputs; nothing upstream may change |
| 18 | //! them until a `CommitOutcome` is recorded; a replay that finds no outcome |
| 19 | //! marks the task `CommitUncertain` and never runs it again. Bound request |
| 20 | //! keys are journaled, kept while a task is aborted or retried, and dropped |
| 21 | //! when its commit ended. |
| 22 | |
| 23 | mod common; |
| 24 | |
| 25 | use common::{enabled, H}; |
| 26 | use spark_harness::execution::TaskState; |
| 27 | use spark_harness::readiness::ProofClaim; |
| 28 | use spark_harness::scheduler::{ |
| 29 | CommitResult, Effects, EventKind, Outcome, SchedError, MAX_APPROVAL_BINDINGS_PER_TASK, |
| 30 | MAX_APPROVAL_KEY_LEN, RESERVED_ID_PREFIX, |
| 31 | }; |
| 32 | use std::collections::BTreeMap; |
| 33 | |
| 34 | fn remove_receipt(h: &H, receipt_id: &str) { |
| 35 | std::fs::remove_file( |
| 36 | h.engine |
| 37 | .completion_store |
| 38 | .join("receipts") |
| 39 | .join(format!("{receipt_id}.json")), |
| 40 | ) |
| 41 | .unwrap(); |
| 42 | } |
| 43 | |
| 44 | fn completion(h: &H, task: &str) -> ProofClaim { |
| 45 | h.sched.task(task).unwrap().completion.clone().unwrap() |
| 46 | } |
| 47 | |
| 48 | /// `BeginCommit` built from the task's current accepted state. |
| 49 | fn begin_kind(h: &H, task: &str) -> EventKind { |
| 50 | let producers: BTreeMap<String, ProofClaim> = h |
| 51 | .sched |
| 52 | .task(task) |
| 53 | .unwrap() |
| 54 | .bindings |
| 55 | .keys() |
| 56 | .map(|p| (p.clone(), completion(h, p))) |
| 57 | .collect(); |
| 58 | EventKind::BeginCommit { |
| 59 | task: task.into(), |
| 60 | generation: h.generation(task), |
| 61 | completion: completion(h, task), |
| 62 | producers, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn begin(h: &mut H, task: &str) -> Result<Outcome, SchedError> { |
| 67 | h.advance(10); |
| 68 | let kind = begin_kind(h, task); |
| 69 | h.send(kind) |
| 70 | } |
| 71 | |
| 72 | fn outcome(h: &mut H, task: &str, outcome: CommitResult) -> Effects { |
| 73 | let generation = h.generation(task); |
| 74 | h.ok(EventKind::CommitOutcome { |
| 75 | task: task.into(), |
| 76 | generation, |
| 77 | outcome, |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | fn committed() -> CommitResult { |
| 82 | CommitResult::Committed { |
| 83 | detail: "test".into(), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn in_progress(r: &Result<Outcome, SchedError>, committing: &str) -> bool { |
| 88 | matches!(r, Err(SchedError::CommitInProgress { committing: c, .. }) if c == committing) |
| 89 | } |
| 90 | |
| 91 | /// a -> b -> c with a and b final. c is still provisional. |
| 92 | fn chain() -> H { |
| 93 | let mut h = H::new(enabled()); |
| 94 | h.register("a", 1, &[]); |
| 95 | h.register("b", 1, &["a"]); |
| 96 | h.register("c", 1, &["b"]); |
| 97 | h.publish("a", 1); |
| 98 | h.publish("b", 1); |
| 99 | h.finish("a"); |
| 100 | h.finish("b"); |
| 101 | assert_eq!(h.sched.state("b"), Some(TaskState::Committable)); |
| 102 | assert_eq!(h.sched.state("c"), Some(TaskState::Provisional)); |
| 103 | h |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn begin_commit_freezes_the_inputs_and_the_outcome_ends_it() { |
| 108 | let mut h = chain(); |
| 109 | let gen_b = h.generation("b"); |
| 110 | assert!(matches!(begin(&mut h, "b"), Ok(Outcome::Applied(_)))); |
| 111 | assert_eq!(h.sched.state("b"), Some(TaskState::Committing)); |
| 112 | let inputs = h.sched.task("b").unwrap().commit.clone().unwrap(); |
| 113 | assert_eq!(inputs.completion, completion(&h, "b")); |
| 114 | assert_eq!(inputs.producers.get("a"), Some(&completion(&h, "a"))); |
| 115 | // The consumer keeps working against the committing producer. |
| 116 | assert_eq!(h.sched.state("c"), Some(TaskState::Provisional)); |
| 117 | outcome(&mut h, "b", committed()); |
| 118 | assert_eq!(h.sched.state("b"), Some(TaskState::Committed)); |
| 119 | assert_eq!(h.generation("b"), gen_b); |
| 120 | // Done: a second begin is refused, as is a second outcome. |
| 121 | assert!(matches!( |
| 122 | begin(&mut h, "b"), |
| 123 | Err(SchedError::WrongState { .. }) |
| 124 | )); |
| 125 | let r = h.send(EventKind::CommitOutcome { |
| 126 | task: "b".into(), |
| 127 | generation: gen_b, |
| 128 | outcome: committed(), |
| 129 | }); |
| 130 | assert!(matches!(r, Err(SchedError::WrongState { .. })), "{r:?}"); |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn while_committing_upstream_and_own_changes_are_refused() { |
| 135 | let mut h = chain(); |
| 136 | let gen_a = h.generation("a"); |
| 137 | let gen_b = h.generation("b"); |
| 138 | begin(&mut h, "b").unwrap(); |
| 139 | let refused_on_a = [ |
| 140 | EventKind::Revoke { |
| 141 | task: "a".into(), |
| 142 | generation: gen_a, |
| 143 | reason: "test".into(), |
| 144 | }, |
| 145 | EventKind::Cancel { |
| 146 | task: "a".into(), |
| 147 | generation: gen_a, |
| 148 | }, |
| 149 | EventKind::NewGeneration { |
| 150 | task: "a".into(), |
| 151 | generation: gen_a + 1, |
| 152 | }, |
| 153 | EventKind::ProducerFailed { |
| 154 | task: "a".into(), |
| 155 | generation: gen_a, |
| 156 | reason: "test".into(), |
| 157 | }, |
| 158 | EventKind::InterfacePublished { |
| 159 | task: "a".into(), |
| 160 | generation: gen_a, |
| 161 | claim: h.mint_interface("a", 2), |
| 162 | }, |
| 163 | ]; |
| 164 | for kind in refused_on_a { |
| 165 | let r = h.send(kind.clone()); |
| 166 | assert!(in_progress(&r, "b"), "{kind:?}: {r:?}"); |
| 167 | } |
| 168 | let refused_on_b = [ |
| 169 | EventKind::Cancel { |
| 170 | task: "b".into(), |
| 171 | generation: gen_b, |
| 172 | }, |
| 173 | EventKind::NewGeneration { |
| 174 | task: "b".into(), |
| 175 | generation: gen_b + 1, |
| 176 | }, |
| 177 | EventKind::ProducerFailed { |
| 178 | task: "b".into(), |
| 179 | generation: gen_b, |
| 180 | reason: "test".into(), |
| 181 | }, |
| 182 | ]; |
| 183 | for kind in refused_on_b { |
| 184 | let r = h.send(kind.clone()); |
| 185 | assert!(in_progress(&r, "b"), "{kind:?}: {r:?}"); |
| 186 | } |
| 187 | // Revoke needs an active task; Committing is not one. |
| 188 | let r = h.send(EventKind::Revoke { |
| 189 | task: "b".into(), |
| 190 | generation: gen_b, |
| 191 | reason: "test".into(), |
| 192 | }); |
| 193 | assert!(matches!(r, Err(SchedError::WrongState { .. })), "{r:?}"); |
| 194 | assert_eq!(h.sched.state("a"), Some(TaskState::Committable)); |
| 195 | assert_eq!(h.sched.state("b"), Some(TaskState::Committing)); |
| 196 | assert_eq!(h.generation("a"), gen_a); |
| 197 | assert_eq!(h.generation("b"), gen_b); |
| 198 | |
| 199 | // Once the outcome is in, the same revoke is accepted and does not |
| 200 | // reach the committed task. |
| 201 | outcome(&mut h, "b", committed()); |
| 202 | let fx = h.ok(EventKind::Revoke { |
| 203 | task: "a".into(), |
| 204 | generation: gen_a, |
| 205 | reason: "test".into(), |
| 206 | }); |
| 207 | assert!(!fx.revoked.contains(&"b".to_string()), "{fx:?}"); |
| 208 | assert_eq!(h.sched.state("b"), Some(TaskState::Committed)); |
| 209 | assert_eq!(h.sched.state("c"), Some(TaskState::Provisional)); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn without_a_commit_the_same_revoke_reaches_the_consumer() { |
| 214 | // Negative control for the test above. |
| 215 | let mut h = chain(); |
| 216 | let gen_a = h.generation("a"); |
| 217 | let gen_b = h.generation("b"); |
| 218 | h.ok(EventKind::Revoke { |
| 219 | task: "a".into(), |
| 220 | generation: gen_a, |
| 221 | reason: "test".into(), |
| 222 | }); |
| 223 | assert_eq!(h.sched.state("b"), Some(TaskState::Blocked)); |
| 224 | assert_eq!(h.generation("b"), gen_b + 1); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn begin_commit_refuses_inputs_that_differ_from_the_state() { |
| 229 | let mut h = chain(); |
| 230 | let good = begin_kind(&h, "b"); |
| 231 | let EventKind::BeginCommit { |
| 232 | generation, |
| 233 | completion: own, |
| 234 | producers, |
| 235 | .. |
| 236 | } = good.clone() |
| 237 | else { |
| 238 | unreachable!() |
| 239 | }; |
| 240 | let mut other = own.clone(); |
| 241 | other.digest = format!("{}0", other.digest); |
| 242 | let with = |
| 243 | |completion: ProofClaim, producers: BTreeMap<String, ProofClaim>| EventKind::BeginCommit { |
| 244 | task: "b".into(), |
| 245 | generation, |
| 246 | completion, |
| 247 | producers, |
| 248 | }; |
| 249 | let mut missing = producers.clone(); |
| 250 | missing.clear(); |
| 251 | let mut extra = producers.clone(); |
| 252 | extra.insert("c".into(), own.clone()); |
| 253 | let mut wrong_producer = producers.clone(); |
| 254 | wrong_producer.insert("a".into(), other.clone()); |
| 255 | let cases = [ |
| 256 | ("own completion", with(other.clone(), producers.clone())), |
| 257 | ("no producers", with(own.clone(), missing)), |
| 258 | ("extra producer", with(own.clone(), extra)), |
| 259 | ("producer claim", with(own.clone(), wrong_producer)), |
| 260 | ( |
| 261 | "stale generation", |
| 262 | EventKind::BeginCommit { |
| 263 | task: "b".into(), |
| 264 | generation: generation + 1, |
| 265 | completion: own.clone(), |
| 266 | producers: producers.clone(), |
| 267 | }, |
| 268 | ), |
| 269 | ]; |
| 270 | for (what, kind) in cases { |
| 271 | let r = h.send(kind); |
| 272 | assert!(r.is_err(), "{what}: {r:?}"); |
| 273 | assert_eq!(h.sched.state("b"), Some(TaskState::Committable), "{what}"); |
| 274 | } |
| 275 | // Not committable yet. |
| 276 | let r = h.send(EventKind::BeginCommit { |
| 277 | task: "c".into(), |
| 278 | generation: h.generation("c"), |
| 279 | completion: own.clone(), |
| 280 | producers: BTreeMap::from([("b".to_string(), completion(&h, "b"))]), |
| 281 | }); |
| 282 | assert!(matches!(r, Err(SchedError::WrongState { .. })), "{r:?}"); |
| 283 | // A producer receipt that no longer verifies. |
| 284 | remove_receipt(&h, &completion(&h, "a").receipt_id); |
| 285 | let r = h.send(good.clone()); |
| 286 | assert!( |
| 287 | matches!(r, Err(SchedError::Proof { ref task, .. }) if task == "a"), |
| 288 | "{r:?}" |
| 289 | ); |
| 290 | assert_eq!(h.sched.state("b"), Some(TaskState::Committable)); |
| 291 | } |
| 292 | |
| 293 | #[test] |
| 294 | fn begin_commit_accepts_the_matching_inputs() { |
| 295 | // Positive control for the refusals above. |
| 296 | let mut h = chain(); |
| 297 | let kind = begin_kind(&h, "b"); |
| 298 | assert!(matches!(h.send(kind), Ok(Outcome::Applied(_)))); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn only_one_commit_runs_per_dependency_chain() { |
| 303 | let mut h = chain(); |
| 304 | h.register("x", 1, &[]); |
| 305 | h.publish("x", 1); |
| 306 | h.finish("x"); |
| 307 | // c becomes committable too. |
| 308 | h.publish("c", 1); |
| 309 | h.finish("c"); |
| 310 | begin(&mut h, "b").unwrap(); |
| 311 | // Below and above b: refused. |
| 312 | assert!(in_progress(&begin(&mut h, "c"), "b")); |
| 313 | assert!(in_progress(&begin(&mut h, "a"), "b")); |
| 314 | // An unrelated task commits at the same time. |
| 315 | assert!(begin(&mut h, "x").is_ok()); |
| 316 | outcome(&mut h, "b", committed()); |
| 317 | // a is upstream of a committed task only: it may commit now. |
| 318 | assert!(begin(&mut h, "c").is_ok()); |
| 319 | assert!(begin(&mut h, "a").is_ok()); |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn failed_commit_aborts_retryably_and_revokes_consumers() { |
| 324 | let mut h = chain(); |
| 325 | let gen_b = h.generation("b"); |
| 326 | let gen_c = h.generation("c"); |
| 327 | begin(&mut h, "b").unwrap(); |
| 328 | outcome( |
| 329 | &mut h, |
| 330 | "b", |
| 331 | CommitResult::Failed { |
| 332 | reason: "refused".into(), |
| 333 | }, |
| 334 | ); |
| 335 | assert_eq!(h.sched.state("b"), Some(TaskState::Aborted)); |
| 336 | assert_eq!(h.sched.state("c"), Some(TaskState::Blocked)); |
| 337 | assert_eq!(h.generation("c"), gen_c + 1); |
| 338 | h.ok(EventKind::Retry { |
| 339 | task: "b".into(), |
| 340 | generation: gen_b, |
| 341 | }); |
| 342 | assert_eq!(h.generation("b"), gen_b + 1); |
| 343 | } |
| 344 | |
| 345 | #[test] |
| 346 | fn uncertain_commit_is_terminal_and_revokes_consumers() { |
| 347 | let mut h = chain(); |
| 348 | let gen_b = h.generation("b"); |
| 349 | let gen_c = h.generation("c"); |
| 350 | begin(&mut h, "b").unwrap(); |
| 351 | outcome( |
| 352 | &mut h, |
| 353 | "b", |
| 354 | CommitResult::Uncertain { |
| 355 | reason: "transport".into(), |
| 356 | }, |
| 357 | ); |
| 358 | assert_eq!(h.sched.state("b"), Some(TaskState::CommitUncertain)); |
| 359 | assert_eq!(h.sched.state("c"), Some(TaskState::Blocked)); |
| 360 | assert_eq!(h.generation("c"), gen_c + 1); |
| 361 | for kind in [ |
| 362 | EventKind::Retry { |
| 363 | task: "b".into(), |
| 364 | generation: gen_b, |
| 365 | }, |
| 366 | EventKind::Cancel { |
| 367 | task: "b".into(), |
| 368 | generation: gen_b, |
| 369 | }, |
| 370 | EventKind::NewGeneration { |
| 371 | task: "b".into(), |
| 372 | generation: gen_b + 1, |
| 373 | }, |
| 374 | ] { |
| 375 | let r = h.send(kind.clone()); |
| 376 | assert!(r.is_err(), "{kind:?}: {r:?}"); |
| 377 | assert_eq!(h.sched.state("b"), Some(TaskState::CommitUncertain)); |
| 378 | } |
| 379 | assert!(begin(&mut h, "b").is_err()); |
| 380 | } |
| 381 | |
| 382 | #[test] |
| 383 | fn replayed_commit_without_an_outcome_becomes_uncertain_and_never_reruns() { |
| 384 | let mut h = H::journaled(enabled()); |
| 385 | h.register("a", 1, &[]); |
| 386 | h.register("b", 1, &["a"]); |
| 387 | h.register("c", 1, &["b"]); |
| 388 | h.publish("a", 1); |
| 389 | h.publish("b", 1); |
| 390 | h.finish("a"); |
| 391 | h.finish("b"); |
| 392 | let gen_c = h.generation("c"); |
| 393 | begin(&mut h, "b").unwrap(); |
| 394 | // Crash before the outcome. |
| 395 | h.restart(enabled()).unwrap(); |
| 396 | assert_eq!(h.sched.state("b"), Some(TaskState::CommitUncertain)); |
| 397 | assert_eq!(h.sched.state("c"), Some(TaskState::Blocked)); |
| 398 | assert_eq!(h.generation("c"), gen_c + 1); |
| 399 | let journal = std::fs::read_to_string(h.fx.journal()).unwrap(); |
| 400 | assert!(journal.contains(&format!("{RESERVED_ID_PREFIX}commit-uncertain-"))); |
| 401 | // Neither a new begin nor a late outcome is accepted. |
| 402 | assert!(begin(&mut h, "b").is_err()); |
| 403 | let r = h.send(EventKind::CommitOutcome { |
| 404 | task: "b".into(), |
| 405 | generation: h.generation("b"), |
| 406 | outcome: committed(), |
| 407 | }); |
| 408 | assert!(matches!(r, Err(SchedError::WrongState { .. })), "{r:?}"); |
| 409 | // A second restart settles nothing new. |
| 410 | let lines = std::fs::read_to_string(h.fx.journal()) |
| 411 | .unwrap() |
| 412 | .lines() |
| 413 | .count(); |
| 414 | h.restart(enabled()).unwrap(); |
| 415 | assert_eq!(h.sched.state("b"), Some(TaskState::CommitUncertain)); |
| 416 | assert_eq!( |
| 417 | std::fs::read_to_string(h.fx.journal()) |
| 418 | .unwrap() |
| 419 | .lines() |
| 420 | .count(), |
| 421 | lines |
| 422 | ); |
| 423 | } |
| 424 | |
| 425 | #[test] |
| 426 | fn replayed_commit_with_an_outcome_keeps_it() { |
| 427 | // Negative control: a recorded outcome survives the restart unchanged. |
| 428 | let mut h = H::journaled(enabled()); |
| 429 | h.register("a", 1, &[]); |
| 430 | h.publish("a", 1); |
| 431 | h.finish("a"); |
| 432 | begin(&mut h, "a").unwrap(); |
| 433 | outcome(&mut h, "a", committed()); |
| 434 | let snap = h.sched.snapshot(); |
| 435 | h.restart(enabled()).unwrap(); |
| 436 | assert_eq!(h.sched.state("a"), Some(TaskState::Committed)); |
| 437 | assert_eq!(h.sched.snapshot()["tasks"], snap["tasks"]); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn audit_defers_a_revocation_that_would_reach_a_running_commit() { |
| 442 | let mut h = chain(); |
| 443 | begin(&mut h, "b").unwrap(); |
| 444 | remove_receipt(&h, &completion(&h, "a").receipt_id); |
| 445 | let revoked = h.sched.audit().unwrap(); |
| 446 | assert!(revoked.is_empty(), "{revoked:?}"); |
| 447 | assert_eq!(h.sched.state("a"), Some(TaskState::Committable)); |
| 448 | assert_eq!(h.sched.state("b"), Some(TaskState::Committing)); |
| 449 | // After the outcome the deferred revocation goes through and stops at |
| 450 | // the committed task. |
| 451 | outcome(&mut h, "b", committed()); |
| 452 | let revoked = h.sched.audit().unwrap(); |
| 453 | assert!(revoked.contains(&"a".to_string()), "{revoked:?}"); |
| 454 | assert_eq!(h.sched.state("b"), Some(TaskState::Committed)); |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn audit_without_a_commit_revokes_the_whole_chain() { |
| 459 | // Negative control for the deferral above. |
| 460 | let mut h = chain(); |
| 461 | remove_receipt(&h, &completion(&h, "a").receipt_id); |
| 462 | let revoked = h.sched.audit().unwrap(); |
| 463 | assert!(revoked.contains(&"a".to_string()), "{revoked:?}"); |
| 464 | assert_eq!(h.sched.state("b"), Some(TaskState::Blocked)); |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn begin_commit_redelivery_is_a_duplicate() { |
| 469 | let mut h = chain(); |
| 470 | h.advance(10); |
| 471 | let kind = begin_kind(&h, "b"); |
| 472 | let event = spark_harness::scheduler::Event { |
| 473 | id: "begin-b".into(), |
| 474 | kind, |
| 475 | }; |
| 476 | assert!(matches!( |
| 477 | h.sched.deliver(event.clone()), |
| 478 | Ok(Outcome::Applied(_)) |
| 479 | )); |
| 480 | let r = h.sched.deliver(event); |
| 481 | assert!(matches!(r, Ok(Outcome::Duplicate)), "{r:?}"); |
| 482 | assert_eq!(h.sched.state("b"), Some(TaskState::Committing)); |
| 483 | } |
| 484 | |
| 485 | // ---------- bound request keys ---------- |
| 486 | |
| 487 | fn bind(h: &mut H, key: &str, task: &str) -> Result<Outcome, SchedError> { |
| 488 | h.advance(1); |
| 489 | let generation = h.generation(task); |
| 490 | h.send(EventKind::BindApproval { |
| 491 | key: key.into(), |
| 492 | task: task.into(), |
| 493 | generation, |
| 494 | }) |
| 495 | } |
| 496 | |
| 497 | fn bound(h: &H, key: &str) -> Option<(String, u64)> { |
| 498 | h.sched |
| 499 | .approval(key) |
| 500 | .map(|b| (b.task.clone(), b.generation)) |
| 501 | } |
| 502 | |
| 503 | fn wrong_state(r: &Result<Outcome, SchedError>) -> bool { |
| 504 | matches!(r, Err(SchedError::WrongState { .. })) |
| 505 | } |
| 506 | |
| 507 | fn cancel(h: &mut H, task: &str) { |
| 508 | let generation = h.generation(task); |
| 509 | h.ok(EventKind::Cancel { |
| 510 | task: task.into(), |
| 511 | generation, |
| 512 | }); |
| 513 | } |
| 514 | |
| 515 | fn retry(h: &mut H, task: &str) { |
| 516 | let generation = h.generation(task); |
| 517 | h.ok(EventKind::Retry { |
| 518 | task: task.into(), |
| 519 | generation, |
| 520 | }); |
| 521 | } |
| 522 | |
| 523 | #[test] |
| 524 | fn bindings_survive_a_replay_and_are_dropped_when_the_task_commits() { |
| 525 | let mut h = H::journaled(enabled()); |
| 526 | h.register("a", 1, &[]); |
| 527 | h.register("b", 1, &["a"]); |
| 528 | let gen_b = h.generation("b"); |
| 529 | bind(&mut h, "approval:x", "b").unwrap(); |
| 530 | bind(&mut h, "proposal:y", "b").unwrap(); |
| 531 | h.restart(enabled()).unwrap(); |
| 532 | assert_eq!(bound(&h, "approval:x"), Some(("b".into(), gen_b))); |
| 533 | assert_eq!(bound(&h, "proposal:y"), Some(("b".into(), gen_b))); |
| 534 | h.publish("a", 1); |
| 535 | h.finish("a"); |
| 536 | h.finish("b"); |
| 537 | begin(&mut h, "b").unwrap(); |
| 538 | // Running: still bound. |
| 539 | assert_eq!(h.sched.approvals_len(), 2); |
| 540 | outcome(&mut h, "b", committed()); |
| 541 | assert_eq!(h.sched.approvals_len(), 0); |
| 542 | h.restart(enabled()).unwrap(); |
| 543 | assert_eq!(h.sched.approvals_len(), 0); |
| 544 | } |
| 545 | |
| 546 | #[test] |
| 547 | fn bindings_are_dropped_only_when_the_commit_ended() { |
| 548 | for (result, ended, kept) in [ |
| 549 | ( |
| 550 | CommitResult::Failed { |
| 551 | reason: "refused".into(), |
| 552 | }, |
| 553 | TaskState::Aborted, |
| 554 | true, |
| 555 | ), |
| 556 | ( |
| 557 | CommitResult::Uncertain { |
| 558 | reason: "transport".into(), |
| 559 | }, |
| 560 | TaskState::CommitUncertain, |
| 561 | false, |
| 562 | ), |
| 563 | (committed(), TaskState::Committed, false), |
| 564 | ] { |
| 565 | let mut h = chain(); |
| 566 | let gen_b = h.generation("b"); |
| 567 | bind(&mut h, "approval:x", "b").unwrap(); |
| 568 | // A consumer's binding is kept: the consumer is revoked to |
| 569 | // `Blocked`, which is not an end. |
| 570 | bind(&mut h, "approval:c", "c").unwrap(); |
| 571 | begin(&mut h, "b").unwrap(); |
| 572 | outcome(&mut h, "b", result.clone()); |
| 573 | assert_eq!(h.sched.state("b"), Some(ended), "{result:?}"); |
| 574 | let want = kept.then(|| ("b".to_string(), gen_b)); |
| 575 | assert_eq!(bound(&h, "approval:x"), want, "{result:?}"); |
| 576 | assert!(bound(&h, "approval:c").is_some(), "{result:?}"); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | #[test] |
| 581 | fn an_aborted_task_keeps_its_bindings_however_it_aborted() { |
| 582 | // Cancel, producer failure and lease expiry abort c (provisional); a |
| 583 | // failed commit aborts b. Each keeps the binding at the same |
| 584 | // generation, so the bound request stays fenced (`NotCommittable`). |
| 585 | type Abort = fn(&mut H); |
| 586 | let aborts: [(&str, &str, Abort); 4] = [ |
| 587 | ("cancel", "c", |h| cancel(h, "c")), |
| 588 | ("producer failed", "c", |h| { |
| 589 | let generation = h.generation("c"); |
| 590 | h.ok(EventKind::ProducerFailed { |
| 591 | task: "c".into(), |
| 592 | generation, |
| 593 | reason: "test".into(), |
| 594 | }); |
| 595 | }), |
| 596 | ("lease expired", "c", |h| { |
| 597 | h.advance(5_000); |
| 598 | h.ok(EventKind::Tick); |
| 599 | }), |
| 600 | ("commit failed", "b", |h| { |
| 601 | begin(h, "b").unwrap(); |
| 602 | outcome( |
| 603 | h, |
| 604 | "b", |
| 605 | CommitResult::Failed { |
| 606 | reason: "EFFECT_REFUSED".into(), |
| 607 | }, |
| 608 | ); |
| 609 | }), |
| 610 | ]; |
| 611 | for (how, task, abort) in aborts { |
| 612 | let mut h = chain(); |
| 613 | let generation = h.generation(task); |
| 614 | bind(&mut h, "approval:x", task).unwrap(); |
| 615 | abort(&mut h); |
| 616 | assert_eq!(h.sched.state(task), Some(TaskState::Aborted), "{how}"); |
| 617 | assert_eq!(h.generation(task), generation, "{how}"); |
| 618 | assert_eq!( |
| 619 | bound(&h, "approval:x"), |
| 620 | Some((task.to_string(), generation)), |
| 621 | "{how}" |
| 622 | ); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | #[test] |
| 627 | fn a_cancel_keeps_the_bindings_of_every_task_it_aborts() { |
| 628 | let mut h = H::journaled(enabled()); |
| 629 | h.register("a", 1, &[]); |
| 630 | h.register("b", 1, &["a"]); |
| 631 | bind(&mut h, "approval:a", "a").unwrap(); |
| 632 | bind(&mut h, "approval:b", "b").unwrap(); |
| 633 | cancel(&mut h, "a"); |
| 634 | assert_eq!(h.sched.state("b"), Some(TaskState::Aborted)); |
| 635 | assert_eq!(h.sched.approvals_len(), 2); |
| 636 | h.restart(enabled()).unwrap(); |
| 637 | assert_eq!(h.sched.approvals_len(), 2); |
| 638 | assert_eq!(bound(&h, "approval:b"), Some(("b".into(), 1))); |
| 639 | } |
| 640 | |
| 641 | #[test] |
| 642 | fn a_retry_keeps_the_old_binding_until_the_request_is_bound_again() { |
| 643 | let mut h = chain(); |
| 644 | let generation = h.generation("c"); |
| 645 | bind(&mut h, "approval:x", "c").unwrap(); |
| 646 | cancel(&mut h, "c"); |
| 647 | retry(&mut h, "c"); |
| 648 | assert_eq!(h.generation("c"), generation + 1); |
| 649 | // Still bound, at the superseded generation: the fence refuses it |
| 650 | // (`StaleGeneration`) instead of the request running unfenced. |
| 651 | assert_eq!(bound(&h, "approval:x"), Some(("c".into(), generation))); |
| 652 | bind(&mut h, "approval:x", "c").unwrap(); |
| 653 | assert_eq!(bound(&h, "approval:x"), Some(("c".into(), generation + 1))); |
| 654 | assert_eq!(h.sched.approvals_len(), 1); |
| 655 | } |
| 656 | |
| 657 | #[test] |
| 658 | fn bind_refuses_bad_keys_stale_generations_and_ended_commits() { |
| 659 | let mut h = chain(); |
| 660 | let gen_b = h.generation("b"); |
| 661 | for key in [String::new(), "k".repeat(MAX_APPROVAL_KEY_LEN + 1)] { |
| 662 | let r = bind(&mut h, &key, "b"); |
| 663 | assert!(matches!(r, Err(SchedError::InvalidEvent(_))), "{r:?}"); |
| 664 | } |
| 665 | assert!(bind(&mut h, &"k".repeat(MAX_APPROVAL_KEY_LEN), "b").is_ok()); |
| 666 | let r = h.send(EventKind::BindApproval { |
| 667 | key: "approval:old".into(), |
| 668 | task: "b".into(), |
| 669 | generation: gen_b + 1, |
| 670 | }); |
| 671 | assert!( |
| 672 | matches!(r, Err(SchedError::FutureGeneration { .. })), |
| 673 | "{r:?}" |
| 674 | ); |
| 675 | // Committing is not an end: a bind is accepted. |
| 676 | begin(&mut h, "b").unwrap(); |
| 677 | assert!(bind(&mut h, "approval:during", "b").is_ok()); |
| 678 | outcome(&mut h, "b", committed()); |
| 679 | assert!(wrong_state(&bind(&mut h, "approval:late", "b"))); |
| 680 | assert_eq!(bound(&h, "approval:late"), None); |
| 681 | |
| 682 | let mut h = chain(); |
| 683 | begin(&mut h, "b").unwrap(); |
| 684 | outcome( |
| 685 | &mut h, |
| 686 | "b", |
| 687 | CommitResult::Uncertain { |
| 688 | reason: "transport".into(), |
| 689 | }, |
| 690 | ); |
| 691 | assert!(wrong_state(&bind(&mut h, "approval:late", "b"))); |
| 692 | assert_eq!(h.sched.approvals_len(), 0); |
| 693 | // Aborted is not an end: a request may be bound to it (and is refused |
| 694 | // by the fence until a retry and a new bind). |
| 695 | cancel(&mut h, "c"); |
| 696 | assert!(bind(&mut h, "approval:aborted", "c").is_ok()); |
| 697 | assert_eq!(h.sched.approvals_len(), 1); |
| 698 | } |
| 699 | |
| 700 | #[test] |
| 701 | fn the_binding_table_is_bounded_per_task_and_fails_closed() { |
| 702 | let mut h = chain(); |
| 703 | for i in 0..MAX_APPROVAL_BINDINGS_PER_TASK { |
| 704 | bind(&mut h, &format!("approval:{i}"), "c").unwrap(); |
| 705 | } |
| 706 | assert_eq!(h.sched.approvals_len(), MAX_APPROVAL_BINDINGS_PER_TASK); |
| 707 | let r = bind(&mut h, "approval:one-more", "c"); |
| 708 | assert!( |
| 709 | matches!(r, Err(SchedError::BindingsFull { ref task, .. }) if task == "c"), |
| 710 | "{r:?}" |
| 711 | ); |
| 712 | assert_eq!(bound(&h, "approval:one-more"), None); |
| 713 | // Rebinding a key c holds replaces it. |
| 714 | assert!(bind(&mut h, "approval:0", "c").is_ok()); |
| 715 | assert_eq!(h.sched.approvals_len(), MAX_APPROVAL_BINDINGS_PER_TASK); |
| 716 | } |
| 717 | |
| 718 | #[test] |
| 719 | fn aborted_tasks_cannot_exhaust_the_table_for_other_tasks() { |
| 720 | let mut h = chain(); |
| 721 | h.register("x", 1, &[]); |
| 722 | for i in 0..MAX_APPROVAL_BINDINGS_PER_TASK { |
| 723 | bind(&mut h, &format!("approval:c-{i}"), "c").unwrap(); |
| 724 | } |
| 725 | cancel(&mut h, "c"); |
| 726 | // c keeps its full share while aborted and after a retry. |
| 727 | retry(&mut h, "c"); |
| 728 | assert_eq!(h.sched.approvals_len(), MAX_APPROVAL_BINDINGS_PER_TASK); |
| 729 | let r = bind(&mut h, "approval:c-new", "c"); |
| 730 | assert!(matches!(r, Err(SchedError::BindingsFull { .. })), "{r:?}"); |
| 731 | // Its own keys may be bound again at the new generation. |
| 732 | assert!(bind(&mut h, "approval:c-0", "c").is_ok()); |
| 733 | // An unrelated task still gets its whole share. |
| 734 | for i in 0..MAX_APPROVAL_BINDINGS_PER_TASK { |
| 735 | bind(&mut h, &format!("approval:x-{i}"), "x").unwrap(); |
| 736 | } |
| 737 | assert_eq!(h.sched.approvals_len(), 2 * MAX_APPROVAL_BINDINGS_PER_TASK); |
| 738 | let r = bind(&mut h, "approval:x-new", "x"); |
| 739 | assert!( |
| 740 | matches!(r, Err(SchedError::BindingsFull { ref task, .. }) if task == "x"), |
| 741 | "{r:?}" |
| 742 | ); |
| 743 | } |