Fix QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING by appending query_id (#4173)

This commit is contained in:
Kamil Kisiela 2024-03-07 15:23:24 +01:00 committed by GitHub
parent aa52c1b613
commit a7e8515c42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -85,6 +85,8 @@ export class ClickHouse {
printWithValues(query).replace(/\n/g, ' ').replace(/\s+/g, ' '),
);
let retries = 0;
const response = await this.httpClient
.post<QueryResponse<T>>(endpoint, {
context: {
@ -145,6 +147,39 @@ export class ClickHouse {
http: httpAgent,
https: httpsAgent,
},
hooks: {
// `beforeRetry` runs first, then `beforeRequest`
beforeRequest: [
options => {
if (
retries > 0 &&
options.searchParams &&
typeof options.searchParams === 'object' &&
'query_id' in options.searchParams &&
typeof options.searchParams.query_id === 'string'
) {
// We do it to avoid QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING error in ClickHouse
// Context: https://clickhouse.com/docs/en/interfaces/http
// > Running requests do not stop automatically if the HTTP connection is lost.
// > The optional 'query_id' parameter can be passed as the query ID (any string).
// More context: https://clickhouse.com/docs/en/operations/settings/settings#replace-running-query
// > When using the HTTP interface, the 'query_id' parameter can be passed.
// > If a query from the same user with the same 'query_id' already exists at this time,
// > the behaviour depends on the 'replace_running_query' parameter.
// > Default: throws QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING exception.
options.searchParams.query_id = options.searchParams.query_id.replace(
/-r\d$/,
'-r' + retries,
);
}
},
],
beforeRetry: [
(_, retryCount) => {
retries = retryCount;
},
],
},
})
.then(response => {
if (response.exception) {