Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug #363

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions src/storage/buffer/buffer_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,32 @@ bool BufferObj::Free() {
}

bool BufferObj::Save() {
rw_locker_.lock(); // This lock will be released in CloseFile()
if (type_ == BufferType::kPersistent) {
// No need to save because of no change happens
rw_locker_.unlock();
return false;
}
switch (status_) {
case BufferStatus::kLoaded:
case BufferStatus::kUnloaded: {
file_worker_->WriteToFile(false);
break;
}
case BufferStatus::kFreed: {
file_worker_->MoveFile();
break;
}
default: {
UniquePtr<String> err_msg = MakeUnique<String>("Invalid buffer status.");
LOG_ERROR(*err_msg);
Error<StorageException>(*err_msg);
bool write = false;
rw_locker_.lock(); // This lock will be released on return if not write, otherwise released by CloseFile()
if (type_ != BufferType::kPersistent) {
switch (status_) {
case BufferStatus::kLoaded:
case BufferStatus::kUnloaded: {
file_worker_->WriteToFile(false);
write = true;
break;
}
case BufferStatus::kFreed: {
file_worker_->MoveFile();
break;
}
default: {
UniquePtr<String> err_msg = MakeUnique<String>("Invalid buffer status.");
LOG_ERROR(*err_msg);
Error<StorageException>(*err_msg);
}
}
type_ = BufferType::kPersistent;
}
type_ = BufferType::kPersistent;
return true;
if (!write) {
rw_locker_.unlock();
}
return write;
}

void BufferObj::Sync() { file_worker_->Sync(); }
Expand Down Expand Up @@ -190,4 +192,4 @@ void BufferObj::CheckState() const {
}
}

} // namespace infinity
} // namespace infinity
24 changes: 16 additions & 8 deletions src/unit_test/storage/buffer/buffer_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import data_file_worker;
import global_resource_usage;
import infinity_context;

using namespace infinity;

class BufferObjTest : public BaseTest {
void SetUp() override {
BaseTest::SetUp();
Expand All @@ -41,13 +43,19 @@ class BufferObjTest : public BaseTest {
infinity::GlobalResourceUsage::UnInit();
BaseTest::TearDown();
}

public:
void SaveBufferObj(BufferObj *buffer_obj) {
if (buffer_obj->Save()) {
buffer_obj->Sync();
buffer_obj->CloseFile();
}
};
};

// Test status transfer of buffer handle.
// ?? status transfer in all
TEST_F(BufferObjTest, test1) {
using namespace infinity;

SizeT memory_limit = 1024;
auto temp_dir = MakeShared<String>("/tmp/infinity/spill");
auto base_dir = MakeShared<String>("/tmp/infinity/data");
Expand Down Expand Up @@ -117,7 +125,7 @@ TEST_F(BufferObjTest, test1) {
// kUnloaded, kTemp -> kFreed, kTemp
EXPECT_EQ(buf1->status(), BufferStatus::kFreed);

buf1->Save();
SaveBufferObj(buf1);
// kFreed, kTemp -> kFreed, kPersistent
EXPECT_EQ(buf1->type(), BufferType::kPersistent);

Expand Down Expand Up @@ -148,7 +156,7 @@ TEST_F(BufferObjTest, test1) {
EXPECT_EQ(buf1->type(), BufferType::kEphemeral);
}

buf1->Save();
SaveBufferObj(buf1);
// kUnloadedModified, kEphemeral -> kUnloaded, kPersistent
EXPECT_EQ(buf1->status(), BufferStatus::kUnloaded);
EXPECT_EQ(buf1->type(), BufferType::kPersistent);
Expand All @@ -160,7 +168,7 @@ TEST_F(BufferObjTest, test1) {
EXPECT_EQ(buf1->status(), BufferStatus::kLoaded);
EXPECT_EQ(buf1->type(), BufferType::kEphemeral);

buf1->Save();
SaveBufferObj(buf1);
// kLoaded, kEphemeral -> kLoaded, kPersistent
EXPECT_EQ(buf1->status(), BufferStatus::kLoaded);
EXPECT_EQ(buf1->type(), BufferType::kPersistent);
Expand All @@ -173,7 +181,7 @@ TEST_F(BufferObjTest, test1) {

{
auto handle1 = buf1->Load();
buf1->Save();
SaveBufferObj(buf1);
// kLoaded, kEphemeral -> kLoaded, kPersistent
EXPECT_EQ(buf1->status(), BufferStatus::kLoaded);
EXPECT_EQ(buf1->type(), BufferType::kPersistent);
Expand All @@ -186,7 +194,7 @@ TEST_F(BufferObjTest, test1) {
{ auto handle2 = buf2->Load(); }
{
auto handle1 = buf1->Load();
buf1->Save();
SaveBufferObj(buf1);
// kLoaded, kPersistent -> kLoaded, kPersistent
EXPECT_EQ(buf1->status(), BufferStatus::kLoaded);
EXPECT_EQ(buf1->type(), BufferType::kPersistent);
Expand All @@ -197,7 +205,7 @@ TEST_F(BufferObjTest, test1) {
}
{ auto handle2 = buf2->Load(); }
{ auto handle1 = buf1->Load(); }
buf1->Save();
SaveBufferObj(buf1);
// kUnloaded, kPersistent -> kUnloaded, kPersistent
EXPECT_EQ(buf1->status(), BufferStatus::kUnloaded);
EXPECT_EQ(buf1->type(), BufferType::kPersistent);
Expand Down