You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by la...@apache.org on 2020/08/15 14:59:32 UTC

[kudu] 04/23: [metrics] fix MeanGauge::snapshot()

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

laiyingchun pushed a commit to tag kudu-1.12.0-mdh1.0.0-4c2c075-centos-release
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit b65012f50d55658180d8c7509a340292232399ec
Author: zhangyifan27 <ch...@163.com>
AuthorDate: Thu May 14 19:15:20 2020 +0800

    [metrics] fix MeanGauge::snapshot()
    
    MeanGauge::snapshot() will always return a metric whose
    total_sum/total_count is ‘0' because there is a bug in the method,
    that leads to wrong values when MeanGauge metrics merged.
    
    Change-Id: I15f5e0705759a4bdd513089885aaaea4f5332ce1
    Reviewed-on: http://gerrit.cloudera.org:8080/15916
    Reviewed-by: Andrew Wong <aw...@cloudera.com>
    Tested-by: Andrew Wong <aw...@cloudera.com>
---
 src/kudu/util/metrics-test.cc | 12 ++++++++++++
 src/kudu/util/metrics.cc      |  1 +
 2 files changed, 13 insertions(+)

diff --git a/src/kudu/util/metrics-test.cc b/src/kudu/util/metrics-test.cc
index 136ba72..5664a4f 100644
--- a/src/kudu/util/metrics-test.cc
+++ b/src/kudu/util/metrics-test.cc
@@ -186,6 +186,18 @@ TEST_F(MetricsTest, SimpleMeanGaugeTest) {
   ASSERT_EQ(2.5, average_usage->value());
 }
 
+TEST_F(MetricsTest, SimpleMeanGaugeSnapshotTest) {
+  scoped_refptr<MeanGauge> average_usage =
+    METRIC_test_mean_gauge.InstantiateMeanGauge(entity_);
+  scoped_refptr<MeanGauge> old_metric =
+    down_cast<MeanGauge*>(average_usage->snapshot().get());
+  ASSERT_EQ(0, old_metric->value());
+  average_usage->set_value(10.0, 2.0);
+  scoped_refptr<MeanGauge> new_metric =
+    down_cast<MeanGauge*>(average_usage->snapshot().get());
+  ASSERT_EQ(5, new_metric->value());
+}
+
 TEST_F(MetricsTest, SimpleMeanGaugeMergeTest) {
   scoped_refptr<MeanGauge> average_usage =
     METRIC_test_mean_gauge.InstantiateMeanGauge(entity_);
diff --git a/src/kudu/util/metrics.cc b/src/kudu/util/metrics.cc
index a48f870..39bc450 100644
--- a/src/kudu/util/metrics.cc
+++ b/src/kudu/util/metrics.cc
@@ -800,6 +800,7 @@ void StringGauge::WriteValue(JsonWriter* writer) const {
 scoped_refptr<Metric> MeanGauge::snapshot() const {
   std::lock_guard<simple_spinlock> l(lock_);
   auto p = new MeanGauge(down_cast<const GaugePrototype<double>*>(prototype_));
+  p->set_value(total_sum_, total_count_);
   p->m_epoch_.store(m_epoch_);
   p->invalid_for_merge_ = invalid_for_merge_;
   p->retire_time_ = retire_time_;