Skip to content

Add background cleanup of store/ subdirs - #38265

Merged
tavplubix merged 11 commits into
masterfrom
cleanup_garbage_in_store_dir
Jun 27, 2022
Merged

tavplubix merged 11 commits into
masterfrom
cleanup_garbage_in_store_dir

Conversation

@tavplubix

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Added background cleanup of subdirectories in store/. In some cases clickhouse-server might left garbage subdirectories in store/ (for example, on unsuccessful table creation) and those dirs were never been removed. Fixes #33710

@robot-clickhouse robot-clickhouse added the pr-improvement Pull request with some product improvements label Jun 21, 2022
Comment thread src/Interpreters/DatabaseCatalog.h Outdated
bool hasUUIDMapping(const UUID & uuid);

void addProtectedUUIDDir(const UUID & uuid);
void removeProtectedUUIDDir(const UUID & uuid);

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.

Make removeProtectedUUIDDir() (and the other new methods) noexcept? Otherwise we an exception may leak out of the UUIdDirectoryProtector destructor.

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.

Actually it was ugly ad-hoc trick, I've removed addProtectedUUIDDir and removeProtectedUUIDDir methods and added more assertions (so it looks less bad now).

As for noexcept, it's really helpful sometimes (for example, when you would prefer to get SIGABRT instead of inconsistent state of data structures in memory due to some stupid exception like bad_alloc), but in most cases it does not make sense. I don't think noexcept is needed for method that calls only std::unordered_set::erase. Also it was called only from dtor that is noexcept be default.

/// And remove only if nobody noticed it after, for example, one month.

struct stat st;
if (stat(unused_dir.string().c_str(), &st))

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.

Guess below calls on the st struct could be replaced (in part) by something portable in std::filesystem?

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.

Probably it could, but these stat/utime/chmod calls look more clear and straightforward to me. We use POSIX API a lot in different places and I think it's ok to use it here.


if (!expected_prefix_dir)
{
LOG_WARNING(log, "Found invalid directory {}, will try to remove it", prefix_dir.path().string());

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.

"invalid" --> "stale"?

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.

In this case directory is rather invalid than stale, because if has unexpected name. Such directory may appear in two cases: due to a bug in ClickHouse or due to manual intervention.

@@ -0,0 +1,11 @@
<clickhouse>

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.

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.

Yes, I will add new config params to docs

@tavplubix

Copy link
Copy Markdown
Member Author

Integration tests:
test_postgresql_replica_database_engine_2 - these tests are disabled in all builds except release one, because MaterializedPostgreSQL does weird things with UUIDs and it triggers logical errors: #36898. One more logical error now. I will try to add some workaround or maybe will completely disable these tests. cc: @kssenii
test_hive_query - was broken in master

Stateless tests:
02305_schema_inference_with_globs - was broken in master

@alesapin

Copy link
Copy Markdown
Member

@Mergifyio update

@mergify

mergify Bot commented Jun 26, 2022

Copy link
Copy Markdown
Contributor

update

✅ Branch has been successfully updated

@tavplubix
tavplubix merged commit bec921c into master Jun 27, 2022
@tavplubix
tavplubix deleted the cleanup_garbage_in_store_dir branch June 27, 2022 13:53

void DatabaseCatalog::cleanupStoreDirectoryTask()
{
fs::path store_path = fs::path(getContext()->getPath()) / "store";

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.

@tavplubix But this will not cleanup store folder on other disks except default (path directive from config), right?

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.

Yes, I will add cleanup for other disks

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.

groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 22, 2026
`DatabaseCatalogCleanupStoreDirectoryTask` (added in
ClickHouse#38265) walked `store/` on every
disk, revoked all access rights from the directories it considered unused
(`chmod 0`), and removed them recursively after
`database_catalog_unused_dir_rm_timeout_sec` (30 days by default).

Its notion of "unused" was "there is no UUID mapping for this directory", which
is not a reliable sign of garbage:

- With `async_load_databases` the task can run while a table is still being
  attached, so it hides the data directory of a live, attached table, and
  every query to the table fails with `Permission denied`
  (ClickHouse#133462).
- After a power loss that loses a `.sql` metadata file, the task hides and then
  deletes data that could be recovered with `ATTACH TABLE ... UUID`
  (ClickHouse#111837).

Leaving garbage in `store/` is better than the complex and error-prone logic of
removing it, so the task is removed, as proposed in
ClickHouse#133462 (comment)

The server settings `database_catalog_unused_dir_hide_timeout_sec`,
`database_catalog_unused_dir_rm_timeout_sec`, and
`database_catalog_unused_dir_cleanup_period_sec` are kept as obsolete settings
without effect, because the server refuses to start with unknown keys in the
configuration, and existing configurations (including the workaround from the
issue) contain them.

The "locking" of UUIDs in `DatabaseCatalog` (`addUUIDMapping`,
`TemporaryLockForUUIDDirectory`) stays: it also protects from two tables or
databases using the same UUID and so the same directory.

The test `test_store_cleanup` is removed together with the task. The test
`01601_detach_permanently` no longer has to tolerate `STD_EXCEPTION`, which was
caused by the task revoking access rights from a table directory.

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

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Garbage on FS after replicated merge tree table creation with invalid zk path

5 participants