Skip to content

MySQL dictionary source: accept socket only in the configuration file - #127853

Open
groeneai wants to merge 7 commits into
ClickHouse:masterfrom
groeneai:mysql-dict-ddl-reject-socket
Open

groeneai wants to merge 7 commits into
ClickHouse:masterfrom
groeneai:mysql-dict-ddl-reject-socket

Conversation

@groeneai

@groeneai groeneai commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #109736

Changelog category (leave one):

  • Backward Incompatible Change

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

A MySQL dictionary source created with a CREATE DICTIONARY query can no longer set socket, at the source or at one of its REPLICA entries (BAD_ARGUMENTS): for localhost the connection went to that Unix socket, while remote_url_allow_hosts checks only host and port. To keep using a Unix socket, define the dictionary in a server configuration file.

Description

Follow-up to @ serxa's review point on #109736 (#109736 (comment)).

For localhost the MySQL connector uses the configured Unix socket and ignores the port: the socket of the source or of a <replica> (a replica inherits the source's). The only destination check, RemoteHostFilter::checkHostAndPort, sees just host and port, so with localhost:3306 in remote_url_allow_hosts a dictionary creator could make the server connect to any local socket.

Change. checkNoSocket rejects socket in a DDL-created dictionary at the source (also the value a replica inherits and a socket next to NAME) and at each <replica>, like checkNoSSLPaths. Configuration-file dictionaries are unchanged. With share_connection, a DDL-created source no longer reuses a cached pool created from a configuration file (the DDL checks see only the query's keys): getPoolEntryName marks the key of a DDL-created source. #129139 already makes a connection without socket use TCP, also for localhost, and rejects connection-string and host-list hosts for every MySQL connection, so this PR no longer changes those. The dictionary cases of 05493_mysql_host_connector_syntax set SOCKET in a DDL dictionary, so they now check port 0 without it.

Validation. 05229_mysql_dictionary_socket covers socket at the source, inherited by a replica, at a replica and next to a named collection, plus HOST 'localhost' without socket failing with a TCP error. It fails on master (the source is accepted and a connection is attempted, ALL_CONNECTION_TRIES_FAILED) and passes 50/50 with randomized settings, as does 05493. gtest_pool_entry_name checks that the same configuration gets different keys for the two origins.


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

A MySQL dictionary source created with a DDL query accepted `socket`. The
connection pool reads it for the source and for every <replica> (a replica
without its own inherits the source's, Pool.cpp:78-80), and the MariaDB
connector uses a Unix socket whenever the host is empty or `localhost`,
ignoring the port. The only destination check for such a dictionary,
RemoteHostFilter::checkHostAndPort, sees `host` and `port` only, so with
`localhost:3306` in remote_url_allow_hosts a user allowed to create a
dictionary could make the server connect to any local socket path.

Reject `socket` for DDL-created dictionaries the same way checkNoSSLPaths
rejects the SSL paths, as requested in the review of ClickHouse#109736: a presence
check at the source prefix next to checkNoSSLPaths, before the
named-collection branch is chosen (so it also covers the value a replica
inherits and a `socket` given next to `NAME`), and at each <replica> prefix.
Configuration-file dictionaries keep `socket`.

The replica call is placed between checkNoSSLPaths and checkNoLocalInfile so
it does not touch the lines ClickHouse#109736 changes.

`HOST 'localhost'` without `socket` still goes to the connector's default
socket; that path is fixed by the build, not chosen by the query.

The embedded documentation drops the DDL socket example and says the key is
only accepted in a configuration file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding; 2 full passes: 1 finding on the first, 0 on the second after the fix).

# Sev Finding Verdict Evidence / action
1 ⚠️ socket given next to NAME of a named collection was accepted and silently ignored: the source-level check ran only in the non-named-collection branch (MySQLDictionarySource.cpp) AGREE, fixed @ 7f9fc23 The check now runs next to checkNoSSLPaths, before the branch is chosen; new test arm (master: NAMED_COLLECTION_DOESNT_EXIST, fix: BAD_ARGUMENTS).

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05452_mysql_dictionary_socket fails on master (the source dials /nonexistent/mysql.sock, code 279). A local server with remote_url_allow_hosts = localhost:3306 and a Unix listener records a connection for the source, inherited and replica socket on master.
b Root cause explained? mysqlxx::Pool takes socket from the source or the replica, and for localhost the connector dials it, ignoring the port; checkHostAndPort validates only host and port.
c Fix matches root cause? Yes. The key is rejected where the DDL configuration becomes a pool, as checkNoSSLPaths and checkNoLocalInfile do; the pool is shared with configuration-file dictionaries, which keep socket.
d Test intent preserved / new tests added? New SQL test: source, inherited by a replica, a later replica, and next to a named collection are rejected; a control without socket reaches a connection attempt.
e Both directions demonstrated? Yes. Master: test FAIL, listener 3/3/3 connections. Fix: test OK (50/50 randomized), listener 0/0/0.
f Fix is general across code paths? Single endpoint, replica-own and replica-inherited socket are covered. A socket next to a named collection is rejected too; the mysql() / MySQL engine pools are built with an empty socket.
g Fix generalizes across inputs (params/datatypes/wrappers)? Presence-based: any socket key at the source or at any replica is rejected, including an empty one. Type wrappers N/A.
h Backward compatible? (maintainer-approved exception only) No, by request of the PR assignee in #109736 (review comment r4354892587). Backward Incompatible Change; the workaround (configuration file) is in the entry.
i Invariants and contracts preserved? Every endpoint the pool dials for a DDL-created dictionary is checked by checkHostAndPort. The guard throws before the pool exists; configuration-file dictionaries never reach it.

Session id: cron:clickhouse-impl-slot-5:20261026-144100

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @evillique, could you review this and add the can be tested label so CI runs? For localhost the MySQL connector dials the socket and ignores the port, while remote_url_allow_hosts checks only host and port, so this rejects socket in a DDL-created MySQL dictionary (at the source and at each replica) the same way the SSL paths are rejected, as asked in the #109736 review.

@clickhouse-gh

clickhouse-gh Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f4bb39a]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, s3 storage, DBReplicated, sequential) FAIL
05339_filelog_shared_watch_thread FAIL cidb

