Skip to content

Commit

Permalink
Add change buffer API to log_entry (#490)
Browse files Browse the repository at this point in the history
* Add change buffer API to log_entry

* Formatting

---------

Co-authored-by: Jung-Sang Ahn <[email protected]>
  • Loading branch information
hkadayam and greensky00 authored Mar 11, 2024
1 parent cae3e0a commit bc1db87
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions include/libnuraft/log_entry.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public:
return buff_;
}

void change_buf(const ptr<buffer>& buff);

uint64_t get_timestamp() const {
return timestamp_us_;
}
Expand Down
15 changes: 12 additions & 3 deletions src/log_entry.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ log_entry::log_entry(ulong term,
{
if (buff_ && !has_crc32 && compute_crc) {
has_crc32_ = true;
crc32_ = crc32_8( buff_->data_begin(),
buff_->size(),
0 );
crc32_ = crc32_8(buff_->data_begin(),
buff_->size(),
0);
}
}

void log_entry::change_buf(const ptr<buffer>& buff) {
buff_ = buff;
if (buff_ && has_crc32_) {
crc32_ = crc32_8(buff_->data_begin(),
buff_->size(),
0);
}
}
}
35 changes: 26 additions & 9 deletions tests/unit/serialization_test.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include "handle_custom_notification.hxx"
#include "nuraft.hxx"
#include "strfmt.hxx"
#include "crc32.hxx"

#include "test_common.h"

Expand Down Expand Up @@ -196,6 +197,18 @@ int snapshot_sync_req_zero_buffer_test(bool done) {
return 0;
}

static int compare_log_entries(const ptr<log_entry>& e1, const ptr<log_entry>& e2) {
CHK_EQ(e1->get_term(), e2->get_term());
CHK_EQ(e1->get_val_type(), e2->get_val_type());
CHK_EQ(e1->get_buf().size(), e2->get_buf().size());
for (size_t i = 0; i < e1->get_buf().size(); ++i) {
byte b1 = e1->get_buf().get_byte();
byte b2 = e2->get_buf().get_byte();
CHK_EQ(b1, b2);
}
return 0;
}

int log_entry_test() {
ptr<buffer> data = buffer::alloc(24 + rnd() % 100);
for (size_t i = 0; i < data->size(); ++i) {
Expand All @@ -207,17 +220,21 @@ int log_entry_test() {
data,
static_cast<log_val_type>(1 + rnd() % 5) );
ptr<buffer> buf2 = entry->serialize();
CHK_Z(compare_log_entries(entry, log_entry::deserialize(*buf2)));

ptr<log_entry> entry1 = log_entry::deserialize(*buf2);

CHK_EQ( entry->get_term(), entry1->get_term() );
CHK_EQ( entry->get_val_type(), entry1->get_val_type() );
CHK_EQ( entry->get_buf().size(), entry1->get_buf().size() );
for (size_t i = 0; i < entry->get_buf().size(); ++i) {
byte b1 = entry->get_buf().get_byte();
byte b2 = entry1->get_buf().get_byte();
CHK_EQ( b1, b2 );
// Change the data buffer in log_entry and check if everything is equal
ptr<buffer> data2 = buffer::alloc(24 + rnd() % 100);
for (size_t i = 0; i < data2->size(); ++i) {
data2->put(static_cast<byte>( rnd() % 255) );
}
uint32_t new_crc = crc32_8(data2->data_begin(),
data2->size(),
0);
entry->change_buf(data2);
buf2 = entry->serialize();
CHK_Z(compare_log_entries(entry, log_entry::deserialize(*buf2)));

CHK_EQ(new_crc, entry->get_crc32());
return 0;
}

Expand Down

0 comments on commit bc1db87

Please sign in to comment.