Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(web): commit desktop sources from Cap's bucket in 256-object pages and keep their S3 connections

Richie McIlroy4131bd119444
diff --git a/apps/web/__tests__/unit/desktop-recording-source.test.ts b/apps/web/__tests__/unit/desktop-recording-source.test.ts
index 2172d261b..179c662f3 100644
--- a/apps/web/__tests__/unit/desktop-recording-source.test.ts
+++ b/apps/web/__tests__/unit/desktop-recording-source.test.ts
@@ -1399,7 +1399,7 @@ describe("large desktop recording snapshots", () => {
});
it.each([
- ["s3", 128],
+ ["s3", 256],
["googleDrive", 16],
] as const)(
"resumes a completed %s copy when the database checkpoint write was lost",
@@ -1407,7 +1407,7 @@ describe("large desktop recording snapshots", () => {
const storage = storageFixture(provider);
seedSegments(
storage,
- Array.from({ length: 129 }, (_, index) => index + 1),
+ Array.from({ length: 299 }, (_, index) => index + 1),
false,
);
const video = recording();
@@ -1432,8 +1432,8 @@ describe("large desktop recording snapshots", () => {
expect(replay.checkpoint.snapshotId).toBe(checkpoint.snapshotId);
const source = await finishSnapshot(video, replay.checkpoint);
const urls = await buildDesktopRecordingSourceUrls(video, source);
- expect(urls.sourceObjects).toHaveLength(130);
- expect(storage.bucket.copyObject).toHaveBeenCalledTimes(130);
+ expect(urls.sourceObjects).toHaveLength(300);
+ expect(storage.bucket.copyObject).toHaveBeenCalledTimes(300);
expect(JSON.stringify(replay.checkpoint).length).toBeLessThan(5_000);
},
);
@@ -1662,11 +1662,15 @@ describe("large desktop recording snapshots", () => {
describe("source commit page size", () => {
const segmentCount = 130;
- function pagedFixture(provider = "s3", customBucket = false) {
+ function pagedFixture(
+ provider = "s3",
+ customBucket = false,
+ segments = segmentCount,
+ ) {
const storage = storageFixture(provider, customBucket);
seedSegments(
storage,
- Array.from({ length: segmentCount }, (_, index) => index + 1),
+ Array.from({ length: segments }, (_, index) => index + 1),
);
return storage;
}
@@ -1771,11 +1775,11 @@ describe("source commit page size", () => {
};
}
- it("uses 128-entry pages for Cap's bucket and keeps 16 elsewhere, both within the leaf schema", async () => {
+ it("uses 256-entry pages for Cap's bucket and keeps 16 elsewhere, both within the leaf schema", async () => {
const actual = await vi.importActual<
typeof import("@/lib/desktop-recording-source-checkpoint")
>("@/lib/desktop-recording-source-checkpoint");
- expect(actual.SOURCE_COMMIT_AWS_PAGE_SIZE).toBe(128);
+ expect(actual.SOURCE_COMMIT_AWS_PAGE_SIZE).toBe(256);
expect(actual.SOURCE_COMMIT_PAGE_SIZE).toBe(16);
expect(actual.SOURCE_COMMIT_AWS_PAGE_SIZE).toBeLessThanOrEqual(
actual.SOURCE_COMMIT_TREE_PAGE_SIZE,
@@ -1785,10 +1789,11 @@ describe("source commit page size", () => {
);
});
- it("resumes a commit saved with larger tree pages using today's 128-entry writer, as after a rollback", async () => {
+ it("resumes a commit saved with 256-entry pages using the previous 128-entry writer, as after a rollback", async () => {
+ mocks.treePageSize = 128;
const reference = await referenceOutput(128);
+ mocks.treePageSize = undefined;
const storage = pagedFixture();
- mocks.treePageSize = 256;
const partial = await advanceWithPageSize(
256,
snapshotCheckpoint(),
@@ -1796,8 +1801,9 @@ describe("source commit page size", () => {
);
expect(partial.source).toBeUndefined();
expect(partial.checkpoint.phase).toBe("verify");
- mocks.treePageSize = undefined;
+ mocks.treePageSize = 128;
const resumed = await advanceWithPageSize(128, partial.checkpoint);
+ mocks.treePageSize = undefined;
if (!resumed.source) throw new Error("Resumed commit did not complete");
expect(await snapshotOutput(storage, resumed.source)).toEqual(
reference.output,
@@ -1909,13 +1915,13 @@ describe("source commit page size", () => {
}
it.each([
- ["Cap's AWS bucket", "s3", false, 128],
+ ["Cap's AWS bucket", "s3", false, 256],
["a custom S3-compatible bucket", "s3", true, 8],
["Google Drive", "googleDrive", false, 8],
] as const)(
"bounds parallel storage requests for %s",
async (_, provider, customBucket, expected) => {
- const storage = pagedFixture(provider, customBucket);
+ const storage = pagedFixture(provider, customBucket, 300);
const active = { head: 0, copy: 0 };
const peak = { head: 0, copy: 0 };
const track = <A, E>(
@@ -1960,7 +1966,7 @@ describe("source commit page size", () => {
// bound is on objects in flight; requests peak at two per object.
expect(peak.head).toBeLessThanOrEqual(expected * 2);
expect(readInventory(storage, source.inventoryKey).objects).toHaveLength(
- segmentCount + 3,
+ 300 + 3,
);
},
);
diff --git a/apps/web/__tests__/unit/s3-bucket-connections.test.ts b/apps/web/__tests__/unit/s3-bucket-connections.test.ts
index df73c43bc..f2d1f5580 100644
--- a/apps/web/__tests__/unit/s3-bucket-connections.test.ts
+++ b/apps/web/__tests__/unit/s3-bucket-connections.test.ts
@@ -100,24 +100,26 @@ async function storageFixture() {
}
describe("S3 connection reuse", () => {
- it("bounds connections across simultaneous recording checkpoints", async () => {
+ it("reuses one bounded set of connections across recording checkpoints", async () => {
const fixture = await storageFixture();
try {
- await Promise.all(
- Array.from({ length: 24 }, async (_, recording) => {
- const [access] = await fixture.runtime.runPromise(
- fixture.service.getBucketAccess(Option.none()),
- );
- await Promise.all(
- Array.from({ length: 8 }, (_, index) =>
- fixture.runtime.runPromise(
- access.headObject(`recording/${recording}/${index}`),
+ // Two waves of checkpoints: the second must reuse the first wave's sockets instead of opening its own.
+ for (let wave = 0; wave < 2; wave++)
+ await Promise.all(
+ Array.from({ length: 16 }, async (_, recording) => {
+ const [access] = await fixture.runtime.runPromise(
+ fixture.service.getBucketAccess(Option.none()),
+ );
+ await Promise.all(
+ Array.from({ length: 8 }, (_, index) =>
+ fixture.runtime.runPromise(
+ access.headObject(`recording/${wave}/${recording}/${index}`),
+ ),
),
- ),
- );
- }),
- );
- expect(fixture.authorizations).toHaveLength(192);
+ );
+ }),
+ );
+ expect(fixture.authorizations).toHaveLength(256);
expect(fixture.connections()).toBeLessThanOrEqual(128);
} finally {
await fixture.close();
@@ -225,9 +227,9 @@ describe("S3 connection reuse", () => {
),
);
expect(pools.map((pool) => pool.httpsAgent.maxFreeSockets)).toEqual([
- 8, 32,
+ 8, 256,
]);
- expect(pools.map((pool) => pool.httpsAgent.maxSockets)).toEqual([50, 128]);
+ expect(pools.map((pool) => pool.httpsAgent.maxSockets)).toEqual([50, 256]);
});
it("reuses custom bucket connections while using current credentials", async () => {
diff --git a/apps/web/lib/desktop-recording-source-checkpoint.ts b/apps/web/lib/desktop-recording-source-checkpoint.ts
index ec741b9b2..a91369213 100644
--- a/apps/web/lib/desktop-recording-source-checkpoint.ts
+++ b/apps/web/lib/desktop-recording-source-checkpoint.ts
@@ -1,13 +1,13 @@
import { z } from "zod";
export const SOURCE_COMMIT_PAGE_SIZE = 16;
-// Cap's own AWS bucket answers fast enough to finish 8x larger pages well inside a step's deadline.
-export const SOURCE_COMMIT_AWS_PAGE_SIZE = 128;
+// Cap's own AWS bucket answers fast enough to finish 16x larger pages well inside a step's deadline.
+export const SOURCE_COMMIT_AWS_PAGE_SIZE = 256;
export const SOURCE_COMMIT_PART_BATCH_SIZE = 4;
export const SOURCE_COMMIT_PART_PAGE_SIZE = 64;
// Readers accept larger tree pages than writers produce, so a deployment can be rolled back while commits written by a newer one are in flight.
export const SOURCE_COMMIT_MAX_TREE_PAGE_ENTRIES = 256;
-export const SOURCE_COMMIT_TREE_PAGE_SIZE = 128;
+export const SOURCE_COMMIT_TREE_PAGE_SIZE = 256;
export const SOURCE_COMMIT_MAX_OBJECTS = 100_002;
const sha256 = z.string().regex(/^[a-f0-9]{64}$/);
diff --git a/apps/web/lib/desktop-recording-source.ts b/apps/web/lib/desktop-recording-source.ts
index 3492c7247..3d96ee99f 100644
--- a/apps/web/lib/desktop-recording-source.ts
+++ b/apps/web/lib/desktop-recording-source.ts
@@ -266,7 +266,7 @@ const committedInventorySchema = z.union([
const SOURCE_COMMIT_STEP_TIMEOUT_MS = 180_000;
const SOURCE_COMMIT_IO_TIMEOUT_MS = 60_000;
const SOURCE_COMMIT_CONCURRENCY = 8;
-const SOURCE_COMMIT_AWS_CONCURRENCY = 128;
+const SOURCE_COMMIT_AWS_CONCURRENCY = 256;
function sourceLimits([bucket, customBucket]: RecordingStorageAccess) {
return bucket.provider === "s3" && Option.isNone(customBucket)
diff --git a/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts b/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts
index d63722a9b..cedeb9edd 100644
--- a/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts
+++ b/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts
@@ -42,5 +42,6 @@ const makeS3ConnectionPool = (maxFreeSockets: number, maxSockets = 50) =>
);
export const s3ConnectionPool = makeS3ConnectionPool(8);
-// Desktop source commits copy 128 objects at once in Cap's own bucket.
-export const defaultBucketS3ConnectionPool = makeS3ConnectionPool(32, 128);
+// Desktop source commits copy 256 objects at once in Cap's own bucket, page after page; keeping those
+// sockets between pages (they still expire after 30 s idle) avoids re-opening TLS to S3 for every page.
+export const defaultBucketS3ConnectionPool = makeS3ConnectionPool(256, 256);