This is an automated email from the ASF dual-hosted git repository.

wangdan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new f9c85bf9e feat(new_metrics): use rwlock instead of mutex to protect 
read/write of metric an entity or registry for lots of queries over them in the 
future (#1196)
f9c85bf9e is described below

commit f9c85bf9eb7d2079e540844c179ab59c538e07d2
Author: Dan Wang <[email protected]>
AuthorDate: Fri Oct 21 18:05:43 2022 +0800

    feat(new_metrics): use rwlock instead of mutex to protect read/write of 
metric an entity or registry for lots of queries over them in the future (#1196)
---
 src/rdsn/src/utils/metrics.cpp | 19 +++++++++----------
 src/rdsn/src/utils/metrics.h   | 10 +++++++---
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/src/rdsn/src/utils/metrics.cpp b/src/rdsn/src/utils/metrics.cpp
index a45462808..00efd008f 100644
--- a/src/rdsn/src/utils/metrics.cpp
+++ b/src/rdsn/src/utils/metrics.cpp
@@ -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()
 {
 }
 
@@ -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
@@ -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);
 }
 
@@ -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,
@@ -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.
@@ -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);
 
diff --git a/src/rdsn/src/utils/metrics.h b/src/rdsn/src/utils/metrics.h
index ae92eafb6..108a06860 100644
--- a/src/rdsn/src/utils/metrics.h
+++ b/src/rdsn/src/utils/metrics.h
@@ -150,7 +150,7 @@ public:
     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()) {
@@ -188,7 +188,7 @@ private:
 
     const std::string _id;
 
-    mutable std::mutex _mtx;
+    mutable utils::rw_lock_nr _lock;
     attr_map _attrs;
     metric_map _metrics;
 
@@ -231,7 +231,7 @@ private:
 
     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);
@@ -623,6 +623,8 @@ private:
     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
@@ -811,6 +813,8 @@ private:
     NthElementFinder _nth_element_finder;
 
     std::unique_ptr<percentile_timer> _timer;
+
+    DISALLOW_COPY_AND_ASSIGN(percentile);
 };
 
 template <typename T,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to