diff --git a/CHANGELOG.md b/CHANGELOG.md index 938983a3..64b55aed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +Release completed Thrift CloudFetch body-reader listeners so idle connections do not retain downloaded result buffers. + +- Avoid an extra full-buffer copy when downloading Thrift CloudFetch results. + - The Thrift token federation exchange (`enableTokenFederation`) now goes through the connection's agent, so `proxy`, `checkServerCertificate`, `customCaCert` and `clientCert`/`clientKey` apply to it. Previously it connected directly with Node's default agent, so behind a required proxy the exchange failed and the driver fell back to the original token. (#611) - Kernel backend: a failed status poll while waiting for a statement (a `runAsync: true` statement, or a default execution that outlives the server's inline wait) and a failed result fetch in `hasMoreRows()` now throw the mapped driver error, such as `HiveDriverError: HTTP 500: Internal Server Error` once retries on HTTP 5xx run out, instead of a raw `Error` whose message is the internal `__databricks_error__:{...}` envelope. As in `fetchChunk()`, a failed fetch in `hasMoreRows()` also closes the statement. (#608) - Honor `cloudFetchConcurrentDownloads` when opening kernel sessions, with a default window of 16 and explicit chunk-setting overrides. Values other than positive safe integers raise `HiveDriverError` at session open. diff --git a/lib/result/CloudFetchResultHandler.ts b/lib/result/CloudFetchResultHandler.ts index fbfe5c7c..bf28511f 100644 --- a/lib/result/CloudFetchResultHandler.ts +++ b/lib/result/CloudFetchResultHandler.ts @@ -1,4 +1,5 @@ import fetch, { RequestInfo, RequestInit, Request, Response } from 'node-fetch'; +import { Readable } from 'stream'; import { TGetResultSetMetadataResp, TRowSet, TSparkArrowResultLink } from '../../thrift/TCLIService_types'; import HiveDriverError from '../errors/HiveDriverError'; import RetryError, { RetryErrorCode } from '../errors/RetryError'; @@ -144,12 +145,32 @@ export default class CloudFetchResultHandler implements IResultsProvider { + private async readBody(response: Response): Promise { + const { body } = response; + if (!(body instanceof Readable)) { + return response.buffer(); + } + const events = ['data', 'end', 'error']; + const existing = events.map((event) => body.listeners(event)); + const result = response.buffer(); + const added = events.map((event, index) => + body.listeners(event).filter((listener) => !existing[index].includes(listener)), + ); + try { + return await result; + } finally { + events.forEach((event, index) => { + added[index].forEach((listener) => body.removeListener(event, listener as () => void)); + }); + } + } + + private async fetch(url: RequestInfo, init?: RequestInit): Promise<{ response: Response; body?: Buffer }> { const connectionProvider = await this.context.getConnectionProvider(); let requestUrl = url; if (typeof requestUrl !== 'string') { @@ -175,9 +196,9 @@ export default class CloudFetchResultHandler implements IResultsProvider { @@ -194,7 +215,7 @@ export default class CloudFetchResultHandler implements IResultsProvider { expect(batches).to.deep.eq([sampleArrowBatch]); }); + [Buffer.alloc(0), Buffer.from([0, 255, 128, 42]), Buffer.from([99, 0, 255, 128, 42, 98]).subarray(1, 5)].forEach( + (body, index) => { + it(`should reuse the fallback response buffer without copying (${index})`, async () => { + const context = new ClientContextStub({ cloudFetchConcurrentDownloads: 1 }); + const rowSetProvider = new ResultsProviderStub([sampleRowSet1], undefined); + const result = new CloudFetchResultHandler(context, rowSetProvider, { + lz4Compressed: false, + status: { statusCode: TStatusCode.SUCCESS_STATUS }, + }); + const response = new Response(body, { status: 200 }); + const buffer = sinon.spy(response, 'buffer'); + const arrayBuffer = sinon.spy(response, 'arrayBuffer'); + context.invokeWithRetryStub.resolves({ request: new Request('localhost'), response }); + + const { batches, rowCount } = await result.fetchNext({ limit: 10000 }); + + expect(batches).to.have.lengthOf(1); + expect(batches[0]).to.equal(body); + expect(batches[0].byteLength).to.equal(body.byteLength); + expect(rowCount).to.equal(1); + expect(buffer.calledOnce).to.be.true; + expect(arrayBuffer.called).to.be.false; + }); + }, + ); + + describe('response body retention', () => { + function createHandler(response: Response) { + const context = new ClientContextStub({ cloudFetchConcurrentDownloads: 1 }); + context.invokeWithRetryStub.resolves({ request: new Request('http://localhost'), response }); + return new CloudFetchResultHandler(context, new ResultsProviderStub([sampleRowSet1], undefined), { + status: { statusCode: TStatusCode.SUCCESS_STATUS }, + }); + } + + it('releases consumed body listeners without removing existing or later listeners', async () => { + const body = new PassThrough(); + const response = new Response(body); + const existing = ['data', 'end', 'error'].map((event) => { + const listener = sinon.spy(); + body.on(event, listener); + return listener; + }); + const later = sinon.spy(); + body.once('data', () => body.on('error', later)); + const errorsBefore = body.listeners('error'); + const handler = createHandler(response); + setImmediate(() => body.end(sampleArrowBatch)); + + const batch = await handler.fetchNext({ limit: 10000 }); + + expect(batch.batches).to.deep.eq([sampleArrowBatch]); + expect(response.bodyUsed).to.be.true; + expect(body.listeners('data')).to.deep.eq([existing[0]]); + expect(body.listeners('end')).to.deep.eq([existing[1]]); + expect(body.listeners('error')).to.deep.eq([...errorsBefore, later]); + body.emit('error', new Error('late stream error')); + expect(existing[2].calledOnce).to.be.true; + expect(later.calledOnce).to.be.true; + }); + + it('releases body reader listeners when consumption rejects', async () => { + const body = new PassThrough(); + const response = new Response(body); + const errorsBefore = body.listeners('error'); + const handler = createHandler(response); + setImmediate(() => body.destroy(new Error('body interrupted'))); + + let thrown: unknown; + try { + await handler.fetchNext({ limit: 10000 }); + } catch (error) { + thrown = error; + } + + expect(thrown).to.be.instanceOf(Error); + expect(response.bodyUsed).to.be.true; + expect(body.listenerCount('data')).to.eq(0); + expect(body.listenerCount('end')).to.eq(0); + expect(body.listeners('error')).to.deep.eq(errorsBefore); + }); + }); + describe('download deadline', () => { let server: http.Server | undefined; @@ -789,6 +874,44 @@ describe('CloudFetchResultHandler', () => { return { ...outcome, elapsedMs: Date.now() - startedAt }; } + it('should decode a pooled Arrow response read inside the retry callback', async () => { + const body = Buffer.concat([sampleArrowSchema, sampleArrowBatch]); + const url = await startServer((req, res) => res.end(body)); + const handler = createHandler(url, { retriesTimeout: 1000 }); + const buffer = sinon.spy(Response.prototype, 'buffer'); + const arrayBuffer = sinon.spy(Response.prototype, 'arrayBuffer'); + const fetchNext = sinon.spy(handler, 'fetchNext'); + const converter = new ArrowResultConverter(new BaseClientContextStub(), handler, { + schema: { + columns: [ + { + columnName: '1', + typeDesc: { types: [{ primitiveEntry: { type: TTypeId.INT_TYPE } }] }, + position: 1, + }, + ], + }, + }); + + try { + expect(await converter.fetchNext({ limit: 10000 })).to.deep.eq([{ 1: 1 }]); + expect(buffer.calledOnce).to.be.true; + expect(arrayBuffer.called).to.be.false; + expect(buffer.firstCall.thisValue.body).to.have.property('pipe').that.is.a('function'); + expect(buffer.firstCall.thisValue.body.listenerCount('data')).to.eq(0); + expect(buffer.firstCall.thisValue.body.listenerCount('end')).to.eq(0); + const downloaded = await buffer.firstCall.returnValue; + expect(downloaded).to.deep.eq(body); + expect(downloaded.length).to.be.lt(Buffer.poolSize >>> 1); + expect(downloaded.buffer.byteLength).to.be.gt(downloaded.length); + expect((await fetchNext.firstCall.returnValue).batches[0]).to.equal(downloaded); + } finally { + fetchNext.restore(); + arrayBuffer.restore(); + buffer.restore(); + } + }); + it('should fail a download whose response headers never arrive with a timeout error', async () => { const closedRequests: Array> = []; const url = await startServer((req) => {