Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(render-farm): skip the reservation write on a chunk's first dispatch

Richie McIlroycb67839bda0d
diff --git a/apps/render-farm/src/coordinator.test.ts b/apps/render-farm/src/coordinator.test.ts
index 510a43317..fb45e22d7 100644
--- a/apps/render-farm/src/coordinator.test.ts
+++ b/apps/render-farm/src/coordinator.test.ts
@@ -251,25 +251,25 @@ function heartbeat(worker: string, taskId: string, attempt: number) {
}
describe("coordinator recovery", () => {
- test("dispatch waits for its reservation, resume preserves original and hedge ranges", async () => {
+ test("a later dispatch waits for its reservation, resume preserves original and hedge ranges", async () => {
const h = harness();
const j = job();
const original = videoState(j);
await h.journalJob(j);
+ await h.dispatchedTask(j, original);
+ const hedge = videoState(j, true);
const gate = Promise.withResolvers<void>();
h.gate(gate.promise);
let sent = false;
- const first = h.dispatchedTask(j, original).then((task) => {
+ const second = h.dispatchedTask(j, hedge).then((task) => {
sent = true;
return task;
});
await Promise.resolve();
expect(sent).toBe(false);
gate.resolve();
- await first;
+ await second;
h.gate();
- const hedge = videoState(j, true);
- await h.dispatchedTask(j, hedge);
await h.resumeJobs();
const resumed = h.jobs.get(j.id) as Job;
expect(resumed.chunks[0]?.dispatches).toBe(2);
@@ -280,6 +280,25 @@ describe("coordinator recovery", () => {
expect(next.kind === "video" && next.upload.firstPart).toBe(22);
});
+ test("a first dispatch writes nothing, stays retired after resume and any worker can re-attach it", async () => {
+ const h = harness();
+ const j = job();
+ const original = videoState(j);
+ await h.journalJob(j);
+ const writes = h.objects.size;
+ const first = await h.dispatchedTask(j, original);
+ expect(first.kind === "video" ? first.upload.firstPart : -1).toBe(
+ j.chunks[0]?.firstPart ?? 0,
+ );
+ expect(h.objects.size).toBe(writes);
+ await h.resumeJobs();
+ const resumed = h.jobs.get(j.id) as Job;
+ expect(resumed.chunks[0]?.dispatches).toBe(1);
+ await h.fetch(heartbeat("worker-c", original.task.taskId, 1));
+ expect(resumed.tasks.get(original.task.taskId)?.state).toBe("running");
+ expect(resumed.tasks.get(original.task.taskId)?.worker).toBe("worker-c");
+ });
+
test("a stale heartbeat cannot adopt a reserved newer attempt", async () => {
const h = harness();
const j = job();
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index ac9c0b698..1385704c1 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -1148,6 +1148,22 @@ async function resumeJobs() {
if (original) original.duplicated = true;
}
}
+ for (const chunk of job.chunks) {
+ chunk.dispatches = Math.max(chunk.dispatches, 1);
+ }
+ // A first dispatch has no reservation of its own: any worker still
+ // rendering it may re-attach it as attempt 1.
+ for (const state of job.tasks.values()) {
+ if (
+ state.task.kind === "video" &&
+ state.state === "queued" &&
+ !state.duplicateOf &&
+ !state.reattach
+ ) {
+ state.attempts = 1;
+ state.reattach = true;
+ }
+ }
if (job.hls) {
job.hls.audioExtradata = job.audioSections
.values()
@@ -1233,6 +1249,10 @@ async function dispatchedTask(
attempt: state.attempts,
upload: { ...task.upload, firstPart, partLimit: chunk.partLimit },
};
+ // 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.
+ if (range === 0) return dispatched;
await journalPut(
journalKey(job.id, `dispatches/${chunk.index}/${range}.json`),
JSON.stringify({
@@ -2064,7 +2084,7 @@ Bun.serve({
state.heldUntil &&
(state.task.kind === "audio" ||
(state.reattach &&
- state.worker === body.worker &&
+ (state.worker === undefined || state.worker === body.worker) &&
state.attempts === entry.attempt))
) {
// Still running from before a coordinator restart: adopt it.