AI Review

Summary

This PR rejects explicit socket settings in DDL-created MySQL dictionaries, including inherited and replica settings, and separates their shared connection pools from configuration-file pools. Reviewed f4bb39a91e180a7f6349c4bfb49e5c500c3cc80a, including all prior discussion and the latest cache-isolation fix. No actionable findings remain.

Missing context / blind spots
  • ⚠️ The current CI report held the full suite behind the review-thread gate, so MySQL-capable stateless and unit execution results are unavailable for this head. The author reports 50/50 local passes for both stateless regressions; I inspected the regression coverage but did not run a local build. The full CI rerun will close this execution gap.
Final Verdict

Status: ✅ Approve. No additional code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-backward-incompatible Pull request with backwards incompatible changes comp-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). labels Oct 26, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 7f9fc23

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build profile diff infra, not from this PR: the CI logs cluster never passed the readiness probe, so the job ended with LogClusterNotReady: the endpoint never became ready before comparing anything. 282 of 286 runs since 2026-10-26 22:00 UTC are red, on 236 PRs #126200 (mine, open)
PR praktika rollup of the row above #126200 (the owner of the row above)

The first run of this head stopped at Config Workflow / Pre Hooks because the can be tested label was not set yet; the run after the label was added is the one above. CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261027-203012

@tiandiwonder tiandiwonder self-assigned this Oct 28, 2026
For `localhost` the MariaDB connector uses a Unix socket and ignores the
port: its default one (`/tmp/mysql.sock`, or `MYSQL_UNIX_PORT`) when no
socket is passed. So a dictionary created with a DDL query and
`HOST 'localhost'` still did not go to the `host`:`port` that
`remote_url_allow_hosts` checks, with or without a named collection.

Add `force_tcp` to `mysqlxx::Connection::connect`, which sets
`MYSQL_OPT_PROTOCOL = MYSQL_PROTOCOL_TCP`, and pass it through `Pool`,
`PoolWithFailover`, `PoolFactory::get` and `createMySQLPoolWithFailover`.
The MySQL dictionary source sets it for DDL-created dictionaries only;
configuration-file dictionaries, the `mysql` table function and the
`MySQL` engines keep their current behaviour. `force_tcp` is part of the
key of a shared pool, so a DDL dictionary never shares a pool with a
configuration-file dictionary that uses the default socket.

Rename the test to 05229, next to 05229_mysql_dictionary_enable_local_infile,
as asked in review, and make its last arm check that `HOST 'localhost'`
without `socket` fails with a TCP error for `localhost`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title MySQL dictionary source: accept socket only in the configuration file MySQL dictionary source: accept socket only in the configuration file, connect over TCP for DDL dictionaries Oct 28, 2026
Comment thread src/Common/mysqlxx/Connection.cpp Resolved
… for DDL dictionaries

mariadb-connector-c gives two characters in `host` a meaning of its own
in `mysql_real_connect` (mariadb_lib.c):

- `;` or `=`: the host is parsed as a connection string, whose options
  are applied after the ones `mysqlxx::Connection` sets. So a DDL
  dictionary with `HOST 'host=localhost;protocol=2;socket=/path/x.sock'`
  overrode the forced TCP protocol and connected to a Unix socket of its
  choosing, with no `socket` key for `checkNoSocket` to reject. The same
  string can set `local-infile` and SSL file paths, which the other DDL
  guards reject as keys.
- `,`: the host is a failover list of `host:port`. With a
  `remote_url_allow_hosts` regexp such as `.*\.allowed\.example`,
  `HOST '127.0.0.1:20255,x.allowed.example'` matched the regexp as one
  string and then connected to `127.0.0.1:20255`, which is not allowed.

Reject hosts containing any of the three for DDL-created dictionaries
at every place the host is checked against `remote_url_allow_hosts`:
the source, each replica and the named-collection addresses. No valid
host name contains them. Dictionaries defined in configuration files
are unchanged.

