Skip to content

Commit 382bf4d

Browse files
saphidcodexjuliusmarminge
authored
fix(server): isolate pull request sync reads by project (#18854)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent ddbc524 commit 382bf4d

2 files changed

Lines changed: 118 additions & 18 deletions

File tree

‎apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts‎

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ describe("PullRequestSyncReactor", () => {
499499
),
500500
);
501501

502-
it.effect("asks the host once for a pull request shared by two threads", () =>
502+
it.effect("asks the host once for a pull request shared by two threads in one project", () =>
503503
Effect.scoped(
504504
Effect.gen(function* () {
505505
yield* TestClock.setTime(Date.parse(NOW));
@@ -516,6 +516,7 @@ describe("PullRequestSyncReactor", () => {
516516
yield* startAndSweep(fixture);
517517

518518
assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
519+
assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 1);
519520
const commands = yield* Ref.get(fixture.syncCommands);
520521
assert.deepStrictEqual(
521522
commands
@@ -531,6 +532,86 @@ describe("PullRequestSyncReactor", () => {
531532
),
532533
);
533534

535+
it.effect.each(["summary", "stack"] as const)(
536+
"isolates a failed project %s refresh and retries it after the other project succeeds",
537+
(operation) =>
538+
Effect.scoped(
539+
Effect.gen(function* () {
540+
yield* TestClock.setTime(Date.parse(NOW));
541+
const otherProject = ProjectId.make("other-project");
542+
const link = makeLink(42, { state: "merged", mergedAt: NOW });
543+
let failing = true;
544+
const failure = new PullRequestOperationError({ operation, detail: "checkout missing" });
545+
const fixture = yield* makeHarness({
546+
snapshot: makeSnapshot([
547+
makeThread("failing", { pullRequests: [link] }),
548+
makeThread("healthy", { projectId: otherProject, pullRequests: [link] }),
549+
makeThread("same-project", { projectId: otherProject, pullRequests: [link] }),
550+
]),
551+
summary: (input) =>
552+
failing && input.projectId === PROJECT_ID && operation === "summary"
553+
? Effect.fail(failure)
554+
: Effect.succeed(
555+
makeSummary(input, {
556+
state: "merged",
557+
mergedAt: NOW,
558+
title: input.projectId,
559+
}),
560+
),
561+
stack: (input) =>
562+
failing && input.projectId === PROJECT_ID && operation === "stack"
563+
? Effect.fail(failure)
564+
: Effect.succeed({
565+
id: input.projectId,
566+
number: 42,
567+
url: "https://github.com/owner/repository/stacks/42",
568+
base: "main",
569+
layers: [{ number: 42, headBranch: "feature", state: "merged" }],
570+
}),
571+
});
572+
yield* Effect.gen(function* () {
573+
const reactor = yield* startAndSweep(fixture);
574+
assert.deepStrictEqual(yield* Ref.get(fixture.summaryCalls), []);
575+
yield* reactor.requestSync(link);
576+
yield* Queue.take(fixture.snapshotReads);
577+
yield* reactor.drain;
578+
const commands = yield* Ref.get(fixture.syncCommands);
579+
assert.sameDeepMembers(
580+
commands.map((command) => [
581+
command.threadId,
582+
command.snapshot.title,
583+
command.stack?.id,
584+
]),
585+
[
586+
["healthy", otherProject, otherProject],
587+
["same-project", otherProject, otherProject],
588+
],
589+
);
590+
assert.sameMembers(
591+
(yield* Ref.get(fixture.summaryCalls)).map((call) => call.projectId),
592+
[PROJECT_ID, otherProject],
593+
);
594+
assert.sameMembers(
595+
(yield* Ref.get(fixture.stackCalls)).map((call) => call.projectId),
596+
operation === "stack" ? [PROJECT_ID, otherProject] : [otherProject],
597+
);
598+
yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands));
599+
failing = false;
600+
yield* sweepAgain(fixture, reactor);
601+
assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 3);
602+
assert.strictEqual(
603+
(yield* Ref.get(fixture.summaryCalls)).at(-1)?.projectId,
604+
PROJECT_ID,
605+
);
606+
const retried = (yield* Ref.get(fixture.syncCommands)).at(-1);
607+
assert.strictEqual(retried?.threadId, ThreadId.make("failing"));
608+
assert.strictEqual(retried?.snapshot.title, PROJECT_ID);
609+
assert.strictEqual(retried?.stack?.id, PROJECT_ID);
610+
}).pipe(Effect.provide(fixture.layer));
611+
}),
612+
),
613+
);
614+
534615
it.effect("recovers the HTTP port when syncing an older Forgejo link", () =>
535616
Effect.scoped(
536617
Effect.gen(function* () {
@@ -580,11 +661,11 @@ describe("PullRequestSyncReactor", () => {
580661
),
581662
);
582663

583-
it.effect("refreshes closed links through the reactor's project after reopening elsewhere", () =>
664+
it.effect("refreshes closed links through each project after reopening elsewhere", () =>
584665
Effect.scoped(
585666
Effect.gen(function* () {
586667
yield* TestClock.setTime(Date.parse(NOW));
587-
const stale = yield* Ref.make(true);
668+
const invalidated = yield* Ref.make<ReadonlyArray<ProjectId>>([]);
588669
const fixture = yield* makeHarness({
589670
snapshot: makeSnapshot([
590671
makeThread("first", { pullRequests: [makeLink(42, { state: "closed" })] }),
@@ -594,24 +675,36 @@ describe("PullRequestSyncReactor", () => {
594675
}),
595676
]),
596677
invalidate: ({ reference }) =>
597-
reference?.projectId === makeProject().id && reference.host === "github.com"
598-
? Ref.set(stale, false)
678+
reference !== undefined
679+
? Ref.update(invalidated, (projects) => [...projects, reference.projectId])
599680
: Effect.void,
600681
summary: (input) =>
601-
Ref.get(stale).pipe(
602-
Effect.map((cached) => makeSummary(input, { state: cached ? "closed" : "open" })),
682+
Ref.get(invalidated).pipe(
683+
Effect.map((projects) =>
684+
makeSummary(input, {
685+
state: projects.includes(input.projectId) ? "open" : "closed",
686+
}),
687+
),
603688
),
604689
});
605690
yield* Effect.gen(function* () {
606691
const reactor = yield* startAndSweep(fixture);
607-
assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
692+
assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2);
608693
yield* reactor.requestSync({
609694
host: "github.com",
610695
repository: "owner/repository",
611696
number: 42,
612697
});
613698
yield* Queue.take(fixture.snapshotReads);
614699
yield* reactor.drain;
700+
assert.sameMembers(
701+
[...(yield* Ref.get(invalidated))],
702+
[PROJECT_ID, ProjectId.make("second-project")],
703+
);
704+
assert.sameMembers(
705+
(yield* Ref.get(fixture.stackCalls)).map((call) => call.projectId),
706+
[PROJECT_ID, ProjectId.make("second-project")],
707+
);
615708
const commands = yield* Ref.get(fixture.syncCommands);
616709
yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands));
617710
const snapshot = yield* Ref.get(fixture.snapshots);

‎apps/server/src/orchestration-v2/PullRequestSyncReactor.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ export const make = Effect.gen(function* () {
152152
const crypto = yield* Crypto.Crypto;
153153

154154
const lastSyncedAt = new Map<string, number>();
155-
const requested = new Map<string, number>();
156-
let requestGeneration = 0;
157-
// Requested keys wait in `requested` for one queued sweep, so a burst of links (an agent
155+
const requested = new Set<string>();
156+
// Requests arriving during a host read wait for the next sweep.
157+
const pendingRequests = new Set<string>();
158+
// PR keys wait in `pendingRequests` for one queued sweep, so a burst of links (an agent
158159
// linking dozens of pull requests) is read together and shares the summary batches.
159160
let requestedSweepQueued = false;
160161
const retryStacks = new Set<string>();
@@ -195,13 +196,20 @@ export const make = Effect.gen(function* () {
195196
const groups = new Map<string, Array<LinkEntry>>();
196197
for (const thread of threads) {
197198
for (const link of visibleThreadPullRequests(thread.pullRequests ?? [])) {
198-
const key = threadPullRequestKeyOf(link);
199+
// Reads depend on the project's checkout and credentials, including their failures.
200+
const key = `${thread.projectId}\0${threadPullRequestKeyOf(link)}`;
199201
const entries = groups.get(key) ?? [];
200202
entries.push({ thread, link });
201203
groups.set(key, entries);
202204
}
203205
}
204206

207+
// A host-level refresh reaches every project, each retaining its own pending retry.
208+
for (const [key, entries] of groups) {
209+
if (pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) requested.add(key);
210+
}
211+
pendingRequests.clear();
212+
205213
for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key);
206214
for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key);
207215
for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key);
@@ -278,19 +286,19 @@ export const make = Effect.gen(function* () {
278286
repository: first.link.repository,
279287
number: first.link.number,
280288
};
281-
const generation = requested.get(key);
289+
const refreshRequested = requested.has(key);
282290
// A held merged summary is served without asking the host, so a snapshot still missing its
283291
// merge time has to drop it or the slow-cadence re-read would hand back the same gap.
284292
const missingMergedAt = entries.some(
285293
(entry) => entry.link.snapshot?.state === "merged" && entry.link.snapshot.mergedAt == null,
286294
);
287-
if (generation !== undefined || missingMergedAt) {
295+
if (refreshRequested || missingMergedAt) {
288296
yield* pullRequests.invalidate({ reference: ref });
289297
}
290298
const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false });
291299
const fields = snapshotFieldsOf(summary);
292300
const needsStack =
293-
generation !== undefined ||
301+
refreshRequested ||
294302
retryStacks.has(key) ||
295303
entries.some(
296304
(entry) =>
@@ -330,8 +338,7 @@ export const make = Effect.gen(function* () {
330338
}
331339
// The host answered, so the cadence clock ticks even if a dispatch below is rejected.
332340
lastSyncedAt.set(key, nowMs);
333-
// A refresh requested while the host read was in flight belongs to the next sweep.
334-
if (requested.get(key) === generation) requested.delete(key);
341+
requested.delete(key);
335342
yield* Effect.forEach(
336343
entries,
337344
(entry) =>
@@ -473,7 +480,7 @@ export const make = Effect.gen(function* () {
473480

474481
const requestSync: PullRequestSyncReactor["Service"]["requestSync"] = (key) =>
475482
Effect.suspend(() => {
476-
requested.set(threadPullRequestKeyOf(key), ++requestGeneration);
483+
pendingRequests.add(threadPullRequestKeyOf(key));
477484
if (requestedSweepQueued) return Effect.void;
478485
requestedSweepQueued = true;
479486
return worker.enqueue("requested");

0 commit comments

Comments
 (0)