Skip to content

Do not regenerate part names from part infos for tables with the deprecated syntax - #114980

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-v0-part-name-regeneration
Aug 21, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-v0-part-name-regeneration

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

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 BAD_DATA_PART_NAME ("Trying to get part name in new format for old format version") on a ReplicatedMergeTree created with the deprecated positional syntax under allow_deprecated_syntax_for_merge_tree. A mutation could be reported as done on a replica that had not applied it, and the table could stay readonly after a restart.

Description

Reported by @ alexey-milovidov on #112457 (Upgrade check (amd_release) report). No related open issue found.

A table created with ENGINE = ReplicatedMergeTree(date, key, granularity) uses date-based (V0) part names. MergeTreePartInfo::getPartNameAndCheckFormat cannot produce one, because the min/max dates it needs are not part of a MergeTreePartInfo, so it throws. Two ActiveDataPartSet entry points take only an info and regenerate the name that way; each had exactly one caller, and both callers already held the name and threw it away. This change passes the name at both call sites, as in 536b611c96a757e, which fixed the ATTACH PARTITION instance of this bug. V0 name formatting is untouched, and current-format tables are unaffected because the name-taking overload re-parses to the same info and runs the same insertion code.

The linked report contains both failures:

  • ReplicatedMergeTreeQueue::getPartNamesToMutate builds mutation.parts_to_do. The throw left it empty, so a mutation reported is_done on a replica that never mutated its part, and mutations_sync = 2 returned success while that replica still served pre-mutation values. Regressed by Improve updating mutations #87206, which changed produced_parts to hold infos instead of names.
  • StorageReplicatedMergeTree::checkPartsImpl builds the set of empty unexpected parts. The throw aborted ReplicatedMergeTreeAttachThread, leaving the table readonly. Regressed by my own Fix LOGICAL_ERROR "Part X intersects previous part Y" during ReplicatedMergeTree startup #103537, which replaced a name-based call with an info-based one.

The V0 gap in MergeTreeData::restorePartFromBackup that the throw-site comment describes is a different code path, absent from both stacks, and is not addressed.

A new stateless test asserts parts_to_do on the frozen replica: it fails on master (parts to do 0) and passes here (parts to do 2), and 50/50 randomized runs pass. checkPartsImpl's state needs parts fabricated on disk, so that half is covered by three V0 cases added to gtest_active_data_part_set.cpp, all of which fail with this change reverted. 00062_replicated_merge_tree_alter_zookeeper_long and 00652_replicated_mutations_zookeeper still pass.

…ecated syntax

A table created with the deprecated positional syntax
(ENGINE = ReplicatedMergeTree(date, key, granularity) under
allow_deprecated_syntax_for_merge_tree) uses date-based V0 part names.
MergeTreePartInfo::getPartNameAndCheckFormat cannot produce such a name,
because getPartNameV0 needs the min/max DayNum that MergeTreePartInfo does
not carry, so it throws BAD_DATA_PART_NAME unconditionally.

Two ActiveDataPartSet entry points accept only a MergeTreePartInfo and must
therefore synthesize a name that way: tryAddPart and add(info, Strings *).
Each had exactly one production caller, and both callers already held the
part name and discarded it:

  * ReplicatedMergeTreeQueue::getPartNamesToMutate builds mutation.parts_to_do.
    The throw aborted mutationsUpdatingTask and left parts_to_do empty, so the
    mutation was reported as done on a replica that had not applied it. With
    mutations_sync = 2 the ALTER returned success while that replica still
    served pre-mutation values. Regressed by ClickHouse#87206, which changed
    QueueEntryRepresentation::produced_parts from a vector of names to a vector
    of infos.

  * StorageReplicatedMergeTree::checkPartsImpl builds the set of empty
    unexpected parts on disk. The throw aborted ReplicatedMergeTreeAttachThread
    and left the table readonly. Regressed by ClickHouse#103537, which replaced
    tryAdd(name, &reason) with add(info, name, ...).

Both failures appear in a single Upgrade check report on ClickHouse#112457. Pass the
name that is already in hand at both call sites instead. V0 name formatting is
untouched, and behaviour on current-format tables is provably unchanged
because the name-taking overload re-parses the name into the identical info
and runs the same addImpl. This is the remedy alexey-milovidov used in
536b611 for the ATTACH PARTITION instance of
the same defect, which added ActiveDataPartSet::tryAdd(const String &).

