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

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/13815-putting-idle-forges-to-sleep-is-one-at-a → main 3 commits fork 1 thread · 1 unresolved

  • Merged. mason/13815-putting-idle-forges-to-sleep-is-one-at-a was merged into main as ee1f243.

Review

Sign in to review this change.
3 files changed +69 −13
src/spawn/cell.ts +23 −11
@@ -261,9 +261,15 @@ export interface CellChildren {
261261 * Stop every running child quiet for `idleMs`; the names of those stopped (their data stays on
262262 * disk).
263263 */
264 sleepIdle(idleMs: number, now?: number): Promise<string[]>;
264 sleepIdle(idleMs: number, now?: number, onAsleep?: (name: string, bodyId: string) => void): Promise<string[]>;
265265 }
266266
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
267273 const registries = new WeakMap<App, CellChildren>();
268274
269275 export function cellChildren(parent: App): CellChildren {
@@ -527,17 +533,23 @@ export function makeRegistry(parent: App, launcher?: Launcher): CellChildren {
527533 touch(bodyId, now = Date.now()) {
528534 if (held.has(bodyId)) seen.set(bodyId, now);
529535 },
530 async sleepIdle(idleMs, now = Date.now()) {
536 async sleepIdle(idleMs, now = Date.now(), onAsleep) {
531537 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));
541553 return asleep;
542554 },
543555 };
src/spawn/host.ts +2 −2
@@ -228,7 +228,7 @@ export async function sleepIdleChildren(parent: App, idleMs = idleAfterMs(), now
228228 // A forge that was in this process is asleep: the moment it can be moved into a cell of its own
229229 // (`spawn/move_cell.ts`).
230230 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 }));
232232 for (const [bodyId, mark] of [...(touched.get(parent) ?? [])]) if (cellsAsleep.includes(mark.name)) {
233233 touched.get(parent)?.delete(bodyId);
234234 waking.get(parent)?.delete(bodyId);
@@ -240,7 +240,7 @@ export async function sleepIdleChildren(parent: App, idleMs = idleAfterMs(), now
240240 if (ledger !== undefined && due !== undefined) wakeWhenDue(parent, ledger, due, now);
241241 }
242242 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 });

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.

244244 return asleep;
245245 }
246246
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs