Add current metrics

This commit is contained in:
kssenii 2022-05-03 19:17:54 +02:00
parent e03bcc93ca
commit ab65e163fa
5 changed files with 53 additions and 6 deletions

View File

@ -89,6 +89,9 @@
M(KafkaConsumersInUse, "Number of consumers which are currently used by direct or background reads") \
M(KafkaWrites, "Number of currently running inserts to Kafka") \
M(KafkaAssignedPartitions, "Number of partitions Kafka tables currently assigned to") \
M(FilesystemCacheReadBuffers, "Number of active cache buffers") \
M(CacheFileSegments, "Number of existing cache file segments") \
M(CacheDetachedFileSegments, "Number of existing detached cache file segments") \
namespace CurrentMetrics
{

View File

@ -284,7 +284,10 @@ void LRUFileCache::fillHolesWithEmptyFileSegments(
if (fill_with_detached_file_segments)
{
auto file_segment = std::make_shared<FileSegment>(current_pos, hole_size, key, this, FileSegment::State::EMPTY);
file_segment->detached = true;
{
std::lock_guard segment_lock(file_segment->mutex);
file_segment->markAsDetached(segment_lock);
}
file_segments.insert(it, file_segment);
}
else
@ -308,7 +311,10 @@ void LRUFileCache::fillHolesWithEmptyFileSegments(
if (fill_with_detached_file_segments)
{
auto file_segment = std::make_shared<FileSegment>(current_pos, hole_size, key, this, FileSegment::State::EMPTY);
file_segment->detached = true;
{
std::lock_guard segment_lock(file_segment->mutex);
file_segment->markAsDetached(segment_lock);
}
file_segments.insert(file_segments.end(), file_segment);
}
else
@ -364,7 +370,10 @@ FileSegmentsHolder LRUFileCache::get(const Key & key, size_t offset, size_t size
if (file_segments.empty())
{
auto file_segment = std::make_shared<FileSegment>(offset, size, key, this, FileSegment::State::EMPTY);
file_segment->detached = true;
{
std::lock_guard segment_lock(file_segment->mutex);
file_segment->markAsDetached(segment_lock);
}
file_segments = { file_segment };
}
else

View File

@ -6,6 +6,11 @@
#include <IO/Operators.h>
#include <filesystem>
namespace CurrentMetrics
{
extern const Metric CacheDetachedFileSegments;
}
namespace DB
{
@ -570,7 +575,7 @@ void FileSegment::completeImpl(std::lock_guard<std::mutex> & cache_lock, std::lo
cache->reduceSizeToDownloaded(key(), offset(), cache_lock, segment_lock);
}
detached = true;
markAsDetached(segment_lock);
if (cache_writer)
{
@ -685,7 +690,7 @@ void FileSegment::detach(std::lock_guard<std::mutex> & cache_lock, std::lock_gua
if (detached)
return;
detached = true;
markAsDetached(segment_lock);
if (!hasFinalizedState())
{
@ -693,6 +698,19 @@ void FileSegment::detach(std::lock_guard<std::mutex> & cache_lock, std::lock_gua
}
}
void FileSegment::markAsDetached(std::lock_guard<std::mutex> & /* segment_lock */)
{
detached = true;
CurrentMetrics::add(CurrentMetrics::CacheDetachedFileSegments);
}
FileSegment::~FileSegment()
{
std::lock_guard segment_lock(mutex);
if (detached)
CurrentMetrics::sub(CurrentMetrics::CacheDetachedFileSegments);
}
FileSegmentsHolder::~FileSegmentsHolder()
{
/// In CacheableReadBufferFromRemoteFS file segment's downloader removes file segments from

View File

@ -8,6 +8,11 @@
namespace Poco { class Logger; }
namespace CurrentMetrics
{
extern const Metric CacheFileSegments;
}
namespace DB
{
@ -66,6 +71,8 @@ public:
size_t offset_, size_t size_, const Key & key_,
IFileCache * cache_, State download_state_);
~FileSegment();
State state() const;
static String stateToString(FileSegment::State state);
@ -156,7 +163,7 @@ private:
void assertDetachedStatus(std::lock_guard<std::mutex> & segment_lock) const;
bool hasFinalizedState() const;
bool isDetached(std::lock_guard<std::mutex> & /* segment_lock */) const { return detached; }
void markAsDetached(std::lock_guard<std::mutex> & segment_lock);
void setDownloaded(std::lock_guard<std::mutex> & segment_lock);
void setDownloadFailed(std::lock_guard<std::mutex> & segment_lock);
@ -215,6 +222,8 @@ private:
std::atomic<bool> is_downloaded{false};
std::atomic<size_t> hits_count = 0; /// cache hits.
std::atomic<size_t> ref_count = 0; /// Used for getting snapshot state
CurrentMetrics::Increment metric_increment{CurrentMetrics::CacheFileSegments};
};
struct FileSegmentsHolder : private boost::noncopyable

View File

@ -7,6 +7,12 @@
#include <Common/logger_useful.h>
#include <Interpreters/FilesystemCacheLog.h>
namespace CurrentMetrics
{
extern const Metric FilesystemCacheReadBuffers;
}
namespace DB
{
@ -110,6 +116,8 @@ private:
String query_id;
bool enable_logging = false;
CurrentMetrics::Increment metric_increment{CurrentMetrics::FilesystemCacheReadBuffers};
};
}