|
2 | 2 | import type * as NodeChildProcess from "node:child_process"; |
3 | 3 | // @effect-diagnostics-next-line nodeBuiltinImport:off - the free-space probe is a plain Promise callback. |
4 | 4 | import * as NodeFS from "node:fs"; |
| 5 | +import * as NodeSqlite from "node:sqlite"; |
5 | 6 |
|
6 | 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
7 | 8 | import { assert, it } from "@effect/vitest"; |
@@ -464,6 +465,125 @@ if (context.update?.status === "pending") { |
464 | 465 | }), |
465 | 466 | ); |
466 | 467 |
|
| 468 | + it.effect.each(["wal", "delete"] as const)( |
| 469 | + "backs up a %s-mode database (online when WAL) and restores its final state", |
| 470 | + (journalMode) => |
| 471 | + Effect.gen(function* () { |
| 472 | + const fs = yield* FileSystem.FileSystem; |
| 473 | + const path = yield* Path.Path; |
| 474 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-online-" }); |
| 475 | + const statePath = path.join(root, "runtime", "service-state.json"); |
| 476 | + const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 477 | + const observationsPath = path.join(root, "observations.json"); |
| 478 | + yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 479 | + // SQLite keeps -wal beside the symlink's target, so the backup must restore it there. |
| 480 | + yield* fs.symlink("actual.sqlite", databasePath); |
| 481 | + const seed = new NodeSqlite.DatabaseSync(databasePath); |
| 482 | + seed.exec(` |
| 483 | + PRAGMA journal_mode = ${journalMode}; |
| 484 | + CREATE TABLE events (id INTEGER PRIMARY KEY, payload TEXT NOT NULL); |
| 485 | + WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 500) |
| 486 | + INSERT INTO events (payload) SELECT 'before the update ' || i FROM n; |
| 487 | + `); |
| 488 | + seed.close(); |
| 489 | + |
| 490 | + const encoded = JSON.stringify({ root, databasePath, observationsPath, journalMode }); |
| 491 | + const childSource = ` |
| 492 | +import { readdirSync, writeFileSync } from "node:fs"; |
| 493 | +import { DatabaseSync } from "node:sqlite"; |
| 494 | +const paths = ${encoded}; |
| 495 | +const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 496 | +if (context.update?.status === "pending") { |
| 497 | + writeFileSync(paths.observationsPath + ".trial", JSON.stringify( |
| 498 | + readdirSync(paths.root + "/runtime/db-backup/" + context.update.id).sort(), |
| 499 | + )); |
| 500 | + const trial = new DatabaseSync(context.update.dbPath); |
| 501 | + trial.exec("PRAGMA journal_mode = " + paths.journalMode + "; INSERT INTO events (payload) VALUES ('trial');"); |
| 502 | + trial.close(); |
| 503 | + process.exit(1); |
| 504 | +} else if (context.update === undefined) { |
| 505 | + const db = new DatabaseSync(paths.databasePath); |
| 506 | + db.exec("PRAGMA busy_timeout = 5000;"); |
| 507 | + db.exec("PRAGMA journal_mode = " + paths.journalMode + ";"); |
| 508 | + const insert = db.prepare("INSERT INTO events (payload) VALUES (?)"); |
| 509 | + const writer = setInterval(() => insert.run("written while serving"), 1); |
| 510 | + process.once("SIGTERM", () => { |
| 511 | + clearInterval(writer); |
| 512 | + insert.run("last write before stopping"); |
| 513 | + const backupRoot = paths.root + "/runtime/db-backup"; |
| 514 | + writeFileSync(paths.observationsPath, JSON.stringify({ |
| 515 | + backupsAtStop: readdirSync(backupRoot).map((entry) => readdirSync(backupRoot + "/" + entry)), |
| 516 | + rows: db.prepare("SELECT count(*) AS n FROM events").get().n, |
| 517 | + })); |
| 518 | + db.close(); |
| 519 | + process.exit(0); |
| 520 | + }); |
| 521 | + process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: paths.databasePath }); |
| 522 | +} else { |
| 523 | + process.exit(0); |
| 524 | +} |
| 525 | +`; |
| 526 | + for (const version of ["1.0.0", "1.1.0"]) { |
| 527 | + yield* writeFakeRuntime( |
| 528 | + fs, |
| 529 | + path, |
| 530 | + path.join(root, "runtime", "versions", version), |
| 531 | + childSource, |
| 532 | + ); |
| 533 | + } |
| 534 | + yield* Effect.promise(() => |
| 535 | + writeServiceState(statePath, { |
| 536 | + protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 537 | + activeVersion: "1.0.0", |
| 538 | + }), |
| 539 | + ); |
| 540 | + |
| 541 | + const launcher = new Launcher( |
| 542 | + root, |
| 543 | + yield* Effect.promise(() => readServiceState(statePath)), |
| 544 | + { |
| 545 | + backupSpaceProbe: { canClone: async () => false }, |
| 546 | + }, |
| 547 | + ); |
| 548 | + yield* Effect.promise(() => |
| 549 | + launcher.run().then( |
| 550 | + () => Promise.reject(new Error("launcher unexpectedly completed")), |
| 551 | + () => Promise.resolve(), |
| 552 | + ), |
| 553 | + ); |
| 554 | + |
| 555 | + const state = yield* Effect.promise(() => readServiceState(statePath)); |
| 556 | + assert.equal(state.update?.status, "rolled-back"); |
| 557 | + const observed = JSON.parse(yield* fs.readFileString(observationsPath)); |
| 558 | + // A WAL database's main file is copied before the server is asked to stop; others fall back offline. |
| 559 | + assert.deepEqual(observed.backupsAtStop, journalMode === "wal" ? [["database"]] : []); |
| 560 | + assert.isAbove(observed.rows, 501); |
| 561 | + // Only the online copy holds no -shm: the offline fallback copies the one the server left. |
| 562 | + assert.deepEqual( |
| 563 | + JSON.parse(yield* fs.readFileString(`${observationsPath}.trial`)), |
| 564 | + journalMode === "wal" ? ["database", "database-wal"] : ["database"], |
| 565 | + ); |
| 566 | + const restored = new NodeSqlite.DatabaseSync(databasePath); |
| 567 | + try { |
| 568 | + assert.equal(restored.prepare("PRAGMA integrity_check").get()?.integrity_check, "ok"); |
| 569 | + assert.equal( |
| 570 | + restored.prepare("SELECT count(*) AS n FROM events").get()?.n, |
| 571 | + observed.rows, |
| 572 | + ); |
| 573 | + assert.equal( |
| 574 | + restored |
| 575 | + .prepare( |
| 576 | + "SELECT count(*) AS n FROM events WHERE payload = 'last write before stopping'", |
| 577 | + ) |
| 578 | + .get()?.n, |
| 579 | + 1, |
| 580 | + ); |
| 581 | + } finally { |
| 582 | + restored.close(); |
| 583 | + } |
| 584 | + }), |
| 585 | + ); |
| 586 | + |
467 | 587 | const requestRejectedUpdate = (backupSpaceProbe: Partial<BackupSpaceProbe>) => |
468 | 588 | Effect.gen(function* () { |
469 | 589 | const fs = yield* FileSystem.FileSystem; |
@@ -670,7 +790,7 @@ setInterval(() => {}, 1_000); |
670 | 790 | assert.include((yield* check(0.5 * 1024 ** 3, true)) ?? "", "1.0 GB needed, 0.5 GB free"); |
671 | 791 | const blocker = yield* check(8 * 1024 ** 3, false); |
672 | 792 | assert.include(blocker ?? "", "Not enough free disk space"); |
673 | | - assert.include(blocker ?? "", "10.5 GB needed, 8.0 GB free"); |
| 793 | + assert.include(blocker ?? "", "10.8 GB needed, 8.0 GB free"); |
674 | 794 | assert.include(blocker ?? "", "The server was not stopped"); |
675 | 795 | }), |
676 | 796 | ); |
|
0 commit comments