Skip to content

Propagate the internal flag to secondary queries - #108506

Merged
mstetsyuk merged 11 commits into
masterfrom
log-internal-secondary-queries
Jul 7, 2026
Merged

mstetsyuk merged 11 commits into
masterfrom
log-internal-secondary-queries

Conversation

@mstetsyuk

@mstetsyuk mstetsyuk commented Jun 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):

Secondary queries executed as part of internal queries will now be logged as internal queries.

@clickhouse-gh

clickhouse-gh Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2176e7c]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, parallel, 1/2) FAIL
02122_join_group_by_timeout FAIL cidb
Stateless tests (amd_tsan, s3 storage, parallel, 1/2) FAIL
Server died FAIL cidb
ThreadSanitizer: data race (STID: 1464-2f74) FAIL cidb, issue

AI Review

Summary

This PR adds an is_internal bit to ClientInfo, updates the native protocol/specs, and splits logging from trusted "internal execution" semantics via log_as_internal. That fixes the original spoofing concern, but the current head still leaves two important secondary-query paths outside the new propagation model, so the PR does not yet satisfy its stated contract.

Findings

⚠️ Majors

  • [src/Interpreters/AsynchronousInsertQueue.cpp:996-1026, src/Interpreters/AsynchronousInsertQueue.cpp:1171-1213, src/Storages/StorageMaterializedView.cpp:620-628, src/Storages/Distributed/DistributedSink.cpp:967] [dismissed by author -- https://github.com/Propagate the internal flag to secondary queries #108506#discussion_r3476235541] The async-insert flush and refreshable-MV refresh paths still build direct internal contexts without ever setting ClientInfo.is_internal = 1. The new log_as_internal arguments only fix the local logQuery* rows for those paths; any secondary write that goes through DistributedSink still serializes context->getClientInfo().is_internal, which stays 0, so the queued/remote insert is logged as external. Suggested fix: mark those direct internal contexts as internal before they can reach DistributedSink/RemoteQueryExecutor, or centralize a helper shared with executeQueryImpl, and add a focused regression on an async-insert or refreshable-MV write to Distributed.
  • [src/QueryPipeline/DistributedPlanExecutor.cpp:696-737, src/QueryPipeline/DistributedPlanExecutor.cpp:773-778, src/QueryPipeline/DistributedPlanExecutor.cpp:1499-1503, src/Server/StatelessWorker/StatelessWorkerEndpoint.cpp:42-98, src/Server/StatelessWorker/StatelessTaskExecutor.cpp:58-62] [dismissed by author -- https://github.com/Propagate the internal flag to secondary queries #108506#discussion_r3513902105] Distributed-plan task fragments still hard-code log_as_internal = false. On the local path, executeTask clones the initiator context, so an internal parent already carries ClientInfo.is_internal = 1, but doExecuteTask drops it in logQueryStart/logQueryFinish/logQueryException. On the remote-worker path, DistributedQueryTaskDescription still does not serialize the flag at all, and StatelessTaskExecutor reconstructs ClientInfo with the default is_internal = 0. Internal queries that run through the distributed-plan executor will therefore keep producing external-looking fragment rows on both local and remote workers. Suggested fix: derive log_as_internal from context->getClientInfo().is_internal in doExecuteTask, serialize the flag in DistributedQueryTaskDescription, restore it in StatelessTaskExecutor, and add a regression that exercises an internal distributed-plan query across worker fragments.
Final Verdict

Changes requested. The wire-format and local logging split are in place, but internal secondary-query logging is still incomplete for direct interpreter paths and distributed-plan fragments.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 25, 2026
Comment thread src/Interpreters/executeQuery.cpp Resolved
Comment thread src/Core/ProtocolDefines.h Resolved
Comment thread src/Core/ProtocolDefines.h Resolved
@CheSema CheSema self-assigned this Jun 29, 2026
@CheSema

CheSema commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

How does a server ensure that a client sends correct is_internal=1?

@CheSema

CheSema commented Jun 30, 2026

Copy link
Copy Markdown
Member

In the code query_kind = SECONDARY exists.
I think we need to describe the difference/relation between query_kind = SECONDARY and is_internal = 1.

@mstetsyuk

mstetsyuk commented Jul 2, 2026 •

Copy link
Copy Markdown
Member Author

How does a server ensure that a client sends correct is_internal=1?

I have not found a clean way to verify if is_internal=1 is not spoofed. It allows bypassing the max_concurrent_queries server setting, which is not ideal security-wise.

So, I suggest that for remote internal queries, we just log them as internal (required for autoscaling: https://github.com/ClickHouse/support-escalation/issues/8036), but continue to treat them as normal (e.g. no bypassing max_concurrent_queries, show on process list).

@mstetsyuk

Copy link
Copy Markdown
Member Author

In the code query_kind = SECONDARY exists.
I think we need to describe the difference/relation between query_kind = SECONDARY and is_internal = 1.

Updated the doc.

@mintlify

mintlify Bot commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🔴 Failed – Jul 6, 2026, 3:32 PM

mstetsyuk and others added 3 commits July 6, 2026 17:48
The makeFullClientInfoWire helper hand-crafts a complete ClientInfo wire
payload, but was not updated for the new trailing is_internal field read
at protocol revision >= 54486, so ClientInfo::read hit an exhausted
buffer and threw "Cannot read all data" in the two tests using it.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108506&sha=4b048ccec9031d7fd1fb5a0cb1f74de703509a9a&name_0=PR&name_1=Unit+tests+%28amd_llvm_coverage%29
PR: #108506

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

Updating the branch to fix the 04001_join_reorder_through_expression test, which was broken today in master.

@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.60% 85.70% +0.10%
Functions 92.70% 92.70% +0.00%
Branches 77.80% 77.80% +0.00%

Changed lines: Changed C/C++ lines covered: 148/149 (99.33%) · Uncovered code

Full report · Diff report

@mstetsyuk
mstetsyuk added this pull request to the merge queue Jul 7, 2026
Merged via the queue into master with commit 7042869 Jul 7, 2026
172 of 175 checks passed
@mstetsyuk
mstetsyuk deleted the log-internal-secondary-queries branch July 7, 2026 19:39
alexey-milovidov added a commit to hinata34/ClickHouse that referenced this pull request Jul 7, 2026
Resolved conflicts in the query-logging path where master (ClickHouse#108506,
"Propagate internal flag to secondary queries") added a `log_as_internal`
parameter to `logQueryStart` / `logExceptionBeforeStart` while this branch
added `applied_rewrite_rules` / `ast_for_formatted_query`. Both additions are
orthogonal, so the resolution keeps both: the signatures now take
`internal, log_as_internal, ..., applied_rewrite_rules[, ast_for_formatted_query]`
and every call site (executeQuery.cpp, AsynchronousInsertQueue.cpp,
RefreshTask.cpp, StorageMaterializedView.cpp) passes both, preserving master's
`log_as_internal` values (`is_internal` for the async-insert / refresh helpers).
@alexey-milovidov alexey-milovidov mentioned this pull request Jul 7, 2026
1 task done
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 7, 2026
alexey-milovidov added a commit that referenced this pull request Jul 7, 2026
…cation

Resolve a conflict in `src/Storages/MaterializedView/RefreshTask.cpp`:
master's `log-internal-secondary-queries` work (#108506) added a
`log_as_internal` parameter to `logQueryFinish`, while this branch prepended
the `IF CHANGED` post-read source-hash recompute block before that call. Kept
both: the source-hash recompute stays, and the `logQueryFinish` call uses
master's updated signature with `/*log_as_internal=*/internal`.

All feature translation units touched by master's churn (`executeQuery.cpp`,
`Planner.cpp`, `ReadFromObjectStorageStep.cpp`, `StorageObjectStorage.cpp`,
`StorageDistributed.cpp`, `MergeTreeData.cpp`, `StorageMerge.cpp`,
`StorageSystemTables.cpp`, `Context.cpp`, `RefreshTask.cpp`) verified to
compile (`-fsyntax-only`) against the merged headers.
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 31, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The makeFullClientInfoWire helper hand-crafts a complete ClientInfo wire
payload, but was not updated for the new trailing is_internal field read
at protocol revision >= 54486, so ClientInfo::read hit an exhausted
buffer and threw "Cannot read all data" in the two tests using it.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108506&sha=4b048ccec9031d7fd1fb5a0cb1f74de703509a9a&name_0=PR&name_1=Unit+tests+%28amd_llvm_coverage%29
PR: ClickHouse#108506

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ondary-queries

Propagate the internal flag to secondary queries
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 16, 2026

This branch was successfully deployed

1 active deployment
staging - docs — 2176e7c0 Deployed Jul 6, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants