Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(render-farm): bind segment reports to the reporting dispatch

Richie McIlroyd0d74930055c
diff --git a/apps/render-farm/src/coordinator.test.ts b/apps/render-farm/src/coordinator.test.ts
index a3a2c02b0..848945bef 100644
--- a/apps/render-farm/src/coordinator.test.ts
+++ b/apps/render-farm/src/coordinator.test.ts
@@ -452,10 +452,12 @@ test("segment reports only list objects the reporting dispatch wrote", async ()
const original = videoState(j);
h.jobs.set(j.id, j);
await h.dispatchedTask(j, original);
- const report = (key: string) =>
+ const hedge = videoState(j, true);
+ await h.dispatchedTask(j, hedge);
+ const report = (key: string, state = original) =>
h.fetch(
new Request(
- `http://test/tasks/${encodeURIComponent(original.task.taskId)}/segment`,
+ `http://test/tasks/${encodeURIComponent(state.task.taskId)}/segment`,
{
method: "POST",
headers: {
@@ -475,9 +477,11 @@ test("segment reports only list objects the reporting dispatch wrote", async ()
);
expect((await report("media/private.mp4")).status).toBe(400);
expect((await report("hls/job/c0-p12-0.m4s")).status).toBe(400);
+ expect((await report("hls/job/c0-p2-0.m4s", hedge)).status).toBe(400);
expect(j.hls.segments.size).toBe(0);
+ expect((await report("hls/job/c0-p12-0.m4s", hedge)).status).toBe(200);
expect((await report("hls/job/c0-p2-0.m4s")).status).toBe(200);
- expect(j.hls.segments.get(0)?.get(0)?.key).toBe("hls/job/c0-p2-0.m4s");
+ expect(j.hls.segments.get(0)?.get(0)?.key).toBe("hls/job/c0-p12-0.m4s");
});
test("job acknowledgement waits for a planning receipt and receipt-only jobs resume", async () => {
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index 241111b8e..3a4494c76 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -226,6 +226,7 @@ type TaskState = {
attempts: number;
dispatching?: boolean;
reattach?: boolean;
+ firstPart?: number;
duplicateOf?: string;
duplicated?: boolean;
/** Reserved by a busy slot ahead of time; not started until it reports progress. */
@@ -1183,6 +1184,7 @@ async function resumeJobs() {
state.attempts = task.attempt ?? 0;
state.worker = worker;
state.reattach = true;
+ if (task.kind === "video") state.firstPart = task.upload.firstPart;
}
if (duplicateOf) {
const original = job.tasks.get(duplicateOf);
@@ -1203,6 +1205,7 @@ async function resumeJobs() {
) {
state.attempts = 1;
state.reattach = true;
+ state.firstPart = job.chunks[state.task.chunk]?.firstPart;
}
}
if (job.hls) {
@@ -1293,6 +1296,7 @@ async function dispatchedTask(
// The journaled plan already reserves every chunk's first range (a resumed
// coordinator never reuses it), so first dispatches skip this write; it
// cost ~1 s of time to first segment on long exports.
+ state.firstPart = firstPart;
if (range === 0) return dispatched;
await journalPut(
journalKey(job.id, `dispatches/${chunk.index}/${range}.json`),
@@ -2199,9 +2203,15 @@ Bun.serve({
const state = job.tasks.get(taskId);
const plan =
state?.task.kind === "video" ? job.chunks[state.task.chunk] : undefined;
- const report = plan
- ? checkSegmentReport(await request.json(), job.hls.prefix, plan)
- : null;
+ const report =
+ plan && state?.firstPart !== undefined
+ ? checkSegmentReport(
+ await request.json(),
+ job.hls.prefix,
+ plan,
+ state.firstPart,
+ )
+ : null;
if (!report) return new Response("invalid segment", { status: 400 });
job.hls.extradata ??= report.extradata || undefined;
let chunk = job.hls.segments.get(report.chunk);
diff --git a/apps/render-farm/src/hls.test.ts b/apps/render-farm/src/hls.test.ts
index 240f07200..8caad5e11 100644
--- a/apps/render-farm/src/hls.test.ts
+++ b/apps/render-farm/src/hls.test.ts
@@ -67,13 +67,7 @@ describe("segmentCuts", () => {
});
describe("checkSegmentReport", () => {
- const chunk = {
- index: 3,
- frames: [90, 150] as [number, number],
- firstPart: 40,
- partLimit: 10,
- dispatches: 2,
- };
+ const chunk = { index: 3, frames: [90, 150] as [number, number] };
const report = (overrides: Record<string, unknown> = {}) => ({
chunk: 3,
index: 1,
@@ -84,28 +78,23 @@ describe("checkSegmentReport", () => {
...overrides,
});
- test("accepts a segment from any dispatched range of the chunk", () => {
- expect(checkSegmentReport(report(), "hls/job", chunk)).not.toBeNull();
- expect(
- checkSegmentReport(
- report({ key: segmentKey("hls/job", 3, 40, 1) }),
- "hls/job",
- chunk,
- ),
- ).not.toBeNull();
+ test("accepts a segment the reporting dispatch wrote", () => {
+ expect(checkSegmentReport(report(), "hls/job", chunk, 50)).not.toBeNull();
});
test.each([
["a key outside the job", { key: "out/other-job.mp4" }],
- ["a range not yet dispatched", { key: segmentKey("hls/job", 3, 60, 1) }],
- ["another chunk's key", { key: segmentKey("hls/job", 2, 40, 1) }],
- ["a key for another index", { key: segmentKey("hls/job", 3, 40, 0) }],
+ ["another copy's key", { key: segmentKey("hls/job", 3, 40, 1) }],
+ ["another chunk's key", { key: segmentKey("hls/job", 2, 50, 1) }],
+ ["a key for another index", { key: segmentKey("hls/job", 3, 50, 0) }],
["another chunk", { chunk: 2 }],
["frames outside the chunk", { frames: [60, 120] }],
["empty frames", { frames: [120, 120] }],
["a fractional index", { index: 1.5 }],
["a missing last flag", { last: undefined }],
])("rejects %s", (_, overrides) => {
- expect(checkSegmentReport(report(overrides), "hls/job", chunk)).toBeNull();
+ expect(
+ checkSegmentReport(report(overrides), "hls/job", chunk, 50),
+ ).toBeNull();
});
});
diff --git a/apps/render-farm/src/hls.ts b/apps/render-farm/src/hls.ts
index a553df465..958b38dc7 100644
--- a/apps/render-farm/src/hls.ts
+++ b/apps/render-farm/src/hls.ts
@@ -47,19 +47,15 @@ export function segmentKey(
}
/**
- * A worker's segment report, accepted only for objects its own dispatch of
- * this chunk wrote: the coordinator presigns report keys into the playlist.
+ * A worker's segment report, accepted only for an object the reporting
+ * dispatch itself wrote: the coordinator presigns report keys into the
+ * playlist, and another copy's key may not exist yet.
*/
export function checkSegmentReport(
report: unknown,
prefix: string,
- chunk: {
- index: number;
- frames: [number, number];
- firstPart: number;
- partLimit: number;
- dispatches: number;
- },
+ chunk: { index: number; frames: [number, number] },
+ firstPart: number,
): SegmentReport | null {
if (!report || typeof report !== "object") return null;
const { index, frames, key, last, extradata } = report as Record<
@@ -84,11 +80,7 @@ export function checkSegmentReport(
) {
return null;
}
- for (let range = 0; range < Math.max(chunk.dispatches, 1); range++) {
- const firstPart = chunk.firstPart + range * chunk.partLimit;
- if (key === segmentKey(prefix, chunk.index, firstPart, index)) {
- return report as SegmentReport;
- }
- }
- return null;
+ return key === segmentKey(prefix, chunk.index, firstPart, index)
+ ? (report as SegmentReport)
+ : null;
}