Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(ch): retry TOO_MANY_SIMULTANEOUS_QUERIES instead of failing the request (#438)

Ritik Jain581ed6783502
diff --git a/packages/db/src/clickhouse/round-robin.test.ts b/packages/db/src/clickhouse/round-robin.test.ts
index d9e2fad16..07bdb325d 100644
--- a/packages/db/src/clickhouse/round-robin.test.ts
+++ b/packages/db/src/clickhouse/round-robin.test.ts
@@ -262,6 +262,38 @@ describe('isRetriableConnectionError', () => {
err: new Error('Code: 159, DB::Exception: Timeout exceeded'),
expected: 'ch-server',
},
+ // ── ch-server allow-list: overload rejections retry ──────────────
+ // TOO_MANY_SIMULTANEOUS_QUERIES means the node hit its concurrency
+ // cap at that instant, not that the query is wrong — another node
+ // (or a backoff) absorbs it. Transient, so the node is NOT
+ // sin-binned: it's healthy, just briefly saturated.
+ {
+ label: 'CH TOO_MANY_SIMULTANEOUS_QUERIES (raw HTTP message)',
+ err: new Error(
+ 'Code: 202. DB::Exception: Too many simultaneous queries. Maximum: 100. (TOO_MANY_SIMULTANEOUS_QUERIES)'
+ ),
+ expected: 'transient',
+ },
+ {
+ label: 'CH TOO_MANY_SIMULTANEOUS_QUERIES (@clickhouse/client parsed shape)',
+ err: Object.assign(
+ new Error('Too many simultaneous queries. Maximum: 100.'),
+ { code: '202', type: 'TOO_MANY_SIMULTANEOUS_QUERIES' }
+ ),
+ expected: 'transient',
+ },
+ {
+ label: 'CH TOO_MANY_SIMULTANEOUS_QUERIES (numeric code from a re-wrapping layer)',
+ err: Object.assign(new Error('query rejected'), { code: 202 }),
+ expected: 'transient',
+ },
+ {
+ label: 'CH TOO_MANY_PARTS stays non-retriable (code 252 ≠ 202)',
+ err: new Error(
+ 'Code: 252. DB::Exception: Too many parts (300). (TOO_MANY_PARTS)'
+ ),
+ expected: 'ch-server',
+ },
// ── cause-chain preserves classification ─────────────────────────
{
label: 'wrapped error: outer is opaque, cause is ECONNRESET',
diff --git a/packages/db/src/clickhouse/round-robin.ts b/packages/db/src/clickhouse/round-robin.ts
index 20cc05a38..69d9af323 100644
--- a/packages/db/src/clickhouse/round-robin.ts
+++ b/packages/db/src/clickhouse/round-robin.ts
@@ -93,6 +93,34 @@ const TRANSIENT_BARE_CODE_REGEX = /\b(ECONNRESET|EPIPE)\b/;
* all of them. */
const CH_SERVER_ERROR_PREFIX = /^Code:\s*\d+/;
+/** The one CH *server* error worth retrying: TOO_MANY_SIMULTANEOUS_QUERIES
+ * (code 202). The query is fine — the node just hit its concurrent-query
+ * cap at that instant, so another node (or the same one after backoff) can
+ * absorb it, which is exactly what this retry loop provides. Deliberately
+ * a single-entry allow-list: every other CH server error (SQL, auth,
+ * MEMORY_LIMIT_EXCEEDED, ...) still propagates immediately, because
+ * retrying those either can't help or piles more load on a struggling
+ * cluster. Matched by @clickhouse/client's parsed fields (code/type) and
+ * by message text for errors that arrive re-wrapped. */
+const RETRIABLE_CH_ERROR_CODE = 202;
+const RETRIABLE_CH_ERROR_NAME = 'TOO_MANY_SIMULTANEOUS_QUERIES';
+const RETRIABLE_CH_MESSAGE = 'Too many simultaneous queries';
+
+function isRetriableChServerError(
+ e: { code?: unknown; type?: unknown },
+ msg: string
+): boolean {
+ return (
+ // @clickhouse/client parses the code as a string ('202'); accept the
+ // numeric form too for errors re-wrapped by other layers.
+ e.code === String(RETRIABLE_CH_ERROR_CODE) ||
+ e.code === RETRIABLE_CH_ERROR_CODE ||
+ e.type === RETRIABLE_CH_ERROR_NAME ||
+ msg.includes(RETRIABLE_CH_ERROR_NAME) ||
+ msg.includes(RETRIABLE_CH_MESSAGE)
+ );
+}
+
export function classifyError(err: unknown): ErrorClass {
if (!err || typeof err !== 'object') {
return 'other';
@@ -105,6 +133,14 @@ export function classifyError(err: unknown): ErrorClass {
};
const msg = String(e.message ?? '');
+ // ── Allow-list: overload rejections retry as transient ────────────────
+ // Checked before the server-error deny below. `transient` (not
+ // `node-down`): the node is healthy, just briefly saturated — sin-binning
+ // it would shrink the pool exactly when capacity is scarcest.
+ if (isRetriableChServerError(e, msg)) {
+ return 'transient';
+ }
+
// ── Deny: ClickHouse server errors ────────────────────────────────────
if (CH_SERVER_ERROR_PREFIX.test(msg)) {
return 'ch-server';
@@ -308,7 +344,10 @@ export async function withRoundRobinRetry<T>(
},
errClass === 'node-down'
? 'CH node unreachable; sin-binning and retrying on next'
- : 'CH transient socket error; retrying on next'
+ : // `transient` covers socket-level failures AND retriable server
+ // overload rejections (TOO_MANY_SIMULTANEOUS_QUERIES) — the
+ // err/code fields above identify which.
+ 'CH transient error; retrying on next'
);
await new Promise((r) => setTimeout(r, delay));
}