Skip to content

Commit 592bf90

Browse files
authored
perf(workers): cut repeated full-tree workspace hashing (#121365)
* perf(workers): reuse workspace hashes during reconcile * chore(workers): drop one-off sync benchmark harness
1 parent 3740070 commit 592bf90

17 files changed

Lines changed: 1184 additions & 428 deletions

‎src/gateway/worker-environments/workspace-accepted-sync.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import {
1717
isAcceptedWorkspacePublicationIndeterminateError,
1818
} from "./workspace-accepted-publication.js";
1919
import {
20-
createAcceptedWorkspacePublisherFactory,
20+
createAcceptedWorkspacePublisherFactory as createAcceptedWorkspacePublisherFactoryRaw,
2121
recoverAcceptedWorkspacePublication,
2222
} from "./workspace-accepted-sync.js";
23+
import { createWorkspaceReconcileMetrics } from "./workspace-hash-memo.js";
2324
import {
2425
serializeWorkerWorkspaceManifest,
2526
type WorkerWorkspaceManifest,
@@ -69,6 +70,40 @@ function settlement(outcome: "begun" | "rolled-back" | "applied" | "committed"):
6970
return result({ stdout: `${JSON.stringify({ version: 1, outcome })}\n` });
7071
}
7172

73+
function createAcceptedWorkspacePublisherFactory(
74+
params: Omit<
75+
Parameters<typeof createAcceptedWorkspacePublisherFactoryRaw>[0],
76+
"hashMemo" | "metrics"
77+
>,
78+
) {
79+
const runWorkspaceCommand = params.runWorkspaceCommand;
80+
return createAcceptedWorkspacePublisherFactoryRaw({
81+
...params,
82+
hashMemo: new Map(),
83+
metrics: createWorkspaceReconcileMetrics(),
84+
runWorkspaceCommand: async (command) => {
85+
const response = await runWorkspaceCommand(command);
86+
const returnedRef = response.stdout.trim();
87+
if (command.argv.at(-1) !== "memo-v1" || !/^sha256:[a-f0-9]{64}$/u.test(returnedRef)) {
88+
return response;
89+
}
90+
return result({
91+
stdout: `${JSON.stringify({
92+
version: 1,
93+
manifestRef: returnedRef,
94+
memo: [],
95+
metrics: {
96+
contentHashCount: 0,
97+
contentHashDurationMs: 0,
98+
memoHitCount: 0,
99+
totalDurationMs: 0,
100+
},
101+
})}\n`,
102+
});
103+
},
104+
});
105+
}
106+
72107
describe("accepted workspace publication", () => {
73108
it.skipIf(process.platform === "win32")(
74109
"waits for the staging receiver group before promoting its inodes live",

‎src/gateway/worker-environments/workspace-accepted-sync.ts‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import {
1010
parseAcceptedWorkspaceSettlement,
1111
type AcceptedWorkspaceSettlementOutcome,
1212
} from "./workspace-accepted-publication.js";
13+
import type { WorkspaceHashMemo, WorkspaceReconcileMetrics } from "./workspace-hash-memo.js";
1314
import {
1415
serializeWorkerWorkspaceManifest,
1516
type WorkerWorkspaceManifest,
1617
} from "./workspace-manifest.js";
1718
import { changedPaths, manifestNodes } from "./workspace-reconcile.js";
1819
import {
19-
parseManifestRef,
20+
captureRemoteWorkspaceManifest,
2021
WORKER_WORKSPACE_RSYNC_DESTINATION,
2122
workerAcceptedWorkspaceRsyncReceiverPath,
2223
workerWorkspaceCommandSucceeded,
@@ -67,6 +68,8 @@ function createAcceptedWorkspacePublisher(params: {
6768
localPath: string;
6869
remoteWorkspaceDir: string;
6970
remoteManifest: WorkerWorkspaceManifest;
71+
hashMemo: WorkspaceHashMemo;
72+
metrics: WorkspaceReconcileMetrics;
7073
}) {
7174
return async (accepted: {
7275
manifestRef: string;
@@ -96,21 +99,14 @@ function createAcceptedWorkspacePublisher(params: {
9699
}
97100

98101
const verifyAcceptedWorkspace = async () => {
99-
const verified = await params.runWorkspaceCommand({
100-
transportRetry: "idempotent",
101-
argv: [
102-
"node",
103-
"-e",
104-
REMOTE_WORKSPACE_MANIFEST_JS,
105-
params.remoteWorkspaceDir,
106-
accepted.manifest.baseCommit ?? "",
107-
...(accepted.manifest.baseCommit ? ["eligible", acceptedDigest] : []),
108-
],
102+
const verifiedRef = await captureRemoteWorkspaceManifest({
103+
runWorkspaceCommand: params.runWorkspaceCommand,
104+
remoteWorkspaceDir: params.remoteWorkspaceDir,
105+
baseCommit: accepted.manifest.baseCommit,
106+
priorManifestDigests: accepted.manifest.baseCommit ? [acceptedDigest] : [],
107+
hashMemo: params.hashMemo,
108+
metrics: params.metrics,
109109
});
110-
if (!workerWorkspaceCommandSucceeded(verified)) {
111-
throw workspaceSyncError(verified);
112-
}
113-
const verifiedRef = parseManifestRef(verified.stdout.trim());
114110
if (verifiedRef !== accepted.manifestRef) {
115111
throw new Error(
116112
`Worker workspace does not match its accepted manifest: expected ${accepted.manifestRef}, got ${verifiedRef}`,

‎src/gateway/worker-environments/workspace-finalize.test.ts‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import { describe, expect, it, vi } from "vitest";
2-
import { verifyReconciledWorkspaceFinal } from "./workspace-finalize.js";
2+
import {
3+
registerWorkspaceReconcileReporter,
4+
verifyReconciledWorkspaceFinal,
5+
} from "./workspace-finalize.js";
36

47
describe("final worker workspace fences", () => {
58
it("rechecks remote and local stability after the final quiescence renewal", async () => {
69
const log: string[] = [];
7-
await verifyReconciledWorkspaceFinal(
8-
{
9-
manifestRef: "sha256:" + "a".repeat(64),
10-
changed: true,
11-
verifyStable: async () => {
12-
log.push("remote");
13-
},
14-
verifyLocalStable: async () => {
15-
log.push("local");
16-
},
10+
const reconciliation = {
11+
manifestRef: "sha256:" + "a".repeat(64),
12+
changed: true,
13+
verifyStable: async () => {
14+
log.push("remote");
1715
},
18-
{
19-
assertActive: async () => {
20-
log.push("quiescence");
21-
},
22-
resume: async () => {},
16+
verifyLocalStable: async () => {
17+
log.push("local");
2318
},
24-
);
19+
};
20+
const outcomes: string[] = [];
21+
registerWorkspaceReconcileReporter(reconciliation, (outcome) => outcomes.push(outcome));
22+
await verifyReconciledWorkspaceFinal(reconciliation, {
23+
assertActive: async () => {
24+
log.push("quiescence");
25+
},
26+
resume: async () => {},
27+
});
2528

2629
expect(log).toEqual(["remote", "local", "quiescence", "remote", "local"]);
30+
expect(outcomes).toEqual(["succeeded"]);
2731
});
2832

2933
it("rejects a remote write observed after the final quiescence renewal", async () => {

‎src/gateway/worker-environments/workspace-finalize.ts‎

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,74 @@ const runRetryableFinalFenceStep = async (operation: () => Promise<void>): Promi
3131
const runResultPreservingFinalFenceStep = async (operation: () => Promise<void>): Promise<void> =>
3232
await runFinalFenceStep(operation, "preserve-result");
3333

34+
type WorkspaceReconcileOutcome = "failed" | "succeeded";
35+
36+
const workspaceReconcileReporters = new WeakMap<
37+
WorkerWorkspaceReconcileResult,
38+
(outcome: WorkspaceReconcileOutcome) => void
39+
>();
40+
41+
export function registerWorkspaceReconcileReporter(
42+
reconciliation: WorkerWorkspaceReconcileResult,
43+
reporter: (outcome: WorkspaceReconcileOutcome) => void,
44+
): void {
45+
workspaceReconcileReporters.set(reconciliation, reporter);
46+
}
47+
48+
function reportWorkspaceReconcile(
49+
reconciliation: WorkerWorkspaceReconcileResult,
50+
outcome: WorkspaceReconcileOutcome,
51+
): void {
52+
const reporter = workspaceReconcileReporters.get(reconciliation);
53+
workspaceReconcileReporters.delete(reconciliation);
54+
reporter?.(outcome);
55+
}
56+
3457
/** Rechecks both owners after renewing the remote quiescence lease. */
3558
export async function verifyReconciledWorkspaceFinal(
3659
reconciliation: WorkerWorkspaceReconcileResult,
3760
quiescence: WorkerWorkspaceQuiescence,
3861
): Promise<WorkerWorkspaceApplyResult | undefined> {
39-
if (reconciliation.applyPreparedStagedResult && reconciliation.publishStagedResult) {
40-
try {
41-
// Fence the prepared remote capture before quiescence renewal can enroll late writers.
42-
await runRetryableFinalFenceStep(async () => await reconciliation.verifyStable());
43-
// Renew quiescence and freeze any writers that appeared after the prepared capture.
44-
await runRetryableFinalFenceStep(async () => await quiescence.assertActive());
45-
// Keep this fence: a late writer can mutate before renewal enrolls and SIGSTOPs it.
46-
await runRetryableFinalFenceStep(async () => await reconciliation.verifyStable());
47-
await reconciliation.applyPreparedStagedResult();
48-
await reconciliation.verifyLocalStable();
49-
// Renew after apply so lease expiry cannot race the final publish gate.
50-
await runResultPreservingFinalFenceStep(async () => await quiescence.assertActive());
51-
// Recheck the remote owner after apply before publishing the prepared result.
52-
await runResultPreservingFinalFenceStep(async () => await reconciliation.verifyStable());
53-
await runResultPreservingFinalFenceStep(async () => await reconciliation.verifyLocalStable());
54-
await reconciliation.publishStagedResult();
55-
return reconciliation.getAppliedWorkspaceResult?.();
56-
} catch (error) {
57-
await reconciliation.discardPreparedStagedResult?.().catch(() => undefined);
58-
throw error;
62+
let succeeded = false;
63+
try {
64+
if (reconciliation.applyPreparedStagedResult && reconciliation.publishStagedResult) {
65+
try {
66+
// Fence the prepared remote capture before quiescence renewal can enroll late writers.
67+
await runRetryableFinalFenceStep(async () => await reconciliation.verifyStable());
68+
// Renew quiescence and freeze any writers that appeared after the prepared capture.
69+
await runRetryableFinalFenceStep(async () => await quiescence.assertActive());
70+
// Keep this fence: a late writer can mutate before renewal enrolls and SIGSTOPs it.
71+
await runRetryableFinalFenceStep(async () => await reconciliation.verifyStable());
72+
await reconciliation.applyPreparedStagedResult();
73+
await reconciliation.verifyLocalStable();
74+
// Renew after apply so lease expiry cannot race the final publish gate.
75+
await runResultPreservingFinalFenceStep(async () => await quiescence.assertActive());
76+
// Recheck the remote owner after apply before publishing the prepared result.
77+
await runResultPreservingFinalFenceStep(async () => await reconciliation.verifyStable());
78+
await runResultPreservingFinalFenceStep(
79+
async () => await reconciliation.verifyLocalStable(),
80+
);
81+
await reconciliation.publishStagedResult();
82+
const applied = reconciliation.getAppliedWorkspaceResult?.();
83+
succeeded = true;
84+
return applied;
85+
} catch (error) {
86+
await reconciliation.discardPreparedStagedResult?.().catch(() => undefined);
87+
throw error;
88+
}
5989
}
90+
const runFenceStep = reconciliation.changed
91+
? runResultPreservingFinalFenceStep
92+
: runRetryableFinalFenceStep;
93+
await runFenceStep(async () => await reconciliation.verifyStable());
94+
await runFenceStep(async () => await reconciliation.verifyLocalStable());
95+
await runFenceStep(async () => await quiescence.assertActive());
96+
await runFenceStep(async () => await reconciliation.verifyStable());
97+
await runFenceStep(async () => await reconciliation.verifyLocalStable());
98+
const applied = reconciliation.getAppliedWorkspaceResult?.();
99+
succeeded = true;
100+
return applied;
101+
} finally {
102+
reportWorkspaceReconcile(reconciliation, succeeded ? "succeeded" : "failed");
60103
}
61-
const runFenceStep = reconciliation.changed
62-
? runResultPreservingFinalFenceStep
63-
: runRetryableFinalFenceStep;
64-
await runFenceStep(async () => await reconciliation.verifyStable());
65-
await runFenceStep(async () => await reconciliation.verifyLocalStable());
66-
await runFenceStep(async () => await quiescence.assertActive());
67-
await runFenceStep(async () => await reconciliation.verifyStable());
68-
await runFenceStep(async () => await reconciliation.verifyLocalStable());
69-
return reconciliation.getAppliedWorkspaceResult?.();
70104
}

0 commit comments

Comments
 (0)