Skip to content

Commit 5327a8f

Browse files
committed
fix(server): 独立处理默认分支抓取超时
在抓取开始时确定共享截止时间,各阶段仅使用剩余预算,私有目录清理覆盖必需及可选抓取。 将可选默认分支超时转换为其自身的失败退避,避免延迟已经成功的上游刷新。 补充上游耗时三秒后默认分支仅余两秒的超时回归,验证持续刷新、指数退避和成功后的重置;49 个定向测试及服务端类型检查通过。
1 parent 0d9dfdb commit 5327a8f

2 files changed

Lines changed: 182 additions & 121 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 104 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,74 +3499,117 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
34993499
}),
35003500
);
35013501

3502-
it.effect("backs off default-branch failures without delaying upstream refreshes", () =>
3503-
Effect.gen(function* () {
3504-
const cwd = yield* makeTmpDir();
3505-
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
3506-
const updater = yield* makeTmpDir("git-vcs-driver-updater-");
3507-
const { initialBranch } = yield* initRepoWithCommit(cwd);
3508-
yield* git(remote, ["init", "--bare"]);
3509-
yield* git(cwd, ["remote", "add", "origin", remote]);
3510-
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
3511-
yield* git(cwd, ["remote", "set-head", "origin", initialBranch]);
3512-
yield* git(cwd, ["checkout", "-b", "feature/backoff"]);
3513-
yield* git(cwd, ["push", "-u", "origin", "feature/backoff"]);
3514-
yield* git(updater, ["clone", "--branch", "feature/backoff", remote, "."]);
3515-
yield* git(updater, ["config", "user.email", "[email protected]"]);
3516-
yield* git(updater, ["config", "user.name", "Test"]);
3517-
yield* git(updater, ["commit", "--allow-empty", "-m", "first remote update"]);
3518-
yield* git(updater, ["push"]);
3502+
it.effect.each(["exit", "timeout"] as const)(
3503+
"backs off default-branch %s failures without delaying upstream refreshes",
3504+
(failureMode) =>
3505+
Effect.gen(function* () {
3506+
const cwd = yield* makeTmpDir();
3507+
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
3508+
const updater = yield* makeTmpDir("git-vcs-driver-updater-");
3509+
const { initialBranch } = yield* initRepoWithCommit(cwd);
3510+
yield* git(remote, ["init", "--bare"]);
3511+
yield* git(cwd, ["remote", "add", "origin", remote]);
3512+
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
3513+
yield* git(cwd, ["remote", "set-head", "origin", initialBranch]);
3514+
yield* git(cwd, ["checkout", "-b", "feature/backoff"]);
3515+
yield* git(cwd, ["push", "-u", "origin", "feature/backoff"]);
3516+
yield* git(updater, ["clone", "--branch", "feature/backoff", remote, "."]);
3517+
yield* git(updater, ["config", "user.email", "[email protected]"]);
3518+
yield* git(updater, ["config", "user.name", "Test"]);
3519+
yield* git(updater, ["commit", "--allow-empty", "-m", "first remote update"]);
3520+
yield* git(updater, ["push"]);
35193521

3520-
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
3521-
let defaultAttempts = 0;
3522-
let failDefault = true;
3523-
const spawner = ChildProcessSpawner.make((command) =>
3524-
Effect.gen(function* () {
3525-
if (
3526-
ChildProcess.isStandardCommand(command) &&
3527-
command.args.includes("fetch") &&
3528-
command.args.at(-1)?.startsWith(`+refs/heads/${initialBranch}:`)
3529-
) {
3530-
defaultAttempts += 1;
3531-
if (failDefault) {
3532-
return ChildProcessSpawner.makeHandle({
3533-
...makeSuccessfulHandle(""),
3534-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
3535-
});
3522+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
3523+
let defaultAttempts = 0;
3524+
let failDefault = true;
3525+
const stalledAttempts = yield* Queue.unbounded<void>();
3526+
const requiredFetches = yield* Queue.unbounded<Deferred.Deferred<void>>();
3527+
const spawner = ChildProcessSpawner.make((command) =>
3528+
Effect.gen(function* () {
3529+
if (
3530+
failureMode === "timeout" &&
3531+
ChildProcess.isStandardCommand(command) &&
3532+
command.args.includes("fetch") &&
3533+
command.args.at(-1)?.startsWith("+refs/heads/feature/backoff:")
3534+
) {
3535+
const release = yield* Deferred.make<void>();
3536+
yield* Queue.offer(requiredFetches, release);
3537+
yield* Deferred.await(release);
3538+
}
3539+
if (
3540+
ChildProcess.isStandardCommand(command) &&
3541+
command.args.includes("fetch") &&
3542+
command.args.at(-1)?.startsWith(`+refs/heads/${initialBranch}:`)
3543+
) {
3544+
defaultAttempts += 1;
3545+
if (failDefault) {
3546+
if (failureMode === "timeout") {
3547+
yield* Queue.offer(stalledAttempts, undefined);
3548+
return yield* Effect.never;
3549+
}
3550+
return ChildProcessSpawner.makeHandle({
3551+
...makeSuccessfulHandle(""),
3552+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
3553+
});
3554+
}
35363555
}
3556+
return yield* delegate.spawn(command);
3557+
}),
3558+
);
3559+
const driver = yield* makeGitVcsDriverCore().pipe(
3560+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
3561+
Effect.provide(layerServerConfig),
3562+
);
3563+
const readStatus = Effect.fnUntraced(function* () {
3564+
const refresh = yield* driver
3565+
.statusDetailsRemote(cwd)
3566+
.pipe(Effect.forkChild({ startImmediately: true }));
3567+
const required = yield* Effect.race(
3568+
Queue.take(requiredFetches),
3569+
Fiber.await(refresh).pipe(Effect.as(null)),
3570+
);
3571+
if (required !== null) {
3572+
yield* TestClock.adjust("3 seconds");
3573+
yield* Deferred.succeed(required, undefined);
35373574
}
3538-
return yield* delegate.spawn(command);
3539-
}),
3540-
);
3541-
const driver = yield* makeGitVcsDriverCore().pipe(
3542-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
3543-
Effect.provide(layerServerConfig),
3544-
);
3545-
assert.equal((yield* driver.statusDetailsRemote(cwd)).behindCount, 1);
3546-
assert.equal(defaultAttempts, 1);
3575+
const stalled = yield* Effect.race(
3576+
Queue.take(stalledAttempts).pipe(Effect.as(true)),
3577+
Fiber.await(refresh).pipe(Effect.as(false)),
3578+
);
3579+
if (stalled) yield* TestClock.adjust("2 seconds");
3580+
return yield* Fiber.join(refresh);
3581+
});
3582+
assert.equal((yield* readStatus()).behindCount, 1);
3583+
assert.equal(defaultAttempts, 1);
3584+
const fs = yield* FileSystem.FileSystem;
3585+
const path = yield* Path.Path;
3586+
assert.deepStrictEqual(
3587+
yield* fs.readDirectory(path.join(cwd, ".git", "objects", "t3-status-fetch")),
3588+
[],
3589+
);
35473590

3548-
yield* git(updater, ["commit", "--allow-empty", "-m", "second remote update"]);
3549-
yield* git(updater, ["push"]);
3550-
yield* TestClock.adjust("16 seconds");
3551-
assert.equal((yield* driver.statusDetailsRemote(cwd)).behindCount, 2);
3552-
assert.equal(defaultAttempts, 1);
3553-
for (const expectedAttempts of [2, 2, 2, 2]) {
3591+
yield* git(updater, ["commit", "--allow-empty", "-m", "second remote update"]);
3592+
yield* git(updater, ["push"]);
35543593
yield* TestClock.adjust("16 seconds");
3555-
yield* driver.statusDetailsRemote(cwd);
3556-
assert.equal(defaultAttempts, expectedAttempts);
3557-
}
3594+
assert.equal((yield* readStatus()).behindCount, 2);
3595+
assert.equal(defaultAttempts, 1);
3596+
for (const expectedAttempts of [2, 2, 2, 2]) {
3597+
yield* TestClock.adjust("16 seconds");
3598+
yield* readStatus();
3599+
assert.equal(defaultAttempts, expectedAttempts);
3600+
}
35583601

3559-
failDefault = false;
3560-
yield* TestClock.adjust("16 seconds");
3561-
yield* driver.statusDetailsRemote(cwd);
3562-
assert.equal(defaultAttempts, 3);
3563-
failDefault = true;
3564-
for (const expectedAttempts of [4, 4, 5]) {
3602+
failDefault = false;
35653603
yield* TestClock.adjust("16 seconds");
3566-
yield* driver.statusDetailsRemote(cwd);
3567-
assert.equal(defaultAttempts, expectedAttempts);
3568-
}
3569-
}),
3604+
yield* readStatus();
3605+
assert.equal(defaultAttempts, 3);
3606+
failDefault = true;
3607+
for (const expectedAttempts of [4, 4, 5]) {
3608+
yield* TestClock.adjust("16 seconds");
3609+
yield* readStatus();
3610+
assert.equal(defaultAttempts, expectedAttempts);
3611+
}
3612+
}),
35703613
);
35713614

35723615
it.effect.each(["gitdir", "onbranch", "global gitdir"] as const)(

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,8 +1308,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13081308
defaultBranchName: string | null,
13091309
): Effect.Effect<void, GitCommandError> =>
13101310
Effect.gen(function* () {
1311+
const deadlineMs =
1312+
DateTime.toEpochMillis(yield* DateTime.now) +
1313+
Duration.toMillis(STATUS_UPSTREAM_REFRESH_TIMEOUT);
13111314
const fetchCwd =
13121315
path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
1316+
const timeoutError = new GitCommandError({
1317+
operation: "GitVcsDriver.fetchRemoteForStatus",
1318+
command: "git",
1319+
cwd: fetchCwd,
1320+
detail: "Git status refresh timed out.",
1321+
});
1322+
const withinDeadline = Effect.fnUntraced(function* <A>(
1323+
effect: Effect.Effect<A, GitCommandError>,
1324+
) {
1325+
const remainingMs = deadlineMs - DateTime.toEpochMillis(yield* DateTime.now);
1326+
if (remainingMs <= 0) return yield* timeoutError;
1327+
return yield* effect.pipe(
1328+
Effect.timeoutOption(remainingMs),
1329+
Effect.flatMap((result) => Effect.fromOption(result, () => timeoutError)),
1330+
);
1331+
});
13131332
const objectDirectory = path.join(gitCommonDir, "objects");
13141333
const isolatedFetchRoot = path.join(objectDirectory, GIT_STATUS_FETCH_OBJECT_DIRECTORY);
13151334

@@ -1345,7 +1364,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13451364
),
13461365
);
13471366

1348-
yield* cleanupStaleIsolatedFetches;
13491367
const operationId = yield* crypto.randomUUIDv4().pipe(
13501368
Effect.mapError(
13511369
(cause) =>
@@ -1357,6 +1375,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13571375
cause,
13581376
}),
13591377
),
1378+
withinDeadline,
13601379
);
13611380
activeStatusFetchOperationIds.add(operationId);
13621381
const temporaryRoot = path.join(isolatedFetchRoot, operationId);
@@ -1391,6 +1410,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13911410
);
13921411

