Skip to content

Allow creating a directory on top of virtual plain-rewritable node - #91935

Merged
jkartseva merged 9 commits into
ClickHouse:masterfrom
jkartseva:fix-dir-doesnt-exist
Dec 14, 2025
Merged

jkartseva merged 9 commits into
ClickHouse:masterfrom
jkartseva:fix-dir-doesnt-exist

Conversation

@jkartseva

Copy link
Copy Markdown
Member

Fixes: #89476

2025.12.11 04:57:24.687960 [ 2760586 ] {50842323-7293-495d-afc1-449a8c8c8ceb} <Error> BackupsWorker: Failed to make backup Disk('disk_s3_plain_rewritable_03517', 'backup0'): Code: 49. DB::Exception: Directory 'backup0/metadata' does not exist. (LOGICAL_ERROR), Stack trace (when copying this message, always include the lines below):

0. ./contrib/llvm-project/libcxx/include/__exception/exception.h:113: Poco::Exception::Exception(String const&, int) @ 0x000000001fcd0af2
1. ./build.release/./src/Common/Exception.cpp:131: DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000001163c19e
2. DB::Exception::Exception(String&&, int, String, bool) @ 0x000000000980068e
3. DB::Exception::Exception(PreformattedMessage&&, int) @ 0x00000000098001c0
4. DB::Exception::Exception<String>(int, FormatStringHelperImpl<std::type_identity<String>::type>, String&&) @ 0x00000000097ffe2b
5. ./build.release/./src/Disks/DiskObjectStorage/MetadataStorages/PlainRewritable/MetadataStorageFromPlainRewritableObjectStorage.cpp:556: DB::MetadataStorageFromPlainRewritableObjectStorageTransaction::generateObjectKeyForPath(String const&) const @ 0x0000000016629b38
6. ./build.release/./src/Disks/DiskObjectStorage/DiskObjectStorageTransaction.cpp:900: DB::DiskObjectStorageTransaction::writeFileImpl(bool, String const&, unsigned long, DB::WriteMode, DB::WriteSettings const&) @ 0x00000000165d3ac2
7. ./build.release/./src/Disks/DiskObjectStorage/DiskObjectStorage.cpp:856: DB::DiskObjectStorage::writeFile(String const&, unsigned long, DB::WriteMode, DB::WriteSettings const&) @ 0x00000000165ca2af
8. ./build.release/./src/Backups/BackupIO_Disk.cpp:89: DB::BackupWriterDisk::writeFile(String const&) @ 0x0000000015db710b
9. ./build.release/./src/Backups/BackupIO_Default.cpp:73: DB::BackupWriterDefault::copyDataToFile(String const&, std::function<std::unique_ptr<DB::SeekableReadBuffer, std::default_delete<DB::SeekableReadBuffer>> ()> const&, unsigned long, unsigned long) @ 0x0000000015cfe900
10. ./build.release/./src/Backups/BackupImpl.cpp:1052: DB::BackupImpl::writeFile(DB::BackupFileInfo const&, std::shared_ptr<DB::IBackupEntry const>) @ 0x0000000015d10ba8
11. ./build.release/./src/Backups/BackupsWorker.cpp:780: DB::BackupsWorker::writeBackupEntries(std::shared_ptr<DB::IBackup>, std::vector<std::pair<String, std::shared_ptr<DB::IBackupEntry const>>, std::allocator<std::pair<String, std::shared_ptr<DB::IBackupEntry const>>>>&&, String const&, std::shared_ptr<DB::IBackupCoordination>, bool, std::shared_ptr<DB::QueryStatus>)::$_0::operator()() const @ 0x0000000015ceabe1
12. ./contrib/llvm-project/libcxx/include/__functional/function.h:508: ? @ 0x0000000012fb8548
13. ./src/Common/threadPoolCallbackRunner.h:184: DB::ThreadPoolCallbackRunnerLocal<void, ThreadPoolImpl<ThreadFromGlobalPoolImpl<false, true>>, std::function<void ()>>::operator()(std::function<void ()>&&, Priority, std::optional<unsigned long>)::'lambda'()::operator()() @ 0x0000000012fb8320
14. ./contrib/llvm-project/libcxx/include/__functional/function.h:508: ? @ 0x000000001177ad8c
15. ./contrib/llvm-project/libcxx/include/__type_traits/invoke.h:0: ThreadFromGlobalPoolImpl<false, true>::ThreadFromGlobalPoolImpl<void (ThreadPoolImpl<ThreadFromGlobalPoolImpl<false, true>>::ThreadFromThreadPool::*)(), ThreadPoolImpl<ThreadFromGlobalPoolImpl<false, true>>::ThreadFromThreadPool*>(void (ThreadPoolImpl<ThreadFromGlobalPoolImpl<false, true>>::ThreadFromThreadPool::*&&)(), ThreadPoolImpl<ThreadFromGlobalPoolImpl<false, true>>::ThreadFromThreadPool*&&)::'lambda'()::operator()() @ 0x000000001177fde6
16. ./contrib/llvm-project/libcxx/include/__functional/function.h:508: ? @ 0x0000000011778af5
17. ./contrib/llvm-project/libcxx/include/__type_traits/invoke.h:0: void* std::__thread_proxy[abi:ne210105]<std::tuple<std::unique_ptr<std::__thread_struct, std::default_delete<std::__thread_struct>>, void (ThreadPoolImpl<std::thread>::ThreadFromThreadPool::*)(), ThreadPoolImpl<std::thread>::ThreadFromThreadPool*>>(void*) @ 0x000000001177e1bb
18. start_thread @ 0x0000000000094ac3
19. __clone3 @ 0x00000000001268c0
 (version 25.12.1.1)
