Fix stress tests

This commit is contained in:
kssenii 2023-02-01 14:28:47 +01:00
parent ad34ff24dc
commit 6320a1c4e1
3 changed files with 42 additions and 24 deletions

View File

@ -469,7 +469,7 @@ CachedOnDiskReadBufferFromFile::getImplementationBuffer(FileSegment & file_segme
bool CachedOnDiskReadBufferFromFile::completeFileSegmentAndGetNext() bool CachedOnDiskReadBufferFromFile::completeFileSegmentAndGetNext()
{ {
auto * current_file_segment = &file_segments->front(); auto * current_file_segment = &file_segments->front();
const auto & completed_range = current_file_segment->range(); auto completed_range = current_file_segment->range();
if (enable_logging) if (enable_logging)
appendFilesystemCacheLog(completed_range, read_type); appendFilesystemCacheLog(completed_range, read_type);

View File

@ -371,25 +371,36 @@ KeyTransactionPtr FileCache::createKeyTransaction(const Key & key, KeyNotFoundPo
{ {
auto lock = metadata_guard.lock(); auto lock = metadata_guard.lock();
cleanup_keys_metadata_queue->clear([this](const Key & cleanup_key) // cleanup_keys_metadata_queue->clear([this](const Key & cleanup_key)
{ // {
[[maybe_unused]] const bool erased = metadata.erase(cleanup_key); // auto it = metadata.find(cleanup_key);
chassert(erased); // if (it == metadata.end())
// throw Exception(ErrorCodes::LOGICAL_ERROR, "No such key {} in metadata", cleanup_key);
try // auto guard = it->second->guard;
{ // auto key_lock = guard->lock();
const fs::path prefix_path = fs::path(getPathInLocalCache(cleanup_key)).parent_path();
if (fs::exists(prefix_path) && fs::is_empty(prefix_path)) // [[maybe_unused]] const bool erased = metadata.erase(it);
fs::remove_all(prefix_path); // chassert(erased);
}
catch (...) // try
{ // {
tryLogCurrentException(__PRETTY_FUNCTION__); // const fs::path prefix_path = fs::path(getPathInLocalCache(cleanup_key)).parent_path();
} // if (fs::exists(prefix_path) && fs::is_empty(prefix_path))
}); // fs::remove_all(prefix_path);
// }
// catch (...)
// {
// tryLogCurrentException(__PRETTY_FUNCTION__);
// }
// });
auto it = metadata.find(key); auto it = metadata.find(key);
if (it == metadata.end()) bool erased = false;
if (it != metadata.end() && it->second->removed)
erased = metadata.erase(it);
if (erased || it == metadata.end())
{ {
switch (key_not_found_policy) switch (key_not_found_policy)
{ {
@ -918,9 +929,11 @@ void KeyTransaction::cleanupKeyDirectory() const
return; return;
/// Someone might still need this directory. /// Someone might still need this directory.
if (!offsets.empty()) if (!key_metadata.empty())
return; return;
key_metadata.removed = true;
/// Now `offsets` empty and the key lock is still locked. /// Now `offsets` empty and the key lock is still locked.
/// So it is guaranteed that no one will add something. /// So it is guaranteed that no one will add something.

View File

@ -167,6 +167,8 @@ public:
KeyGuardPtr guard = std::make_shared<KeyGuard>(); KeyGuardPtr guard = std::make_shared<KeyGuard>();
bool created_base_directory = false; bool created_base_directory = false;
bool removed = false;
}; };
using KeyMetadataPtr = std::shared_ptr<KeyMetadata>; using KeyMetadataPtr = std::shared_ptr<KeyMetadata>;
@ -384,15 +386,18 @@ struct KeyTransactionCreator
{ {
KeyTransactionCreator( KeyTransactionCreator(
const FileCacheKey & key_, const FileCacheKey & key_,
FileCache::KeyMetadata & offsets_, FileCache::KeyMetadata & key_metadata_,
KeysQueuePtr cleanup_keys_metadata_queue_, KeysQueuePtr cleanup_keys_metadata_queue_,
const FileCache * cache_) const FileCache * cache_)
: key(key_), offsets(offsets_), cleanup_keys_metadata_queue(cleanup_keys_metadata_queue_), cache(cache_) {} : key(key_)
, key_metadata(key_metadata_)
, cleanup_keys_metadata_queue(cleanup_keys_metadata_queue_)
, cache(cache_) {}
KeyTransactionPtr create(); KeyTransactionPtr create();
FileCacheKey key; FileCacheKey key;
FileCache::KeyMetadata & offsets; FileCache::KeyMetadata & key_metadata;
KeysQueuePtr cleanup_keys_metadata_queue; KeysQueuePtr cleanup_keys_metadata_queue;
const FileCache * cache; const FileCache * cache;
}; };
@ -420,8 +425,8 @@ struct KeyTransaction : private boost::noncopyable
bool isLastHolder(size_t offset); bool isLastHolder(size_t offset);
FileCache::KeyMetadata & getOffsets() { return offsets; } FileCache::KeyMetadata & getKeyMetadata() { return key_metadata; }
const FileCache::KeyMetadata & getOffsets() const { return offsets; } const FileCache::KeyMetadata & getKeyMetadata() const { return key_metadata; }
std::vector<size_t> delete_offsets; std::vector<size_t> delete_offsets;
@ -433,7 +438,7 @@ private:
const FileCache * cache; const FileCache * cache;
KeyGuard::Lock lock; KeyGuard::Lock lock;
FileCache::KeyMetadata & offsets; FileCache::KeyMetadata & key_metadata;
KeysQueuePtr cleanup_keys_metadata_queue; KeysQueuePtr cleanup_keys_metadata_queue;
Poco::Logger * log; Poco::Logger * log;