13931412
yield* Effect.gen(function* () {
1413+
yield* cleanupStaleIsolatedFetches;
13941414
const objectFormat = yield* executeGit(
13951415
"GitVcsDriver.fetchRemoteForStatus.objectFormat",
13961416
fetchCwd,
@@ -1669,66 +1689,64 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16691689
});
16701690
});
16711691

1672-
const [upstreamFetchTarget, ...optionalFetchTargets] = fetchTargets;
1673-
if (!upstreamFetchTarget) return;
1674-
1675-
yield* fetchAndPromoteTarget(upstreamFetchTarget);
1676-
yield* Effect.forEach(
1677-
optionalFetchTargets,
1678-
(fetchTarget) =>
1679-
Effect.gen(function* () {
1680-
// A healthy upstream must not reset the retry budget for a slow default branch.
1681-
const key = [gitCommonDir, remoteName, fetchTarget.remoteRef].join("\0");
1682-
const previousFailure = optionalStatusFetchFailures.get(key);
1683-
const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
1684-
if (previousFailure !== undefined && nowMs < previousFailure.retryAtMs) return;
1685-
yield* fetchAndPromoteTarget(fetchTarget).pipe(
1686-
Effect.tap(() => Effect.sync(() => optionalStatusFetchFailures.delete(key))),
1687-
Effect.catch((cause) =>
1688-
Effect.gen(function* () {
1689-
const failures = (optionalStatusFetchFailures.get(key)?.failures ?? 0) + 1;
1690-
const failedAtMs = DateTime.toEpochMillis(yield* DateTime.now);
1691-
optionalStatusFetchFailures.delete(key);
1692-
optionalStatusFetchFailures.set(key, {
1693-
failures,
1694-
retryAtMs:
1695-
failedAtMs +
1696-
Duration.toMillis(statusUpstreamRefreshFailureCooldown(failures)),
1697-
});
1698-
if (optionalStatusFetchFailures.size > STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY) {
1699-
const oldestKey = optionalStatusFetchFailures.keys().next().value;
1700-
if (oldestKey !== undefined) optionalStatusFetchFailures.delete(oldestKey);
1701-
}
1702-
yield* Effect.logWarning(
1703-
"Failed to refresh the default branch during Git status fetch.",
1704-
{
1705-
cause,
1706-
remoteName,
1707-
defaultBranchName,
1708-
},
1709-
);
1710-
}),
1711-
),
1712-
);
1713-
}),
1714-
{ concurrency: 1, discard: true },
1715-
);
1716-
}).pipe(Effect.ensuring(cleanupIsolatedFetch));
1717-
}).pipe(
1718-
Effect.timeoutOption(STATUS_UPSTREAM_REFRESH_TIMEOUT),
1719-
Effect.flatMap((result) =>
1720-
Effect.fromOption(
1721-
result,
1722-
() =>
1723-
new GitCommandError({
1724-
operation: "GitVcsDriver.fetchRemoteForStatus",
1725-
command: "git",
1726-
cwd: gitCommonDir,
1727-
detail: "Git status refresh timed out.",
1728-
}),
1692+
return fetchAndPromoteTarget;
1693+
}).pipe(
1694+
withinDeadline,
1695+
Effect.flatMap((fetchAndPromoteTarget) =>
1696+
Effect.gen(function* () {
1697+
const [upstreamFetchTarget, ...optionalFetchTargets] = fetchTargets;
1698+
if (!upstreamFetchTarget) return;
1699+
1700+
yield* fetchAndPromoteTarget(upstreamFetchTarget).pipe(withinDeadline);
1701+
yield* Effect.forEach(
1702+
optionalFetchTargets,
1703+
(fetchTarget) =>
1704+
Effect.gen(function* () {
1705+
// A healthy upstream must not reset the retry budget for a slow default branch.
1706+
const key = [gitCommonDir, remoteName, fetchTarget.remoteRef].join("\0");
1707+
const previousFailure = optionalStatusFetchFailures.get(key);
1708+
const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
1709+
if (previousFailure !== undefined && nowMs < previousFailure.retryAtMs) return;
1710+
yield* fetchAndPromoteTarget(fetchTarget).pipe(
1711+
withinDeadline,
1712+
Effect.tap(() => Effect.sync(() => optionalStatusFetchFailures.delete(key))),
1713+
Effect.catch((cause) =>
1714+
Effect.gen(function* () {
1715+
const failures = (optionalStatusFetchFailures.get(key)?.failures ?? 0) + 1;
1716+
const failedAtMs = DateTime.toEpochMillis(yield* DateTime.now);
1717+
optionalStatusFetchFailures.delete(key);
1718+
optionalStatusFetchFailures.set(key, {
1719+
failures,
1720+
retryAtMs:
1721+
failedAtMs +
1722+
Duration.toMillis(statusUpstreamRefreshFailureCooldown(failures)),
1723+
});
1724+
if (
1725+
optionalStatusFetchFailures.size > STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY
1726+
) {
1727+
const oldestKey = optionalStatusFetchFailures.keys().next().value;
1728+
if (oldestKey !== undefined)
1729+
optionalStatusFetchFailures.delete(oldestKey);
1730+
}
1731+
yield* Effect.logWarning(
1732+
"Failed to refresh the default branch during Git status fetch.",
1733+
{
1734+
cause,
1735+
remoteName,
1736+
defaultBranchName,
1737+
},
1738+
);
1739+
}),
1740+
),
1741+
);
1742+
}),
1743+
{ concurrency: 1, discard: true },
1744+
);
1745+
}),
17291746
),
1730-
),
1731-
);
1747+
Effect.ensuring(cleanupIsolatedFetch),
1748+
);
1749+
});
17321750

17331751
const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
17341752
cwd: string,

0 commit comments

Comments
 (0)