mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-11 10:25:47 +02:00
fix(server): defrag unit test fix
Signed-off-by: Boaz Sade <boaz@dragonflydb.io>
This commit is contained in:
parent
5d39521de3
commit
044d4a4044
1 changed files with 26 additions and 13 deletions
|
@ -12,6 +12,7 @@ extern "C" {
|
||||||
#include <absl/strings/strip.h>
|
#include <absl/strings/strip.h>
|
||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
|
|
||||||
|
#include "base/flags.h"
|
||||||
#include "base/gtest.h"
|
#include "base/gtest.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
#include "facade/facade_test.h"
|
#include "facade/facade_test.h"
|
||||||
|
@ -19,10 +20,13 @@ extern "C" {
|
||||||
#include "server/main_service.h"
|
#include "server/main_service.h"
|
||||||
#include "server/test_utils.h"
|
#include "server/test_utils.h"
|
||||||
|
|
||||||
|
ABSL_DECLARE_FLAG(float, commit_use_threshold);
|
||||||
|
|
||||||
namespace dfly {
|
namespace dfly {
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace util;
|
using namespace util;
|
||||||
|
using absl::SetFlag;
|
||||||
using absl::StrCat;
|
using absl::StrCat;
|
||||||
using ::io::Result;
|
using ::io::Result;
|
||||||
using testing::ElementsAre;
|
using testing::ElementsAre;
|
||||||
|
@ -755,12 +759,18 @@ TEST_F(DflyEngineTest, Bug496) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(DefragDflyEngineTest, TestDefragOption) {
|
TEST_F(DefragDflyEngineTest, TestDefragOption) {
|
||||||
|
absl::SetFlag(&FLAGS_commit_use_threshold, 1.1);
|
||||||
// Fill data into dragonfly and then check if we have
|
// Fill data into dragonfly and then check if we have
|
||||||
// any location in memory to defrag. See issue #448 for details about this.
|
// any location in memory to defrag. See issue #448 for details about this.
|
||||||
max_memory_limit = 300'000; // control memory size so no need for too many keys
|
constexpr size_t kMaxMemoryForTest = 1'100'000;
|
||||||
constexpr int kNumberOfKeys = 100'000; // this fill the memory
|
constexpr int kNumberOfKeys = 1'000; // this fill the memory
|
||||||
constexpr int kKeySize = 137;
|
constexpr int kKeySize = 637;
|
||||||
constexpr int kMaxDefragTriesForTests = 10;
|
constexpr int kMaxDefragTriesForTests = 10;
|
||||||
|
constexpr int kFactor = 10;
|
||||||
|
constexpr int kMaxNumKeysToDelete = 100;
|
||||||
|
|
||||||
|
max_memory_limit = kMaxMemoryForTest; // control memory size so no need for too many keys
|
||||||
|
shard_set->TEST_EnableHeartBeat(); // enable use memory update (used_mem_current)
|
||||||
|
|
||||||
std::vector<std::string> keys2delete;
|
std::vector<std::string> keys2delete;
|
||||||
keys2delete.push_back("del");
|
keys2delete.push_back("del");
|
||||||
|
@ -769,8 +779,6 @@ TEST_F(DefragDflyEngineTest, TestDefragOption) {
|
||||||
// The keys that we will delete are all in the form of "key-name:1<other digits>"
|
// The keys that we will delete are all in the form of "key-name:1<other digits>"
|
||||||
// This is because we are populating keys that has this format, but we don't want
|
// This is because we are populating keys that has this format, but we don't want
|
||||||
// to delete all keys, only some random keys so we deleting those that start with 1
|
// to delete all keys, only some random keys so we deleting those that start with 1
|
||||||
constexpr int kFactor = 10;
|
|
||||||
int kMaxNumKeysToDelete = 10'000;
|
|
||||||
int current_step = kFactor;
|
int current_step = kFactor;
|
||||||
for (int i = 1; i < kMaxNumKeysToDelete; current_step *= kFactor) {
|
for (int i = 1; i < kMaxNumKeysToDelete; current_step *= kFactor) {
|
||||||
for (; i < current_step; i++) {
|
for (; i < current_step; i++) {
|
||||||
|
@ -784,23 +792,28 @@ TEST_F(DefragDflyEngineTest, TestDefragOption) {
|
||||||
RespExpr resp = Run(
|
RespExpr resp = Run(
|
||||||
{"DEBUG", "POPULATE", std::to_string(kNumberOfKeys), "key-name", std::to_string(kKeySize)});
|
{"DEBUG", "POPULATE", std::to_string(kNumberOfKeys), "key-name", std::to_string(kKeySize)});
|
||||||
ASSERT_EQ(resp, "OK");
|
ASSERT_EQ(resp, "OK");
|
||||||
resp = Run({"DBSIZE"});
|
auto r = CheckedInt({"DBSIZE"});
|
||||||
EXPECT_THAT(resp, IntArg(kNumberOfKeys));
|
|
||||||
|
ASSERT_EQ(r, kNumberOfKeys);
|
||||||
|
|
||||||
shard_set->pool()->AwaitFiberOnAll([&](unsigned index, ProactorBase* base) {
|
shard_set->pool()->AwaitFiberOnAll([&](unsigned index, ProactorBase* base) {
|
||||||
EngineShard* shard = EngineShard::tlocal();
|
EngineShard* shard = EngineShard::tlocal();
|
||||||
ASSERT_FALSE(shard == nullptr); // we only have one and its should not be empty!
|
ASSERT_FALSE(shard == nullptr); // we only have one and its should not be empty!
|
||||||
fibers_ext::SleepFor(100ms);
|
fibers_ext::SleepFor(100ms);
|
||||||
EXPECT_EQ(shard->stats().defrag_realloc_total, 0);
|
auto mem_used = 0;
|
||||||
// we are expecting to have at least one try by now
|
// make sure that the task that collect memory usage from all shard ran
|
||||||
EXPECT_GT(shard->stats().defrag_task_invocation_total, 0);
|
// for at least once, and that no defrag was done yet.
|
||||||
|
for (int i = 0; i < 3 && mem_used == 0; i++) {
|
||||||
|
fibers_ext::SleepFor(100ms);
|
||||||
|
EXPECT_EQ(shard->stats().defrag_realloc_total, 0);
|
||||||
|
mem_used = used_mem_current.load(memory_order_relaxed);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ArgSlice delete_cmd(keys);
|
ArgSlice delete_cmd(keys);
|
||||||
auto r = CheckedInt(delete_cmd);
|
r = CheckedInt(delete_cmd);
|
||||||
// the first element in this is the command del so size is one less
|
// the first element in this is the command del so size is one less
|
||||||
ASSERT_EQ(r, keys2delete.size() - 1);
|
ASSERT_EQ(r, kMaxNumKeysToDelete - 1);
|
||||||
|
|
||||||
// At this point we need to see whether we did running the task and whether the task did something
|
// At this point we need to see whether we did running the task and whether the task did something
|
||||||
shard_set->pool()->AwaitFiberOnAll([&](unsigned index, ProactorBase* base) {
|
shard_set->pool()->AwaitFiberOnAll([&](unsigned index, ProactorBase* base) {
|
||||||
EngineShard* shard = EngineShard::tlocal();
|
EngineShard* shard = EngineShard::tlocal();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue