Skip to content

Fix MergeTreeDeduplicationLog losing the active log and leaving phantom blobs - #121670

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-deduplication-log-phantom-blob
Sep 23, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-deduplication-log-phantom-blob

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Three fixes in MergeTreeDeduplicationLog (used by non_replicated_deduplication_window):

  • dropOutdatedLogs had an off-by-one: once the sum of entries reached the deduplication window, it dropped all older logs including the current one, which the writer may still append to. The active log is now never dropped, and its entries count towards the window.
  • The appending writer for an unfinished log was opened eagerly on load and on setDeduplicationWindowSize. On object storage, finalizing an empty WriteMode::Append buffer registers a blob in the file's metadata without uploading any object. So every DETACH/ATTACH cycle with no inserts in between added one phantom blob, and the next load failed with NoSuchKey ("Error while loading MergeTree deduplication log"). The writer is now opened lazily in prepareToWrite, when the first record is written.
  • sync the writer after finalizing it in shutdown.

New test 04640_deduplication_log_reattach_no_phantom_blobs (S3 disk, three DETACH/ATTACH cycles).

Split out of #96130, where the random DETACH/ATTACH testing hook found the bug.

Related: #96130

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 MergeTree tables with non_replicated_deduplication_window on object storage failing to load their deduplication log (NoSuchKey) after being detached and attached back without inserts in between, and fix the deduplication log possibly dropping its active file.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/121670]

Version info

  • Merged into: 26.10.1.523-master (included in 26.10 and later)

…ntom blobs

- `dropOutdatedLogs` had an off-by-one: once the sum of entries reached the
  deduplication window, it dropped "all older files, including current", so
  the active log that the writer still appends to could be removed. Never drop
  the current log, and count its entries towards the window.
- The appending writer for an unfinished log was opened eagerly in `load` and
  `setDeduplicationWindowSize`. On object storage, finalizing an empty
  `WriteMode::Append` buffer registers a blob in the file's metadata without
  uploading any object, so every `DETACH`/`ATTACH` cycle without inserts in
  between added one phantom blob, and the next load failed with `NoSuchKey`
  ("Error while loading MergeTree deduplication log"). Open the writer lazily
  in `prepareToWrite` on the first written record.
- `sync` the writer after finalizing it in `shutdown`.

Split out of #96130

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d4c8257]

Summary: ✅


AI Review

Summary

This PR fixes MergeTreeDeduplicationLog retention and writer-lifecycle handling around non_replicated_deduplication_window, including the object-storage phantom-blob bug on DETACH/ATTACH. The implementation looks reasonable, but the new evidence only covers the load() / DETACH/ATTACH path; the other behavior changes in the patch are still unpinned by regression coverage, so I would not approve it yet.

Tests
  • ⚠️ [tests/queries/0_stateless/04640_deduplication_log_reattach_no_phantom_blobs.sql:27] The added test proves the DETACH/ATTACH phantom-blob scenario, but it does not exercise the dropOutdatedLogs() equality boundary or the lazy-open path in setDeduplicationWindowSize(). Please add a focused regression for one of those paths, such as an exact-window retention case or an ALTER TABLE ... MODIFY SETTING non_replicated_deduplication_window cycle on s3_disk with no inserts between toggles.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: add focused regression coverage for the other changed MergeTreeDeduplicationLog path(s), not just the DETACH/ATTACH phantom-blob case.

LLVM Coverage Report

Measured on commit d4c8257.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 25/26 (96.15%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 23, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 23, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The changes LGTM.

@alexey-milovidov alexey-milovidov self-assigned this Sep 23, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 23, 2026
Merged via the queue into master with commit 18a2ec0 Sep 23, 2026
177 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-deduplication-log-phantom-blob branch September 23, 2026 16:45
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 23, 2026
wickdChromosome pushed a commit to wickdChromosome/ClickHouse that referenced this pull request Sep 26, 2026
… SETTING`

ClickHouse#121670 stopped
`MergeTreeDeduplicationLog::setDeduplicationWindowSize` from opening an
appending writer for the unfinished log eagerly, but its test only covered
the `load` path. Toggle `non_replicated_deduplication_window` on `s3_disk`
without inserts between the toggles, with `DETACH`/`ATTACH` after each one.

Verified locally against MinIO: a binary without the fix logs
"Error while loading MergeTree deduplication log" (`NoSuchKey`) on both
reattaches after the `ALTER`, a binary with the fix logs none.

Related: ClickHouse#121670

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
alexey-milovidov pushed a commit to melvynator/ClickHouse that referenced this pull request Nov 10, 2026
A file on an object-storage disk with local metadata can list an object of
size 0 whose key was never uploaded: an empty append records one (StripeLog
`INSERT ... SELECT ... LIMIT 0` on every release since 25.3,
MergeTreeDeduplicationLog before ClickHouse#121670). `initialize` never selects such an
object, but the sequential walk does: when the current object ends,
`moveToNextBuffer` builds a reader for the next one whatever its size, and for
a 0-byte object that is an open-ended GET of a missing key (S3_ERROR
NoSuchKey), or, when the reader returns no bytes (filesystem cache, Azure),
`nextImpl`'s single move + read ends the read early (ATTEMPT_TO_READ_AFTER_EOF,
CANNOT_READ_ALL_DATA).

The walk is reached by a sequential read across the object boundary
(`max_threads = 1`, BACKUP), by the gather's own short forward seek
(`remote_filesystem_read_method = 'read'`), and since ClickHouse#100763 by the default
threadpool path, whose bounded `AsynchronousBoundedReadBuffer` turns the seek
between StripeLog blocks into bytes to ignore. So a StripeLog table on S3 with
an empty INSERT in the middle cannot be read on 26.4+ (including 26.8 LTS),
and a table written on 26.3 becomes unreadable after the upgrade.

Drop 0-byte objects from the object list in the constructor. They add no bytes
to the file, so no file offset changes, and every walk of the list then agrees
with `initialize`.

The new integration test writes the table on 26.3.42.3 and reads it after
upgrading to the current binary, on a plain s3 disk and on a cache over it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
lazerg pushed a commit to lazerg/ClickHouse that referenced this pull request Nov 12, 2026
With local metadata, an empty `WriteMode::Append` uploads no object
(`WriteBufferInlineOrBlob` cancels the upload when `create_blob_if_empty`
is not set), but `AddBlobOperation` still listed the 0-byte object in the
file's metadata. The metadata then referenced a key that does not exist.
`StripeLogSink` opens `data.bin` for append even for an INSERT that writes
no rows, so `INSERT ... SELECT ... LIMIT 0` followed by another INSERT made
every later read of the table fail with `S3_ERROR` (NoSuchKey).

`AddBlobOperation::execute` now skips a 0-byte object and still writes the
metadata file, so a first empty append still creates the file. This is the
contract the in-memory metadata backend of ClickHouse#100371 already has; ClickHouse#121670
fixed the same phantom at the caller for `MergeTreeDeduplicationLog`.

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

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. 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.

2 participants