Skip to content

Bypass filesystem cache in case of disk failure - #83823

Closed
codeworse wants to merge 7 commits into
ClickHouse:masterfrom
codeworse:filesystem_cache_fail
Closed

codeworse wants to merge 7 commits into
ClickHouse:masterfrom
codeworse:filesystem_cache_fail

Conversation

@codeworse

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

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

On ClickHouse start, which read data from S3, the parts may be marked as broken-on-start in case of filesystem cache disk failure. I think the problem with local disk should not affect the parts(should not detach it). So, I've added bypass filesystem cache in case of filesystem error.

Documentation entry for user-facing changes

  • Add tests

@codeworse
codeworse marked this pull request as ready for review July 16, 2025 10:31
@GrigoryPervakov GrigoryPervakov added the can be tested Allows running workflows for external contributors label Jul 16, 2025
@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b2bf257]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 16, 2025
@alexey-milovidov

Copy link
Copy Markdown
Member

Yes, the parts should not be marked as broken, but I would like to keep the exception visible and refuse server startup.

@tiandiwonder
tiandiwonder requested a review from Copilot December 1, 2025 05:41

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 addresses an issue where ClickHouse parts using S3 storage with filesystem cache could be incorrectly marked as broken during server startup if the local cache disk encounters errors. The fix ensures that filesystem cache errors bypass the cache rather than failing the entire read/write operation.

Key Changes:

  • Added error handling to gracefully handle filesystem errors in cache operations, allowing reads/writes to continue without caching
  • Introduced a failpoint (cache_filesystem_failure) to simulate cache disk failures for testing
  • Added integration test to verify that parts are not detached when cache disk fails

Reviewed changes

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

Show a summary per file
File Description
src/Interpreters/Cache/FileSegment.cpp Added failpoint and filesystem_error exception handling to prevent cache failures from propagating
src/Disks/IO/CachedOnDiskWriteBufferFromFile.cpp Added failpoint and catch block for filesystem_error to log and skip cache on errors
src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp Added filesystem_error catch block to gracefully handle cache write failures during reads
src/Common/FailPoint.cpp Registered new cache_filesystem_failure failpoint
tests/integration/test_cache_bypass_on_disk_fail/test.py Integration test verifying parts aren't detached when cache disk fails
tests/integration/test_cache_bypass_on_disk_fail/configs/s3_cache.xml Test configuration for S3 with filesystem cache

Comment on lines +477 to +480
catch (const fs::filesystem_error &)
{
setDownloadFailed();
throw;

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

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

This catch block rethrows filesystem_error exceptions, which contradicts the PR's goal of bypassing cache failures. The exception will still propagate and potentially cause parts to be marked as broken. The handler should consume the exception like in the other files, not rethrow it.

Suggested change
catch (const fs::filesystem_error &)
{
setDownloadFailed();
throw;
catch (const fs::filesystem_error & e)
{
setDownloadFailed();
LOG_WARNING(log, "Filesystem cache failure ignored: {}", e.what());

Copilot uses AI. Check for mistakes.
{
file_segment.write(data, size, offset);
}
catch (std::filesystem::filesystem_error & e)

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

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

Exception should be caught by const reference (const std::filesystem::filesystem_error & e) to prevent unnecessary copying and follow C++ best practices. This is inconsistent with line 797 which catches ErrnoException correctly.

Suggested change
catch (std::filesystem::filesystem_error & e)
catch (const std::filesystem::filesystem_error & e)

Copilot uses AI. Check for mistakes.
#endif
fiu_do_on(FailPoints::cache_filesystem_failure,
{
throw std::filesystem::filesystem_error("FailPoint while caching data", std::error_code());

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

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

Corrected capitalization: 'FailPoint' should be 'Failpoint' to match the error message in CachedOnDiskWriteBufferFromFile.cpp line 333.

Suggested change
throw std::filesystem::filesystem_error("FailPoint while caching data", std::error_code());
throw std::filesystem::filesystem_error("Failpoint while caching data", std::error_code());

Copilot uses AI. Check for mistakes.
SELECT * FROM t0
"""
)
assert node.query("SELECT count(*) FROM system.detached_parts WHERE table = 't0'") == '0\n'

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

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

The test should verify that data can still be read correctly after bypass, not just that parts aren't detached. Add an assertion to validate the actual query results match the inserted data to ensure correctness when cache is bypassed.

Copilot uses AI. Check for mistakes.
@tiandiwonder tiandiwonder self-assigned this Dec 1, 2025
@tiandiwonder
tiandiwonder self-requested a review December 1, 2025 05:48
@tiandiwonder

Copy link
Copy Markdown
Contributor

Yes, the parts should not be marked as broken, but I would like to keep the exception visible and refuse server startup.

agree

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

Add a new parameter to skip disk cache failure at startup, and print error includes something like "you can set --skip-cache-on-disk-failure-at-startup to restart clickhouse server".


namespace FailPoints
{
extern const char cache_filesystem_failure[];

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.

cache_on_disk_failure is better?

tryLogCurrentException(__PRETTY_FUNCTION__);
return;
}
catch (const std::filesystem::filesystem_error & e)

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.

insert(write) failure is different from failure at startup? we cannot skip it.

node.query("DROP TABLE IF EXISTS t0")

node.query(
"""

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.

Would you please add a test case for startup?

@alexey-milovidov

Copy link
Copy Markdown
Member

Master already has a fix for this exact issue (commit ff7f9ff): it adds the skip_cache_on_disk_failure setting, CACHE_CANNOT_WRITE_TO_CACHE_DISK error code, proper error handling in the read path, and comprehensive tests in test_cache_bypass_on_disk_failure/. The PR's changes are largely superseded.

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 pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants