Skip to content

Compare the HTTP connection store limit against stored connections, not the group total - #119078

Merged
alexey-milovidov merged 1 commit into
masterfrom
http-pool-store-limit-counts-stored
Sep 10, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
http-pool-store-limit-counts-stored

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Closes: #119072

disk_connections_store_limit (and the storage_* / http_* counterparts) was compared against the total number of connections in the group, active plus stored, rather than against the number of connections actually stored. As soon as the concurrency of a group exceeded the limit, the pool stopped caching entirely: every HTTP request to object storage opened a new TCP connection and closed it after use, and on a busy writer the churn exhausted the ephemeral port range of the host with tens of thousands of sockets in CLOSE-WAIT. The failure surfaced as Cannot assign requested address from Poco rather than as a ClickHouse limit. Lowering the setting, the intuitive reaction to "too many connections", made it strictly worse.

The connection group now keeps a separate count of stored connections, and the store limit is checked against that count. The slot is reserved atomically before the connection is stored, so endpoint pools of the same group cannot overshoot the limit together; it is released when a stored connection is reused, expires, or is dropped with its pool. The *ConnectionsStored current metrics are maintained through the same counter. The setting descriptions now say what the limit bounds.

The unit test holds five connections with a store limit of two and checks that releasing two of them while three are still in use stores both, that the store then stays full, and that reusing a stored connection frees its slot.

Not changed here, from the other two suggestions in the issue: the default *_connections_hard_limit stays at 200000, because the ephemeral port range is consumed per destination address and a group that talks to many endpoints can legitimately hold more connections than one address allows. Stale connections that are not at the top of the expiry heap are still reaped only when they reach it, at the next borrow or sweep.

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):

Fix the HTTP connection pool comparing disk_connections_store_limit, storage_connections_store_limit, and http_connections_store_limit against the total number of connections in the group instead of the number of stored connections. A server whose concurrency exceeded the limit cached no connections at all, opened a new TCP connection for every request to object storage, and could exhaust the ephemeral port range of the host. The limit now bounds only the idle connections kept for reuse.


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

Version info

  • Merged into: 26.9.1.1172 (included in 26.9 and later)

…ot the group total

`isStoreLimitReached` compared `store_limit` against `total_connections_in_group`, which
counts the connections in use as well as the stored ones. Once the concurrency of a group
exceeded the limit, `atConnectionDestroy` reset every connection after use and the pool
cached nothing at all: each request to object storage opened and closed its own TCP
connection, and the churn exhausted the ephemeral port range of the host with sockets in
`CLOSE-WAIT`. Lowering the setting made it strictly worse.

The `ConnectionGroup` now counts the stored connections separately. A slot is reserved
under the group mutex before a connection is stored, so endpoint pools of the same group
cannot overshoot the limit together, and it is released when a stored connection is
reused, expires, or is dropped with its pool. The `*ConnectionsStored` current metrics
are maintained through the same counter. The descriptions of the `*_connections_store_limit`
settings now say what the limit bounds.

Closes: #119072

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [29fb966]

Summary: ✅


AI Review

Summary

This PR fixes the HTTP connection pool so *_connections_store_limit is checked against idle cached connections instead of all connections in the group, updates the setting descriptions to match that behavior, and adds a regression test for the reported failure mode. I did not find an unresolved correctness, concurrency, or metadata issue in the current diff, and the Praktika report is all green.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit 29fb966.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 81/84 (96.43%) · 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 9, 2026

@alexey-milovidov alexey-milovidov left a comment

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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Sep 9, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 29fb96677 with master b75d29e7d (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.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 10, 2026
Merged via the queue into master with commit 7d56971 Sep 10, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the http-pool-store-limit-counts-stored branch September 10, 2026 20:16
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

It's like a miracle!

The PR fixed it decisively. Same workload, same host, store_limit at 2,000 — a value that under the old code disabled caching outright:
  
  ┌───────────────────────────────┬────────────┬─────────────┐
  │                               │ 26.9.1.805 │ 26.9.1.1209 │
  ├───────────────────────────────┼────────────┼─────────────┤
  │ DiskConnectionsTotal          │ 40,187     │ 5,682       │
  ├───────────────────────────────┼────────────┼─────────────┤
  │ Established sockets           │ 21,641     │ 5,692       │
  ├───────────────────────────────┼────────────┼─────────────┤
  │ All sockets (of 55,295 ports) │ 43,506     │ 6,878       │
  ├───────────────────────────────┼────────────┼─────────────┤
  │ Concurrent merges             │ 7          │ 9           │
  ├───────────────────────────────┼────────────┼─────────────┤
  │ DiskConnectionsReset          │ 20,545,836 │ 1,670       │
  ├───────────────────────────────┼────────────┼─────────────┤
  │ DiskConnectionsReused         │ 49,216     │ 18,166      │
  ├───────────────────────────────┼────────────┼─────────────┤
  │ Reuse rate                    │ 0.24%      │ 92%         │
  └───────────────────────────────┴────────────┴─────────────┘
  
  Seven times fewer connections while running more merges, and port usage down from saturated to 12%.

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

Labels

comp-networking The connection layer under the protocols, on the server and on the client side: socket read / wri... 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.

The disk connection pool stops caching under load, and exhausts the ephemeral port range

2 participants