-
Notifications
You must be signed in to change notification settings - Fork 1
Balance pooled job distribution across runner children (first-fit → round-robin) #953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kaspernj
wants to merge
1
commit into
master
Choose a base branch
from
feat/round-robin-pooled-child-selection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // @ts-check | ||
|
|
||
| import BackgroundJobsWorker from "../../src/background-jobs/worker.js" | ||
| import {describe, expect, it} from "../../src/testing/test.js" | ||
|
|
||
| /** | ||
| * @param {string} id | ||
| * @returns {import("../../src/background-jobs/types.js").BackgroundJobPayload & {id: string}} | ||
| */ | ||
| function fakePayload(id) { | ||
| return /** @type {import("../../src/background-jobs/types.js").BackgroundJobPayload & {id: string}} */ (/** @type {unknown} */ ({id})) | ||
| } | ||
|
|
||
| /** | ||
| * Adds a pooled child to a worker without forking a real process, so pooled child | ||
| * selection can be exercised directly. | ||
| * @param {BackgroundJobsWorker} worker - Worker under test. | ||
| * @param {{inflight?: number, lastDispatchSeq?: number, retiring?: boolean}} [args] - Seed state. | ||
| * @returns {import("node:child_process").ChildProcess} - The fake child. | ||
| */ | ||
| function addFakePooledChild(worker, {inflight = 0, lastDispatchSeq = 0, retiring = false} = {}) { | ||
| const child = /** @type {import("node:child_process").ChildProcess} */ (/** @type {unknown} */ ({send() {}})) | ||
| /** @type {Map<string, {payload: import("../../src/background-jobs/types.js").BackgroundJobPayload & {id: string}}>} */ | ||
| const inflightMap = new Map() | ||
|
|
||
| for (let index = 0; index < inflight; index++) { | ||
| inflightMap.set(`seed-${worker.pooledChildren.size}-${index}`, {payload: fakePayload(`seed-${index}`)}) | ||
| } | ||
|
|
||
| worker.pooledChildren.add(child) | ||
| worker.pooledChildStates.set(child, {createdAtMs: 0, inflight: inflightMap, jobsRun: 0, lastDispatchSeq, retiring}) | ||
|
|
||
| return child | ||
| } | ||
|
|
||
| /** | ||
| * @param {BackgroundJobsWorker} worker - Worker under test. | ||
| * @param {import("node:child_process").ChildProcess} child - Pooled child. | ||
| * @returns {number} - Its in-flight job count. | ||
| */ | ||
| function inflightSize(worker, child) { | ||
| const state = worker.pooledChildStates.get(child) | ||
|
|
||
| if (!state) throw new Error("Missing pooled child state") | ||
|
|
||
| return state.inflight.size | ||
| } | ||
|
|
||
| describe("Background jobs - pooled distribution", () => { | ||
| it("spreads jobs evenly across children instead of first-fit packing the earliest", () => { | ||
| const worker = new BackgroundJobsWorker({pooledRunnerConcurrency: 4, pooledRunnerCount: 3}) | ||
| const first = addFakePooledChild(worker) | ||
| const second = addFakePooledChild(worker) | ||
| const third = addFakePooledChild(worker) | ||
|
|
||
| for (let index = 0; index < 6; index++) worker._runPooledJob(fakePayload(`job-${index}`)) | ||
|
|
||
| // Round-robin: two each. First-fit would have packed [4, 2, 0]. | ||
| expect([inflightSize(worker, first), inflightSize(worker, second), inflightSize(worker, third)]).toEqual([2, 2, 2]) | ||
| }) | ||
|
|
||
| it("does not burst a freshly added child to catch up to already-loaded children", () => { | ||
| const worker = new BackgroundJobsWorker({pooledRunnerConcurrency: 10, pooledRunnerCount: 3}) | ||
|
|
||
| worker._pooledDispatchSeq = 2 | ||
|
|
||
| const loadedA = addFakePooledChild(worker, {inflight: 2, lastDispatchSeq: 1}) | ||
| const loadedB = addFakePooledChild(worker, {inflight: 2, lastDispatchSeq: 2}) | ||
| const fresh = addFakePooledChild(worker, {inflight: 0, lastDispatchSeq: 0}) | ||
|
|
||
| for (let index = 0; index < 3; index++) worker._runPooledJob(fakePayload(`job-${index}`)) | ||
|
|
||
| // The fresh child takes one job as its turn comes up, then the rotation moves on to | ||
| // the others — not a burst of all three to "catch up" (which naive least-loaded, | ||
| // always picking the lowest in-flight, would do). | ||
| expect(inflightSize(worker, fresh)).toEqual(1) | ||
| expect(inflightSize(worker, loadedA)).toEqual(3) | ||
| expect(inflightSize(worker, loadedB)).toEqual(3) | ||
| }) | ||
|
|
||
| it("skips retiring children and lazily spawns only when every non-retiring child is full", () => { | ||
| const worker = new BackgroundJobsWorker({pooledRunnerConcurrency: 2, pooledRunnerCount: 4}) | ||
| const retiring = addFakePooledChild(worker, {retiring: true}) | ||
| const full = addFakePooledChild(worker, {inflight: 2}) | ||
|
|
||
| // No non-retiring child has a free slot, so selection returns undefined (the caller | ||
| // then spawns a new child). | ||
| expect(worker._selectPooledChild()).toEqual(undefined) | ||
| // The retiring child is never selected even though it has open slots. | ||
| expect(inflightSize(worker, retiring)).toEqual(0) | ||
| expect(inflightSize(worker, full)).toEqual(2) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a seeded pooled child state does not include the new
lastDispatchSeqfield,state.lastDispatchSeqisundefined, soundefined < Infinityis false and_selectPooledChild()returnsundefinedeven though the child has capacity. Existing worker specs create fakepooledChildStatesthis way and then call_runPooledJob()on a worker with no configuration, so this now falls through to_createPooledChild()and throws instead of using the fake child; please either default missing sequence values to0here or update all seeded child states.Useful? React with 👍 / 👎.