Skip to content

Fix flaky UniqueKeyIndexCache.InsertLookupAndHitMissCounters - #106839

Merged
azat merged 1 commit into
ClickHouse:masterfrom
murphy-4o:fix-flaky-unique-key-index-cache-counters
Jun 9, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
murphy-4o:fix-flaky-unique-key-index-cache-counters

Conversation

@murphy-4o

@murphy-4o murphy-4o commented Jun 9, 2026 •

Copy link
Copy Markdown
Member

Caused by: #104282

Fixes the flaky test UniqueKeyIndexCache.InsertLookupAndHitMissCounters.

Root cause

The test reads hit/miss deltas from ProfileEvents::global_counters, but ProfileEvents::increment (called inside UniqueKeyIndexCache::Lookup) routes through CurrentThread::getProfileEvents:

return current_thread ? *current_thread->current_performance_counters : ProfileEvents::global_counters;

The increment reaches global_counters only when no ThreadStatus is attached to the current thread.

All gtests in unit_tests_dbms run on a single main thread, and some tests attach a ThreadStatus to it that persists for the rest of the binary - for example gtest_filecache sets current_thread = MainThreadStatus::get() and never clears it. Once that has happened, the UniqueKeyIndexCache increments land in that thread's current_performance_counters subtree, which does not propagate to global_counters, so the deltas the test reads are 0 instead of 1 and the assertions fail.

Whether this happens depends purely on gtest execution order, and CI runs with --gtest_shuffle, which is why the failure is flaky. The pre-existing WITH_COVERAGE guard in the test was a partial workaround for the same underlying issue (coverage builds always set up a thread-local subtree).

Fix

Read the deltas from CurrentThread::getProfileEvents - the exact Counters object the increment targets - so the assertion is correct whether or not a ThreadStatus is attached to the main thread. This also makes the WITH_COVERAGE special case unnecessary, so it is removed.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Documentation entry for user-facing changes

  • No documentation needed.

Version info

  • Merged into: 26.6.1.573

The test read hit/miss deltas from `ProfileEvents::global_counters`, but
`ProfileEvents::increment` (called inside `UniqueKeyIndexCache::Lookup`) routes
through `CurrentThread::getProfileEvents`, which returns `global_counters` only
when no `ThreadStatus` is attached to the current thread.

All gtests in `unit_tests_dbms` run on one main thread, and some tests attach a
`ThreadStatus` to it that persists for the rest of the binary (e.g.
`gtest_filecache` sets `current_thread = MainThreadStatus::get()`). Once that has
happened, the increments land in that thread's `current_performance_counters`
subtree, which does not propagate to `global_counters`, so the deltas read 0
instead of 1. Whether it happens depends on gtest execution order, and CI runs
with `--gtest_shuffle`, hence the flakiness.

Read the deltas from `CurrentThread::getProfileEvents` instead - the exact
`Counters` object the increment targets - so the assertion is correct whether or
not a `ThreadStatus` is attached. This also makes the previous `WITH_COVERAGE`
special case unnecessary, so it is removed.

Co-Authored-By: Claude <[email protected]>
@murphy-4o murphy-4o mentioned this pull request Jun 9, 2026
1 task
@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6e944d4]

Summary: ✅


AI Review

Summary

This PR fixes the flaky UniqueKeyIndexCache.InsertLookupAndHitMissCounters gtest by reading hit/miss deltas from the same ProfileEvents::Counters object that ProfileEvents::increment updates through CurrentThread::getProfileEvents, so the assertion works whether the main gtest thread has an attached ThreadStatus or falls back to ProfileEvents::global_counters. I found no correctness, safety, test-quality, or PR metadata issues that need reviewer comments.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 9, 2026
@murphy-4o
murphy-4o marked this pull request as ready for review June 9, 2026 13:35
@kssenii kssenii self-assigned this Jun 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: Changed C/C++ lines covered by tests: 12/12 (100.00%) | Lost baseline coverage: none · Uncovered code

Newly covered by added/modified tests: 412 line(s), 43 function(s) across 118 file(s) · Details

Top files
  • src/Databases/DataLake/RestCatalog.cpp: 40 line(s), 3 function(s)
  • src/Storages/StorageReplicatedMergeTree.cpp: 16 line(s)
  • src/Functions/TimeSeries/TimeSeriesTagsFunctionHelpers.cpp: 13 line(s)
  • src/Storages/Statistics/ConditionSelectivityEstimator.cpp: 11 line(s), 2 function(s)
  • src/Databases/DataLake/DatabaseDataLake.cpp: 11 line(s)

Full report · Diff report

@azat
azat added this pull request to the merge queue Jun 9, 2026
Merged via the queue into ClickHouse:master with commit 8768b49 Jun 9, 2026
166 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 9, 2026
pull Bot pushed a commit to CrazyForks/ClickHouse that referenced this pull request Jun 11, 2026
ConnectionPoolTest::SetUp and ResolvePoolTest::SetUp called
CurrentThread::getProfileEvents().reset(), which does setParent(nullptr) —
once any earlier test attaches a ThreadStatus to the gtest main thread, this
permanently unlinks the thread's counters from ProfileEvents::global_counters.
Use resetCounters instead, which zeroes values but keeps the parent chain
intact.

Related: ClickHouse#106839

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

4 participants