Open source CapCut alternative.
TypeScript
fix(cut): account for artifact retention and retry storage conflicts [rebuild]
David Le208d163bd87f
diff --git a/site/src/cut/lib/artifactPolicy.test.ts b/site/src/cut/lib/artifactPolicy.test.tsnew file mode 100644index 00000000..6969e464--- /dev/null+++ b/site/src/cut/lib/artifactPolicy.test.ts@@ -0,0 +1,15 @@+import { expect, test } from "bun:test";+import { artifactLifecycle, artifactUsageBytes } from "./artifactPolicy";++test("derived playback and scratch are distinct from retained media", () => {+ for (const kind of ["preview", "card", "hls"]) expect(artifactLifecycle(kind)).toBe("derived");+ expect(artifactLifecycle("overlay")).toBe("scratch");+ for (const kind of ["media", "export", "library"]) expect(artifactLifecycle(kind)).toBe("retained");+});++test("converting a charged preview to a derived artifact refunds its previous bytes once", () => {+ const bytes = BigInt(100);+ expect(artifactUsageBytes(bytes, true, true) - artifactUsageBytes(bytes, true, false)).toBe(-bytes);+ expect(artifactUsageBytes(bytes, true, true) - artifactUsageBytes(bytes, true, true)).toBe(BigInt(0));+ expect(artifactUsageBytes(bytes, false, false)).toBe(BigInt(0));+});diff --git a/site/src/cut/lib/artifactPolicy.ts b/site/src/cut/lib/artifactPolicy.tsnew file mode 100644index 00000000..352857e1--- /dev/null+++ b/site/src/cut/lib/artifactPolicy.ts@@ -0,0 +1,12 @@+export type ArtifactLifecycle = "retained" | "derived" | "scratch";++/** Derived playback files can be rebuilt from retained project media. */+export function artifactLifecycle(kind: string): ArtifactLifecycle {+ if (kind === "preview" || kind === "card" || kind === "hls") return "derived";+ if (kind === "overlay") return "scratch";+ return "retained";+}++export function artifactUsageBytes(bytes: bigint, complete: boolean, quotaExempt: boolean): bigint {+ return complete && !quotaExempt ? bytes : BigInt(0);+}diff --git a/site/src/cut/server/cloud/gc.ts b/site/src/cut/server/cloud/gc.tsindex 8e69cfa4..edd58655 100644--- a/site/src/cut/server/cloud/gc.ts+++ b/site/src/cut/server/cloud/gc.ts@@ -268,6 +268,22 @@ async function sweepStaleOverlays(): Promise<number> {}export async function runGc(): Promise<Response> {+ // Existing derived files adopt the same accounting as newly rendered ones.+ const chargedDerived = await prisma.cutMediaObject.findMany({+ where: { kind: { in: ["preview", "card"] }, quotaExempt: false, uploadState: "complete" },+ take: RECLAIM_SCAN_LIMIT,+ select: { id: true, userId: true, bytes: true },+ });+ for (const object of chargedDerived) {+ await prisma.$transaction(async (tx) => {+ const changed = await tx.cutMediaObject.updateMany({+ where: { id: object.id, quotaExempt: false, bytes: object.bytes },+ data: { quotaExempt: true },+ });+ if (changed.count) await addUsage(tx, object.userId, -Number(object.bytes));+ });+ }+const pending = await prisma.cutMediaObject.findMany({where: {uploadState: "pending",@@ -316,6 +332,22 @@ export async function runGc(): Promise<Response> {return [p.id, new Set(assets.map((a) => a.fileName))];}));+ const activeRenders = await prisma.cutRenderJob.findMany({+ where: { state: { in: ["queued", "running"] }, projectId: { in: docProjects.map((p) => p.id) } },+ select: { projectId: true, spec: true },+ });+ for (const job of activeRenders) {+ if (!job.projectId) continue;+ const spec = job.spec as {+ fromDoc?: { snapshot?: { doc?: { assets?: { fileName: string }[] } } };+ spec?: { clips?: { file?: string }[]; audio?: { file?: string }[]; overlayVideos?: { file?: string }[] };+ } | null;+ const files = referenced.get(job.projectId) ?? new Set<string | undefined>();+ for (const asset of spec?.fromDoc?.snapshot?.doc?.assets ?? []) files.add(asset.fileName);+ for (const item of [...(spec?.spec?.clips ?? []), ...(spec?.spec?.audio ?? []), ...(spec?.spec?.overlayVideos ?? [])])+ if (item.file) files.add(item.file);+ referenced.set(job.projectId, files);+ }const orphans = mediaCandidates.filter((c) => !referenced.get(c.projectId!)?.has(c.fileName));const byUser = new Map<string, typeof orphans>();for (const o of orphans) {@@ -333,6 +365,26 @@ export async function runGc(): Promise<Response> {await del(rows.map((r) => r.r2Key));}+ // Retired preview revisions get the same grace as other unreferenced media.+ // The current preview remains available for an idle project indefinitely.+ const previews = await prisma.cutMediaObject.findMany({+ where: { kind: "preview", updatedAt: { lt: new Date(Date.now() - ORPHAN_MAX_AGE_MS) } },+ select: { id: true, userId: true, projectId: true, r2Key: true, bytes: true, quotaExempt: true },+ });+ const previewProjects = await prisma.cutProject.findMany({+ where: { id: { in: previews.flatMap((p) => p.projectId ? [p.projectId] : []) } },+ select: { previewKey: true },+ });+ const livePreviewKeys = new Set(previewProjects.map((p) => p.previewKey));+ for (const preview of previews) {+ if (livePreviewKeys.has(preview.r2Key)) continue;+ await prisma.$transaction(async (tx) => {+ const removed = await tx.cutMediaObject.deleteMany({ where: { id: preview.id } });+ if (removed.count && !preview.quotaExempt) await addUsage(tx, preview.userId, -Number(preview.bytes));+ });+ await del([preview.r2Key]);+ }+// Old terminal jobs go together with their overlay PNGs — the worker deletes// overlays when it settles a job, but a job canceled while still queued was// never claimed, so its overlays only die here.diff --git a/site/src/cut/server/cloud/storageCounter.ts b/site/src/cut/server/cloud/storageCounter.tsnew file mode 100644index 00000000..bb7e397d--- /dev/null+++ b/site/src/cut/server/cloud/storageCounter.ts@@ -0,0 +1,16 @@+import type { Prisma } from "@/generated/prisma/client";++/** Called inside a transaction. The upsert locks the account row until commit. */+export async function adjustStorageBytes(tx: Prisma.TransactionClient, userId: string, delta: bigint): Promise<void> {+ const row = await tx.cutStorageUsage.upsert({+ where: { userId },+ create: { userId, bytes: BigInt(0) },+ update: { userId },+ });+ const increment = delta < -row.bytes ? -row.bytes : delta;+ const changed = await tx.cutStorageUsage.updateMany({+ where: { userId, bytes: row.bytes },+ data: { bytes: { increment } },+ });+ if (changed.count !== 1) throw new Error("Storage usage changed during the operation.");+}diff --git a/site/src/cut/server/cloud/usage.ts b/site/src/cut/server/cloud/usage.tsindex a5acc8ba..33fca20c 100644--- a/site/src/cut/server/cloud/usage.ts+++ b/site/src/cut/server/cloud/usage.ts@@ -1,5 +1,6 @@import type { Prisma } from "@/generated/prisma/client";import { prisma } from "@/lib/prisma";+import { adjustStorageBytes } from "./storageCounter";import { graceState } from "./grace";import { cutLimitsFor } from "./limits";@@ -32,13 +33,7 @@ export async function reservedBytes(userId: string): Promise<number> {/** Adjust the user's stored-bytes counter inside the caller's transaction, so* it moves with the CutMediaObject rows it mirrors. Clamped at zero. */export async function addUsage(tx: Prisma.TransactionClient, userId: string, delta: number) {- const row = await tx.cutStorageUsage.findUnique({ where: { userId } });- const next = Math.max(0, (row ? Number(row.bytes) : 0) + Math.round(delta));- await tx.cutStorageUsage.upsert({- where: { userId },- create: { userId, bytes: BigInt(next) },- update: { bytes: BigInt(next) },- });+ await adjustStorageBytes(tx, userId, BigInt(Math.round(delta)));}/** 413 when `incoming` more bytes would break the account's storage quota,diff --git a/site/src/cut/worker/db.test.ts b/site/src/cut/worker/db.test.tsnew file mode 100644index 00000000..27d28f7b--- /dev/null+++ b/site/src/cut/worker/db.test.ts@@ -0,0 +1,102 @@+import { afterAll, afterEach, expect, mock, test } from "bun:test";+import { Prisma } from "@/generated/prisma/client";+import { prisma as actualPrisma } from "@/lib/prisma";++const originalPrisma = actualPrisma;+const prisma = {+ settingOverride: { findUnique: mock(async () => ({ value: { maxAttempts: 3 } })) },+ $transaction: mock(async (+ run: (tx: Prisma.TransactionClient) => Promise<unknown>,+ options: { isolationLevel: string },+): Promise<unknown> => {+ void run; void options;+ throw new Error("Unexpected transaction");+}) };+mock.module("@/lib/prisma", () => ({ prisma }));+const { registerObject, unregisterObjects } = await import("./db");+afterAll(() => { mock.module("@/lib/prisma", () => ({ prisma: originalPrisma })); });++const conflict = () => new Prisma.PrismaClientKnownRequestError("Write conflict", {+ code: "P2034", clientVersion: "test",+});+const object = {+ id: "object", userId: "user", projectId: "project", r2Key: "cut/user/file.mp4",+ fileName: "file.mp4", mime: "video/mp4", bytes: BigInt(40), kind: "export",+ uploadState: "complete", quotaExempt: false,+};+const input = { ...object, bytes: 100 };+const restores: (() => void)[] = [];+afterEach(() => { for (const restore of restores.splice(0).reverse()) restore(); });++function harness(failures = 1, error: Error = conflict()) {+ let stored: typeof object | null = { ...object };+ let usage = BigInt(140); // Includes another 100 bytes belonging to the account.+ let attempts = 0;+ const transaction = prisma.$transaction.mockImplementation((async (+ run: (tx: Prisma.TransactionClient) => Promise<unknown>,+ ) => {+ attempts++;+ let pending = stored ? { ...stored } : null;+ let pendingUsage = usage;+ const tx = {+ cutMediaObject: {+ findUnique: async () => pending,+ findMany: async () => pending ? [pending] : [],+ upsert: async ({ update }: { update: Partial<typeof object> }) => {+ pending = { ...object, ...pending, ...update };+ return pending;+ },+ deleteMany: async () => { pending = null; return { count: 1 }; },+ },+ cutStorageUsage: {+ upsert: async () => ({ userId: "user", bytes: pendingUsage }),+ updateMany: async ({ where, data }: {+ where: { bytes: bigint }; data: { bytes: { increment: bigint } };+ }) => {+ if (where.bytes !== pendingUsage) return { count: 0 };+ pendingUsage += data.bytes.increment;+ return { count: 1 };+ },+ },+ };+ const result = await run(tx as unknown as Prisma.TransactionClient);+ if (attempts <= failures) {+ // The conflicting writer commits a different object size. The retry+ // must reread it and recompute the delta after this attempt rolls back.+ if (stored) { stored = { ...stored, bytes: stored.bytes + BigInt(10) }; usage += BigInt(10); }+ throw error;+ }+ stored = pending;+ usage = pendingUsage;+ return result;+ }));+ restores.push(() => transaction.mockRestore());+ return { transaction, state: () => ({ stored, usage, attempts }) };+}++test("registration retries the whole transaction and charges the fresh size delta once", async () => {+ const h = harness();+ expect(await registerObject(input)).toBe("object");+ expect(h.state()).toMatchObject({ attempts: 2, usage: BigInt(200), stored: { bytes: BigInt(100) } });+ for (const call of h.transaction.mock.calls) expect(call[1]).toEqual({ isolationLevel: "Serializable" });+});++test("unregistration retries and refunds the freshly read bytes once", async () => {+ const h = harness();+ await unregisterObjects("user", [object.r2Key]);+ expect(h.state()).toEqual({ attempts: 2, usage: BigInt(100), stored: null });+});++test("conflicts stop at the configured attempt limit", async () => {+ const error = conflict();+ const h = harness(10, error);+ await expect(registerObject(input)).rejects.toBe(error);+ expect(h.state().attempts).toBe(3);+});++test("other failures propagate without retrying", async () => {+ const error = new Error("Database unavailable");+ const h = harness(1, error);+ await expect(registerObject(input)).rejects.toBe(error);+ expect(h.state().attempts).toBe(1);+});diff --git a/site/src/cut/worker/db.ts b/site/src/cut/worker/db.tsindex 48856fb2..6e0622f6 100644--- a/site/src/cut/worker/db.ts+++ b/site/src/cut/worker/db.ts@@ -1,7 +1,29 @@+import { adjustStorageBytes } from "../server/cloud/storageCounter";+import { artifactLifecycle, artifactUsageBytes } from "../lib/artifactPolicy";import { prisma } from "@/lib/prisma";+import { Prisma } from "@/generated/prisma/client";+import { resolveSettings } from "@/lib/config/resolve";export { prisma };+/** A conflict rolls back every write; retry with a fresh transaction snapshot. */+async function storageTransaction<T>(run: (tx: Prisma.TransactionClient) => Promise<T>): Promise<T> {+ const override = await prisma.settingOverride.findUnique({+ where: { key: "cutStorageTransactions" }, select: { value: true },+ });+ const { maxAttempts } = resolveSettings(+ override ? { cutStorageTransactions: override.value } : {}, []+ ).settings.cutStorageTransactions;+ for (let attempt = 1; ; attempt++) {+ try {+ return await prisma.$transaction(run, { isolationLevel: "Serializable" });+ } catch (error) {+ if (!(error instanceof Prisma.PrismaClientKnownRequestError) || error.code !== "P2034" || attempt >= maxAttempts)+ throw error;+ }+ }+}+/** The columns a claimed CutRenderJob row hands the job runners. */export interface ClaimedJob {id: string;@@ -28,34 +50,22 @@ export async function registerObject(opts: {bytes: number;kind: string;}): Promise<string> {- const prior = await prisma.cutMediaObject.findUnique({- where: { r2Key: opts.r2Key },- select: { bytes: true, uploadState: true },- });- const priorBytes = prior?.uploadState === "complete" ? prior.bytes : BigInt(0);- const delta = BigInt(opts.bytes) - priorBytes;- const [row] = await prisma.$transaction([- prisma.cutMediaObject.upsert({+ return storageTransaction(async (tx) => {+ const prior = await tx.cutMediaObject.findUnique({where: { r2Key: opts.r2Key },- create: {- userId: opts.userId,- projectId: opts.projectId,- r2Key: opts.r2Key,- fileName: opts.fileName,- mime: opts.mime,- bytes: BigInt(opts.bytes),- kind: opts.kind,- uploadState: "complete",- },- update: { bytes: BigInt(opts.bytes), uploadState: "complete" },- }),- prisma.cutStorageUsage.upsert({- where: { userId: opts.userId },- create: { userId: opts.userId, bytes: delta },- update: { bytes: { increment: delta } },- }),- ]);- return row.id;+ select: { bytes: true, uploadState: true, quotaExempt: true },+ });+ const quotaExempt = artifactLifecycle(opts.kind) !== "retained";+ const priorBytes = prior ? artifactUsageBytes(prior.bytes, prior.uploadState === "complete", prior.quotaExempt) : BigInt(0);+ const delta = artifactUsageBytes(BigInt(opts.bytes), true, quotaExempt) - priorBytes;+ const row = await tx.cutMediaObject.upsert({+ where: { r2Key: opts.r2Key },+ create: { ...opts, bytes: BigInt(opts.bytes), quotaExempt, uploadState: "complete" },+ update: { bytes: BigInt(opts.bytes), uploadState: "complete", quotaExempt },+ });+ if (delta !== BigInt(0)) await adjustStorageBytes(tx, opts.userId, delta);+ return row.id;+ });}/**@@ -65,23 +75,17 @@ export async function registerObject(opts: {*/export async function unregisterObjects(userId: string, r2Keys: string[]): Promise<void> {if (r2Keys.length === 0) return;- const rows = await prisma.cutMediaObject.findMany({- where: { userId, r2Key: { in: r2Keys } },- select: { id: true, bytes: true, uploadState: true },- });- if (rows.length === 0) return;- const bytes = rows.reduce(- (sum, r) => (r.uploadState === "complete" ? sum + Number(r.bytes) : sum),- 0- );- await prisma.$transaction(async (tx) => {- await tx.cutMediaObject.deleteMany({ where: { id: { in: rows.map((r) => r.id) } } });- const usage = await tx.cutStorageUsage.findUnique({ where: { userId } });- const next = Math.max(0, (usage ? Number(usage.bytes) : 0) - bytes);- await tx.cutStorageUsage.upsert({- where: { userId },- create: { userId, bytes: BigInt(next) },- update: { bytes: BigInt(next) },+ await storageTransaction(async (tx) => {+ const rows = await tx.cutMediaObject.findMany({+ where: { userId, r2Key: { in: r2Keys } },+ select: { id: true, bytes: true, uploadState: true, quotaExempt: true },});+ if (rows.length === 0) return;+ const bytes = rows.reduce(+ (sum, row) => sum + artifactUsageBytes(row.bytes, row.uploadState === "complete", row.quotaExempt),+ BigInt(0)+ );+ await tx.cutMediaObject.deleteMany({ where: { id: { in: rows.map((row) => row.id) } } });+ await adjustStorageBytes(tx, userId, -bytes);});}diff --git a/site/src/lib/config/registry.ts b/site/src/lib/config/registry.tsindex 01db42c9..553878ef 100644--- a/site/src/lib/config/registry.ts+++ b/site/src/lib/config/registry.ts@@ -31,6 +31,13 @@ function validTimeZone(zone: string): boolean {}export const SETTINGS = defineSettings({+ cutStorageTransactions: {+ schema: z.object({ maxAttempts: z.number().int().min(1).max(10) }).strict(),+ default: { maxAttempts: 5 },+ public: false,+ title: "Cut storage transactions",+ description: "Maximum attempts for storage accounting transactions that encounter a database write conflict.",+ },librarySharing: {schema: z.object({ pageSize: z.number().int().min(10).max(100) }).strict(),default: { pageSize: 40 },