@foldrun/core 17.3.0 → 18.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/approvals.js +3 -3
- package/dist/src/blob-cache.d.ts +58 -0
- package/dist/src/blob-cache.js +312 -0
- package/dist/src/blob-pins.d.ts +44 -0
- package/dist/src/blob-pins.js +61 -0
- package/dist/src/blob-store.d.ts +47 -0
- package/dist/src/blob-store.js +362 -0
- package/dist/src/blob-sweeper.d.ts +70 -0
- package/dist/src/blob-sweeper.js +282 -0
- package/dist/src/blob-types.d.ts +145 -0
- package/dist/src/blob-types.js +115 -0
- package/dist/src/evals.js +1 -1
- package/dist/src/history.d.ts +92 -9
- package/dist/src/history.js +168 -20
- package/dist/src/journal-store.d.ts +58 -0
- package/dist/src/journal-store.js +117 -0
- package/dist/src/manifest.d.ts +92 -0
- package/dist/src/manifest.js +239 -0
- package/dist/src/ops-log.js +26 -3
- package/dist/src/platform.d.ts +7 -0
- package/dist/src/platform.js +1 -0
- package/dist/src/publish.d.ts +134 -0
- package/dist/src/publish.js +434 -0
- package/dist/src/records.d.ts +72 -0
- package/dist/src/records.js +24 -0
- package/dist/src/revision-store.d.ts +161 -0
- package/dist/src/revision-store.js +378 -0
- package/dist/src/run-container.d.ts +20 -0
- package/dist/src/run-container.js +61 -0
- package/dist/src/runner.js +1 -1
- package/dist/src/s3-blob-store.d.ts +29 -0
- package/dist/src/s3-blob-store.js +531 -0
- package/dist/src/state-retention.d.ts +15 -0
- package/dist/src/state-retention.js +65 -0
- package/dist/src/storage.d.ts +29 -1
- package/dist/src/storage.js +331 -81
- package/dist/src/store.d.ts +41 -0
- package/dist/src/store.js +188 -5
- package/dist/src/trigger-log.js +17 -0
- package/dist/src/workspace-revisions.d.ts +269 -0
- package/dist/src/workspace-revisions.js +1382 -0
- package/package.json +1 -1
- package/src/approvals.ts +3 -3
- package/src/blob-cache.ts +357 -0
- package/src/blob-pins.ts +99 -0
- package/src/blob-store.ts +389 -0
- package/src/blob-sweeper.ts +336 -0
- package/src/blob-types.ts +216 -0
- package/src/evals.ts +1 -1
- package/src/history.ts +226 -19
- package/src/journal-store.ts +152 -0
- package/src/manifest.ts +274 -0
- package/src/ops-log.ts +25 -3
- package/src/platform.ts +8 -0
- package/src/publish.ts +520 -0
- package/src/records.ts +105 -0
- package/src/revision-store.ts +513 -0
- package/src/run-container.ts +62 -0
- package/src/runner.ts +1 -1
- package/src/s3-blob-store.ts +560 -0
- package/src/state-retention.ts +83 -0
- package/src/storage.ts +335 -81
- package/src/store.ts +178 -5
- package/src/trigger-log.ts +15 -0
- package/src/workspace-revisions.ts +1423 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// BlobStore: immutable objects by key (FOL-126, phase 1).
|
|
2
|
+
//
|
|
3
|
+
// The small interface the revision work stands on. It is NOT a filesystem
|
|
4
|
+
// shim: no rename, no list-by-prefix, no append, no overwrite. A key is
|
|
5
|
+
// written once (put with `ifNoneMatch`), read as a stream, asked about
|
|
6
|
+
// (head), and removed (delete — only the garbage collector does that).
|
|
7
|
+
//
|
|
8
|
+
// Two drivers, both in this file:
|
|
9
|
+
//
|
|
10
|
+
// fs a directory tree under the data root. A conditional put is
|
|
11
|
+
// link(2) of a finished temp file onto the target: atomic, and fails
|
|
12
|
+
// with EEXIST if another writer got there first, on a local disk and
|
|
13
|
+
// on NFS alike. This is what self-host Compose and the laptop get.
|
|
14
|
+
// s3 any S3-compatible endpoint (R2, MinIO, AWS), signed by the code in
|
|
15
|
+
// storage.ts. A conditional put is `If-None-Match: *`; 412 means "it is
|
|
16
|
+
// already there".
|
|
17
|
+
//
|
|
18
|
+
// `bounded()` wraps either with what a production caller needs and neither
|
|
19
|
+
// driver should each reinvent: a deadline on every call, a retry budget for
|
|
20
|
+
// failures worth retrying, and a cap on calls in flight.
|
|
21
|
+
//
|
|
22
|
+
// Large objects: `putSourceIfAbsent` streams from a re-readable source (a file
|
|
23
|
+
// on disk) and, on s3 past a threshold, uploads in resumable parts. The s3
|
|
24
|
+
// driver lives in s3-blob-store.ts; the shared types and errors in
|
|
25
|
+
// blob-types.ts; both are re-exported here so callers import one module.
|
|
26
|
+
// tests/s3-blob-store.test.ts runs the s3 driver against a real MinIO
|
|
27
|
+
// (FOLDRUN_TEST_S3_URL), the contract suite runs it against the fake.
|
|
28
|
+
import fs from "node:fs";
|
|
29
|
+
import path from "node:path";
|
|
30
|
+
import crypto from "node:crypto";
|
|
31
|
+
import { Readable } from "node:stream";
|
|
32
|
+
import { pipeline } from "node:stream/promises";
|
|
33
|
+
import { dataRoot } from "./paths.js";
|
|
34
|
+
import { s3Config } from "./storage.js";
|
|
35
|
+
import { s3BlobStore } from "./s3-blob-store.js";
|
|
36
|
+
import { BlobNotFound, BlobSourceError, BlobTransientError, assertKey, hashRange, } from "./blob-types.js";
|
|
37
|
+
export * from "./blob-types.js";
|
|
38
|
+
export { s3BlobStore };
|
|
39
|
+
// ---------- fs driver ----------
|
|
40
|
+
export function fsBlobStore(root = path.join(dataRoot(), ".objects")) {
|
|
41
|
+
const at = (key) => path.join(root, ...assertKey(key).split("/"));
|
|
42
|
+
const tmpName = (target) => `${target}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`;
|
|
43
|
+
return {
|
|
44
|
+
kind: "fs",
|
|
45
|
+
async putSourceIfAbsent(key, source, opts = {}) {
|
|
46
|
+
const target = at(key);
|
|
47
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
48
|
+
if (fs.existsSync(target))
|
|
49
|
+
return { created: false };
|
|
50
|
+
const tmp = tmpName(target);
|
|
51
|
+
try {
|
|
52
|
+
const sha = crypto.createHash("sha256");
|
|
53
|
+
const out = fs.createWriteStream(tmp);
|
|
54
|
+
await pipeline(Readable.from(source.read(0, source.size, opts.signal), { objectMode: false }), async function* (chunks) {
|
|
55
|
+
for await (const c of chunks) {
|
|
56
|
+
sha.update(c);
|
|
57
|
+
yield c;
|
|
58
|
+
}
|
|
59
|
+
}, out);
|
|
60
|
+
if (opts.sha256 && sha.digest("hex") !== opts.sha256)
|
|
61
|
+
throw new BlobSourceError(`the bytes for ${key} do not hash to ${opts.sha256}`);
|
|
62
|
+
try {
|
|
63
|
+
fs.linkSync(tmp, target);
|
|
64
|
+
return { created: true };
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
if (err.code === "EEXIST")
|
|
68
|
+
return { created: false };
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
fs.rmSync(tmp, { force: true });
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
async abortStaleUploads({ olderThanMs, prefix = "" }) {
|
|
77
|
+
// The fs driver's "incomplete uploads" are temp files a dead writer left.
|
|
78
|
+
let n = 0;
|
|
79
|
+
const walk = (dir) => {
|
|
80
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
81
|
+
const f = path.join(dir, e.name);
|
|
82
|
+
if (e.isDirectory())
|
|
83
|
+
walk(f);
|
|
84
|
+
else if (e.name.endsWith(".tmp") && path.relative(root, f).startsWith(prefix) && Date.now() - fs.statSync(f).mtimeMs >= olderThanMs) {
|
|
85
|
+
fs.rmSync(f, { force: true });
|
|
86
|
+
n++;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
if (fs.existsSync(root))
|
|
91
|
+
walk(root);
|
|
92
|
+
return n;
|
|
93
|
+
},
|
|
94
|
+
async putIfAbsent(key, body) {
|
|
95
|
+
const target = at(key);
|
|
96
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
97
|
+
if (fs.existsSync(target))
|
|
98
|
+
return { created: false };
|
|
99
|
+
const tmp = `${target}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`;
|
|
100
|
+
fs.writeFileSync(tmp, body);
|
|
101
|
+
try {
|
|
102
|
+
// link() refuses an existing target: the atomic "create if absent".
|
|
103
|
+
fs.linkSync(tmp, target);
|
|
104
|
+
return { created: true };
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
if (err.code === "EEXIST")
|
|
108
|
+
return { created: false };
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
fs.rmSync(tmp, { force: true });
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
async get(key, opts = {}) {
|
|
116
|
+
const target = at(key);
|
|
117
|
+
if (!fs.existsSync(target))
|
|
118
|
+
throw new BlobNotFound(key);
|
|
119
|
+
const stream = fs.createReadStream(target, opts.range ? { start: opts.range.start, end: opts.range.end } : {});
|
|
120
|
+
return Readable.toWeb(stream);
|
|
121
|
+
},
|
|
122
|
+
async head(key) {
|
|
123
|
+
const target = at(key); // a bad key throws; only a missing file is null
|
|
124
|
+
try {
|
|
125
|
+
const st = fs.statSync(target);
|
|
126
|
+
return st.isFile() ? { size: st.size, modifiedAt: st.mtime } : null;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
async delete(key) {
|
|
133
|
+
fs.rmSync(at(key), { force: true });
|
|
134
|
+
},
|
|
135
|
+
async purgeTenant(tenant) {
|
|
136
|
+
fs.rmSync(path.dirname(at(`t/${tenant}/x`)), { recursive: true, force: true });
|
|
137
|
+
},
|
|
138
|
+
async *list(prefix) {
|
|
139
|
+
const base = at(`${prefix.replace(/\/+$/, "")}/x`);
|
|
140
|
+
const dir = path.dirname(base);
|
|
141
|
+
const walk = function* (d, rel) {
|
|
142
|
+
let names = [];
|
|
143
|
+
try {
|
|
144
|
+
names = fs.readdirSync(d, { withFileTypes: true });
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
for (const e of names) {
|
|
150
|
+
if (e.isDirectory())
|
|
151
|
+
yield* walk(path.join(d, e.name), `${rel}${e.name}/`);
|
|
152
|
+
else if (e.isFile() && !e.name.endsWith(".tmp")) {
|
|
153
|
+
try {
|
|
154
|
+
const st = fs.statSync(path.join(d, e.name));
|
|
155
|
+
yield { key: `${rel}${e.name}`, size: st.size, modifiedAt: st.mtime };
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
// gone meanwhile
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
yield* walk(dir, `${prefix.replace(/\/+$/, "")}/`);
|
|
164
|
+
},
|
|
165
|
+
async tenants() {
|
|
166
|
+
try {
|
|
167
|
+
return fs.readdirSync(path.join(root, "t"), { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name);
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return [];
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
// ---------- download to a file ----------
|
|
176
|
+
/**
|
|
177
|
+
* Fetch an object into a file, resuming. The bytes land in `<dest>.part`;
|
|
178
|
+
* a connection cut mid-body (a transient error) is picked up again with a
|
|
179
|
+
* Range from where the file ends, and so is a `.part` a dead process left.
|
|
180
|
+
* The whole file is hashed against `sha256` (when given) before it is renamed
|
|
181
|
+
* into place; a mismatch discards it and throws BlobSourceError.
|
|
182
|
+
*/
|
|
183
|
+
export async function downloadToFile(store, key, dest, opts = {}) {
|
|
184
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
185
|
+
const part = `${dest}.part`;
|
|
186
|
+
const size = (await store.head(key, { signal: opts.signal }))?.size;
|
|
187
|
+
if (size === undefined)
|
|
188
|
+
throw new BlobNotFound(key);
|
|
189
|
+
const max = opts.attempts ?? 5;
|
|
190
|
+
for (let attempt = 0;; attempt++) {
|
|
191
|
+
let have = fs.existsSync(part) ? fs.statSync(part).size : 0;
|
|
192
|
+
if (have > size) {
|
|
193
|
+
fs.rmSync(part, { force: true });
|
|
194
|
+
have = 0;
|
|
195
|
+
}
|
|
196
|
+
if (have < size) {
|
|
197
|
+
try {
|
|
198
|
+
const body = await store.get(key, { signal: opts.signal, ...(have > 0 ? { range: { start: have, end: size - 1 } } : {}) });
|
|
199
|
+
await pipeline(Readable.fromWeb(body), fs.createWriteStream(part, { flags: have > 0 ? "a" : "w" }));
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
const transient = err instanceof BlobTransientError || err?.cause instanceof BlobTransientError;
|
|
203
|
+
if (!transient || attempt + 1 >= max || opts.signal?.aborted)
|
|
204
|
+
throw err;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else if (have === 0) {
|
|
209
|
+
// An empty object: nothing to fetch, but the file must still exist to
|
|
210
|
+
// be checked and renamed into place like any other.
|
|
211
|
+
fs.writeFileSync(part, "");
|
|
212
|
+
}
|
|
213
|
+
if (fs.statSync(part).size !== size) {
|
|
214
|
+
if (attempt + 1 >= max)
|
|
215
|
+
throw new BlobTransientError(`download of ${key} stopped short`);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
if (opts.sha256) {
|
|
221
|
+
const h = await hashRange(fileRange(part), 0, size, opts.signal);
|
|
222
|
+
if (h.sha256 !== opts.sha256) {
|
|
223
|
+
fs.rmSync(part, { force: true });
|
|
224
|
+
throw new BlobSourceError(`${key} does not hash to ${opts.sha256}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
fs.renameSync(part, dest);
|
|
228
|
+
return { size };
|
|
229
|
+
}
|
|
230
|
+
const fileRange = (file) => ({
|
|
231
|
+
size: fs.statSync(file).size,
|
|
232
|
+
async *read(start, end) {
|
|
233
|
+
if (end > start)
|
|
234
|
+
yield* fs.createReadStream(file, { start, end: end - 1 });
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
// ---------- the store in force ----------
|
|
238
|
+
/** s3 when FOLDRUN_STORAGE_DRIVER=s3 and it is fully configured, else fs.
|
|
239
|
+
* (The same fall-back rule as storage.ts's driverFor.) */
|
|
240
|
+
export function blobStore() {
|
|
241
|
+
if (process.env.FOLDRUN_STORAGE_DRIVER === "s3") {
|
|
242
|
+
const cfg = s3Config();
|
|
243
|
+
if (cfg)
|
|
244
|
+
return bounded(s3BlobStore(cfg));
|
|
245
|
+
}
|
|
246
|
+
return bounded(fsBlobStore());
|
|
247
|
+
}
|
|
248
|
+
export const DEFAULT_LIMITS = { deadlineMs: 30_000, retries: 3, concurrency: 8, backoffMs: 200, minBytesPerSec: 1024 * 1024 };
|
|
249
|
+
export function limitsFromEnv(env = process.env) {
|
|
250
|
+
const n = (v, d) => (v !== undefined && /^\d+$/.test(v) ? Number(v) : d);
|
|
251
|
+
return {
|
|
252
|
+
deadlineMs: n(env.FOLDRUN_BLOB_DEADLINE_MS, DEFAULT_LIMITS.deadlineMs),
|
|
253
|
+
retries: n(env.FOLDRUN_BLOB_RETRIES, DEFAULT_LIMITS.retries),
|
|
254
|
+
concurrency: Math.max(1, n(env.FOLDRUN_BLOB_CONCURRENCY, DEFAULT_LIMITS.concurrency)),
|
|
255
|
+
backoffMs: DEFAULT_LIMITS.backoffMs,
|
|
256
|
+
minBytesPerSec: Math.max(1, n(env.FOLDRUN_BLOB_MIN_BYTES_PER_SEC, DEFAULT_LIMITS.minBytesPerSec)),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
class Semaphore {
|
|
260
|
+
waiting = [];
|
|
261
|
+
active = 0;
|
|
262
|
+
max;
|
|
263
|
+
constructor(max) {
|
|
264
|
+
this.max = max;
|
|
265
|
+
}
|
|
266
|
+
async run(fn) {
|
|
267
|
+
if (this.active >= this.max)
|
|
268
|
+
await new Promise((r) => this.waiting.push(r));
|
|
269
|
+
this.active++;
|
|
270
|
+
try {
|
|
271
|
+
return await fn();
|
|
272
|
+
}
|
|
273
|
+
finally {
|
|
274
|
+
this.active--;
|
|
275
|
+
this.waiting.shift()?.();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const isTransient = (err) => err instanceof BlobTransientError || ["ECONNRESET", "ETIMEDOUT", "EAGAIN", "EBUSY", "EMFILE"].includes(err?.code ?? "");
|
|
280
|
+
/**
|
|
281
|
+
* Wrap a store so every call has a deadline (an AbortSignal handed down; the
|
|
282
|
+
* call fails with BlobTransientError if it passes), is retried on transient
|
|
283
|
+
* failure within a budget, and counts against a concurrency cap.
|
|
284
|
+
*
|
|
285
|
+
* A `get` is deadline-bound only until the stream starts: a long download is
|
|
286
|
+
* the caller's to bound. A not-found, a 4xx and a bad key are never retried.
|
|
287
|
+
*/
|
|
288
|
+
export function bounded(inner, limits = {}) {
|
|
289
|
+
const L = { ...limitsFromEnv(), ...limits };
|
|
290
|
+
const gate = new Semaphore(L.concurrency);
|
|
291
|
+
/** The caller's signal and this attempt's deadline, either of which ends it. */
|
|
292
|
+
const both = (caller, ms) => {
|
|
293
|
+
const deadline = AbortSignal.timeout(ms);
|
|
294
|
+
return caller ? AbortSignal.any([caller, deadline]) : deadline;
|
|
295
|
+
};
|
|
296
|
+
async function call(what, caller, fn, bytes = 0) {
|
|
297
|
+
const ms = L.deadlineMs + Math.ceil((bytes / L.minBytesPerSec) * 1000);
|
|
298
|
+
return gate.run(async () => {
|
|
299
|
+
let attempt = 0;
|
|
300
|
+
for (;;) {
|
|
301
|
+
if (caller?.aborted)
|
|
302
|
+
throw caller.reason ?? new Error(`${what}: cancelled`);
|
|
303
|
+
const signal = both(caller, ms);
|
|
304
|
+
try {
|
|
305
|
+
return await Promise.race([
|
|
306
|
+
fn(signal),
|
|
307
|
+
new Promise((_, reject) => signal.addEventListener("abort", () => reject(caller?.aborted ? (caller.reason ?? new Error(`${what}: cancelled`)) : new BlobTransientError(`${what}: no answer within ${ms}ms`)), { once: true })),
|
|
308
|
+
]);
|
|
309
|
+
}
|
|
310
|
+
catch (err) {
|
|
311
|
+
if (caller?.aborted || !isTransient(err) || attempt >= L.retries)
|
|
312
|
+
throw err;
|
|
313
|
+
attempt++;
|
|
314
|
+
await new Promise((r) => setTimeout(r, L.backoffMs * 2 ** (attempt - 1) * (0.5 + Math.random())));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
return {
|
|
320
|
+
...inner,
|
|
321
|
+
kind: inner.kind,
|
|
322
|
+
putIfAbsent: (key, body, opts) => call("put", opts?.signal, (signal) => inner.putIfAbsent(key, body, { ...opts, signal }), body.byteLength),
|
|
323
|
+
// The deadline covers getting the stream going, not draining it: the
|
|
324
|
+
// timer is cleared once the object starts to arrive. The caller's signal
|
|
325
|
+
// stays attached to the stream, so cancelling still stops the download.
|
|
326
|
+
get: (key, opts) => call("get", opts?.signal, async () => {
|
|
327
|
+
const ctl = new AbortController();
|
|
328
|
+
const timer = setTimeout(() => ctl.abort(), L.deadlineMs);
|
|
329
|
+
const onCaller = () => ctl.abort(opts?.signal?.reason);
|
|
330
|
+
opts?.signal?.addEventListener("abort", onCaller, { once: true });
|
|
331
|
+
try {
|
|
332
|
+
return await inner.get(key, { ...opts, signal: ctl.signal });
|
|
333
|
+
}
|
|
334
|
+
finally {
|
|
335
|
+
clearTimeout(timer);
|
|
336
|
+
}
|
|
337
|
+
}),
|
|
338
|
+
// A streamed put has no fixed deadline (its length decides), so it gets the
|
|
339
|
+
// retry budget and the concurrency cap, and the s3 driver's own per-request
|
|
340
|
+
// deadlines. Cancelling is the caller's signal.
|
|
341
|
+
putSourceIfAbsent: (key, source, opts) => gate.run(async () => {
|
|
342
|
+
for (let attempt = 0;; attempt++) {
|
|
343
|
+
if (opts?.signal?.aborted)
|
|
344
|
+
throw opts.signal.reason ?? new Error("put: cancelled");
|
|
345
|
+
try {
|
|
346
|
+
return await inner.putSourceIfAbsent(key, source, opts);
|
|
347
|
+
}
|
|
348
|
+
catch (err) {
|
|
349
|
+
if (opts?.signal?.aborted || !isTransient(err) || attempt >= L.retries)
|
|
350
|
+
throw err;
|
|
351
|
+
await new Promise((r) => setTimeout(r, L.backoffMs * 2 ** attempt * (0.5 + Math.random())));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}),
|
|
355
|
+
abortStaleUploads: (o) => (inner.abortStaleUploads ? inner.abortStaleUploads(o) : Promise.resolve(0)),
|
|
356
|
+
head: (key, opts) => call("head", opts?.signal, (signal) => inner.head(key, { signal })),
|
|
357
|
+
delete: (key, opts) => call("delete", opts?.signal, (signal) => (inner.delete ? inner.delete(key, { signal }) : Promise.reject(new Error("this store cannot delete")))),
|
|
358
|
+
...(inner.purgeTenant ? { purgeTenant: (tenant) => inner.purgeTenant(tenant) } : {}),
|
|
359
|
+
...(inner.list ? { list: (prefix, o) => inner.list(prefix, o) } : {}),
|
|
360
|
+
...(inner.tenants ? { tenants: (o) => inner.tenants(o) } : {}),
|
|
361
|
+
};
|
|
362
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { BlobStore, BlobSweeper } from "./blob-store.ts";
|
|
2
|
+
import type { BlobPins } from "./blob-pins.ts";
|
|
3
|
+
import type { RevisionStore } from "./revision-store.ts";
|
|
4
|
+
export type MarkKind = "blob" | "manifest" | "workspace";
|
|
5
|
+
export interface Mark {
|
|
6
|
+
firstSeen: Date;
|
|
7
|
+
size: number;
|
|
8
|
+
}
|
|
9
|
+
/** Where "first seen unreferenced" is kept (a table in the platform). */
|
|
10
|
+
export interface SweepMarks {
|
|
11
|
+
/** The database's clock: every record and comparison uses it, so pods agree. */
|
|
12
|
+
now(): Promise<Date>;
|
|
13
|
+
load(tenant: string, kind: MarkKind): Promise<Map<string, Mark>>;
|
|
14
|
+
/** Record refs not yet recorded (an existing record keeps its first time). */
|
|
15
|
+
put(tenant: string, kind: MarkKind, entries: {
|
|
16
|
+
ref: string;
|
|
17
|
+
size: number;
|
|
18
|
+
}[], at: Date): Promise<void>;
|
|
19
|
+
clear(tenant: string, kind: MarkKind, refs: string[]): Promise<void>;
|
|
20
|
+
/** When the last run (real or dry) finished, for the schedule. */
|
|
21
|
+
lastRunAt(): Promise<Date | null>;
|
|
22
|
+
recordRun(report: SweepReport): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export interface SweepOptions {
|
|
25
|
+
dryRun: boolean;
|
|
26
|
+
/** Unreferenced for this long before deletion: the restore-point window. */
|
|
27
|
+
delayMs: number;
|
|
28
|
+
/** Never touch an object younger than this. Default one hour. */
|
|
29
|
+
minAgeMs?: number;
|
|
30
|
+
/** Delete at most this many objects in one run, all accounts together. */
|
|
31
|
+
maxDeletes: number;
|
|
32
|
+
/** Only these accounts (tests, an operator). */
|
|
33
|
+
tenants?: string[];
|
|
34
|
+
/** Go ahead even when the database holds no revision (default: refuse). */
|
|
35
|
+
allowEmpty?: boolean;
|
|
36
|
+
}
|
|
37
|
+
export interface SweepReport {
|
|
38
|
+
dryRun: boolean;
|
|
39
|
+
tenants: number;
|
|
40
|
+
scanned: number;
|
|
41
|
+
scannedBytes: number;
|
|
42
|
+
live: number;
|
|
43
|
+
/** Seen unreferenced for the first time in this run. */
|
|
44
|
+
marked: number;
|
|
45
|
+
markedBytes: number;
|
|
46
|
+
/** Unreferenced, but not for long enough yet. */
|
|
47
|
+
waiting: number;
|
|
48
|
+
deleted: number;
|
|
49
|
+
deletedBytes: number;
|
|
50
|
+
/** A dry run: due now, and what a real run would delete. */
|
|
51
|
+
wouldDelete: number;
|
|
52
|
+
wouldDeleteBytes: number;
|
|
53
|
+
/** Would have been deleted (dry run), or were due but a pin or a new revision held them. */
|
|
54
|
+
skippedHeld: number;
|
|
55
|
+
/** Not an object the sweeper owns (left alone). */
|
|
56
|
+
foreign: number;
|
|
57
|
+
errors: number;
|
|
58
|
+
refused?: string;
|
|
59
|
+
}
|
|
60
|
+
export interface SweepDeps {
|
|
61
|
+
blobs: BlobStore & BlobSweeper;
|
|
62
|
+
revisions: RevisionStore;
|
|
63
|
+
pins?: BlobPins;
|
|
64
|
+
}
|
|
65
|
+
export declare function sweepBlobs(deps: SweepDeps, marks: SweepMarks, opts: SweepOptions): Promise<SweepReport>;
|
|
66
|
+
/** For tests and a host without a database table (the clock is injectable). */
|
|
67
|
+
export declare function memorySweepMarks(clock?: () => Date): SweepMarks & {
|
|
68
|
+
runs: SweepReport[];
|
|
69
|
+
table: Map<string, Mark>;
|
|
70
|
+
};
|