Skip to content

Share S3 client cache per bucket - #96802

Merged
alexey-milovidov merged 26 commits into
ClickHouse:masterfrom
zvonand:fix/92482
Jun 13, 2026
Merged

alexey-milovidov merged 26 commits into
ClickHouse:masterfrom
zvonand:fix/92482

Conversation

@zvonand

@zvonand zvonand commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #92482

Changelog category (leave one):

  • Performance Improvement

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

S3 clients with the same endpoint and bucket share a cache, avoiding duplicate region discovery. Closes #92482

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.743

@scanhex12 scanhex12 added the can be tested Allows running workflows for external contributors label Feb 13, 2026
@scanhex12 scanhex12 self-assigned this Feb 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e23d72c]

Summary: ✅


AI Review

Summary

This PR shares the S3::ClientCache for clients created with the same S3 endpoint and bucket, and wires that shared cache through S3 disk, backup, and Keeper snapshot client creation. The current code addresses the prior race, stale-key pruning, key-boundary hashing, header include, and refcount-test concerns from earlier review threads. I did not find a remaining actionable correctness issue in the changed code.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Feb 13, 2026
@zvonand

zvonand commented Feb 16, 2026

Copy link
Copy Markdown
Contributor Author

The only fail is CH Inc sync (nothing I can do about it)

Comment thread src/IO/S3/Client.cpp Resolved

@alexey-milovidov alexey-milovidov left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code looks good (except the string concatenation hackery, which must be fixed). But is it true that the same client can be used across the bucket? I think there could be corner cases when different IAM roles are authorised to different parts of the bucket.

@alexey-milovidov alexey-milovidov self-assigned this Mar 22, 2026
@zvonand

This comment was marked as outdated.

Comment thread src/IO/S3/Client.cpp Resolved
Comment thread src/IO/S3/Client.cpp Resolved
@zvonand

zvonand commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

After refreshing this in mind: the clients themselves are not shared. The only thing that is shared is ClientCache; only region and URI. Credentials are not involved here.

@alexey-milovidov

Copy link
Copy Markdown
Member

@zvonand, sounds good! Please address review, fix failures, and let's merge.

Comment thread src/IO/S3/Client.cpp Resolved
@zvonand

This comment was marked as outdated.

@zvonand

zvonand commented Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Reviews are addressed.
Stateless fails are not related and have issues opened. Unit tests are failing in latest commits in master in a similar way.

@alexey-milovidov

@alexey-milovidov

Copy link
Copy Markdown
Member

Please fix failed tests.

Comment thread src/IO/S3/Client.h Resolved
il9ue pushed a commit to il9ue/ClickHouse that referenced this pull request May 20, 2026
…olution in next commit)

---
Original cherry-pick message follows:

Merge d0085a8 into 12f07ad

# Conflicts:
#	src/Disks/DiskObjectStorage/ObjectStorages/S3/diskSettings.cpp
il9ue pushed a commit to il9ue/ClickHouse that referenced this pull request May 20, 2026
subkanthi pushed a commit to Altinity/ClickHouse that referenced this pull request May 26, 2026
…olution in next commit)

---
Original cherry-pick message follows:

Merge d0085a8 into 12f07ad

# Conflicts:
#	src/Disks/DiskObjectStorage/ObjectStorages/S3/diskSettings.cpp
subkanthi pushed a commit to Altinity/ClickHouse that referenced this pull request May 26, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, link the fix for "Logical error: Column identifier A is already registered (STID: 4697-4326)" here.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov The fix is in flight: #106025 ("Fix LOGICAL_ERROR 'Column identifier is already registered' in ALTER UPDATE with EXISTS"), MERGEABLE, CI running on commit d52ff95.

