Repository navigation
fix(common-utils): run at most four filter value reads at once per connection #3901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@hyperdx/common-utils': patch | ||
| '@hyperdx/app': patch | ||
| '@hyperdx/api': patch | ||
| --- | ||
|
|
||
| fix: run at most four filter value text index reads at once per connection in | ||
| each browser tab or API process. The search filter panel used to start every | ||
| batch together, so on a slow service one page load held about ten proxy requests | ||
| open per Map column. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,9 @@ const TEXT_INDEX_KEYS_PER_QUERY = 10; | |
| // Smallest dictionary block size that gets one read per key; see | ||
| // getMapTextIndexKeyValues. | ||
| const PER_KEY_TEXT_INDEX_MIN_DICTIONARY_BLOCK_SIZE = 2048; | ||
| // Text index values reads in flight per connection. Each holds a proxy socket | ||
| // for the whole read, which on a slow service is tens of seconds. HDX-6272 | ||
| const MAX_CONCURRENT_TEXT_INDEX_READS = 4; | ||
|
|
||
| /** | ||
| * The index read could not be scoped to the requested date range, so it was | ||
|
|
@@ -161,6 +164,46 @@ async function* inCompletionOrder<T>( | |
| } | ||
| } | ||
|
|
||
| /** Runs at most `max` tasks at once; the rest start in call order. */ | ||
| function createLimiter(max: number) { | ||
| let active = 0; | ||
| const waiting: Array<() => void> = []; | ||
| return async <T>(task: () => Promise<T>): Promise<T> => { | ||
| if (active < max) active++; | ||
| // The finishing task hands its slot straight over, so `active` is unchanged. | ||
| else await new Promise<void>(resolve => waiting.push(resolve)); | ||
| try { | ||
| return await task(); | ||
| } finally { | ||
| const next = waiting.shift(); | ||
| if (next) next(); | ||
| else active--; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // Module-level: each hook builds its own Metadata, but they share a connection. | ||
| const textIndexReadLimiters = new Map< | ||
| string, | ||
| ReturnType<typeof createLimiter> | ||
| >(); | ||
| const limitTextIndexRead = <T>( | ||
| connectionId: string, | ||
| signal: AbortSignal | undefined, | ||
| task: () => Promise<T>, | ||
| ): Promise<T> => { | ||
| let limiter = textIndexReadLimiters.get(connectionId); | ||
| if (!limiter) { | ||
| limiter = createLimiter(MAX_CONCURRENT_TEXT_INDEX_READS); | ||
| textIndexReadLimiters.set(connectionId, limiter); | ||
| } | ||
| return limiter(() => { | ||
| // The node client ignores a signal that aborted before the query starts. | ||
| signal?.throwIfAborted(); | ||
| return task(); | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abort check misses pre-query awaitMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 6fff982. Configure here. |
||
| }; | ||
|
|
||
| export type MetricNames = { | ||
| names: string[]; | ||
| /** True when more names matched than `limit`, so the page is incomplete. */ | ||
|
|
@@ -1941,41 +1984,41 @@ export class Metadata { | |
| } | ||
| const sql = concatChSql(' UNION ALL ', sqlBranches); | ||
|
|
||
| return await this.clickhouseClient | ||
| .query<'JSON'>({ | ||
| query: sql.sql, | ||
| query_params: sql.params, | ||
| connectionId, | ||
| clickhouse_settings: perKey | ||
| ? { | ||
| ...this.getClickHouseSettings(), | ||
| // The per-key LIMIT bounds each read. A row cap would stop | ||
| // the sorted dictionary read partway and leave the keys that | ||
| // sort after it with no values. | ||
| max_rows_to_read: '0', | ||
| } | ||
| : { | ||
| // shortcut: bounds the shared read, but keys that sort after | ||
| // a high-cardinality key can come back with no values; drop | ||
| // it if that matters more than the read's cost. | ||
| max_rows_to_read: String( | ||
| this.getClickHouseSettings().max_rows_to_read ?? | ||
| DEFAULT_METADATA_MAX_ROWS_TO_READ, | ||
| ), | ||
| read_overflow_mode: 'break', | ||
| ...this.getClickHouseSettings(), | ||
| }, | ||
| abort_signal: signal, | ||
| }) | ||
| .then(res => | ||
| res.json<{ column: string; key: string; value: string[] }>(), | ||
| ) | ||
| .then(d => | ||
| d.data.map(row => ({ | ||
| key: `${row.column}['${row.key}']`, | ||
| value: row.value, | ||
| })), | ||
| ); | ||
| const result = await limitTextIndexRead(connectionId, signal, () => | ||
| this.clickhouseClient | ||
| .query<'JSON'>({ | ||
| query: sql.sql, | ||
| query_params: sql.params, | ||
| connectionId, | ||
| clickhouse_settings: perKey | ||
| ? { | ||
| ...this.getClickHouseSettings(), | ||
| // The per-key LIMIT bounds each read. A row cap would stop | ||
| // the sorted dictionary read partway and leave the keys that | ||
| // sort after it with no values. | ||
| max_rows_to_read: '0', | ||
| } | ||
| : { | ||
| // shortcut: bounds the shared read, but keys that sort after | ||
| // a high-cardinality key can come back with no values; drop | ||
| // it if that matters more than the read's cost. | ||
| max_rows_to_read: String( | ||
| this.getClickHouseSettings().max_rows_to_read ?? | ||
| DEFAULT_METADATA_MAX_ROWS_TO_READ, | ||
| ), | ||
| read_overflow_mode: 'break', | ||
| ...this.getClickHouseSettings(), | ||
| }, | ||
| abort_signal: signal, | ||
| }) | ||
| .then(res => | ||
| res.json<{ column: string; key: string; value: string[] }>(), | ||
| ), | ||
| ); | ||
| return result.data.map(row => ({ | ||
| key: `${row.column}['${row.key}']`, | ||
| value: row.value, | ||
| })); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Queued abort fails shared fetchMedium Severity The queued Additional Locations (1)Reviewed by Cursor Bugbot for commit 6fff982. Configure here. |
||
| } catch (error) { | ||
| // Text-index queries can fail transiently (part merged mid-read, | ||
| // unsupported server, etc.). Isolate the failure so sibling | ||
|
|
@@ -2037,21 +2080,22 @@ export class Metadata { | |
| } | ||
| const sql = concatChSql(' UNION ALL ', sqlBranches); | ||
|
|
||
| const values = await this.clickhouseClient | ||
| .query<'JSON'>({ | ||
| query: sql.sql, | ||
| query_params: sql.params, | ||
| connectionId, | ||
| clickhouse_settings: { | ||
| ...this.getClickHouseSettings(), | ||
| // See getMapTextIndexKeyValues. | ||
| max_rows_to_read: '0', | ||
| }, | ||
| abort_signal: signal, | ||
| }) | ||
| .then(res => res.json<KeyValues>()) | ||
| .then(d => d.data); | ||
| return values; | ||
| const result = await limitTextIndexRead(connectionId, signal, () => | ||
| this.clickhouseClient | ||
| .query<'JSON'>({ | ||
| query: sql.sql, | ||
| query_params: sql.params, | ||
| connectionId, | ||
| clickhouse_settings: { | ||
| ...this.getClickHouseSettings(), | ||
| // See getMapTextIndexKeyValues. | ||
| max_rows_to_read: '0', | ||
| }, | ||
| abort_signal: signal, | ||
| }) | ||
| .then(res => res.json<KeyValues>()), | ||
| ); | ||
| return result.data; | ||
| } catch (error) { | ||
| // See `getMapTextIndexKeyValues` — same isolation rationale. | ||
| console.warn( | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 P2 — An aborted read stays in the queue and only settles once some other read frees a slot
In
createLimiter, also accept the signal: when it fires, remove the waiter fromwaitingand reject it right away, instead of callingthrowIfAbortedonly after the task is dequeued. As written, an aborted waiter sits inwaitinguntil an unrelated read finishes. That read may belong to another caller with its own signal, and the limiter is process-wide in the API. Sodescribe_source(packages/api/src/mcp/tools/sources/describeSource.ts:358/407), which awaitsgetAllKeyValuesafter its budget aborts, keeps hanging for up to a full read (12–20s per the PR description). The handler outlives its deadline even though no query will be sent.Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond