Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Avoid constructing an end-of-buffer exception after every complete Thrift response.
- 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
8 changes: 7 additions & 1 deletion lib/connection/connections/ThriftHttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ export default class ThriftHttpConnection extends EventEmitter {
});
}

private handleThriftResponse(transportWithData: TTransport) {
private handleThriftResponse(
transportWithData: TTransport & { borrow?(): { readIndex: number; writeIndex: number } },
) {
if (!this.client) {
throw new Thrift.TApplicationException(Thrift.TApplicationExceptionType.INTERNAL_ERROR, 'Client not available');
}
Expand Down Expand Up @@ -278,6 +280,10 @@ export default class ThriftHttpConnection extends EventEmitter {
'Received a response to an unknown RPC function',
);
}
const remaining = transportWithData.borrow?.();
if (remaining && remaining.readIndex === remaining.writeIndex) {
break;
}
}
} catch (error) {
if (error instanceof InputBufferUnderrunError) {
Expand Down
125 changes: 125 additions & 0 deletions tests/unit/connection/connections/ThriftHttpResponse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { TBinaryProtocol, TBufferedTransport, Thrift } from 'thrift';
import ThriftHttpConnection from '../../../../lib/connection/connections/ThriftHttpConnection';
import NullRetryPolicy from '../../../../lib/connection/connections/NullRetryPolicy';

describe('Thrift HTTP response boundaries', () => {
function reply(sequenceId: number, value: string | null, method = 'Reply'): Buffer {
let result = Buffer.alloc(0);
const output = new TBinaryProtocol(
new TBufferedTransport(undefined, (buffer) => {
result = buffer as Buffer;
}),
);
output.writeMessageBegin(method, Thrift.MessageType.REPLY, sequenceId);
output.writeBool(value !== null);
if (value !== null) output.writeString(value);
output.writeMessageEnd();
output.flush();
return result;
}

function reader({ legacy = false, decodeError }: { legacy?: boolean; decodeError?: Error } = {}) {
let reads = 0;
class CountingProtocol extends TBinaryProtocol {
readMessageBegin() {
reads += 1;
return super.readMessageBegin();
}
}
const connection = new ThriftHttpConnection({
url: 'http://localhost/',
protocol: CountingProtocol,
getRetryPolicy: async () => new NullRetryPolicy(),
});
const callbacks = [sinon.spy(), sinon.spy(), sinon.spy()];
const requests: Record<number, Function> = Object.fromEntries(
callbacks.map((callback, index) => [index + 1, callback]),
);
connection.client = {
_reqs: requests,
recv_Reply(input: TBinaryProtocol, messageType: number, sequenceId: number) {
if (decodeError) throw decodeError;
expect(messageType).to.equal(Thrift.MessageType.REPLY);
const value = input.readBool() ? input.readString() : null;
input.readMessageEnd();
const callback = requests[sequenceId];
delete requests[sequenceId];
callback(null, value);
},
} as any;
const receive = TBufferedTransport.receiver((input) => {
if (legacy) Object.defineProperty(input, 'borrow', { value: undefined });
connection['handleThriftResponse'](input);
}, 0);
return { callbacks, requests, receive, reads: () => reads };
}

for (const value of [null, '', 'a short result', 'x'.repeat(4096)]) {
it(`does not attempt another message after a complete ${
value === null ? 'null' : `${value.length}-byte`
} result`, async () => {
const input = reader();
input.receive(reply(1, value));
await new Promise<void>((resolve) => setImmediate(resolve));
expect(input.callbacks[0].calledOnceWithExactly(null, value)).to.equal(true);
expect(input.reads()).to.equal(1);
expect(input.requests).not.to.have.property('-1');
});
}

it('decodes every response in a buffer and dispatches by sequence ID', async () => {
const input = reader();
input.receive(Buffer.concat([reply(3, 'third'), reply(1, null), reply(2, 'second')]));
await new Promise<void>((resolve) => setImmediate(resolve));
expect(input.callbacks[0].calledOnceWithExactly(null, null)).to.equal(true);
expect(input.callbacks[1].calledOnceWithExactly(null, 'second')).to.equal(true);
expect(input.callbacks[2].calledOnceWithExactly(null, 'third')).to.equal(true);
expect(input.reads()).to.equal(3);
expect(input.requests).to.deep.equal({});
});

it('keeps the underrun handling for an empty response', () => {
const input = reader();
input.receive(Buffer.alloc(0));
expect(input.reads()).to.equal(1);
expect(input.callbacks.every((callback) => callback.notCalled)).to.equal(true);
});

it('rolls back a partial response and resumes without repeating completed callbacks', async () => {
const input = reader();
const partial = reply(2, 'second');
input.receive(Buffer.concat([reply(1, 'first'), partial.subarray(0, partial.length - 2)]));
await new Promise<void>((resolve) => setImmediate(resolve));
expect(input.callbacks[0].calledOnceWithExactly(null, 'first')).to.equal(true);
expect(input.callbacks[1].notCalled).to.equal(true);
input.receive(partial.subarray(partial.length - 2));
await new Promise<void>((resolve) => setImmediate(resolve));
expect(input.callbacks[0].calledOnce).to.equal(true);
expect(input.callbacks[1].calledOnceWithExactly(null, 'second')).to.equal(true);
});

it('keeps the underrun fallback for transports without borrow', async () => {
const input = reader({ legacy: true });
input.receive(reply(1, 'legacy'));
await new Promise<void>((resolve) => setImmediate(resolve));
expect(input.callbacks[0].calledOnceWithExactly(null, 'legacy')).to.equal(true);
expect(input.reads()).to.equal(2);
});

it('still rejects unknown methods and removes their temporary callback', () => {
const input = reader();
expect(() => input.receive(reply(1, 'unexpected', 'Unknown'))).to.throw(
'Received a response to an unknown RPC function',
);
expect(input.requests).not.to.have.property('-1');
expect(input.callbacks[0].notCalled).to.equal(true);
});

it('does not swallow decoder errors', () => {
const error = new Error('decoder failure');
const input = reader({ decodeError: error });
expect(() => input.receive(reply(1, 'value'))).to.throw(error);
});
});
Loading