-
Notifications
You must be signed in to change notification settings - Fork 236
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
Add optional CRC per log entry. #454
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ limitations under the License. | |
#include "buffer.hxx" | ||
#include "log_val_type.hxx" | ||
#include "ptr.hxx" | ||
#include <cstdint> | ||
|
||
#ifdef _NO_EXCEPTION | ||
#include <cassert> | ||
|
@@ -38,12 +39,10 @@ public: | |
log_entry(ulong term, | ||
const ptr<buffer>& buff, | ||
log_val_type value_type = log_val_type::app_log, | ||
uint64_t log_timestamp = 0) | ||
: term_(term) | ||
, value_type_(value_type) | ||
, buff_(buff) | ||
, timestamp_us_(log_timestamp) | ||
{} | ||
uint64_t log_timestamp = 0, | ||
bool has_crc32 = false, | ||
uint32_t crc32 = 0, | ||
bool compute_crc = true); | ||
|
||
__nocopy__(log_entry); | ||
|
||
|
@@ -92,6 +91,18 @@ public: | |
timestamp_us_ = t; | ||
} | ||
|
||
bool has_crc32() const { | ||
return has_crc32_; | ||
} | ||
|
||
uint32_t get_crc32() const { | ||
return crc32_; | ||
} | ||
|
||
void set_crc32(uint32_t crc) { | ||
crc32_ = crc; | ||
} | ||
Comment on lines
+98
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For backward compatibility (for the previous logs that do not have CRC, and they can be mixed with new logs with CRC), we should have one more field indicating if CRC exists or not, let's say Otherwise, we cannot distinguish non-existing CRC and CRC=0. Also we need an API returning that flag: bool has_crc32() const () { ... } that will be used in asio layer (replacing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
|
||
ptr<buffer> serialize() { | ||
buff_->pos(0); | ||
ptr<buffer> buf = buffer::alloc( sizeof(ulong) + | ||
|
@@ -135,10 +146,17 @@ private: | |
|
||
/** | ||
* The timestamp (since epoch) when this log entry was generated | ||
* in microseconds. Used only when `log_entry_timestamp_` in | ||
* in microseconds. Used only when `replicate_log_timestamp_` in | ||
* `asio_service_options` is set. | ||
*/ | ||
uint64_t timestamp_us_; | ||
|
||
/** | ||
* CRC32 checksum of this log entry. | ||
* Used only when `crc_on_payload` in `asio_service_options` is set. | ||
*/ | ||
bool has_crc32_; | ||
uint32_t crc32_; | ||
}; | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "crc32.hxx" | ||
#include "log_entry.hxx" | ||
|
||
namespace nuraft { | ||
log_entry::log_entry(ulong term, | ||
const ptr<buffer>& buff, | ||
log_val_type value_type, | ||
uint64_t log_timestamp, | ||
bool has_crc32, | ||
uint32_t crc32, | ||
bool compute_crc) | ||
: term_(term) | ||
, value_type_(value_type) | ||
, buff_(buff) | ||
, timestamp_us_(log_timestamp) | ||
, has_crc32_(has_crc32) | ||
, crc32_(crc32) | ||
{ | ||
if (!buff_ && !has_crc32 && compute_crc) { | ||
has_crc32_ = true; | ||
crc32_ = crc32_8( buff->data_begin(), | ||
buff->size(), | ||
0 ); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialization in the constructor is missing.