Public: anyone can read this; the crew can change it.
The data volume has under 15% free #13917
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13915-the-data-volume-has-under-15-free → main 3 commits fork
6 files
changed +80
−6
docs/features/disk-free.md +7 −1
| @@ -39,7 +39,13 @@ oldest remaining files go until the total fits. | ||
| 39 | 39 | A preview whose pull went quiet is stopped (dormant) and waking it deploys again with a fresh |
| 40 | 40 | checkout and install. `dropCheckouts` (`src/apps/dormant_checkout.ts`) removes its checkout |
| 41 | 41 | directories (`<env>/<sha7>`, `node_modules` and all) as it goes dormant, in the idle sweep and at |
| 42 | boot. Its data, home and build cache stay. | |
| 42 | boot. Its data and home stay. | |
| 43 | ||
| 44 | Its build cache goes with the checkouts (#13915). `dropPreviewBuildCache` | |
| 45 | (`src/apps/build_cache.ts`) removes the environment's `build-cache` directory when it is dormant | |
| 46 | and no deploy is running or queued, so a wake builds from scratch. Production's build cache is | |
| 47 | never dropped. The size of what this gives back is not measured here: the cache is a cargo | |
| 48 | target directory per preview, and the volume's own count shows whether it was enough. | |
| 43 | 49 | |
| 44 | 50 | ## A clone idle for three days goes whole (#13734, #13843) |
| 45 | 51 |
src/apps/build_cache.ts +17 −2
| @@ -104,9 +104,11 @@ export function cacheSize(root: string): number { | ||
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | const validAppId = (appId: string): boolean => /^[0-9a-z]+$/i.test(appId); | |
| 108 | ||
| 107 | 109 | /** Remove every environment's cache of an app; the next deploy builds from scratch. */ |
| 108 | 110 | export function clearBuildCache(dataDir: string, appId: string): void { |
| 109 | if (!/^[0-9a-z]+$/i.test(appId)) return; | |
| 111 | if (!validAppId(appId)) return; | |
| 110 | 112 | const root = join(appsRoot(dataDir), appId); |
| 111 | 113 | if (!existsSync(root)) return; |
| 112 | 114 | for (const env of readdirSync(root)) |
| @@ -115,10 +117,23 @@ export function clearBuildCache(dataDir: string, appId: string): void { | ||
| 115 | 117 | |
| 116 | 118 | /** All environments' cache bytes for an app, for its page. */ |
| 117 | 119 | export function appCacheSize(dataDir: string, appId: string): number { |
| 118 | if (!/^[0-9a-z]+$/i.test(appId)) return 0; | |
| 120 | if (!validAppId(appId)) return 0; | |
| 119 | 121 | const root = join(appsRoot(dataDir), appId); |
| 120 | 122 | if (!existsSync(root)) return 0; |
| 121 | 123 | let total = 0; |
| 122 | 124 | for (const env of readdirSync(root)) if (isEnvName(env)) total += cacheSize(buildCacheRoot(dataDir, appId, env)); |
| 123 | 125 | return total; |
| 124 | 126 | } |
| 127 | ||
| 128 | /** | |
| 129 | * A dormant preview's build cache goes (#13915): a wake deploys again and builds from scratch, | |
| 130 | * which a preview can afford and the data volume cannot do without. Production's stays. Whether | |
| 131 | * anything was removed. | |
| 132 | */ | |
| 133 | export function dropPreviewBuildCache(dataDir: string, appId: string, env: EnvName): boolean { | |
| 134 | if (env === "production" || !validAppId(appId)) return false; | |
| 135 | const root = buildCacheRoot(dataDir, appId, env); | |
| 136 | if (!existsSync(root)) return false; | |
| 137 | rmSync(root, { recursive: true, force: true }); | |
| 138 | return true; | |
| 139 | } |
src/apps/dormant_checkout.ts +2 −2
| @@ -2,8 +2,8 @@ | ||
| 2 | 2 | * What a dormant preview gives back (#13631). A preview whose pull went quiet is stopped, and |
| 3 | 3 | * waking it deploys again the ordinary way: a fresh checkout and install. Its checkouts (one |
| 4 | 4 | * directory named for the first seven characters of a commit, `node_modules` about 110 MB of |
| 5 | * each) are then dead weight on the data volume. They go; the environment's data, home and | |
| 6 | * build cache stay. | |
| 5 | * each) are then dead weight on the data volume. They go; the environment's data and home stay. | |
| 6 | * Since #13915 its build cache goes with them (`dropPreviewBuildCache`, `build_cache.ts`). | |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { readdirSync, statSync, type Dirent } from "node:fs"; |
src/apps/runtime.ts +3 −1
| @@ -89,7 +89,7 @@ import { | ||
| 89 | 89 | SAMPLE_SPAN_MS, |
| 90 | 90 | } from "../core/meter.ts"; |
| 91 | 91 | import { dirBytes } from "./bytes.ts"; |
| 92 | import { appCacheSize, buildCacheRoot } from "./build_cache.ts"; | |
| 92 | import { appCacheSize, buildCacheRoot, dropPreviewBuildCache } from "./build_cache.ts"; | |
| 93 | 93 | import { BUSY_WORDS, busyReason, type BusyReason } from "./busy.ts"; |
| 94 | 94 | import { exportTree, readFileAt } from "./checkout.ts"; |
| 95 | 95 | import { seconds, Stopwatch } from "./timing.ts"; |
| @@ -2307,6 +2307,8 @@ export class AppsRuntime { | ||
| 2307 | 2307 | .filter((oid): oid is string => oid !== undefined && oid !== ""); |
| 2308 | 2308 | try { |
| 2309 | 2309 | await dropCheckouts(this.app.config.dataDir, state.app.id, state.env, keep); |
| 2310 | if (state.dormant !== undefined && state.deploying === undefined && state.queued === undefined) | |
| 2311 | dropPreviewBuildCache(this.app.config.dataDir, state.app.id, state.env); | |
| 2310 | 2312 | } catch (error) { |
| 2311 | 2313 | log.warn("could not drop dormant checkouts", { app: state.app.slug, env: state.env, error: String(error) }); |
| 2312 | 2314 | } |
test/apps-idle.test.ts +38
| @@ -236,9 +236,12 @@ describe("previews of idle pulls sleep", () => { | ||
| 236 | 236 | await runtime.stop(); |
| 237 | 237 | // A checkout left on disk by the life before: booting dormant gives it back. |
| 238 | 238 | mkdirSync(join(envDir(dataDir, record.id, `pull-${later}`), "abcdef0", "node_modules"), { recursive: true }); |
| 239 | mkdirSync(join(envDir(dataDir, record.id, `pull-${later}`), "build-cache", "0123456789abcdef"), { recursive: true }); | |
| 239 | 240 | runtime = new AppsRuntime(app, options()); |
| 240 | 241 | await runtime.start(); |
| 241 | 242 | await until(() => (checkouts(later).length === 0 ? true : undefined)); |
| 243 | // #13915: booting dormant gives the build cache back with the checkouts. | |
| 244 | await until(() => (!existsSync(join(envDir(dataDir, record.id, `pull-${later}`), "build-cache")) ? true : undefined)); | |
| 242 | 245 | expect(envOf(later)?.dormant).toBeDefined(); |
| 243 | 246 | expect(envOf(later)?.state).toBe("stopped"); |
| 244 | 247 | await sweeps(2); |
| @@ -247,4 +250,39 @@ describe("previews of idle pulls sleep", () => { | ||
| 247 | 250 | ).length; |
| 248 | 251 | expect(after).toBe(before); |
| 249 | 252 | }, 120_000); |
| 253 | ||
| 254 | test("only a dormant preview with nothing coming up gives back its build cache (#13915)", async () => { | |
| 255 | // Invariant: the cache goes with the checkouts when the environment is dormant and no deploy is | |
| 256 | // running or queued; a live, deploying or queued environment keeps it. | |
| 257 | const cache = (n: number): string => join(envDir(dataDir, record.id, `pull-${n}`), "build-cache"); | |
| 258 | const plant = (n: number): void => void mkdirSync(join(cache(n), "0123456789abcdef"), { recursive: true }); | |
| 259 | const run = listRuns(app.db, repo.id, { kind: "deploy", limit: 1 })[0]; | |
| 260 | if (!run) throw new Error("an earlier test left no deploy run"); | |
| 261 | const coming = { ...run, commitOid: "1234567abcdef" }; | |
| 262 | const state = ( | |
| 263 | n: number, | |
| 264 | extra: Partial<Parameters<AppsRuntime["dropDormantCheckouts"]>[0]>, | |
| 265 | ): Parameters<AppsRuntime["dropDormantCheckouts"]>[0] => ({ | |
| 266 | app: record, | |
| 267 | env: `pull-${n}`, | |
| 268 | chain: Promise.resolve(), | |
| 269 | openStreams: 0, | |
| 270 | openRequests: 0, | |
| 271 | restarts: [], | |
| 272 | ...extra, | |
| 273 | }); | |
| 274 | const dormant = { since: new Date().toISOString(), idleSince: new Date().toISOString(), commitOid: "abcdef0123456" }; | |
| 275 | plant(9001); | |
| 276 | plant(9002); | |
| 277 | plant(9003); | |
| 278 | plant(9004); | |
| 279 | await runtime.dropDormantCheckouts(state(9001, {})); | |
| 280 | await runtime.dropDormantCheckouts(state(9002, { dormant, queued: coming })); | |
| 281 | await runtime.dropDormantCheckouts(state(9003, { dormant, deploying: { run: coming, abort: new AbortController() } })); | |
| 282 | expect(existsSync(cache(9001))).toBe(true); | |
| 283 | expect(existsSync(cache(9002))).toBe(true); | |
| 284 | expect(existsSync(cache(9003))).toBe(true); | |
| 285 | await runtime.dropDormantCheckouts(state(9004, { dormant })); | |
| 286 | expect(existsSync(cache(9004))).toBe(false); | |
| 287 | }); | |
| 250 | 288 | }); |
test/dormant-checkout.test.ts +13
| @@ -39,3 +39,16 @@ test("a sweep with an age leaves a checkout touched within it, and takes an olde | ||
| 39 | 39 | expect(await dropCheckouts(dir, "app1", "production", [], STRAY_CHECKOUT_AGE_MS, now + STRAY_CHECKOUT_AGE_MS + 1000)).toBe(2); |
| 40 | 40 | expect(existsSync(join(env, "abc1234"))).toBe(false); |
| 41 | 41 | }); |
| 42 | ||
| 43 | test("a dormant preview's build cache goes, production's stays (#13915)", async () => { | |
| 44 | const { dropPreviewBuildCache } = await import("../src/apps/build_cache.ts"); | |
| 45 | const dir = mkdtempSync(join(tmpdir(), "dormant-")); | |
| 46 | for (const env of ["pull-7", "production"] as const) mkdirSync(join(envDir(dir, "app1", env), "build-cache", "0123456789abcdef"), { recursive: true }); | |
| 47 | mkdirSync(join(envDir(dir, "app1", "pull-7"), "data"), { recursive: true }); | |
| 48 | expect(dropPreviewBuildCache(dir, "app1", "pull-7")).toBe(true); | |
| 49 | expect(dropPreviewBuildCache(dir, "app1", "pull-7")).toBe(false); | |
| 50 | expect(dropPreviewBuildCache(dir, "app1", "production")).toBe(false); | |
| 51 | expect(existsSync(join(envDir(dir, "app1", "pull-7"), "build-cache"))).toBe(false); | |
| 52 | expect(existsSync(join(envDir(dir, "app1", "pull-7"), "data"))).toBe(true); | |
| 53 | expect(existsSync(join(envDir(dir, "app1", "production"), "build-cache"))).toBe(true); | |
| 54 | }); |
new above
offline