Public: anyone can read this; the crew can change it.
Putting idle forges to sleep is one at a time: N idle forges take N times 12 to 15 s #13829
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13815-putting-idle-forges-to-sleep-is-one-at-a → main 3 commits fork 1 thread · 1 unresolved
3 files
changed +69
−13
src/spawn/cell.ts +23 −11
| @@ -261,9 +261,15 @@ export interface CellChildren { | ||
| 261 | 261 | * Stop every running child quiet for `idleMs`; the names of those stopped (their data stays on |
| 262 | 262 | * disk). |
| 263 | 263 | */ |
| 264 | sleepIdle(idleMs: number, now?: number): Promise<string[]>; | |
| 264 | sleepIdle(idleMs: number, now?: number, onAsleep?: (name: string, bodyId: string) => void): Promise<string[]>; | |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | /** | |
| 268 | * How many idle children are stopped at once: each stop may wait out a grace period, so one at a | |
| 269 | * time would add them up. | |
| 270 | */ | |
| 271 | export const SLEEP_AT_ONCE = 8; | |
| 272 | ||
| 267 | 273 | const registries = new WeakMap<App, CellChildren>(); |
| 268 | 274 | |
| 269 | 275 | export function cellChildren(parent: App): CellChildren { |
| @@ -527,17 +533,23 @@ export function makeRegistry(parent: App, launcher?: Launcher): CellChildren { | ||
| 527 | 533 | touch(bodyId, now = Date.now()) { |
| 528 | 534 | if (held.has(bodyId)) seen.set(bodyId, now); |
| 529 | 535 | }, |
| 530 | async sleepIdle(idleMs, now = Date.now()) { | |
| 536 | async sleepIdle(idleMs, now = Date.now(), onAsleep) { | |
| 531 | 537 | const asleep: string[] = []; |
| 532 | for (const child of [...held.values()]) { | |
| 533 | if (isNewForge(startedAt.get(child.bodyId), now)) continue; | |
| 534 | if (now - (seen.get(child.bodyId) ?? now) < idleMs || (busy.get(child.bodyId) ?? 0) > 0) continue; | |
| 535 | // Asked of its database off this thread: the file is the child's to write (`child_db.ts`). | |
| 536 | if (await childCrewAtWork(childDirOf(parent, child.bodyId), now)) continue; | |
| 537 | asleep.push(child.name); | |
| 538 | seen.delete(child.bodyId); | |
| 539 | await this.stop(child.bodyId); | |
| 540 | } | |
| 538 | const queue = [...held.values()]; | |
| 539 | const worker = async (): Promise<void> => { | |
| 540 | for (let child = queue.shift(); child !== undefined; child = queue.shift()) { | |
| 541 | if (isNewForge(startedAt.get(child.bodyId), now)) continue; | |
| 542 | if (now - (seen.get(child.bodyId) ?? now) < idleMs || (busy.get(child.bodyId) ?? 0) > 0) continue; | |
| 543 | // Asked of its database off this thread: the file is the child's to write | |
| 544 | // (`child_db.ts`). | |
| 545 | if (await childCrewAtWork(childDirOf(parent, child.bodyId), now)) continue; | |
| 546 | seen.delete(child.bodyId); | |
| 547 | await this.stop(child.bodyId); | |
| 548 | asleep.push(child.name); | |
| 549 | onAsleep?.(child.name, child.bodyId); | |
| 550 | } | |
| 551 | }; | |
| 552 | await Promise.all(Array.from({ length: Math.min(SLEEP_AT_ONCE, queue.length) }, worker)); | |
| 541 | 553 | return asleep; |
| 542 | 554 | }, |
| 543 | 555 | }; |
src/spawn/host.ts +2 −2
| @@ -228,7 +228,7 @@ export async function sleepIdleChildren(parent: App, idleMs = idleAfterMs(), now | ||
| 228 | 228 | // A forge that was in this process is asleep: the moment it can be moved into a cell of its own |
| 229 | 229 | // (`spawn/move_cell.ts`). |
| 230 | 230 | if (asleep.length > 0) sleepHooks.get(parent)?.(); |
| 231 | const cellsAsleep = await cellChildren(parent).sleepIdle(idleMs, now); | |
| 231 | const cellsAsleep = await cellChildren(parent).sleepIdle(idleMs, now, (name) => parent.log.info("child forge asleep", { name })); | |
| 232 | 232 | for (const [bodyId, mark] of [...(touched.get(parent) ?? [])]) if (cellsAsleep.includes(mark.name)) { |
| 233 | 233 | touched.get(parent)?.delete(bodyId); |
| 234 | 234 | waking.get(parent)?.delete(bodyId); |
| @@ -240,7 +240,7 @@ export async function sleepIdleChildren(parent: App, idleMs = idleAfterMs(), now | ||
| 240 | 240 | if (ledger !== undefined && due !== undefined) wakeWhenDue(parent, ledger, due, now); |
| 241 | 241 | } |
| 242 | 242 | asleep.push(...cellsAsleep); |
| 243 | for (const name of asleep) parent.log.info("child forge asleep", { name }); | |
| 243 | for (const name of asleep.filter((name) => !cellsAsleep.includes(name))) parent.log.info("child forge asleep", { name }); | |
| 244 | 244 | return asleep; |
| 245 | 245 | } |
| 246 | 246 |
test/spawn-sleep-together.test.ts added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp } from "../src/app.ts"; | |
| 6 | import { makeRegistry, SLEEP_AT_ONCE, type CellChild } from "../src/spawn/cell.ts"; | |
| 7 | ||
| 8 | test("idle cells are stopped together, a bounded number at a time, each named as it sleeps", async () => { | |
| 9 | const dir = mkdtempSync(join(tmpdir(), "sleep-together-")); | |
| 10 | const parent = createApp({ dataDir: dir, url: "https://f.test" }); | |
| 11 | const procs: Array<ReturnType<typeof Bun.spawn>> = []; | |
| 12 | let live = 0; | |
| 13 | let peak = 0; | |
| 14 | const cells = makeRegistry(parent, async (input) => { | |
| 15 | const proc = Bun.spawn(["sleep", "30"]); | |
| 16 | procs.push(proc); | |
| 17 | // A child that takes 300 ms to leave once asked. | |
| 18 | const exited = proc.exited.then(async () => { | |
| 19 | live++; | |
| 20 | peak = Math.max(peak, live); | |
| 21 | await Bun.sleep(300); // real-timer: the child must take real time to leave so overlap and elapsed time can be measured | |
| 22 | live--; | |
| 23 | return 0; | |
| 24 | }); | |
| 25 | return { ...input, port: 1, pid: proc.pid, exited } as unknown as CellChild; | |
| 26 | }); | |
| 27 | try { | |
| 28 | const count = SLEEP_AT_ONCE + 2; | |
| 29 | for (let i = 0; i < count; i++) await cells.start({ bodyId: `b${i}`, name: `k${i}`, size: "small" }); | |
| 30 | const named: string[] = []; | |
| 31 | const began = Date.now(); | |
| 32 | const asleep = await cells.sleepIdle(1000, Date.now() + 3_600_000, (name) => named.push(name)); | |
| 33 | expect(asleep.length).toBe(count); | |
| 34 | expect(named.sort()).toEqual(asleep.slice().sort()); | |
| 35 | expect(cells.list()).toEqual([]); | |
| 36 | expect(peak).toBeLessThanOrEqual(SLEEP_AT_ONCE); | |
| 37 | // One at a time would be count * 300 ms. | |
| 38 | expect(Date.now() - began).toBeLessThan(count * 300 - 600); | |
| 39 | } finally { | |
| 40 | for (const proc of procs) proc.kill(); | |
| 41 | parent.close(); | |
| 42 | rmSync(dir, { recursive: true, force: true }); | |
| 43 | } | |
| 44 | }); |
new above
offline
Filtering by name hides an in-process forge that shares a name with a cell. Log the in-process names before the push of cellsAsleep rather than filtering after.