Skip to content
TypeScript

perf(server): batch checkpoint object flushes on macOS and Windows (#21291)

Joseph Vidal55ba1baa559e
diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts
index 44b3ad5ab85..d8dfaa27b62 100644
--- a/apps/server/src/vcs/GitVcsDriver.test.ts
+++ b/apps/server/src/vcs/GitVcsDriver.test.ts
@@ -1367,69 +1367,83 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
-it.effect("GitVcsDriver flushes checkpoint objects and refs to disk before publishing them", () => {
- const observedArgs: ReadonlyArray<string>[] = [];
-
- return Effect.gen(function* () {
- const driver = yield* GitVcsDriver.makeVcsDriverShape();
+it.effect.each([
+ { platform: "linux", fsyncMethods: ["fsync"] },
+ { platform: "darwin", fsyncMethods: ["fsync", "batch"] },
+ { platform: "win32", fsyncMethods: ["fsync", "batch"] },
+] as const)(
+ "GitVcsDriver flushes checkpoint objects and refs to disk before publishing them on $platform",
+ ({ platform, fsyncMethods }) => {
+ const observedArgs: ReadonlyArray<string>[] = [];
+
+ return Effect.gen(function* () {
+ const driver = yield* GitVcsDriver.makeVcsDriverShape();
- yield* driver.checkpoints.captureCheckpoint({
- cwd: "/repo",
- checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread/turn/1"),
- });
+ yield* driver.checkpoints.captureCheckpoint({
+ cwd: "/repo",
+ checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread/turn/1"),
+ });
- const writeCommands = ["add", "write-tree", "commit-tree", "update-ref"];
- const writes = observedArgs.filter((args) =>
- writeCommands.some((command) => args.includes(command)),
- );
- assert.strictEqual(writes.length, 4);
- for (const args of writes) {
- const command = args.findIndex((arg) => writeCommands.includes(arg));
- for (const setting of ["core.fsync=objects,reference", "core.fsyncMethod=fsync"]) {
- const index = args.indexOf(setting);
- assert.strictEqual(args[index - 1], "-c", args.join(" "));
- assert.isBelow(index, command);
+ const writeCommands = ["add", "write-tree", "commit-tree", "update-ref"];
+ const writes = observedArgs.filter((args) =>
+ writeCommands.some((command) => args.includes(command)),
+ );
+ assert.strictEqual(writes.length, 4);
+ for (const args of writes) {
+ const command = args.findIndex((arg) => writeCommands.includes(arg));
+ assert.deepStrictEqual(
+ args.filter((arg) => arg.startsWith("core.fsyncMethod=")),
+ fsyncMethods.map((method) => `core.fsyncMethod=${method}`),
+ );
+ for (const setting of [
+ "core.fsync=objects,reference",
+ ...fsyncMethods.map((method) => `core.fsyncMethod=${method}`),
+ ]) {
+ const index = args.indexOf(setting);
+ assert.strictEqual(args[index - 1], "-c", args.join(" "));
+ assert.isBelow(index, command);
+ }
}
- }
- assert.deepStrictEqual(observedArgs.at(-1), [
- "-C",
- "/repo",
- "-c",
- "core.fsync=objects,reference",
- "-c",
- "core.fsyncMethod=fsync",
- "update-ref",
- "refs/t3/checkpoints/thread/turn/1",
- "commit0000",
- ]);
- }).pipe(
- Effect.provide(
- Layer.mergeAll(
- NodeServices.layer,
- Layer.mock(VcsProcess.VcsProcess)({
- run: (input) =>
- Effect.sync(() => {
- observedArgs.push(input.args);
- const stdout = input.args.includes("write-tree")
- ? "tree0000\n"
- : input.args.includes("commit-tree")
- ? "commit0000\n"
- : input.args.includes("--git-common-dir")
- ? ".git\n"
- : "";
- return {
- exitCode: ChildProcessSpawner.ExitCode(0),
- stdout,
- stderr: "",
- stdoutTruncated: false,
- stderrTruncated: false,
- };
- }),
- }),
+ assert.deepStrictEqual(observedArgs.at(-1), [
+ "-C",
+ "/repo",
+ "-c",
+ "core.fsync=objects,reference",
+ ...fsyncMethods.flatMap((method) => ["-c", `core.fsyncMethod=${method}`]),
+ "update-ref",
+ "refs/t3/checkpoints/thread/turn/1",
+ "commit0000",
+ ]);
+ }).pipe(
+ Effect.provideService(HostProcess.Platform, platform),
+ Effect.provide(
+ Layer.mergeAll(
+ NodeServices.layer,
+ Layer.mock(VcsProcess.VcsProcess)({
+ run: (input) =>
+ Effect.sync(() => {
+ observedArgs.push(input.args);
+ const stdout = input.args.includes("write-tree")
+ ? "tree0000\n"
+ : input.args.includes("commit-tree")
+ ? "commit0000\n"
+ : input.args.includes("--git-common-dir")
+ ? ".git\n"
+ : "";
+ return {
+ exitCode: ChildProcessSpawner.ExitCode(0),
+ stdout,
+ stderr: "",
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ };
+ }),
+ }),
+ ),
),
- ),
- );
-});
+ );
+ },
+);
it.effect.each(["win32", "linux"] as const)(
"checkpoint working-tree commands enable Git long paths only on Windows (%s)",
diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts
index 28a105c3460..684285d608d 100644
--- a/apps/server/src/vcs/GitVcsDriver.ts
+++ b/apps/server/src/vcs/GitVcsDriver.ts
@@ -542,6 +542,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
const path = yield* Path.Path;
const vcsProcess = yield* VcsProcess.VcsProcess;
const crypto = yield* Crypto.Crypto;
+ const hostPlatform = yield* HostProcess.Platform;
const capabilities = {
kind: "git" as const,
supportsWorktrees: true,
@@ -807,17 +808,23 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
// an unclean restart can leave 0-byte files under refs/t3/** that break every
// later fetch and push. Checkpoint writes flush before they are published;
// macOS defaults to writeout-only, which does not reach the disk either.
+ // Set fsync first: Git 2.36 ignores unknown batch without resetting the method.
+ // Git documents batch/fsync equivalence on macOS HFS+/APFS and Windows NTFS/ReFS;
+ // keep per-object fsync on other platforms. Batch still fully flushes refs.
// On macOS, VcsProcess retries unsupported full flushes with plain fsync.
const durableWrite = [
"-c",
"core.fsync=objects,reference",
"-c",
"core.fsyncMethod=fsync",
+ ...(hostPlatform === "darwin" || hostPlatform === "win32"
+ ? ["-c", "core.fsyncMethod=batch"]
+ : []),
] as const;
// Checkpoints must handle existing paths beyond Windows MAX_PATH, even when
// core.longpaths is disabled in the user's Git configuration.
- const longPaths = (yield* HostProcess.Platform) === "win32" ? ["-c", "core.longpaths=true"] : [];
+ const longPaths = hostPlatform === "win32" ? ["-c", "core.longpaths=true"] : [];
const checkpoints: VcsDriver.VcsCheckpointOps = {
captureCheckpoint: Effect.fn("GitVcsDriver.checkpoints.captureCheckpoint")(function* (input) {
diff --git a/apps/server/src/vcs/VcsProcess.test.ts b/apps/server/src/vcs/VcsProcess.test.ts
index e6de543c124..6617b15691d 100644
--- a/apps/server/src/vcs/VcsProcess.test.ts
+++ b/apps/server/src/vcs/VcsProcess.test.ts
@@ -184,8 +184,17 @@ describe("VcsProcess.run", () => {
stderr: "fatal: fsync error on '.git/objects/a5/tmp_obj_Pseg66': Operation not supported",
fsyncMethods: ["fsync"],
},
+ {
+ stderr: "fatal: fsync error on '.git/objects/bulk_fsync_Pseg66': Operation not supported",
+ fsyncMethods: ["batch", "writeout-only"],
+ },
+ {
+ stderr:
+ "fatal: update_ref failed for ref 'refs/t3/checkpoints/x': cannot update ref 'refs/t3/checkpoints/x': couldn't write '/Volumes/share/.git/refs/t3/checkpoints/x.lock'",
+ fsyncMethods: ["batch", "writeout-only"],
+ },
] as const)(
- "falls back to writeout-only only for unsupported fsync on macOS: $stderr",
+ "falls back to writeout-only only for unsupported flushes on macOS ($fsyncMethods): $stderr",
({ stderr, fsyncMethods, ...scenario }) =>
Effect.gen(function* () {
const seen: Array<ReadonlyArray<string>> = [];
@@ -199,7 +208,7 @@ describe("VcsProcess.run", () => {
Effect.sync(() => {
seen.push(input.args);
const failed =
- input.args.includes("core.fsyncMethod=fsync") ||
+ input.args.includes(`core.fsyncMethod=${fsyncMethods[0]}`) ||
("fallbackFails" in scenario && scenario.fallbackFails);
return {
stdout: "",
@@ -221,7 +230,14 @@ describe("VcsProcess.run", () => {
"operation" in scenario
? scenario.operation
: VcsProcess.CHECKPOINT_CAPTURE_OPERATION,
- args: ["-c", "core.fsync=objects,reference", "-c", "core.fsyncMethod=fsync", "add"],
+ args: [
+ "-c",
+ "core.fsync=objects,reference",
+ "-c",
+ ...(fsyncMethods[0] === "batch" ? ["core.fsyncMethod=fsync", "-c"] : []),
+ `core.fsyncMethod=${fsyncMethods[0]}`,
+ "add",
+ ],
})
.pipe(Effect.exit);
expect(seen).toEqual(
@@ -229,6 +245,9 @@ describe("VcsProcess.run", () => {
"-c",
"core.fsync=objects,reference",
"-c",
+ ...(fsyncMethods[0] === "batch"
+ ? [`core.fsyncMethod=${method === "batch" ? "fsync" : method}`, "-c"]
+ : []),
`core.fsyncMethod=${method}`,
"add",
]),
diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts
index 42ca8d28370..40823895077 100644
--- a/apps/server/src/vcs/VcsProcess.ts
+++ b/apps/server/src/vcs/VcsProcess.ts
@@ -172,6 +172,7 @@ const isTransientGitExit = (stderr: string) =>
// bare write error too; explicit unrelated errors remain failures.
// Linux writeout-only only starts writeback, so it must not use this fallback.
const FULL_FSYNC_ARG = "core.fsyncMethod=fsync";
+const BATCH_FSYNC_ARG = "core.fsyncMethod=batch";
const WRITEOUT_ONLY_FSYNC_ARG = "core.fsyncMethod=writeout-only";
const canRetryFullFsync = (stderr: string) =>
/fsync error on [^\r\n]+: operation not supported\r?$/im.test(stderr) ||
@@ -249,13 +250,15 @@ export const make = Effect.gen(function* () {
result.code !== null &&
result.code !== 0 &&
input.command === "git" &&
- input.args.includes(FULL_FSYNC_ARG) &&
+ (input.args.includes(FULL_FSYNC_ARG) || input.args.includes(BATCH_FSYNC_ARG)) &&
canRetryFullFsync(result.stderr)
) {
result = yield* spawn(
{
...input,
- args: input.args.map((arg) => (arg === FULL_FSYNC_ARG ? WRITEOUT_ONLY_FSYNC_ARG : arg)),
+ args: input.args.map((arg) =>
+ arg === FULL_FSYNC_ARG || arg === BATCH_FSYNC_ARG ? WRITEOUT_ONLY_FSYNC_ARG : arg,
+ ),
},
baseError,
);