Add some checks

This commit is contained in:
kssenii 2023-06-28 15:26:17 +02:00
parent c7ae150ffb
commit 15f64a7cb9
5 changed files with 23 additions and 9 deletions

View File

@ -872,6 +872,7 @@ void FileSegment::setDetachedState(const FileSegmentGuard::Lock & lock)
setDownloadState(State::DETACHED, lock);
key_metadata.reset();
cache = nullptr;
queue_iterator = nullptr;
}
void FileSegment::detach(const FileSegmentGuard::Lock & lock, const LockedKey &)
@ -890,7 +891,7 @@ void FileSegment::use()
if (!cache)
{
chassert(isCompleted(true));
chassert(isDetached());
return;
}

View File

@ -44,7 +44,7 @@ public:
virtual size_t use(const CacheGuard::Lock &) = 0;
virtual std::shared_ptr<IIterator> remove(const CacheGuard::Lock &) = 0;
virtual void remove(const CacheGuard::Lock &) = 0;
virtual const Entry & getEntry() const = 0;

View File

@ -166,15 +166,17 @@ void LRUFileCachePriority::iterate(IterateFunc && func, const CacheGuard::Lock &
}
}
LRUFileCachePriority::Iterator
LRUFileCachePriority::LRUFileCacheIterator::remove(const CacheGuard::Lock &)
void LRUFileCachePriority::LRUFileCacheIterator::remove(const CacheGuard::Lock &)
{
return std::make_shared<LRUFileCacheIterator>(
cache_priority, cache_priority->remove(queue_iter));
checkUsable();
cache_priority->remove(queue_iter);
queue_iter = LRUQueueIterator{};
}
void LRUFileCachePriority::LRUFileCacheIterator::invalidate()
{
checkUsable();
LOG_TEST(
cache_priority->log,
"Invalidating entry in LRU queue. Key: {}, offset: {}, previous size: {}",
@ -187,6 +189,8 @@ void LRUFileCachePriority::LRUFileCacheIterator::invalidate()
void LRUFileCachePriority::LRUFileCacheIterator::updateSize(int64_t size)
{
checkUsable();
LOG_TEST(
cache_priority->log,
"Update size with {} in LRU queue for key: {}, offset: {}, previous size: {}",
@ -198,8 +202,15 @@ void LRUFileCachePriority::LRUFileCacheIterator::updateSize(int64_t size)
size_t LRUFileCachePriority::LRUFileCacheIterator::use(const CacheGuard::Lock &)
{
checkUsable();
cache_priority->queue.splice(cache_priority->queue.end(), cache_priority->queue, queue_iter);
return ++queue_iter->hits;
}
void LRUFileCachePriority::LRUFileCacheIterator::checkUsable() const
{
if (queue_iter == LRUQueueIterator{})
throw Exception(ErrorCodes::LOGICAL_ERROR, "Attempt to use invalid iterator");
}
}

View File

@ -60,13 +60,15 @@ public:
size_t use(const CacheGuard::Lock &) override;
Iterator remove(const CacheGuard::Lock &) override;
void remove(const CacheGuard::Lock &) override;
void invalidate() override;
void updateSize(int64_t size) override;
private:
void checkUsable() const;
LRUFileCachePriority * cache_priority;
mutable LRUFileCachePriority::LRUQueueIterator queue_iter;
};

View File

@ -147,7 +147,6 @@ String CacheMetadata::getFileNameForFileSegment(size_t offset, FileSegmentKind s
file_suffix = "_temporary";
break;
case FileSegmentKind::Regular:
file_suffix = "";
break;
}
return std::to_string(offset) + file_suffix;
@ -398,6 +397,8 @@ KeyMetadata::iterator LockedKey::removeFileSegment(size_t offset, const FileSegm
if (file_segment->queue_iterator)
file_segment->queue_iterator->invalidate();
file_segment->detach(segment_lock, *this);
const auto path = key_metadata->getFileSegmentPath(*file_segment);
bool exists = fs::exists(path);
if (exists)
@ -408,7 +409,6 @@ KeyMetadata::iterator LockedKey::removeFileSegment(size_t offset, const FileSegm
else if (file_segment->downloaded_size)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected path {} to exist", path);
file_segment->detach(segment_lock, *this);
return key_metadata->erase(it);
}