The test also gains no-socket `localhost` controls at a replica and with
a named collection, which reach the default Unix socket without TCP
forcing on those paths.

Reported by the AI review of the PR (r4405080282) and the second-model
review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f4bb39a91 with master 365bb22b2 (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.64, programs/clickhouse-keeper ×0.65 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.5 s 14.2 s +10.7 s (+301%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.6 s +5.4 s (+256%)
Compile time of recompiled translation units

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - bbfc810

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_debug, parallel) / Logical error: 'ReadBuffer is canceled. Can't read from it.' (STID: 2508-2bbd) logical error, not from this PR (its diff is MySQL dictionary code only): the embedded web-terminal client calls ask() from ClientBase::initOutputFormat again after its PTY input was canceled, and the debug server aborts. Also hit master 97a30be8613a #128090 (mine, open)
Stateless tests (amd_debug, parallel) / Server liveness check failed collateral of the abort above #128090 (the owner of the first row)
Stateless tests (amd_debug, parallel) / 05215_h3_to_center_child_error_branches collateral of the abort above (liveness check failed, the server was gone) #128090 (the owner of the first row)
Mergeable Check, PR praktika rollups of the rows above #128090 (the owner of the rows above)

praktika dropped 72 later jobs because of the first row, so they did not run on this head. CH Inc sync is green.

Session id: cron:our-pr-ci-monitor:20261028-223002

@tiandiwonder

Copy link
Copy Markdown
Contributor

@groeneai please check the ci result.

groeneai and others added 2 commits November 12, 2026 21:27
…ct-socket

Resolves the conflict with ClickHouse#129139 in the embedded MySQL dictionary source docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ClickHouse#129139 made every MySQL connection without an explicit `socket` use TCP,
including `localhost`, and made every MySQL connection reject hosts that the
client library reads as a connection string or a list of hosts. That covers
the `force_tcp` flag and the dictionary-only host check of this PR, so both are
removed and the mysqlxx and MySQLHelpers files are back to master.

What remains is rejecting `socket` in a dictionary created with a DDL query,
at the source and at each replica. 05229_mysql_dictionary_socket keeps the
`socket` cases and one TCP control. The dictionary cases of
05493_mysql_host_connector_syntax set `SOCKET` in a DDL dictionary, so they now
check port 0 without `socket`; a dictionary with `socket` and port 0 is only
reachable from a server configuration file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title MySQL dictionary source: accept socket only in the configuration file, connect over TCP for DDL dictionaries MySQL dictionary source: accept socket only in the configuration file Nov 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

I checked it. On bbfc810 the only failure was Stateless tests (amd_debug, parallel): Logical error: 'ReadBuffer is canceled' (STID 2508-2bbd) in the embedded client, which is not from this PR (it also hit master; my fix is #128090). The 72 later jobs were skipped because of it, so they never ran on that head.

The PR also conflicted with master after #129139. That PR already makes MySQL connections without socket use TCP, also for localhost, and rejects connection-string and host-list hosts for every MySQL connection. So I merged master and removed those parts from this PR in de120d8. What is left is rejecting socket in DDL dictionaries. The port 0 dictionary cases of 05493_mysql_host_connector_syntax set SOCKET in a DDL dictionary, so they now run without it. A dictionary with socket and port 0 can now only come from a server configuration file.

Locally 05229_mysql_dictionary_socket fails on master and passes 50/50 with the change, and 05493 passes 50/50. CI is running on de120d8.

…entries of configuration-file ones

With `share_connection`, `PoolFactory::get` returns a cached pool keyed by
`getPoolEntryName`, which joins the configuration fields without framing.
The DDL checks (`socket`, `ssl_*` paths, `enable_local_infile`, the host
filter) only see the keys of the `CREATE DICTIONARY` query, so a DDL source
whose key matched a pool created from a configuration file got that pool,
including its configured socket. On bbfc810 the `force_tcp` suffix kept the
keys apart; it went away with ClickHouse#129139.

`getPoolEntryName` now takes `created_from_ddl` and appends a marker for
DDL-created sources, so their keys never equal a configuration-file key.
The dictionary source passes it through `PoolFactory::get`. Covered by a
new case in `gtest_pool_entry_name.cpp` (same configuration, different
origin, different key).

Also drop the `MySQL socket` alternative from the 05493 grep: the DDL
dictionary cases there no longer set `SOCKET`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - f4bb39a

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, s3 storage, DBReplicated, sequential) / 05339_filelog_shared_watch_thread flaky, not caused by this PR (the diff touches only the MySQL dictionary source and its pool key): the same test failed in this job on master 3 times and on 10 other PRs since 2026-11-11. Its oracle counts GlobalThread growth over a window that is about 18 s long in the DBReplicated job, which also catches unrelated remote-FS reader threads a fix task is created (investigating at full effort, fixing-PR link to follow here)
Mergeable Check, PR praktika rollups of the row above a fix task is created (investigating at full effort, fixing-PR link to follow here)

Session id: cron:our-pr-ci-monitor:20261113-083007

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for 05339_filelog_shared_watch_thread (ledger line above): #131241

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). pr-backward-incompatible Pull request with backwards incompatible changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants