2023-01-04 17:56:45 +00:00
|
|
|
#pragma once
|
|
|
|
#include <mutex>
|
|
|
|
#include <Interpreters/Cache/FileCache_fwd.h>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2023-01-05 21:39:17 +00:00
|
|
|
/**
|
2023-03-03 16:20:27 +00:00
|
|
|
* FileCache::get/getOrSet/set
|
2023-03-05 12:17:17 +00:00
|
|
|
* 1. CacheMetadataGuard::Lock (take key lock and release metadata lock)
|
2023-03-03 16:20:27 +00:00
|
|
|
* 2. KeyGuard::Lock (hold till the end of the method)
|
|
|
|
*
|
|
|
|
* FileCache::tryReserve
|
|
|
|
* 1. CacheGuard::Lock
|
|
|
|
* 2. KeyGuard::Lock (taken without metadata lock)
|
|
|
|
* 3. any number of KeyGuard::Lock's for files which are going to be evicted (taken via metadata lock)
|
|
|
|
*
|
|
|
|
* FileCache::removeIfExists
|
|
|
|
* 1. CacheGuard::Lock
|
|
|
|
* 2. KeyGuard::Lock (taken via metadata lock)
|
|
|
|
* 3. FileSegmentGuard::Lock
|
|
|
|
*
|
|
|
|
* FileCache::removeAllReleasable
|
|
|
|
* 1. CacheGuard::Lock
|
|
|
|
* 2. any number of KeyGuard::Lock's locks (takken via metadata lock), but at a moment of time only one key lock can be hold
|
|
|
|
* 3. FileSegmentGuard::Lock
|
|
|
|
*
|
|
|
|
* FileCache::getSnapshot (for all cache)
|
|
|
|
* 1. metadata lock
|
|
|
|
* 2. any number of KeyGuard::Lock's locks (takken via metadata lock), but at a moment of time only one key lock can be hold
|
|
|
|
* 3. FileSegmentGuard::Lock
|
|
|
|
*
|
|
|
|
* FileCache::getSnapshot(key)
|
|
|
|
* 1. KeyGuard::Lock (taken via metadata lock)
|
|
|
|
* 2. FileSegmentGuard::Lock
|
|
|
|
*
|
|
|
|
* FileSegment::complete
|
|
|
|
* 1. CacheGuard::Lock
|
|
|
|
* 2. KeyGuard::Lock (taken without metadata lock)
|
|
|
|
* 3. FileSegmentGuard::Lock
|
|
|
|
*
|
|
|
|
* Rules:
|
|
|
|
* 1. Priority of locking: CacheGuard::Lock > CacheMetadataGuard::Lock > KeyGuard::Lock > FileSegmentGuard::Lock
|
|
|
|
* 2. If we take more than one key lock at a moment of time, we need to take CacheGuard::Lock (example: tryReserve())
|
2023-01-05 21:39:17 +00:00
|
|
|
*/
|
2023-01-04 17:56:45 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-27 12:48:46 +00:00
|
|
|
* Cache priority queue guard.
|
2023-01-05 21:39:17 +00:00
|
|
|
*/
|
2023-01-27 12:48:46 +00:00
|
|
|
struct CacheGuard
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-04 17:56:45 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
2023-01-20 15:30:25 +00:00
|
|
|
/**
|
|
|
|
* Guard for cache metadata.
|
|
|
|
*/
|
|
|
|
struct CacheMetadataGuard
|
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-20 15:30:25 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-20 15:30:25 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-20 15:30:25 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
2023-01-05 21:39:17 +00:00
|
|
|
/**
|
2023-01-27 12:48:46 +00:00
|
|
|
* Guard for a set of keys.
|
|
|
|
* One guard per key prefix (first three digits of the path hash).
|
2023-01-05 21:39:17 +00:00
|
|
|
*/
|
2023-01-27 12:48:46 +00:00
|
|
|
struct KeyGuard
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-04 17:56:45 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
2023-01-27 12:48:46 +00:00
|
|
|
using KeyGuardPtr = std::shared_ptr<KeyGuard>;
|
2023-01-04 17:56:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Guard for a file segment.
|
|
|
|
*/
|
|
|
|
struct FileSegmentGuard
|
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-04 17:56:45 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|