Repository navigation
spike(id): probe D1 journal sessions (#5864) #6350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
32cbca4
spike(id): probe D1 journal sessions
e731528
Merge remote-tracking branch 'origin/main' into queue/10599-5864-d1-j…
a07fdb2
Merge remote-tracking branch 'origin/main' into queue/10599-5864-d1-j…
8836caf
fix(id): reject unsupported arguments in D1 journal smoke (#5864)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Local D1 journal-fit PoC | ||
|
|
||
| From `pkgs/id/worker-poc`, run the whole smoke with: | ||
|
|
||
| ```sh | ||
| node d1-journal-poc/smoke.mjs | ||
| ``` | ||
|
|
||
| It needs Node.js 22 or newer and `wrangler` available on `PATH`. The harness | ||
| starts its own local Wrangler Worker on a free ephemeral port, gives it a | ||
| unique temporary D1 persistence directory, and stops/removes both on exit. Its | ||
| separate config is always used with `--local`; the placeholder database ID is | ||
| not a remote database configuration. It does not use the existing Worker-PoC | ||
| Wrangler config or its Durable Object database. | ||
|
|
||
| The Worker stores submitted newline-terminated JSONL bytes directly as a D1 | ||
| BLOB, assigning a per-world ordinal with an explicit `ORDER BY ordinal ASC` | ||
| when reading snapshots and tails. The smoke uses one synthetic world and the | ||
| current journal header version 1 (`created` and `caps_granted` entries). It | ||
| checks that a bookmark from an append is carried to a separate request and | ||
| that snapshot bytes plus a later tail are exactly the submitted JSONL bytes. | ||
|
|
||
| ## Scope and limits | ||
|
|
||
| - It assumes #806's single writer per world. This is an ordinal-allocation | ||
| fit probe, not a multi-writer conflict protocol. | ||
| - #5863's storage contract is still open; this schema is only for this PoC. | ||
| - There is no signature verification implementation here. The probe checks | ||
| exact-byte storage and ordering only. | ||
| - A D1 bookmark is a read-consistency token, distinct from the stream cursor. | ||
| This PoC's stream cursor is only the last returned ordinal; it is not the | ||
| proposed production ordinal-plus-digest cursor. | ||
| - D1 documents `withSession()` sequential consistency and bookmark use across | ||
| requests as a provider contract: | ||
| [D1 `withSession`](https://developers.cloudflare.com/d1/worker-api/d1-database/#withsession) | ||
| and [D1 read replication](https://developers.cloudflare.com/d1/best-practices/read-replication/). | ||
| Local Wrangler cannot prove regional replica routing, production | ||
| `served_by_region`/`served_by_primary`, or production replica behavior. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,273 @@ | ||
| // Local Wrangler D1 journal-fit smoke. Run from worker-poc: | ||
| // node d1-journal-poc/smoke.mjs | ||
| // Interface: zero arguments. This harness takes no options and no | ||
| // positional arguments; anything supplied is a usage error (see the | ||
| // argument check below, which runs before any side effects). | ||
| // Requires Node >=22 and `wrangler` on PATH. Uses only a unique temporary | ||
| // persistence directory; it does not use or configure the main worker-poc D1. | ||
| import assert from "node:assert/strict"; | ||
| import { spawn } from "node:child_process"; | ||
| import { once } from "node:events"; | ||
| import { mkdtemp, rm } from "node:fs/promises"; | ||
| import { createServer } from "node:net"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { setTimeout as delay } from "node:timers/promises"; | ||
|
|
||
| // Zero-argument interface: reject any supplied argument before the | ||
| // harness creates temporary state or starts Wrangler. | ||
| const suppliedArgs = process.argv.slice(2); | ||
| if (suppliedArgs.length > 0) { | ||
| console.error( | ||
| "usage: node d1-journal-poc/smoke.mjs (no arguments; unexpected argument: " + | ||
| suppliedArgs[0] + | ||
| ")", | ||
| ); | ||
| process.exit(2); | ||
| } | ||
|
|
||
| const pocDir = path.dirname(fileURLToPath(import.meta.url)); | ||
| const workerPocDir = path.resolve(pocDir, ".."); | ||
| const configPath = path.join(pocDir, "wrangler.toml"); | ||
| const worldId = "d1-journal-poc-world"; | ||
| const maxCapturedLogChars = 60_000; | ||
|
|
||
| let child; | ||
| let childClosed; | ||
| let childFailure; | ||
| let stdout = ""; | ||
| let stderr = ""; | ||
| let stopPromise; | ||
| let stateDir; | ||
| let interruptedBy; | ||
| const interrupt = new AbortController(); | ||
|
|
||
| function appendLog(existing, chunk) { | ||
| return (existing + chunk.toString()).slice(-maxCapturedLogChars); | ||
| } | ||
|
|
||
| function captureLogs(processHandle) { | ||
| processHandle.stdout.on("data", (chunk) => { | ||
| stdout = appendLog(stdout, chunk); | ||
| }); | ||
| processHandle.stderr.on("data", (chunk) => { | ||
| stderr = appendLog(stderr, chunk); | ||
| }); | ||
| processHandle.on("error", (error) => { | ||
| childFailure = error; | ||
| }); | ||
| childClosed = new Promise((resolve) => { | ||
| processHandle.once("close", (code, signal) => resolve({ code, signal })); | ||
| }); | ||
| } | ||
|
|
||
| function signalProcessGroup(signal) { | ||
| if (!child || child.pid === undefined) return; | ||
| try { | ||
| if (process.platform !== "win32") process.kill(-child.pid, signal); | ||
| else child.kill(signal); | ||
| } catch (error) { | ||
| if (error?.code !== "ESRCH") throw error; | ||
| } | ||
| } | ||
|
|
||
| async function stopServer() { | ||
| if (!child || !childClosed) return; | ||
| if (stopPromise) return stopPromise; | ||
| stopPromise = (async () => { | ||
| signalProcessGroup("SIGTERM"); | ||
| let timeout; | ||
| const graceful = await Promise.race([ | ||
| childClosed.then(() => true), | ||
| new Promise((resolve) => { | ||
| timeout = setTimeout(() => resolve(false), 5_000); | ||
| }), | ||
| ]); | ||
| clearTimeout(timeout); | ||
| if (!graceful) { | ||
| signalProcessGroup("SIGKILL"); | ||
| await childClosed; | ||
| } | ||
| })(); | ||
| return stopPromise; | ||
| } | ||
|
|
||
| async function freePort() { | ||
| const server = createServer(); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| const address = server.address(); | ||
| assert(address && typeof address === "object"); | ||
| const { port } = address; | ||
| await new Promise((resolve, reject) => { | ||
| server.close((error) => (error ? reject(error) : resolve())); | ||
| }); | ||
| return port; | ||
| } | ||
|
|
||
| function abortSignal() { | ||
| return AbortSignal.any([interrupt.signal, AbortSignal.timeout(5_000)]); | ||
| } | ||
|
|
||
| function serverLogs() { | ||
| return [stdout && `Wrangler stdout:\n${stdout}`, stderr && `Wrangler stderr:\n${stderr}`] | ||
| .filter(Boolean) | ||
| .join("\n"); | ||
| } | ||
|
|
||
| async function waitForServer(baseUrl) { | ||
| const deadline = Date.now() + 45_000; | ||
| while (Date.now() < deadline) { | ||
| if (childFailure) throw childFailure; | ||
| if (child.exitCode !== null || child.signalCode !== null) { | ||
| throw new Error(`Wrangler exited before becoming ready (${serverLogs()})`); | ||
| } | ||
| try { | ||
| const response = await fetch(`${baseUrl}/health`, { signal: abortSignal() }); | ||
| if (response.ok && (await response.text()) === "ok") return; | ||
| } catch (error) { | ||
| if (interrupt.signal.aborted) throw error; | ||
| } | ||
| await delay(200); | ||
| } | ||
| throw new Error(`Timed out waiting for local Wrangler (${serverLogs()})`); | ||
| } | ||
|
|
||
| async function responseError(response) { | ||
| const text = await response.text(); | ||
| return `${response.status} ${response.statusText}: ${text}`; | ||
| } | ||
|
|
||
| async function append(baseUrl, bytes) { | ||
| const response = await fetch(`${baseUrl}/append`, { | ||
| method: "POST", | ||
| headers: { "content-type": "application/x-ndjson" }, | ||
| body: bytes, | ||
| signal: abortSignal(), | ||
| }); | ||
| if (!response.ok) assert.fail(`append failed: ${await responseError(response)}`); | ||
| return response.json(); | ||
| } | ||
|
|
||
| async function readJsonl(response, label) { | ||
| if (!response.ok) assert.fail(`${label} failed: ${await responseError(response)}`); | ||
| const bytes = Buffer.from(await response.arrayBuffer()); | ||
| const first = Number(response.headers.get("x-journal-first-ordinal")); | ||
| const last = Number(response.headers.get("x-journal-last-ordinal")); | ||
| const count = Number(response.headers.get("x-journal-entry-count")); | ||
| const cursor = Number(response.headers.get("x-journal-stream-cursor")); | ||
| assert(Number.isSafeInteger(first) && first >= 0, `${label}: missing first ordinal`); | ||
| assert(Number.isSafeInteger(last) && last >= 0, `${label}: missing last ordinal`); | ||
| assert(Number.isSafeInteger(count) && count >= 0, `${label}: missing entry count`); | ||
| assert.equal(cursor, last, `${label}: stream cursor should name the last returned ordinal`); | ||
| if (count > 0) assert.equal(last - first + 1, count, `${label}: ordinal range contains a gap`); | ||
| return { bytes, first, last, count, cursor }; | ||
| } | ||
|
|
||
| async function main() { | ||
| stateDir = await mkdtemp(path.join(os.tmpdir(), "id-d1-journal-poc-")); | ||
| const persistDir = path.join(stateDir, "wrangler-state"); | ||
| const port = await freePort(); | ||
| const baseUrl = `http://127.0.0.1:${port}`; | ||
| child = spawn( | ||
| "wrangler", | ||
| [ | ||
| "dev", | ||
| "--local", | ||
| "--ip", | ||
| "127.0.0.1", | ||
| "--port", | ||
| String(port), | ||
| "--persist-to", | ||
| persistDir, | ||
| "--config", | ||
| configPath, | ||
| ], | ||
| { | ||
| cwd: workerPocDir, | ||
| detached: process.platform !== "win32", | ||
| env: { ...process.env, WRANGLER_SEND_METRICS: "false" }, | ||
| stdio: ["ignore", "pipe", "pipe"], | ||
| }, | ||
| ); | ||
| captureLogs(child); | ||
| await waitForServer(baseUrl); | ||
|
|
||
| const entries = [ | ||
| Buffer.from(`{"entry":"created","world_id":"${worldId}","version":1}\n`), | ||
| Buffer.from('{"entry":"caps_granted","granted":[]}\n'), | ||
| // Different legal whitespace makes byte preservation observable: the | ||
| // Worker must store and return the submitted bytes, not reserialize JSON. | ||
| Buffer.from('{ "entry": "caps_granted", "granted": [ ] }\n'), | ||
| ]; | ||
|
|
||
| const firstAppend = await append(baseUrl, entries[0]); | ||
| const secondAppend = await append(baseUrl, entries[1]); | ||
| assert.equal(firstAppend.ordinal, 1, "first entry should have ordinal 1"); | ||
| assert.equal(secondAppend.ordinal, 2, "ordinals should increase monotonically"); | ||
| assert.equal(typeof secondAppend.bookmark, "string"); | ||
| assert(secondAppend.bookmark.length > 0, "append should return a D1 session bookmark"); | ||
|
|
||
| // This is a distinct HTTP request and therefore a fresh D1 session. The | ||
| // append bookmark must make the just-written prefix visible to it. | ||
| const snapshotResponse = await fetch(`${baseUrl}/snapshot`, { | ||
| headers: { "x-d1-bookmark": secondAppend.bookmark }, | ||
| signal: abortSignal(), | ||
| }); | ||
| const snapshot = await readJsonl(snapshotResponse, "bookmark snapshot"); | ||
| assert.equal(snapshot.first, 1); | ||
| assert.equal(snapshot.last, 2); | ||
| assert.equal(snapshot.count, 2); | ||
| assert.deepEqual(snapshot.bytes, Buffer.concat(entries.slice(0, 2)), "snapshot must be byte-identical JSONL"); | ||
|
|
||
| const thirdAppend = await append(baseUrl, entries[2]); | ||
| assert.equal(thirdAppend.ordinal, 3, "later append should follow the snapshot cursor"); | ||
|
|
||
| const tailResponse = await fetch(`${baseUrl}/tail?after=${snapshot.cursor}`, { | ||
| headers: { "x-d1-bookmark": thirdAppend.bookmark }, | ||
| signal: abortSignal(), | ||
| }); | ||
| const tail = await readJsonl(tailResponse, "ordered tail"); | ||
| assert.equal(tail.first, snapshot.cursor + 1, "tail should start immediately after the snapshot"); | ||
| assert.equal(tail.last, 3); | ||
| assert.equal(tail.count, 1); | ||
| assert.deepEqual(tail.bytes, entries[2], "tail must return the exact appended bytes"); | ||
| assert.deepEqual( | ||
| Buffer.concat([snapshot.bytes, tail.bytes]), | ||
| Buffer.concat(entries), | ||
| "snapshot plus ordered tail must have no duplicate, gap, or reorder", | ||
| ); | ||
|
|
||
| console.log("D1 journal-fit smoke: PASS"); | ||
| console.log(" append ordinals: 1, 2, 3"); | ||
| console.log(" bookmark carried to a fresh request: read-your-write PASS"); | ||
| console.log(" snapshot + ordered tail: byte-identical JSONL, no gap/duplicate/reorder"); | ||
| } | ||
|
|
||
| function onSignal(signal) { | ||
| interruptedBy = signal; | ||
| interrupt.abort(new Error(`interrupted by ${signal}`)); | ||
| void stopServer(); | ||
| } | ||
|
|
||
| process.once("SIGINT", onSignal); | ||
| process.once("SIGTERM", onSignal); | ||
|
|
||
| try { | ||
| await main(); | ||
| } catch (error) { | ||
| if (!interruptedBy) { | ||
| console.error(`D1 journal-fit smoke: FAIL: ${error?.stack ?? error}`); | ||
| const logs = serverLogs(); | ||
| if (logs) console.error(logs); | ||
| process.exitCode = 1; | ||
| } else { | ||
| process.exitCode = interruptedBy === "SIGINT" ? 130 : 143; | ||
| } | ||
| } finally { | ||
| process.removeListener("SIGINT", onSignal); | ||
| process.removeListener("SIGTERM", onSignal); | ||
| await stopServer(); | ||
| if (stateDir) await rm(stateDir, { recursive: true, force: true }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this standalone harness is invoked with an unsupported option such as
--dry-runor a stray positional, it silently executes the full smoke workflow, creating temporary state and starting Wrangler, because it never examinesprocess.argv. This violates the operator-script argument contract in rootAGENTS.md. Declare the zero-argument interface in the header and reject supplied arguments with a usage error naming the offending argument and a nonzero exit before performing any side effects.AGENTS.md reference: AGENTS.md:L47-L51
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8836caf. smoke.mjs now declares its zero-argument interface in the header and rejects any supplied argument before creating temporary state or starting Wrangler: it prints a usage error naming the offending argument and exits 2. Verified on the laptop:
node d1-journal-poc/smoke.mjs --dry-runexits 2 with the usage error, and the plain zero-argument run still PASSes (ordinals 1, 2, 3; bookmark read-your-write; snapshot + ordered tail byte-identical).