diff --git a/sdk/cloud/server/media.ts b/sdk/cloud/server/media.ts index ab5b5b68..fc2f2bb9 100644 --- a/sdk/cloud/server/media.ts +++ b/sdk/cloud/server/media.ts @@ -225,13 +225,54 @@ export async function farmInputs(root: string, farm: FarmBlobs, skip: (p: string let sent = 0 const todo = media.filter((m, i) => !have.has(m.oid) && media.findIndex((x) => x.oid === m.oid) === i) if (todo.length) said(`sending ${todo.length} media files (${(todo.reduce((n, m) => n + m.size, 0) / 1e6).toFixed(1)} MB) to the farm's cache, once`) - for (const m of todo) { - await farm.put(m.abs, m.oid, m.size) - sent += m.size + // four at a time: one by one, 50 takes took minutes on a box whose uplink sits mostly idle + let next = 0 + const one = async () => { + for (let i = next++; i < todo.length; i = next++) { + await farm.put(todo[i].abs, todo[i].oid, todo[i].size) + sent += todo[i].size + } } + await Promise.all(Array.from({ length: Math.min(4, todo.length) }, one)) return { media: media.map(({ abs: _, ...m }) => m), sent } } +/** the project's video and audio that no source file names, which a render doesn't need: neither sent to the farm nor + * packed. 12-03: footage/originals/ (the full-size takes, several GB) went to the farm on the first render of every + * StreetTalk run, a still included, and the render ran past its 5 minutes. A file is named by its path, its file name, + * or its stem with its folder's name ("originals/" + `${id}.mov`). When no source names any, all of it is used, as + * before; RAEA_CLOUD_GPU_ALL_MEDIA=1 sends it all regardless */ +export function unusedMedia(root: string, skip: (p: string) => boolean = () => false): string[] { + if (process.env.RAEA_CLOUD_GPU_ALL_MEDIA === "1") return [] + const AV = new Set(["mp4", "mov", "m4v", "webm", "mkv", "mxf", "avi", "wav", "mp3", "m4a", "aac", "flac", "ogg"]) + const SRC = new Set(["tsx", "ts", "jsx", "js", "mjs", "json", "css"]) + // folders of what's made for the agent or by a render, not what a composition reads from (footage/index.json lists + // every take) + const NOT_SOURCE = new Set(["footage", "brief_docs", "review", "out", "node_modules", "previews"]) + const av: string[] = [] + let text = "" + const walk = (dir: string, source: boolean) => { + for (const e of readdirSync(dir, { withFileTypes: true })) { + const abs = path.join(dir, e.name) + const rel = path.relative(root, abs).split(path.sep).join("/") + if (skip(rel) || e.name === ".git" || e.name.startsWith(".raea-cloud")) continue + const ext = e.name.includes(".") ? e.name.split(".").pop()!.toLowerCase() : "" + if (e.isDirectory()) walk(abs, source && !(dir === root && NOT_SOURCE.has(e.name))) + else if (e.isFile() && AV.has(ext)) av.push(rel) + else if (e.isFile() && source && SRC.has(ext) && statSync(abs).size < 2 << 20) text += readFileSync(abs, "utf8") + "\n" + } + } + walk(root, true) + const named = (rel: string) => { + const name = rel.split("/").pop()! + const stem = name.replace(/\.[^.]+$/, "") + const folder = rel.includes("/") ? rel.split("/").slice(-2, -1)[0] + "/" : "" + return text.includes(rel) || text.includes(name) || (stem.length >= 3 && text.includes(stem) && (!folder || text.includes(folder))) + } + const used = av.filter(named) + return used.length ? av.filter((f) => !used.includes(f)) : [] +} + /** tar's exclude-from lines for paths that go by hash: each matched as written (glob characters escaped) */ export const excludes = (media: Media[]) => media.map((m) => "./" + m.path.replace(/[\\*?[\]]/g, (c) => "\\" + c)).join("\n") + "\n" diff --git a/sdk/cloud/server/offload.ts b/sdk/cloud/server/offload.ts index 2bff681b..7caad352 100644 --- a/sdk/cloud/server/offload.ts +++ b/sdk/cloud/server/offload.ts @@ -21,7 +21,7 @@ import os from "node:os" import path from "node:path" import { pathToFileURL } from "node:url" import { RaeaCloud } from "../src/index.ts" -import { excluded, excludes as mediaExcludes, farmBlobs, farmInputs, inputs, lfsOf, localSources, outputs } from "./media.ts" +import { excluded, excludes as mediaExcludes, farmBlobs, farmInputs, inputs, lfsOf, localSources, outputs, unusedMedia } from "./media.ts" const REAL = process.env.RAEA_REAL ?? "/opt/raea/bin/raea" // flags whose value is a path raea writes; the rest of a render's path arguments are read @@ -111,16 +111,24 @@ async function main() { const sent = path.join(tmp, "in.tar") // the media that goes by hash: out of the tar, named in its manifest const lfs = lfsOf(p.root) - let byHash = typeof lfs === "string" ? [] : await inputs(p.root, lfs, excluded(excludes)) + // video and audio no source file names stays here, out of the tar and the farm's cache (media.ts unusedMedia) + const unused = new Set(unusedMedia(p.root, excluded(excludes))) + if (unused.size) say(`${unused.size} video and audio files no source names stay here`) + const skip = (rel: string) => excluded(excludes)(rel) || unused.has(rel) + let byHash = typeof lfs === "string" ? [] : await inputs(p.root, lfs, skip) // not in raea's LFS: the media goes into the farm's own cache by hash, once, instead of in every render's tar // (RAEA_CLOUD_GPU_BLOBS=0 puts it back in the tar). A farm without the cache, or one that can't be reached here, // leaves it in the tar, and the health check below says why the farm can't be reached let viaFarm: { media: typeof byHash; sent: number } | null = null if (typeof lfs === "string" && process.env.RAEA_CLOUD_GPU_BLOBS !== "0") { - viaFarm = await farmInputs(p.root, farmBlobs(url, token), excluded(excludes), say).catch((e) => (say(`the farm's media cache: ${(e as Error).message}; the media goes in the tar`), null)) + viaFarm = await farmInputs(p.root, farmBlobs(url, token), skip, say).catch((e) => (say(`the farm's media cache: ${(e as Error).message}; the media goes in the tar`), null)) if (viaFarm) byHash = viaFarm.media } const extra: string[] = [] + if (unused.size) { + writeFileSync(path.join(tmp, "unused-excludes"), mediaExcludes([...unused].map((f) => ({ path: f, oid: "", size: 0 })))) + extra.push("-X", path.join(tmp, "unused-excludes")) + } if (byHash.length) { writeFileSync(path.join(tmp, "media-excludes"), mediaExcludes(byHash)) writeFileSync(path.join(tmp, ".raea-cloud-media.json"), JSON.stringify({ objects: byHash })) diff --git a/sdk/cloud/test/unused-media.ts b/sdk/cloud/test/unused-media.ts new file mode 100644 index 00000000..bb18ad63 --- /dev/null +++ b/sdk/cloud/test/unused-media.ts @@ -0,0 +1,40 @@ +// What a render leaves on the box (server/media.ts unusedMedia): video and audio no source file names. 12-03: every +// StreetTalk run's first render sent footage/originals/ (several GB) to the farm, a still included, and ran past its +// 5 minutes. Run: node test/unused-media.ts (prints ok, or FAIL and exits 1) +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import path from "node:path" +import { unusedMedia } from "../server/media.ts" + +const fail = (m: string): never => { + console.error(`FAIL ${m}`) + process.exit(1) +} +const project = (files: Record) => { + const root = mkdtempSync(path.join(tmpdir(), "raea-unused-")) + for (const [f, c] of Object.entries(files)) { + mkdirSync(path.dirname(path.join(root, f)), { recursive: true }) + writeFileSync(path.join(root, f), c) + } + return root +} +const same = (a: string[], b: string[], what: string) => JSON.stringify([...a].sort()) === JSON.stringify([...b].sort()) || fail(`${what}: ${JSON.stringify([...a].sort())}`) + +// a cut on the light copies: the originals and the untouched takes stay; index.json (which lists every take) isn't source +const takes = { + "footage/Angle1-Take1.mp4": "x", "footage/Angle1-Take2.mp4": "x", "footage/Angle2-Take1.mp4": "x", + "footage/originals/Angle1-Take1.mov": "x", "footage/originals/Angle1-Take2.mov": "x", "footage/originals/Angle2-Take1.mov": "x", + "footage/index.json": JSON.stringify(["Angle1-Take1.mp4", "Angle1-Take2.mp4", "Angle2-Take1.mp4"]), + "media/music/bed.mp3": "x", +} +same(unusedMedia(project({ ...takes, "cut_a.tsx": `