Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(render-farm): hedge a chunk whose frames stop advancing and SIGKILL hung engines

Richie McIlroy5d7e5bd3d4e0
diff --git a/apps/render-farm/src/coordinator.test.ts b/apps/render-farm/src/coordinator.test.ts
index d31af9fd5..a3a2c02b0 100644
--- a/apps/render-farm/src/coordinator.test.ts
+++ b/apps/render-farm/src/coordinator.test.ts
@@ -93,12 +93,13 @@ function harness() {
const compiled = new Bun.Transpiler({ loader: "ts" }).transformSync(source);
const coordinator = new Function(
...Object.keys(deps),
- `${compiled}\nreturn {jobs, queue, dispatchedTask, onVideoDone, onAudioDone, publishPlaylist, journalJob, resumeJobs, newHlsState, finish, requeue, setPlanner: (fn) => { planJob = fn; }};`,
+ `${compiled}\nreturn {jobs, queue, straggler, dispatchedTask, onVideoDone, onAudioDone, publishPlaylist, journalJob, resumeJobs, newHlsState, finish, requeue, setPlanner: (fn) => { planJob = fn; }};`,
)(...Object.values(deps)) as {
setPlanner: (fn: (job: Job) => Promise<void>) => void;
requeue: (state: TaskState, reason: string) => void;
jobs: Map<string, Job>;
queue: TaskState[];
+ straggler: () => TaskState | undefined;
dispatchedTask: (job: Job, state: TaskState) => Promise<protocol.Task>;
onVideoDone: (
job: Job,
@@ -423,6 +424,27 @@ describe("coordinator recovery", () => {
});
});
+test("a copy whose frame count stopped part-way is hedged without waiting on its average rate", () => {
+ const h = harness();
+ const j = job();
+ h.jobs.set(j.id, j);
+ for (let index = 0; index < 3; index++) {
+ j.taskStats.push({ kind: "video", frames: 30, engineRenderMs: 1000 });
+ }
+ const original = videoState(j);
+ original.progress = {
+ frames: 27,
+ total: 30,
+ elapsedMs: 900,
+ at: 0,
+ advancedAt: Number.POSITIVE_INFINITY,
+ };
+ expect(h.straggler()).toBeUndefined();
+ original.progress.advancedAt = Number.NEGATIVE_INFINITY;
+ expect(h.straggler()?.duplicateOf).toBe(original.task.taskId);
+ expect(original.duplicated).toBe(true);
+});
+
test("segment reports only list objects the reporting dispatch wrote", async () => {
const h = harness();
const j = job();
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index 0f213caf2..241111b8e 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -238,7 +238,14 @@ type TaskState = {
heldUntil?: number;
/** Last heartbeat in which the owning worker listed this task. */
lastReportedAt?: number;
- progress?: { frames: number; total: number; elapsedMs: number; at: number };
+ progress?: {
+ frames: number;
+ total: number;
+ elapsedMs: number;
+ at: number;
+ /** When `frames` last grew. */
+ advancedAt: number;
+ };
};
type HlsState = {
@@ -1379,6 +1386,8 @@ function dispatch() {
* whose projected finish is furthest behind what a fresh slot could do. Slow
* replicas (noisy neighbours) otherwise set the whole export's tail.
*/
+const FROZEN_MS = 5_000;
+
function straggler(): TaskState | undefined {
let best: { state: TaskState; gain: number } | undefined;
for (const job of jobs.values()) {
@@ -1416,10 +1425,19 @@ function straggler(): TaskState | undefined {
state.progress?.elapsedMs ?? now() - (state.startedAt ?? now());
const frames = state.progress?.frames ?? 0;
const expected = total / typicalRate + 2500;
+ // Engines report frames several times a second. A copy part-way
+ // through whose count stopped is hung: projecting from its average
+ // rate hedged one frozen near its end only when the engine watchdog
+ // fired, 30 s later.
+ const frozen =
+ frames > 0 &&
+ frames < total &&
+ now() - (state.progress?.advancedAt ?? now()) > FROZEN_MS;
// A copy with no frames long past its expected time is stuck (not
// merely slow): its remaining time is unknown, so always hedge it.
- const remaining =
- frames > 0
+ const remaining = frozen
+ ? Number.POSITIVE_INFINITY
+ : frames > 0
? ((total - frames) * (state.progress?.elapsedMs ?? elapsed)) / frames
: elapsed > expected * 2
? Number.POSITIVE_INFINITY
@@ -2142,9 +2160,14 @@ Bun.serve({
state.lastReportedAt = Date.now();
if (entry.phase === "reserved") continue;
if (state.task.kind === "video") {
- if (entry.frames > (state.progress?.frames ?? 0) && job)
- job.t.lastProgress = now();
- state.progress = { ...entry, at: now() };
+ const advanced = entry.frames > (state.progress?.frames ?? 0);
+ if (advanced && job) job.t.lastProgress = now();
+ state.progress = {
+ ...entry,
+ at: now(),
+ advancedAt:
+ advanced || !state.progress ? now() : state.progress.advancedAt,
+ };
}
}
const worker = workers.get(body.worker);
diff --git a/apps/render-farm/src/worker.ts b/apps/render-farm/src/worker.ts
index 85b759b31..94ea723a9 100644
--- a/apps/render-farm/src/worker.ts
+++ b/apps/render-farm/src/worker.ts
@@ -859,7 +859,9 @@ function cancel(taskIds: string[]) {
for (const [slot, task] of busy) {
if (!taskIds.includes(task.taskId)) continue;
console.log(`cancelling ${task.taskId}`);
- engines[slot]?.kill();
+ // SIGKILL: a stopped or wedged engine never acts on SIGTERM and would
+ // hold its slot until the watchdog gave up on it too.
+ engines[slot]?.kill("SIGKILL");
}
}
@@ -872,7 +874,7 @@ setInterval(() => {
`${entry.taskId}: no progress for ${now - entry.lastProgressAt} ms, killing slot${slot}`,
);
entry.lastProgressAt = now;
- engines[slot]?.kill();
+ engines[slot]?.kill("SIGKILL");
}
}, 5_000);