Skip to content

Commit 2ee1529

Browse files
fix(infra): keep task cache cleanup ahead of uploads (#25143)
1 parent 44df336 commit 2ee1529

4 files changed

Lines changed: 130 additions & 8 deletions

File tree

‎infra/task-cache/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ The smoke check uploads a unique fixture, restores its output into a cold second
6060
- Vite Task separates operating systems and CPU architectures. macOS results do not satisfy Linux CI; Linux developer machines can populate compatible CI results.
6161
- The current protocol retains one result for each execution key. Competing source versions can overwrite each other's entry. Inputs are validated before reuse, so this causes misses rather than stale hits. Merging to main does not promote or reset the cache.
6262
- A local hit makes no remote request and is not uploaded retroactively. Clear only an isolated worktree's task cache when testing uploads of an already-cached task.
63-
- Production retains results for seven days within an 8 GB budget. Remote errors fall back to execution. `VP_REMOTE_CACHE=off vp run ...` disables the remote tier for one invocation.
63+
- Production retains results for seven days within a 64 GB budget. Remote errors fall back to execution. `VP_REMOTE_CACHE=off vp run ...` disables the remote tier for one invocation.
6464

6565
## Savings and tracing
6666

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
);

‎infra/task-cache/src/Cleanup.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
});

‎infra/task-cache/src/worker.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Redacted from "effect/Redacted";
88
import * as HttpServerResponse from "effect/http/HttpServerResponse";
99
import * as Admission from "./Admission.ts";
1010
import * as Cache from "./Cache.ts";
11+
import * as Cleanup from "./Cleanup.ts";
1112
import * as NativeClients from "./NativeClients.ts";
1213
import * as Savings from "./Savings.ts";
1314
import type { ProtocolBindings } from "./protocolTypes.ts";
@@ -42,7 +43,7 @@ export const layer = Worker.make(
4243
return Cache.Bindings.of({
4344
INDEX: yield* database.raw,
4445
ARTIFACTS: (yield* artifacts.raw) as unknown as ProtocolBindings["ARTIFACTS"],
45-
GC_BATCH_SIZE: "16",
46+
GC_BATCH_SIZE: String(Cleanup.batchSize),
4647
developerKeys: Redacted.value(developerKeys),
4748
readerKeys: Redacted.value(readerKeys),
4849
readLimit: yield* reads.raw,
@@ -76,12 +77,8 @@ export const layer = Worker.make(
7677
savingsLayer,
7778
]),
7879
);
79-
yield* Cloudflare.Workers.cron("*/5 * * * *", () =>
80-
Cache.Cache.pipe(
81-
Effect.flatMap((cache) => cache.cleanup),
82-
Effect.provide(runtime),
83-
Effect.orDie,
84-
),
80+
yield* Cloudflare.Workers.cron("* * * * *", () =>
81+
Cleanup.run().pipe(Effect.provide(runtime), Effect.orDie),
8582
);
8683
yield* Cloudflare.Workers.cron("0 * * * *", () =>
8784
Savings.Savings.pipe(

0 commit comments

Comments
 (0)