Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40f5599

Browse files
authored
Fixed incorrect rate metric in StatsManager (vesoft-inc#754)
1 parent 3b18530 commit 40f5599

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

src/common/http/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ nebula_add_test(
99
$<TARGET_OBJECTS:process_obj>
1010
$<TARGET_OBJECTS:fs_obj>
1111
$<TARGET_OBJECTS:stats_obj>
12+
$<TARGET_OBJECTS:time_obj>
1213
$<TARGET_OBJECTS:base_obj>
1314
LIBRARIES
1415
proxygenhttpserver

src/common/stats/StatsManager.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ void StatsManager::addValue(int32_t index, VT value) {
138138
folly::RWSpinLock::ReadHolder rh(sm.statsLock_);
139139
DCHECK_LT(index, sm.stats_.size());
140140
std::lock_guard<std::mutex> g(*(sm.stats_[index].first));
141-
sm.stats_[index].second->addValue(Clock::now(), value);
141+
sm.stats_[index].second->addValue(seconds(time::WallClock::fastNowInSec()), value);
142142
} else {
143143
// Histogram
144144
index = - (index + 1);
145145
folly::RWSpinLock::ReadHolder rh(sm.histogramsLock_);
146146
DCHECK_LT(index, sm.histograms_.size());
147147
std::lock_guard<std::mutex> g(*(sm.histograms_[index].first));
148-
sm.histograms_[index].second->addValue(Clock::now(), value);
148+
sm.histograms_[index].second->addValue(seconds(time::WallClock::fastNowInSec()), value);
149149
}
150150
}
151151

@@ -262,23 +262,25 @@ void StatsManager::readAllValue(folly::dynamic& vals) {
262262
StatsManager::VT StatsManager::readStats(int32_t index,
263263
StatsManager::TimeRange range,
264264
StatsManager::StatsMethod method) {
265+
using std::chrono::seconds;
265266
auto& sm = get();
266267

268+
267269
CHECK_NE(index, 0);
268270

269271
if (index > 0) {
270272
// stats
271273
--index;
272274
DCHECK_LT(index, sm.stats_.size());
273275
std::lock_guard<std::mutex> g(*(sm.stats_[index].first));
274-
sm.stats_[index].second->update(Clock::now());
276+
sm.stats_[index].second->update(seconds(time::WallClock::fastNowInSec()));
275277
return readValue(*(sm.stats_[index].second), range, method);
276278
} else {
277279
// histograms_
278280
index = - (index + 1);
279281
DCHECK_LT(index, sm.histograms_.size());
280282
std::lock_guard<std::mutex> g(*(sm.histograms_[index].first));
281-
sm.histograms_[index].second->update(Clock::now());
283+
sm.histograms_[index].second->update(seconds(time::WallClock::fastNowInSec()));
282284
return readValue(*(sm.histograms_[index].second), range, method);
283285
}
284286
}
@@ -312,6 +314,7 @@ StatsManager::VT StatsManager::readStats(const std::string& counterName,
312314
StatsManager::VT StatsManager::readHisto(const std::string& counterName,
313315
StatsManager::TimeRange range,
314316
double pct) {
317+
using std::chrono::seconds;
315318
auto& sm = get();
316319

317320
// Look up the counter name
@@ -332,7 +335,7 @@ StatsManager::VT StatsManager::readHisto(const std::string& counterName,
332335
DCHECK_LT(index, sm.histograms_.size());
333336

334337
std::lock_guard<std::mutex> g(*(sm.histograms_[index].first));
335-
sm.histograms_[index].second->update(Clock::now());
338+
sm.histograms_[index].second->update(seconds(time::WallClock::fastNowInSec()));
336339
auto level = static_cast<size_t>(range);
337340
return sm.histograms_[index].second->getPercentileEstimate(pct, level);
338341
}

src/common/stats/StatsManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define COMMON_STATS_STATSMANAGER_H_
99

1010
#include "base/Base.h"
11+
#include "time/WallClock.h"
1112
#include <folly/RWSpinLock.h>
1213
#include <folly/stats/MultiLevelTimeSeries.h>
1314
#include <folly/stats/TimeseriesHistogram.h>
@@ -39,9 +40,8 @@ namespace stats {
3940
*/
4041
class StatsManager final {
4142
using VT = int64_t;
42-
using Clock = std::chrono::steady_clock;
43-
using StatsType = folly::MultiLevelTimeSeries<VT, Clock>;
44-
using HistogramType = folly::TimeseriesHistogram<VT, Clock>;
43+
using StatsType = folly::MultiLevelTimeSeries<VT>;
44+
using HistogramType = folly::TimeseriesHistogram<VT>;
4545

4646
public:
4747
enum class StatsMethod {

src/common/stats/StatsManager.inl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ StatsManager::VT StatsManager::readValue(StatsHolder& stats,
1919
case StatsMethod::COUNT:
2020
return stats.count(level);
2121
case StatsMethod::AVG:
22-
return stats.avg(level);
22+
return stats.template avg<VT>(level);
2323
case StatsMethod::RATE:
24-
return stats.rate(level);
24+
return stats.template rate<VT>(level);
2525
}
2626

2727
LOG(FATAL) << "Unknown statistic method";
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
nebula_add_test(
2-
NAME stats_manager_test
3-
SOURCES StatsManagerTest.cpp
4-
OBJECTS $<TARGET_OBJECTS:stats_obj> $<TARGET_OBJECTS:base_obj>
5-
LIBRARIES gtest
2+
NAME
3+
stats_manager_test
4+
SOURCES
5+
StatsManagerTest.cpp
6+
OBJECTS
7+
$<TARGET_OBJECTS:stats_obj>
8+
$<TARGET_OBJECTS:time_obj>
9+
$<TARGET_OBJECTS:base_obj>
10+
$<TARGET_OBJECTS:thread_obj>
11+
LIBRARIES
12+
gtest
613
)
714

815

916
nebula_add_executable(
10-
NAME stats_manager_bm
11-
SOURCES StatsManagerBenchmark.cpp
12-
OBJECTS $<TARGET_OBJECTS:stats_obj>
13-
LIBRARIES follybenchmark boost_regex
17+
NAME
18+
stats_manager_bm
19+
SOURCES
20+
StatsManagerBenchmark.cpp
21+
OBJECTS
22+
$<TARGET_OBJECTS:stats_obj>
23+
$<TARGET_OBJECTS:time_obj>
24+
$<TARGET_OBJECTS:base_obj>
25+
LIBRARIES
26+
follybenchmark boost_regex
1427
)

src/common/stats/test/StatsManagerTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "base/Base.h"
88
#include <gtest/gtest.h>
99
#include "stats/StatsManager.h"
10+
#include "thread/GenericWorker.h"
1011

1112
namespace nebula {
1213
namespace stats {
@@ -42,6 +43,30 @@ TEST(StatsManager, StatsTest) {
4243
}
4344

4445

46+
TEST(StatsManager, RateTest) {
47+
auto statId = StatsManager::registerStats("ratetest");
48+
auto thread = std::make_unique<thread::GenericWorker>();
49+
ASSERT_TRUE(thread->start());
50+
51+
auto task = [=] () {
52+
StatsManager::addValue(statId);
53+
};
54+
constexpr auto qps = 100L;
55+
thread->addRepeatTask(1 * 1000 / qps, task);
56+
57+
::usleep(60 * 1000 * 1000);
58+
59+
auto actual = StatsManager::readValue("ratetest.rate.60");
60+
61+
ASSERT_LT(std::max(qps, actual) - std::min(qps, actual), 10L) << "expected: " << qps
62+
<< ", actual: " << actual;
63+
64+
thread->stop();
65+
thread->wait();
66+
thread.reset();
67+
}
68+
69+
4570
TEST(StatsManager, HistogramTest) {
4671
auto statId = StatsManager::registerHisto("stat02", 1, 1, 100);
4772
std::vector<std::thread> threads;

src/webservice/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ nebula_add_test(
1010
$<TARGET_OBJECTS:process_obj>
1111
$<TARGET_OBJECTS:fs_obj>
1212
$<TARGET_OBJECTS:stats_obj>
13+
$<TARGET_OBJECTS:time_obj>
1314
LIBRARIES
1415
proxygenhttpserver
1516
proxygenlib
@@ -30,6 +31,7 @@ nebula_add_test(
3031
$<TARGET_OBJECTS:process_obj>
3132
$<TARGET_OBJECTS:fs_obj>
3233
$<TARGET_OBJECTS:stats_obj>
34+
$<TARGET_OBJECTS:time_obj>
3335
LIBRARIES
3436
proxygenhttpserver
3537
proxygenlib

0 commit comments

Comments
 (0)