Repository navigation
fix: serialize concurrent fetches on one operation across backends #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vuanhphung
wants to merge
9
commits into
main
Choose a base branch
from
vuanhphung/sea-overlap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
885f3c5
fix(kernel): preserve rows across overlapping result fetches
vuanhphung 9397550
fix(kernel): apply overlap review deltas
vuanhphung 87af336
fix: serialize concurrent fetches on one operation across backends
vuanhphung b2d8ccf
test: let readers queue before closing the operation
vuanhphung 98aace2
test: gate distinct Arrow source batches during concurrent reads
vuanhphung f79269a
test: cover queued Thrift reads and clarify concurrency guarantees
vuanhphung 0978bcf
test: select inline delivery explicitly in overlap cases
vuanhphung 4bb5f46
test: cover overlap in kernel CI and verify inline delivery
vuanhphung 6353d6d
test: skip only kernel overlap cases when native binding is absent
vuanhphung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { expect } from 'chai'; | ||
| import { DBSQLClient } from '../../../lib'; | ||
| import DBSQLOperation from '../../../lib/DBSQLOperation'; | ||
| import { LogLevel } from '../../../lib/contracts/IDBSQLLogger'; | ||
| import { ResultFormat } from '../../../lib/contracts/ResultMetadata'; | ||
| import { tryGetKernelNative } from '../../../lib/kernel/KernelNativeLoader'; | ||
| import config from '../utils/config'; | ||
|
|
||
| describe('Concurrent result fetching', () => { | ||
|
peco-review-bot[bot] marked this conversation as resolved.
Resolved
|
||
| for (const useKernel of [false, true]) { | ||
| for (const { cloudFetch, options } of [ | ||
| { cloudFetch: false, options: {} }, | ||
| { cloudFetch: false, options: { maxRows: 17 } }, | ||
| { cloudFetch: true, options: {} }, | ||
| ]) { | ||
| const delivery = cloudFetch ? 'CloudFetch' : `inline, ${JSON.stringify(options)}`; | ||
| const test = useKernel && !tryGetKernelNative() ? it.skip : it; | ||
| test(`returns each id once across overlapping fetchAll calls (${ | ||
| useKernel ? 'kernel' : 'thrift' | ||
| }, ${delivery})`, async () => { | ||
| const messages: string[] = []; | ||
| const client = new DBSQLClient({ | ||
| logger: { | ||
| log(level, message) { | ||
| if (level === LogLevel.info) messages.push(message); | ||
| }, | ||
| }, | ||
| }); | ||
|
peco-review-bot[bot] marked this conversation as resolved.
Resolved
|
||
| client.getConfig().useCloudFetch = useKernel || cloudFetch; | ||
| try { | ||
| await client.connect({ host: config.host, path: config.path, token: config.token, useKernel } as any); | ||
|
peco-review-bot[bot] marked this conversation as resolved.
Resolved
|
||
| const session = await client.openSession(); | ||
| try { | ||
| const rowCount = options.maxRows ? 1000 : 100000; | ||
| const query = cloudFetch | ||
| ? `SELECT id, concat( | ||
| sha2(CAST(id AS STRING), 512), sha2(CAST(id + 1 AS STRING), 512), | ||
| sha2(CAST(id + 2 AS STRING), 512), sha2(CAST(id + 3 AS STRING), 512) | ||
| ) AS payload FROM range(0, 100000) AS T(id) ORDER BY id` | ||
| : `SELECT id FROM range(0, ${rowCount}) AS T(id) ORDER BY id`; | ||
| const operation = await session.executeStatement(query); | ||
| try { | ||
| const chunks = await Promise.all([operation.fetchAll(options), operation.fetchAll(options)]); | ||
| const ids = chunks.flat().map((row: any) => Number(row.id)); | ||
| expect(ids.sort((left, right) => left - right)).to.deep.equal( | ||
| Array.from({ length: rowCount }, (unused, index) => index), | ||
| ); | ||
| expect(await operation.hasMoreRows()).to.equal(false); | ||
| if (useKernel) { | ||
| const reader = cloudFetch ? 'CloudFetch' : 'inline Arrow'; | ||
| expect(messages.some((message) => message.includes(`Using ${reader} reader:`))).to.equal(true); | ||
| } else if (cloudFetch) { | ||
| const metadata = await (operation as DBSQLOperation).getResultMetadata(); | ||
| expect(metadata.resultFormat).to.equal(ResultFormat.UrlBased); | ||
| } | ||
| } finally { | ||
| await operation.close(); | ||
| } | ||
| } finally { | ||
| await session.close(); | ||
| } | ||
| } finally { | ||
| await client.close(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import { expect } from 'chai'; | ||
| import sinon from 'sinon'; | ||
| import Int64 from 'node-int64'; | ||
| import { tableFromArrays, tableToIPC } from 'apache-arrow'; | ||
| import OperationStateError, { OperationStateErrorCode } from '../../lib/errors/OperationStateError'; | ||
| import { | ||
| TFetchOrientation, | ||
| TOperationState, | ||
| TOperationType, | ||
| TSparkRowSetType, | ||
| TStatusCode, | ||
| TTypeId, | ||
| } from '../../thrift/TCLIService_types'; | ||
| import ClientContextStub from './.stubs/ClientContextStub'; | ||
| import { createOperationForTest } from './.stubs/createOperationForTest'; | ||
|
|
||
| function createFinishedOperation(context: ClientContextStub) { | ||
| return createOperationForTest({ | ||
| context, | ||
| handle: { | ||
| operationId: { guid: Buffer.alloc(16), secret: Buffer.alloc(16) }, | ||
| operationType: TOperationType.EXECUTE_STATEMENT, | ||
| hasResultSet: true, | ||
| }, | ||
| directResults: { | ||
| operationStatus: { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| operationState: TOperationState.FINISHED_STATE, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| describe('Concurrent Thrift result consumption', () => { | ||
| for (const phase of ['metadata', 'fetch'] as const) { | ||
| it(`rejects a queued fetchChunk after close during an in-flight ${phase} request without further RPCs`, async () => { | ||
| const clock = sinon.useFakeTimers(); | ||
| const context = new ClientContextStub(); | ||
| let release!: () => void; | ||
| const gate = new Promise<void>((resolve) => { | ||
| release = resolve; | ||
| }); | ||
| const metadata = sinon.stub(context.driver, 'getResultSetMetadata').callsFake(async () => { | ||
| if (phase === 'metadata') { | ||
| await gate; | ||
| throw new Error('metadata request interrupted by close'); | ||
| } | ||
| return context.driver.getResultSetMetadataResp; | ||
| }); | ||
| const fetch = sinon.stub(context.driver, 'fetchResults').callsFake(async () => { | ||
| await gate; | ||
| return context.driver.fetchResultsResp; | ||
| }); | ||
| const operation = createFinishedOperation(context); | ||
| try { | ||
| const pending = Promise.allSettled([ | ||
| operation.fetchChunk({ maxRows: 1 }), | ||
| operation.fetchChunk({ maxRows: 1 }), | ||
| ]); | ||
| await clock.tickAsync(0); | ||
| const beforeClose = { metadata: metadata.callCount, fetch: fetch.callCount }; | ||
| expect(beforeClose).to.deep.equal({ metadata: 1, fetch: phase === 'fetch' ? 1 : 0 }); | ||
| await operation.close(); | ||
| release(); | ||
| await clock.runAllAsync(); | ||
| const outcomes = await pending; | ||
| expect({ metadata: metadata.callCount, fetch: fetch.callCount }).to.deep.equal(beforeClose); | ||
| expect(outcomes.map((outcome) => outcome.status)).to.deep.equal(['rejected', 'rejected']); | ||
| if (outcomes[1].status === 'rejected') { | ||
| expect(outcomes[1].reason).to.be.instanceOf(OperationStateError); | ||
| expect(outcomes[1].reason.errorCode).to.equal(OperationStateErrorCode.Closed); | ||
| } | ||
| } finally { | ||
| release(); | ||
| clock.restore(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| it('preserves an exact union through the Thrift Arrow pipeline when concurrent reads succeed', async () => { | ||
| const clock = sinon.useFakeTimers(); | ||
| const context = new ClientContextStub(); | ||
| let arrowSchema!: Buffer; | ||
| const batches = [ | ||
| [0, 1], | ||
| [2, 3], | ||
| [4, 5], | ||
| ].map((ids) => { | ||
| const ipc = Buffer.from(tableToIPC(tableFromArrays({ id: Int32Array.from(ids) }))); | ||
| const schemaEnd = 8 + ipc.readInt32LE(4); | ||
| arrowSchema = ipc.subarray(0, schemaEnd); | ||
| return { batch: ipc.subarray(schemaEnd), rowCount: new Int64(ids.length) }; | ||
| }); | ||
| context.driver.getResultSetMetadataResp = { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| resultFormat: TSparkRowSetType.ARROW_BASED_SET, | ||
| arrowSchema, | ||
| schema: { | ||
| columns: [ | ||
| { columnName: 'id', position: 1, typeDesc: { types: [{ primitiveEntry: { type: TTypeId.INT_TYPE } }] } }, | ||
| ], | ||
| }, | ||
| }; | ||
| let release!: () => void; | ||
| const gate = new Promise<void>((resolve) => { | ||
| release = resolve; | ||
| }); | ||
| let next = 0; | ||
| sinon.stub(context.driver, 'fetchResults').callsFake(async () => { | ||
| const index = next++; | ||
| await gate; | ||
| return { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| hasMoreRows: index < batches.length - 1, | ||
| results: { | ||
| startRowOffset: new Int64(index * 2), | ||
| rows: [], | ||
| arrowBatches: batches[index] ? [batches[index]] : [], | ||
| }, | ||
| }; | ||
| }); | ||
| const operation = createFinishedOperation(context); | ||
| try { | ||
| const pending = Promise.all([operation.fetchAll(), operation.fetchAll()]); | ||
| await clock.tickAsync(0); | ||
| release(); | ||
| await clock.runAllAsync(); | ||
| const chunks = await pending; | ||
| expect( | ||
| chunks | ||
| .flat() | ||
| .map((row: any) => row.id) | ||
| .sort((left, right) => left - right), | ||
| ).to.deep.equal([0, 1, 2, 3, 4, 5]); | ||
| } finally { | ||
| release(); | ||
| clock.restore(); | ||
| } | ||
| }); | ||
|
|
||
| it('sends FETCH_FIRST once when two first reads start without prefetched results', async () => { | ||
| const clock = sinon.useFakeTimers(); | ||
| const context = new ClientContextStub(); | ||
| const orientations: Array<TFetchOrientation | undefined> = []; | ||
| let release!: () => void; | ||
| const gate = new Promise<void>((resolve) => { | ||
| release = resolve; | ||
| }); | ||
| sinon.stub(context.driver, 'fetchResults').callsFake(async ({ orientation }) => { | ||
| orientations.push(orientation); | ||
| await gate; | ||
| return { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| hasMoreRows: false, | ||
| results: { | ||
| startRowOffset: new Int64(0), | ||
| rows: [], | ||
| columns: [ | ||
| { | ||
| i32Val: { | ||
| values: orientation === TFetchOrientation.FETCH_FIRST ? [0, 1, 2] : [], | ||
| nulls: Buffer.alloc(1), | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| }); | ||
| const operation = createOperationForTest({ | ||
| context, | ||
| handle: { | ||
| operationId: { guid: Buffer.alloc(16), secret: Buffer.alloc(16) }, | ||
| operationType: TOperationType.EXECUTE_STATEMENT, | ||
| hasResultSet: true, | ||
| }, | ||
| directResults: { | ||
| operationStatus: { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| operationState: TOperationState.FINISHED_STATE, | ||
| }, | ||
| resultSetMetadata: { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| resultFormat: TSparkRowSetType.COLUMN_BASED_SET, | ||
| schema: { | ||
| columns: [ | ||
| { columnName: 'id', position: 1, typeDesc: { types: [{ primitiveEntry: { type: TTypeId.INT_TYPE } }] } }, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| try { | ||
| const pending = Promise.all([operation.fetchAll(), operation.fetchAll()]); | ||
| await clock.tickAsync(0); | ||
| release(); | ||
| await clock.runAllAsync(); | ||
| const chunks = await pending; | ||
| expect({ | ||
| firstReads: orientations.filter((orientation) => orientation === TFetchOrientation.FETCH_FIRST).length, | ||
| ids: chunks.flat().map((row: any) => row.id), | ||
| }).to.deep.equal({ firstReads: 1, ids: [0, 1, 2] }); | ||
| } finally { | ||
| clock.restore(); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.