Skip to content

Commit 62eb3c8

Browse files
committed
fix(aws-lambda): renders re-upload a site's expired files instead of failing
1 parent abb8f6b commit 62eb3c8

7 files changed

Lines changed: 146 additions & 25 deletions

File tree

‎packages/aws-lambda/src/s3Transport.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,28 @@ export async function putContentStoreFile(
287287
return outcome;
288288
}
289289

290+
/** Confirm an object the caller never uploaded is in the content store, restarting an aging one. */
291+
export async function ensureContentStoreObject(
292+
client: S3Client,
293+
bucket: string,
294+
sha256: string,
295+
sizeBytes: number,
296+
): Promise<void> {
297+
const uri = contentStoreUri(bucket, sha256);
298+
const { key } = parseS3Uri(uri);
299+
const existing = await inspectContentAddressedObject(client, bucket, key, sizeBytes, sha256);
300+
if (existing.state === "matching") {
301+
if (existing.lastModified) await refreshAgingObject(client, uri, sha256, existing.lastModified);
302+
return;
303+
}
304+
if (existing.state === "conflict") throwImmutableObjectConflict(uri);
305+
const error = new Error(
306+
`[s3Transport] PLAN_ARTIFACT_MISSING: ${uri} is not in the content store`,
307+
);
308+
error.name = "PLAN_ARTIFACT_MISSING";
309+
throw error;
310+
}
311+
290312
// S3 lifecycle expiry counts from the last write, not the last read, so reuse has to write.
291313
async function refreshAgingObject(
292314
client: S3Client,

‎packages/aws-lambda/src/sdk/__fixtures__/fakeS3.ts‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* `deploySite`'s `finally` block has rmSync'd it.
99
*/
1010

11+
import { Readable } from "node:stream";
1112
import { buffer } from "node:stream/consumers";
1213
import type { S3Client } from "@aws-sdk/client-s3";
1314

@@ -26,26 +27,30 @@ export class FakeS3 {
2627
async send(command: unknown): Promise<unknown> {
2728
const cmdName = (command as { constructor: { name: string } }).constructor.name;
2829
const input = (
29-
command as { input: { Bucket: string; Key: string; Metadata?: Record<string, string> } }
30+
command as {
31+
input: { Bucket: string; Key: string; Metadata?: Record<string, string>; Body?: unknown };
32+
}
3033
).input;
3134
const id = `${input.Bucket}/${input.Key}`;
32-
if (cmdName === "HeadObjectCommand") {
33-
this.ops.push({ kind: "head", bucket: input.Bucket, key: input.Key });
34-
return this.head(id);
35-
}
36-
if (cmdName === "PutObjectCommand") {
37-
this.ops.push({ kind: "put", bucket: input.Bucket, key: input.Key });
38-
this.existing.add(id);
39-
this.metadata.set(id, input.Metadata);
40-
this.bodies.set(
41-
id,
42-
await drainBody(
43-
(command as { input: { Body: NodeJS.ReadableStream | Buffer } }).input.Body,
44-
),
45-
);
46-
return {};
47-
}
48-
throw new Error(`FakeS3: unexpected command ${cmdName}`);
35+
const handlers: Record<string, () => Promise<unknown>> = {
36+
HeadObjectCommand: async () => {
37+
this.ops.push({ kind: "head", bucket: input.Bucket, key: input.Key });
38+
return this.head(id);
39+
},
40+
GetObjectCommand: async () => ({
41+
Body: Readable.from([this.bodies.get(id) ?? Buffer.alloc(0)]),
42+
}),
43+
PutObjectCommand: async () => {
44+
this.ops.push({ kind: "put", bucket: input.Bucket, key: input.Key });
45+
this.existing.add(id);
46+
this.metadata.set(id, input.Metadata);
47+
this.bodies.set(id, await drainBody(input.Body as NodeJS.ReadableStream | Buffer));
48+
return {};
49+
},
50+
};
51+
const handler = handlers[cmdName];
52+
if (!handler) throw new Error(`FakeS3: unexpected command ${cmdName}`);
53+
return handler();
4954
}
5055

5156
private head(id: string): unknown {

‎packages/aws-lambda/src/sdk/deploySite.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,28 @@
1111
* where `siteId` is derived from a SHA-256 over the project files.
1212
*/
1313

14-
import { mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
14+
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
1515
import { tmpdir } from "node:os";
1616
import { join } from "node:path";
1717
import { S3Client } from "@aws-sdk/client-s3";
18+
import { CONTENT_STORE_RETENTION_DAYS } from "../contentStore.js";
1819
import { hashProjectDir, listSiteFiles } from "@hyperframes/producer/distributed";
1920
import {
21+
downloadS3ObjectToFile,
22+
ensureContentStoreObject,
2023
mapConcurrent,
24+
parseS3Uri,
2125
putContentStoreFile,
2226
sha256File,
2327
uploadContentAddressedFileToS3,
2428
} from "../s3Transport.js";
25-
import { serializeSiteManifest, siteManifestUri, type SiteFile } from "../siteManifest.js";
29+
import {
30+
isSiteManifestUri,
31+
parseSiteManifest,
32+
serializeSiteManifest,
33+
siteManifestUri,
34+
type SiteFile,
35+
} from "../siteManifest.js";
2636

2737
/** Options for {@link deploySite}. */
2838
export interface DeploySiteOptions {
@@ -64,6 +74,43 @@ export function siteHandleFromId(bucketName: string, siteId: string): SiteHandle
6474
return { siteId, bucketName, projectS3Uri, bytes: 0, uploadedAt: "", uploaded: false };
6575
}
6676

77+
/** Before a render reuses `site`, re-store any file the content store expired, from `projectDir`. */
78+
export async function ensureSiteStored(
79+
site: SiteHandle,
80+
s3: S3Client,
81+
projectDir?: string,
82+
): Promise<void> {
83+
if (!isSiteManifestUri(site.projectS3Uri)) return;
84+
const { bucket } = parseS3Uri(site.projectS3Uri);
85+
const workdir = mkdtempSync(join(tmpdir(), "hf-site-check-"));
86+
try {
87+
const manifestPath = join(workdir, "files.json");
88+
await downloadS3ObjectToFile(s3, site.projectS3Uri, manifestPath);
89+
const files = parseSiteManifest(readFileSync(manifestPath, "utf-8"));
90+
const missing: string[] = [];
91+
await mapConcurrent(files, 16, async ({ path, sha256, sizeBytes }) => {
92+
try {
93+
await ensureContentStoreObject(s3, bucket, sha256, sizeBytes);
94+
} catch (error) {
95+
if ((error as Error).name !== "PLAN_ARTIFACT_MISSING") throw error;
96+
if (!projectDir) return void missing.push(path);
97+
await putContentStoreFile(s3, join(projectDir, path), bucket, sha256);
98+
}
99+
});
100+
if (missing.length > 0) {
101+
const error = new Error(
102+
`[deploySite] SITE_FILES_MISSING: ${missing.length} of ${files.length} files of site ` +
103+
`${site.siteId} are no longer stored (unused for ${CONTENT_STORE_RETENTION_DAYS} days). ` +
104+
"Run `hyperframes lambda sites create`, or pass projectDir to re-upload them.",
105+
);
106+
error.name = "SITE_FILES_MISSING";
107+
throw error;
108+
}
109+
} finally {
110+
rmSync(workdir, { recursive: true, force: true });
111+
}
112+
}
113+
67114
/** Upload `projectDir`'s files to the content store and write the site manifest. */
68115
export async function deploySite(opts: DeploySiteOptions): Promise<SiteHandle> {
69116
const s3 = opts.s3 ?? new S3Client({ region: opts.region });

‎packages/aws-lambda/src/sdk/renderToLambda.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from "node:path";
55
import type { SFNClient } from "@aws-sdk/client-sfn";
66
import type { SerializableDistributedRenderConfig } from "../events.js";
77
import { asS3Client, FakeS3 } from "./__fixtures__/fakeS3.js";
8-
import type { SiteHandle } from "./deploySite.js";
8+
import { deploySite, type SiteHandle } from "./deploySite.js";
99
import { renderToLambda } from "./renderToLambda.js";
1010
import { InvalidConfigError } from "./validateConfig.js";
1111

@@ -163,6 +163,48 @@ describe("renderToLambda", () => {
163163
expect(s3.existing.size).toBe(0);
164164
});
165165

166+
describe("with a site whose files expired from the content store", () => {
167+
async function deployThenExpire(s3: FakeS3): Promise<{ site: SiteHandle; expiredKey: string }> {
168+
const site = await deploySite({ projectDir, bucketName: "test-bucket", s3: asS3Client(s3) });
169+
const expiredKey = [...s3.existing].find((id) => id.includes("/cas/sha256/"))!;
170+
s3.existing.delete(expiredKey);
171+
s3.ops.length = 0;
172+
return { site, expiredKey };
173+
}
174+
const renderWith = (s3: FakeS3, sfn: FakeSFN, site: SiteHandle, dir?: string) =>
175+
renderToLambda({
176+
siteHandle: site,
177+
projectDir: dir,
178+
bucketName: "test-bucket",
179+
stateMachineArn: "arn:aws:states:us-east-1:1234:stateMachine:hf",
180+
config: baseConfig,
181+
sfn: asSFNClient(sfn),
182+
s3: asS3Client(s3),
183+
});
184+
185+
it("re-uploads the expired file from the project dir, then renders", async () => {
186+
const s3 = new FakeS3();
187+
const sfn = new FakeSFN();
188+
const { site, expiredKey } = await deployThenExpire(s3);
189+
await renderWith(s3, sfn, site, projectDir);
190+
expect(
191+
s3.ops.filter((op) => op.kind === "put").map((op) => `${op.bucket}/${op.key}`),
192+
).toEqual([expiredKey]);
193+
expect(sfn.starts).toHaveLength(1);
194+
});
195+
196+
it("without a project dir, refuses with the missing-file count before starting", async () => {
197+
const s3 = new FakeS3();
198+
const sfn = new FakeSFN();
199+
const { site } = await deployThenExpire(s3);
200+
await expect(renderWith(s3, sfn, site)).rejects.toMatchObject({
201+
name: "SITE_FILES_MISSING",
202+
message: expect.stringContaining("1 of 1 files"),
203+
});
204+
expect(sfn.starts).toHaveLength(0);
205+
});
206+
});
207+
166208
it("rejects invalid configs synchronously before any AWS call", async () => {
167209
const sfn = new FakeSFN();
168210
const s3 = new FakeS3();

‎packages/aws-lambda/src/sdk/renderToLambda.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919

2020
import { randomUUID } from "node:crypto";
2121
import { SFNClient, StartExecutionCommand } from "@aws-sdk/client-sfn";
22-
import type { S3Client } from "@aws-sdk/client-s3";
22+
import { S3Client } from "@aws-sdk/client-s3";
2323
import type { LambdaPlanProtocol, SerializableDistributedRenderConfig } from "../events.js";
2424
import { formatExtension } from "../formatExtension.js";
2525
import { formatS3Uri } from "../s3Transport.js";
26-
import { deploySite, type SiteHandle } from "./deploySite.js";
26+
import { deploySite, ensureSiteStored, type SiteHandle } from "./deploySite.js";
2727
import {
2828
validateDistributedRenderConfig,
2929
validateStepFunctionsInputSize,
3030
} from "./validateConfig.js";
3131

3232
/** Options for {@link renderToLambda}. */
3333
export interface RenderToLambdaOptions {
34-
/** Local project directory. Required when `siteHandle` is not supplied. */
34+
/** Local project directory. Required without `siteHandle`; with one, re-uploads expired files. */
3535
projectDir?: string;
3636
/** Re-use an existing `deploySite` upload (skips tar+S3 PUT). */
3737
siteHandle?: SiteHandle;
@@ -101,6 +101,10 @@ export async function renderToLambda(opts: RenderToLambdaOptions): Promise<Rende
101101
});
102102
const outputS3Uri = formatS3Uri({ bucket: opts.bucketName, key: outputKey });
103103

104+
if (opts.siteHandle) {
105+
const s3 = opts.s3 ?? new S3Client({ region: opts.region });
106+
await ensureSiteStored(opts.siteHandle, s3, opts.projectDir);
107+
}
104108
const site =
105109
opts.siteHandle ??
106110
(await deploySite({

‎packages/cli/src/commands/lambda/render-batch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ export async function runRenderBatch(args: RenderBatchArgs): Promise<void> {
271271
}
272272
try {
273273
const handle = await renderToLambda({
274+
projectDir,
274275
siteHandle,
275276
bucketName: stack.bucketName,
276277
stateMachineArn: stack.stateMachineArn,

‎packages/cli/src/commands/lambda/render.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export async function runRender(args: RenderArgs): Promise<void> {
131131
const siteHandle = args.siteId ? siteHandleFromId(stack.bucketName, args.siteId) : undefined;
132132

133133
const handle = await renderToLambda({
134-
projectDir: siteHandle ? undefined : projectDir,
134+
projectDir,
135135
siteHandle,
136136
bucketName: stack.bucketName,
137137
stateMachineArn: stack.stateMachineArn,

0 commit comments

Comments
 (0)