/** * LIVE CPU (coordinator, 23 Nov 2026: his 3.7 GB object reset for exceeding its CPU, 18:13 to 18:17 UTC). A pass from * nothing made its first orders' indexes in the request itself (Store.firstIndexes), each a read of the whole file: at * his size the request ran past its CPU limit, the reset rolled the index back, and the next pass from nothing began it * again. Now a big file's pass never makes an index: it asks, it is answered at once in its own order, and the alarm * makes the indexes one an alarm; once they stand, a pass from nothing carries first orders again. */ import { env, runDurableObjectAlarm, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { CARRY_ON_MS } from "../src/calendar_engine.ts"; import { FIRST_INDEX_NOW_BYTES, ORDER_INDEX_MAX_BYTES } from "../src/store.ts"; import { personalShape, writeShape } from "./big_object.ts"; import { http, personalObject } from "./personal.ts"; const firstIndexes = (sql: SqlStorage): string[] => sql.exec<{ name: string }>("SELECT name FROM sqlite_master WHERE type = 'index' AND name LIKE 'rows_first_%' ORDER BY name").toArray().map((r) => r.name); /** First-order indexes still asked of the alarm (the same meta holds the lookups' indexes, READ COST). */ function firstAsked(sql: SqlStorage): number { const held = sql.exec<{ value: string }>("SELECT value FROM meta WHERE key = 'order_indexes_wanted'").toArray()[0]; return Object.keys(held ? (JSON.parse(held.value) as object) : {}).filter((k) => k.startsWith("rows_first_")).length; } describe("a big file's first-order indexes", { timeout: 300_000 }, () => { it("are asked by a pass and made by the alarm, one an alarm, never in the request", async () => { const slug = "first-index-alarm"; const { credential } = await personalObject(slug); const phone = { authorization: `Bearer ${credential}` }; const stub = env.BUSINESS.get(env.BUSINESS.idFromName(slug)); await runInDurableObject(stub, (_i, s) => { const last = writeShape(s.storage.sql, personalShape(1 / 400)); s.storage.sql.exec("UPDATE meta SET value = CAST(? AS TEXT) WHERE key = 'version'", last); // The file past the size at which an index is made at once: filler of its own, no rows. s.storage.sql.exec("CREATE TABLE IF NOT EXISTS filler (b BLOB)"); while (s.storage.sql.databaseSize < FIRST_INDEX_NOW_BYTES + 4 * 1024 * 1024) s.storage.sql.exec("INSERT INTO filler (b) SELECT randomblob(1048576) FROM (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4)"); for (const name of firstIndexes(s.storage.sql)) s.storage.sql.exec(`DROP INDEX ${name}`); }); // The pass from nothing: answered, and no index made in it. Under load the real alarm (brought round within a // second) can run before this test looks: what it makes is told apart from what a request makes. const made: string[] = []; const perAlarm: number[] = []; await runInDurableObject(stub, (instance, s) => { const sql = s.storage.sql as unknown as { exec: (q: string, ...a: unknown[]) => unknown }; const real = sql.exec.bind(sql); const object = instance as unknown as { alarm: () => Promise }; const alarm = object.alarm.bind(object); let inAlarm = false; object.alarm = async () => { inAlarm = true; perAlarm.push(0); try { await alarm(); } finally { inAlarm = false; } }; sql.exec = (q: string, ...a: unknown[]) => { if (/CREATE INDEX/i.test(q) && q.includes("rows_first_")) { if (inAlarm) perAlarm[perAlarm.length - 1]! += 1; else made.push(q); } return real(q, ...a); }; }); const pass = await http<{ rows: unknown[] }>(slug, "GET", "/v1/sync?since=0", undefined, phone); expect(pass.status, JSON.stringify(pass.body).slice(0, 300)).toBe(200); expect(pass.body.rows.length).toBeGreaterThan(0); expect(made.filter((q) => q.includes("rows_first_"))).toEqual([]); // Asked: still waiting in meta, or already made by an alarm that ran first. (The same meta holds the lookups' indexes, // READ COST, which are not first orders.) const asked = await runInDurableObject(stub, (_i, s) => { const held = s.storage.sql.exec<{ value: string }>("SELECT value FROM meta WHERE key = 'order_indexes_wanted'").toArray()[0]; return { indexes: firstIndexes(s.storage.sql), wanted: [...firstIndexes(s.storage.sql), ...Object.keys(held ? (JSON.parse(held.value) as object) : {}).filter((k) => k.startsWith("rows_first_"))] }; }); expect(asked.wanted.length).toBeGreaterThan(1); // The alarm is brought round soon (the engines' carry-on), not at its hour. // (Unless an alarm already ran and made every one.) // (None set means the pool is running one now.) if (asked.indexes.length < asked.wanted.length) expect((await runInDurableObject(stub, (_i, s) => s.storage.getAlarm())) ?? 0).toBeLessThanOrEqual(Date.now() + CARRY_ON_MS + 1_000); // One an alarm, until every one stands. // (The alarm is run here by hand; the test pool would otherwise run it at its time, which is the same work.) // (Run by hand here, while the pool may run the same alarm at its time: each run makes at most one.) // READ COST (7274fe1d24): the same alarm makes the lookup indexes a pass asks for (rows_eq_*, rows_bin_*), one an // alarm as well, and the alarm's own work asks one more (the prompts' keep, rows_prompt_ts): every index asked is // counted here, and the alarm runs until none is owed. const standing = (sql: SqlStorage): number => { const names = new Set(sql.exec<{ name: string }>("SELECT name FROM sqlite_master WHERE type = 'index'").toArray().map((r) => r.name)); return asked.wanted.filter((n) => names.has(n)).length; }; const owed = (sql: SqlStorage): boolean => sql.exec("SELECT 1 FROM meta WHERE key = 'order_indexes_wanted'").toArray().length > 0; for (let i = 0; i < (asked.wanted.length + 2) * 3; i++) { if ((await runInDurableObject(stub, (_i, s) => standing(s.storage.sql) >= asked.wanted.length && !owed(s.storage.sql)))) break; // MEASURE GENTLE: an index is made only while nobody uses the object; this one's pass was just now. await runInDurableObject(stub, (instance) => void Object.assign(instance as object, { lastRequestAt: 0, lastPullAt: 0 })); await runDurableObjectAlarm(stub); } expect(await runInDurableObject(stub, (_i, s) => standing(s.storage.sql))).toBe(asked.wanted.length); expect(Math.max(0, ...perAlarm), "one index an alarm").toBeLessThanOrEqual(1); expect(await runInDurableObject(stub, (_i, s) => firstAsked(s.storage.sql))).toBe(0); // A pass from nothing now walks them, and asks for nothing more. const again = await http<{ rows: unknown[] }>(slug, "GET", "/v1/sync?since=0", undefined, phone); expect(again.status).toBe(200); expect(await runInDurableObject(stub, (_i, s) => firstAsked(s.storage.sql))).toBe(0); }); }); /** * LIVE CPU 3 (24 Nov 2026: his object reset for its CPU again, 23:46 to 23:55 UTC, with no request slow). Making one of * these indexes reads every row of the file, whatever its WHERE: on an invented 1.9 GB object 1.4 million rows, 9 to * 12 s each. In the alarm at his 3.7 GB that ran past the CPU limit; the reset rolled the index back, the alarm came * again, and every request in the object waited and failed. Past ORDER_INDEX_MAX_BYTES no index is made at all. */ describe("a file too big to make an order index in one invocation", { timeout: 600_000 }, () => { it("never makes one, in a request or an alarm, and drops what was asked before", async () => { const slug = "order-index-too-big"; const { credential } = await personalObject(slug); const phone = { authorization: `Bearer ${credential}` }; const stub = env.BUSINESS.get(env.BUSINESS.idFromName(slug)); await runInDurableObject(stub, (_i, s) => { const last = writeShape(s.storage.sql, personalShape(1 / 400)); s.storage.sql.exec("UPDATE meta SET value = CAST(? AS TEXT) WHERE key = 'version'", last); s.storage.sql.exec("CREATE TABLE IF NOT EXISTS filler (b BLOB)"); while (s.storage.sql.databaseSize < ORDER_INDEX_MAX_BYTES + 4 * 1024 * 1024) s.storage.sql.exec("INSERT INTO filler (b) SELECT randomblob(1048576) FROM (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4)"); for (const name of firstIndexes(s.storage.sql)) s.storage.sql.exec(`DROP INDEX ${name}`); // As on his object: indexes asked for before this change, waiting for the alarm. s.storage.sql.exec("INSERT OR REPLACE INTO meta (key, value) VALUES ('order_indexes_wanted', ?)", JSON.stringify({ rows_first_memory: "CREATE INDEX IF NOT EXISTS rows_first_memory ON rows (version, id) WHERE table_id = 'memory'" })); }); const made: string[] = []; await runInDurableObject(stub, (_i, s) => { const sql = s.storage.sql as unknown as { exec: (q: string, ...a: unknown[]) => unknown }; const real = sql.exec.bind(sql); sql.exec = (q: string, ...a: unknown[]) => { if (/CREATE INDEX/i.test(q) && q.includes("rows_")) made.push(q); return real(q, ...a); }; }); // The alarm drops what was asked and makes nothing; its mark is gone once it ends. // (Run here, or by the pool at its time: the same work.) await runDurableObjectAlarm(stub); expect(made.filter((q) => /rows_(first|order)_/.test(q))).toEqual([]); const after = await runInDurableObject(stub, (_i, s) => s.storage.sql.exec<{ key: string }>("SELECT key FROM meta WHERE key IN ('order_indexes_wanted', 'alarm_running')").toArray().map((r) => r.key)); expect(after).toEqual([]); // A pass from nothing is answered, and asks for nothing. const pass = await http<{ rows: unknown[] }>(slug, "GET", "/v1/sync?since=0", undefined, phone); expect(pass.status).toBe(200); expect(pass.body.rows.length).toBeGreaterThan(0); expect(made.filter((q) => /rows_(first|order)_/.test(q))).toEqual([]); expect(await runInDurableObject(stub, (_i, s) => s.storage.sql.exec("SELECT 1 FROM meta WHERE key = 'order_indexes_wanted'").toArray().length)).toBe(0); }); });