Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions lib/result/CloudFetchResultHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ export default class CloudFetchResultHandler implements IResultsProvider<ArrowBa
this.chunkIndex += 1;

return {
batches: [Buffer.from(result)],
batches: [result],
rowCount: link.rowCount.toNumber(true),
};
}

private async fetch(url: RequestInfo, init?: RequestInit): Promise<{ response: Response; body?: ArrayBuffer }> {
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') {
Expand All @@ -175,9 +175,9 @@ export default class CloudFetchResultHandler implements IResultsProvider<ArrowBa
// Read the body inside the retried block. CloudFetch downloads are large
// GETs against pre-signed cloud-storage URLs that frequently surface
// `socket hang up` / "Premature close" once the stream is mid-transfer.
// Pulling `arrayBuffer()` in here lets the retry policy treat those
// Pulling `buffer()` in here lets the retry policy treat those
// body-stream failures the same as connect-time failures.
let downloaded: ArrayBuffer | undefined;
let downloaded: Buffer | undefined;
let result: { request: Request; response: Response };
try {
const download = retryPolicy.invokeWithRetry(async () => {
Expand All @@ -194,7 +194,7 @@ export default class CloudFetchResultHandler implements IResultsProvider<ArrowBa
try {
const response = await fetch(request);
if (response.ok) {
downloaded = await response.arrayBuffer();
downloaded = await response.buffer();
} else {
lastFailure = { request, response };
}
Expand Down Expand Up @@ -232,7 +232,7 @@ export default class CloudFetchResultHandler implements IResultsProvider<ArrowBa
// callback). In production the body is always read inside the retried
// block above, so this path is a no-op.
if (downloaded === undefined && result.response.ok) {
downloaded = await result.response.arrayBuffer();
downloaded = await result.response.buffer();
}
return { response: result.response, body: downloaded };
}
Expand Down
65 changes: 64 additions & 1 deletion tests/unit/result/CloudFetchResultHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -725,6 +726,32 @@ describe('CloudFetchResultHandler', () => {
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('download deadline', () => {
let server: http.Server | undefined;

Expand Down Expand Up @@ -789,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<Promise<void>> = [];
const url = await startServer((req) => {
Expand Down
Loading