Skip to content

Commit 91f55d9

Browse files
fix(server): PR sync reads share GitHub requests again on busy servers (#28422)
Co-authored-by: Julius Marminge <[email protected]>
1 parent c9f0cac commit 91f55d9

5 files changed

Lines changed: 189 additions & 20 deletions

File tree

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const missingSummary = Effect.fail(
171171

172172
interface HarnessOptions {
173173
readonly invalidate?: PullRequestService.PullRequestService["Service"]["invalidate"];
174+
readonly batchesReads?: (input: PullRequestRef) => boolean;
174175
readonly snapshot: TestShellSnapshot;
175176
readonly summary?: (
176177
input: PullRequestRef,
@@ -228,6 +229,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
228229
const layerDependencies = Layer.mergeAll(
229230
Layer.mock(PullRequestService.PullRequestService)({
230231
withSharedProjectReads: (effect) => effect,
232+
batchesReads: (input) => Effect.succeed(options.batchesReads?.(input) ?? true),
231233
summary,
232234
stack,
233235
invalidate: options.invalidate ?? (() => Effect.void),
@@ -1224,6 +1226,110 @@ describe("PullRequestSyncReactor", () => {
12241226
),
12251227
);
12261228

1229+
it.effect("reads hosts that batch all at once and other hosts at the old sweep width", () =>
1230+
Effect.scoped(
1231+
Effect.gen(function* () {
1232+
yield* TestClock.setTime(Date.parse(NOW));
1233+
const numbers = Array.from({ length: 60 }, (_, index) => index + 1);
1234+
const hostOf = (number: number) => (number <= 30 ? "github.com" : "gitlab.example.com");
1235+
const batches = (input: PullRequestRef) => input.host === "github.com";
1236+
const batched = yield* Ref.make(0);
1237+
const unbatched = yield* Ref.make(0);
1238+
const started = yield* Queue.unbounded<void>();
1239+
const release = yield* Deferred.make<void>();
1240+
const fixture = yield* makeHarness({
1241+
snapshot: makeSnapshot(
1242+
numbers.map((number) =>
1243+
makeThread(`thread-${number}`, {
1244+
pullRequests: [makeLink(number, null, { host: hostOf(number) })],
1245+
}),
1246+
),
1247+
),
1248+
batchesReads: batches,
1249+
summary: (input) => {
1250+
const inFlight = batches(input) ? batched : unbatched;
1251+
return Ref.update(inFlight, (count) => count + 1).pipe(
1252+
Effect.andThen(Queue.offer(started, undefined)),
1253+
Effect.andThen(Deferred.await(release)),
1254+
Effect.andThen(Ref.update(inFlight, (count) => count - 1)),
1255+
Effect.as(makeSummary(input)),
1256+
);
1257+
},
1258+
});
1259+
yield* Effect.gen(function* () {
1260+
const reactor = yield* PullRequestSyncReactor.PullRequestSyncReactor;
1261+
yield* reactor.start();
1262+
yield* Deferred.succeed(fixture.activation, undefined);
1263+
yield* Queue.takeN(started, 55);
1264+
assert.strictEqual(yield* Ref.get(batched), 30);
1265+
assert.strictEqual(yield* Ref.get(unbatched), 25);
1266+
yield* Deferred.succeed(release, undefined);
1267+
yield* reactor.drain;
1268+
assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, numbers.length);
1269+
}).pipe(Effect.provide(fixture.layer));
1270+
}),
1271+
),
1272+
);
1273+
1274+
it.effect("reads a member a sibling flags while the sweep is full", () =>
1275+
Effect.scoped(
1276+
Effect.gen(function* () {
1277+
yield* TestClock.setTime(Date.parse(NOW));
1278+
const stackOf = (layers: ReadonlyArray<number>) => ({
1279+
id: "9",
1280+
number: 9,
1281+
url: "https://github.com/owner/repository/stack/9",
1282+
base: "main",
1283+
layers: layers.map(stackLayer),
1284+
});
1285+
// #50 is merged and not due until #1's stack read shows it a different layer list.
1286+
const merged = makeLink(
1287+
50,
1288+
{ state: "merged", mergedAt: NOW },
1289+
{ stack: { kind: "native", ...stackOf([50]) } },
1290+
);
1291+
const started = yield* Ref.make(0);
1292+
const full = yield* Deferred.make<void>();
1293+
const synced1 = yield* Deferred.make<void>();
1294+
const fixture = yield* makeHarness({
1295+
snapshot: makeSnapshot([
1296+
// Another host takes 24 of the 25 shared slots and #1 the last one.
1297+
...Array.from({ length: 24 }, (_, index) =>
1298+
makeThread(`thread-${index + 2}`, {
1299+
pullRequests: [makeLink(index + 2, null, { host: "gitlab.example.com" })],
1300+
}),
1301+
),
1302+
makeThread("thread-1", { pullRequests: [makeLink(1)] }),
1303+
makeThread("thread-50", { pullRequests: [merged] }),
1304+
]),
1305+
batchesReads: () => false,
1306+
// #1 reads once all 25 slots are taken; the other 24 hold theirs until #1 has synced.
1307+
summary: (input) =>
1308+
Ref.updateAndGet(started, (count) => count + 1).pipe(
1309+
// Forked, so #1 resumes only after the sweep has handed out its next slot.
1310+
Effect.tap((count) =>
1311+
count === 25 ? Effect.forkChild(Deferred.succeed(full, undefined)) : Effect.void,
1312+
),
1313+
Effect.andThen(
1314+
input.number === 50
1315+
? Effect.void
1316+
: Deferred.await(input.number === 1 ? full : synced1),
1317+
),
1318+
Effect.as(makeSummary(input)),
1319+
),
1320+
stack: (input) => Effect.succeed(input.number === 1 ? stackOf([1, 50]) : null),
1321+
onSync: (command) =>
1322+
command.number === 1 ? Deferred.succeed(synced1, undefined) : Effect.void,
1323+
});
1324+
yield* Effect.gen(function* () {
1325+
yield* startAndSweep(fixture);
1326+
const read = (yield* Ref.get(fixture.summaryCalls)).map((ref) => ref.number);
1327+
assert.include(read, 50);
1328+
}).pipe(Effect.provide(fixture.layer));
1329+
}),
1330+
),
1331+
);
1332+
12271333
it.effect("skips the stack read for a pull request the summary places in no stack", () =>
12281334
Effect.scoped(
12291335
Effect.gen(function* () {

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

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ interface LinkEntry {
183183
readonly link: ThreadPullRequestLink;
184184
}
185185

186+
const referenceOf = ({ thread, link }: LinkEntry) => ({
187+
projectId: thread.projectId,
188+
host: normalizeThreadPullRequestKey(link).host,
189+
repository: link.repository,
190+
number: link.number,
191+
});
192+
193+
/** Pauses and batching both belong to a project's host. */
194+
const pauseKeyOf = (entry: LinkEntry) => {
195+
const { projectId, host } = referenceOf(entry);
196+
return `${projectId}\0${host}`;
197+
};
198+
186199
/** Another layer of the stack `url` belongs to, in its host's canonical shape. */
187200
function siblingChangeRequestUrl(url: string, number: number): string | null {
188201
if (!Number.isSafeInteger(number) || number < 1) return null;
@@ -478,12 +491,7 @@ export const make = Effect.gen(function* () {
478491

479492
const syncGroup = Effect.fnUntraced(function* (key: string, entries: ReadonlyArray<LinkEntry>) {
480493
const first = entries[0]!;
481-
const ref = {
482-
projectId: first.thread.projectId,
483-
host: normalizeThreadPullRequestKey(first.link).host,
484-
repository: first.link.repository,
485-
number: first.link.number,
486-
};
494+
const ref = referenceOf(first);
487495
const refreshRequested = requested.has(key);
488496
// A held merged summary is served without asking the host, so a snapshot still missing its
489497
// merge time has to drop it or the slow-cadence re-read would hand back the same gap.
@@ -612,25 +620,63 @@ export const make = Effect.gen(function* () {
612620
return Effect.void;
613621
}),
614622
);
623+
const pools = {
624+
// Wider than one summary batch, so reads that finish their cache checks at different
625+
// times still share batches.
626+
batched: { width: 100, reads: yield* Semaphore.make(100) },
627+
// One read per pull request, often a CLI process, so these keep the narrower width.
628+
unbatched: { width: 25, reads: yield* Semaphore.make(25) },
629+
};
630+
const isPaused = (pauseKey: string, atMs: number) => (pausedUntil.get(pauseKey) ?? 0) > atMs;
631+
// Judged once a slot is free, so a stack retry a sibling flags meanwhile still counts, and
632+
// pauses set during the sweep apply at once.
633+
const readIfDue = (
634+
key: string,
635+
entries: ReadonlyArray<LinkEntry>,
636+
pauseKey: string,
637+
reads: Semaphore.Semaphore,
638+
) =>
639+
reads.withPermits(1)(
640+
Effect.suspend(() =>
641+
isDue(key, entries, nowMs)
642+
? Clock.currentTimeMillis.pipe(
643+
Effect.flatMap((startedAtMs) =>
644+
isPaused(pauseKey, startedAtMs) ? Effect.void : readGroup(key, entries, pauseKey),
645+
),
646+
)
647+
: Effect.void,
648+
),
649+
);
650+
const groupsByHost = Map.groupBy(
651+
Array.from(groups).filter(([key]) => scope === "all" || requested.has(key)),
652+
([, entries]) => pauseKeyOf(entries[0]!),
653+
);
615654
yield* Effect.forEach(
616-
groups,
617-
([key, entries]) => {
618-
if (!((scope === "all" || requested.has(key)) && isDue(key, entries, nowMs))) {
655+
groupsByHost,
656+
([pauseKey, hostGroups]) => {
657+
// A host paused or with nothing due skips the lookup. Stack retries only flag groups on
658+
// the flagging group's own host, so nothing on it becomes due this sweep.
659+
if (
660+
isPaused(pauseKey, nowMs) ||
661+
!hostGroups.some(([key, entries]) => isDue(key, entries, nowMs))
662+
) {
619663
return Effect.void;
620664
}
621-
const first = entries[0]!;
622-
const pauseKey = `${first.thread.projectId}\0${normalizeThreadPullRequestKey(first.link).host}`;
623-
// Check pauses at each read so changes during the sweep apply immediately.
624-
return Clock.currentTimeMillis.pipe(
625-
Effect.flatMap((startedAtMs) =>
626-
(pausedUntil.get(pauseKey) ?? 0) > startedAtMs
627-
? Effect.void
628-
: readGroup(key, entries, pauseKey),
629-
),
665+
return pullRequests.batchesReads(referenceOf(hostGroups[0]![1][0]!)).pipe(
666+
Effect.flatMap((batches) => {
667+
const { width, reads } = batches ? pools.batched : pools.unbatched;
668+
return Effect.forEach(
669+
hostGroups,
670+
([key, entries]) => readIfDue(key, entries, pauseKey, reads),
671+
{
672+
concurrency: width,
673+
discard: true,
674+
},
675+
);
676+
}),
630677
);
631678
},
632-
// Match summary batch width so GitHub reads coalesce into one request.
633-
{ concurrency: 25, discard: true },
679+
{ concurrency: "unbounded", discard: true },
634680
);
635681
for (const [reason, { count, key }] of skips) {
636682
yield* Effect.logWarning("pull request sync skipped", { count, key, reason });

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ export class PullRequestService extends Context.Service<
200200
input: PullRequestRef,
201201
options?: { readonly recoverTransientFailure?: boolean },
202202
) => Effect.Effect<PullRequestSummary, PullRequestError>;
203+
/** Whether the reference's host batches summary reads; false when it cannot be resolved. */
204+
readonly batchesReads: (input: PullRequestRef) => Effect.Effect<boolean>;
203205
readonly stack: (
204206
input: PullRequestRef,
205207
options?: { readonly includeDetails?: boolean },
@@ -536,6 +538,7 @@ function withRateLimitBackoff(
536538
...(api.readsPublicHostWithoutCheckout === undefined
537539
? {}
538540
: { readsPublicHostWithoutCheckout: api.readsPublicHostWithoutCheckout }),
541+
...(api.batchesReads === undefined ? {} : { batchesReads: api.batchesReads }),
539542
getViewer:
540543
options?.viewerAllowsPause === true
541544
? interactive("getViewer", api.getViewer)
@@ -3074,6 +3077,16 @@ export const make = Effect.gen(function* () {
30743077
);
30753078
};
30763079

3080+
const batchesReads: PullRequestService["Service"]["batchesReads"] = Effect.fn(
3081+
"PullRequestService.batchesReads",
3082+
)(
3083+
function* (input) {
3084+
const project = yield* requireProject(input);
3085+
return project.api.batchesReads === true;
3086+
},
3087+
Effect.orElseSucceed(() => false),
3088+
);
3089+
30773090
const stack: PullRequestService["Service"]["stack"] = (input, options) =>
30783091
persistedRead(
30793092
input,
@@ -3850,6 +3863,7 @@ export const make = Effect.gen(function* () {
38503863
return yield* listStats({ ...input, refs: refs.flatMap(Option.toArray) });
38513864
}),
38523865
summary: credentialCached(summary),
3866+
batchesReads,
38533867
stack: credentialCached(stack),
38543868
subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
38553869
Effect.map((subscription) => Stream.fromSubscription(subscription)),

‎packages/source-control-core/src/server/PullRequestProvider.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ export interface PullRequestProviderApi {
318318
* definition's `publicHost` can be read from a project with no checkout of its repository.
319319
*/
320320
readonly readsPublicHostWithoutCheckout?: boolean;
321+
/** Concurrent summary reads share requests, so callers may keep many in flight. */
322+
readonly batchesReads?: boolean;
321323
/**
322324
* Absent means every host detected as this kind is read. A host it refuses is listed as one that
323325
* cannot be browsed here, and nothing is sent for it.

‎packages/source-control-github/src/server/GitHubPullRequestProvider.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ export const make = Effect.gen(function* () {
399399
capabilities: CAPABILITIES,
400400
unavailableRequirement: UNAVAILABLE_REQUIREMENT,
401401
readsPublicHostWithoutCheckout: true,
402+
batchesReads: true,
402403
mergeMessageRewrite: removeAgentCredits,
403404
getRoutingIdentity: (input) =>
404405
cli.getRoutingIdentity(input).pipe(Effect.mapError(fail("routeIdentity"))),

0 commit comments

Comments
 (0)