From 1f719a9ead67a717923eac67ef1350692d9acc83 Mon Sep 17 00:00:00 2001 From: Free Ekanayaka Date: Thu, 14 Sep 2023 19:14:35 +0100 Subject: [PATCH 1/5] uv/encoding: Avoid logic duplication in sizeofAppendEntries Re-use uvSizeofBatchHeader() instead of duplicating its logic in sizeofAppendEntries. Signed-off-by: Free Ekanayaka --- src/uv_encoding.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/uv_encoding.c b/src/uv_encoding.c index 8e770f6b..cee461b1 100644 --- a/src/uv_encoding.c +++ b/src/uv_encoding.c @@ -47,8 +47,7 @@ static size_t sizeofAppendEntries(const struct raft_append_entries *p) sizeof(uint64_t) + /* Previous log entry index */ sizeof(uint64_t) + /* Previous log entry term */ sizeof(uint64_t) + /* Leader's commit index */ - sizeof(uint64_t) + /* Number of entries in the batch */ - 16 * p->n_entries /* One header per entry */; + uvSizeofBatchHeader(p->n_entries) /* Batch header */; } static size_t sizeofAppendEntriesResultV0(void) From 4fd09926859dda613adcca808bddf9d1aff9c78a Mon Sep 17 00:00:00 2001 From: Free Ekanayaka Date: Thu, 14 Sep 2023 19:15:45 +0100 Subject: [PATCH 2/5] uv/encoding: Improve comments in encodeAppendEntries() The comments of the various fields now match the ones in sizeofAppendEntries(). Signed-off-by: Free Ekanayaka --- src/uv_encoding.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/uv_encoding.c b/src/uv_encoding.c index cee461b1..5bbd34af 100644 --- a/src/uv_encoding.c +++ b/src/uv_encoding.c @@ -129,9 +129,9 @@ static void encodeAppendEntries(const struct raft_append_entries *p, void *buf) cursor = buf; bytePut64(&cursor, p->term); /* Leader's term. */ - bytePut64(&cursor, p->prev_log_index); /* Previous index. */ - bytePut64(&cursor, p->prev_log_term); /* Previous term. */ - bytePut64(&cursor, p->leader_commit); /* Commit index. */ + bytePut64(&cursor, p->prev_log_index); /* Previous log entry index. */ + bytePut64(&cursor, p->prev_log_term); /* Previous log entry term. */ + bytePut64(&cursor, p->leader_commit); /* Leader's commit index. */ uvEncodeBatchHeader(p->entries, p->n_entries, cursor); } From ce0890e1c2855aceb467a2c4798f37b0f08e8ed7 Mon Sep 17 00:00:00 2001 From: Free Ekanayaka Date: Thu, 14 Sep 2023 19:17:58 +0100 Subject: [PATCH 3/5] uv/encoding: Improve comments in sizeofAppendEntriesResult() Signed-off-by: Free Ekanayaka --- src/uv_encoding.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/uv_encoding.c b/src/uv_encoding.c index 5bbd34af..ecf032e3 100644 --- a/src/uv_encoding.c +++ b/src/uv_encoding.c @@ -59,7 +59,8 @@ static size_t sizeofAppendEntriesResultV0(void) static size_t sizeofAppendEntriesResult(void) { - return sizeofAppendEntriesResultV0() + sizeof(uint64_t) /* 64 bit Flags. */; + return sizeofAppendEntriesResultV0() + /* Size of older version 0 message */ + sizeof(uint64_t) /* Server features. */; } static size_t sizeofInstallSnapshot(const struct raft_install_snapshot *p) From ee7bb48b74f90708329831b0909d5d453f619da1 Mon Sep 17 00:00:00 2001 From: Free Ekanayaka Date: Thu, 14 Sep 2023 19:34:09 +0100 Subject: [PATCH 4/5] uv/encoding: Fill the spurious extra 64-bit field of AppendEntries There is a spurious 64-bit field in the AppendEntries message, that was supposed to contain the Leader ID, but never did (since the sender ID is included in struct raft_message). This commits now fills it with 0, in case we want to re-purpose it later in the future. Signed-off-by: Free Ekanayaka --- src/uv_encoding.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/uv_encoding.c b/src/uv_encoding.c index ecf032e3..1e060f08 100644 --- a/src/uv_encoding.c +++ b/src/uv_encoding.c @@ -42,12 +42,12 @@ static size_t sizeofRequestVoteResult(void) static size_t sizeofAppendEntries(const struct raft_append_entries *p) { - return sizeof(uint64_t) + /* Leader's term. */ - sizeof(uint64_t) + /* Leader ID */ - sizeof(uint64_t) + /* Previous log entry index */ - sizeof(uint64_t) + /* Previous log entry term */ - sizeof(uint64_t) + /* Leader's commit index */ - uvSizeofBatchHeader(p->n_entries) /* Batch header */; + return sizeof(uint64_t) + /* Leader's term. */ + sizeof(uint64_t) + /* Previous log entry index */ + sizeof(uint64_t) + /* Previous log entry term */ + sizeof(uint64_t) + /* Leader's commit index */ + uvSizeofBatchHeader(p->n_entries) + /* Batch header */ + sizeof(uint64_t); /* XXX: currently unused */ } static size_t sizeofAppendEntriesResultV0(void) @@ -134,7 +134,10 @@ static void encodeAppendEntries(const struct raft_append_entries *p, void *buf) bytePut64(&cursor, p->prev_log_term); /* Previous log entry term. */ bytePut64(&cursor, p->leader_commit); /* Leader's commit index. */ - uvEncodeBatchHeader(p->entries, p->n_entries, cursor); + uvEncodeBatchHeader(p->entries, p->n_entries, cursor); /* Batch header */ + + cursor = (uint8_t *)cursor + uvSizeofBatchHeader(p->n_entries); + bytePut64(&cursor, 0); /* XXX: currently unused */ } static void encodeAppendEntriesResult( From dc5c4f6b4b7c5025278b560d44ada72d05194ca7 Mon Sep 17 00:00:00 2001 From: Free Ekanayaka Date: Thu, 14 Sep 2023 19:34:18 +0100 Subject: [PATCH 5/5] uv/encoding: Fill the spurious extra 64-bit field of InstallSnapshot There is a spurious 64-bit field in the InstallSnapshot message, that was supposed to contain the Leader ID, but never did (since the sender ID is included in struct raft_message). This commits now fills it with 0, in case we want to re-purpose it later in the future. Signed-off-by: Free Ekanayaka --- src/uv_encoding.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/uv_encoding.c b/src/uv_encoding.c index 1e060f08..ae7af40f 100644 --- a/src/uv_encoding.c +++ b/src/uv_encoding.c @@ -67,13 +67,13 @@ static size_t sizeofInstallSnapshot(const struct raft_install_snapshot *p) { size_t conf_size = configurationEncodedSize(&p->conf); return sizeof(uint64_t) + /* Leader's term. */ - sizeof(uint64_t) + /* Leader ID */ sizeof(uint64_t) + /* Snapshot's last index */ sizeof(uint64_t) + /* Term of last index */ sizeof(uint64_t) + /* Configuration's index */ sizeof(uint64_t) + /* Length of configuration */ conf_size + /* Configuration data */ - sizeof(uint64_t); /* Length of snapshot data */ + sizeof(uint64_t) + /* Length of snapshot data */ + sizeof(uint64_t); /* XXX: currently unused */ } static size_t sizeofTimeoutNow(void) @@ -160,14 +160,18 @@ static void encodeInstallSnapshot(const struct raft_install_snapshot *p, cursor = buf; - bytePut64(&cursor, p->term); /* Leader's term. */ - bytePut64(&cursor, p->last_index); /* Snapshot last index. */ - bytePut64(&cursor, p->last_term); /* Term of last index. */ - bytePut64(&cursor, p->conf_index); /* Configuration index. */ - bytePut64(&cursor, conf_size); /* Configuration length. */ - configurationEncodeToBuf(&p->conf, cursor); + bytePut64(&cursor, p->term); /* Leader's term */ + bytePut64(&cursor, p->last_index); /* Snapshot's last index */ + bytePut64(&cursor, p->last_term); /* Term of last index */ + bytePut64(&cursor, p->conf_index); /* Configuration's index */ + bytePut64(&cursor, conf_size); /* Length of configuration */ + + configurationEncodeToBuf(&p->conf, cursor); /* Configuration data */ cursor = (uint8_t *)cursor + conf_size; - bytePut64(&cursor, p->data.len); /* Snapshot data size. */ + + bytePut64(&cursor, p->data.len); /* Length of snapshot data */ + + bytePut64(&cursor, 0); /* XXX: currently unused */ } static void encodeTimeoutNow(const struct raft_timeout_now *p, void *buf)