20

This happens because plain-rewritable does not allow creating a physical directory (backed by an object in the object storage), if the node in the directory tree already exists and is virtual.

In the example with backup, the following operations are executed:

createDirectories("backup0/metadata/d0/")
writeFile("backup0/metadata/d0.sql")
createDirectories("backup0/metadata/") <== Physical directory is not created
writeFile("backup0/metadata/d0.sql") <== LOGICAL_ERROR

This PR relaxes virtual node checks.

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 Directory '{}' does not exist (LOGICAL_ERROR) when creating backups on plain-rewritable disks.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@jkartseva jkartseva added the can be tested Allows running workflows for external contributors label Dec 11, 2025
@jkartseva
jkartseva requested a review from Michicosun December 11, 2025 08:36
@clickhouse-gh

clickhouse-gh Bot commented Dec 11, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2e300d3]

Summary: ❌

job_name test_name status info comment
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting in Function_not: the query: FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Dec 11, 2025
@jkartseva
jkartseva marked this pull request as draft December 11, 2025 09:21
@Michicosun Michicosun self-assigned this Dec 11, 2025
@Michicosun

Michicosun commented Dec 11, 2025 •

Copy link
Copy Markdown
Member

Also, maybe a better approach will be to materialize the virtual directory during file creation, because I am not sure that the parent directory after a child recursive create will always be explicitly created before the file creation.

like this sequence

tx->createDirectoryRecursive('/a/b/c');
tx->createMetadataFile('/a/b/file');

wdyt?

@jkartseva
jkartseva force-pushed the fix-dir-doesnt-exist branch 4 times, most recently from 29e88e2 to 2da5f8e Compare December 12, 2025 02:01
@jkartseva

Copy link
Copy Markdown
Member Author

Also, maybe a better approach will be to materialize the virtual directory during file creation, because I am not sure that the parent directory after a child recursive create will always be explicitly created before the file creation.

I added directory materialization before generateObjectKeyForPathis called, because it expects paths in object storage.
I also added an existsDirectory check to restrict it to virtual nodes, but the code looks a bit weird:

  if (metadata_storage.existsDirectory(parent_path))
      createDirectories(parent_path); 

It probably makes sense to remove the existsDirectory check and create physical directories unconditionally; I can’t think of any caveats.

@jkartseva

Copy link
Copy Markdown
Member Author

DEBUG_OR_SANITIZER_BUILD macro is because sanitizer builds abort on LOGICAL_ERROR

@jkartseva
jkartseva marked this pull request as ready for review December 12, 2025 03:25
@jkartseva
jkartseva force-pushed the fix-dir-doesnt-exist branch from 2da5f8e to 5256a01 Compare December 12, 2025 21:39

@Michicosun Michicosun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good

@jkartseva
jkartseva enabled auto-merge December 13, 2025 06:41
@jkartseva
jkartseva added this pull request to the merge queue Dec 14, 2025
Merged via the queue into ClickHouse:master with commit d9df8ed Dec 14, 2025
@jkartseva
jkartseva deleted the fix-dir-doesnt-exist branch December 14, 2025 06:30
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 14, 2025
valerypetrov pushed a commit to valerypetrov/ClickHouse that referenced this pull request Nov 22, 2026
… or linked into it

On a `plain_rewritable` disk `createDirectoryRecursive` writes a `prefix.path`
only for the leaf, so its ancestors exist only as virtual nodes. Writing a file
into such a directory already materializes it (ClickHouse#91935, in
`generateObjectKeyForPath`), but `moveFile`/`replaceFile` and `createHardLink`
did not, and their commit threw `LOGICAL_ERROR: Directory '...' is virtual`.
That is hit by the first INSERT into every `Set`/`Join` table on such a disk
(`StorageSetOrJoinBase::restore` creates `<path>/tmp`, and the INSERT then
`replaceFile`s `tmp/<n>.bin` into `<path>`), by a `Join` mutation, and by
`clickhouse-disks move`/`link`.

The new `materializeVirtualDirectory` is called for the target directory of a
move and of a hard link. It plans a `CreateDirectoryOperation` for it in a new
materialize-only mode and records the directory in the uncommitted state, so
the move or link plans against a real, empty, implicit directory. Like the
write path, it registers `useMissingDirectory`, so a commit fails with
`DIRECTORY_ALREADY_EXISTS` when another transaction materialized or replaced
the directory in between (the move, the real hard link and the
hard-links-disabled copy were all planned for the implicit form). The
materialize-only operation throws `DIRECTORY_DOESNT_EXIST` when the directory
was removed in between, instead of recreating it. A target directory that does
not exist at all still fails as before.

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

can be tested Allows running workflows for external contributors 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.

Backup Logical error: 'Creation of a file under the virtual directory is not possible'

3 participants