Skip to content

Commit 5ae9905

Browse files
committed
fix PR
Signed-off-by: adi_holden <[email protected]>
1 parent fe9f401 commit 5ae9905

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

src/server/dragonfly_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern "C" {
2222

2323
ABSL_DECLARE_FLAG(float, mem_defrag_threshold);
2424
ABSL_DECLARE_FLAG(std::vector<std::string>, rename_command);
25-
ABSL_DECLARE_FLAG(double, maxmemory_ratio);
25+
ABSL_DECLARE_FLAG(double, oom_deny_ratio);
2626

2727
namespace dfly {
2828

@@ -383,7 +383,7 @@ TEST_F(DflyEngineTest, Bug207) {
383383
shard_set->TEST_EnableHeartBeat();
384384
shard_set->TEST_EnableCacheMode();
385385
absl::FlagSaver fs;
386-
absl::SetFlag(&FLAGS_maxmemory_ratio, 4);
386+
absl::SetFlag(&FLAGS_oom_deny_ratio, 4);
387387

388388
max_memory_limit = 300000;
389389

@@ -414,7 +414,7 @@ TEST_F(DflyEngineTest, StickyEviction) {
414414
shard_set->TEST_EnableHeartBeat();
415415
shard_set->TEST_EnableCacheMode();
416416
absl::FlagSaver fs;
417-
absl::SetFlag(&FLAGS_maxmemory_ratio, 4);
417+
absl::SetFlag(&FLAGS_oom_deny_ratio, 4);
418418

419419
max_memory_limit = 300000;
420420

src/server/main_service.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ ABSL_FLAG(MaxMemoryFlag, maxmemory, MaxMemoryFlag{},
7777
"Limit on maximum-memory that is used by the database. "
7878
"0 - means the program will automatically determine its maximum memory usage. "
7979
"default: 0");
80-
ABSL_FLAG(double, maxmemory_ratio, 1.1,
80+
ABSL_FLAG(double, oom_deny_ratio, 1.1,
8181
"commands with flag denyoom will return OOM when the ratio between maxmemory and used "
82-
"memory is above "
83-
"this value");
82+
"memory is above this value");
8483

8584
bool AbslParseFlag(std::string_view in, MaxMemoryFlag* flag, std::string* err) {
8685
int64_t val;
@@ -896,9 +895,9 @@ void Service::DispatchCommand(CmdArgList args, facade::ConnectionContext* cntx)
896895
}
897896

898897
uint64_t start_ns = ProactorBase::GetMonotonicTimeNs(), end_ns;
899-
double maxmemory_ratio = GetFlag(FLAGS_maxmemory_ratio);
900-
uint64_t used_memory = ServerState::tlocal()->GetCachedUsedMemory(start_ns);
901-
if (used_memory > (max_memory_limit * maxmemory_ratio) && (cid->opt_mask() & CO::DENYOOM)) {
898+
double oom_deny_ratio = GetFlag(FLAGS_oom_deny_ratio);
899+
uint64_t used_memory = ServerState::tlocal()->GetUsedMemory(start_ns);
900+
if (used_memory > (max_memory_limit * oom_deny_ratio) && (cid->opt_mask() & CO::DENYOOM)) {
902901
return (*cntx)->SendError(kOutOfMemory);
903902
}
904903

src/server/server_state.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ void ServerState::Destroy() {
7171
state_ = nullptr;
7272
}
7373

74-
uint64_t ServerState::GetCachedUsedMemory(uint64_t now_ns) {
75-
uint64_t kCacheEveryNs = 1000;
76-
if (now_ns > last_chached_used_current_ + kCacheEveryNs) {
77-
last_chached_used_current_ = now_ns;
78-
used_mem_ = used_mem_current;
74+
uint64_t ServerState::GetUsedMemory(uint64_t now_ns) {
75+
static constexpr uint64_t kCacheEveryNs = 1000;
76+
if (now_ns > used_mem_last_update_ + kCacheEveryNs) {
77+
used_mem_last_update_ = now_ns;
78+
used_mem_cached_ = used_mem_current.load(memory_order_relaxed);
7979
}
80-
return used_mem_;
80+
return used_mem_cached_;
8181
}
8282

8383
bool ServerState::AllowInlineScheduling() const {

src/server/server_state.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class ServerState { // public struct - to allow initialization.
130130
gstate_ = s;
131131
}
132132

133-
uint64_t GetCachedUsedMemory(uint64_t now_ns);
133+
uint64_t GetUsedMemory(uint64_t now_ns);
134134

135135
bool AllowInlineScheduling() const;
136136

@@ -228,8 +228,8 @@ class ServerState { // public struct - to allow initialization.
228228

229229
absl::flat_hash_map<std::string, base::Histogram> call_latency_histos_;
230230
uint32_t thread_index_ = 0;
231-
uint64_t used_mem_ = 0;
232-
uint64_t last_chached_used_current_ = 0;
231+
uint64_t used_mem_cached_ = 0; // thread local cache of used_mem_current
232+
uint64_t used_mem_last_update_ = 0;
233233

234234
static __thread ServerState* state_;
235235
};

0 commit comments

Comments
 (0)