* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "test_fresh_slice_table_snapshot.h"
#include <map>
using namespace ock::bss;
BoostStateDBImpl *TestFreshSliceTableSnapshot::mBoostStateDB = nullptr;
ConfigRef TestFreshSliceTableSnapshot::mConfig = nullptr;
MemManagerRef TestFreshSliceTableSnapshot::mMemManager = nullptr;
uint64_t TestFreshSliceTableSnapshot::mCheckpointId = NO_1;
std::vector<QueryKey> TestFreshSliceTableSnapshot::originKeyList{};
std::vector<Value> TestFreshSliceTableSnapshot::originValueList{};
void TestFreshSliceTableSnapshot::SetUp()
{
std::string restorePath = "/tmp/" + std::to_string(mCheckpointId);
DeleteDirectoryContents(restorePath);
ConfigRef config = std::make_shared<Config>();
config->Init(NO_0, NO_15, NO_16);
mConfig = config;
mConfig->mMemorySegmentSize = IO_SIZE_64M;
mConfig->SetEvictMinSize(IO_SIZE_2G);
mConfig->SetSliceStandardSizePerBucket(IO_SIZE_1M);
mConfig->SetLocalPath("/tmp/" + std::to_string(mCheckpointId) + "/sst");
mMemManager = std::make_shared<MemManager>(AllocatorType::DIRECT);
mMemManager->Initialize(config);
mBoostStateDB = (BoostStateDBImpl *)BoostStateDBFactory::Create();
BResult result = mBoostStateDB->Open(config);
mBoostStateDB->GetSliceTable()->SetMemHighMark(IO_SIZE_2G);
ASSERT_TRUE(result == BSS_OK);
ASSERT_TRUE(mBoostStateDB != nullptr);
}
void TestFreshSliceTableSnapshot::TearDown()
{
if (mBoostStateDB != nullptr) {
mBoostStateDB->Close();
delete mBoostStateDB;
mBoostStateDB = nullptr;
}
std::string restorePath = "/tmp/" + std::to_string(mCheckpointId);
DeleteDirectoryContents(restorePath);
}
void TestFreshSliceTableSnapshot::SetUpTestCase()
{
}
void TestFreshSliceTableSnapshot::TearDownTestCase()
{
}
TEST_F(TestFreshSliceTableSnapshot, TestSnapshotFuncInFreshTable)
{
TestFreshSliceTableSnapshot::PutDataToFreshSliceTable(originKeyList, originValueList, NO_10000);
for (auto &key : originKeyList) {
Value value{};
mBoostStateDB->GetFreshTable()->Get(key, value);
ASSERT_TRUE(!value.IsNull());
}
ASSERT_NE(TestFreshSliceTableSnapshot::mBoostStateDB->CreateSyncCheckpoint("/tmp/" + std::to_string(mCheckpointId),
mCheckpointId),
nullptr);
ASSERT_EQ(mBoostStateDB->CreateAsyncCheckpoint(mCheckpointId, false), BSS_OK);
if (mBoostStateDB != nullptr) {
mBoostStateDB->Close();
delete mBoostStateDB;
mBoostStateDB = nullptr;
}
ConfigRef config = std::make_shared<Config>();
config->Init(NO_0, NO_15, NO_16);
mConfig = config;
mConfig->mMemorySegmentSize = IO_SIZE_64M;
mConfig->SetEvictMinSize(IO_SIZE_2G);
mConfig->SetLocalPath("/tmp/" + std::to_string(mCheckpointId) + "/sst");
mMemManager = std::make_shared<MemManager>(AllocatorType::DIRECT);
mMemManager->Initialize(config);
mBoostStateDB = (BoostStateDBImpl *)BoostStateDBFactory::Create();
BResult result = mBoostStateDB->Open(config);
ASSERT_TRUE(result == BSS_OK);
mBoostStateDB->GetSliceTable()->SetMemHighMark(IO_SIZE_2G);
std::string restorePath = "/tmp/" + std::to_string(mCheckpointId);
std::unordered_map<std::string, std::string> pathMap;
std::vector<std::string> restorePaths;
restorePaths.emplace_back(restorePath);
ASSERT_EQ(mBoostStateDB->Restore(restorePaths, pathMap, false, true), BSS_OK);
for (auto &key : originKeyList) {
Value value{};
mBoostStateDB->GetFreshTable()->Get(key, value);
ASSERT_TRUE(!value.IsNull());
}
}
TEST_F(TestFreshSliceTableSnapshot, TestSnapshotFuncInSliceTable)
{
originKeyList.clear();
originValueList.clear();
TestFreshSliceTableSnapshot::PutDataToFreshSliceTable(originKeyList, originValueList, NO_10000);
BResult ret = mBoostStateDB->GetFreshTable()->TriggerSegmentFlush();
ASSERT_EQ(ret, BSS_OK);
while (!mBoostStateDB->GetFreshTable()->IsSnapshotQueueEmpty()) {
sleep(NO_1);
}
TestAllKeysFindInSliceAndLsm();
ASSERT_NE(TestFreshSliceTableSnapshot::mBoostStateDB->CreateSyncCheckpoint("/tmp/" + std::to_string(mCheckpointId),
mCheckpointId),
nullptr);
ASSERT_EQ(mBoostStateDB->CreateAsyncCheckpoint(mCheckpointId, false), BSS_OK);
if (mBoostStateDB != nullptr) {
mBoostStateDB->Close();
delete mBoostStateDB;
mBoostStateDB = nullptr;
}
ConfigRef config = std::make_shared<Config>();
config->Init(NO_0, NO_15, NO_16);
mConfig = config;
mConfig->SetSliceStandardSizePerBucket(IO_SIZE_1M);
mConfig->mMemorySegmentSize = IO_SIZE_16K;
mConfig->SetEvictMinSize(IO_SIZE_2G);
mConfig->SetLocalPath("/tmp/" + std::to_string(mCheckpointId) + "/sst");
mMemManager = std::make_shared<MemManager>(AllocatorType::DIRECT);
mMemManager->Initialize(config);
mBoostStateDB = (BoostStateDBImpl *)BoostStateDBFactory::Create();
BResult result = mBoostStateDB->Open(config);
ASSERT_TRUE(result == BSS_OK);
mBoostStateDB->GetSliceTable()->SetMemHighMark(IO_SIZE_2G);
std::string restorePath = "/tmp/" + std::to_string(mCheckpointId);
std::unordered_map<std::string, std::string> pathMap;
std::vector<std::string> restorePaths;
restorePaths.emplace_back(restorePath);
ASSERT_EQ(mBoostStateDB->Restore(restorePaths, pathMap, false, true), BSS_OK);
TestAllKeysFindInSliceAndLsm();
}
TEST_F(TestFreshSliceTableSnapshot, TestSnapshotFuncInFileStore)
{
originKeyList.clear();
originValueList.clear();
mBoostStateDB->GetSliceTable()->GetFullSortEvictor()->SetVictMinSize(1);
mBoostStateDB->GetSliceTable()->SetMemHighMark(NO_0);
TestFreshSliceTableSnapshot::PutDataToFreshSliceTable(originKeyList, originValueList, NO_100000);
BResult ret = mBoostStateDB->GetFreshTable()->TriggerSegmentFlush();
ASSERT_EQ(ret, BSS_OK);
while (!mBoostStateDB->GetFreshTable()->IsSnapshotQueueEmpty()) {
sleep(NO_1);
}
while (!IsAllSliceEvicted()) {
mBoostStateDB->GetSliceTable()->TryCurrentDBEvict(0, true, true);
}
TestAllKeysFindInSliceAndLsm();
ASSERT_NE(TestFreshSliceTableSnapshot::mBoostStateDB->CreateSyncCheckpoint("/tmp/" + std::to_string(mCheckpointId),
mCheckpointId),
nullptr);
ASSERT_EQ(mBoostStateDB->CreateAsyncCheckpoint(mCheckpointId, false), BSS_OK);
if (mBoostStateDB != nullptr) {
mBoostStateDB->Close();
delete mBoostStateDB;
mBoostStateDB = nullptr;
}
ConfigRef config = std::make_shared<Config>();
config->Init(NO_0, NO_15, NO_16);
mConfig = config;
mConfig->mTotalMemHighMarkRatio = 0;
mConfig->SetEvictMinSize(NO_1);
mConfig->SetSliceStandardSizePerBucket(IO_SIZE_1M);
mConfig->mMemorySegmentSize = IO_SIZE_16K;
mConfig->SetLocalPath("/tmp/" + std::to_string(mCheckpointId) + "/sst");
mMemManager = std::make_shared<MemManager>(AllocatorType::DIRECT);
mMemManager->Initialize(config);
mBoostStateDB = (BoostStateDBImpl *)BoostStateDBFactory::Create();
BResult result = mBoostStateDB->Open(config);
ASSERT_TRUE(result == BSS_OK);
mBoostStateDB->GetSliceTable()->SetMemHighMark(NO_0);
std::string restorePath = "/tmp/" + std::to_string(mCheckpointId);
std::unordered_map<std::string, std::string> pathMap;
std::vector<std::string> restorePaths;
restorePaths.emplace_back(restorePath);
ASSERT_EQ(mBoostStateDB->Restore(restorePaths, pathMap, false, true), BSS_OK);
TestAllKeysFindInSliceAndLsm();
TestFreshSliceTableSnapshot::PutDataToFreshSliceTable(originKeyList, originValueList, NO_100);
ret = mBoostStateDB->GetFreshTable()->TriggerSegmentFlush();
ASSERT_EQ(ret, BSS_OK);
while (!mBoostStateDB->GetFreshTable()->IsSnapshotQueueEmpty()) {
sleep(NO_1);
}
while (!IsAllSliceEvicted()) {
mBoostStateDB->GetSliceTable()->TryCurrentDBEvict(0, true, true);
}
}