Skip to content

Commit 780220f

Browse files
committed
Fix data race problem on statistic module related to metric
1 parent 4a69764 commit 780220f

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ configure_make(
88
"--enable-shared=no",
99
"--disable-libevent-regress",
1010
"--disable-openssl",
11+
"--with-pic",
1112
],
1213
lib_source = "@com_github_libevent//:all",
1314
out_lib_dir = "lib",

sentinel-core/statistic/base/metric_bucket.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int64_t MetricBucket::Get(const MetricEvent& event) const {
1919
return counters_[i].load();
2020
}
2121

22-
int64_t MetricBucket::MinRt() const { return min_rt_; }
22+
int64_t MetricBucket::MinRt() const { return min_rt_.load(); }
2323

2424
void MetricBucket::Add(const MetricEvent& event, int64_t n) {
2525
int i = (int)event;
@@ -29,12 +29,12 @@ void MetricBucket::Add(const MetricEvent& event, int64_t n) {
2929
void MetricBucket::AddRt(int64_t rt) {
3030
Add(MetricEvent::RT, rt);
3131
// Not thread-safe, but it's okay.
32-
if (rt < min_rt_) {
33-
min_rt_ = rt;
32+
if (rt < min_rt_.load()) {
33+
min_rt_.store(rt);
3434
}
3535
}
3636

37-
void MetricBucket::InitMinRt() { min_rt_ = Constants::kMaxAllowedRt; }
37+
void MetricBucket::InitMinRt() { min_rt_.store(Constants::kMaxAllowedRt); }
3838

3939
} // namespace Stat
4040
} // namespace Sentinel

sentinel-core/statistic/base/metric_bucket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MetricBucket {
2323
const std::unique_ptr<std::atomic<int64_t>[]> counters_ =
2424
std::make_unique<std::atomic<int64_t>[]>(
2525
static_cast<int>(MetricEvent::Count));
26-
long min_rt_;
26+
std::atomic<long> min_rt_;
2727

2828
void InitMinRt();
2929
};

sentinel-core/statistic/base/window_wrap.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <atomic>
34
#include <memory>
45

56
namespace Sentinel {
@@ -21,7 +22,7 @@ class WindowWrap {
2122
bool IsTimeInBucket(int64_t time_millis) const;
2223

2324
private:
24-
int64_t bucket_start_;
25+
std::atomic<int64_t> bucket_start_;
2526
const int64_t bucket_length_ms_;
2627
const std::shared_ptr<T> value_;
2728
};
@@ -36,7 +37,7 @@ int64_t WindowWrap<T>::BucketLengthInMs() const {
3637

3738
template <typename T>
3839
int64_t WindowWrap<T>::BucketStart() const {
39-
return bucket_start_;
40+
return bucket_start_.load();
4041
}
4142

4243
template <typename T>
@@ -46,7 +47,7 @@ std::shared_ptr<T> WindowWrap<T>::Value() const {
4647

4748
template <typename T>
4849
void WindowWrap<T>::ResetTo(int64_t start_time) {
49-
this->bucket_start_ = start_time;
50+
this->bucket_start_.store(start_time);
5051
}
5152

5253
template <typename T>

0 commit comments

Comments
 (0)