GeezerVison is showing github.com — test(infra): cover cleanup claims and D1 query budget · pingdotgg/t3code@ab537e5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +112Lines changed: 112 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// @effect-diagnostics nodeBuiltinImport:off - real SQLite exercises concurrent D1 claims and accounting. | | 2 | +import * as NodeFS from "node:fs"; | | 3 | +import * as NodeSqlite from "node:sqlite"; | | 4 | +import * as Effect from "effect/Effect"; | | 5 | +import * as Layer from "effect/Layer"; | | 6 | +import { expect, it } from "@effect/vitest"; | | 7 | +import { cleanup } from "../vendor/vite-plus-remote-cache-cloudflare/src/database.ts"; | | 8 | +import * as Cache from "./Cache.ts"; | | 9 | +import * as Cleanup from "./Cleanup.ts"; | | 10 | + | | 11 | +it.effect("cleans three full disjoint batches within the D1 invocation budget", () => | | 12 | + Effect.gen(function* () { | | 13 | + const sqlite = new NodeSqlite.DatabaseSync(":memory:"); | | 14 | + try { | | 15 | + sqlite.exec( | | 16 | + NodeFS.readFileSync( | | 17 | + new URL( | | 18 | + "../vendor/vite-plus-remote-cache-cloudflare/migrations/0001_cache.sql", | | 19 | + import.meta.url, | | 20 | + ), | | 21 | + "utf8", | | 22 | + ), | | 23 | + ); | | 24 | + sqlite.exec(` | | 25 | + INSERT INTO scopes (scope_id, endpoint, repository, repository_id, repository_owner_id, branch) | | 26 | + VALUES ('fixture', 'https://cache.example', 'owner/repo', '1', '2', 'refs/heads/main'); | | 27 | + WITH RECURSIVE ids(id) AS (SELECT 0 UNION ALL SELECT id+1 FROM ids WHERE id<1023) | | 28 | + INSERT INTO generations (generation_id, scope_id, state, policy_version, token_exp, | | 29 | + lease_until, gc_after, value_object, blob_object, charged_bytes) | | 30 | + SELECT 'g'||id, 'fixture', 'retired', 1, unixepoch()+3600, 0, 0, | | 31 | + 'value/'||id, 'blob/'||id, 1 FROM ids; | | 32 | + INSERT INTO generations (generation_id, scope_id, state, policy_version, token_exp, | | 33 | + lease_until, gc_after, value_object, blob_object, charged_bytes) | | 34 | + VALUES ('live', 'fixture', 'ready', 1, unixepoch()+3600, 0, unixepoch()+3600, | | 35 | + 'live/value', 'live/blob', 1); | | 36 | + `); | | 37 | + let queries = 0; | | 38 | + const claims: string[][] = []; | | 39 | + const deleted = new Set<string>(); | | 40 | + const query = () => { | | 41 | + if (++queries > 1000) throw new Error("D1 query budget exceeded"); | | 42 | + }; | | 43 | + const prepare = (sql: string) => { | | 44 | + let args: NodeSqlite.SQLInputValue[] = []; | | 45 | + const statement = { | | 46 | + bind: (...values: NodeSqlite.SQLInputValue[]) => { | | 47 | + args = values.map((value) => | | 48 | + value instanceof ArrayBuffer ? new Uint8Array(value) : value, | | 49 | + ); | | 50 | + return statement; | | 51 | + }, | | 52 | + all: async () => { | | 53 | + query(); | | 54 | + const results = sqlite.prepare(sql).all(...args); | | 55 | + if (sql.startsWith("UPDATE generations")) | | 56 | + claims.push(results.map((row) => String(row.generation_id))); | | 57 | + return { results }; | | 58 | + }, | | 59 | + run: async () => { | | 60 | + query(); | | 61 | + if (sql.startsWith("DELETE FROM generations")) { | | 62 | + const row = sqlite | | 63 | + .prepare( | | 64 | + "SELECT value_object, blob_object FROM generations WHERE generation_id = ?", | | 65 | + ) | | 66 | + .get(args[0]!); | | 67 | + expect(deleted.has(String(row!.value_object))).toBe(true); | | 68 | + expect(deleted.has(String(row!.blob_object))).toBe(true); | | 69 | + } | | 70 | + return sqlite.prepare(sql).run(...args); | | 71 | + }, | | 72 | + }; | | 73 | + return statement; | | 74 | + }; | | 75 | + const env = { | | 76 | + GC_BATCH_SIZE: String(Cleanup.batchSize), | | 77 | + INDEX: { | | 78 | + prepare, | | 79 | + batch: async (statements: ReturnType<typeof prepare>[]) => | | 80 | + Promise.all(statements.map((statement) => statement.run())), | | 81 | + }, | | 82 | + ARTIFACTS: { | | 83 | + delete: async (keys: string[]) => { | | 84 | + for (const key of keys) { | | 85 | + expect(deleted.has(key)).toBe(false); | | 86 | + deleted.add(key); | | 87 | + } | | 88 | + }, | | 89 | + }, | | 90 | + } as unknown as Parameters<typeof cleanup>[0]; | | 91 | + yield* Cleanup.run().pipe( | | 92 | + Effect.provide( | | 93 | + Layer.succeed(Cache.Cache, { | | 94 | + handle: () => Effect.die("unused"), | | 95 | + cleanup: Effect.promise(() => cleanup(env)), | | 96 | + }), | | 97 | + ), | | 98 | + ); | | 99 | + expect(claims.map((claim) => claim.length)).toEqual([256, 256, 256]); | | 100 | + expect(new Set(claims.flat()).size).toBe(768); | | 101 | + expect(queries).toBeLessThanOrEqual(1000); | | 102 | + expect(deleted.size).toBe(1536); | | 103 | + expect(deleted.has("live/value")).toBe(false); | | 104 | + expect(sqlite.prepare("SELECT charged_bytes FROM scopes").get()).toEqual({ | | 105 | + charged_bytes: 257, | | 106 | + }); | | 107 | + expect(sqlite.prepare("SELECT count(*) AS n FROM generations").get()).toEqual({ n: 257 }); | | 108 | + } finally { | | 109 | + sqlite.close(); | | 110 | + } | | 111 | + }), | | 112 | +); |
Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as Effect from "effect/Effect"; | | 2 | +import * as Cache from "./Cache.ts"; | | 3 | + | | 4 | +export const batchSize = 256; | | 5 | + | | 6 | +export const run = Effect.fn("TaskCache.cleanup")(function* () { | | 7 | + const cache = yield* Cache.Cache; | | 8 | + // Three full batches stay below D1’s 1,000-query invocation limit. | | 9 | + yield* Effect.all([cache.cleanup, cache.cleanup, cache.cleanup], { | | 10 | + concurrency: 3, | | 11 | + discard: true, | | 12 | + }); | | 13 | +}); |
Collapse file Copy file name to clipboardExpand all lines: infra/task-cache/src/worker.ts +3-12Lines changed: 3 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,7 @@ import * as Redacted from "effect/Redacted"; | 8 | 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; | 9 | 9 | import * as Admission from "./Admission.ts"; | 10 | 10 | import * as Cache from "./Cache.ts"; | | 11 | +import * as Cleanup from "./Cleanup.ts"; | 11 | 12 | import * as NativeClients from "./NativeClients.ts"; | 12 | 13 | import * as Savings from "./Savings.ts"; | 13 | 14 | import type { ProtocolBindings } from "./protocolTypes.ts"; | @@ -42,7 +43,7 @@ export const layer = Worker.make( | 42 | 43 | return Cache.Bindings.of({ | 43 | 44 | INDEX: yield* database.raw, | 44 | 45 | ARTIFACTS: (yield* artifacts.raw) as unknown as ProtocolBindings["ARTIFACTS"], | 45 | | - GC_BATCH_SIZE: "256", | | 46 | + GC_BATCH_SIZE: String(Cleanup.batchSize), | 46 | 47 | developerKeys: Redacted.value(developerKeys), | 47 | 48 | readerKeys: Redacted.value(readerKeys), | 48 | 49 | readLimit: yield* reads.raw, | @@ -77,17 +78,7 @@ export const layer = Worker.make( | 77 | 78 | ]), | 78 | 79 | ); | 79 | 80 | yield* Cloudflare.Workers.cron("* * * * *", () => | 80 | | - Cache.Cache.pipe( | 81 | | - // Three full batches stay below D1’s 1,000-query invocation limit. | 82 | | - Effect.flatMap((cache) => | 83 | | - Effect.all([cache.cleanup, cache.cleanup, cache.cleanup], { | 84 | | - concurrency: 3, | 85 | | - discard: true, | 86 | | - }), | 87 | | - ), | 88 | | - Effect.provide(runtime), | 89 | | - Effect.orDie, | 90 | | - ), | | 81 | + Cleanup.run().pipe(Effect.provide(runtime), Effect.orDie), | 91 | 82 | ); | 92 | 83 | yield* Cloudflare.Workers.cron("0 * * * *", () => | 93 | 84 | Savings.Savings.pipe( | |
|