feat(core): optimize query resource lifecycle
This commit is contained in:
1 parent
b4bd6c42a7
commit
295b67dd36
29 files changed
+1857
-335
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import type { QueryTab } from "@/types/database";
|
||||
import type { TabResultSnapshot } from "@/lib/tabs/tabResultCache";
|
||||
|
||||
export function queryResultLifecycleSnapshot(): TabResultSnapshot {
|
||||
const first = {
|
||||
columns: ["id", "name"],
|
||||
rows: [[1, "Ada"]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 4,
|
||||
sourceStatement: "SELECT id, name FROM users",
|
||||
};
|
||||
const second = {
|
||||
columns: ["count"],
|
||||
rows: [[1]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 2,
|
||||
sourceStatement: "SELECT COUNT(*) FROM users",
|
||||
};
|
||||
const resultRuns: NonNullable<QueryTab["resultRuns"]> = [
|
||||
{
|
||||
id: "run-1",
|
||||
sequence: 1,
|
||||
result: first,
|
||||
results: [first, second],
|
||||
activeResultIndex: 0,
|
||||
resultLocalSortOriginalRows: [[1, "Ada"]],
|
||||
resultBaseSql: "SELECT id, name FROM users",
|
||||
resultPageSql: "SELECT id, name FROM users LIMIT 100 OFFSET 200",
|
||||
resultPageLimit: 100,
|
||||
resultPageOffset: 200,
|
||||
tableMeta: {
|
||||
schema: "public",
|
||||
tableName: "users",
|
||||
tableType: "TABLE",
|
||||
columns: [{ name: "id", data_type: "integer", nullable: false }],
|
||||
primaryKeys: ["id"],
|
||||
},
|
||||
},
|
||||
];
|
||||
return {
|
||||
result: first,
|
||||
results: [first, second],
|
||||
activeResultIndex: 0,
|
||||
resultLocalSortOriginalRows: [[1, "Ada"]],
|
||||
resultRuns,
|
||||
activeResultRunId: "run-1",
|
||||
queryAnalysis: { tableName: "users", schema: "public", allowInsert: true, allowInsertDelete: true },
|
||||
querySourceColumns: ["id", "name"],
|
||||
tableMeta: resultRuns[0].tableMeta,
|
||||
resultPageSql: "SELECT id, name FROM users LIMIT 100 OFFSET 200",
|
||||
resultPageLimit: 100,
|
||||
resultPageOffset: 200,
|
||||
resultCountSql: "SELECT COUNT(*) FROM users",
|
||||
resultTotalRowCount: 301,
|
||||
cachedAt: 1,
|
||||
};
|
||||
}
|
||||
+93
-47
@@ -1,95 +1,141 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { cancelPendingSidebarDataOpen, runSidebarDataOpenImmediately } from "@/lib/sidebar/sidebarDataOpenCoordinator";
|
||||
|
||||
function deferred() {
|
||||
let resolve!: () => void;
|
||||
const promise = new Promise<void>((done) => {
|
||||
resolve = done;
|
||||
});
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
describe("sidebarDataOpenCoordinator", () => {
|
||||
afterEach(() => {
|
||||
cancelPendingSidebarDataOpen();
|
||||
});
|
||||
|
||||
it("cancels the active request when a newer open starts", async () => {
|
||||
it("cancels the active request when a newer ordinary open starts", async () => {
|
||||
const cancel = vi.fn();
|
||||
const running = deferred();
|
||||
let firstIsCurrent = true;
|
||||
|
||||
runSidebarDataOpenImmediately(async (request) => {
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, async (request) => {
|
||||
request.registerCancel(cancel);
|
||||
await Promise.resolve();
|
||||
await running.promise;
|
||||
firstIsCurrent = request.isCurrent();
|
||||
});
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, () => undefined);
|
||||
await Promise.resolve();
|
||||
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
expect(firstIsCurrent).toBe(true);
|
||||
|
||||
running.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
expect(firstIsCurrent).toBe(false);
|
||||
});
|
||||
|
||||
it("replaces a superseded queued request before it starts", async () => {
|
||||
const running = deferred();
|
||||
const second = vi.fn();
|
||||
const third = vi.fn();
|
||||
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn", supersede: false }, () => running.promise);
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, second);
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, third);
|
||||
running.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(second).not.toHaveBeenCalled();
|
||||
expect(third).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("bounds active work per connection", async () => {
|
||||
const first = deferred();
|
||||
const starts: string[] = [];
|
||||
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn", supersede: false }, async () => {
|
||||
starts.push("first");
|
||||
await first.promise;
|
||||
});
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn", supersede: false }, () => {
|
||||
starts.push("second");
|
||||
});
|
||||
|
||||
expect(starts).toEqual(["first"]);
|
||||
first.resolve();
|
||||
await vi.waitFor(() => expect(starts).toEqual(["first", "second"]));
|
||||
});
|
||||
|
||||
it("allows different connections to make progress independently", () => {
|
||||
const blocked = deferred();
|
||||
const otherConnection = vi.fn();
|
||||
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn-a", supersede: false }, () => blocked.promise);
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn-b", supersede: false }, otherConnection);
|
||||
|
||||
expect(otherConnection).toHaveBeenCalledOnce();
|
||||
blocked.resolve();
|
||||
});
|
||||
|
||||
it("keeps explicit new-tab opens independent while still queueing them", async () => {
|
||||
const first = deferred();
|
||||
const firstCancel = vi.fn();
|
||||
const second = vi.fn();
|
||||
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn", supersede: false }, async (request) => {
|
||||
request.registerCancel(firstCancel);
|
||||
await first.promise;
|
||||
});
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn", supersede: false }, second);
|
||||
|
||||
expect(firstCancel).not.toHaveBeenCalled();
|
||||
expect(second).not.toHaveBeenCalled();
|
||||
first.resolve();
|
||||
await vi.waitFor(() => expect(second).toHaveBeenCalledOnce());
|
||||
});
|
||||
|
||||
it("runs registered cancellation immediately for an already stale request", async () => {
|
||||
const cancel = vi.fn();
|
||||
let registerLateCancel: (() => void) | undefined;
|
||||
const running = deferred();
|
||||
|
||||
runSidebarDataOpenImmediately((request) => {
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, async (request) => {
|
||||
registerLateCancel = () => request.registerCancel(cancel);
|
||||
await running.promise;
|
||||
});
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, () => undefined);
|
||||
registerLateCancel?.();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
running.resolve();
|
||||
});
|
||||
|
||||
it("recovers after a runner throws synchronously", async () => {
|
||||
const nextRunner = vi.fn();
|
||||
|
||||
runSidebarDataOpenImmediately(() => {
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn", supersede: false }, () => {
|
||||
throw new Error("boom");
|
||||
});
|
||||
await Promise.resolve();
|
||||
runSidebarDataOpenImmediately(nextRunner);
|
||||
await Promise.resolve();
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn", supersede: false }, nextRunner);
|
||||
|
||||
expect(nextRunner).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("keeps only the newest rapid navigation request current", async () => {
|
||||
const states: boolean[] = [];
|
||||
const captures: Array<() => void> = [];
|
||||
it("aborts a superseded request signal", () => {
|
||||
let signal: AbortSignal | undefined;
|
||||
|
||||
for (let index = 0; index < 3; index += 1) {
|
||||
runSidebarDataOpenImmediately((request) => {
|
||||
captures.push(() => states.push(request.isCurrent()));
|
||||
});
|
||||
}
|
||||
captures.forEach((capture) => capture());
|
||||
|
||||
expect(states).toEqual([false, false, true]);
|
||||
});
|
||||
|
||||
it("does not cancel work that was not registered as sidebar-owned", async () => {
|
||||
const sidebarCancel = vi.fn();
|
||||
const unrelatedEditorCancel = vi.fn();
|
||||
|
||||
runSidebarDataOpenImmediately((request) => request.registerCancel(sidebarCancel));
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
await Promise.resolve();
|
||||
|
||||
expect(sidebarCancel).toHaveBeenCalledOnce();
|
||||
expect(unrelatedEditorCancel).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("prevents a superseded connection completion from becoming current", async () => {
|
||||
let finishConnection: (() => void) | undefined;
|
||||
let completionWasCurrent = true;
|
||||
|
||||
runSidebarDataOpenImmediately(async (request) => {
|
||||
await new Promise<void>((resolve) => {
|
||||
finishConnection = resolve;
|
||||
});
|
||||
completionWasCurrent = request.isCurrent();
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, (request) => {
|
||||
signal = request.signal;
|
||||
});
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
finishConnection?.();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, () => undefined);
|
||||
|
||||
expect(completionWasCurrent).toBe(false);
|
||||
expect(signal?.aborted).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { estimateQueryResultBytes, estimateQueryResultsBytes, selectInactiveResultEvictions } from "@/lib/tabs/queryResultSize";
|
||||
|
||||
function result(rows: unknown[][]) {
|
||||
return { columns: ["value"], rows, affected_rows: 0, execution_time_ms: 1 };
|
||||
}
|
||||
|
||||
describe("query result size estimation", () => {
|
||||
it("grows with string and binary payloads", () => {
|
||||
const small = estimateQueryResultBytes(result([["a"]]));
|
||||
const large = estimateQueryResultBytes(result([["a".repeat(10_000)], [new Uint8Array(20_000)]]));
|
||||
expect(large).toBeGreaterThan(small + 30_000);
|
||||
});
|
||||
|
||||
it("counts multiple results and tolerates cyclic document values", () => {
|
||||
const document: Record<string, unknown> = { name: "node" };
|
||||
document.self = document;
|
||||
const first = result([[document]]);
|
||||
const second = result(Array.from({ length: 100 }, (_, index) => [index]));
|
||||
expect(estimateQueryResultsBytes(undefined, [first, second])).toBeGreaterThan(estimateQueryResultBytes(first));
|
||||
});
|
||||
});
|
||||
|
||||
describe("inactive result byte budgets", () => {
|
||||
it("evicts oldest small results to the count limit", () => {
|
||||
const entries = Array.from({ length: 8 }, (_, index) => ({ id: `${index}`, estimatedBytes: 10, accessedAt: index }));
|
||||
expect(selectInactiveResultEvictions(entries, 1_000, 5)).toEqual(["0", "1", "2"]);
|
||||
});
|
||||
|
||||
it("evicts least-recently-used large results to the byte limit", () => {
|
||||
const entries = [
|
||||
{ id: "old", estimatedBytes: 80, accessedAt: 1 },
|
||||
{ id: "middle", estimatedBytes: 70, accessedAt: 2 },
|
||||
{ id: "new", estimatedBytes: 60, accessedAt: 3 },
|
||||
];
|
||||
expect(selectInactiveResultEvictions(entries, 100, 10)).toEqual(["old", "middle"]);
|
||||
});
|
||||
|
||||
it("does not consider the active result even when it exceeds the budget", () => {
|
||||
const inactive = [{ id: "inactive", estimatedBytes: 10, accessedAt: 1 }];
|
||||
expect(selectInactiveResultEvictions(inactive, 100, 5)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { encodeTabResultSnapshot } from "@/lib/tabs/tabResultCache";
|
||||
import { estimateQueryResultBytes } from "@/lib/tabs/queryResultSize";
|
||||
import { cancelPendingSidebarDataOpen, runSidebarDataOpenImmediately, sidebarDataOpenDiagnostics } from "@/lib/sidebar/sidebarDataOpenCoordinator";
|
||||
|
||||
describe("resource lifecycle performance harness", () => {
|
||||
it("records deterministic serialization and memory-estimation measurements", () => {
|
||||
const result = {
|
||||
columns: ["id", "label", "payload"],
|
||||
rows: Array.from({ length: 25_000 }, (_, index) => [index, `row-${index}`, "x".repeat(64)]),
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
};
|
||||
|
||||
const estimateStartedAt = performance.now();
|
||||
const estimatedBytes = estimateQueryResultBytes(result);
|
||||
const estimateDurationMs = performance.now() - estimateStartedAt;
|
||||
const serializeStartedAt = performance.now();
|
||||
const encodedBytes = encodeTabResultSnapshot({ result, cachedAt: 1 }).byteLength;
|
||||
const serializationDurationMs = performance.now() - serializeStartedAt;
|
||||
|
||||
console.info("[resource-lifecycle-baseline]", JSON.stringify({ rows: result.rows.length, estimatedBytes, encodedBytes, estimateDurationMs, serializationDurationMs }));
|
||||
expect(estimatedBytes).toBeGreaterThan(encodedBytes);
|
||||
expect(estimateDurationMs).toBeLessThan(250);
|
||||
expect(serializationDurationMs).toBeLessThan(1_500);
|
||||
});
|
||||
});
|
||||
|
||||
describe("rapid table-open performance harness", () => {
|
||||
it("coalesces a 100-request burst to one active and one latest queued request", async () => {
|
||||
let release!: () => void;
|
||||
const blocked = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const committed: number[] = [];
|
||||
const startedAt = performance.now();
|
||||
runSidebarDataOpenImmediately({ connectionKey: "connection" }, async (request) => {
|
||||
await blocked;
|
||||
if (request.isCurrent()) committed.push(0);
|
||||
});
|
||||
for (let index = 1; index < 100; index += 1) {
|
||||
runSidebarDataOpenImmediately({ connectionKey: "connection" }, (request) => {
|
||||
if (request.isCurrent()) committed.push(index);
|
||||
});
|
||||
}
|
||||
const enqueueDurationMs = performance.now() - startedAt;
|
||||
const burstDiagnostics = sidebarDataOpenDiagnostics();
|
||||
release();
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
const totalDurationMs = performance.now() - startedAt;
|
||||
console.info("[rapid-table-open-baseline]", JSON.stringify({ enqueueDurationMs, totalDurationMs, ...burstDiagnostics }));
|
||||
|
||||
expect(burstDiagnostics).toEqual({ activeTasks: 1, queuedTasks: 1, trackedRequests: 2 });
|
||||
expect(committed).toEqual([99]);
|
||||
cancelPendingSidebarDataOpen();
|
||||
});
|
||||
});
|
||||
+91
-2
@@ -1,7 +1,78 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { decodeTabResultSnapshot, encodeTabResultSnapshot } from "@/lib/tabs/tabResultCache";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { decodeTabResultSnapshot, encodeTabResultSnapshot, readResultCacheBackends, promoteFallbackResultCacheRead, resultCacheBackendOrder, resultCacheRuntimeConfig, selectResultCachePruneKeys, writeResultCacheBackends, type ResultCacheBackend } from "@/lib/tabs/tabResultCache";
|
||||
import { queryResultLifecycleSnapshot } from "@/lib/__tests__/fixtures/queryResultLifecycle";
|
||||
|
||||
function backend(name: ResultCacheBackend["name"], overrides: Partial<ResultCacheBackend> = {}): ResultCacheBackend {
|
||||
return {
|
||||
name,
|
||||
available: () => true,
|
||||
read: async () => undefined,
|
||||
write: async () => true,
|
||||
delete: async () => undefined,
|
||||
listMetadata: async () => [],
|
||||
prune: async () => ({ deletedEntries: 0, deletedBytes: 0, orphanDeletions: 0, remainingEntries: 0, remainingBytes: 0 }),
|
||||
deleteOwner: async () => undefined,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("tab result cache statement execution metadata", () => {
|
||||
it("selects one authoritative backend before fallback", () => {
|
||||
expect(resultCacheBackendOrder(true)).toEqual(["runtime", "indexed-db"]);
|
||||
expect(resultCacheBackendOrder(false)).toEqual(["indexed-db", "runtime"]);
|
||||
});
|
||||
|
||||
it("uses explicit browser runtime configuration", () => {
|
||||
expect(resultCacheRuntimeConfig(false, { VITE_DBX_RESULT_CACHE_BACKEND: "http", VITE_DBX_RESULT_CACHE_FALLBACK: "true" } as ImportMetaEnv)).toEqual({
|
||||
primary: "runtime",
|
||||
fallbackEnabled: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("writes only to the first successful backend", async () => {
|
||||
const primaryWrite = vi.fn(async () => true);
|
||||
const fallbackWrite = vi.fn(async () => true);
|
||||
await writeResultCacheBackends([backend("runtime", { write: primaryWrite }), backend("indexed-db", { write: fallbackWrite })], "key", new Uint8Array([1]), { rowCount: 1, columnCount: 1 });
|
||||
expect(primaryWrite).toHaveBeenCalledOnce();
|
||||
expect(fallbackWrite).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reads legacy fallback data for promotion", async () => {
|
||||
const bytes = new Uint8Array([1, 2, 3]);
|
||||
const primary = backend("runtime");
|
||||
const fallback = backend("indexed-db", { read: async () => bytes });
|
||||
expect(await readResultCacheBackends([primary, fallback], "key")).toEqual({ bytes, backend: fallback });
|
||||
});
|
||||
|
||||
it("promotes legacy IndexedDB or native bytes to the authoritative backend", async () => {
|
||||
const bytes = new Uint8Array([1, 2, 3]);
|
||||
const primaryWrite = vi.fn(async () => true);
|
||||
const primary = backend("runtime", { write: primaryWrite });
|
||||
const legacy = backend("indexed-db");
|
||||
|
||||
expect(await promoteFallbackResultCacheRead([primary, legacy], "key", { bytes, backend: legacy }, { rowCount: 2, columnCount: 1 }, "connection-1")).toBe(true);
|
||||
expect(primaryWrite).toHaveBeenCalledWith("key", bytes, { rowCount: 2, columnCount: 1 }, "connection-1");
|
||||
});
|
||||
|
||||
it("skips an obsolete write before touching a backend", async () => {
|
||||
const write = vi.fn(async () => true);
|
||||
expect(await writeResultCacheBackends([backend("runtime", { write })], "key", new Uint8Array([1]), { rowCount: 1, columnCount: 1 }, () => false)).toBe(false);
|
||||
expect(write).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("prunes crash leftovers and then unreferenced LRU entries", () => {
|
||||
const selection = selectResultCachePruneKeys(
|
||||
[
|
||||
{ key: "live", rowCount: 1, columnCount: 1, byteSize: 8, createdAt: 1, lastAccessedAt: 1 },
|
||||
{ key: "crash", rowCount: 1, columnCount: 1, byteSize: 7, createdAt: 1, lastAccessedAt: 1 },
|
||||
{ key: "recent", rowCount: 1, columnCount: 1, byteSize: 6, createdAt: 9_999, lastAccessedAt: 9_999 },
|
||||
],
|
||||
{ liveKeys: ["live"], maxBytes: 10, orphanGraceMs: 100, maxAgeMs: 1_000 },
|
||||
10_000,
|
||||
);
|
||||
expect(selection.keys).toEqual(["crash", "recent"]);
|
||||
expect(selection.result).toMatchObject({ orphanDeletions: 1, remainingBytes: 8 });
|
||||
});
|
||||
it("preserves statement identity and the editor fingerprint", () => {
|
||||
const encoded = encodeTabResultSnapshot({
|
||||
results: [
|
||||
@@ -35,4 +106,22 @@ describe("tab result cache statement execution metadata", () => {
|
||||
{ statementIndex: 1, error: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it("restores multi-result, pagination, local-sort, and editable metadata fixtures", () => {
|
||||
const restored = decodeTabResultSnapshot(encodeTabResultSnapshot(queryResultLifecycleSnapshot()));
|
||||
|
||||
expect(restored?.results).toHaveLength(2);
|
||||
expect(restored?.resultRuns?.[0]?.resultLocalSortOriginalRows).toEqual([[1, "Ada"]]);
|
||||
expect(restored?.resultPageOffset).toBe(200);
|
||||
expect(restored?.resultTotalRowCount).toBe(301);
|
||||
expect(restored?.tableMeta?.primaryKeys).toEqual(["id"]);
|
||||
expect(restored?.querySourceColumns).toEqual(["id", "name"]);
|
||||
});
|
||||
|
||||
it("treats corrupt and unsupported snapshots as missing", () => {
|
||||
expect(decodeTabResultSnapshot(new Uint8Array([0xff, 0x00]))).toBeUndefined();
|
||||
const encoded = encodeTabResultSnapshot(queryResultLifecycleSnapshot());
|
||||
encoded[0] = 0xff;
|
||||
expect(decodeTabResultSnapshot(encoded)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user