Public: anyone can read this; the crew can change it.
A woken cell starts in seconds and on the release that is serving #16481
Check · Review · Land · Live done — Landed and live in production.
sold/wake-fix → main 0 commits 2 threads · 2 unresolved
5 files
changed +113
−15
src/runs/peer.ts +17 −2
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import type { Database } from "bun:sqlite"; |
| 19 | 19 | import type { Log } from "../kernel/log.ts"; |
| 20 | import { SIZE_ENV } from "../spawn/usage.ts"; | |
| 20 | 21 | import { HEARTBEAT_MS, heldRuns } from "./lease.ts"; |
| 21 | 22 | |
| 22 | 23 | /** Two renewals missed: the lease is not being kept. */ |
| @@ -29,6 +30,19 @@ export const PEER_WAIT_MAX_MS = 720_000; | ||
| 29 | 30 | */ |
| 30 | 31 | const POLL_MS = 1_000; |
| 31 | 32 | |
| 33 | /** | |
| 34 | * A cell is a child forge its parent stops and starts on its own data; a predecessor that is gone | |
| 35 | * stops renewing within one fresh window, so a cell waits that long for it, not for the pod's | |
| 36 | * grace (a woken cell took four minutes to ready its crew on 2026-11-25). | |
| 37 | */ | |
| 38 | export const CELL_PEER_WAIT_MAX_MS = PEER_FRESH_MS; | |
| 39 | ||
| 40 | /** How long this process holds its boot for a peer: the pod's grace, or a cell's one window. */ | |
| 41 | export function peerWaitCeiling(env: Readonly<Record<string, string | undefined>> = process.env): number { | |
| 42 | const cell = env[SIZE_ENV]; | |
| 43 | return cell !== undefined && cell !== "" ? CELL_PEER_WAIT_MAX_MS : PEER_WAIT_MAX_MS; | |
| 44 | } | |
| 45 | ||
| 32 | 46 | /** Running runs whose lease another process renewed within `PEER_FRESH_MS` of `now`. */ |
| 33 | 47 | export function peerRuns(db: Database, now = Date.now()): number { |
| 34 | 48 | const here = heldRuns(db); |
| @@ -52,17 +66,18 @@ export type PeerOutcome = "alone" | "gone" | "waited out"; | ||
| 52 | 66 | export async function waitForPeer(db: Database, log: Log, opts: PeerWait = {}): Promise<PeerOutcome> { |
| 53 | 67 | const now = opts.now ?? Date.now; |
| 54 | 68 | const sleep = opts.sleep ?? Bun.sleep; |
| 55 | const maxMs = opts.maxMs ?? PEER_WAIT_MAX_MS; | |
| 69 | const maxMs = opts.maxMs ?? peerWaitCeiling(); | |
| 56 | 70 | const started = now(); |
| 57 | 71 | let live = peerRuns(db, started); |
| 58 | 72 | if (live === 0) return "alone"; |
| 59 | 73 | log.warn("another forge still works this database: this one serves, and starts its own work once it lets go", { |
| 60 | 74 | runs: live, |
| 75 | cell: maxMs < PEER_WAIT_MAX_MS, | |
| 61 | 76 | maxSeconds: Math.round(maxMs / 1000), |
| 62 | 77 | }); |
| 63 | 78 | while (live > 0) { |
| 64 | 79 | if (now() - started >= maxMs) { |
| 65 | log.warn("another forge still works this database; starting beside it", { runs: live }); | |
| 80 | log.warn("another forge still works this database; starting beside it", { runs: live, waitedSeconds: Math.round((now() - started) / 1000) }); | |
| 66 | 81 | return "waited out"; |
| 67 | 82 | } |
| 68 | 83 | await sleep(opts.pollMs ?? POLL_MS); |
src/spawn/cell.ts +21 −11
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | import { namedRunner } from "../crew/runner_env.ts"; |
| 12 | import { existsSync } from "node:fs"; | |
| 12 | 13 | import { tmpdir } from "node:os"; |
| 13 | 14 | import { dirname, join } from "node:path"; |
| 14 | 15 | import { clientAddress, namesClient, nameClient } from "../http/peer.ts"; |
| @@ -18,6 +19,8 @@ import { cellsAvailable, commandPid, killTree, openCell, type Cell, type CellSpe | ||
| 18 | 19 | import { childEnv, freePort, lineCutter } from "../apps/process.ts"; |
| 19 | 20 | import { childCommand } from "../settings/child_heap.ts"; |
| 20 | 21 | import { childDirOf } from "../actions/spawn.ts"; |
| 22 | import { releaseDir } from "../release/checkout.ts"; | |
| 23 | import { alive, readServing } from "../release/serving.ts"; | |
| 21 | 24 | import { openBeatDoor, parentLocal, type BeatDoor } from "./beat-door.ts"; |
| 22 | 25 | import { hostedPlacement, parentOrigin, childPublicUrl, type BodyStore, type Placement } from "./placement.ts"; |
| 23 | 26 | import { SIZE_IO, SIZES, type Size } from "./sizes.ts"; |
| @@ -100,15 +103,6 @@ export async function stopGently(child: { readonly pid: number; readonly exited: | ||
| 100 | 103 | await child.exited; |
| 101 | 104 | } |
| 102 | 105 | |
| 103 | function alive(pid: number): boolean { | |
| 104 | try { | |
| 105 | process.kill(pid, 0); | |
| 106 | return true; | |
| 107 | } catch { | |
| 108 | return false; | |
| 109 | } | |
| 110 | } | |
| 111 | ||
| 112 | 106 | /** A forge started within this long is not put to sleep by the idle sweep (#11155). */ |
| 113 | 107 | export const NEW_FORGE_MS = 10 * 60_000; |
| 114 | 108 | |
| @@ -118,6 +112,20 @@ export const START_BACKOFF_MS = 30_000; | ||
| 118 | 112 | const MAIN = join(import.meta.dir, "..", "main.ts"); |
| 119 | 113 | const CODE = join(import.meta.dir, "..", ".."); |
| 120 | 114 | |
| 115 | /** | |
| 116 | * The tree the parent serves from now. A release swap starts the new tree from `releases/<sha>/` | |
| 117 | * and names it in `release/serving.json`; a cell woken after that runs that tree, not the paths | |
| 118 | * this process was loaded from (the old release's, while it drains). Today's paths when no swap | |
| 119 | * has named one, its process is gone, or its tree is not there. | |
| 120 | */ | |
| 121 | export function servingTree(dataDir: string, own: { main: string; root: string } = { main: MAIN, root: CODE }): { main: string; root: string; sha?: string } { | |
| 122 | const s = readServing(dataDir); | |
| 123 | if (s === undefined || !alive(s.pid)) return own; | |
| 124 | const root = releaseDir(dataDir, s.sha); | |
| 125 | const main = join(root, "src", "main.ts"); | |
| 126 | return existsSync(main) ? { main, root, sha: s.sha } : own; | |
| 127 | } | |
| 128 | ||
| 121 | 129 | /** A size's limits as a cell holds them. */ |
| 122 | 130 | export function cellLimits(size: Size): Limits { |
| 123 | 131 | const s = SIZES[size]; |
| @@ -259,6 +267,8 @@ export interface Stamp { | ||
| 259 | 267 | } |
| 260 | 268 | |
| 261 | 269 | export function stampFor(source: Pick<CodeSource, "kind" | "commit">, parent: Running = running()): Stamp { |
| 270 | // The parent's tree from a swapped release: that commit, and no version this process does not run. | |
| 271 | if (source.kind === "parent" && source.commit !== undefined && source.commit !== parent.commit) return { commit: source.commit }; | |
| 262 | 272 | if (source.kind === "parent") return { ...(parent.version === UNSTAMPED ? {} : { version: parent.version }), ...(parent.commit === null ? {} : { commit: parent.commit }) }; |
| 263 | 273 | return source.commit === undefined || source.commit === "" ? {} : { commit: source.commit }; |
| 264 | 274 | } |
| @@ -542,7 +552,7 @@ export function makeRegistry(parent: App, launcher?: Launcher): CellChildren { | ||
| 542 | 552 | const url = childPublicUrl(input.name, parentOrigin(parent.config)); |
| 543 | 553 | const door = openBeatDoor(parent); |
| 544 | 554 | const parentPort = door.port; |
| 545 | const spec = launchSpecFor(parent, input, { port, parentPort }, undefined, source.kind === "parent" ? undefined : source.root); | |
| 555 | const spec = launchSpecFor(parent, input, { port, parentPort }, undefined, source.kind === "parent" && source.root === CODE ? undefined : source.root); | |
| 546 | 556 | const heard = childLog((line) => parent.log.info("child forge", { name: input.name, line })); |
| 547 | 557 | const log = (raw: string): void => { |
| 548 | 558 | const line = plainLine(raw); |
| @@ -615,7 +625,7 @@ export function makeRegistry(parent: App, launcher?: Launcher): CellChildren { | ||
| 615 | 625 | // The parent decides what the cell runs: its own code, the last that came up, or the parent's |
| 616 | 626 | // (`own_code.ts`). |
| 617 | 627 | const started = await oneAtATime(parent, input.name, async () => { |
| 618 | const sources = await sourcesFor(parent, { name: input.name, childDir: childDirOf(parent, input.bodyId) }, { main: MAIN, root: CODE }); | |
| 628 | const sources = await sourcesFor(parent, { name: input.name, childDir: childDirOf(parent, input.bodyId) }, servingTree(parent.config.dataDir)); | |
| 619 | 629 | return startFirstThatWorks(parent, input.name, sources, (source) => startOn(input, source)); |
| 620 | 630 | }); |
| 621 | 631 | if ("why" in started) { |
src/spawn/own_code.ts +2 −2
| @@ -407,9 +407,9 @@ export function recordGood(app: App, name: string, commit: string): void { | ||
| 407 | 407 | export async function sourcesFor( |
| 408 | 408 | app: App, |
| 409 | 409 | input: { readonly name: string; readonly childDir: string }, |
| 410 | parent: { readonly main: string; readonly root: string }, | |
| 410 | parent: { readonly main: string; readonly root: string; readonly sha?: string }, | |
| 411 | 411 | ): Promise<CodeSource[]> { |
| 412 | const fallback: CodeSource = { kind: "parent", ...parent }; | |
| 412 | const fallback: CodeSource = { kind: "parent", main: parent.main, root: parent.root, ...(parent.sha === undefined ? {} : { commit: parent.sha }) }; | |
| 413 | 413 | const state = readOwnCode(app, input.name); |
| 414 | 414 | const repo = ownRepoOf(input.childDir); |
| 415 | 415 | if (!state.on || repo === undefined) return [fallback]; |
test/wake-peer-cap.test.ts added +33
| @@ -0,0 +1,33 @@ | ||
| 1 | import { Database } from "bun:sqlite"; | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { createLog } from "../src/kernel/log.ts"; | |
| 4 | import { CELL_PEER_WAIT_MAX_MS, PEER_FRESH_MS, PEER_WAIT_MAX_MS, peerWaitCeiling, waitForPeer } from "../src/runs/peer.ts"; | |
| 5 | ||
| 6 | test("a cell waits one heartbeat window for a predecessor; the parent keeps the pod's grace", () => { | |
| 7 | expect(peerWaitCeiling({})).toBe(PEER_WAIT_MAX_MS); | |
| 8 | expect(peerWaitCeiling({ FORGE_CELL_SIZE: "small" })).toBe(CELL_PEER_WAIT_MAX_MS); | |
| 9 | expect(CELL_PEER_WAIT_MAX_MS).toBe(PEER_FRESH_MS); | |
| 10 | }); | |
| 11 | ||
| 12 | test("a cell's wait ends at its ceiling while a peer keeps its run fresh", async () => { | |
| 13 | const prior = process.env["FORGE_CELL_SIZE"]; | |
| 14 | process.env["FORGE_CELL_SIZE"] = "small"; | |
| 15 | try { | |
| 16 | const db = new Database(":memory:"); | |
| 17 | db.run("CREATE TABLE run (id TEXT PRIMARY KEY, state TEXT, heartbeat_at TEXT)"); | |
| 18 | let clock = 1_000_000; | |
| 19 | db.run("INSERT INTO run VALUES ('r1','running',?1)", [new Date(clock).toISOString()]); | |
| 20 | const out = await waitForPeer(db, createLog("peer-test"), { | |
| 21 | now: () => clock, | |
| 22 | sleep: async (ms) => { | |
| 23 | clock += ms; | |
| 24 | db.run("UPDATE run SET heartbeat_at = ?1", [new Date(clock).toISOString()]); | |
| 25 | }, | |
| 26 | }); | |
| 27 | expect(out).toBe("waited out"); | |
| 28 | expect(clock - 1_000_000).toBeLessThanOrEqual(CELL_PEER_WAIT_MAX_MS + 1_000); | |
| 29 | } finally { | |
| 30 | if (prior === undefined) delete process.env["FORGE_CELL_SIZE"]; | |
| 31 | else process.env["FORGE_CELL_SIZE"] = prior; | |
| 32 | } | |
| 33 | }); |
test/wake-serving-tree.test.ts added +40
| @@ -0,0 +1,40 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { servingTree, stampFor } from "../src/spawn/cell.ts"; | |
| 6 | ||
| 7 | const own = { main: "/old/src/main.ts", root: "/old" }; | |
| 8 | const sha = "a".repeat(40); | |
| 9 | ||
| 10 | function data(serving?: { pid: number; sha: string }, tree = true): string { | |
| 11 | const dir = mkdtempSync(join(tmpdir(), "wake-")); | |
| 12 | if (serving) { | |
| 13 | mkdirSync(join(dir, "release"), { recursive: true }); | |
| 14 | writeFileSync(join(dir, "release", "serving.json"), JSON.stringify(serving)); | |
| 15 | } | |
| 16 | if (tree) { | |
| 17 | mkdirSync(join(dir, "releases", sha, "src"), { recursive: true }); | |
| 18 | writeFileSync(join(dir, "releases", sha, "src", "main.ts"), ""); | |
| 19 | } | |
| 20 | return dir; | |
| 21 | } | |
| 22 | ||
| 23 | test("a waking cell reads the tree the swapped release serves from", () => { | |
| 24 | const dir = data({ pid: process.pid, sha }); | |
| 25 | const t = servingTree(dir, own); | |
| 26 | expect(t.root).toBe(join(dir, "releases", sha)); | |
| 27 | expect(t.main).toBe(join(dir, "releases", sha, "src", "main.ts")); | |
| 28 | expect(t.sha).toBe(sha); | |
| 29 | }); | |
| 30 | ||
| 31 | test("with no swap named, a dead server, or no tree, today's paths stand", () => { | |
| 32 | expect(servingTree(data(), own)).toEqual(own); | |
| 33 | expect(servingTree(data({ pid: 2 ** 22 + 12345, sha }), own)).toEqual(own); | |
| 34 | expect(servingTree(data({ pid: process.pid, sha }, false), own)).toEqual(own); | |
| 35 | }); | |
| 36 | ||
| 37 | test("a cell on a swapped release's tree is stamped with that commit, not the old version", () => { | |
| 38 | expect(stampFor({ kind: "parent", commit: sha }, { version: "0.7.1", commit: "b".repeat(40) })).toEqual({ commit: sha }); | |
| 39 | expect(stampFor({ kind: "parent", commit: sha }, { version: "0.7.1", commit: sha })).toEqual({ version: "0.7.1", commit: sha }); | |
| 40 | }); |
new above
offline
Duplicate of
SIZE_ENVin src/spawn/usage.ts. Import it, or share it from a leaf module.