Skip to content

Commit

Permalink
fix issue 3869 (vesoft-inc#619)
Browse files Browse the repository at this point in the history
Co-authored-by: Sophie <[email protected]>

Co-authored-by: hs.zhang <[email protected]>
Co-authored-by: Sophie <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2022
1 parent db0ae57 commit 30551a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
34 changes: 20 additions & 14 deletions src/common/stats/StatsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,18 @@ void StatsManager::addValue(const CounterId& id, VT value) {
bool isHisto = id.isHisto();
if (!isHisto) {
// Stats
DCHECK(sm.stats_.find(index) != sm.stats_.end());
std::lock_guard<std::mutex> g(*(sm.stats_[index].first));
sm.stats_[index].second->addValue(seconds(time::WallClock::fastNowInSec()), value);
auto iter = sm.stats_.find(index);
if (iter != sm.stats_.end()) {
std::lock_guard<std::mutex> g(*(iter->second.first));
iter->second.second->addValue(seconds(time::WallClock::fastNowInSec()), value);
}
} else {
// Histogram
DCHECK(sm.histograms_.find(index) != sm.histograms_.end());
std::lock_guard<std::mutex> g(*(sm.histograms_[index].first));
sm.histograms_[index].second->addValue(seconds(time::WallClock::fastNowInSec()), value);
auto iter = sm.histograms_.find(index);
if (iter != sm.histograms_.end()) {
std::lock_guard<std::mutex> g(*(iter->second.first));
iter->second.second->addValue(seconds(time::WallClock::fastNowInSec()), value);
}
}
}

Expand Down Expand Up @@ -464,16 +468,18 @@ StatusOr<StatsManager::VT> StatsManager::readStats(const CounterId& id,

if (!id.isHisto()) {
// stats
DCHECK(sm.stats_.find(index) != sm.stats_.end());
std::lock_guard<std::mutex> g(*(sm.stats_[index].first));
sm.stats_[index].second->update(seconds(time::WallClock::fastNowInSec()));
return readValue(*(sm.stats_[index].second), range, method);
auto iter = sm.stats_.find(index);
DCHECK(iter != sm.stats_.end());
std::lock_guard<std::mutex> g(*(iter->second.first));
iter->second.second->update(seconds(time::WallClock::fastNowInSec()));
return readValue(*(iter->second.second), range, method);
} else {
// histograms_
DCHECK(sm.histograms_.find(index) != sm.histograms_.end());
std::lock_guard<std::mutex> g(*(sm.histograms_[index].first));
sm.histograms_[index].second->update(seconds(time::WallClock::fastNowInSec()));
return readValue(*(sm.histograms_[index].second), range, method);
auto iter = sm.histograms_.find(index);
DCHECK(iter != sm.histograms_.end());
std::lock_guard<std::mutex> g(*(iter->second.first));
iter->second.second->update(seconds(time::WallClock::fastNowInSec()));
return readValue(*(iter->second.second), range, method);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/common/stats/StatsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define COMMON_STATS_STATSMANAGER_H_

#include <folly/RWSpinLock.h>
#include <folly/concurrency/ConcurrentHashMap.h>
#include <folly/stats/MultiLevelTimeSeries.h>
#include <folly/stats/TimeseriesHistogram.h>

Expand Down Expand Up @@ -206,8 +207,8 @@ class StatsManager final {
std::unordered_map<std::string, CounterInfo> nameMap_;

// All time series stats
std::unordered_map<std::string,
std::pair<std::unique_ptr<std::mutex>, std::unique_ptr<StatsType>>>
folly::ConcurrentHashMap<std::string,
std::pair<std::unique_ptr<std::mutex>, std::unique_ptr<StatsType>>>
stats_;

// All histogram stats
Expand Down

0 comments on commit 30551a7

Please sign in to comment.