Repository navigation
Fix LOGICAL_ERROR and data loss in mutation when part.metadata_version > table.metadata_version - #102349
Fix LOGICAL_ERROR and data loss in mutation when part.metadata_version > table.metadata_version#102349tiandiwonder wants to merge 1 commit into
Conversation
|
Workflow [PR], commit [53be33e] Summary: ❌
AI ReviewSummaryThis PR fixes mutation handling when Findings
ClickHouse Rules
Final Verdict
|
| /// the next schema-change mutation (e.g. the ADD COLUMN materialisation) does not | ||
| /// overwrite the data that was already carried through by this mutation. | ||
| /// effective_metadata_snapshot already holds the correct max() version in that case. | ||
| ctx->new_data_part->setColumns(new_columns, new_infos, ctx->effective_metadata_snapshot->getMetadataVersion()); |
There was a problem hiding this comment.
MutateTask::prepare now builds effective_metadata_snapshot, but MutateSomePartColumnsTask still constructs MergedColumnOnlyOutputStream with ctx->metadata_snapshot.
In the subset-mutation path, MergedColumnOnlyOutputStream::fillChecksums calls new_part->setColumns(..., metadata_snapshot->getMetadataVersion()), so with a concrete state part.meta=12, table.meta=11 we still end with output part meta=11 after fillChecksums, despite ctx->new_data_part->setColumns(..., 12) earlier.
That downgrades metadata_version and can re-trigger silent overwrite on the next schema-materialization mutation.
Please pass ctx->effective_metadata_snapshot here as well (same as MutateAllPartColumnsTask) so both mutation writer paths preserve max(part, table).
46d0eb7 to
a988a45
Compare
a988a45 to
53be33e
Compare
| # r2 will NOT be affected: it already processed its ALTER_METADATA and | ||
| # won't execute it again. | ||
| # --------------------------------------------------------------------------- | ||
| $CLICKHOUSE_CLIENT -q "SYSTEM ENABLE FAILPOINT rmt_pause_before_apply_metadata_alter" |
There was a problem hiding this comment.
SYSTEM ENABLE FAILPOINT rmt_pause_before_apply_metadata_alter is not protected by a cleanup handler. With set -e, any assertion/command failure before the explicit disable step leaves this failpoint enabled for the server process, which can contaminate later tests in the same run.
Please add a trap-based cleanup right after enabling (or at script start), e.g. trap "$CLICKHOUSE_CLIENT -q \"SYSTEM DISABLE FAILPOINT rmt_pause_before_apply_metadata_alter\" || true" EXIT, and clear the trap after successful disable if desired.
LLVM Coverage Report
Changed lines: 97.83% (45/46) | lost baseline coverage: 3 line(s) · Uncovered code |
|
Dear @tiandiwonder, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message. |
|
close it for now, will check it in future. |
A part stores its metadata version on disk in metadata_version.txt and keeps it verbatim when it is fetched from another replica (DataPartsExchange.cpp:264-266). A replica's table metadata version, in contrast, only advances through setTableStructure (StorageReplicatedMergeTree.cpp:1822), whose only caller is executeMetadataAlter (:6731), so it advances only when that replica executes its own ALTER_METADATA queue entry. A replica can therefore hold a fetched part at metadata version 1, containing a column the replica does not know about yet, while its own snapshot is still at version 0. A plain data mutation such as ALTER DELETE carries alter_version = -1, so isAlterMutation() is false and the alter_sequence gating in shouldExecuteLogEntry does not apply to it. splitAndModifyMutationCommands then iterates the part's columns, finds one that is absent from the table snapshot and throws LOGICAL_ERROR at MutateTask.cpp:510, which aborts the server in debug and sanitizer builds. The soft path just above it only covers the opposite ordering, where the part is older than the table and the column was genuinely dropped. This is a gap between the two subclasses of ReplicatedMergeMutateTaskBase. MergeFromLogEntryTask::prepare already refuses to run in exactly this state (MergeFromLogEntryTask.cpp:186-196, "ALTER_METADATA is still in progress"); the mutation path had no equivalent. Add the same rule for MUTATE_PART entries in ReplicatedMergeTreeQueue::shouldExecuteLogEntry, so such an entry is postponed until the pending ALTER_METADATA is applied, after which the mutation runs normally. The queue predicate is used rather than MutateFromLogEntryTask::prepare so that nothing is reserved or locked before bailing out: prepare would already have built a FutureMergedMutatedPart, reserved disk, taken a table lockForShare, inserted a MergeListEntry and, under zero-copy replication, possibly taken a zero-copy lock. Writing the reason through LogToStr also surfaces it in system.replication_queue.postpone_reason. MutateTask.cpp is deliberately unchanged. The assertion keeps its full meaning and the equal-versions case still throws, so this is not the soft-keep direction of ClickHouse#96008, which was reverted in ClickHouse#97301 as a data-loss risk. ClickHouse#102349 reached the same placement and was closed by its own author. The guard is unconditional, matching the merge sibling, so it inherits that guard's liveness assumption that some ALTER_METADATA eventually applies. Every producer of the mismatch keeps or plants one, including the dummy_alter entry that the recovery path synthesizes (StorageReplicatedMergeTree.cpp:3966-3976). Closes: ClickHouse#91015 Co-Authored-By: Claude Fable 5 <[email protected]>
Fixes #102153
Prior Fix and Revert
Analysis of why the revert was justified:
PR #96008 caused column h to enter for_interpreter as a READ_COLUMN command, so MutationsInterpreter
would attempt to SELECT column h. However, available_columns — derived from
metadata_snapshot->getColumns().getNamesOfPhysical() — does not include h, causing the interpreter to
throw UNKNOWN_IDENTIFIER: Missing columns: 'h' at execution time. Even setting that error aside, the
output part's metadata_version would be written as the current table version (N), while the part
physically contains column h. This leaves the part in an inconsistent state: on the next mutation,
part.meta == table.meta but h is present in the part yet absent from the schema, which is a genuine
invariant violation.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix mutation LOGICAL_ERROR and silent data loss when a part fetched from a more advanced replica has
part.metadata_version > table.metadata_version
Documentation entry for user-facing changes