Skip to content
Closed
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

- Concurrent `IOperation.cancel()` calls on one operation now share a single cancel request, so the Thrift backend sends one `CancelOperation` instead of one per call (the kernel backend already sent one). If that request fails, every waiting call rejects and a later `cancel()` retries.
- Thrift CloudFetch downloads are now bounded end to end by `retriesTimeout` (default 15 minutes), including every attempt and retry backoff. Previously a stalled download, where the response headers or body never arrived, blocked the fetch forever. A download still running when the deadline expires is aborted and fails with `RetryError` (`TIMEOUT_EXCEEDED`, message `CloudFetch result download failed: Retry timeout exceeded`). (#520)
- Thrift connections made through `proxy` now apply `checkServerCertificate`, `customCaCert` and `clientCert`/`clientKey`. Previously the proxied path ignored them and verified against Node's process-wide trust store, so a certificate outside the configured trust set could be accepted. **Compatibility:** proxied connections now verify unless `checkServerCertificate: false` is set; as on direct connections, `NODE_TLS_REJECT_UNAUTHORIZED=0` no longer turns verification off. For an `https` proxy these options also apply to the proxy's own certificate. (#485)
- Honor the client `useCloudFetch: false` default on the kernel backend so complete results can be fetched without cloud-storage downloads. (#574)
Expand Down
22 changes: 18 additions & 4 deletions lib/DBSQLOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export default class DBSQLOperation implements IOperation {

private cancelled: boolean = false;

// In-flight cancel request, shared by concurrent cancel() calls.
private cancelPromise?: Promise<Status>;

public readonly _data?: unknown;

private metadata?: ResultMetadata;
Expand Down Expand Up @@ -207,10 +210,21 @@ export default class DBSQLOperation implements IOperation {
if (this.closed || this.cancelled) {
return Status.success();
}
const result = await this.backend.cancel();
this.cancelled = true;
this.onClose?.();
return result;
// Concurrent callers share one cancel request. It is cleared when it
// settles, so a failed cancel can be retried.
if (!this.cancelPromise) {
this.cancelPromise = (async () => {
try {
const result = await this.backend.cancel();
this.cancelled = true;
this.onClose?.();
return result;
} finally {
this.cancelPromise = undefined;
}
})();
}
return this.cancelPromise;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/DBSQLOperation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,34 @@ describe('DBSQLOperation', () => {
expect(operation['closed']).to.be.false;
});

it('should send one cancel request for concurrent calls', async () => {
const context = new ClientContextStub();
const driver = sinon.spy(context.driver);
const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context });

await Promise.all([operation.cancel(), operation.cancel(), operation.cancel()]);

expect(driver.cancelOperation.callCount).to.be.equal(1);
expect(operation['cancelled']).to.be.true;
});

it('should allow a retry after concurrent calls share a failed cancel', async () => {
const context = new ClientContextStub();
const driver = sinon.spy(context.driver);
context.driver.cancelOperationResp.status.statusCode = TStatusCode.ERROR_STATUS;
const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context });

const results = await Promise.allSettled([operation.cancel(), operation.cancel()]);
expect(results.map((r) => r.status)).to.deep.equal(['rejected', 'rejected']);
expect(driver.cancelOperation.callCount).to.be.equal(1);
expect(operation['cancelled']).to.be.false;

context.driver.cancelOperationResp.status.statusCode = TStatusCode.SUCCESS_STATUS;
await operation.cancel();
expect(driver.cancelOperation.callCount).to.be.equal(2);
expect(operation['cancelled']).to.be.true;
});

it('should return immediately if already closed', async () => {
const context = new ClientContextStub();
const driver = sinon.spy(context.driver);
Expand Down
Loading