fix(storage): hold a per-transaction lock so reconcile skips a live OPFS put (05-01)
Reconcile cleaned up any incomplete journal row, including a put still running in another tab, and the put then reported success for deleted files. Put and delete now hold a Web Lock per transaction from begin to return; reconcile takes those locks with ifAvailable, re-reads the page under them and reports the rows it skipped (D-3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
926f448fce
commit
8fc816ee25
@@ -219,3 +219,193 @@ test("runs put, open, remove and reconcile through a native DedicatedWorker, OPF
|
||||
expect(result.deletion).toBe("DELETED");
|
||||
expect(["DELETED", "ABSENT"]).toContain(result.opfsCleanup);
|
||||
});
|
||||
|
||||
type OpfsTabScope = Readonly<{
|
||||
namespace: string;
|
||||
authorityToken: string;
|
||||
namespaceToken: string;
|
||||
partitionToken: string;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Installs an OPFS runtime for `scope` in this tab and keeps it on
|
||||
* `globalThis.__opfsTab` for later evaluate calls.
|
||||
*/
|
||||
async function installOpfsTab(
|
||||
page: import("@playwright/test").Page,
|
||||
scope: OpfsTabScope,
|
||||
): Promise<boolean> {
|
||||
return await page.evaluate(async (tabScope) => {
|
||||
const runtimeModulePath =
|
||||
"/src/adapters/storage/opfs/browser-opfs-runtime.ts";
|
||||
const policyModulePath = "/src/adapters/storage/opfs/opfs-policy.ts";
|
||||
const { createBrowserOpfsRuntime, inspectBrowserOpfsSupport } =
|
||||
(await import(
|
||||
/* @vite-ignore */ runtimeModulePath
|
||||
)) as typeof import("../../src/adapters/storage/opfs/browser-opfs-runtime.ts");
|
||||
const { resolveOpfsRuntimePolicy } = (await import(
|
||||
/* @vite-ignore */ policyModulePath
|
||||
)) as typeof import("../../src/adapters/storage/opfs/opfs-policy.ts");
|
||||
const policy = resolveOpfsRuntimePolicy();
|
||||
if (!inspectBrowserOpfsSupport(policy).ok) return false;
|
||||
(globalThis as unknown as { __opfsTab: unknown }).__opfsTab =
|
||||
createBrowserOpfsRuntime({
|
||||
workerUrl: new URL(
|
||||
"/tests/browser-capabilities/opfs-test.worker.ts",
|
||||
location.href,
|
||||
),
|
||||
workerName: `opfs-tab-${crypto.randomUUID()}`,
|
||||
scope: tabScope,
|
||||
storagePolicy: {
|
||||
owner: "browser-capability",
|
||||
namespace: tabScope.namespace,
|
||||
classification: "PERSONAL",
|
||||
authority: "LOCAL_FIRST",
|
||||
accountScope: "OPAQUE_PARTITION",
|
||||
retention: { kind: "EXPLICIT_DELETE" },
|
||||
softBudgetBytes: 1024 * 1024,
|
||||
hardBudgetBytes: 2 * 1024 * 1024,
|
||||
evictionPriority: "USER_AUTHORED",
|
||||
logoutAction: "EXPORT_THEN_PURGE",
|
||||
accountDeletionAction: "PURGE_PARTITION",
|
||||
pressureAction: "RETAIN",
|
||||
unavailableFallback: "EXPORT_REQUIRED",
|
||||
},
|
||||
policy,
|
||||
});
|
||||
return true;
|
||||
}, scope);
|
||||
}
|
||||
|
||||
test("05-01 a reconcile in another tab skips a live put and the put stays readable", async ({
|
||||
page,
|
||||
}) => {
|
||||
const secondPage = await page.context().newPage();
|
||||
const suffix = crypto.randomUUID().replaceAll("-", "");
|
||||
const scope: OpfsTabScope = {
|
||||
namespace: "durable-objects",
|
||||
authorityToken: `authority_${suffix}`,
|
||||
namespaceToken: `namespace_${suffix}`,
|
||||
partitionToken: `partition_${suffix}`,
|
||||
};
|
||||
type Tab = import("../../src/adapters/storage/opfs/browser-opfs-runtime.ts").BrowserOpfsRuntime;
|
||||
try {
|
||||
await Promise.all([page.goto("/"), secondPage.goto("/")]);
|
||||
if (!(await installOpfsTab(page, scope))) return;
|
||||
expect(await installOpfsTab(secondPage, scope)).toBe(true);
|
||||
|
||||
// Tab A starts a put whose source stalls, so its journal row stays
|
||||
// PREPARING and its worker holds the OPFS mutation lock.
|
||||
await page.evaluate(async () => {
|
||||
const state = globalThis as unknown as {
|
||||
__opfsTab: Tab;
|
||||
__release?: () => void;
|
||||
__put?: Promise<unknown>;
|
||||
};
|
||||
let streaming!: () => void;
|
||||
const started = new Promise<void>((resolve) => {
|
||||
streaming = resolve;
|
||||
});
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
state.__release = resolve;
|
||||
});
|
||||
state.__put = state.__opfsTab.objects.put({
|
||||
objectId: "object_tab_12345678",
|
||||
expectedGeneration: null,
|
||||
mediaType: "application/octet-stream",
|
||||
source: {
|
||||
byteLength: 4,
|
||||
async *stream() {
|
||||
streaming();
|
||||
await gate;
|
||||
yield { ok: true as const, value: new Uint8Array([2, 7, 1, 8]) };
|
||||
},
|
||||
},
|
||||
});
|
||||
await started;
|
||||
});
|
||||
|
||||
// Tab B reconciles while tab A's put is in flight.
|
||||
await secondPage.evaluate(() => {
|
||||
const state = globalThis as unknown as {
|
||||
__opfsTab: Tab;
|
||||
__reconcile?: Promise<unknown>;
|
||||
};
|
||||
state.__reconcile = state.__opfsTab.maintenance.reconcile();
|
||||
});
|
||||
await page.waitForTimeout(250);
|
||||
const put = await page.evaluate(async () => {
|
||||
const state = globalThis as unknown as {
|
||||
__release: () => void;
|
||||
__put: Promise<unknown>;
|
||||
};
|
||||
state.__release();
|
||||
return await state.__put;
|
||||
});
|
||||
const reconciled = await secondPage.evaluate(
|
||||
async () =>
|
||||
await (globalThis as unknown as { __reconcile: Promise<unknown> })
|
||||
.__reconcile,
|
||||
);
|
||||
const opened = await page.evaluate(async () => {
|
||||
const runtime = (globalThis as unknown as { __opfsTab: Tab }).__opfsTab;
|
||||
const result = await runtime.objects.open({
|
||||
objectId: "object_tab_12345678",
|
||||
});
|
||||
if (!result.ok) return result.error.code;
|
||||
const bytes: number[] = [];
|
||||
for await (const chunk of result.value.source.stream(
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
if (!chunk.ok) return chunk.error.code;
|
||||
bytes.push(...chunk.value);
|
||||
}
|
||||
return bytes;
|
||||
});
|
||||
|
||||
expect(put).toMatchObject({ ok: true, value: { generation: 1 } });
|
||||
expect(reconciled).toMatchObject({
|
||||
ok: true,
|
||||
value: { skippedLiveTransactions: 1, rolledBackTransactions: 0 },
|
||||
});
|
||||
expect(opened).toEqual([2, 7, 1, 8]);
|
||||
} finally {
|
||||
for (const tab of [page, secondPage]) {
|
||||
await tab.evaluate(() =>
|
||||
(globalThis as unknown as { __opfsTab?: Tab }).__opfsTab?.close(),
|
||||
);
|
||||
}
|
||||
const cleanup = await page.evaluate(async (tabScope) => {
|
||||
const journalModulePath =
|
||||
"/src/adapters/storage/opfs/indexeddb-opfs-journal.ts";
|
||||
const policyModulePath = "/src/adapters/storage/opfs/opfs-policy.ts";
|
||||
const { opfsJournalDatabaseName } = (await import(
|
||||
/* @vite-ignore */ journalModulePath
|
||||
)) as typeof import("../../src/adapters/storage/opfs/indexeddb-opfs-journal.ts");
|
||||
const { DEFAULT_OPFS_RUNTIME_POLICY } = (await import(
|
||||
/* @vite-ignore */ policyModulePath
|
||||
)) as typeof import("../../src/adapters/storage/opfs/opfs-policy.ts");
|
||||
const deletion = await new Promise<string>((resolve) => {
|
||||
const request = indexedDB.deleteDatabase(
|
||||
opfsJournalDatabaseName(tabScope.authorityToken),
|
||||
);
|
||||
request.onsuccess = () => resolve("DELETED");
|
||||
request.onerror = () => resolve(request.error?.name ?? "UNKNOWN");
|
||||
request.onblocked = () => resolve("BLOCKED");
|
||||
});
|
||||
try {
|
||||
const root = await (
|
||||
await navigator.storage.getDirectory()
|
||||
).getDirectoryHandle(DEFAULT_OPFS_RUNTIME_POLICY.rootDirectoryName);
|
||||
await (
|
||||
await root.getDirectoryHandle("authorities")
|
||||
).removeEntry(tabScope.authorityToken, { recursive: true });
|
||||
} catch {
|
||||
// Nothing was written for this authority.
|
||||
}
|
||||
return deletion;
|
||||
}, scope);
|
||||
expect(cleanup).toBe("DELETED");
|
||||
await secondPage.close();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -666,6 +666,57 @@ describe("OPFS byte-store coordinator", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("05-01 decides a reconciled row from what the journal holds once its lock is taken", async () => {
|
||||
// Listed while its owner still ran, the row was PREPARING. By the time
|
||||
// the owner let go it had committed and only finalization failed, so the
|
||||
// staging receipt names the committed generation. Cleaning it up from the
|
||||
// stale snapshot would delete a committed object.
|
||||
const journal = createJournal();
|
||||
const row: OpfsJournalTransaction = {
|
||||
transactionId: "transaction_12345678",
|
||||
fencingToken: "fencing_12345678",
|
||||
mutation: "PUT",
|
||||
scope,
|
||||
phase: "PREPARING",
|
||||
objectId: "object_12345678",
|
||||
expectedGeneration: null,
|
||||
targetGeneration: 1,
|
||||
targetByteLength: 1,
|
||||
targetStoragePolicy: storagePolicy,
|
||||
budgetReservation: {
|
||||
namespace: scope.namespace,
|
||||
reservedBytes: 1,
|
||||
hardBudgetBytes: storagePolicy.hardBudgetBytes,
|
||||
},
|
||||
startedAtEpochMs: 100,
|
||||
};
|
||||
journal.transactions.set(row.transactionId, {
|
||||
...row,
|
||||
phase: "COMMITTED",
|
||||
preparedObject: committedObject("object_12345678", storagePolicy, 100),
|
||||
});
|
||||
vi.spyOn(journal, "listIncomplete").mockResolvedValueOnce(
|
||||
browserDataSuccess({ transactions: [row], moreAvailable: false }),
|
||||
);
|
||||
const worker = createWorker();
|
||||
const cleanupTransaction = vi.spyOn(worker, "cleanupTransaction");
|
||||
const finalizePut = vi.spyOn(worker, "finalizePut");
|
||||
const adapter = createOpfsByteStoreAdapter({
|
||||
journal,
|
||||
worker,
|
||||
scope,
|
||||
storagePolicy,
|
||||
now: () => 100,
|
||||
});
|
||||
|
||||
expect(await adapter.maintenance.reconcile()).toMatchObject({
|
||||
ok: true,
|
||||
value: { inspectedTransactions: 1, rolledBackTransactions: 0 },
|
||||
});
|
||||
expect(cleanupTransaction).not.toHaveBeenCalled();
|
||||
expect(finalizePut).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not roll back journal after an unknown worker mutation effect", async () => {
|
||||
const journal = createJournal();
|
||||
const worker = createWorker({
|
||||
|
||||
@@ -13,6 +13,7 @@ import type {
|
||||
} from "../../src/application/ports/browser-file-storage/shared.ts";
|
||||
import { browserDataSuccess } from "../../src/adapters/browser-file-storage/result.ts";
|
||||
import { createOpfsByteStoreAdapter } from "../../src/adapters/storage/opfs/opfs-byte-store-adapter.ts";
|
||||
import { createIndexedDbOpfsJournal } from "../../src/adapters/storage/opfs/indexeddb-opfs-journal.ts";
|
||||
import {
|
||||
resolveOpfsRuntimePolicy,
|
||||
} from "../../src/adapters/storage/opfs/opfs-policy.ts";
|
||||
@@ -33,6 +34,7 @@ import {
|
||||
type OpfsWorkerMessageHost,
|
||||
type OpfsWorkerRuntime,
|
||||
} from "../../src/adapters/storage/opfs/opfs-worker-runtime.ts";
|
||||
import { MemoryIndexedDbFactory } from "../helpers/memory-indexeddb.ts";
|
||||
|
||||
const scopeA: OpfsStorageScope = Object.freeze({
|
||||
namespace: "durable-objects",
|
||||
@@ -2083,3 +2085,102 @@ describe("NS-05 a bootstrap failure answers the request it belongs to", () => {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Two runtimes ("tabs") share one OPFS root, one exclusive worker lock and
|
||||
* one journal database, the way two tabs of one origin do.
|
||||
*/
|
||||
function sharedRootTabs(policy = runtimePolicy) {
|
||||
const root = new MemoryDirectory();
|
||||
const leases = exclusiveLeases();
|
||||
const idb = new MemoryIndexedDbFactory();
|
||||
const tab = (tag: string) => {
|
||||
const runtime = createOpfsWorkerRuntime({
|
||||
root: root as unknown as FileSystemDirectoryHandle,
|
||||
crypto: globalThis.crypto,
|
||||
policy,
|
||||
leaseManager: leases,
|
||||
dedicatedWorker: true,
|
||||
supportsSynchronousAccessHandles: false,
|
||||
});
|
||||
let sequence = 0;
|
||||
return createOpfsByteStoreAdapter({
|
||||
journal: createIndexedDbOpfsJournal({
|
||||
authorityToken: scopeA.authorityToken,
|
||||
factory: idb.factory,
|
||||
keyRange: idb.keyRange,
|
||||
}),
|
||||
worker: loopbackGateway(runtime, policy),
|
||||
scope: scopeA,
|
||||
storagePolicy,
|
||||
policy,
|
||||
createTransactionId: () => `transaction_${tag}_${++sequence}`,
|
||||
});
|
||||
};
|
||||
return { root, leases, idb, tab };
|
||||
}
|
||||
|
||||
async function openedBytes(
|
||||
adapter: ReturnType<typeof createOpfsByteStoreAdapter>,
|
||||
objectId: string,
|
||||
): Promise<Readonly<{ generation: number; bytes: number[] }> | string> {
|
||||
const opened = await adapter.objects.open({ objectId });
|
||||
if (!opened.ok) return opened.error.code;
|
||||
const bytes: number[] = [];
|
||||
for await (const chunk of opened.value.source.stream(
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
if (!chunk.ok) return chunk.error.code;
|
||||
bytes.push(...chunk.value);
|
||||
}
|
||||
return { generation: opened.value.descriptor.generation, bytes };
|
||||
}
|
||||
|
||||
describe("05-01 reconcile leaves a live put's journal row alone", () => {
|
||||
it.each([
|
||||
["a first put", null],
|
||||
["an overwrite", 1],
|
||||
] as const)(
|
||||
"keeps %s that reconcile overlapped in another tab",
|
||||
async (_label, expectedGeneration) => {
|
||||
const { tab } = sharedRootTabs();
|
||||
const tabA = tab("a");
|
||||
const tabB = tab("b");
|
||||
if (expectedGeneration !== null) {
|
||||
expect(
|
||||
await tabA.objects.put({
|
||||
objectId: "object_12345678",
|
||||
expectedGeneration: null,
|
||||
mediaType: "application/octet-stream",
|
||||
source: oneByteSource(),
|
||||
}),
|
||||
).toMatchObject({ ok: true, value: { generation: 1 } });
|
||||
}
|
||||
|
||||
const stalled = stalledSource();
|
||||
const putA = tabA.objects.put({
|
||||
objectId: "object_12345678",
|
||||
expectedGeneration,
|
||||
mediaType: "application/octet-stream",
|
||||
source: stalled.source,
|
||||
});
|
||||
// Tab A holds the worker lock and its journal row is PREPARING.
|
||||
await stalled.streaming;
|
||||
const reconcileB = tabB.maintenance.reconcile();
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
stalled.release();
|
||||
|
||||
const [put, reconciled] = await Promise.all([putA, reconcileB]);
|
||||
const generation = (expectedGeneration ?? 0) + 1;
|
||||
expect(put).toMatchObject({ ok: true, value: { generation } });
|
||||
expect(reconciled).toMatchObject({
|
||||
ok: true,
|
||||
value: { skippedLiveTransactions: 1, rolledBackTransactions: 0 },
|
||||
});
|
||||
expect(await openedBytes(tabA, "object_12345678")).toEqual({
|
||||
generation,
|
||||
bytes: [5],
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user