From 829854113b34075676fc224c0db2df69a429ea6b Mon Sep 17 00:00:00 2001 From: alesapin Date: Wed, 20 Apr 2022 15:10:36 +0200 Subject: [PATCH] Add a little thread safety --- src/Disks/IDiskRemote.cpp | 21 +++++++++++++++++---- src/Disks/IDiskRemote.h | 3 +++ src/Storages/StorageReplicatedMergeTree.cpp | 2 -- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Disks/IDiskRemote.cpp b/src/Disks/IDiskRemote.cpp index acc3517d30e..80f1a3ff116 100644 --- a/src/Disks/IDiskRemote.cpp +++ b/src/Disks/IDiskRemote.cpp @@ -45,7 +45,6 @@ IDiskRemote::Metadata IDiskRemote::Metadata::createAndStoreMetadata(const String return result; } - IDiskRemote::Metadata IDiskRemote::Metadata::readUpdateAndStoreMetadata(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync, IDiskRemote::MetadataUpdater updater) { Metadata result(remote_fs_root_path_, metadata_disk_, metadata_file_path_); @@ -55,7 +54,6 @@ IDiskRemote::Metadata IDiskRemote::Metadata::readUpdateAndStoreMetadata(const St return result; } - IDiskRemote::Metadata IDiskRemote::Metadata::createUpdateAndStoreMetadata(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync, IDiskRemote::MetadataUpdater updater) { Metadata result(remote_fs_root_path_, metadata_disk_, metadata_file_path_); @@ -64,6 +62,16 @@ IDiskRemote::Metadata IDiskRemote::Metadata::createUpdateAndStoreMetadata(const return result; } +IDiskRemote::Metadata IDiskRemote::Metadata::readUpdateStoreMetadataAndRemove(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync, IDiskRemote::MetadataUpdater updater) +{ + Metadata result(remote_fs_root_path_, metadata_disk_, metadata_file_path_); + if (updater(result)) + result.save(sync); + metadata_disk_->removeFile(metadata_file_path_); + + return result; + +} IDiskRemote::Metadata IDiskRemote::Metadata::createAndStoreMetadataIfNotExists(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync, bool overwrite) { @@ -231,6 +239,12 @@ IDiskRemote::Metadata IDiskRemote::readUpdateAndStoreMetadata(const String & pat } +IDiskRemote::Metadata IDiskRemote::readUpdateStoreMetadataAndRemove(const String & path, bool sync, IDiskRemote::MetadataUpdater updater) +{ + std::unique_lock lock(metadata_mutex); + return Metadata::readUpdateStoreMetadataAndRemove(remote_fs_root_path, metadata_disk, path, sync, updater); +} + IDiskRemote::Metadata IDiskRemote::readOrCreateUpdateAndStoreMetadata(const String & path, WriteMode mode, bool sync, IDiskRemote::MetadataUpdater updater) { if (mode == WriteMode::Rewrite || !metadata_disk->exists(path)) @@ -308,8 +322,7 @@ void IDiskRemote::removeMetadata(const String & path, std::vector & path return true; }; - readUpdateAndStoreMetadata(path, false, metadata_updater); - metadata_disk->removeFile(path); + readUpdateStoreMetadataAndRemove(path, false, metadata_updater); /// If there is no references - delete content from remote FS. } catch (const Exception & e) diff --git a/src/Disks/IDiskRemote.h b/src/Disks/IDiskRemote.h index b2251e1abe3..65bcdf3e719 100644 --- a/src/Disks/IDiskRemote.h +++ b/src/Disks/IDiskRemote.h @@ -78,6 +78,8 @@ public: Metadata readMetadata(const String & path) const; Metadata readMetadataUnlocked(const String & path, std::shared_lock &) const; Metadata readUpdateAndStoreMetadata(const String & path, bool sync, MetadataUpdater updater); + Metadata readUpdateStoreMetadataAndRemove(const String & path, bool sync, MetadataUpdater updater); + Metadata readOrCreateUpdateAndStoreMetadata(const String & path, WriteMode mode, bool sync, MetadataUpdater updater); Metadata createAndStoreMetadata(const String & path, bool sync); @@ -229,6 +231,7 @@ struct IDiskRemote::Metadata static Metadata readMetadata(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_); static Metadata readUpdateAndStoreMetadata(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync, Updater updater); + static Metadata readUpdateStoreMetadataAndRemove(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync, Updater updater); static Metadata createAndStoreMetadata(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync); static Metadata createUpdateAndStoreMetadata(const String & remote_fs_root_path_, DiskPtr metadata_disk_, const String & metadata_file_path_, bool sync, Updater updater); diff --git a/src/Storages/StorageReplicatedMergeTree.cpp b/src/Storages/StorageReplicatedMergeTree.cpp index a776347c5f4..858841a4986 100644 --- a/src/Storages/StorageReplicatedMergeTree.cpp +++ b/src/Storages/StorageReplicatedMergeTree.cpp @@ -7461,9 +7461,7 @@ std::pair StorageReplicatedMergeTree::unlockSharedDataByID( files_not_to_remove.clear(); if (!files_not_to_remove_str.empty()) - { boost::split(files_not_to_remove, files_not_to_remove_str, boost::is_any_of("\n ")); - } String zookeeper_part_uniq_node = fs::path(zc_zookeeper_path) / part_id;