Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(render-farm): plan export parts from chunk stashes instead of padding
Richie McIlroyab7b5ce5cec5
diff --git a/apps/render-farm/src/stitch.test.ts b/apps/render-farm/src/stitch.test.tsnew file mode 100644index 000000000..695a506c6--- /dev/null+++ b/apps/render-farm/src/stitch.test.ts@@ -0,0 +1,159 @@+import { describe, expect, test } from "bun:test";+import { MIN_PART } from "./protocol";+import {+ HEADER_PART,+ planStitch,+ type StashedChunk,+ stashBytes,+ uploadProblem,+} from "./stitch";++const MB = 1024 * 1024;++/** A chunk of `bytes`, split the way a worker splits it. */+function chunk(index: number, bytes: number): StashedChunk {+ const slot = 2 + index * 100;+ const stash = stashBytes(bytes);+ const parts: StashedChunk["parts"] = [];+ let rest = bytes - stash;+ let partNumber = slot + 1;+ while (rest > 0) {+ const size = rest >= 16 * MB + MIN_PART ? 16 * MB : rest;+ parts.push({ partNumber: partNumber++, etag: `e${partNumber}`, size });+ rest -= size;+ }+ return { slot, stash: { key: `s/${index}`, bytes: stash }, parts };+}++/** The whole file as S3 will join it: header, then every chunk in order. */+function layout(header: number, chunks: StashedChunk[]) {+ const plan = planStitch(header, chunks);+ const all = [+ ...plan.map((part) => ({ partNumber: part.partNumber, size: part.bytes })),+ ...chunks.flatMap((c) =>+ c.parts.map((part) => ({ partNumber: part.partNumber, size: part.size })),+ ),+ ].sort((a, b) => a.partNumber - b.partNumber);+ return { plan, all };+}++function expectValid(header: number, bytes: number[]) {+ const chunks = bytes.map((b, i) => chunk(i, b));+ const { plan, all } = layout(header, chunks);+ for (const part of all.slice(0, -1)) {+ expect(part.size).toBeGreaterThanOrEqual(MIN_PART);+ }+ const numbers = all.map((part) => part.partNumber);+ expect(new Set(numbers).size).toBe(numbers.length);+ expect(all.reduce((sum, part) => sum + part.size, 0)).toBe(+ header + bytes.reduce((sum, b) => sum + b, 0),+ );+ // Sources appear in file order: header first, then each chunk's stash.+ const order = plan+ .flatMap((part) => part.sources)+ .map((s) => (s.kind === "header" ? -1 : Number(s.key.slice(2))));+ expect(order).toEqual([-1, ...bytes.map((_, i) => i)]);+ return plan;+}++describe("planStitch", () => {+ test("a short export becomes one part with no padding", () => {+ const plan = expectValid(40_000, Array(9).fill(220_000));+ expect(plan).toHaveLength(1);+ expect(plan[0]?.partNumber).toBe(HEADER_PART);+ expect(plan[0]?.bytes).toBe(40_000 + 9 * 220_000);+ });++ test("large chunks copy their stashes and only the header is joined", () => {+ const plan = expectValid(300_000, Array(6).fill(40 * MB));+ expect(plan[0]?.sources.map((s) => s.kind)).toEqual(["header", "stash"]);+ for (const part of plan.slice(1)) expect(part.sources).toHaveLength(1);+ });++ test("mixed and awkward sizes always form valid parts", () => {+ const sizes = [+ [3 * MB, 0.1 * MB, 12 * MB, 0.2 * MB, 0.2 * MB, 7 * MB, 30 * MB, 1 * MB],+ [6 * MB, 6 * MB, 6 * MB],+ [0.5 * MB, 50 * MB, 0.5 * MB],+ [MIN_PART - 1, MIN_PART, MIN_PART + 1, 2 * MIN_PART - 1, 2 * MIN_PART],+ [1],+ ];+ for (const bytes of sizes) expectValid(1000, bytes.map(Math.round));+ });+});++describe("uploadProblem", () => {+ const upload = { stashKey: "s/0", firstPart: 3, partLimit: 10 };+ test("accepts a chunk split as the worker splits it", () => {+ const c = chunk(0, 40 * MB);+ expect(+ uploadProblem(+ { ...upload, firstPart: c.slot + 1 },+ { bytes: 40 * MB, stash: c.stash, parts: c.parts },+ ),+ ).toBeNull();+ });+ test("rejects padding, missing bytes and foreign stashes", () => {+ expect(+ uploadProblem(upload, {+ bytes: MB,+ stash: { key: "s/0", bytes: MB },+ parts: [],+ }),+ ).toBeNull();+ expect(+ uploadProblem(upload, {+ bytes: MB,+ stash: { key: "s/1", bytes: MB },+ parts: [],+ }),+ ).toContain("dispatch");+ expect(+ uploadProblem(upload, {+ bytes: 12 * MB,+ stash: { key: "s/0", bytes: MIN_PART },+ parts: [{ partNumber: 3, size: 6 * MB }],+ }),+ ).toContain("add up");+ expect(+ uploadProblem(upload, {+ bytes: 14 * MB,+ stash: { key: "s/0", bytes: MIN_PART },+ parts: [+ { partNumber: 3, size: 6 * MB },+ { partNumber: 4, size: 14 * MB - MIN_PART - 6 * MB },+ ],+ }),+ ).toContain("minimum");+ expect(+ uploadProblem(upload, {+ bytes: 20 * MB,+ stash: { key: "s/0", bytes: MIN_PART },+ parts: [{ partNumber: 13, size: 20 * MB - MIN_PART }],+ }),+ ).toContain("range");+ });+});++describe("planStitch ahead of assembly", () => {+ test("parts planned for an accepted run are the parts of the final plan", () => {+ const sizes = [0.3, 12, 0.2, 0.2, 7, 30, 1, 0.4, 6, 0.1].map((mb) =>+ Math.round(mb * MB),+ );+ const chunks = sizes.map((bytes, index) => chunk(index, bytes));+ const final = planStitch(0, chunks);+ const key = (part: {+ partNumber: number;+ sources: { kind: string; key?: string }[];+ }) =>+ `${part.partNumber}:${part.sources.map((s) => s.key ?? s.kind).join(",")}`;+ const finalKeys = new Set(final.map(key));+ for (let run = 1; run < chunks.length; run++) {+ for (const part of planStitch(0, chunks.slice(0, run), {+ partial: true,+ })) {+ expect(finalKeys.has(key(part))).toBe(true);+ }+ }+ });+});diff --git a/apps/render-farm/src/stitch.ts b/apps/render-farm/src/stitch.tsnew file mode 100644index 000000000..5ee76932c--- /dev/null+++ b/apps/render-farm/src/stitch.ts@@ -0,0 +1,125 @@+import { MIN_PART } from "./protocol";++/**+ * How an export's bytes become S3 multipart parts without padding.+ *+ * Every part but the last must be at least 5 MiB, yet a short or static+ * chunk can encode to far less. Each chunk therefore stashes its opening+ * bytes (the whole chunk when it is under 10 MiB) as an object of its own and+ * uploads only the rest as parts, each at least 5 MiB. The coordinator turns+ * the stashes into parts: a stash that can stand alone is copied server side,+ * and runs of small ones are joined, together with the MP4 header that opens+ * the file, into parts large enough to be valid.+ */++export type StashedChunk = {+ /** Part number reserved for the coordinator, below the chunk's own parts. */+ slot: number;+ stash: { key: string; bytes: number };+ parts: { partNumber: number; etag: string; size: number }[];+};++export type StitchSource =+ | { kind: "header"; bytes: number }+ | { kind: "stash"; key: string; bytes: number };++export type StitchPart = {+ partNumber: number;+ sources: StitchSource[];+ bytes: number;+};++export const HEADER_PART = 1;++/** Bytes of a chunk that go into its stash rather than its own parts. */+export function stashBytes(chunkBytes: number) {+ return chunkBytes < 2 * MIN_PART ? chunkBytes : MIN_PART;+}++/**+ * With `partial`, `chunks` is only the run accepted so far: the part still+ * open at its end is left out, since later chunks may join it.+ */+export function planStitch(+ headerBytes: number,+ chunks: StashedChunk[],+ options: { partial?: boolean } = {},+): StitchPart[] {+ const out: StitchPart[] = [];+ let pending: StitchPart | null = {+ partNumber: HEADER_PART,+ sources: [{ kind: "header", bytes: headerBytes }],+ bytes: headerBytes,+ };+ const emit = () => {+ if (pending) out.push(pending);+ pending = null;+ };+ for (const chunk of chunks) {+ const source: StitchSource = {+ kind: "stash",+ key: chunk.stash.key,+ bytes: chunk.stash.bytes,+ };+ if (!pending) {+ if (chunk.parts.length > 0 || chunk.stash.bytes >= MIN_PART) {+ out.push({+ partNumber: chunk.slot,+ sources: [source],+ bytes: source.bytes,+ });+ continue;+ }+ pending = { partNumber: chunk.slot, sources: [], bytes: 0 };+ }+ pending.sources.push(source);+ pending.bytes += source.bytes;+ // A chunk's own parts must follow a finished part. Flushing as soon as+ // a part is valid keeps what the coordinator downloads to a minimum:+ // the next stash that can stand alone is copied instead.+ if (chunk.parts.length > 0 || pending.bytes >= MIN_PART) emit();+ }+ if (!options.partial) emit();+ for (const part of options.partial ? out : out.slice(0, -1)) {+ if (part.bytes < MIN_PART) {+ throw new Error(`part ${part.partNumber} is under S3's 5 MiB minimum`);+ }+ }+ return out;+}++/**+ * A chunk result is usable only if its stash and parts are exactly the bytes+ * it rendered, in its own part range, with every part at least 5 MiB.+ */+export function uploadProblem(+ upload: { stashKey: string; firstPart: number; partLimit: number },+ result: {+ bytes: number;+ stash?: { key: string; bytes: number };+ parts: { partNumber: number; size: number }[];+ },+) {+ if (!result.stash || result.stash.key !== upload.stashKey) {+ return "stash does not match the dispatch";+ }+ if (result.stash.bytes !== stashBytes(result.bytes)) {+ return "stash size does not match the chunk";+ }+ const partBytes = result.parts.reduce((sum, part) => sum + part.size, 0);+ if (result.stash.bytes + partBytes !== result.bytes) {+ return "stash and parts do not add up to the chunk";+ }+ for (const part of result.parts) {+ if (+ part.partNumber < upload.firstPart ||+ part.partNumber >= upload.firstPart + upload.partLimit+ ) {+ return `part ${part.partNumber} is outside the dispatch's range`;+ }+ if (part.size < MIN_PART) {+ return `part ${part.partNumber} is under S3's 5 MiB minimum`;+ }+ }+ return null;+}