Skip to content

Commit b1d5e78

Browse files
HermanZengjalehman
andauthored
fix(queue): arbitrate shared capacity across grouped lanes (#122764)
* fix(queue): arbitrate shared capacity across lanes * fix(queue): preserve scoped lane completion ownership * test(gateway): prove cross-lane capacity fairness * fix(queue): preserve group helper API closure * fix(queue): remove unsafe drain callback cast --------- Co-authored-by: Josh Lehman <[email protected]>
1 parent 6b72d65 commit b1d5e78

6 files changed

Lines changed: 703 additions & 111 deletions

‎src/gateway/server-lanes.hook-group.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,59 @@ describe("cron+hook capacity group", () => {
153153
await Promise.all(runs);
154154
});
155155

156+
it("does not let a sustained hook burst recapture capacity ahead of older cron work", async () => {
157+
publish(HOOKS_ON);
158+
159+
const activeHookGates = Array.from({ length: DEFAULT_CRON_MAX_CONCURRENT_RUNS }, () => gate());
160+
const activeHooks = activeHookGates.map((g) =>
161+
enqueueCommandInLane(CommandLane.HookDispatch, async () => await g.promise, {
162+
priority: "background",
163+
warnAfterMs: 10_000,
164+
}),
165+
);
166+
await settle();
167+
expect(getCommandLaneSnapshot(CommandLane.HookDispatch).activeCount).toBe(
168+
DEFAULT_CRON_MAX_CONCURRENT_RUNS,
169+
);
170+
171+
const cronGate = gate();
172+
const cronRun = enqueueCommandInLane(
173+
CommandLane.CronNested,
174+
async () => await cronGate.promise,
175+
{ priority: "background", warnAfterMs: 10_000 },
176+
);
177+
const lateHookGate = gate();
178+
const lateHook = enqueueCommandInLane(
179+
CommandLane.HookDispatch,
180+
async () => await lateHookGate.promise,
181+
{ priority: "background", warnAfterMs: 10_000 },
182+
);
183+
await settle();
184+
expect(getCommandLaneSnapshot(CommandLane.CronNested).queuedCount).toBe(1);
185+
expect(getCommandLaneSnapshot(CommandLane.HookDispatch).queuedCount).toBe(1);
186+
187+
activeHookGates[0]?.release();
188+
await activeHooks[0];
189+
await settle();
190+
191+
expect(getCommandLaneSnapshot(CommandLane.CronNested)).toMatchObject({
192+
activeCount: 1,
193+
queuedCount: 0,
194+
groupActive: DEFAULT_CRON_MAX_CONCURRENT_RUNS,
195+
});
196+
expect(getCommandLaneSnapshot(CommandLane.HookDispatch)).toMatchObject({
197+
activeCount: DEFAULT_CRON_MAX_CONCURRENT_RUNS - 1,
198+
queuedCount: 1,
199+
});
200+
201+
cronGate.release();
202+
for (const g of activeHookGates.slice(1)) {
203+
g.release();
204+
}
205+
lateHookGate.release();
206+
await Promise.all([cronRun, ...activeHooks.slice(1), lateHook]);
207+
});
208+
156209
it("admits seven cron plus one hook, then gives freed capacity to a second hook", async () => {
157210
publish(HOOKS_ON);
158211

‎src/process/command-queue.capacity-groups.test.ts‎

Lines changed: 264 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919

2020
const CRON = "cron-nested";
2121
const HOOK = "hook-dispatch";
22+
const DELIVERY = "delivery-dispatch";
2223
const GROUP = "cron-hooks";
2324

2425
type LaneGroupSpec = NonNullable<Parameters<typeof publishLaneConfiguration>[0]["groups"]>[string];
@@ -53,6 +54,7 @@ beforeEach(() => {
5354
clearCommandLaneGroup(GROUP);
5455
setCommandLaneConcurrency(CRON, 8);
5556
setCommandLaneConcurrency(HOOK, 8);
57+
setCommandLaneConcurrency(DELIVERY, 8);
5658
});
5759

5860
afterEach(() => {
@@ -70,20 +72,26 @@ describe("command lane capacity groups", () => {
7072

7173
// Fill the group to its budget minus the hook's reservation.
7274
const gates = Array.from({ length: 7 }, () => gate());
73-
const cronRuns = gates.map((g) => enqueueCommandInLane(CRON, async () => await g.promise));
75+
const cronRuns = gates.map((g) =>
76+
enqueueCommandInLane(CRON, async () => await g.promise, { priority: "foreground" }),
77+
);
7478
await settle();
7579
expect(getCommandLaneSnapshot(CRON).activeCount).toBe(7);
7680

7781
// The 8th slot is the hook's hard reservation: cron must not take it.
7882
const extra = gate();
79-
const blockedCron = enqueueCommandInLane(CRON, async () => await extra.promise);
83+
const blockedCron = enqueueCommandInLane(CRON, async () => await extra.promise, {
84+
priority: "foreground",
85+
});
8086
await settle();
8187
expect(getCommandLaneSnapshot(CRON).activeCount).toBe(7);
8288
expect(getCommandLaneSnapshot(CRON).blockedBy).toBe("sibling-reservation");
8389

8490
// And the hook starts immediately despite the group being otherwise full.
8591
const hookGate = gate();
86-
const hookRun = enqueueCommandInLane(HOOK, async () => await hookGate.promise);
92+
const hookRun = enqueueCommandInLane(HOOK, async () => await hookGate.promise, {
93+
priority: "background",
94+
});
8795
await settle();
8896
expect(getCommandLaneSnapshot(HOOK).activeCount).toBe(1);
8997
expect(getCommandLaneSnapshot(HOOK).groupActive).toBe(8);
@@ -178,6 +186,259 @@ describe("command lane capacity groups", () => {
178186
await Promise.all([second, hookRun]);
179187
});
180188

189+
test.each(["successful", "failing"] as const)(
190+
"a %s completion gives shared capacity to the older eligible sibling head",
191+
async (outcome) => {
192+
setCommandLaneGroup(GROUP, {
193+
budget: 2,
194+
members: [CRON, HOOK],
195+
reservations: { [HOOK]: 1 },
196+
});
197+
198+
const firstHookGate = gate();
199+
const secondHookGate = gate();
200+
const firstHook = enqueueCommandInLane(
201+
HOOK,
202+
async () => {
203+
await firstHookGate.promise;
204+
if (outcome === "failing") {
205+
throw new Error("expected hook failure");
206+
}
207+
},
208+
{ priority: "background" },
209+
);
210+
const secondHook = enqueueCommandInLane(HOOK, async () => await secondHookGate.promise, {
211+
priority: "background",
212+
});
213+
await settle();
214+
expect(getCommandLaneSnapshot(HOOK).activeCount).toBe(2);
215+
216+
// Cron queues first. A later hook queues behind the same full group. The
217+
// completing hook lane must not synchronously reclaim the shared slot.
218+
const cronGate = gate();
219+
const cronRun = enqueueCommandInLane(CRON, async () => await cronGate.promise, {
220+
priority: "background",
221+
});
222+
const thirdHookGate = gate();
223+
const thirdHook = enqueueCommandInLane(HOOK, async () => await thirdHookGate.promise, {
224+
priority: "background",
225+
});
226+
await settle();
227+
228+
firstHookGate.release();
229+
if (outcome === "failing") {
230+
await expect(firstHook).rejects.toThrow("expected hook failure");
231+
} else {
232+
await firstHook;
233+
}
234+
await settle();
235+
236+
expect(getCommandLaneSnapshot(CRON)).toMatchObject({ activeCount: 1, queuedCount: 0 });
237+
expect(getCommandLaneSnapshot(HOOK)).toMatchObject({ activeCount: 1, queuedCount: 1 });
238+
239+
cronGate.release();
240+
secondHookGate.release();
241+
thirdHookGate.release();
242+
await Promise.all([cronRun, secondHook, thirdHook]);
243+
},
244+
);
245+
246+
test("priority outranks group-global enqueue sequence", async () => {
247+
setCommandLaneGroup(GROUP, { budget: 1, members: [CRON, HOOK] });
248+
249+
const blockerGate = gate();
250+
const blocker = enqueueCommandInLane(HOOK, async () => await blockerGate.promise);
251+
await settle();
252+
253+
const cronGate = gate();
254+
const olderBackground = enqueueCommandInLane(CRON, async () => await cronGate.promise, {
255+
priority: "background",
256+
});
257+
const hookGate = gate();
258+
const newerForeground = enqueueCommandInLane(HOOK, async () => await hookGate.promise, {
259+
priority: "foreground",
260+
});
261+
262+
blockerGate.release();
263+
await blocker;
264+
await settle();
265+
266+
expect(getCommandLaneSnapshot(HOOK)).toMatchObject({ activeCount: 1, queuedCount: 0 });
267+
expect(getCommandLaneSnapshot(CRON)).toMatchObject({ activeCount: 0, queuedCount: 1 });
268+
269+
hookGate.release();
270+
await newerForeground;
271+
cronGate.release();
272+
await olderBackground;
273+
});
274+
275+
test("three-member arbitration is independent of member iteration order", async () => {
276+
setCommandLaneGroup(GROUP, { budget: 1, members: [CRON, HOOK, DELIVERY] });
277+
278+
const blockerGate = gate();
279+
const blocker = enqueueCommandInLane(CRON, async () => await blockerGate.promise, {
280+
priority: "background",
281+
});
282+
await settle();
283+
284+
// DELIVERY is last in the member Set but queues before HOOK. A simple
285+
// sibling-first loop would start HOOK merely because it is visited first.
286+
const deliveryGate = gate();
287+
const olderDelivery = enqueueCommandInLane(DELIVERY, async () => await deliveryGate.promise, {
288+
priority: "background",
289+
});
290+
const hookGate = gate();
291+
const newerHook = enqueueCommandInLane(HOOK, async () => await hookGate.promise, {
292+
priority: "background",
293+
});
294+
295+
blockerGate.release();
296+
await blocker;
297+
await settle();
298+
299+
expect(getCommandLaneSnapshot(DELIVERY)).toMatchObject({ activeCount: 1, queuedCount: 0 });
300+
expect(getCommandLaneSnapshot(HOOK)).toMatchObject({ activeCount: 0, queuedCount: 1 });
301+
302+
deliveryGate.release();
303+
await olderDelivery;
304+
hookGate.release();
305+
await newerHook;
306+
});
307+
308+
test("multi-slot reset re-arbitrates before stale completions arrive", async () => {
309+
setCommandLaneGroup(GROUP, { budget: 2, members: [CRON, HOOK] });
310+
311+
const staleGates = [gate(), gate()];
312+
const staleHooks = staleGates.map((g) =>
313+
enqueueCommandInLane(HOOK, async () => await g.promise, { priority: "background" }),
314+
);
315+
await settle();
316+
317+
const cronGate = gate();
318+
const cronRun = enqueueCommandInLane(CRON, async () => await cronGate.promise, {
319+
priority: "background",
320+
});
321+
const queuedHookGates = [gate(), gate()];
322+
const queuedHooks = queuedHookGates.map((g) =>
323+
enqueueCommandInLane(HOOK, async () => await g.promise, { priority: "background" }),
324+
);
325+
await settle();
326+
327+
expect(resetCommandLane(HOOK)).toBe(2);
328+
await settle();
329+
expect(getCommandLaneSnapshot(CRON)).toMatchObject({ activeCount: 1, queuedCount: 0 });
330+
expect(getCommandLaneSnapshot(HOOK)).toMatchObject({ activeCount: 1, queuedCount: 1 });
331+
expect(getCommandLaneSnapshot(HOOK).groupActive).toBe(2);
332+
333+
// The reset invalidated these task IDs. Their late completions must neither
334+
// remove new-generation IDs nor admit the remaining queued hook.
335+
for (const g of staleGates) {
336+
g.release();
337+
}
338+
await Promise.all(staleHooks);
339+
await settle();
340+
expect(getCommandLaneSnapshot(CRON).activeCount).toBe(1);
341+
expect(getCommandLaneSnapshot(HOOK)).toMatchObject({ activeCount: 1, queuedCount: 1 });
342+
343+
cronGate.release();
344+
queuedHookGates[0]?.release();
345+
await Promise.all([cronRun, queuedHooks[0]]);
346+
queuedHookGates[1]?.release();
347+
await queuedHooks[1];
348+
});
349+
350+
test("resetAllLanes refills a group by queue order rather than lane order", async () => {
351+
setCommandLaneGroup(GROUP, { budget: 1, members: [HOOK, CRON] });
352+
353+
const staleGate = gate();
354+
const staleHook = enqueueCommandInLane(HOOK, async () => await staleGate.promise, {
355+
priority: "background",
356+
});
357+
await settle();
358+
359+
const cronGate = gate();
360+
const olderCron = enqueueCommandInLane(CRON, async () => await cronGate.promise, {
361+
priority: "background",
362+
});
363+
const hookGate = gate();
364+
const newerHook = enqueueCommandInLane(HOOK, async () => await hookGate.promise, {
365+
priority: "background",
366+
});
367+
await settle();
368+
369+
resetAllLanes();
370+
await settle();
371+
expect(getCommandLaneSnapshot(CRON)).toMatchObject({ activeCount: 1, queuedCount: 0 });
372+
expect(getCommandLaneSnapshot(HOOK)).toMatchObject({ activeCount: 0, queuedCount: 1 });
373+
374+
staleGate.release();
375+
await staleHook;
376+
await settle();
377+
expect(getCommandLaneSnapshot(CRON).activeCount).toBe(1);
378+
expect(getCommandLaneSnapshot(HOOK).queuedCount).toBe(1);
379+
380+
cronGate.release();
381+
await olderCron;
382+
hookGate.release();
383+
await newerHook;
384+
});
385+
386+
test("commits a slot before an onWait callback can re-enter the group", async () => {
387+
setCommandLaneConcurrency(CRON, 0);
388+
setCommandLaneConcurrency(HOOK, 1);
389+
390+
const cronGate = gate();
391+
const hookGate = gate();
392+
let hookRun: Promise<void> | undefined;
393+
let active = 0;
394+
let peak = 0;
395+
const cronRun = enqueueCommandInLane(
396+
CRON,
397+
async () => {
398+
active += 1;
399+
peak = Math.max(peak, active);
400+
await cronGate.promise;
401+
active -= 1;
402+
},
403+
{
404+
priority: "background",
405+
warnAfterMs: 0,
406+
onWait: () => {
407+
hookRun = enqueueCommandInLane(
408+
HOOK,
409+
async () => {
410+
active += 1;
411+
peak = Math.max(peak, active);
412+
await hookGate.promise;
413+
active -= 1;
414+
},
415+
{ priority: "foreground" },
416+
);
417+
},
418+
},
419+
);
420+
await settle();
421+
422+
publishLaneConfiguration({
423+
lanes: { [CRON]: 1 },
424+
groups: { [GROUP]: { budget: 1, members: [CRON, HOOK] } },
425+
});
426+
await settle();
427+
428+
expect(hookRun).toBeDefined();
429+
expect(peak).toBe(1);
430+
expect(getCommandLaneSnapshot(CRON).activeCount).toBe(1);
431+
expect(getCommandLaneSnapshot(HOOK)).toMatchObject({ activeCount: 0, queuedCount: 1 });
432+
433+
cronGate.release();
434+
await cronRun;
435+
await settle();
436+
expect(getCommandLaneSnapshot(HOOK).activeCount).toBe(1);
437+
hookGate.release();
438+
await hookRun;
439+
expect(peak).toBe(1);
440+
});
441+
181442
test("a failing task releases group capacity like a successful one", async () => {
182443
setCommandLaneGroup(GROUP, { budget: 1, members: [CRON, HOOK] });
183444

0 commit comments

Comments
 (0)