Skip to content

Commit

Permalink
Less contention in the cache, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-milovidov committed Mar 8, 2024
1 parent e66ac57 commit e7f9928
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Common/ProfileEvents.cpp
Expand Up @@ -475,6 +475,7 @@ The server successfully detected this situation and will download merged part fr
M(FileSegmentUseMicroseconds, "File segment use() time") \
M(FileSegmentRemoveMicroseconds, "File segment remove() time") \
M(FileSegmentHolderCompleteMicroseconds, "File segments holder complete() time") \
M(FileSegmentFailToIncreasePriority, "Number of times the priority was not increased due to a high contention on the cache lock") \
M(FilesystemCacheHoldFileSegments, "Filesystem cache file segments count, which were hold") \
M(FilesystemCacheUnusedHoldFileSegments, "Filesystem cache file segments count, which were hold, but not used (because of seek or LIMIT n, etc)") \
\
Expand Down
5 changes: 5 additions & 0 deletions src/Interpreters/Cache/FileCache.cpp
Expand Up @@ -188,6 +188,11 @@ CacheGuard::Lock FileCache::lockCache() const
return cache_guard.lock();
}

CacheGuard::Lock FileCache::tryLockCache() const
{
return cache_guard.tryLock();
}

FileSegments FileCache::getImpl(const LockedKey & locked_key, const FileSegment::Range & range, size_t file_segments_limit) const
{
/// Given range = [left, right] and non-overlapping ordered set of file segments,
Expand Down
1 change: 1 addition & 0 deletions src/Interpreters/Cache/FileCache.h
Expand Up @@ -173,6 +173,7 @@ class FileCache : private boost::noncopyable
void deactivateBackgroundOperations();

CacheGuard::Lock lockCache() const;
CacheGuard::Lock tryLockCache() const;

std::vector<FileSegment::Info> sync();

Expand Down
7 changes: 5 additions & 2 deletions src/Interpreters/Cache/FileSegment.cpp
Expand Up @@ -23,6 +23,7 @@ namespace ProfileEvents
extern const Event FileSegmentWriteMicroseconds;
extern const Event FileSegmentUseMicroseconds;
extern const Event FileSegmentHolderCompleteMicroseconds;
extern const Event FileSegmentFailToIncreasePriority;
extern const Event FilesystemCacheHoldFileSegments;
extern const Event FilesystemCacheUnusedHoldFileSegments;
}
Expand Down Expand Up @@ -965,8 +966,10 @@ void FileSegment::increasePriority()
auto it = getQueueIterator();
if (it)
{
auto cache_lock = cache->lockCache();
hits_count = it->increasePriority(cache_lock);
if (auto cache_lock = cache->tryLockCache())
hits_count = it->increasePriority(cache_lock);
else
ProfileEvents::increment(ProfileEvents::FileSegmentFailToIncreasePriority);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Interpreters/Cache/Guards.h
Expand Up @@ -65,10 +65,12 @@ struct CacheGuard : private boost::noncopyable
/// so, we wouldn't be able to pass CacheGuard::Lock to a function which accepts KeyGuard::Lock, for example
struct Lock : public std::unique_lock<std::mutex>
{
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
using Base = std::unique_lock<std::mutex>;
using Base::Base;
};

Lock lock() { return Lock(mutex); }
Lock tryLock() { return Lock(mutex, std::try_to_lock); }
std::mutex mutex;
};

Expand Down

0 comments on commit e7f9928

Please sign in to comment.