Skip to content

Fix broken async INSERT under a KEYED BY client_key quota - #122296

Merged
Felixoid merged 1 commit into
masterfrom
dedup-hash-quota-access
Oct 1, 2026
Merged

Felixoid merged 1 commit into
masterfrom
dedup-hash-quota-access

Conversation

@Felixoid

@Felixoid Felixoid commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed asynchronous INSERT under a quota KEYED BY client_key: the flush ran without the client's quota key, so it failed with QUOTA_REQUIRES_CLIENT_KEY (or charged the wrong bucket). The quota key is now carried through the async batch and restored on flush, and background quota recomputes no longer log a spurious QUOTA_REQUIRES_CLIENT_KEY error.

Note

A side quest generated during development in #111663


Workflow [PR]
Sync PR [sync-upstream/pr/122296]

Version info

  • Merged into: 26.10.1.1150-master (included in 26.10 and later)

@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [331fe93]

Summary: ✅


AI Review

Summary

This PR carries quota_key through the async-insert batch key and restores it on the flush context, with both unit and stateless coverage for the KEYED BY client_key case. That fixes the narrow client_key path, but the broader quota-identity invariant is still only partially restored: address-based quota keys are still dropped before the background flush runs.

Findings

❌ Blockers

  • [dismissed by author -- https://github.com/Fix broken async INSERT under a KEYED BY client_key quota #122296#discussion_r4110936821] [src/Interpreters/AsynchronousInsertQueue.cpp:672-684,1202-1206] The batch key and replayed ClientInfo still carry only quota_key. ContextAccessParams derives quota selection from current_address, getLastForwardedForHost(), and quota_key (src/Access/ContextAccessParams.cpp:43-45), while QuotaCache still keys quotas on ip_address, forwarded_ip_address, and client_key_or_ip_address through those fields (src/Access/QuotaCache.cpp:99-145). Because the flush context is copied from the global/background context, async inserts still run with the background wildcard address and empty forwarded-for chain, so quotas keyed by address are still enforced and billed against the wrong bucket. Carry current_address and forwarded_for through InsertQuery, fold them into the batching key, and restore them on insert_context alongside setQuotaClientKey.
Final Verdict

Request changes. The client_key fix itself looks correct, but async quota identity is still incomplete because address-based quota keys are not preserved across the deferred flush.

LLVM Coverage Report

Measured on commit 331fe93.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 30/37 (81.08%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 25, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-async-insert Asynchronous INSERT buffering/queueing and flush semantics. label Sep 25, 2026
@Felixoid
Felixoid force-pushed the dedup-hash-quota-access branch from 69436a3 to 5f9ddb6 Compare September 26, 2026 09:09
client_info.current_user,
client_info.initial_user,
client_info.authenticated_user,
client_info.quota_key,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ContextAccessParams still derives quota selection from client_info.current_address and client_info.getLastForwardedForHost() (src/Access/ContextAccessParams.cpp:43-45), but this queue key only starts carrying quota_key. Two async inserts from the same user/query/settings but different client IPs still build the same InsertQuery, coalesce into one batch, and then flush under whatever address the background context has, so KEYED BY ip_address / KEYED BY forwarded_ip_address quotas are still charged and enforced against the wrong bucket. Please carry the address / forwarded-for fields through InsertQuery, fold them into the hash/equality key, and replay them on insert_context alongside setQuotaClientKey.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current_address can differ for the NAT cases. I am pretty sure we don't care about ip_address and forwarded_ip_address.

@Felixoid
Felixoid force-pushed the dedup-hash-quota-access branch from 5f9ddb6 to 331fe93 Compare September 30, 2026 14:26
@Felixoid

Copy link
Copy Markdown
Member Author

Dropped the 5f9ddb6 since it's already fixed in the master

@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 331fe93cc with master f05f2a72a (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of cdacddff3; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

16 translation units recompiled, 149 s compile time in total, 16 of them have a recent master baseline.

Job report

@Felixoid
Felixoid added this pull request to the merge queue Oct 1, 2026
Merged via the queue into master with commit be50c1b Oct 1, 2026
149 checks passed
@Felixoid
Felixoid deleted the dedup-hash-quota-access branch October 1, 2026 09:05
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-async-insert Asynchronous INSERT buffering/queueing and flush semantics. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants