From 1d35db7da376a3c0bfc9f2f198effdbc6be9d2b2 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Thu, 19 Nov 2026 07:20:04 +0000 Subject: [PATCH 1/3] Reuse Thrift CloudFetch response buffers Signed-off-by: Vu Anh Phung Co-authored-by: Isaac --- CHANGELOG.md | 2 ++ lib/result/CloudFetchResultHandler.ts | 12 ++++----- .../result/CloudFetchResultHandler.test.ts | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 938983a3..7359ff2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- 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..71d8c44b 100644 --- a/lib/result/CloudFetchResultHandler.ts +++ b/lib/result/CloudFetchResultHandler.ts @@ -144,12 +144,12 @@ export default class CloudFetchResultHandler implements IResultsProvider { + 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 +175,9 @@ export default class CloudFetchResultHandler implements IResultsProvider { @@ -194,7 +194,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 exact 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('download deadline', () => { let server: http.Server | undefined; From 3b90851034f78350888a5547157a50213b38eb75 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Thu, 19 Nov 2026 15:36:52 +0000 Subject: [PATCH 2/3] Cover CloudFetch buffer reads on the real HTTP path Signed-off-by: Vu Anh Phung Co-authored-by: Isaac --- .../result/CloudFetchResultHandler.test.ts | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/unit/result/CloudFetchResultHandler.test.ts b/tests/unit/result/CloudFetchResultHandler.test.ts index ba6fd73c..f5257426 100644 --- a/tests/unit/result/CloudFetchResultHandler.test.ts +++ b/tests/unit/result/CloudFetchResultHandler.test.ts @@ -9,8 +9,9 @@ import { ShouldRetryResult } from '../../../lib/connection/contracts/IRetryPolic import { HttpTransactionDetails } from '../../../lib/connection/contracts/IConnectionProvider'; import { LogLevel } from '../../../lib/contracts/IDBSQLLogger'; import CloudFetchResultHandler from '../../../lib/result/CloudFetchResultHandler'; +import ArrowResultConverter from '../../../lib/result/ArrowResultConverter'; import ResultsProviderStub from '../.stubs/ResultsProviderStub'; -import { TRowSet, TSparkArrowResultLink, TStatusCode } from '../../../thrift/TCLIService_types'; +import { TRowSet, TSparkArrowResultLink, TStatusCode, TTypeId } from '../../../thrift/TCLIService_types'; import { ArrowBatch } from '../../../lib/result/utils'; import RetryError, { RetryErrorCode } from '../../../lib/errors/RetryError'; @@ -727,7 +728,7 @@ describe('CloudFetchResultHandler', () => { [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 exact response buffer without copying (${index})`, async () => { + 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, { @@ -815,6 +816,42 @@ 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'); + 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) => { From 36ee05a79cdfa261160780f3f6191456f564dce8 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Thu, 19 Nov 2026 15:49:59 +0000 Subject: [PATCH 3/3] Release completed CloudFetch body-reader listeners Detach only the listeners installed by node-fetch after each body read settles, preserving other listeners and late-error handling. Signed-off-by: Vu Anh Phung Co-authored-by: Isaac --- CHANGELOG.md | 2 + lib/result/CloudFetchResultHandler.ts | 25 +++++++- .../result/CloudFetchResultHandler.test.ts | 60 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7359ff2f..64b55aed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 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) diff --git a/lib/result/CloudFetchResultHandler.ts b/lib/result/CloudFetchResultHandler.ts index 71d8c44b..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'; @@ -149,6 +150,26 @@ export default class CloudFetchResultHandler implements IResultsProvider { + 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; @@ -194,7 +215,7 @@ export default class CloudFetchResultHandler implements IResultsProvider { }, ); + 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; @@ -840,6 +898,8 @@ describe('CloudFetchResultHandler', () => { 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);