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

Commit ac5cf9e

Browse files
author
Mathieu Borderé
committed
raft.h: Change types of raft_term, raft_index, raft_time and raft_id
Most of these types are encoded using 64bits but the old types only promise being at least 64bits long, so in exotic cases information could be lost during encoding. Changed to uint64_t, which has a fixed width.
1 parent eb897b2 commit ac5cf9e

25 files changed

+90
-84
lines changed

include/raft.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef RAFT_H
22
#define RAFT_H
33

4+
#include <inttypes.h>
45
#include <stdarg.h>
56
#include <stdbool.h>
67
#include <stddef.h>
@@ -48,22 +49,22 @@ enum {
4849
*/
4950
RAFT_API const char *raft_strerror(int errnum);
5051

51-
typedef unsigned long long raft_id;
52+
typedef uint64_t raft_id;
5253

5354
/**
54-
* Hold the value of a raft term. Guaranteed to be at least 64-bit long.
55+
* Hold the value of a raft term.
5556
*/
56-
typedef unsigned long long raft_term;
57+
typedef uint64_t raft_term;
5758

5859
/**
59-
* Hold the value of a raft entry index. Guaranteed to be at least 64-bit long.
60+
* Hold the value of a raft entry index.
6061
*/
61-
typedef unsigned long long raft_index;
62+
typedef uint64_t raft_index;
6263

6364
/**
6465
* Hold a time value expressed in milliseconds since the epoch.
6566
*/
66-
typedef unsigned long long raft_time;
67+
typedef uint64_t raft_time;
6768

6869
/**
6970
* A data buffer.

src/client.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int raft_apply(struct raft *r,
3636

3737
/* Index of the first entry being appended. */
3838
index = logLastIndex(r->log) + 1;
39-
tracef("%u commands starting at %lld", n, index);
39+
tracef("%u commands starting at %"PRIu64, n, index);
4040
req->type = RAFT_COMMAND;
4141
req->index = index;
4242
req->cb = cb;
@@ -86,7 +86,7 @@ int raft_barrier(struct raft *r, struct raft_barrier *req, raft_barrier_cb cb)
8686

8787
/* Index of the barrier entry being appended. */
8888
index = logLastIndex(r->log) + 1;
89-
tracef("barrier starting at %lld", index);
89+
tracef("barrier starting at %"PRIu64, index);
9090
req->type = RAFT_BARRIER;
9191
req->index = index;
9292
req->cb = cb;
@@ -180,7 +180,7 @@ int raft_add(struct raft *r,
180180
return rv;
181181
}
182182

183-
tracef("add server: id %llu, address %s", id, address);
183+
tracef("add server: id %"PRIu64", address %s", id, address);
184184

185185
/* Make a copy of the current configuration, and add the new server to
186186
* it. */
@@ -224,7 +224,7 @@ int raft_assign(struct raft *r,
224224
raft_index last_index;
225225
int rv;
226226

227-
tracef("raft_assign to id:%llu the role:%d", id, role);
227+
tracef("raft_assign to id:%"PRIu64" the role:%d", id, role);
228228
if (role != RAFT_STANDBY && role != RAFT_VOTER && role != RAFT_SPARE) {
229229
rv = RAFT_BADROLE;
230230
ErrMsgFromCode(r->errmsg, rv);
@@ -239,7 +239,7 @@ int raft_assign(struct raft *r,
239239
server = configurationGet(&r->configuration, id);
240240
if (server == NULL) {
241241
rv = RAFT_NOTFOUND;
242-
ErrMsgPrintf(r->errmsg, "no server has ID %llu", id);
242+
ErrMsgPrintf(r->errmsg, "no server has ID %"PRIu64, id);
243243
goto err;
244244
}
245245

@@ -305,7 +305,7 @@ int raft_assign(struct raft *r,
305305
rv = replicationProgress(r, server_index);
306306
if (rv != 0 && rv != RAFT_NOCONNECTION) {
307307
/* This error is not fatal. */
308-
tracef("failed to send append entries to server %llu: %s (%d)",
308+
tracef("failed to send append entries to server %"PRIu64": %s (%d)",
309309
server->id, raft_strerror(rv), rv);
310310
}
311311

@@ -336,7 +336,7 @@ int raft_remove(struct raft *r,
336336
goto err;
337337
}
338338

339-
tracef("remove server: id %llu", id);
339+
tracef("remove server: id %"PRIu64, id);
340340

341341
/* Make a copy of the current configuration, and remove the given server
342342
* from it. */
@@ -403,7 +403,7 @@ int raft_transfer(struct raft *r,
403403
unsigned i;
404404
int rv;
405405

406-
tracef("transfer to %llu", id);
406+
tracef("transfer to %016"PRIu64, id);
407407
if (r->state != RAFT_LEADER || r->transfer != NULL) {
408408
tracef("transfer error - state:%d", r->state);
409409
rv = RAFT_NOTLEADER;

src/configuration.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ void configurationTrace(const struct raft *r, struct raft_configuration *c, cons
345345
struct raft_server *s;
346346
for (i = 0; i < c->n; i++) {
347347
s = &c->servers[i];
348-
tracef("id:%llu address:%s role:%d", s->id, s->address, s->role);
348+
tracef("id:%"PRIu64" address:%s role:%d", s->id, s->address, s->role);
349349
}
350350
tracef("=== CONFIG END ===");
351351
}

src/convert.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ int convertToLeader(struct raft *r)
211211
* we are the only voter around. */
212212
size_t n_voters = configurationVoterCount(&r->configuration);
213213
if (n_voters == 1 && (r->last_stored > r->commit_index)) {
214-
tracef("Apply log entries after self election %llu %llu", r->last_stored, r->commit_index);
214+
tracef("Apply log entries after self election last_stored: %"PRIu64" commit_index:%"PRId64, r->last_stored, r->commit_index);
215215
r->commit_index = r->last_stored;
216216
rv = replicationApply(r);
217217
}

src/election.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int electionStart(struct raft *r)
165165
rv = electionSend(r, server);
166166
if (rv != 0) {
167167
/* This is not a critical failure, let's just log it. */
168-
tracef("failed to send vote request to server %llu: %s", server->id,
168+
tracef("failed to send vote request to server %"PRIu64": %s", server->id,
169169
raft_strerror(rv));
170170
}
171171
}
@@ -235,7 +235,7 @@ int electionVote(struct raft *r,
235235
if (args->last_log_term < local_last_term) {
236236
/* The requesting server has last entry's log term lower than ours. */
237237
tracef(
238-
"local last entry %llu has term %llu higher than %llu -> not "
238+
"local last entry %"PRIu64" has term %"PRId64" higher than %"PRId64" -> not "
239239
"granting",
240240
local_last_index, local_last_term, args->last_log_term);
241241
return 0;
@@ -244,7 +244,7 @@ int electionVote(struct raft *r,
244244
if (args->last_log_term > local_last_term) {
245245
/* The requesting server has a more up-to-date log. */
246246
tracef(
247-
"remote last entry %llu has term %llu higher than %llu -> "
247+
"remote last entry %"PRIu64" has term %"PRId64" higher than %"PRId64" -> "
248248
"granting vote",
249249
args->last_log_index, args->last_log_term, local_last_term);
250250
goto grant_vote;
@@ -277,7 +277,7 @@ int electionVote(struct raft *r,
277277
r->election_timer_start = r->io->time(r->io);
278278
}
279279

280-
tracef("vote granted to %llu", args->candidate_id);
280+
tracef("vote granted to %"PRIu64, args->candidate_id);
281281
*granted = true;
282282

283283
return 0;

src/fixture.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ static int serverInit(struct raft_fixture *f, unsigned i, struct raft_fsm *fsm)
990990
f->servers[i] = s;
991991
s->alive = true;
992992
s->id = i + 1;
993-
sprintf(s->address, "%llu", s->id);
993+
sprintf(s->address, "%"PRIu64, s->id);
994994
rv = ioInit(&s->io, i, &f->time);
995995
if (rv != 0) {
996996
return rv;
@@ -1182,7 +1182,7 @@ static bool updateLeaderAndCheckElectionSafety(struct raft_fixture *f)
11821182

11831183
if (other->current_term == raft->current_term) {
11841184
fprintf(stderr,
1185-
"server %llu and %llu are both leaders in term %llu",
1185+
"server %"PRIu64" and %"PRId64" are both leaders in term %"PRId64,
11861186
raft->id, other->id, raft->current_term);
11871187
abort();
11881188
}

src/membership.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ int membershipCanChangeConfiguration(struct raft *r)
2121
}
2222

2323
if (r->configuration_uncommitted_index != 0) {
24-
tracef("r->configuration_uncommitted_index %llu", r->configuration_uncommitted_index);
24+
tracef("r->configuration_uncommitted_index %"PRIu64, r->configuration_uncommitted_index);
2525
rv = RAFT_CANTCHANGE;
2626
goto err;
2727
}
2828

2929
if (r->leader_state.promotee_id != 0) {
30-
tracef("r->leader_state.promotee_id %llu", r->leader_state.promotee_id);
30+
tracef("r->leader_state.promotee_id %"PRIu64, r->leader_state.promotee_id);
3131
rv = RAFT_CANTCHANGE;
3232
goto err;
3333
}
@@ -75,7 +75,7 @@ bool membershipUpdateCatchUpRound(struct raft *r)
7575
/* If the server did not reach the target index for this round, it did not
7676
* catch up. */
7777
if (match_index < r->leader_state.round_index) {
78-
tracef("member (index: %u) not yet caught up match_index:%llu round_index:%llu",
78+
tracef("member (index: %u) not yet caught up match_index:%"PRIu64" round_index:%"PRId64,
7979
server_index, match_index, r->leader_state.round_index);
8080
return false;
8181
}
@@ -125,7 +125,7 @@ int membershipUncommittedChange(struct raft *r,
125125

126126
rv = configurationDecode(&entry->buf, &configuration);
127127
if (rv != 0) {
128-
tracef("failed to decode configuration at index:%llu", index);
128+
tracef("failed to decode configuration at index:%"PRIu64, index);
129129
goto err;
130130
}
131131
configurationTrace(r, &configuration, "uncommitted config change");
@@ -209,7 +209,7 @@ int membershipLeadershipTransferStart(struct raft *r)
209209
r->transfer->send.data = r;
210210
rv = r->io->send(r->io, &r->transfer->send, &message, NULL);
211211
if (rv != 0) {
212-
ErrMsgTransferf(r->io->errmsg, r->errmsg, "send timeout now to %llu",
212+
ErrMsgTransferf(r->io->errmsg, r->errmsg, "send timeout now to %"PRIu64,
213213
server->id);
214214
return rv;
215215
}

src/progress.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ bool progressMaybeDecrement(struct raft *r,
235235
/* The rejection must be stale or spurious if the rejected index does not
236236
* match the next index minus one. */
237237
if (rejected != p->next_index - 1) {
238-
tracef("rejected index %llu different from next index %lld -> ignore ",
238+
tracef("rejected index %"PRIu64" different from next index %"PRId64" -> ignore ",
239239
rejected, p->next_index);
240240
return false;
241241
}

src/recv.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int recvBumpCurrentTerm(struct raft *r, raft_term term)
119119
assert(r != NULL);
120120
assert(term > r->current_term);
121121

122-
sprintf(msg, "remote term %lld is higher than %lld -> bump local term",
122+
sprintf(msg, "remote term %"PRIu64" is higher than %"PRId64" -> bump local term",
123123
term, r->current_term);
124124
if (r->state != RAFT_FOLLOWER) {
125125
strcat(msg, " and step down");
@@ -165,7 +165,7 @@ int recvEnsureMatchingTerms(struct raft *r, raft_term term, int *match)
165165
recvCheckMatchingTerms(r, term, match);
166166

167167
if (*match == -1) {
168-
tracef("old term - current_term:%llu other_term:%llu", r->current_term, term);
168+
tracef("old term - current_term:%"PRIu64" other_term:%"PRId64, r->current_term, term);
169169
return 0;
170170
}
171171

src/recv_append_entries.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ int recvAppendEntries(struct raft *r,
3333
assert(id > 0);
3434
assert(args != NULL);
3535
assert(address != NULL);
36-
tracef("self:%llu from:%llu@%s leader_commit:%llu n_entries:%d prev_log_index:%llu prev_log_term:%llu, term:%llu",
37-
r->id, id, address, args->leader_commit, args->n_entries, args->prev_log_index, args->prev_log_term, args->term);
36+
tracef("self:%"PRIu64" from:%"PRId64"@%s leader_commit:%"PRId64" n_entries:%d "
37+
"prev_log_index:%"PRIu64" prev_log_term:%"PRId64", term:%"PRId64,
38+
r->id, id, address, args->leader_commit, args->n_entries,
39+
args->prev_log_index, args->prev_log_term, args->term);
3840

3941
result->rejected = args->prev_log_index;
4042
result->last_log_index = logLastIndex(r->log);

src/recv_append_entries_result.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int recvAppendEntriesResult(struct raft *r,
2121
assert(address != NULL);
2222
assert(result != NULL);
2323

24-
tracef("self:%llu from:%llu@%s last_log_index:%llu rejected:%llu term:%llu",
24+
tracef("self:%"PRIu64" from:%"PRId64"@%s last_log_index:%"PRId64" rejected:%"PRId64" term:%"PRId64,
2525
r->id, id, address, result->last_log_index, result->rejected, result->term);
2626

2727
if (r->state != RAFT_LEADER) {

src/recv_install_snapshot.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int recvInstallSnapshot(struct raft *r,
2828
bool async;
2929

3030
assert(address != NULL);
31-
tracef("self:%llu from:%llu@%s conf_index:%llu last_index:%llu last_term:%llu term:%llu",
31+
tracef("self:%"PRIu64" from:%"PRId64"@%s conf_index:%"PRId64" last_index:%"PRId64" last_term:%"PRId64" term:%"PRId64,
3232
r->id, id, address, args->conf_index, args->last_index, args->last_term, args->term);
3333

3434
result->rejected = args->last_index;

src/recv_request_vote.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ int recvRequestVote(struct raft *r,
2929
assert(id > 0);
3030
assert(args != NULL);
3131

32-
tracef("self:%llu from:%llu@%s candidate_id:%llu disrupt_leader:%d last_log_index:%llu "
33-
"last_log_term:%llu pre_vote:%d term:%llu",
32+
tracef("self:%"PRIu64" from:%"PRId64"@%s candidate_id:%"PRId64" disrupt_leader:%d last_log_index:%"PRId64
33+
" last_log_term:%"PRIu64" pre_vote:%d term:%"PRId64,
3434
r->id, id, address, args->candidate_id, args->disrupt_leader, args->last_log_index,
3535
args->last_log_term, args->pre_vote, args->term);
3636
result->vote_granted = false;
@@ -89,7 +89,7 @@ int recvRequestVote(struct raft *r,
8989
* same as the request term (otherwise we would have rejected the request or
9090
* bumped our term). */
9191
if (!args->pre_vote) {
92-
tracef("no pre_vote: current_term:%llu term:%llu", r->current_term, args->term);
92+
tracef("no pre_vote: current_term:%"PRIu64" term:%"PRId64, r->current_term, args->term);
9393
assert(r->current_term == args->term);
9494
}
9595

src/recv_request_vote_result.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int recvRequestVoteResult(struct raft *r,
2424
assert(r != NULL);
2525
assert(id > 0);
2626

27-
tracef("self:%llu from:%llu@%s term:%llu vote_granted:%d pre_vote:%d",
27+
tracef("self:%"PRIu64" from:%"PRId64"@%s term:%"PRId64" vote_granted:%d pre_vote:%d",
2828
r->id, id, address, result->term, result->vote_granted, result->pre_vote);
2929
votes_index = configurationIndexOfVoter(&r->configuration, id);
3030
if (votes_index == r->configuration.n) {

src/recv_timeout_now.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ int recvTimeoutNow(struct raft *r,
2626

2727
(void)address;
2828

29-
tracef("self:%llu from:%llu@%s last_log_index:%llu last_log_term:%llu term:%llu", r->id, id, address, args->last_log_index, args->last_log_term, args->term);
29+
tracef("self:%"PRIu64" from:%"PRId64"@%s last_log_index:%"PRId64" last_log_term:%"PRId64" term:%"PRId64,
30+
r->id, id, address, args->last_log_index, args->last_log_term, args->term);
3031
/* Ignore the request if we are not voters. */
3132
local_server = configurationGet(&r->configuration, r->id);
3233
if (local_server == NULL || local_server->role != RAFT_VOTER) {
@@ -38,7 +39,7 @@ int recvTimeoutNow(struct raft *r,
3839
* leader. */
3940
if (r->state != RAFT_FOLLOWER ||
4041
r->follower_state.current_leader.id != id) {
41-
tracef("Ignore - r->state:%d current_leader.id:%llu", r->state, r->follower_state.current_leader.id);
42+
tracef("Ignore - r->state:%d current_leader.id:%"PRIu64, r->state, r->follower_state.current_leader.id);
4243
return 0;
4344
}
4445

0 commit comments

Comments
 (0)