Skip to content

Commit 53414b1

Browse files
fix(github): Do not split a failed patch query
A failed patch query split in halves down to one pull request each, inside one heartbeat and with no time limit. During a GitHub outage that is a tree of queries before the daily reconcile runs. A failed patch query now waits for the next heartbeat as a whole, so an outage costs one query per chunk of 25. Only the page card read still splits, inside its 20 s budget, so `loadInHalves` moves next to it. Co-Authored-By: BYK <[email protected]>
1 parent 1d5666a commit 53414b1

4 files changed

Lines changed: 78 additions & 86 deletions

File tree

‎packages/junior-github/src/pull-request-graphql.ts‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,6 @@ export function aliasedPullRequests<T>(
8484
});
8585
}
8686

87-
/**
88-
* Run `load` on `items`. A failed run splits in halves, and the halves are
89-
* its retry, while `canSplit` allows it. A run that cannot split goes to
90-
* `failed`.
91-
*/
92-
export async function loadInHalves<T>(args: {
93-
canSplit?(): boolean;
94-
failed(items: T[], error: unknown): Promise<void>;
95-
items: T[];
96-
load(items: T[]): Promise<void>;
97-
}): Promise<void> {
98-
try {
99-
await args.load(args.items);
100-
} catch (error) {
101-
if (args.items.length === 1 || args.canSplit?.() === false) {
102-
await args.failed(args.items, error);
103-
return;
104-
}
105-
const half = Math.ceil(args.items.length / 2);
106-
await Promise.all(
107-
[args.items.slice(0, half), args.items.slice(half)].map((items) =>
108-
loadInHalves({ ...args, items }),
109-
),
110-
);
111-
}
112-
}
113-
11487
const stateCount = z.object({
11588
count: z.number().int().nonnegative(),
11689
state: z.string(),

‎packages/junior-github/src/pull-request-patches.ts‎

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
aliasedPullRequests,
3030
githubCheckCounts,
3131
githubMergeable,
32-
loadInHalves,
3332
pullRequestChunks,
3433
pullRequestHeadFactsNodeSchema,
3534
type PullRequestRef,
@@ -154,12 +153,13 @@ async function expirePending(cache: PluginCache, key: string): Promise<void> {
154153

155154
/**
156155
* Run the due patches: one query reads the merge state and checks of up
157-
* to 25 pull requests, and a failed query splits in halves. A patch whose
158-
* merge state GitHub still computes, whose read is older than its entry,
159-
* or whose query fails alone, waits for the next heartbeat. A patch with
160-
* no pull request in the answer, or out of attempts, is dropped, and its
161-
* entry is read again soon. A failed query is logged, not thrown, so the
162-
* rest of the heartbeat still runs.
156+
* to 25 pull requests. A patch whose merge state GitHub still computes,
157+
* whose read is older than its entry, or whose query fails, waits for the
158+
* next heartbeat. A failed query does not split, so an outage costs one
159+
* query per chunk. A patch with no pull request in the answer, or out of
160+
* attempts, is dropped, and its entry is read again
161+
* soon. A failed query is logged, not thrown, so the rest of the heartbeat
162+
* still runs.
163163
*/
164164
export async function runGitHubPullRequestPatches(args: {
165165
cache: PluginCache;
@@ -184,49 +184,41 @@ export async function runGitHubPullRequestPatches(args: {
184184
if (!ref) dropped.push(key);
185185
return ref ? [{ due: [key, patch] satisfies Due, ref }] : [];
186186
});
187-
// The queries of every chunk and of its halves share one limit.
188-
const queries = pLimit(PATCH_QUERY_CONCURRENCY);
189187
await pLimit(PATCH_QUERY_CONCURRENCY).map(
190188
pullRequestChunks(reads),
191-
async (chunk) =>
192-
await loadInHalves({
193-
failed: async ([read], error) => {
194-
if (!read) return;
195-
again.push(read.due);
196-
args.log.error(
197-
"GitHub pull request patch query failed",
198-
{
199-
number: read.ref.number,
200-
repository: `${read.ref.owner}/${read.ref.name}`,
201-
},
202-
error,
203-
);
204-
},
205-
items: chunk,
206-
load: async (part) => {
207-
// Answer `p<i>` is the pull request of `part[i]`.
208-
const nodes = aliasedPullRequests(
209-
await queries(() =>
210-
args.loadHeadFacts(part.map((read) => read.ref)),
211-
),
212-
part.length,
213-
pullRequestHeadFactsNodeSchema,
214-
);
215-
for (const [index, { due: entry }] of part.entries()) {
216-
const [key, patch] = entry;
217-
const node = nodes[index];
218-
if (!node) {
219-
dropped.push(key);
220-
continue;
221-
}
222-
const last = patch.attempts + 1 >= PATCH_MAX_ATTEMPTS;
223-
// oxlint-disable-next-line no-await-in-loop -- Each write takes its own entry lock; patches are few.
224-
if (await writeHeadFacts(args.cache, key, node, last)) {
225-
again.push(entry);
226-
}
227-
}
228-
},
229-
}),
189+
async (chunk) => {
190+
let response: unknown;
191+
try {
192+
response = await args.loadHeadFacts(chunk.map((read) => read.ref));
193+
} catch (error) {
194+
again.push(...chunk.map((read) => read.due));
195+
args.log.error(
196+
"GitHub pull request patch query failed",
197+
{ pullRequests: chunk.length },
198+
error,
199+
);
200+
return;
201+
}
202+
// Answer `p<i>` is the pull request of `chunk[i]`.
203+
const nodes = aliasedPullRequests(
204+
response,
205+
chunk.length,
206+
pullRequestHeadFactsNodeSchema,
207+
);
208+
for (const [index, { due: entry }] of chunk.entries()) {
209+
const [key, patch] = entry;
210+
const node = nodes[index];
211+
if (!node) {
212+
dropped.push(key);
213+
continue;
214+
}
215+
const last = patch.attempts + 1 >= PATCH_MAX_ATTEMPTS;
216+
// oxlint-disable-next-line no-await-in-loop -- Each write takes its own entry lock; patches are few.
217+
if (await writeHeadFacts(args.cache, key, node, last)) {
218+
again.push(entry);
219+
}
220+
}
221+
},
230222
);
231223
const retries = again.filter(
232224
([, patch]) => patch.attempts + 1 < PATCH_MAX_ATTEMPTS,

‎packages/junior-github/src/pull-request-prefetch.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import {
1919
aliasedPullRequests,
2020
githubPullRequestFieldsFromNode,
21-
loadInHalves,
2221
pullRequestCardNodeSchema,
2322
pullRequestChunks,
2423
type PullRequestRef,
@@ -101,6 +100,31 @@ async function loadChunk(
101100
});
102101
}
103102

103+
// Run `load` on `items`. A failed run splits in halves, and the halves are
104+
// its retry, while `canSplit` allows it. A run that cannot split goes to
105+
// `failed`.
106+
async function loadInHalves<T>(args: {
107+
canSplit(): boolean;
108+
failed(items: T[], error: unknown): Promise<void>;
109+
items: T[];
110+
load(items: T[]): Promise<void>;
111+
}): Promise<void> {
112+
try {
113+
await args.load(args.items);
114+
} catch (error) {
115+
if (args.items.length === 1 || !args.canSplit()) {
116+
await args.failed(args.items, error);
117+
return;
118+
}
119+
const half = Math.ceil(args.items.length / 2);
120+
await Promise.all(
121+
[args.items.slice(0, half), args.items.slice(half)].map((items) =>
122+
loadInHalves({ ...args, items }),
123+
),
124+
);
125+
}
126+
}
127+
104128
// A chunk that fails splits in halves, and the halves are its retry. A pull
105129
// request that fails alone is skipped: its card reads on its own, and keeps
106130
// its stale entry when that read fails too. A chunk that fails after the

‎packages/junior/tests/integration/api/github-object-refresh.test.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -596,16 +596,18 @@ describe("GitHub pull request cache refresh", () => {
596596
vi.useFakeTimers({ toFake: ["Date"] });
597597
const start = Date.parse(T0);
598598
vi.setSystemTime(start);
599-
await sendGitHubWebhook(app, "pull_request", {
600-
action: "opened",
601-
pull_request: pullRequest(34, {
602-
body: "Cache GitHub objects.\n\n<!-- junior-session-footer:start -->\nJunior\n<!-- junior-session-footer:end -->",
603-
user: { login: "junior-test[bot]" },
604-
}),
605-
});
599+
for (const number of [34, 35]) {
600+
await sendGitHubWebhook(app, "pull_request", {
601+
action: "opened",
602+
pull_request: pullRequest(number, {
603+
body: "Cache GitHub objects.\n\n<!-- junior-session-footer:start -->\nJunior\n<!-- junior-session-footer:end -->",
604+
user: { login: "junior-test[bot]" },
605+
}),
606+
});
607+
}
606608

607-
// A day later, the open pull request is due for its reconcile, and a
608-
// finished check suite adds a patch.
609+
// A day later, the open pull requests are due for their reconcile, and
610+
// a finished check suite adds a patch for each.
609611
vi.setSystemTime(start + 24.5 * 60 * 60_000);
610612
await sendGitHubWebhook(app, "check_suite", {
611613
action: "completed",
@@ -614,12 +616,13 @@ describe("GitHub pull request cache refresh", () => {
614616
head_branch: "feat/object-cache",
615617
head_sha: "aaa111",
616618
id: 7002,
617-
pull_requests: [{ number: 34 }],
619+
pull_requests: [{ number: 34 }, { number: 35 }],
618620
status: "completed",
619621
},
620622
});
621623
vi.setSystemTime(Date.now() + 60_000);
622624
await runTestHeartbeat(queue, Date.now());
625+
// The failed patch query does not split. Its patches wait.
623626
expect(operations).toEqual([
624627
"PullRequestHeadFacts",
625628
"PullRequestLifecycles",

0 commit comments

Comments
 (0)