fix(cassandra): use cursor pagination for table data and exports
Closes #7465
This commit is contained in:
1 parent
d452346082
commit
4190bab5ad
17 files changed
+1099
-88
No files matched your search
@@ -589,6 +589,70 @@ describe("useDataGridActions", () => {
|
||||
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT * FROM public.users LIMIT 100 OFFSET 100", expect.objectContaining({ appendResult: { maxRows: 10_000 } }));
|
||||
});
|
||||
|
||||
it("continues the matching Cassandra table cursor and client session", async () => {
|
||||
mocks.infiniteScroll = false;
|
||||
mocks.getConfig.mockReturnValue({ id: "cassandra-1", db_type: "cassandra" });
|
||||
mocks.buildTableSelectSql.mockResolvedValueOnce('SELECT * FROM "paged_rows";');
|
||||
const active = tableDataTab({ id: "tab-a" });
|
||||
const target = tableDataTab({
|
||||
id: "tab-b",
|
||||
connectionId: "cassandra-1",
|
||||
resultPageLimit: 100,
|
||||
resultPageOffset: 0,
|
||||
resultClientSessionId: "tab-b",
|
||||
result: {
|
||||
columns: ["id"],
|
||||
rows: Array.from({ length: 100 }, (_, index) => [index]),
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
session_id: "cassandra-page-1",
|
||||
has_more: true,
|
||||
},
|
||||
});
|
||||
mocks.tabs.push(active, target);
|
||||
const actions = useDataGridActions(computed(() => active));
|
||||
|
||||
await actions.onPaginate(target.id, 100, 100);
|
||||
|
||||
expect(mocks.executeTabSql).toHaveBeenCalledWith(
|
||||
"tab-b",
|
||||
'SELECT * FROM "paged_rows";',
|
||||
expect.objectContaining({
|
||||
pagination: { offset: 100, limit: 100, sessionId: "cassandra-page-1", clientSessionId: "tab-b" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("restarts Cassandra table cursors for jumps and page-size changes", async () => {
|
||||
mocks.infiniteScroll = false;
|
||||
mocks.getConfig.mockReturnValue({ id: "cassandra-1", db_type: "cassandra" });
|
||||
mocks.buildTableSelectSql.mockResolvedValue('SELECT * FROM "paged_rows";');
|
||||
const tab = tableDataTab({
|
||||
connectionId: "cassandra-1",
|
||||
resultPageLimit: 100,
|
||||
resultPageOffset: 0,
|
||||
resultClientSessionId: "tab-1",
|
||||
result: {
|
||||
columns: ["id"],
|
||||
rows: Array.from({ length: 100 }, (_, index) => [index]),
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
session_id: "cassandra-page-1",
|
||||
has_more: true,
|
||||
},
|
||||
});
|
||||
mocks.tabs.push(tab);
|
||||
const actions = useDataGridActions(computed(() => tab));
|
||||
|
||||
await actions.onPaginate(tab.id, 300, 100);
|
||||
await actions.onPaginate(tab.id, 100, 50);
|
||||
|
||||
expect(mocks.executeTabSql.mock.calls.map((call) => call[2].pagination)).toEqual([
|
||||
{ offset: 300, limit: 100, sessionId: undefined, clientSessionId: undefined },
|
||||
{ offset: 100, limit: 50, sessionId: undefined, clientSessionId: undefined },
|
||||
]);
|
||||
});
|
||||
|
||||
it("ignores a stale structured order when its column was renamed", async () => {
|
||||
const tab = tableDataTab({
|
||||
resultSortColumn: "old_name",
|
||||
|
||||
@@ -8,6 +8,8 @@ const addExportTaskMock = vi.hoisted(() => vi.fn());
|
||||
const updateTableExportTaskMock = vi.hoisted(() => vi.fn());
|
||||
const apiMock = vi.hoisted(() => ({
|
||||
buildTableSelectSql: vi.fn(async () => 'SELECT * FROM "main"."users" LIMIT 10000'),
|
||||
closeClientConnectionSession: vi.fn(),
|
||||
closeQuerySession: vi.fn(),
|
||||
executeQuery: vi.fn(),
|
||||
exportQueryResultCsv: vi.fn(),
|
||||
exportQueryResultJson: vi.fn(),
|
||||
@@ -242,6 +244,70 @@ describe("useSidebarTreeExportRuntime", () => {
|
||||
expect(toastMock).toHaveBeenCalledWith("导出失败:上一个 DuckDB 查询仍在停止中,请稍后重试。", 5000);
|
||||
});
|
||||
|
||||
it("isolates and advances Cassandra JSON export cursors", async () => {
|
||||
apiMock.buildTableSelectSql.mockResolvedValue('SELECT * FROM "events";');
|
||||
apiMock.executeQuery.mockResolvedValueOnce({ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-1", has_more: true }).mockResolvedValueOnce({ columns: ["id"], rows: [[2]], affected_rows: 0, execution_time_ms: 1, has_more: false });
|
||||
const activeNode = shallowRef({ id: "table-1", type: "table", label: "events", connectionId: "cassandra-1", database: "app", children: [] } as TreeNode);
|
||||
const connectionStore = {
|
||||
ensureConnected: vi.fn(),
|
||||
getConfig: vi.fn(() => ({ db_type: "cassandra", query_timeout_secs: 30 })),
|
||||
connectionIdentifierQuote: vi.fn(() => '"'),
|
||||
treeNodes: [],
|
||||
selectedTreeNodeIds: [],
|
||||
};
|
||||
const runtime = useSidebarTreeExportRuntime({
|
||||
activeNode,
|
||||
connectionStore: connectionStore as never,
|
||||
settingsStore: exportSettings() as never,
|
||||
acceptedSelectionIds: () => null,
|
||||
});
|
||||
|
||||
await runtime.exportData("json");
|
||||
|
||||
const firstOptions = apiMock.executeQuery.mock.calls[0]?.[5];
|
||||
const secondOptions = apiMock.executeQuery.mock.calls[1]?.[5];
|
||||
expect(firstOptions).toMatchObject({ maxRows: 2_147_483_647, fetchSize: 10_000, pageSize: 10_000, resultSessionId: undefined, timeoutSecs: 30 });
|
||||
expect(secondOptions).toMatchObject({ maxRows: 2_147_483_647, fetchSize: 10_000, pageSize: 10_000, resultSessionId: "cursor-1", clientSessionId: firstOptions.clientSessionId, timeoutSecs: 30 });
|
||||
expect(firstOptions.clientSessionId).toMatch(/^table-export:/);
|
||||
expect(apiMock.exportQueryResultJson).toHaveBeenCalledWith("events.json", ["id"], [[1], [2]]);
|
||||
expect(apiMock.closeClientConnectionSession).toHaveBeenCalledWith("cassandra-1", "app", firstOptions.clientSessionId, undefined);
|
||||
});
|
||||
|
||||
it("closes a failed Cassandra cursor before disposing its client session", async () => {
|
||||
apiMock.buildTableSelectSql.mockResolvedValue('SELECT * FROM "events";');
|
||||
apiMock.executeQuery.mockResolvedValueOnce({ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1, session_id: "cursor-1", has_more: true }).mockRejectedValueOnce(new Error("cursor lost"));
|
||||
const closing = deferred<void>();
|
||||
apiMock.closeQuerySession.mockReturnValueOnce(closing.promise);
|
||||
const runtime = useSidebarTreeExportRuntime({
|
||||
activeNode: shallowRef({ id: "table-1", type: "table", label: "events", connectionId: "cassandra-1", database: "app", children: [] } as TreeNode),
|
||||
connectionStore: { ensureConnected: vi.fn(), getConfig: () => ({ db_type: "cassandra" }), connectionIdentifierQuote: () => '"', treeNodes: [], selectedTreeNodeIds: [] } as never,
|
||||
settingsStore: exportSettings() as never,
|
||||
acceptedSelectionIds: () => null,
|
||||
});
|
||||
const exported = runtime.exportData("json");
|
||||
await vi.waitFor(() => expect(apiMock.closeQuerySession).toHaveBeenCalled());
|
||||
expect(apiMock.closeClientConnectionSession).not.toHaveBeenCalled();
|
||||
closing.reject(new Error("close failed"));
|
||||
await exported;
|
||||
expect(apiMock.closeClientConnectionSession).toHaveBeenCalledTimes(1);
|
||||
expect(apiMock.exportQueryResultJson).not.toHaveBeenCalled();
|
||||
expect(toastMock).toHaveBeenCalledWith("导出失败:cursor lost", 5000);
|
||||
});
|
||||
|
||||
it("preserves SQL Server legacy JSON export without enabling a new cursor path", async () => {
|
||||
apiMock.executeQuery.mockResolvedValueOnce({ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 });
|
||||
const runtime = useSidebarTreeExportRuntime({
|
||||
activeNode: shallowRef({ id: "table-1", type: "table", label: "events", connectionId: "sqlserver-1", database: "app", children: [] } as TreeNode),
|
||||
connectionStore: { ensureConnected: vi.fn(), getConfig: () => ({ db_type: "sqlserver", driver_profile: "sqlserver-legacy" }), connectionIdentifierQuote: () => '"', treeNodes: [], selectedTreeNodeIds: [] } as never,
|
||||
settingsStore: exportSettings() as never,
|
||||
acceptedSelectionIds: () => null,
|
||||
});
|
||||
await runtime.exportData("json");
|
||||
expect(apiMock.executeQuery.mock.calls[0]).toHaveLength(3);
|
||||
expect(apiMock.closeClientConnectionSession).not.toHaveBeenCalled();
|
||||
expect(apiMock.exportQueryResultJson).toHaveBeenCalledWith("events.json", ["id"], [[1]]);
|
||||
});
|
||||
|
||||
it("exports a mongo collection through the save-file path without a setup dialog", async () => {
|
||||
apiMock.exportMongodbQuery.mockImplementation(async (_request, onProgress) => {
|
||||
onProgress({ exportId: "export-1", status: "done", documentsRead: 3, bytesWritten: 12, elapsedMs: 4 });
|
||||
|
||||
@@ -23,6 +23,7 @@ import type { DataGridReloadIntent } from "@/lib/dataGrid/dataGridToolbar";
|
||||
import { continuousQueryResultMaxRows } from "@/lib/dataGrid/queryResultRowLimit";
|
||||
import { queryResultBaseSql, queryResultExecutionSql } from "@/lib/tabs/tabPresentation";
|
||||
import { sqlExecutionTargetCapabilities } from "@/lib/database/sqlExecutionTargetCapabilities";
|
||||
import { usesAgentCursorForTableData } from "@/lib/database/databaseDriverManifest";
|
||||
|
||||
const DATA_TAB_METADATA_TTL_MS = TABLE_METADATA_CACHE_TTL_MS;
|
||||
|
||||
@@ -602,8 +603,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
||||
const expectedNextOffset = appendResult ? tab.result?.rows.length : (tab.resultPageOffset ?? 0) + (tab.resultPageLimit ?? limit);
|
||||
const continuesResultSession = offset === expectedNextOffset && limit === tab.resultPageLimit;
|
||||
const connection = useConnectionStore().getConfig(tab.connectionId);
|
||||
const isSqlServerLegacy = connection?.db_type === "sqlserver" && connection.driver_profile?.trim().toLowerCase() === "sqlserver-legacy";
|
||||
const sessionId = isSqlServerLegacy && tab.result?.has_more && tab.result.session_id && continuesResultSession ? tab.result.session_id : undefined;
|
||||
const sessionId = usesAgentCursorForTableData(connection?.db_type, connection?.driver_profile) && tab.result?.has_more && tab.result.session_id && continuesResultSession ? tab.result.session_id : undefined;
|
||||
await queryStore.executeTabSql(tab.id, sql, {
|
||||
pagination: { offset, limit, sessionId, clientSessionId: sessionId ? tab.resultClientSessionId : undefined },
|
||||
...appendOptions,
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
} from "@/components/sidebar/sidebarTreeDialogState";
|
||||
import type { CsvQuoteMode } from "@/lib/export/csvQuoteMode";
|
||||
import type { SqlInsertDialect } from "@/lib/export/sqlInsertMode";
|
||||
import { uuid } from "@/lib/common/utils";
|
||||
|
||||
type StructureCopyFormat = "tsv" | "markdown";
|
||||
|
||||
@@ -355,6 +356,8 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
|
||||
try {
|
||||
await connectionStore.ensureConnected(connectionId);
|
||||
const queryColumns = config.db_type === "neo4j" ? (await api.getColumns(connectionId, database, target.metadataSchema, target.tableName, target.catalog)).map((column) => column.name) : undefined;
|
||||
const useAgentCursor = config.db_type === "cassandra";
|
||||
const clientSessionId = useAgentCursor ? `table-export:${uuid()}` : undefined;
|
||||
const result = await fetchTableDataForExport({
|
||||
databaseType: target.databaseType,
|
||||
identifierQuote: target.identifierQuote,
|
||||
@@ -362,7 +365,17 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
|
||||
tableName: target.tableName,
|
||||
tableType: target.tableType,
|
||||
columns: queryColumns,
|
||||
executePage: (sql) => api.executeQuery(connectionId, database, sql),
|
||||
useAgentCursor,
|
||||
executePage: (sql, cursorOptions) => (cursorOptions ? api.executeQuery(connectionId, database, sql, undefined, undefined, { ...cursorOptions, clientSessionId, catalog: target.catalog, timeoutSecs: config.query_timeout_secs }) : api.executeQuery(connectionId, database, sql)),
|
||||
closeCursor: useAgentCursor
|
||||
? async (sessionId) => {
|
||||
try {
|
||||
if (sessionId) await api.closeQuerySession(connectionId, database, sessionId, clientSessionId, target.catalog);
|
||||
} finally {
|
||||
await api.closeClientConnectionSession(connectionId, database, clientSessionId!, target.catalog);
|
||||
}
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
|
||||
const outputPath = await resolveTableExportOutputPath(target, "json", outputDirectory);
|
||||
|
||||
Reference in new issue
Block a user