Skip to content

Reorder operations in DiskAccessStorage for consistency: always modify files first - #104411

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
vitlibar:disk-access-storage-disk-first
May 13, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
vitlibar:disk-access-storage-disk-first

Conversation

@vitlibar

@vitlibar vitlibar commented May 8, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Reorder operations in DiskAccessStorage for consistency: always modify files first

Version info

  • Merged into: 26.5.1.621

@clickhouse-gh

clickhouse-gh Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [05844fa]

Summary: ✅


AI Review

Summary

This PR reorders DiskAccessStorage mutations to write/delete on disk before mutating memory_storage, and adds recovery tests for .tmp cleanup and duplicate deduplication. The main remaining risk is duplicate resolution still performs destructive cleanup when file timestamps are unavailable or ambiguous, which can delete the wrong *.sql file.

Findings

❌ Blockers

  • [src/Access/DiskAccessStorage.cpp:401-425] reloadAllAndRebuildLists logs mtime_ec but still uses mtime as an ordering signal and schedules deletion from that result. If last_write_time fails, this can pick the wrong survivor and delete valid data.
    Suggested fix: when mtime_ec is set for either duplicate candidate, take a non-destructive path (keep both files, keep need_rebuild_lists.mark, and skip deletion for that key).

⚠️ Majors

  • [src/Access/DiskAccessStorage.cpp:419-432] Equal-mtime duplicates are resolved by directory iteration order (else branch always deletes the later-iterated path). On coarse-resolution filesystems this is nondeterministic and can keep stale state.
    Suggested fix: avoid destructive cleanup on equal timestamps, or add an explicit stable tie-break rule that does not depend on iterator order.
Tests
  • ⚠️ Add a recovery test where last_write_time is unavailable/fails (or is treated as unknown) to verify duplicate cleanup is non-destructive in that case.
  • ⚠️ Add a duplicate test with equal mtimes to verify deterministic, safe behavior.
ClickHouse Rules
Item Status Notes
Deletion logging ✅
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ Startup recovery can still delete the wrong duplicate file when timestamp ordering is unreliable
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Destructive dedup on ambiguous ordering is risky for production rollout
Compilation time ✅
No large/binary files ✅
Performance & Safety
  • Safety concern: ambiguous/unavailable mtime currently feeds destructive duplicate cleanup.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Make duplicate resolution non-destructive when timestamp ordering is unavailable or ambiguous.
    2. Add regression coverage for unknown/equal mtime duplicate cases.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 8, 2026
@vitlibar
vitlibar requested a review from Copilot May 8, 2026 17:16
/// Do removing.
memory_storage.remove(id, /* throw_if_not_exists= */ false);

/// Step 2: Modify files first.

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 main idea of this PR is that we must modify files first, and internal memory storage after that - so if a failure appears while we're modifying files then the memory storage won't be modified and DiskAccessStorage will be in consistent state

Comment thread src/Access/DiskAccessStorage.cpp Resolved
@vitlibar
vitlibar force-pushed the disk-access-storage-disk-first branch from 08e7c2e to b86d582 Compare May 8, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts DiskAccessStorage update/insert/remove flows to apply on-disk changes before mutating in-memory state, and enhances rebuild/recovery to clean up orphan .tmp files and resolve duplicate <uuid>.sql entries deterministically.

Changes:

  • Reordered insertNoLock, removeNoLock, and updateNoLock to perform filesystem operations first, then commit to memory_storage (with a LOGICAL_ERROR guard if invariants are violated).
  • Extended reloadAllAndRebuildLists() to detect and later delete orphan <uuid>.tmp files and older duplicate <uuid>.sql files (same entity type+name).
  • Added gtests covering orphan tmp cleanup and duplicate-name resolution during rebuild.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Access/tests/gtest_disk_access_storage.cpp Adds recovery-focused unit tests for rebuild behavior (orphan .tmp cleanup, duplicate entity name resolution).
src/Access/DiskAccessStorage.cpp Reorders disk vs memory mutations for consistency; enhances rebuild to handle orphan temp files and duplicate entities.

Comment thread src/Access/DiskAccessStorage.cpp Resolved
@vitlibar
vitlibar force-pushed the disk-access-storage-disk-first branch from b86d582 to 72f1eee Compare May 8, 2026 18:00
@vitlibar
vitlibar requested a review from Copilot May 8, 2026 18:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/Access/DiskAccessStorage.cpp Resolved
@vitlibar
vitlibar force-pushed the disk-access-storage-disk-first branch from 72f1eee to 478f40b Compare May 8, 2026 18:31
@vitlibar
vitlibar requested a review from Copilot May 8, 2026 18:31
Comment thread src/Access/DiskAccessStorage.cpp Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread src/Access/DiskAccessStorage.cpp Resolved
Comment thread src/Access/DiskAccessStorage.cpp Resolved
Comment thread src/Access/DiskAccessStorage.cpp Resolved
@vitlibar
vitlibar force-pushed the disk-access-storage-disk-first branch from 478f40b to 05844fa Compare May 8, 2026 19:36
@vitlibar
vitlibar requested a review from Copilot May 8, 2026 19:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread src/Access/DiskAccessStorage.cpp Resolved
Comment thread src/Access/DiskAccessStorage.cpp Resolved
Comment thread src/Access/DiskAccessStorage.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 78.26% (144/184) · Uncovered code

Full report · Diff report

@Ergus Ergus self-assigned this May 9, 2026
@alexey-milovidov alexey-milovidov self-assigned this May 13, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 13, 2026
Merged via the queue into ClickHouse:master with commit ae05eb0 May 13, 2026
169 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

5 participants