fix(query): reduce retained result memory
This commit is contained in:
1 parent
2f3d1de46a
commit
68cfb5af5c
5 files changed
+292
-17
No files matched your search
@@ -803,7 +803,7 @@ test("removing the active result run selects an adjacent run", async () => {
|
||||
];
|
||||
await store.setActiveResultRun(tabId, "run-2");
|
||||
|
||||
assert.equal(store.removeResultRun(tabId, "run-2"), true);
|
||||
assert.equal(await store.removeResultRun(tabId, "run-2"), true);
|
||||
|
||||
assert.deepEqual(
|
||||
tab.resultRuns?.map((run) => run.id),
|
||||
@@ -814,7 +814,7 @@ test("removing the active result run selects an adjacent run", async () => {
|
||||
assert.deepEqual(tab.result?.rows, [[3]]);
|
||||
assert.equal(tab.sql, "select draft");
|
||||
|
||||
assert.equal(store.removeResultRun(tabId, "run-3"), true);
|
||||
assert.equal(await store.removeResultRun(tabId, "run-3"), true);
|
||||
|
||||
assert.deepEqual(
|
||||
tab.resultRuns?.map((run) => run.id),
|
||||
@@ -824,6 +824,42 @@ test("removing the active result run selects an adjacent run", async () => {
|
||||
assert.deepEqual(tab.result?.columns, ["one"]);
|
||||
});
|
||||
|
||||
test("removing the active result run clears output when remaining caches are unavailable", async () => {
|
||||
setActivePinia(createPinia());
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("conn-1", "db");
|
||||
const tab = store.tabs.find((item) => item.id === tabId);
|
||||
assert.ok(tab);
|
||||
|
||||
tab.resultRuns = [
|
||||
{
|
||||
id: "run-1",
|
||||
title: "Run 1",
|
||||
sequence: 1,
|
||||
sql: "select 1",
|
||||
createdAt: 1,
|
||||
result: { columns: ["one"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 },
|
||||
},
|
||||
{
|
||||
id: "run-2",
|
||||
title: "Run 2",
|
||||
sequence: 2,
|
||||
sql: "select 2",
|
||||
createdAt: 2,
|
||||
resultCacheKey: `missing-result-run-${Date.now()}`,
|
||||
resultCacheState: "disk",
|
||||
resultEvicted: true,
|
||||
},
|
||||
];
|
||||
await store.setActiveResultRun(tabId, "run-1");
|
||||
|
||||
assert.equal(await store.removeResultRun(tabId, "run-1"), true);
|
||||
assert.equal(tab.activeResultRunId, undefined);
|
||||
assert.equal(tab.result, undefined);
|
||||
assert.equal(tab.results, undefined);
|
||||
assert.deepEqual(tab.resultRuns?.map((run) => run.id), ["run-2"]);
|
||||
});
|
||||
|
||||
test("removed result runs are excluded from result archives", async () => {
|
||||
setActivePinia(createPinia());
|
||||
const store = useQueryStore();
|
||||
@@ -852,9 +888,9 @@ test("removed result runs are excluded from result archives", async () => {
|
||||
resultBaseSql: "select 2",
|
||||
},
|
||||
];
|
||||
store.setActiveResultRun(tabId, "run-2");
|
||||
await store.setActiveResultRun(tabId, "run-2");
|
||||
|
||||
assert.equal(store.removeResultRun(tabId, "run-1"), true);
|
||||
assert.equal(await store.removeResultRun(tabId, "run-1"), true);
|
||||
const archive = await store.exportResultArchive(tabId);
|
||||
assert.ok(archive);
|
||||
const decoded = await decodeQueryResultArchive(archive);
|
||||
@@ -885,9 +921,9 @@ test("removing the last result run clears output and makes result archive unavai
|
||||
resultBaseSql: "select 1",
|
||||
},
|
||||
];
|
||||
store.setActiveResultRun(tabId, "run-1");
|
||||
await store.setActiveResultRun(tabId, "run-1");
|
||||
|
||||
assert.equal(store.removeResultRun(tabId, "run-1"), true);
|
||||
assert.equal(await store.removeResultRun(tabId, "run-1"), true);
|
||||
|
||||
assert.deepEqual(tab.resultRuns, []);
|
||||
assert.equal(tab.activeResultRunId, undefined);
|
||||
@@ -1007,6 +1043,150 @@ test("completed query executions append result runs and select the latest run",
|
||||
}
|
||||
});
|
||||
|
||||
test("kept result runs evict inactive payloads without losing switch or archive data", async () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
setActivePinia(createPinia());
|
||||
const connectionStore = useConnectionStore();
|
||||
const store = useQueryStore();
|
||||
const originalFetch = globalThis.fetch;
|
||||
const runtimeCache = new Map<string, string>();
|
||||
let executeCount = 0;
|
||||
|
||||
connectionStore.addEphemeralConnection(conn("conn-1"));
|
||||
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
|
||||
const url = String(input);
|
||||
if (url === "/api/query/prepare-pagination-plan") {
|
||||
const body = JSON.parse(String(init?.body ?? "{}"));
|
||||
return new Response(JSON.stringify({ sqlToExecute: body.options.sql, useAgentResultSession: false }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
if (url === "/api/query/execute-multi") {
|
||||
executeCount++;
|
||||
return new Response(JSON.stringify([{ columns: [`run_${executeCount}`], rows: [[executeCount]], affected_rows: 0, execution_time_ms: 1 }]), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
if (url === "/api/query/analyze-editability") {
|
||||
return new Response(JSON.stringify({ editable: false, reason: "complex-source" }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
if (url === "/api/tab-runtime-cache" && init?.method === "POST") {
|
||||
const body = JSON.parse(String(init.body ?? "{}")) as { key: string; payloadBase64: string };
|
||||
runtimeCache.set(body.key, body.payloadBase64);
|
||||
return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
if (url.startsWith("/api/tab-runtime-cache?")) {
|
||||
const key = new URL(url, "http://localhost").searchParams.get("key") ?? "";
|
||||
if (init?.method === "DELETE") {
|
||||
runtimeCache.delete(key);
|
||||
return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
return new Response(JSON.stringify({ payloadBase64: runtimeCache.get(key) }), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
return new Response("unexpected request", { status: 500 });
|
||||
});
|
||||
|
||||
try {
|
||||
const tabId = store.createTab("conn-1", "db", "Query");
|
||||
store.toggleResultAutoSave(tabId);
|
||||
await store.executeTabSql(tabId, "select 1");
|
||||
await store.executeTabSql(tabId, "select 2");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId);
|
||||
assert.ok(tab?.resultRuns?.[0]);
|
||||
assert.ok(tab.resultRuns[1]);
|
||||
await waitFor(() => tab.resultRuns?.[0]?.result === undefined && tab.resultRuns?.[0]?.resultCacheState === "disk");
|
||||
assert.deepEqual(tab.result?.columns, ["run_2"]);
|
||||
assert.deepEqual(tab.resultRuns[1]?.result?.columns, ["run_2"]);
|
||||
|
||||
await store.setActiveResultRun(tabId, tab.resultRuns[0].id);
|
||||
assert.deepEqual(tab.result?.columns, ["run_1"]);
|
||||
assert.deepEqual(tab.result?.rows, [[1]]);
|
||||
assert.deepEqual(tab.resultRuns[0]?.result?.columns, ["run_1"]);
|
||||
|
||||
const archive = await store.exportResultArchive(tabId);
|
||||
assert.ok(archive);
|
||||
const decoded = await decodeQueryResultArchive(archive);
|
||||
assert.deepEqual(
|
||||
decoded?.snapshot.resultRuns?.map((run) => run.result?.rows),
|
||||
[[[1]], [[2]]],
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
restoreStorage();
|
||||
}
|
||||
});
|
||||
|
||||
test("removing the active result run restores a disk-backed adjacent run", async () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
setActivePinia(createPinia());
|
||||
const connectionStore = useConnectionStore();
|
||||
const store = useQueryStore();
|
||||
const originalFetch = globalThis.fetch;
|
||||
const runtimeCache = new Map<string, string>();
|
||||
let executeCount = 0;
|
||||
|
||||
connectionStore.addEphemeralConnection(conn("conn-1"));
|
||||
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
|
||||
const url = String(input);
|
||||
if (url === "/api/query/prepare-pagination-plan") {
|
||||
const body = JSON.parse(String(init?.body ?? "{}"));
|
||||
return new Response(JSON.stringify({ sqlToExecute: body.options.sql, useAgentResultSession: false }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
if (url === "/api/query/execute-multi") {
|
||||
executeCount++;
|
||||
return new Response(JSON.stringify([{ columns: [`run_${executeCount}`], rows: [[executeCount]], affected_rows: 0, execution_time_ms: 1 }]), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
if (url === "/api/query/analyze-editability") {
|
||||
return new Response(JSON.stringify({ editable: false, reason: "complex-source" }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
if (url === "/api/tab-runtime-cache" && init?.method === "POST") {
|
||||
const body = JSON.parse(String(init.body ?? "{}")) as { key: string; payloadBase64: string };
|
||||
runtimeCache.set(body.key, body.payloadBase64);
|
||||
return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
if (url.startsWith("/api/tab-runtime-cache?")) {
|
||||
const key = new URL(url, "http://localhost").searchParams.get("key") ?? "";
|
||||
if (init?.method === "DELETE") {
|
||||
runtimeCache.delete(key);
|
||||
return new Response(JSON.stringify(true), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
return new Response(JSON.stringify({ payloadBase64: runtimeCache.get(key) }), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
return new Response("unexpected request", { status: 500 });
|
||||
});
|
||||
|
||||
try {
|
||||
const tabId = store.createTab("conn-1", "db", "Query");
|
||||
store.toggleResultAutoSave(tabId);
|
||||
await store.executeTabSql(tabId, "select 1");
|
||||
await store.executeTabSql(tabId, "select 2");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId);
|
||||
assert.ok(tab?.resultRuns?.[0]);
|
||||
assert.ok(tab.resultRuns[1]);
|
||||
await waitFor(() => tab.resultRuns?.[0]?.result === undefined && tab.resultRuns?.[0]?.resultCacheState === "disk");
|
||||
|
||||
assert.equal(await store.removeResultRun(tabId, tab.resultRuns[1].id), true);
|
||||
|
||||
assert.equal(tab.activeResultRunId, tab.resultRuns[0]?.id);
|
||||
assert.deepEqual(tab.result?.columns, ["run_1"]);
|
||||
assert.deepEqual(tab.result?.rows, [[1]]);
|
||||
assert.deepEqual(tab.resultRuns[0]?.result?.columns, ["run_1"]);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
restoreStorage();
|
||||
}
|
||||
});
|
||||
|
||||
test("failed query executions append switchable error result runs", async () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
setActivePinia(createPinia());
|
||||
|
||||
Reference in new issue
Block a user