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

Avoid memory copy when parsing log #202

Merged
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
15 changes: 6 additions & 9 deletions src/Service/LogEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ ptr<buffer> LogEntryBody::serialize(ptr<log_entry> & entry)
return entry_buf;
}

ptr<log_entry> LogEntryBody::parse(const char * entry_str, const UInt64 & term, size_t buf_size)
ptr<log_entry> LogEntryBody::parse(const char * entry_str, size_t buf_size)
{
auto entry_buf = buffer::alloc(buf_size);
entry_buf->put_raw(reinterpret_cast<const byte *>(entry_str), buf_size);

entry_buf->pos(0);
nuraft::log_val_type tp = static_cast<nuraft::log_val_type>(entry_buf->get_byte());

ptr<buffer> data = buffer::copy(*entry_buf);
return cs_new<log_entry>(term, data, tp);
nuraft::log_val_type tp = static_cast<nuraft::log_val_type>(entry_str[0]);
auto data = buffer::alloc(buf_size - 1);
data->put_raw(reinterpret_cast<const byte *>(entry_str + 1), buf_size - 1);
data->pos(0);
return cs_new<log_entry>(0, data, tp); /// term is set latter
}

}
2 changes: 1 addition & 1 deletion src/Service/LogEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LogEntryBody
{
public:
static ptr<buffer> serialize(ptr<log_entry> & entry);
static ptr<log_entry> parse(const char * entry_str, const UInt64 & term, size_t buf_size);
static ptr<log_entry> parse(const char * entry_str, size_t buf_size);
};

}
3 changes: 2 additions & 1 deletion src/Service/NuRaftLogSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ int NuRaftLogSegment::loadLogEntry(int fd, off_t offset, LogEntryHeader * head,
return -1;
}

entry = LogEntryBody::parse(entry_str, head->term, head->data_length);
entry = LogEntryBody::parse(entry_str, head->data_length);
entry->set_term(head->term);

delete[] entry_str;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use buffer for entry_str instead of char pointer to avoid copy and delete.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because when we serialize data there is no header of buffer and this copy can not be avoided

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, maybe better to use buffer to avoid manual memory release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to worry about memory deallocation issues for the method is only invoked synchronously.

Copy link
Contributor Author

@JackyWoo JackyWoo Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think we should not use raw pointer in NuRaftLogSegment.cpp, maybe fixed in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

return 0;
Expand Down
17 changes: 17 additions & 0 deletions src/Service/tests/gtest_raft_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>
#include <libnuraft/nuraft.hxx>

#include <Service/LogEntry.h>
#include <Service/KeeperUtils.h>
#include <Service/NuRaftFileLogStore.h>
#include <Service/NuRaftLogSegment.h>
Expand Down Expand Up @@ -78,6 +79,22 @@ TEST(RaftLog, serializeEntry)
ASSERT_EQ(zk_create_request->data, data);
}

TEST(RaftLog, parseLogEntrybody)
{
ptr<buffer> data = buffer::alloc(2);
data->pos(0);
data->put(static_cast<byte>('1'));
data->put(static_cast<byte>('2'));
data->pos(0);
ptr<log_entry> log = cs_new<log_entry>(0, data, nuraft::log_val_type::app_log);

ptr<buffer> serialized_log = LogEntryBody::serialize(log);
ptr<log_entry> parsed_log = LogEntryBody::parse(reinterpret_cast<const char *>(serialized_log->data_begin()), serialized_log->size());

ASSERT_EQ(parsed_log->get_val_type(), log->get_val_type());
ASSERT_EQ(parsed_log->get_buf().get_str(), log->get_buf().get_str());
}

TEST(RaftLog, appendEntry)
{
String log_dir(LOG_DIR + "/1");
Expand Down
Loading