dropped_parts is deliberately left holding infos only:
removePartAndCoveredParts(info) is getPartsCoveredBy + remove, neither of
which regenerates a name.

The V0 gap in MergeTreeData::restorePartFromBackup that the comment at the
throw site describes is a different code path, absent from both stacks here,
and is not addressed.

Testing: 04907_mutation_parts_to_do_deprecated_syntax asserts parts_to_do on
the frozen replica and fails on master with "parts to do 0". checkPartsImpl's
state requires fabricating parts on disk, which is not allowed in a stateless
test, so that half is covered by three V0 cases in gtest_active_data_part_set,
all of which fail with this change reverted.
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (1 finding, resolved)

An independent model reviewed this diff cold against the frozen PR body. One finding, adjudicated
below. Two further nits from my own review are recorded as noted-not-blocking.

⚠️ 04907_...deprecated_syntax.sql - automatic merge selection on v0_r1 could change the
asserted queue and parts_to_do counts (test determinism) - DISAGREE

Only v0_r2 is frozen, and a successful insert does schedule the leader's merge selector
(ReplicatedMergeTreeSink.cpp:1243), so the shape of the concern is right. But the leader only
creates a MERGE_PARTS entry if SimpleMergeSelector::select returns a range, which needs
allow() at SimpleMergeSelector.cpp:195 to hold for a two-part range, and it cannot here:

  • LHS is (s1 + s2 + 2f) / (max(s1,s2) + f) with f = size_fixed_cost_to_add, which is <= 2.0
    for all sizes, equal only when the two parts are byte-identical.
  • RHS is lowered_base = 5 - 3c (:179) with c = min(1, age_norm + parts_norm), and
    parts_norm = 0 for a 2-part partition (:175, thresholds 10/50).
  • So allow() needs age_norm >= 1, i.e. min_age >= max_age_to_lower_base, which for sub-MB
    parts is max_age_to_lower_base_at_min_size = 3600 seconds (SimpleMergeSelector.h:143).

The test's entire wall clock is 0.83 - 1.34 s across 50 runs. Feeding age = 3600 into the same
formula does flip allow() to true, so the bound discriminates rather than being vacuous. No
bypass is reachable on this path: min_age_to_force_merge_seconds defaults to 0, blurry base is
off, aggressive_ = false at both leader call sites (StorageReplicatedMergeTree.cpp:4497,
:4517), the algorithm default is SIMPLE, the table has no TTL, and the test issues no
OPTIMIZE. Nothing under tests/config/ overrides those for the stateless or upgrade suites.

The suggested remedy would also be wrong here. The cited precedent pins a non-replicated
MergeTree, where max_bytes_to_merge_at_max_space_in_pool = 1 leaves mutations alone. On a
ReplicatedMergeTree that value makes getMaxSourcePartsBytesForMerge return 0
(CompactionStatistics.cpp:107) and sets ignore_max_size in shouldExecuteLogEntry
(ReplicatedMergeTreeQueue.cpp:1848), i.e. it adds a new gate over the mutation bookkeeping this
test measures. 19 of the 20 stateless tests that read system.replication_queue pin no merges.

💡 Noted, not blocking

  • Both name-regenerating ActiveDataPartSet entry points (tryAddPart, add(info, Strings *))
    are left with no callers. Deleting them would widen a bug-fix diff, and the precedent fix for
    this defect class added an overload rather than removing one. One pre-existing doc comment in
    gtest_active_data_part_set.cpp still names tryAddPart.
  • The new test asserts the corrected bookkeeping but not the absence of the exception text. A log
    assertion would add flakiness for no coverage gain; the system.errors delta was measured out of
    band instead (+5 on master, 0 with the fix).

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate
# Gate Answer
a Deterministic repro? Yes. Plain SQL (V0 ReplicatedMergeTree + a pending queue entry + ALTER ... UPDATE), 3/3 identical on the base binary; the new test fails deterministically on master through tests/clickhouse-test.
b Root cause explained? Yes. getPartNameAndCheckFormat cannot rebuild a date-based name from a MergeTreePartInfo (the min/max dates are not stored there) and throws. The two ActiveDataPartSet entry points that take only an info hit it; both of their callers already held the name.
c Fix matches root cause? Yes. The name is passed at both call sites rather than regenerated. No widened bound, no no-random-* tag, no guard at the throw site, no swallowed exception. Same remedy as 536b611c96a757e.
d Test intent preserved / new tests added? Yes. No existing assertion weakened or removed. One new stateless test plus three V0 cases in gtest_active_data_part_set.cpp.
e Demonstrated in both directions? Yes. parts to do 0 (fails) on master vs parts to do 2 (passes) here; the three gtest cases fail with the source change reverted and pass with it.
f General across code paths? Yes. getPartNameAndCheckFormat has 11 call sites; exactly two reached name regeneration inside ActiveDataPartSet, and after this change both have zero production callers. dropped_parts is unchanged because removePartAndCoveredParts(info) never regenerates a name. No SharedMergeTree or Replicated database counterpart exists, since the deprecated syntax is rejected on both.
g Generalizes across inputs? Yes on the relevant axis. The value is a part-name string, so type wrappers do not apply. Both format versions are covered (three new V0 cases, eight pre-existing current-format cases), as are the disjoint, intersecting-without-containment and fully-contained shapes.
h Backward compatible? Yes. No on-disk format, serialization, protocol, setting or default change, so no SettingsChangesHistory.cpp entry. No new public API.
i Invariants and contracts preserved? Yes. The same parsed info reaches the same addImpl, so the pairwise disjoint-or-contained invariant, the add outcome and out_replaced_parts are unchanged for current-format tables. OverlappingPartCovering's auxiliary-list guarantee from #103537 still holds (asserted). parts_to_do is restored to holding real part names, which is what its consumers read. No new shared state and no lock ordering touched.

