Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new_metrics): use rwlock instead of mutex to protect a metric entity or registry against few writes and numerous reads #1196

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/rdsn/src/utils/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::set<kth_percentile_type> get_all_kth_percentile_types()
}

metric_entity::metric_entity(const std::string &id, attr_map &&attrs)
: _id(id), _attrs(std::move(attrs))
: _id(id), _lock(), _attrs(std::move(attrs)), _metrics()
{
}

Expand All @@ -49,7 +49,7 @@ metric_entity::~metric_entity()

void metric_entity::close(close_option option)
{
std::lock_guard<std::mutex> guard(_mtx);
utils::auto_write_lock l(_lock);

// The reason why each metric is closed in the entity rather than in the destructor of each
// metric is that close() for the metric will return immediately without waiting for any close
Expand Down Expand Up @@ -80,19 +80,19 @@ void metric_entity::close(close_option option)

metric_entity::attr_map metric_entity::attributes() const
{
std::lock_guard<std::mutex> guard(_mtx);
utils::auto_read_lock l(_lock);
return _attrs;
}

metric_entity::metric_map metric_entity::metrics() const
{
std::lock_guard<std::mutex> guard(_mtx);
utils::auto_read_lock l(_lock);
return _metrics;
}

void metric_entity::set_attributes(attr_map &&attrs)
{
std::lock_guard<std::mutex> guard(_mtx);
utils::auto_write_lock l(_lock);
_attrs = std::move(attrs);
}

Expand All @@ -114,7 +114,7 @@ metric_entity_prototype::metric_entity_prototype(const char *name) : _name(name)

metric_entity_prototype::~metric_entity_prototype() {}

metric_registry::metric_registry()
metric_registry::metric_registry() : _lock(), _entities()
{
// We should ensure that metric_registry is destructed before shared_io_service is destructed.
// Once shared_io_service is destructed before metric_registry is destructed,
Expand All @@ -126,7 +126,7 @@ metric_registry::metric_registry()

metric_registry::~metric_registry()
{
std::lock_guard<std::mutex> guard(_mtx);
utils::auto_write_lock l(_lock);

// Once the registery is chosen to be destructed, all of the entities and metrics owned by it
// will no longer be needed.
Expand All @@ -146,15 +146,14 @@ metric_registry::~metric_registry()

metric_registry::entity_map metric_registry::entities() const
{
std::lock_guard<std::mutex> guard(_mtx);

utils::auto_read_lock l(_lock);
return _entities;
}

metric_entity_ptr metric_registry::find_or_create_entity(const std::string &id,
metric_entity::attr_map &&attrs)
{
std::lock_guard<std::mutex> guard(_mtx);
utils::auto_write_lock l(_lock);

entity_map::const_iterator iter = _entities.find(id);

Expand Down
10 changes: 7 additions & 3 deletions src/rdsn/src/utils/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class metric_entity : public ref_counter
template <typename MetricType, typename... Args>
ref_ptr<MetricType> find_or_create(const metric_prototype *prototype, Args &&... args)
{
std::lock_guard<std::mutex> guard(_mtx);
utils::auto_write_lock l(_lock);

metric_map::const_iterator iter = _metrics.find(prototype);
if (iter != _metrics.end()) {
Expand Down Expand Up @@ -188,7 +188,7 @@ class metric_entity : public ref_counter

const std::string _id;

mutable std::mutex _mtx;
mutable utils::rw_lock_nr _lock;
attr_map _attrs;
metric_map _metrics;

Expand Down Expand Up @@ -231,7 +231,7 @@ class metric_registry : public utils::singleton<metric_registry>

metric_entity_ptr find_or_create_entity(const std::string &id, metric_entity::attr_map &&attrs);

mutable std::mutex _mtx;
mutable utils::rw_lock_nr _lock;
entity_map _entities;

DISALLOW_COPY_AND_ASSIGN(metric_registry);
Expand Down Expand Up @@ -621,6 +621,8 @@ class percentile_timer
std::atomic<state> _state;
utils::notify_event _completed;
std::unique_ptr<boost::asio::deadline_timer> _timer;

DISALLOW_COPY_AND_ASSIGN(percentile_timer);
};

// The percentile is a metric type that samples observations. The size of samples has an upper
Expand Down Expand Up @@ -809,6 +811,8 @@ class percentile : public closeable_metric
NthElementFinder _nth_element_finder;

std::unique_ptr<percentile_timer> _timer;

DISALLOW_COPY_AND_ASSIGN(percentile);
};

template <typename T,
Expand Down