Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit eb897b2

Browse files
author
Mathieu Borderé
committed
raft.h: Future-proof raft_message.
1 parent a35e1f0 commit eb897b2

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

include/raft.h

+23
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ struct raft_request_vote
215215
raft_index last_log_term; /* Term of log entry at last_log_index. */
216216
bool disrupt_leader; /* True if current leader should be discarded. */
217217
bool pre_vote; /* True if this is a pre-vote request. */
218+
uint64_t reserved[8]; /* Future use */
218219
};
219220

220221
/**
@@ -225,6 +226,7 @@ struct raft_request_vote_result
225226
raft_term term; /* Receiver's current term (candidate updates itself). */
226227
bool vote_granted; /* True means candidate received vote. */
227228
raft_tribool pre_vote; /* The response to a pre-vote RequestVote or not. */
229+
uint64_t reserved[8]; /* Future use */
228230
};
229231

230232
/**
@@ -241,6 +243,7 @@ struct raft_append_entries
241243
raft_index leader_commit; /* Leader's commit index. */
242244
struct raft_entry *entries; /* Log entries to append. */
243245
unsigned n_entries; /* Size of the log entries array. */
246+
uint64_t reserved[8]; /* Future use */
244247
};
245248

246249
/**
@@ -251,6 +254,7 @@ struct raft_append_entries_result
251254
raft_term term; /* Receiver's current_term. */
252255
raft_index rejected; /* If non-zero, the index that was rejected. */
253256
raft_index last_log_index; /* Receiver's last log entry index, as hint. */
257+
uint64_t reserved[8]; /* Future use */
254258
};
255259

256260
/**
@@ -264,6 +268,7 @@ struct raft_install_snapshot
264268
struct raft_configuration conf; /* Config as of last_index. */
265269
raft_index conf_index; /* Commit index of conf. */
266270
struct raft_buffer data; /* Raw snapshot data. */
271+
uint64_t reserved[8]; /* Future use */
267272
};
268273

269274
/**
@@ -277,6 +282,7 @@ struct raft_timeout_now
277282
raft_term term; /* Leader's term. */
278283
raft_index last_log_index; /* Index of leader's last log entry. */
279284
raft_index last_log_term; /* Term of log entry at last_log_index. */
285+
uint64_t reserved[8]; /* Future use */
280286
};
281287

282288
/**
@@ -293,6 +299,22 @@ enum {
293299

294300
/**
295301
* A single RPC message that can be sent or received over the network.
302+
*
303+
* Updating to a new version of an RPC message is done based on the encoded size
304+
* of the message, see e.g. `sizeofRequestVoteV1` in uv_encoding.c
305+
*
306+
* The RPC message types all have a `reserved` field. When extending such a
307+
* struct, and thus shrinking the `reserved` array, one should be careful not to
308+
* give meaning the `zero` value because the raft implementation needs to know
309+
* whether to other side knows the new field or not. `zero` means the other side
310+
* does not know the new field/functionality (this is why raft_tribool exists).
311+
* Implementers MUST always zero out unknown fields before passing them to raft.
312+
*
313+
* Adding a new message to the union is supported, as long as its size is
314+
* smaller than the largest struct in the union, which should be plenty.
315+
* TODO: Add a compile-time check to verify this once a new message is actually
316+
* needed.
317+
*
296318
*/
297319
struct raft_message
298320
{
@@ -307,6 +329,7 @@ struct raft_message
307329
struct raft_install_snapshot install_snapshot;
308330
struct raft_timeout_now timeout_now;
309331
};
332+
uint64_t reserved[8]; /* Future use */
310333
};
311334

312335
/**

src/fixture.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static void ioFlushSend(struct io *io, struct send *send)
376376
goto out;
377377
}
378378

379-
transmit = raft_malloc(sizeof *transmit);
379+
transmit = raft_calloc(1, sizeof *transmit);
380380
assert(transmit != NULL);
381381

382382
transmit->type = TRANSMIT;

src/uv_encoding.c

+2
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ int uvDecodeMessage(const unsigned long type,
486486
return RAFT_INVALID;
487487
}
488488

489+
memset(message, 0, sizeof(*message));
490+
489491
message->type = (unsigned short)type;
490492

491493
*payload_len = 0;

0 commit comments

Comments
 (0)