Skip to content

Fix uneven shard distribution in StorageCache::shardIndex - #99384

Merged
serxa merged 4 commits into
masterfrom
fix-sharded-cache
May 13, 2026
Merged

serxa merged 4 commits into
masterfrom
fix-sharded-cache

Conversation

@serxa

@serxa serxa commented Mar 13, 2026 •

Copy link
Copy Markdown
Member

Fixes incorrect shard selection introduced in #97411.

StorageCache::shardIndex used & (NumShards - 1) to map a hash to a shard. This is only a correct modulo when NumShards is a power of two. With NumShards == 6, the bitmask is 5 == 0b101, so only bits 0 and 2 can be set in the result. The only possible shard indices are 0, 1, 4, 5 — shards 2 and 3 are never selected, making 2 out of 6 shards permanently empty and concentrating all lock contention onto the other four.

Fixed by using % NumShards instead, which is correct for any value of NumShards.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Note

Low Risk
Low risk, localized change that replaces an incorrect bitmask with modulo to ensure shard indices stay within the shards array bounds.

Overview
Fixes a potential out-of-bounds access in ContextData::StorageCache::shardIndex by switching from a bitmask (& (NumShards - 1)) to % NumShards, which correctly maps hashes to valid shard indices even when NumShards is not a power of two.

Written by Cursor Bugbot for commit a173407. This will update automatically on new commits. Configure here.

@clickhouse-gh

clickhouse-gh Bot commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [534e1e5]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_msan, WasmEdge, parallel, 1/2) FAIL
04039_merge_tree_snapshot_teardown_race FAIL cidb, issue

AI Review

Summary

This PR correctly fixes ContextData::StorageCache::shardIndex by replacing a power-of-two-only bitmask with % NumShards for NumShards = 6, so all shards can be selected instead of only a subset. I found no unresolved correctness, safety, or compatibility issues in the current code.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 13, 2026
`& (NumShards - 1)` is only a correct modulo when `NumShards` is a power
of two. With `NumShards == 6`, the bitmask is `5 == 0b101`, so only bits
0 and 2 can be set in the result. The possible shard indices are
0, 1, 4, 5 — shards 2 and 3 are never selected, making 2 out of 6
shards permanently empty and concentrating all load onto the other four.

Fix by using `% NumShards` instead.

Follow-up to #97411.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@serxa
serxa force-pushed the fix-sharded-cache branch from 54a9d27 to a173407 Compare March 13, 2026 11:59
@serxa serxa changed the title Fix out-of-bounds shard index in StorageCache::shardIndex Fix uneven shard distribution in StorageCache::shardIndex Mar 13, 2026
@serxa

serxa commented Mar 17, 2026

Copy link
Copy Markdown
Member Author

cc @scanhex12

@serxa
serxa requested a review from scanhex12 March 31, 2026 12:53
Comment thread src/Interpreters/Context.h Resolved
@serxa
serxa enabled auto-merge May 8, 2026 11:08
@serxa serxa added pr-improvement Pull request with some product improvements pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-bugfix Pull request with bugfix, not backported by default pr-improvement Pull request with some product improvements labels May 8, 2026
@serxa

serxa commented May 12, 2026

Copy link
Copy Markdown
Member Author

@groeneai, check the failed CI tests; they seem to be unrelated. Do we need to fix them in a separate pull request, or are they already fixed, and we just need to merge the master branch?

@groeneai

Copy link
Copy Markdown
Collaborator

@serxa — checked. Both failing checks are 100% chronic infrastructure flake unrelated to your one-line % NumShards change. No separate fix PR needed on your side; the upstream fix is already in flight.

Failing checks on 1bcb5a4d

# Check Test Pattern
1 Unit tests (asan_ubsan, function_prop_fuzzer) FunctionsStress.stress 1 threads got stuck
2 Unit tests (tsan, function_prop_fuzzer) FunctionsStress.stress 1 threads got stuck

Both fail at src/Functions/tests/gtest_functions_stress.cpp:2270 with failure_details.empty() — Actual: false / Expected: true. Confirmed via CIDB on this exact head SHA.

Root cause (new chronic regression, not your PR)

  • 2026-05-11 14:55:17 UTC — PR #103982 merges, converting the prev.thread_id == curr.thread_id chassert in RUsageCounters to a gtest check that surfaces stuck-worker failures.
  • 2026-05-11 15:18:32 UTC — first FunctionsStress.stress / threads got stuck failure ever recorded.
  • Your CI run hits at 17:59–18:04 UTC — squarely inside the regression window.
  • Underlying culprit: pathological-loop behavior in isProbablePrime (PR #104234). Worker threads hit Miller-Rabin with adversarial inputs and don't return; previously masked by the chassert path, now caught by the threads-stuck detection.

CIDB evidence (30 days, signature FunctionsStress.stress + "threads got stuck")

master PR unique PRs first seen last seen
All sanitizers 7 79 50 2026-05-11 15:18 2026-05-12 09:09

Spread across 50 unrelated PRs across all 3 sanitizer variants (asan_ubsan / tsan / msan) — unambiguous chronic infrastructure flake.

Fix in flight (no action needed from you)

Recommendation

Merge master once #104633 lands, then re-run the two unit-test checks. Your diff (one-line & (NumShards - 1) → % NumShards in StorageCache::shardIndex) has zero interaction with FunctionsStress / Functions/isPrime.cpp / RUsageCounters. The PR is otherwise green and approved.

If you'd prefer not to wait, re-running the two checks alone should also work — the failure rate is high but not deterministic (each individual re-run has a chance of passing).

@clickhouse-gh

clickhouse-gh Bot commented May 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (1/1) · Uncovered code

Full report · Diff report

@serxa
serxa added this pull request to the merge queue May 13, 2026
Merged via the queue into master with commit dcaa90a May 13, 2026
165 of 167 checks passed
@serxa
serxa deleted the fix-sharded-cache branch May 13, 2026 19:12
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

5 participants