Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(render-farm): cap chunks where every range keeps three parts beside the stash part

Richie McIlroyd3b65f92e602
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index 7e570d07c..666c7fc10 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -19,7 +19,11 @@ import {
type Run,
type TrackIndex,
} from "./mp4";
-import { planChunkBoundaries } from "./planning";
+import {
+ chunkPartLayout,
+ MIN_RANGE_PARTS,
+ planChunkBoundaries,
+} from "./planning";
import { ProbeEngine } from "./probe-engine";
import {
type AudioResultMeta,
@@ -34,12 +38,7 @@ import {
type VideoTask,
type WorkItem,
} from "./protocol";
-import {
- acceptOnce,
- completeUpload,
- PART_RANGES,
- reservePartRange,
-} from "./recovery";
+import { acceptOnce, completeUpload, reservePartRange } from "./recovery";
import { mediaS3ConfigFromEnv, S3, s3ConfigFromEnv } from "./s3";
import { pickQueued as pickQueuedTask } from "./scheduler";
import { planStitch, type StitchPart, uploadProblem } from "./stitch";
@@ -862,11 +861,8 @@ async function planJob(job: Job) {
leadInFrames: job.hls ? LEAD_IN_SECONDS * job.fps : 0,
});
const chunkCount = boundaries.length - 1;
- const partsPerChunk = Math.floor(9998 / chunkCount);
- // Each chunk's block opens with a part number the coordinator fills from
- // the chunk's stash (see stitch.ts); its dispatch ranges follow.
- const partLimit = Math.floor((partsPerChunk - 1) / PART_RANGES);
- if (partLimit < 3) {
+ const { partsPerChunk, partLimit } = chunkPartLayout(chunkCount);
+ if (partLimit < MIN_RANGE_PARTS) {
throw new Error("too many chunks for one multipart upload");
}
const samplesPerFrame = SAMPLE_RATE / job.fps;
diff --git a/apps/render-farm/src/planning.test.ts b/apps/render-farm/src/planning.test.ts
index 1ad4d62b7..b9b9a7129 100644
--- a/apps/render-farm/src/planning.test.ts
+++ b/apps/render-farm/src/planning.test.ts
@@ -1,5 +1,11 @@
import { describe, expect, test } from "bun:test";
-import { type ChunkPlanInput, planChunkBoundaries } from "./planning";
+import {
+ type ChunkPlanInput,
+ chunkPartLayout,
+ MAX_CHUNKS,
+ MIN_RANGE_PARTS,
+ planChunkBoundaries,
+} from "./planning";
const base: ChunkPlanInput = {
totalFrames: 1800,
@@ -78,7 +84,31 @@ test("all planner inputs leave six disjoint ranges even with a lead-in", () => {
...options,
});
const count = boundaries.length - 1;
- expect(Math.floor(Math.floor(9998 / count) / 6)).toBeGreaterThanOrEqual(3);
+ expect(chunkPartLayout(count).partLimit).toBeGreaterThanOrEqual(
+ MIN_RANGE_PARTS,
+ );
expectValid(boundaries, 2_160_000);
}
});
+
+test("the chunk cap is the largest count whose part ranges still fit", () => {
+ // The lead-in split adds one chunk after the cap.
+ const largest = MAX_CHUNKS + 1;
+ expect(chunkPartLayout(largest).partLimit).toBeGreaterThanOrEqual(
+ MIN_RANGE_PARTS,
+ );
+ expect(chunkPartLayout(largest + 1).partLimit).toBeLessThan(MIN_RANGE_PARTS);
+ for (const chunks of [MAX_CHUNKS, MAX_CHUNKS + 1, 527, 554, 10_000]) {
+ const boundaries = planChunkBoundaries({
+ ...base,
+ totalFrames: 2_160_000,
+ leadInFrames: 120,
+ chunks,
+ });
+ const count = boundaries.length - 1;
+ expect(count).toBeLessThanOrEqual(largest);
+ expect(chunkPartLayout(count).partLimit).toBeGreaterThanOrEqual(
+ MIN_RANGE_PARTS,
+ );
+ }
+});
diff --git a/apps/render-farm/src/planning.ts b/apps/render-farm/src/planning.ts
index c41d30f23..8e2b1eedf 100644
--- a/apps/render-farm/src/planning.ts
+++ b/apps/render-farm/src/planning.ts
@@ -23,7 +23,30 @@ export type ChunkPlanInput = {
* Boundaries sit on GOP edges when chunks are long enough, else on whole
* seconds; every chunk opens with its own IDR either way.
*/
-export const MAX_CHUNKS = Math.floor(9998 / (PART_RANGES * 3)) - 1;
+/** Part numbers an export's chunks share: S3's 10,000 less the header's part 1 and a spare. */
+const UPLOAD_PARTS = 9998;
+/** Fewest parts a dispatch range may hold. */
+export const MIN_RANGE_PARTS = 3;
+
+/**
+ * How an export's part numbers divide between its chunks: each chunk's block
+ * opens with one part the coordinator fills from the chunk's stash (see
+ * stitch.ts), followed by `PART_RANGES` dispatch ranges of `partLimit` parts.
+ */
+export function chunkPartLayout(chunkCount: number) {
+ const partsPerChunk = Math.floor(UPLOAD_PARTS / Math.max(1, chunkCount));
+ return {
+ partsPerChunk,
+ partLimit: Math.floor((partsPerChunk - 1) / PART_RANGES),
+ };
+}
+
+/**
+ * Most chunks the planner makes before the lead-in split, which can add one:
+ * every count up to `MAX_CHUNKS + 1` leaves each range `MIN_RANGE_PARTS`.
+ */
+export const MAX_CHUNKS =
+ Math.floor(UPLOAD_PARTS / (PART_RANGES * MIN_RANGE_PARTS + 1)) - 1;
export function planChunkBoundaries(input: ChunkPlanInput) {
const { totalFrames, fps } = input;