Skip to content

Cherry pick #122484 to 26.8: Issue AI function requests concurrently from a server-wide thread pool - #128317

Merged
robot-ch-test-poll merged 33 commits into
backport/26.8/122484from
cherrypick/26.8/122484
Oct 30, 2026
Merged

robot-ch-test-poll merged 33 commits into
backport/26.8/122484from
cherrypick/26.8/122484

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #122484

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

george-larionov and others added 30 commits September 24, 2026 19:58
Requests run on `getAIRequestThreadPool` (a `StaticThreadPool`), configured by
`max_ai_request_thread_pool_size`, `max_ai_request_thread_pool_free_size` and
`ai_request_thread_pool_queue_size`. `ai_function_max_concurrent_requests` bounds
one function call's share. Retries, API-call reservation and token accounting
move into `submitAIRequest` / `submitAIEmbeddingRequest`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`AIQuotaTracker::checkQuotas` and `recordApiCall` throw on `KILL QUERY` or
`max_execution_time`, so a killed query no longer finishes its current block,
and queued requests and retries are not dispatched. Also doc and comment tweaks.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`submitAIRequest` is overloaded for both request types over one `submit`
template; `runAttempt` overloads pick `call` or `embed`. `AIEmbeddingResponse`
gains an always-zero `output_tokens`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A `/v1/chat/jitter` mock endpoint answers after a random delay, so concurrent
responses complete out of order.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`ai_function_max_concurrent_requests` becomes
`ai_function_max_concurrent_requests_per_stream`, defaulting to 16.
`max_ai_request_thread_pool_size` defaults to 1000. Token-quota tests pin
the setting to 1.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Measured against the inference gateway, 8 per stream keeps a typical query
within provider rate limits, and 3 retries absorb the remaining 429s. Tests
that call unreachable endpoints pin one retry.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Settings history moved into the declarations on master: the new
`ai_function_max_concurrent_requests_per_stream` and the
`ai_function_max_retries` default change now carry their 26.10 records there.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`ai_function_max_concurrent_requests_per_stream` becomes `..._per_thread`, and the
pool settings become `ai_function_max_thread_pool_size`,
`ai_function_max_thread_pool_free_size` and `ai_function_thread_pool_queue_size`.
Regenerated reference docs for these settings.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
New `ProfileEvents`: `AIAPICallsRetried`, `AIAPICallsFailed` (including errors
replaced by a default value) and `AIAPICallsThrottled` (HTTP 429). Server-wide
totals and history come from `system.events` and `system.metric_log`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…me limits

`ai_function_max_concurrent_requests_per_thread` records 1 -> 8 in 26.10, so
`compatibility` below 26.10 keeps the previous serial behavior. Integration
tests cover `max_execution_time` with both overflow modes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Conflicts were only in the generated settings route maps and explorers: master's
versions plus this branch's `ai_function_*` settings.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Conflicts were only in the generated session settings route map and explorer:
master's versions plus `ai_function_max_concurrent_requests_per_thread`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The generated settings route maps and explorers are taken from master as-is: the
nightly docs regeneration adds this branch's settings after merge, so they no
longer conflict with every regeneration.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`ai_function_max_retries` is at most 5 and `ai_function_retry_initial_delay_ms`
at most 10000, checked in `makeRequestPolicy`, so the backoff needs no cap.
`AIEmbeddingResponse::output_tokens` is `static constexpr`, keeping the struct
assignable.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Conflicts with the query id change: the executor path now sets `query_id` and
`initial_query_id` and submits `AIChatRequest`; `sanitizeForLog` is `sanitize`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Conflicts were only in the AI function integration tests: the cancellation
tests from master run alongside this branch's concurrency tests, pinned to one
request per thread.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Brings in `ai_function_allow_sql_named_collections`; merged without conflicts.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A killed or timed-out query no longer waits for other queries' requests
queued ahead of its own in the shared AI request pool. Queue admission,
waits for results, and the cleanup on error check for cancellation every
100 ms. Requests left in the queue by a cancelled query throw instead of
being sent.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
george-larionov and others added 2 commits October 23, 2026 02:28
A cancelled query does not wait for its AI requests, so they can outlive it.
Their memory is tracked under the user trackers in `process_list`, so drain
the pool in `~ContextSharedPart`. The retry backoff stops once the query is
cancelled, which bounds the drain. Tolerate `NOTHING_TO_KILL` in the cleanup
of the cancellation test.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…atic-pool

Issue AI function requests concurrently from a server-wide thread pool
@robot-clickhouse robot-clickhouse added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Oct 30, 2026
Adapt the request executor to 26.8's value-returning `IAIProvider::call`/`embed`, without porting #114905 or its tests.
Keep 26.8's generated docs pages and `aiEmbed` row counters; add the settings changes to the `26.8` block of `SettingsChangesHistory`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll
robot-ch-test-poll merged commit cefdb99 into backport/26.8/122484 Oct 30, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.8/122484 branch October 30, 2026 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants