hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiBc9b707aAhmet Kaan Gümüş4 months ago189 commits
| 1 | //! SurrealDB connection (WebSocket to a server; in-memory for tests) + schema. |
| 2 | |
| 3 | use Surreal; |
| 4 | use Any; |
| 5 | use Root; |
| 6 | |
| 7 | use crateConfig; |
| 8 | use crate; |
| 9 | use crateTimestamp; |
| 10 | use crate; |
| 11 | use crateAppError; |
| 12 | use crateMIGRATION_BATCHES; |
| 13 | |
| 14 | /// The shared database handle. |
| 15 | /// |
| 16 | /// `Arc` is load-bearing, not decoration. `Surreal`'s own `Clone` mints a |
| 17 | /// fresh server-side session per clone (`Uuid::new_v4()` + a fire-and-forget |
| 18 | /// `SessionId::Clone` to the router, SDK `lib.rs:340`), and axum clones the |
| 19 | /// application state on every request — so a bare `Surreal<Any>` here means a |
| 20 | /// new session, and a matching `SessionId::Drop`, for each request served. |
| 21 | /// Under concurrency those lifecycle events race the queries riding on them: |
| 22 | /// sessions disappear ("Session not found"), and a session whose signin has |
| 23 | /// not been replayed answers "Anonymous access not allowed" / "Specify a |
| 24 | /// namespace" — the same strings [`crate::error::is_session_replay_error`] |
| 25 | /// treats as a transient reconnect, so the failures masquerade as one. |
| 26 | /// |
| 27 | /// Cloning the `Arc` shares the single session established at boot instead. |
| 28 | pub type Database = Arc; |
| 29 | |
| 30 | /// Connect to the SurrealDB server, sign in as root, apply the schema and seed |
| 31 | /// the admin. One process, one database, stop-the-world deploys — so all of it |
| 32 | /// runs unconditionally on every boot. |
| 33 | pub async |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | /// The `ADMIN_USERNAME` / `ADMIN_PASSWORD` bootstrap pair, if configured. |
| 56 | /// Both-or-neither: half a pair aborts startup rather than silently running |
| 57 | /// without the seed. |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | /// The two ways a rival's claim shows up as an error on ours: our `CREATE` hit |
| 74 | /// their row, or the key-value layer aborted our transaction because theirs |
| 75 | /// touched the same key first. |
| 76 | /// |
| 77 | /// Read from the *typed* error surface, not the message. Every conflict wording |
| 78 | /// the storage engines produce — surrealkv's "Transaction conflict: Write |
| 79 | /// conflict, retry the transaction. This transaction can be retried", |
| 80 | /// surrealmx's read-conflict variant, RocksDB's `Busy`/`TryAgain` — funnels |
| 81 | /// through one `KvsError::TransactionConflict` into |
| 82 | /// `QueryError::TransactionConflict` (surrealdb-core 3.2.3 |
| 83 | /// `err/to_types.rs:295`, `kvs/err.rs:64-179`), and a duplicate record id into |
| 84 | /// `AlreadyExistsError::Record` (`to_types.rs:206`). So the two checks below |
| 85 | /// cover all of them, where matching substrings covered only whichever wording |
| 86 | /// the engine of the day happened to use. |
| 87 | /// |
| 88 | /// The message fallback stays for one reason: these errors also travel over the |
| 89 | /// WebSocket, where the SDK rebuilds them from the wire (`Error::from_parts`), |
| 90 | /// and a server that omitted the typed details would otherwise turn a lost race |
| 91 | /// into a failed boot. Widest known wordings, cheap, and belt-and-braces. |
| 92 | pub |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | /// Send one guarded single-statement write, re-sending it while the store |
| 107 | /// answers "conflict, retry", and hand back the rows it returned. |
| 108 | /// |
| 109 | /// The guard on such a statement reads a column a rival writes, so the two |
| 110 | /// contend on one record *by design* — that contention is what makes the guard |
| 111 | /// atomic. Losing a round writes nothing at all, which makes re-sending the |
| 112 | /// whole of the recovery; without it an ordinary raced `PATCH` or `DELETE` |
| 113 | /// answers 500 instead of the 404 or 409 it owes. Only for statements that |
| 114 | /// cannot produce "already exists" (the other thing [`lost_the_race`] matches), |
| 115 | /// so `UPDATE` and `DELETE` and not `CREATE`. |
| 116 | pub async |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | /// Send one guarded `BEGIN…COMMIT` cascade, re-sending it while the store |
| 140 | /// answers "conflict, retry", and hand back both the response and the errors |
| 141 | /// drained off it. |
| 142 | /// |
| 143 | /// [`write_with_retry`]'s story, for the cascades that cannot be one statement: |
| 144 | /// the guard reads a column a rival writes, so the two contend on one record by |
| 145 | /// design, and a lost round aborts the transaction having written *nothing* — |
| 146 | /// which is what makes re-sending the whole of the recovery. |
| 147 | /// |
| 148 | /// Both halves come back because the caller needs both and `take_errors` |
| 149 | /// consumes: the response carries the result slots, and only the error map can |
| 150 | /// tell a deliberate `THROW` from a lost round. `refusals` are the caller's |
| 151 | /// `THROW` markers and they are matched *first* — a `THROW` is a decision, and |
| 152 | /// it outranks a conflict. Everything after that ordering is why this exists: |
| 153 | /// an aborted transaction errors *every* slot, and all but the failing one say |
| 154 | /// a generic "not executed", which is the consequence of the abort and never a |
| 155 | /// reason of its own. Picking one of those out of the unordered `HashMap` — or |
| 156 | /// taking the first via `check()` — retired a retryable round as a 500, measured |
| 157 | /// on `Course::delete` at 1-3 of 20 raced rounds. |
| 158 | /// |
| 159 | /// [`write_with_retry`]'s restriction carries over, and a cascade makes it |
| 160 | /// easier to trip: no statement inside may be able to answer "already exists", |
| 161 | /// because that is the other thing [`lost_the_race`] matches and re-sending it |
| 162 | /// would just fail the same way until the tries run out — turning a 409 into a |
| 163 | /// 500, which is the exact defect this exists to prevent. The test to apply to |
| 164 | /// every statement in the batch is not which verb it uses but whether the store |
| 165 | /// could *legitimately* answer "already exists" to it — that answer is a |
| 166 | /// decision, and this loop cannot tell one from a conflict, so it would burn all |
| 167 | /// the tries re-asking a question already settled. `UPDATE` and `DELETE` never |
| 168 | /// can. A `CREATE` can, unless the id is unreachable by any rival: a freshly |
| 169 | /// generated ULID on a table with no `UNIQUE` index is such an id. An `UPSERT` |
| 170 | /// can, unless its id is *bijective* with every unique tuple its table indexes — |
| 171 | /// then the index entry can only ever point at the row the id already names, and |
| 172 | /// the write resolves onto it instead of colliding with it |
| 173 | /// ([`crate::domain::exam_result::ExamResult`]'s composite id is that shape). |
| 174 | /// A statement that fails this test belongs in front of the cascade, where its |
| 175 | /// 409 can be read as the answer it is. |
| 176 | pub async |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | /// Wait out one lost round. Exponential with jitter, because racers arrive in |
| 219 | /// lockstep (one HTTP burst) and a fixed delay would just re-synchronize them. |
| 220 | /// Attempt zero waits not at all. |
| 221 | pub async |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | /// Dial the database, retrying until it answers. |
| 231 | /// |
| 232 | /// Never gives up, because giving up is worse than waiting: the database is |
| 233 | /// normally a sibling container booting in parallel, and a process that exits |
| 234 | /// on the first refusal gets restarted by the runtime, fails again in ~100ms, |
| 235 | /// and burns its whole restart budget inside a second — leaving the backend |
| 236 | /// permanently down over a startup skew that would have cleared on its own. |
| 237 | /// |
| 238 | /// Only the dial is retried. Bad credentials or a broken migration still fail |
| 239 | /// hard, since no amount of waiting fixes those. |
| 240 | async |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | /// A fresh in-memory database with the schema applied. For tests. |
| 258 | pub async |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | /// A handle on a **real** SurrealDB server, in a scratch namespace of its own |
| 266 | /// that is dropped and re-made on every call. For the tests whose subject is |
| 267 | /// the store's own conflict detection, which [`init_mem`]'s embedded engine |
| 268 | /// does not have: it drops one of two concurrent writes to a record and answers |
| 269 | /// `Ok` to both (see [`crate::domain::cap`]), which *forges* the very |
| 270 | /// integrity failure those tests exist to catch — measured 2026-07-30 at 2 |
| 271 | /// failed runs in 36 on `memory`, against 0 in 10 000 rounds here. |
| 272 | /// |
| 273 | /// Every caller is `#[ignore]`d, so a machine with no server prints them as |
| 274 | /// `ignored` rather than passing: this is not a test that may quietly skip. |
| 275 | /// `HEZARFEN_TEST_DB` overrides the address; the credentials are `compose.yaml`'s. |
| 276 | /// |
| 277 | /// The guard handed back with the handle serializes these tests against each |
| 278 | /// other and must be held for the whole test — hence the tuple, which cannot be |
| 279 | /// forgotten the way a separate `lock()` line can. `cargo test -- --ignored` |
| 280 | /// runs them in parallel, and while each has a namespace to itself they all |
| 281 | /// burst against the *one* server: their beats are sub-millisecond, so a |
| 282 | /// sibling's burst pushes a delete clean out of the window it is meant to land |
| 283 | /// in (measured 4 runs in 4 at 3 of 6 failing their own "the race was reached" |
| 284 | /// guard). Serialized, the plain documented command works. |
| 285 | |
| 286 | pub static RACE_LOCK: Mutex = const_new; |
| 287 | |
| 288 | |
| 289 | pub async |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | /// Apply the schema + backfills. Idempotent — `init` runs it on every boot, |
| 317 | /// and tests re-run it on a live handle to simulate a second boot. |
| 318 | pub async |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | /// Parameters the batches are executed with. Bound, not baked into the SQL |
| 330 | /// string: a `const` cannot be interpolated into another `const`, and a |
| 331 | /// hand-copied 300000 would drift silently. |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 |