STID 4697-4326 is a chronic trunk bug — multiple prior fixes (#100770, #101048, #101051, #101104) covered some code paths but missed the mutations re-analysis path that PR #106025 now closes.

@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Dear @scanhex12, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@alexey-milovidov

Copy link
Copy Markdown
Member

Merged the latest master into the branch (it was ~1378 commits behind) to refresh CI. No conflicts.

I reviewed the two failures from the previous run (commit d6445412, which was stale — from May 31):

  • AST fuzzer (amd_debug): Logical error: Column identifier A is already registered (STID 4697-4326) — unrelated. The stack is the ALTER UPDATE / MutationsInterpreter → buildSubqueryPlansForSetsAndAdd re-analysis path, a chronic trunk bug. Fix is in flight in Fix LOGICAL_ERROR "Column identifier is already registered" in ALTER UPDATE with EXISTS #106025, as @groeneai linked above.
  • Stateless tests (arm_asan_ubsan): Server died — unrelated. The server logs contain no <Fatal>, no sanitizer report, and no crash; the server received SIGTERM (signal 15) and shut down gracefully. The run was dominated by CI telemetry-export memory pressure (User memory limit exceeded: would use 29.90 GiB), i.e. an infra flake.

Built locally and the new S3 client-cache unit tests pass:

[  PASSED  ] 3 tests.
  IOTestAwsS3Client.ClientCacheRegistryGetOrCreateCacheForKey
  IOTestAwsS3Client.ClientSharesCacheWithClone
  IOTestAwsS3Client.ClientCacheRegistryRefcount

Let's see the fresh CI run.

@zvonand

zvonand commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Now it's only the same Fuzzer fail + some internal conflicts (CH Inc sync)

@alexey-milovidov

Copy link
Copy Markdown
Member

Re: my earlier question about whether the same cache can be shared across a bucket when different IAM roles are authorized to different parts of it — I checked, and it is safe.

ClientCache only stores two things (src/IO/S3/Client.h):

  • region_for_bucket_cache — bucket → AWS region
  • uri_for_bucket_cache — bucket → endpoint URI redirect

Both are physical-location facts about the bucket: which region it lives in and which endpoint hostname serves it. They are intrinsic to the (endpoint, bucket) pair and do not depend on credentials. A bucket lives in exactly one region and is served from one URI regardless of which IAM role accesses it (or which prefix that role is authorized for). Authorization is enforced server-side per request, using each Client's own credentials_provider — which stays per-Client and is not part of the shared cache.

So the corner case I worried about (different roles authorized to different parts of a bucket) does not affect what is cached: every role resolves the same region and URI for the bucket. Sharing the cache per (endpoint, bucket) is sound.

The string-concatenation hashing concern is also addressed — it now uses SipHash with explicit length framing.

Note on CI: the only red is the targeted AST fuzzer (Logical error: Column identifier A is already registered), reproduced by ALTER TABLE ... UPDATE ... WHERE ... EXISTS((SELECT ...)). This is unrelated to S3 client caching and is tracked by #99931, with a fix in #106414.

Merged master to refresh CI. Verified the affected S3 translation units compile and all 15 IOTestAwsS3Client gtests pass (including the new ClientCacheRegistryGetOrCreateCacheForKey, ClientSharesCacheWithClone, and ClientCacheRegistryRefcount).

@clickhouse-gh

clickhouse-gh Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.60% +0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 153/162 (94.44%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 4 line(s) · Uncovered code

Full report · Diff report

@zvonand

zvonand commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

this time, all the tests passed (except for CH Inc sync)

@alexey-milovidov

Copy link
Copy Markdown
Member

Fixed the CH Inc sync check: the sync PR had a merge conflict in src/IO/S3/Client.cpp (this PR adds a pruneExpiredCachesLocked call at the end of clearCacheForAll, while the private master only removed a blank line there and added unrelated code below). Resolved by keeping this PR's block, verified the file compiles, and pushed the merge — the sync PR is mergeable again, waiting for the private CI to confirm.

@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR changes S3 client-cache plumbing — it shares cached client state keyed by endpoint and bucket and adds refcounting — which affects S3 connection and region-detection setup, not query execution. The two flagged improvements (clickbench Q4 at -18.9% and Q15 at -16.0%) are pure aggregation SELECTs whose hot path this change does not touch, so it cannot plausibly account for double-digit speedups while every other query stays flat. With only 5 source runs and a baseline that's already drifting, these deltas are run-to-run variance rather than a real effect, so both are downgraded to not-sure.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
⚠️ 4 not_sure 267 216 -18.9% <0.0001 aggregation: PR only shares S3 client caches (connection/region setup); cannot speed up Q4 aggregation. Off-path, k=5 variance.
⚠️ 15 not_sure 250 210 -16.0% <0.0001 aggregation: S3 client-cache plumbing doesn't touch Q15's aggregation hot path; -16% is off-path run-to-run variance.

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 431edd51-eb21-4baa-a65b-ff0478305d3a
  • MIRAI run: 734e0746-9e27-49cc-bb3e-348d1bed7b02
  • PR check IDs:
    • clickbench_207605_1781125298
    • clickbench_207711_1781125366
    • clickbench_207811_1781125401
    • tpch_adapted_1_official_207927_1781125560
    • tpch_adapted_1_official_207947_1781125561

@zvonand

zvonand commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

CH Inc sync — tests failed

I cannot fix them as I do not know them 🥲

@alexey-milovidov

Copy link
Copy Markdown
Member

Triage of the CH Inc sync failure (the only red; public CI is fully green and all review threads are resolved):

Nothing to change in this PR itself.

@alexey-milovidov

Copy link
Copy Markdown
Member

Status: public CI is green (144/144, 0 failures), all review threads resolved, branch is mergeable. The sole remaining red is CH Inc sync, which is a deterministic false positive of the private Check public build job (it overlays the private BackupIO_S3.{h,cpp} — which carry the private-only virtual copyFileFromBlobPath — onto a public clone, so override fails to compile for reasons that pre-date this PR).

The infra fix is clickhouse-private PR #60710 (skip files with pre-existing private/public divergence). I updated it to address review feedback — notably it now compares private master vs public master (not the checked-out PR HEAD), so the PR's own legitimate changes are still overlaid and built. Once #60710 merges into private master, rerunning the Check public build job will pick it up and the sync check will pass. After that this PR is waiting solely on review/merge.

@alexey-milovidov
alexey-milovidov merged commit 8a69dec into ClickHouse:master Jun 13, 2026
165 of 166 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 13, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request Jun 13, 2026
1 task
@filimonov

Copy link
Copy Markdown
Contributor

a continuation #113330

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 pr-improvement Pull request with some product improvements pr-performance Pull request with some performance improvements 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.

massive S3 request can run suboptimal

6 participants