Skip to content

Fix replicated database log retention check overflow - #118103

Merged
alexey-milovidov merged 15 commits into
masterfrom
fix-log-entry-check-overflow
Sep 26, 2026
Merged

alexey-milovidov merged 15 commits into
masterfrom
fix-log-entry-check-overflow

Conversation

@kirillshokhin

@kirillshokhin kirillshokhin commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

logs_to_keep setting sets how many log entries are retained in the replicated database DDL log.
It was 64 bit wide and the straightforward check whether a log queue entry is in the retention window may result in overflow and unexpected removal of entries from the queue or replica being declared lost when it shouldn't.

The head of the log pointer - max_log_ptr is 32-bit, and since it's derived from the Keeper node value we can't widen it to 64 bits.

This commit adds NonZeroUInt32 type for settings and uses it for the logs_to_keep setting. This way it's not possible to create a new database with logs_to_keep greater than UInt32::max. The retention check is rewritten in the overflow-safe way.

To maintain backward compatibility any existing logs_to_keep value greater than UInt32::max is clamped to the UInt32::max on ATTACH or read from the Keeper. UInt32::max effectively means retain all the logs, which is arguably the intentional behavior of a large value, rather than wraparound checks.

Related: #117544

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 an integer overflow in the DDL log retention check of Replicated databases that could cause premature removal of DDL log entries and replicas being incorrectly declared lost when logs_to_keep exceeded 4294967295. Out-of-range values are now rejected on CREATE and clamped (with a warning) when replayed from existing metadata, server config, or Keeper.


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

Version info

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

`logs_to_keep` setting sets how many log entries are retained in the
replicated database DDL log.
It was 64 bit wide and the straightforward check whether a log queue
entry is in the retention window may result in overflow and unexpected
removal of entries from the queue or replica being declared lost when it
shouldn't.

The head of the log pointer - `max_log_ptr` is 32-bit, and since it's
derived from the Keeper node value we can't widen it to 64 bits.

This commit adds `NonZeroUInt32` type for settings and uses it for the
`logs_to_keep` setting. This way it's not possible to create a new
database with `logs_to_keep` greater than `UInt32::max`.
The retention check is rewritten in the overflow-safe way.

To maintain backward compatibility any existing `logs_to_keep` value
greater than `UInt32::max` is clamped to the `UInt32::max` on `ATTACH`
or read from the Keeper. `UInt32::max` effectively means `retain all the
logs`, which is arguably the intentional behavior of a large value,
rather than wraparound checks.
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a7c3700]

Summary: ✅


AI Review

Summary

This PR narrows Replicated database logs_to_keep to a 32-bit non-zero carrier, makes the retention-window checks overflow-safe, and carries the compatibility clamp through metadata replay, Keeper state, and backup/export paths. I did not find any remaining contract violations in the current patch: the previously raised gaps around internal-wrapper execution, binary settings deserialization, Keeper warnings, and exported definitions are addressed, and the added tests now cover the important replay and round-trip cases.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit a7c3700.

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

Changed lines: Changed C/C++ lines covered: 148/149 (99.33%) · 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 4, 2026
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a7c370086 with master d5e7a4509 (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

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

70 object files changed (+48.93 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Databases/DatabaseReplicatedSettings.cpp.o 140.64 KiB 167.56 KiB +26.91 KiB (+19.14%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

* Use distinct shard name.
* Use metadata helpers to rewrite metadata file both with local and
  remote disk setup.
* Reset metadata cache before `ATTACH` re-reads the edited file.
Add the case checking that during the replica initialization it's not
declared lost due to an overflow error with too big `logs_to_keep` in
Keeper.
Comment thread src/Core/SettingsFields.h Resolved
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
`args.internal` is too broad flag for compatibility clamp check:
it includes `PARALLEL WITH`/`EXECUTE AS` client queries. Instead a
`is_metadata_load` flag is checked and a new `is_restore_from_backup`
flag is introduced to cover the restore from the backup containing the
old overflowing `logs_to_keep` values.

A new integration test covering restore from the backup with overflowing
`logs_to_keep` value is added.
* Make `05061_replicated_database_logs_to_keep_clamp` fail on probe
  timeouts.
* Fix hardcoded UUID in `05223_replicated_database_logs_to_keep_internal_wrapper`
alexey-milovidov and others added 3 commits September 19, 2026 20:48
* Write clamped `logs_to_keep` value to the metadata on DB restore.
* Clamped `logs_to_keep` value observed in `SHOW CREATE`,
  `engine_full` and `BACKUP`.
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

This PR only changes Replicated-database DDL-log retention plumbing: it narrows logs_to_keep to a range-checked 32-bit setting, clamps legacy over-range values on replay/restore/config, and adds an overflow-safe retention check. None of that touches the query-execution path a ClickBench SELECT exercises. Both flagged queries (Q16 +15.5%, Q18 +18.7%) sit inside master's current variance band and were already deterministically suppressed as within-envelope, so we downgrade them to not_sure — the deltas are run-to-run variance, not a real regression. Q16 is additionally marked noisy in its history, reinforcing that read.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 535 618 +15.5% <0.0001 This PR only touches Replicated-database DDL log retention (logs_to_keep clamping); it cannot affect a ClickBench SELECT hot path. The +15.5% is run-to-run variance on a query already flagged noisy.
⚠️ 18 not_sure 892 1059 +18.7% <0.0001 Same reasoning: the diff is Replicated-database backup/settings plumbing, not query execution. The +18.7% sits inside master's current variance band and reflects measurement noise, not a PR effect.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 97dca38b-ce2c-4915-92b4-32393f6e09e1
  • MIRAI run: bf0aadd4-1695-4a49-ae89-21be65c4b352
  • PR check IDs:
    • clickbench_1343644_1790090841
    • clickbench_1343657_1790090841
    • clickbench_1343664_1790090841
    • tpch_adapted_1_official_1343670_1790090841
    • tpch_adapted_1_official_1343700_1790090841
    • tpch_adapted_1_official_1343714_1790090841

@alexey-milovidov alexey-milovidov self-assigned this Sep 26, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 26, 2026
Merged via the queue into master with commit f2d400e Sep 26, 2026
177 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. 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