@clickhouse-gh

clickhouse-gh Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7df7ccd]

Summary: ✅


AI Review

Summary

This PR fixes the two production paths that were still trying to reconstruct V0 part names from MergeTreePartInfo: mutation parts_to_do initialization in ReplicatedMergeTreeQueue and empty-unexpected-part covering in OverlappingPartCovering. The current diff keeps the fix narrow, preserves the existing behavior for current-format tables, and adds focused regression coverage for both the mutation path and the startup-helper path. I did not find an unresolved correctness, compatibility, or evidence gap in the current code.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.10% 79.10% +0.00%

Changed lines: Changed C/C++ lines covered: 47/52 (90.38%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alesapin @Michicosun, could you review this? Two ActiveDataPartSet entry points take only a MergeTreePartInfo and so rebuild the part name via getPartNameAndCheckFormat, which cannot produce a date-based name and throws BAD_DATA_PART_NAME, leaving parts_to_do empty in getPartNamesToMutate and aborting the attach thread in checkPartsImpl; both callers already held the name, so this passes it instead.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Aug 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7df7ccdd0 with master 5c9f28ed5 (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
Binary Master PR Δ
programs/clickhouse-stripped 694.55 MiB 691.54 MiB -3.01 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

…iming

The setup-sanity line counted every entry in r2's replication queue. r2 can
still hold an unprocessed GET_PART entry for its own part at that point: the
entry is normally consumed by the "part already exists" fast path in
StorageReplicatedMergeTree::executeLogEntry, but SYSTEM STOP FETCHES makes
canExecuteFetch postpone it first, so it stays queued. The count is then 2
rather than 1. Reproduced by delaying r2's queue processing until after the
second INSERT; under TSAN the same interleaving happens on its own, which
failed the flaky check 4 times in 50 runs while amd_asan_ubsan, amd_msan,
amd_debug and amd_binary stayed green.

Counting only GET_PART entries for parts r2 does not already have is stable
in both interleavings and still reddens when no entry is pending at all,
which keeps the precondition meaningful: with an empty queue both parts reach
parts_to_do through the name-based path and the assertion under test would
pass without exercising the fix.

The four assertions the test exists for are untouched and were unchanged in
every failing run.
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 7df7ccd

CI is fully finished on this head and there are no failures to own: 177 check runs completed,
160 success / 17 skipped, 0 failures, Config Workflow and Finish Workflow both success, and
the praktika report shows 0 dropped jobs (so this is a run with real coverage, not a dropped one).

Check / test Reason Owner / fixing PR
(none) no failing check on this commit nothing to own

The 04907_mutation_parts_to_do_deprecated_syntax failure in Stateless tests (amd_tsan, flaky check)
was on the earlier head 71a487618cd6; on this head all five flaky checks pass 50/50.

Session id: cron:our-pr-ci-monitor:20260816-123000

@groeneai groeneai added the groeneai-origin-request PR origin: a maintainer pinged or directed groeneai label Aug 19, 2026

@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.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Aug 21, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 21, 2026
Merged via the queue into ClickHouse:master with commit 5318ea2 Aug 21, 2026
183 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 21, 2026
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 groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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.

3 participants