Public: anyone can read this; the crew can change it.

The data volume has under 15% free #13917

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/13915-the-data-volume-has-under-15-free → main 3 commits fork

  • Merged. torque/13915-the-data-volume-has-under-15-free was merged into main as 7d44540.

Review

Sign in to review this change.
6 files changed +80 −6
docs/features/disk-free.md +7 −1
@@ -39,7 +39,13 @@ oldest remaining files go until the total fits.
3939 A preview whose pull went quiet is stopped (dormant) and waking it deploys again with a fresh
4040 checkout and install. `dropCheckouts` (`src/apps/dormant_checkout.ts`) removes its checkout
4141 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.
4349
4450 ## A clone idle for three days goes whole (#13734, #13843)
4551
src/apps/build_cache.ts +17 −2
@@ -104,9 +104,11 @@ export function cacheSize(root: string): number {
104104 }
105105 }
106106
107 const validAppId = (appId: string): boolean => /^[0-9a-z]+$/i.test(appId);
108
107109 /** Remove every environment's cache of an app; the next deploy builds from scratch. */
108110 export function clearBuildCache(dataDir: string, appId: string): void {
109 if (!/^[0-9a-z]+$/i.test(appId)) return;
111 if (!validAppId(appId)) return;
110112 const root = join(appsRoot(dataDir), appId);
111113 if (!existsSync(root)) return;
112114 for (const env of readdirSync(root))
@@ -115,10 +117,23 @@ export function clearBuildCache(dataDir: string, appId: string): void {
115117
116118 /** All environments' cache bytes for an app, for its page. */
117119 export function appCacheSize(dataDir: string, appId: string): number {
118 if (!/^[0-9a-z]+$/i.test(appId)) return 0;
120 if (!validAppId(appId)) return 0;
119121 const root = join(appsRoot(dataDir), appId);
120122 if (!existsSync(root)) return 0;
121123 let total = 0;
122124 for (const env of readdirSync(root)) if (isEnvName(env)) total += cacheSize(buildCacheRoot(dataDir, appId, env));
123125 return total;
124126 }
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 @@
22 * What a dormant preview gives back (#13631). A preview whose pull went quiet is stopped, and
33 * waking it deploys again the ordinary way: a fresh checkout and install. Its checkouts (one
44 * 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`).
77 */
88
99 import { readdirSync, statSync, type Dirent } from "node:fs";
src/apps/runtime.ts +3 −1
@@ -89,7 +89,7 @@ import {
8989 SAMPLE_SPAN_MS,
9090 } from "../core/meter.ts";
9191 import { dirBytes } from "./bytes.ts";
92 import { appCacheSize, buildCacheRoot } from "./build_cache.ts";
92 import { appCacheSize, buildCacheRoot, dropPreviewBuildCache } from "./build_cache.ts";
9393 import { BUSY_WORDS, busyReason, type BusyReason } from "./busy.ts";
9494 import { exportTree, readFileAt } from "./checkout.ts";
9595 import { seconds, Stopwatch } from "./timing.ts";
@@ -2307,6 +2307,8 @@ export class AppsRuntime {
23072307 .filter((oid): oid is string => oid !== undefined && oid !== "");
23082308 try {
23092309 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);
23102312 } catch (error) {
23112313 log.warn("could not drop dormant checkouts", { app: state.app.slug, env: state.env, error: String(error) });
23122314 }
test/apps-idle.test.ts +38
@@ -236,9 +236,12 @@ describe("previews of idle pulls sleep", () => {
236236 await runtime.stop();
237237 // A checkout left on disk by the life before: booting dormant gives it back.
238238 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 });
239240 runtime = new AppsRuntime(app, options());
240241 await runtime.start();
241242 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));
242245 expect(envOf(later)?.dormant).toBeDefined();
243246 expect(envOf(later)?.state).toBe("stopped");
244247 await sweeps(2);
@@ -247,4 +250,39 @@ describe("previews of idle pulls sleep", () => {
247250 ).length;
248251 expect(after).toBe(before);
249252 }, 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 });
250288 });
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
3939 expect(await dropCheckouts(dir, "app1", "production", [], STRAY_CHECKOUT_AGE_MS, now + STRAY_CHECKOUT_AGE_MS + 1000)).toBe(2);
4040 expect(existsSync(join(env, "abc1234"))).toBe(false);
4141 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs