2 * Copyright (c) 2017, 2018 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "raft-private.h"
28 #include "openvswitch/dynamic-string.h"
29 #include "openvswitch/hmap.h"
30 #include "openvswitch/json.h"
31 #include "openvswitch/list.h"
32 #include "openvswitch/poll-loop.h"
33 #include "openvswitch/vlog.h"
34 #include "ovsdb-error.h"
35 #include "ovsdb-parser.h"
36 #include "ovsdb/log.h"
39 #include "socket-util.h"
47 VLOG_DEFINE_THIS_MODULE(raft
);
49 /* Roles for a Raft server:
51 * - Followers: Servers in touch with the current leader.
53 * - Candidate: Servers unaware of a current leader and seeking election to
56 * - Leader: Handles all client requests. At most one at a time.
58 * In normal operation there is exactly one leader and all of the other servers
66 /* Flags for unit tests. */
67 enum raft_failure_test
{
69 FT_CRASH_BEFORE_SEND_APPEND_REQ
,
70 FT_CRASH_AFTER_SEND_APPEND_REQ
,
71 FT_CRASH_BEFORE_SEND_EXEC_REP
,
72 FT_CRASH_AFTER_SEND_EXEC_REP
,
73 FT_CRASH_BEFORE_SEND_EXEC_REQ
,
74 FT_CRASH_AFTER_SEND_EXEC_REQ
,
75 FT_CRASH_AFTER_RECV_APPEND_REQ_UPDATE
,
78 static enum raft_failure_test failure_test
;
80 /* A connection between this Raft server and another one. */
82 struct ovs_list list_node
; /* In struct raft's 'conns' list. */
83 struct jsonrpc_session
*js
; /* JSON-RPC connection. */
84 struct uuid sid
; /* This server's unique ID. */
85 char *nickname
; /* Short name for use in log messages. */
86 bool incoming
; /* True if incoming, false if outgoing. */
87 unsigned int js_seqno
; /* Seqno for noticing (re)connections. */
90 static void raft_conn_close(struct raft_conn
*);
92 /* A "command", that is, a request to append an entry to the log.
94 * The Raft specification only allows clients to issue commands to the leader.
95 * With this implementation, clients may issue a command on any server, which
96 * then relays the command to the leader if necessary.
98 * This structure is thus used in three cases:
100 * 1. We are the leader and the command was issued to us directly.
102 * 2. We are a follower and relayed the command to the leader.
104 * 3. We are the leader and a follower relayed the command to us.
106 struct raft_command
{
108 struct hmap_node hmap_node
; /* In struct raft's 'commands' hmap. */
109 unsigned int n_refs
; /* Reference count. */
110 enum raft_command_status status
; /* Execution status. */
111 struct uuid eid
; /* Entry ID of result. */
114 uint64_t index
; /* Index in log (0 if being relayed). */
117 long long int timestamp
; /* Issue or last ping time, for expiration. */
120 struct uuid sid
; /* The follower (otherwise UUID_ZERO). */
123 static void raft_command_complete(struct raft
*, struct raft_command
*,
124 enum raft_command_status
);
126 static void raft_complete_all_commands(struct raft
*,
127 enum raft_command_status
);
129 /* Type of deferred action, see struct raft_waiter. */
130 enum raft_waiter_type
{
136 /* An action deferred until a log write commits to disk. */
138 struct ovs_list list_node
;
139 uint64_t commit_ticket
;
141 enum raft_waiter_type type
;
145 * Waits for a RAFT_REC_ENTRY write to our local log to commit. Upon
146 * completion, updates 'log_synced' to indicate that the new log entry
147 * or entries are committed and, if we are leader, also updates our
148 * local 'match_index'. */
155 * Waits for a RAFT_REC_TERM or RAFT_REC_VOTE record write to commit.
156 * Upon completion, updates 'synced_term' and 'synced_vote', which
157 * triggers sending RPCs deferred by the uncommitted 'term' and
166 * Sometimes, sending an RPC to a peer must be delayed until an entry,
167 * a term, or a vote mentioned in the RPC is synced to disk. This
168 * waiter keeps a copy of such an RPC until the previous waiters have
174 static struct raft_waiter
*raft_waiter_create(struct raft
*,
175 enum raft_waiter_type
,
177 static void raft_waiters_destroy(struct raft
*);
179 /* The Raft state machine. */
181 struct hmap_node hmap_node
; /* In 'all_rafts'. */
182 struct ovsdb_log
*log
;
184 /* Persistent derived state.
186 * This must be updated on stable storage before responding to RPCs. It can be
187 * derived from the header, snapshot, and log in 'log'. */
189 struct uuid cid
; /* Cluster ID (immutable for the cluster). */
190 struct uuid sid
; /* Server ID (immutable for the server). */
191 char *local_address
; /* Local address (immutable for the server). */
192 char *local_nickname
; /* Used for local server in log messages. */
193 char *name
; /* Schema name (immutable for the cluster). */
195 /* Contains "struct raft_server"s and represents the server configuration
196 * most recently added to 'log'. */
199 #define ELECTION_BASE_MSEC 1000
200 #define ELECTION_RANGE_MSEC 1000
201 /* The election timeout base value for leader election, in milliseconds.
202 * It can be set by unixctl cluster/change-election-timer. Default value is
203 * ELECTION_BASE_MSEC. */
204 uint64_t election_timer
;
205 /* If not 0, it is the new value of election_timer being proposed. */
206 uint64_t election_timer_new
;
208 /* Persistent state on all servers.
210 * Must be updated on stable storage before responding to RPCs. */
212 /* Current term and the vote for that term. These might be on the way to
214 uint64_t term
; /* Initialized to 0 and only increases. */
215 struct uuid vote
; /* All-zeros if no vote yet in 'term'. */
217 /* The term and vote that have been synced to disk. */
218 uint64_t synced_term
;
219 struct uuid synced_vote
;
223 * A log entry with index 1 never really exists; the initial snapshot for a
224 * Raft is considered to include this index. The first real log entry has
227 * A new Raft instance contains an empty log: log_start=2, log_end=2.
228 * Over time, the log grows: log_start=2, log_end=N.
229 * At some point, the server takes a snapshot: log_start=N, log_end=N.
230 * The log continues to grow: log_start=N, log_end=N+1...
232 * Must be updated on stable storage before responding to RPCs. */
233 struct raft_entry
*entries
; /* Log entry i is in log[i - log_start]. */
234 uint64_t log_start
; /* Index of first entry in log. */
235 uint64_t log_end
; /* Index of last entry in log, plus 1. */
236 uint64_t log_synced
; /* Index of last synced entry. */
237 size_t allocated_log
; /* Allocated entries in 'log'. */
239 /* Snapshot state (see Figure 5.1)
241 * This is the state of the cluster as of the last discarded log entry,
242 * that is, at log index 'log_start - 1' (called prevIndex in Figure 5.1).
243 * Only committed log entries can be included in a snapshot. */
244 struct raft_entry snap
;
248 * The snapshot is always committed, but the rest of the log might not be yet.
249 * 'last_applied' tracks what entries have been passed to the client. If the
250 * client hasn't yet read the latest snapshot, then even the snapshot isn't
251 * applied yet. Thus, the invariants are different for these members:
253 * log_start - 2 <= last_applied <= commit_index < log_end.
254 * log_start - 1 <= commit_index < log_end.
257 enum raft_role role
; /* Current role. */
258 uint64_t commit_index
; /* Max log index known to be committed. */
259 uint64_t last_applied
; /* Max log index applied to state machine. */
260 struct uuid leader_sid
; /* Server ID of leader (zero, if unknown). */
262 long long int election_base
; /* Time of last heartbeat from leader. */
263 long long int election_timeout
; /* Time at which we start an election. */
265 /* Used for joining a cluster. */
266 bool joining
; /* Attempting to join the cluster? */
267 struct sset remote_addresses
; /* Addresses to try to find other servers. */
268 long long int join_timeout
; /* Time to re-send add server request. */
270 /* Used for leaving a cluster. */
271 bool leaving
; /* True if we are leaving the cluster. */
272 bool left
; /* True if we have finished leaving. */
273 long long int leave_timeout
; /* Time to re-send remove server request. */
276 bool failed
; /* True if unrecoverable error has occurred. */
278 /* File synchronization. */
279 struct ovs_list waiters
; /* Contains "struct raft_waiter"s. */
281 /* Network connections. */
282 struct pstream
*listener
; /* For connections from other Raft servers. */
283 long long int listen_backoff
; /* For retrying creating 'listener'. */
284 struct ovs_list conns
; /* Contains struct raft_conns. */
286 /* Leaders only. Reinitialized after becoming leader. */
287 struct hmap add_servers
; /* Contains "struct raft_server"s to add. */
288 struct raft_server
*remove_server
; /* Server being removed. */
289 struct hmap commands
; /* Contains "struct raft_command"s. */
290 long long int ping_timeout
; /* Time at which to send a heartbeat */
292 /* Candidates only. Reinitialized at start of election. */
293 int n_votes
; /* Number of votes for me. */
295 /* Followers and candidates only. */
296 bool candidate_retrying
; /* The earlier election timed-out and we are
298 bool had_leader
; /* There has been leader elected since last
299 election initiated. This is to help setting
300 candidate_retrying. */
303 /* All Raft structures. */
304 static struct hmap all_rafts
= HMAP_INITIALIZER(&all_rafts
);
306 static void raft_init(void);
308 static struct ovsdb_error
*raft_read_header(struct raft
*)
309 OVS_WARN_UNUSED_RESULT
;
311 static void raft_send_execute_command_reply(struct raft
*,
312 const struct uuid
*sid
,
313 const struct uuid
*eid
,
314 enum raft_command_status
,
315 uint64_t commit_index
);
317 static void raft_update_our_match_index(struct raft
*, uint64_t min_index
);
319 static void raft_send_remove_server_reply__(
320 struct raft
*, const struct uuid
*target_sid
,
321 const struct uuid
*requester_sid
, struct unixctl_conn
*requester_conn
,
322 bool success
, const char *comment
);
323 static void raft_finished_leaving_cluster(struct raft
*);
325 static void raft_server_init_leader(struct raft
*, struct raft_server
*);
327 static bool raft_rpc_is_heartbeat(const union raft_rpc
*);
328 static bool raft_is_rpc_synced(const struct raft
*, const union raft_rpc
*);
330 static void raft_handle_rpc(struct raft
*, const union raft_rpc
*);
332 static bool raft_send_at(struct raft
*, const union raft_rpc
*,
334 #define raft_send(raft, rpc) raft_send_at(raft, rpc, __LINE__)
336 static bool raft_send_to_conn_at(struct raft
*, const union raft_rpc
*,
337 struct raft_conn
*, int line_number
);
338 #define raft_send_to_conn(raft, rpc, conn) \
339 raft_send_to_conn_at(raft, rpc, conn, __LINE__)
341 static void raft_send_append_request(struct raft
*,
342 struct raft_server
*, unsigned int n
,
343 const char *comment
);
345 static void raft_become_leader(struct raft
*);
346 static void raft_become_follower(struct raft
*);
347 static void raft_reset_election_timer(struct raft
*);
348 static void raft_reset_ping_timer(struct raft
*);
349 static void raft_send_heartbeats(struct raft
*);
350 static void raft_start_election(struct raft
*, bool leadership_transfer
);
351 static bool raft_truncate(struct raft
*, uint64_t new_end
);
352 static void raft_get_servers_from_log(struct raft
*, enum vlog_level
);
353 static void raft_get_election_timer_from_log(struct raft
*);
355 static bool raft_handle_write_error(struct raft
*, struct ovsdb_error
*);
357 static void raft_run_reconfigure(struct raft
*);
359 static void raft_set_leader(struct raft
*, const struct uuid
*sid
);
360 static struct raft_server
*
361 raft_find_server(const struct raft
*raft
, const struct uuid
*sid
)
363 return raft_server_find(&raft
->servers
, sid
);
367 raft_make_address_passive(const char *address_
)
369 if (!strncmp(address_
, "unix:", 5)) {
370 return xasprintf("p%s", address_
);
372 char *address
= xstrdup(address_
);
374 inet_parse_host_port_tokens(strchr(address
, ':') + 1, &host
, &port
);
376 struct ds paddr
= DS_EMPTY_INITIALIZER
;
377 ds_put_format(&paddr
, "p%.3s:%s:", address
, port
);
378 if (strchr(host
, ':')) {
379 ds_put_format(&paddr
, "[%s]", host
);
381 ds_put_cstr(&paddr
, host
);
384 return ds_steal_cstr(&paddr
);
393 struct raft
*raft
= xzalloc(sizeof *raft
);
394 hmap_node_nullify(&raft
->hmap_node
);
395 hmap_init(&raft
->servers
);
396 raft
->log_start
= raft
->log_end
= 1;
397 raft
->role
= RAFT_FOLLOWER
;
398 sset_init(&raft
->remote_addresses
);
399 raft
->join_timeout
= LLONG_MAX
;
400 ovs_list_init(&raft
->waiters
);
401 raft
->listen_backoff
= LLONG_MIN
;
402 ovs_list_init(&raft
->conns
);
403 hmap_init(&raft
->add_servers
);
404 hmap_init(&raft
->commands
);
406 raft
->election_timer
= ELECTION_BASE_MSEC
;
411 /* Creates an on-disk file that represents a new Raft cluster and initializes
412 * it to consist of a single server, the one on which this function is called.
414 * Creates the local copy of the cluster's log in 'file_name', which must not
415 * already exist. Gives it the name 'name', which should be the database
416 * schema name and which is used only to match up this database with the server
417 * added to the cluster later if the cluster ID is unavailable.
419 * The new server is located at 'local_address', which must take one of the
420 * forms "tcp:IP:PORT" or "ssl:IP:PORT", where IP is an IPv4 address or a
421 * square bracket enclosed IPv6 address and PORT is a TCP port number.
423 * This only creates the on-disk file. Use raft_open() to start operating the
426 * Returns null if successful, otherwise an ovsdb_error describing the
428 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
429 raft_create_cluster(const char *file_name
, const char *name
,
430 const char *local_address
, const struct json
*data
)
432 /* Parse and verify validity of the local address. */
433 struct ovsdb_error
*error
= raft_address_validate(local_address
);
438 /* Create log file. */
439 struct ovsdb_log
*log
;
440 error
= ovsdb_log_open(file_name
, RAFT_MAGIC
, OVSDB_LOG_CREATE_EXCL
,
446 /* Write log file. */
447 struct raft_header h
= {
448 .sid
= uuid_random(),
449 .cid
= uuid_random(),
450 .name
= xstrdup(name
),
451 .local_address
= xstrdup(local_address
),
453 .remote_addresses
= SSET_INITIALIZER(&h
.remote_addresses
),
457 .data
= json_nullable_clone(data
),
458 .eid
= uuid_random(),
459 .servers
= json_object_create(),
462 shash_add_nocopy(json_object(h
.snap
.servers
),
463 xasprintf(UUID_FMT
, UUID_ARGS(&h
.sid
)),
464 json_string_create(local_address
));
465 error
= ovsdb_log_write_and_free(log
, raft_header_to_json(&h
));
466 raft_header_uninit(&h
);
468 error
= ovsdb_log_commit_block(log
);
470 ovsdb_log_close(log
);
475 /* Creates a database file that represents a new server in an existing Raft
478 * Creates the local copy of the cluster's log in 'file_name', which must not
479 * already exist. Gives it the name 'name', which must be the same name
480 * passed in to raft_create_cluster() earlier.
482 * 'cid' is optional. If specified, the new server will join only the cluster
483 * with the given cluster ID.
485 * The new server is located at 'local_address', which must take one of the
486 * forms "tcp:IP:PORT" or "ssl:IP:PORT", where IP is an IPv4 address or a
487 * square bracket enclosed IPv6 address and PORT is a TCP port number.
489 * Joining the cluster requires contacting it. Thus, 'remote_addresses'
490 * specifies the addresses of existing servers in the cluster. One server out
491 * of the existing cluster is sufficient, as long as that server is reachable
492 * and not partitioned from the current cluster leader. If multiple servers
493 * from the cluster are specified, then it is sufficient for any of them to
494 * meet this criterion.
496 * This only creates the on-disk file and does no network access. Use
497 * raft_open() to start operating the new server. (Until this happens, the
498 * new server has not joined the cluster.)
500 * Returns null if successful, otherwise an ovsdb_error describing the
502 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
503 raft_join_cluster(const char *file_name
,
504 const char *name
, const char *local_address
,
505 const struct sset
*remote_addresses
,
506 const struct uuid
*cid
)
508 ovs_assert(!sset_is_empty(remote_addresses
));
510 /* Parse and verify validity of the addresses. */
511 struct ovsdb_error
*error
= raft_address_validate(local_address
);
516 SSET_FOR_EACH (addr
, remote_addresses
) {
517 error
= raft_address_validate(addr
);
521 if (!strcmp(addr
, local_address
)) {
522 return ovsdb_error(NULL
, "remote addresses cannot be the same "
523 "as the local address");
527 /* Verify validity of the cluster ID (if provided). */
528 if (cid
&& uuid_is_zero(cid
)) {
529 return ovsdb_error(NULL
, "all-zero UUID is not valid cluster ID");
532 /* Create log file. */
533 struct ovsdb_log
*log
;
534 error
= ovsdb_log_open(file_name
, RAFT_MAGIC
, OVSDB_LOG_CREATE_EXCL
,
540 /* Write log file. */
541 struct raft_header h
= {
542 .sid
= uuid_random(),
543 .cid
= cid
? *cid
: UUID_ZERO
,
544 .name
= xstrdup(name
),
545 .local_address
= xstrdup(local_address
),
547 /* No snapshot yet. */
549 sset_clone(&h
.remote_addresses
, remote_addresses
);
550 error
= ovsdb_log_write_and_free(log
, raft_header_to_json(&h
));
551 raft_header_uninit(&h
);
553 error
= ovsdb_log_commit_block(log
);
555 ovsdb_log_close(log
);
560 /* Reads the initial header record from 'log', which must be a Raft clustered
561 * database log, and populates '*md' with the information read from it. The
562 * caller must eventually destroy 'md' with raft_metadata_destroy().
564 * On success, returns NULL. On failure, returns an error that the caller must
565 * eventually destroy and zeros '*md'. */
566 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
567 raft_read_metadata(struct ovsdb_log
*log
, struct raft_metadata
*md
)
569 struct raft
*raft
= raft_alloc();
572 struct ovsdb_error
*error
= raft_read_header(raft
);
575 md
->name
= xstrdup(raft
->name
);
576 md
->local
= xstrdup(raft
->local_address
);
579 memset(md
, 0, sizeof *md
);
587 /* Frees the metadata in 'md'. */
589 raft_metadata_destroy(struct raft_metadata
*md
)
597 static const struct raft_entry
*
598 raft_get_entry(const struct raft
*raft
, uint64_t index
)
600 ovs_assert(index
>= raft
->log_start
);
601 ovs_assert(index
< raft
->log_end
);
602 return &raft
->entries
[index
- raft
->log_start
];
606 raft_get_term(const struct raft
*raft
, uint64_t index
)
608 return (index
== raft
->log_start
- 1
610 : raft_get_entry(raft
, index
)->term
);
613 static const struct json
*
614 raft_servers_for_index(const struct raft
*raft
, uint64_t index
)
616 ovs_assert(index
>= raft
->log_start
- 1);
617 ovs_assert(index
< raft
->log_end
);
619 const struct json
*servers
= raft
->snap
.servers
;
620 for (uint64_t i
= raft
->log_start
; i
<= index
; i
++) {
621 const struct raft_entry
*e
= raft_get_entry(raft
, i
);
623 servers
= e
->servers
;
630 raft_set_servers(struct raft
*raft
, const struct hmap
*new_servers
,
631 enum vlog_level level
)
633 struct raft_server
*s
, *next
;
634 HMAP_FOR_EACH_SAFE (s
, next
, hmap_node
, &raft
->servers
) {
635 if (!raft_server_find(new_servers
, &s
->sid
)) {
636 ovs_assert(s
!= raft
->remove_server
);
638 hmap_remove(&raft
->servers
, &s
->hmap_node
);
639 VLOG(level
, "server %s removed from configuration", s
->nickname
);
640 raft_server_destroy(s
);
644 HMAP_FOR_EACH_SAFE (s
, next
, hmap_node
, new_servers
) {
645 if (!raft_find_server(raft
, &s
->sid
)) {
646 VLOG(level
, "server %s added to configuration", s
->nickname
);
648 struct raft_server
*new
649 = raft_server_add(&raft
->servers
, &s
->sid
, s
->address
);
650 raft_server_init_leader(raft
, new);
656 raft_add_entry(struct raft
*raft
,
657 uint64_t term
, struct json
*data
, const struct uuid
*eid
,
658 struct json
*servers
, uint64_t election_timer
)
660 if (raft
->log_end
- raft
->log_start
>= raft
->allocated_log
) {
661 raft
->entries
= x2nrealloc(raft
->entries
, &raft
->allocated_log
,
662 sizeof *raft
->entries
);
665 uint64_t index
= raft
->log_end
++;
666 struct raft_entry
*entry
= &raft
->entries
[index
- raft
->log_start
];
669 entry
->eid
= eid
? *eid
: UUID_ZERO
;
670 entry
->servers
= servers
;
671 entry
->election_timer
= election_timer
;
675 /* Writes a RAFT_REC_ENTRY record for 'term', 'data', 'eid', 'servers',
676 * 'election_timer' to * 'raft''s log and returns an error indication. */
677 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
678 raft_write_entry(struct raft
*raft
, uint64_t term
, struct json
*data
,
679 const struct uuid
*eid
, struct json
*servers
,
680 uint64_t election_timer
)
682 struct raft_record r
= {
683 .type
= RAFT_REC_ENTRY
,
686 .index
= raft_add_entry(raft
, term
, data
, eid
, servers
,
690 .election_timer
= election_timer
,
691 .eid
= eid
? *eid
: UUID_ZERO
,
694 return ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
));
697 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
698 raft_write_state(struct ovsdb_log
*log
,
699 uint64_t term
, const struct uuid
*vote
)
701 struct raft_record r
= { .term
= term
};
702 if (vote
&& !uuid_is_zero(vote
)) {
703 r
.type
= RAFT_REC_VOTE
;
706 r
.type
= RAFT_REC_TERM
;
708 return ovsdb_log_write_and_free(log
, raft_record_to_json(&r
));
711 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
712 raft_apply_record(struct raft
*raft
, unsigned long long int rec_idx
,
713 const struct raft_record
*r
)
715 /* Apply "term", which is present in most kinds of records (and otherwise
718 * A Raft leader can replicate entries from previous terms to the other
719 * servers in the cluster, retaining the original terms on those entries
720 * (see section 3.6.2 "Committing entries from previous terms" for more
721 * information), so it's OK for the term in a log record to precede the
723 if (r
->term
> raft
->term
) {
724 raft
->term
= raft
->synced_term
= r
->term
;
725 raft
->vote
= raft
->synced_vote
= UUID_ZERO
;
730 if (r
->entry
.index
< raft
->commit_index
) {
731 return ovsdb_error(NULL
, "record %llu attempts to truncate log "
732 "from %"PRIu64
" to %"PRIu64
" entries, but "
733 "commit index is already %"PRIu64
,
734 rec_idx
, raft
->log_end
, r
->entry
.index
,
736 } else if (r
->entry
.index
> raft
->log_end
) {
737 return ovsdb_error(NULL
, "record %llu with index %"PRIu64
" skips "
738 "past expected index %"PRIu64
,
739 rec_idx
, r
->entry
.index
, raft
->log_end
);
742 if (r
->entry
.index
< raft
->log_end
) {
743 /* This can happen, but it is notable. */
744 VLOG_DBG("record %llu truncates log from %"PRIu64
" to %"PRIu64
745 " entries", rec_idx
, raft
->log_end
, r
->entry
.index
);
746 raft_truncate(raft
, r
->entry
.index
);
749 uint64_t prev_term
= (raft
->log_end
> raft
->log_start
750 ? raft
->entries
[raft
->log_end
751 - raft
->log_start
- 1].term
753 if (r
->term
< prev_term
) {
754 return ovsdb_error(NULL
, "record %llu with index %"PRIu64
" term "
755 "%"PRIu64
" precedes previous entry's term "
757 rec_idx
, r
->entry
.index
, r
->term
, prev_term
);
760 raft
->log_synced
= raft_add_entry(
762 json_nullable_clone(r
->entry
.data
), &r
->entry
.eid
,
763 json_nullable_clone(r
->entry
.servers
),
764 r
->entry
.election_timer
);
771 if (r
->term
< raft
->term
) {
772 return ovsdb_error(NULL
, "record %llu votes for term %"PRIu64
" "
773 "but current term is %"PRIu64
,
774 rec_idx
, r
->term
, raft
->term
);
775 } else if (!uuid_is_zero(&raft
->vote
)
776 && !uuid_equals(&raft
->vote
, &r
->sid
)) {
777 return ovsdb_error(NULL
, "record %llu votes for "SID_FMT
" in term "
778 "%"PRIu64
" but a previous record for the "
779 "same term voted for "SID_FMT
, rec_idx
,
780 SID_ARGS(&raft
->vote
), r
->term
,
783 raft
->vote
= raft
->synced_vote
= r
->sid
;
789 if (!strcmp(r
->note
, "left")) {
790 return ovsdb_error(NULL
, "record %llu indicates server has left "
791 "the cluster; it cannot be added back (use "
792 "\"ovsdb-tool join-cluster\" to add a new "
797 case RAFT_REC_COMMIT_INDEX
:
798 if (r
->commit_index
< raft
->commit_index
) {
799 return ovsdb_error(NULL
, "record %llu regresses commit index "
800 "from %"PRIu64
" to %"PRIu64
,
801 rec_idx
, raft
->commit_index
, r
->commit_index
);
802 } else if (r
->commit_index
>= raft
->log_end
) {
803 return ovsdb_error(NULL
, "record %llu advances commit index to "
804 "%"PRIu64
" but last log index is %"PRIu64
,
805 rec_idx
, r
->commit_index
, raft
->log_end
- 1);
807 raft
->commit_index
= r
->commit_index
;
812 case RAFT_REC_LEADER
:
813 /* XXX we could use this to take back leadership for quick restart */
821 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
822 raft_read_header(struct raft
*raft
)
824 /* Read header record. */
826 struct ovsdb_error
*error
= ovsdb_log_read(raft
->log
, &json
);
827 if (error
|| !json
) {
828 /* Report error or end-of-file. */
831 ovsdb_log_mark_base(raft
->log
);
833 struct raft_header h
;
834 error
= raft_header_from_json(&h
, json
);
842 raft
->name
= xstrdup(h
.name
);
843 raft
->local_address
= xstrdup(h
.local_address
);
844 raft
->local_nickname
= raft_address_to_nickname(h
.local_address
, &h
.sid
);
845 raft
->joining
= h
.joining
;
848 sset_clone(&raft
->remote_addresses
, &h
.remote_addresses
);
850 raft_entry_clone(&raft
->snap
, &h
.snap
);
851 raft
->log_start
= raft
->log_end
= h
.snap_index
+ 1;
852 raft
->commit_index
= h
.snap_index
;
853 raft
->last_applied
= h
.snap_index
- 1;
856 raft_header_uninit(&h
);
861 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
862 raft_read_log(struct raft
*raft
)
864 for (unsigned long long int i
= 1; ; i
++) {
866 struct ovsdb_error
*error
= ovsdb_log_read(raft
->log
, &json
);
869 /* We assume that the error is due to a partial write while
870 * appending to the file before a crash, so log it and
872 char *error_string
= ovsdb_error_to_string_free(error
);
873 VLOG_WARN("%s", error_string
);
880 struct raft_record r
;
881 error
= raft_record_from_json(&r
, json
);
883 error
= raft_apply_record(raft
, i
, &r
);
884 raft_record_uninit(&r
);
888 return ovsdb_wrap_error(error
, "error reading record %llu from "
889 "%s log", i
, raft
->name
);
893 /* Set the most recent servers. */
894 raft_get_servers_from_log(raft
, VLL_DBG
);
896 /* Set the most recent election_timer. */
897 raft_get_election_timer_from_log(raft
);
903 raft_reset_election_timer(struct raft
*raft
)
905 unsigned int duration
= (raft
->election_timer
906 + random_range(ELECTION_RANGE_MSEC
));
907 raft
->election_base
= time_msec();
908 if (failure_test
== FT_DELAY_ELECTION
) {
909 /* Slow down this node so that it won't win the next election. */
910 duration
+= raft
->election_timer
;
912 raft
->election_timeout
= raft
->election_base
+ duration
;
916 raft_reset_ping_timer(struct raft
*raft
)
918 raft
->ping_timeout
= time_msec() + raft
->election_timer
/ 3;
922 raft_add_conn(struct raft
*raft
, struct jsonrpc_session
*js
,
923 const struct uuid
*sid
, bool incoming
)
925 struct raft_conn
*conn
= xzalloc(sizeof *conn
);
926 ovs_list_push_back(&raft
->conns
, &conn
->list_node
);
931 conn
->nickname
= raft_address_to_nickname(jsonrpc_session_get_name(js
),
933 conn
->incoming
= incoming
;
934 conn
->js_seqno
= jsonrpc_session_get_seqno(conn
->js
);
937 /* Starts the local server in an existing Raft cluster, using the local copy of
938 * the cluster's log in 'file_name'. Takes ownership of 'log', whether
939 * successful or not. */
940 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
941 raft_open(struct ovsdb_log
*log
, struct raft
**raftp
)
943 struct raft
*raft
= raft_alloc();
946 struct ovsdb_error
*error
= raft_read_header(raft
);
951 if (!raft
->joining
) {
952 error
= raft_read_log(raft
);
957 /* Find our own server. */
958 if (!raft_find_server(raft
, &raft
->sid
)) {
959 error
= ovsdb_error(NULL
, "server does not belong to cluster");
963 /* If there's only one server, start an election right away so that the
964 * cluster bootstraps quickly. */
965 if (hmap_count(&raft
->servers
) == 1) {
966 raft_start_election(raft
, false);
969 raft
->join_timeout
= time_msec() + 1000;
972 raft_reset_ping_timer(raft
);
973 raft_reset_election_timer(raft
);
976 hmap_insert(&all_rafts
, &raft
->hmap_node
, hash_string(raft
->name
, 0));
985 /* Returns the name of 'raft', which in OVSDB is the database schema name. */
987 raft_get_name(const struct raft
*raft
)
992 /* Returns the cluster ID of 'raft'. If 'raft' has not yet completed joining
993 * its cluster, then 'cid' will be all-zeros (unless the administrator
994 * specified a cluster ID running "ovsdb-tool join-cluster").
996 * Each cluster has a unique cluster ID. */
998 raft_get_cid(const struct raft
*raft
)
1003 /* Returns the server ID of 'raft'. Each server has a unique server ID. */
1005 raft_get_sid(const struct raft
*raft
)
1010 /* Returns true if 'raft' has completed joining its cluster, has not left or
1011 * initiated leaving the cluster, does not have failed disk storage, and is
1012 * apparently connected to the leader in a healthy way (or is itself the
1015 * If 'raft' is candidate:
1016 * a) if it is the first round of election, consider it as connected, hoping
1017 * it will successfully elect a new leader soon.
1018 * b) if it is already retrying, consider it as disconnected (so that clients
1019 * may decide to reconnect to other members). */
1021 raft_is_connected(const struct raft
*raft
)
1023 bool ret
= (!raft
->candidate_retrying
1028 VLOG_DBG("raft_is_connected: %s\n", ret
? "true": "false");
1032 /* Returns true if 'raft' is the cluster leader. */
1034 raft_is_leader(const struct raft
*raft
)
1036 return raft
->role
== RAFT_LEADER
;
1039 /* Returns true if 'raft' is the process of joining its cluster. */
1041 raft_is_joining(const struct raft
*raft
)
1043 return raft
->joining
;
1046 /* Only returns *connected* connections. */
1047 static struct raft_conn
*
1048 raft_find_conn_by_sid(struct raft
*raft
, const struct uuid
*sid
)
1050 if (!uuid_is_zero(sid
)) {
1051 struct raft_conn
*conn
;
1052 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1053 if (uuid_equals(sid
, &conn
->sid
)
1054 && jsonrpc_session_is_connected(conn
->js
)) {
1062 static struct raft_conn
*
1063 raft_find_conn_by_address(struct raft
*raft
, const char *address
)
1065 struct raft_conn
*conn
;
1066 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1067 if (!strcmp(jsonrpc_session_get_name(conn
->js
), address
)) {
1074 static void OVS_PRINTF_FORMAT(3, 4)
1075 raft_record_note(struct raft
*raft
, const char *note
,
1076 const char *comment_format
, ...)
1079 va_start(args
, comment_format
);
1080 char *comment
= xvasprintf(comment_format
, args
);
1083 struct raft_record r
= {
1084 .type
= RAFT_REC_NOTE
,
1086 .note
= CONST_CAST(char *, note
),
1088 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
1093 /* If we're leader, try to transfer leadership to another server, logging
1094 * 'reason' as the human-readable reason (it should be a phrase suitable for
1095 * following "because") . */
1097 raft_transfer_leadership(struct raft
*raft
, const char *reason
)
1099 if (raft
->role
!= RAFT_LEADER
) {
1103 struct raft_server
*s
;
1104 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
1105 if (!uuid_equals(&raft
->sid
, &s
->sid
)
1106 && s
->phase
== RAFT_PHASE_STABLE
) {
1107 struct raft_conn
*conn
= raft_find_conn_by_sid(raft
, &s
->sid
);
1112 union raft_rpc rpc
= {
1115 .comment
= CONST_CAST(char *, reason
),
1116 .type
= RAFT_RPC_BECOME_LEADER
,
1122 raft_send_to_conn(raft
, &rpc
, conn
);
1124 raft_record_note(raft
, "transfer leadership",
1125 "transferring leadership to %s because %s",
1126 s
->nickname
, reason
);
1132 /* Send a RemoveServerRequest to the rest of the servers in the cluster.
1134 * If we know which server is the leader, we can just send the request to it.
1135 * However, we might not know which server is the leader, and we might never
1136 * find out if the remove request was actually previously committed by a
1137 * majority of the servers (because in that case the new leader will not send
1138 * AppendRequests or heartbeats to us). Therefore, we instead send
1139 * RemoveRequests to every server. This theoretically has the same problem, if
1140 * the current cluster leader was not previously a member of the cluster, but
1141 * it seems likely to be more robust in practice. */
1143 raft_send_remove_server_requests(struct raft
*raft
)
1145 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
1146 VLOG_INFO_RL(&rl
, "sending remove request (joining=%s, leaving=%s)",
1147 raft
->joining
? "true" : "false",
1148 raft
->leaving
? "true" : "false");
1149 const struct raft_server
*s
;
1150 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
1151 if (!uuid_equals(&s
->sid
, &raft
->sid
)) {
1152 union raft_rpc rpc
= (union raft_rpc
) {
1153 .remove_server_request
= {
1155 .type
= RAFT_RPC_REMOVE_SERVER_REQUEST
,
1161 raft_send(raft
, &rpc
);
1165 raft
->leave_timeout
= time_msec() + raft
->election_timer
;
1168 /* Attempts to start 'raft' leaving its cluster. The caller can check progress
1169 * using raft_is_leaving() and raft_left(). */
1171 raft_leave(struct raft
*raft
)
1173 if (raft
->joining
|| raft
->failed
|| raft
->leaving
|| raft
->left
) {
1176 VLOG_INFO(SID_FMT
": starting to leave cluster "CID_FMT
,
1177 SID_ARGS(&raft
->sid
), CID_ARGS(&raft
->cid
));
1178 raft
->leaving
= true;
1179 raft_transfer_leadership(raft
, "this server is leaving the cluster");
1180 raft_become_follower(raft
);
1181 raft_send_remove_server_requests(raft
);
1182 raft
->leave_timeout
= time_msec() + raft
->election_timer
;
1185 /* Returns true if 'raft' is currently attempting to leave its cluster. */
1187 raft_is_leaving(const struct raft
*raft
)
1189 return raft
->leaving
;
1192 /* Returns true if 'raft' successfully left its cluster. */
1194 raft_left(const struct raft
*raft
)
1199 /* Returns true if 'raft' has experienced a disk I/O failure. When this
1200 * returns true, only closing and reopening 'raft' allows for recovery. */
1202 raft_failed(const struct raft
*raft
)
1204 return raft
->failed
;
1207 /* Forces 'raft' to attempt to take leadership of the cluster by deposing the
1208 * current cluster. */
1210 raft_take_leadership(struct raft
*raft
)
1212 if (raft
->role
!= RAFT_LEADER
) {
1213 raft_start_election(raft
, true);
1217 /* Closes everything owned by 'raft' that might be visible outside the process:
1218 * network connections, commands, etc. This is part of closing 'raft'; it is
1219 * also used if 'raft' has failed in an unrecoverable way. */
1221 raft_close__(struct raft
*raft
)
1223 if (!hmap_node_is_null(&raft
->hmap_node
)) {
1224 hmap_remove(&all_rafts
, &raft
->hmap_node
);
1225 hmap_node_nullify(&raft
->hmap_node
);
1228 raft_complete_all_commands(raft
, RAFT_CMD_SHUTDOWN
);
1230 struct raft_server
*rs
= raft
->remove_server
;
1232 raft_send_remove_server_reply__(raft
, &rs
->sid
, &rs
->requester_sid
,
1233 rs
->requester_conn
, false,
1234 RAFT_SERVER_SHUTDOWN
);
1235 raft_server_destroy(raft
->remove_server
);
1236 raft
->remove_server
= NULL
;
1239 struct raft_conn
*conn
, *next
;
1240 LIST_FOR_EACH_SAFE (conn
, next
, list_node
, &raft
->conns
) {
1241 raft_conn_close(conn
);
1245 /* Closes and frees 'raft'.
1247 * A server's cluster membership is independent of whether the server is
1248 * actually running. When a server that is a member of a cluster closes, the
1249 * cluster treats this as a server failure. */
1251 raft_close(struct raft
*raft
)
1257 raft_transfer_leadership(raft
, "this server is shutting down");
1261 ovsdb_log_close(raft
->log
);
1263 raft_servers_destroy(&raft
->servers
);
1265 for (uint64_t index
= raft
->log_start
; index
< raft
->log_end
; index
++) {
1266 struct raft_entry
*e
= &raft
->entries
[index
- raft
->log_start
];
1267 raft_entry_uninit(e
);
1269 free(raft
->entries
);
1271 raft_entry_uninit(&raft
->snap
);
1273 raft_waiters_destroy(raft
);
1275 raft_servers_destroy(&raft
->add_servers
);
1277 hmap_destroy(&raft
->commands
);
1279 pstream_close(raft
->listener
);
1281 sset_destroy(&raft
->remote_addresses
);
1282 free(raft
->local_address
);
1283 free(raft
->local_nickname
);
1290 raft_conn_receive(struct raft
*raft
, struct raft_conn
*conn
,
1291 union raft_rpc
*rpc
)
1293 struct jsonrpc_msg
*msg
= jsonrpc_session_recv(conn
->js
);
1298 struct ovsdb_error
*error
= raft_rpc_from_jsonrpc(&raft
->cid
, &raft
->sid
,
1300 jsonrpc_msg_destroy(msg
);
1302 char *s
= ovsdb_error_to_string_free(error
);
1303 VLOG_INFO("%s: %s", jsonrpc_session_get_name(conn
->js
), s
);
1308 if (uuid_is_zero(&conn
->sid
)) {
1309 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(50, 50);
1310 conn
->sid
= rpc
->common
.sid
;
1311 VLOG_INFO_RL(&rl
, "%s: learned server ID "SID_FMT
,
1312 jsonrpc_session_get_name(conn
->js
), SID_ARGS(&conn
->sid
));
1313 } else if (!uuid_equals(&conn
->sid
, &rpc
->common
.sid
)) {
1314 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1315 VLOG_WARN_RL(&rl
, "%s: ignoring message with unexpected server ID "
1316 SID_FMT
" (expected "SID_FMT
")",
1317 jsonrpc_session_get_name(conn
->js
),
1318 SID_ARGS(&rpc
->common
.sid
), SID_ARGS(&conn
->sid
));
1319 raft_rpc_uninit(rpc
);
1323 const char *address
= (rpc
->type
== RAFT_RPC_HELLO_REQUEST
1324 ? rpc
->hello_request
.address
1325 : rpc
->type
== RAFT_RPC_ADD_SERVER_REQUEST
1326 ? rpc
->add_server_request
.address
1329 char *new_nickname
= raft_address_to_nickname(address
, &conn
->sid
);
1330 if (strcmp(conn
->nickname
, new_nickname
)) {
1331 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(50, 50);
1332 VLOG_INFO_RL(&rl
, "%s: learned remote address %s",
1333 jsonrpc_session_get_name(conn
->js
), address
);
1335 free(conn
->nickname
);
1336 conn
->nickname
= new_nickname
;
1346 raft_get_nickname(const struct raft
*raft
, const struct uuid
*sid
,
1347 char buf
[SID_LEN
+ 1], size_t bufsize
)
1349 if (uuid_equals(sid
, &raft
->sid
)) {
1350 return raft
->local_nickname
;
1353 const char *s
= raft_servers_get_nickname__(&raft
->servers
, sid
);
1358 return raft_servers_get_nickname(&raft
->add_servers
, sid
, buf
, bufsize
);
1362 log_rpc(const union raft_rpc
*rpc
, const char *direction
,
1363 const struct raft_conn
*conn
, int line_number
)
1365 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(600, 600);
1366 if (!raft_rpc_is_heartbeat(rpc
) && !VLOG_DROP_DBG(&rl
)) {
1367 struct ds s
= DS_EMPTY_INITIALIZER
;
1369 ds_put_format(&s
, "raft.c:%d ", line_number
);
1371 ds_put_format(&s
, "%s%s ", direction
, conn
->nickname
);
1372 raft_rpc_format(rpc
, &s
);
1373 VLOG_DBG("%s", ds_cstr(&s
));
1379 raft_send_add_server_request(struct raft
*raft
, struct raft_conn
*conn
)
1381 union raft_rpc rq
= {
1382 .add_server_request
= {
1384 .type
= RAFT_RPC_ADD_SERVER_REQUEST
,
1388 .address
= raft
->local_address
,
1391 raft_send_to_conn(raft
, &rq
, conn
);
1395 raft_conn_run(struct raft
*raft
, struct raft_conn
*conn
)
1397 jsonrpc_session_run(conn
->js
);
1399 unsigned int new_seqno
= jsonrpc_session_get_seqno(conn
->js
);
1400 bool just_connected
= (new_seqno
!= conn
->js_seqno
1401 && jsonrpc_session_is_connected(conn
->js
));
1402 conn
->js_seqno
= new_seqno
;
1403 if (just_connected
) {
1404 if (raft
->joining
) {
1405 raft_send_add_server_request(raft
, conn
);
1406 } else if (raft
->leaving
) {
1407 union raft_rpc rq
= {
1408 .remove_server_request
= {
1410 .type
= RAFT_RPC_REMOVE_SERVER_REQUEST
,
1416 raft_send_to_conn(raft
, &rq
, conn
);
1418 union raft_rpc rq
= (union raft_rpc
) {
1421 .type
= RAFT_RPC_HELLO_REQUEST
,
1424 .address
= raft
->local_address
,
1427 raft_send_to_conn(raft
, &rq
, conn
);
1431 for (size_t i
= 0; i
< 50; i
++) {
1433 if (!raft_conn_receive(raft
, conn
, &rpc
)) {
1437 log_rpc(&rpc
, "<--", conn
, 0);
1438 raft_handle_rpc(raft
, &rpc
);
1439 raft_rpc_uninit(&rpc
);
1444 raft_waiter_complete_rpc(struct raft
*raft
, const union raft_rpc
*rpc
)
1446 uint64_t term
= raft_rpc_get_term(rpc
);
1447 if (term
&& term
< raft
->term
) {
1448 /* Drop the message because it's for an expired term. */
1452 if (!raft_is_rpc_synced(raft
, rpc
)) {
1453 /* This is a bug. A reply message is deferred because some state in
1454 * the message, such as a term or index, has not been committed to
1455 * disk, and they should only be completed when that commit is done.
1456 * But this message is being completed before the commit is finished.
1457 * Complain, and hope that someone reports the bug. */
1458 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
1459 if (VLOG_DROP_ERR(&rl
)) {
1463 struct ds s
= DS_EMPTY_INITIALIZER
;
1465 if (term
> raft
->synced_term
) {
1466 ds_put_format(&s
, " because message term %"PRIu64
" is "
1467 "past synced term %"PRIu64
,
1468 term
, raft
->synced_term
);
1471 uint64_t index
= raft_rpc_get_min_sync_index(rpc
);
1472 if (index
> raft
->log_synced
) {
1473 ds_put_format(&s
, " %s message index %"PRIu64
" is past last "
1474 "synced index %"PRIu64
,
1475 s
.length
? "and" : "because",
1476 index
, raft
->log_synced
);
1479 const struct uuid
*vote
= raft_rpc_get_vote(rpc
);
1480 if (vote
&& !uuid_equals(vote
, &raft
->synced_vote
)) {
1481 char buf1
[SID_LEN
+ 1];
1482 char buf2
[SID_LEN
+ 1];
1483 ds_put_format(&s
, " %s vote %s differs from synced vote %s",
1484 s
.length
? "and" : "because",
1485 raft_get_nickname(raft
, vote
, buf1
, sizeof buf1
),
1486 raft_get_nickname(raft
, &raft
->synced_vote
,
1487 buf2
, sizeof buf2
));
1490 char buf
[SID_LEN
+ 1];
1491 ds_put_format(&s
, ": %s ",
1492 raft_get_nickname(raft
, &rpc
->common
.sid
,
1494 raft_rpc_format(rpc
, &s
);
1495 VLOG_ERR("internal error: deferred %s message completed "
1496 "but not ready to send%s",
1497 raft_rpc_type_to_string(rpc
->type
), ds_cstr(&s
));
1503 struct raft_conn
*dst
= raft_find_conn_by_sid(raft
, &rpc
->common
.sid
);
1505 raft_send_to_conn(raft
, rpc
, dst
);
1510 raft_waiter_complete(struct raft
*raft
, struct raft_waiter
*w
)
1514 if (raft
->role
== RAFT_LEADER
) {
1515 raft_update_our_match_index(raft
, w
->entry
.index
);
1517 raft
->log_synced
= w
->entry
.index
;
1521 raft
->synced_term
= w
->term
.term
;
1522 raft
->synced_vote
= w
->term
.vote
;
1526 raft_waiter_complete_rpc(raft
, w
->rpc
);
1532 raft_waiter_destroy(struct raft_waiter
*w
)
1538 ovs_list_remove(&w
->list_node
);
1546 raft_rpc_uninit(w
->rpc
);
1554 raft_waiters_run(struct raft
*raft
)
1556 if (ovs_list_is_empty(&raft
->waiters
)) {
1560 uint64_t cur
= ovsdb_log_commit_progress(raft
->log
);
1561 struct raft_waiter
*w
, *next
;
1562 LIST_FOR_EACH_SAFE (w
, next
, list_node
, &raft
->waiters
) {
1563 if (cur
< w
->commit_ticket
) {
1566 raft_waiter_complete(raft
, w
);
1567 raft_waiter_destroy(w
);
1572 raft_waiters_wait(struct raft
*raft
)
1574 struct raft_waiter
*w
;
1575 LIST_FOR_EACH (w
, list_node
, &raft
->waiters
) {
1576 ovsdb_log_commit_wait(raft
->log
, w
->commit_ticket
);
1582 raft_waiters_destroy(struct raft
*raft
)
1584 struct raft_waiter
*w
, *next
;
1585 LIST_FOR_EACH_SAFE (w
, next
, list_node
, &raft
->waiters
) {
1586 raft_waiter_destroy(w
);
1590 static bool OVS_WARN_UNUSED_RESULT
1591 raft_set_term(struct raft
*raft
, uint64_t term
, const struct uuid
*vote
)
1593 struct ovsdb_error
*error
= raft_write_state(raft
->log
, term
, vote
);
1594 if (!raft_handle_write_error(raft
, error
)) {
1598 struct raft_waiter
*w
= raft_waiter_create(raft
, RAFT_W_TERM
, true);
1599 raft
->term
= w
->term
.term
= term
;
1600 raft
->vote
= w
->term
.vote
= vote
? *vote
: UUID_ZERO
;
1605 raft_accept_vote(struct raft
*raft
, struct raft_server
*s
,
1606 const struct uuid
*vote
)
1608 if (uuid_equals(&s
->vote
, vote
)) {
1611 if (!uuid_is_zero(&s
->vote
)) {
1612 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1613 char buf1
[SID_LEN
+ 1];
1614 char buf2
[SID_LEN
+ 1];
1615 VLOG_WARN_RL(&rl
, "server %s changed its vote from %s to %s",
1617 raft_get_nickname(raft
, &s
->vote
, buf1
, sizeof buf1
),
1618 raft_get_nickname(raft
, vote
, buf2
, sizeof buf2
));
1621 if (uuid_equals(vote
, &raft
->sid
)
1622 && ++raft
->n_votes
> hmap_count(&raft
->servers
) / 2) {
1623 raft_become_leader(raft
);
1628 raft_start_election(struct raft
*raft
, bool leadership_transfer
)
1630 if (raft
->leaving
) {
1634 struct raft_server
*me
= raft_find_server(raft
, &raft
->sid
);
1639 if (!raft_set_term(raft
, raft
->term
+ 1, &raft
->sid
)) {
1643 ovs_assert(raft
->role
!= RAFT_LEADER
);
1644 raft
->role
= RAFT_CANDIDATE
;
1645 /* If there was no leader elected since last election, we know we are
1647 raft
->candidate_retrying
= !raft
->had_leader
;
1648 raft
->had_leader
= false;
1652 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1653 if (!VLOG_DROP_INFO(&rl
)) {
1654 long long int now
= time_msec();
1655 if (now
>= raft
->election_timeout
) {
1656 VLOG_INFO("term %"PRIu64
": %lld ms timeout expired, "
1657 "starting election",
1658 raft
->term
, now
- raft
->election_base
);
1660 VLOG_INFO("term %"PRIu64
": starting election", raft
->term
);
1663 raft_reset_election_timer(raft
);
1665 struct raft_server
*peer
;
1666 HMAP_FOR_EACH (peer
, hmap_node
, &raft
->servers
) {
1667 peer
->vote
= UUID_ZERO
;
1668 if (uuid_equals(&raft
->sid
, &peer
->sid
)) {
1672 union raft_rpc rq
= {
1675 .type
= RAFT_RPC_VOTE_REQUEST
,
1679 .last_log_index
= raft
->log_end
- 1,
1681 raft
->log_end
> raft
->log_start
1682 ? raft
->entries
[raft
->log_end
- raft
->log_start
- 1].term
1684 .leadership_transfer
= leadership_transfer
,
1687 raft_send(raft
, &rq
);
1690 /* Vote for ourselves. */
1691 raft_accept_vote(raft
, me
, &raft
->sid
);
1695 raft_open_conn(struct raft
*raft
, const char *address
, const struct uuid
*sid
)
1697 if (strcmp(address
, raft
->local_address
)
1698 && !raft_find_conn_by_address(raft
, address
)) {
1699 raft_add_conn(raft
, jsonrpc_session_open(address
, true), sid
, false);
1704 raft_conn_close(struct raft_conn
*conn
)
1706 jsonrpc_session_close(conn
->js
);
1707 ovs_list_remove(&conn
->list_node
);
1708 free(conn
->nickname
);
1712 /* Returns true if 'conn' should stay open, false if it should be closed. */
1714 raft_conn_should_stay_open(struct raft
*raft
, struct raft_conn
*conn
)
1716 /* Close the connection if it's actually dead. If necessary, we'll
1717 * initiate a new session later. */
1718 if (!jsonrpc_session_is_alive(conn
->js
)) {
1722 /* Keep incoming sessions. We trust the originator to decide to drop
1724 if (conn
->incoming
) {
1728 /* If we are joining the cluster, keep sessions to the remote addresses
1729 * that are supposed to be part of the cluster we're joining. */
1730 if (raft
->joining
&& sset_contains(&raft
->remote_addresses
,
1731 jsonrpc_session_get_name(conn
->js
))) {
1735 /* We have joined the cluster. If we did that "recently", then there is a
1736 * chance that we do not have the most recent server configuration log
1737 * entry. If so, it's a waste to disconnect from the servers that were in
1738 * remote_addresses and that will probably appear in the configuration,
1739 * just to reconnect to them a moment later when we do get the
1740 * configuration update. If we are not ourselves in the configuration,
1741 * then we know that there must be a new configuration coming up, so in
1742 * that case keep the connection. */
1743 if (!raft_find_server(raft
, &raft
->sid
)) {
1747 /* Keep the connection only if the server is part of the configuration. */
1748 return raft_find_server(raft
, &conn
->sid
);
1751 /* Allows 'raft' to maintain the distributed log. Call this function as part
1752 * of the process's main loop. */
1754 raft_run(struct raft
*raft
)
1756 if (raft
->left
|| raft
->failed
) {
1760 raft_waiters_run(raft
);
1762 if (!raft
->listener
&& time_msec() >= raft
->listen_backoff
) {
1763 char *paddr
= raft_make_address_passive(raft
->local_address
);
1764 int error
= pstream_open(paddr
, &raft
->listener
, DSCP_DEFAULT
);
1766 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1767 VLOG_WARN_RL(&rl
, "%s: listen failed (%s)",
1768 paddr
, ovs_strerror(error
));
1769 raft
->listen_backoff
= time_msec() + 1000;
1774 if (raft
->listener
) {
1775 struct stream
*stream
;
1776 int error
= pstream_accept(raft
->listener
, &stream
);
1778 raft_add_conn(raft
, jsonrpc_session_open_unreliably(
1779 jsonrpc_open(stream
), DSCP_DEFAULT
), NULL
,
1781 } else if (error
!= EAGAIN
) {
1782 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1783 VLOG_WARN_RL(&rl
, "%s: accept failed: %s",
1784 pstream_get_name(raft
->listener
),
1785 ovs_strerror(error
));
1789 /* Run RPCs for all open sessions. */
1790 struct raft_conn
*conn
;
1791 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1792 raft_conn_run(raft
, conn
);
1795 /* Close unneeded sessions. */
1796 struct raft_conn
*next
;
1797 LIST_FOR_EACH_SAFE (conn
, next
, list_node
, &raft
->conns
) {
1798 if (!raft_conn_should_stay_open(raft
, conn
)) {
1799 raft_conn_close(conn
);
1803 /* Open needed sessions. */
1804 struct raft_server
*server
;
1805 HMAP_FOR_EACH (server
, hmap_node
, &raft
->servers
) {
1806 raft_open_conn(raft
, server
->address
, &server
->sid
);
1808 if (raft
->joining
) {
1809 const char *address
;
1810 SSET_FOR_EACH (address
, &raft
->remote_addresses
) {
1811 raft_open_conn(raft
, address
, NULL
);
1815 if (!raft
->joining
&& time_msec() >= raft
->election_timeout
) {
1816 if (raft
->role
== RAFT_LEADER
) {
1817 /* Check if majority of followers replied, then reset
1818 * election_timeout and reset s->replied. Otherwise, become
1821 * Raft paper section 6.2: Leaders: A server might be in the leader
1822 * state, but if it isn’t the current leader, it could be
1823 * needlessly delaying client requests. For example, suppose a
1824 * leader is partitioned from the rest of the cluster, but it can
1825 * still communicate with a particular client. Without additional
1826 * mechanism, it could delay a request from that client forever,
1827 * being unable to replicate a log entry to any other servers.
1828 * Meanwhile, there might be another leader of a newer term that is
1829 * able to communicate with a majority of the cluster and would be
1830 * able to commit the client’s request. Thus, a leader in Raft
1831 * steps down if an election timeout elapses without a successful
1832 * round of heartbeats to a majority of its cluster; this allows
1833 * clients to retry their requests with another server. */
1835 HMAP_FOR_EACH (server
, hmap_node
, &raft
->servers
) {
1836 if (server
->replied
) {
1840 if (count
>= hmap_count(&raft
->servers
) / 2) {
1841 HMAP_FOR_EACH (server
, hmap_node
, &raft
->servers
) {
1842 server
->replied
= false;
1844 raft_reset_election_timer(raft
);
1846 raft_become_follower(raft
);
1847 raft_start_election(raft
, false);
1850 raft_start_election(raft
, false);
1855 if (raft
->leaving
&& time_msec() >= raft
->leave_timeout
) {
1856 raft_send_remove_server_requests(raft
);
1859 if (raft
->joining
&& time_msec() >= raft
->join_timeout
) {
1860 raft
->join_timeout
= time_msec() + 1000;
1861 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1862 raft_send_add_server_request(raft
, conn
);
1866 long long int now
= time_msec();
1867 if (now
>= raft
->ping_timeout
) {
1868 if (raft
->role
== RAFT_LEADER
) {
1869 raft_send_heartbeats(raft
);
1871 /* Check if any commands timeout. Timeout is set to twice the time of
1872 * election base time so that commands can complete properly during
1873 * leader election. E.g. a leader crashed and current node with pending
1874 * commands becomes new leader: the pending commands can still complete
1875 * if the crashed leader has replicated the transactions to majority of
1876 * followers before it crashed. */
1877 struct raft_command
*cmd
, *next_cmd
;
1878 HMAP_FOR_EACH_SAFE (cmd
, next_cmd
, hmap_node
, &raft
->commands
) {
1880 && now
- cmd
->timestamp
> raft
->election_timer
* 2) {
1881 raft_command_complete(raft
, cmd
, RAFT_CMD_TIMEOUT
);
1884 raft_reset_ping_timer(raft
);
1887 /* Do this only at the end; if we did it as soon as we set raft->left or
1888 * raft->failed in handling the RemoveServerReply, then it could easily
1889 * cause references to freed memory in RPC sessions, etc. */
1890 if (raft
->left
|| raft
->failed
) {
1896 raft_wait_session(struct jsonrpc_session
*js
)
1899 jsonrpc_session_wait(js
);
1900 jsonrpc_session_recv_wait(js
);
1904 /* Causes the next call to poll_block() to wake up when 'raft' needs to do
1907 raft_wait(struct raft
*raft
)
1909 if (raft
->left
|| raft
->failed
) {
1913 raft_waiters_wait(raft
);
1915 if (raft
->listener
) {
1916 pstream_wait(raft
->listener
);
1918 poll_timer_wait_until(raft
->listen_backoff
);
1921 struct raft_conn
*conn
;
1922 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1923 raft_wait_session(conn
->js
);
1926 if (!raft
->joining
) {
1927 poll_timer_wait_until(raft
->election_timeout
);
1929 poll_timer_wait_until(raft
->join_timeout
);
1931 if (raft
->leaving
) {
1932 poll_timer_wait_until(raft
->leave_timeout
);
1934 if (raft
->role
== RAFT_LEADER
|| !hmap_is_empty(&raft
->commands
)) {
1935 poll_timer_wait_until(raft
->ping_timeout
);
1939 static struct raft_waiter
*
1940 raft_waiter_create(struct raft
*raft
, enum raft_waiter_type type
,
1943 struct raft_waiter
*w
= xzalloc(sizeof *w
);
1944 ovs_list_push_back(&raft
->waiters
, &w
->list_node
);
1945 w
->commit_ticket
= start_commit
? ovsdb_log_commit_start(raft
->log
) : 0;
1950 /* Returns a human-readable representation of 'status' (or NULL if 'status' is
1953 raft_command_status_to_string(enum raft_command_status status
)
1956 case RAFT_CMD_INCOMPLETE
:
1957 return "operation still in progress";
1958 case RAFT_CMD_SUCCESS
:
1960 case RAFT_CMD_NOT_LEADER
:
1961 return "not leader";
1962 case RAFT_CMD_BAD_PREREQ
:
1963 return "prerequisite check failed";
1964 case RAFT_CMD_LOST_LEADERSHIP
:
1965 return "lost leadership";
1966 case RAFT_CMD_SHUTDOWN
:
1967 return "server shutdown";
1968 case RAFT_CMD_IO_ERROR
:
1970 case RAFT_CMD_TIMEOUT
:
1977 /* Converts human-readable status in 's' into status code in '*statusp'.
1978 * Returns true if successful, false if 's' is unknown. */
1980 raft_command_status_from_string(const char *s
,
1981 enum raft_command_status
*statusp
)
1983 for (enum raft_command_status status
= 0; ; status
++) {
1984 const char *s2
= raft_command_status_to_string(status
);
1988 } else if (!strcmp(s
, s2
)) {
1995 static const struct uuid
*
1996 raft_get_eid(const struct raft
*raft
, uint64_t index
)
1998 for (; index
>= raft
->log_start
; index
--) {
1999 const struct raft_entry
*e
= raft_get_entry(raft
, index
);
2004 return &raft
->snap
.eid
;
2008 raft_current_eid(const struct raft
*raft
)
2010 return raft_get_eid(raft
, raft
->log_end
- 1);
2013 static struct raft_command
*
2014 raft_command_create_completed(enum raft_command_status status
)
2016 ovs_assert(status
!= RAFT_CMD_INCOMPLETE
);
2018 struct raft_command
*cmd
= xzalloc(sizeof *cmd
);
2020 cmd
->status
= status
;
2024 static struct raft_command
*
2025 raft_command_create_incomplete(struct raft
*raft
, uint64_t index
)
2027 struct raft_command
*cmd
= xzalloc(sizeof *cmd
);
2028 cmd
->n_refs
= 2; /* One for client, one for raft->commands. */
2030 cmd
->status
= RAFT_CMD_INCOMPLETE
;
2031 hmap_insert(&raft
->commands
, &cmd
->hmap_node
, cmd
->index
);
2035 static struct raft_command
* OVS_WARN_UNUSED_RESULT
2036 raft_command_initiate(struct raft
*raft
,
2037 const struct json
*data
, const struct json
*servers
,
2038 uint64_t election_timer
, const struct uuid
*eid
)
2040 /* Write to local log. */
2041 uint64_t index
= raft
->log_end
;
2042 if (!raft_handle_write_error(
2043 raft
, raft_write_entry(
2044 raft
, raft
->term
, json_nullable_clone(data
), eid
,
2045 json_nullable_clone(servers
),
2047 return raft_command_create_completed(RAFT_CMD_IO_ERROR
);
2050 struct raft_command
*cmd
= raft_command_create_incomplete(raft
, index
);
2053 cmd
->timestamp
= time_msec();
2055 raft_waiter_create(raft
, RAFT_W_ENTRY
, true)->entry
.index
= cmd
->index
;
2057 if (failure_test
== FT_CRASH_BEFORE_SEND_APPEND_REQ
) {
2058 ovs_fatal(0, "Raft test: crash before sending append_request.");
2060 /* Write to remote logs. */
2061 struct raft_server
*s
;
2062 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
2063 if (!uuid_equals(&s
->sid
, &raft
->sid
) && s
->next_index
== index
) {
2064 raft_send_append_request(raft
, s
, 1, "execute command");
2068 if (failure_test
== FT_CRASH_AFTER_SEND_APPEND_REQ
) {
2069 ovs_fatal(0, "Raft test: crash after sending append_request.");
2071 raft_reset_ping_timer(raft
);
2077 log_all_commands(struct raft
*raft
)
2079 struct raft_command
*cmd
, *next
;
2080 HMAP_FOR_EACH_SAFE (cmd
, next
, hmap_node
, &raft
->commands
) {
2081 VLOG_DBG("raft command eid: "UUID_FMT
, UUID_ARGS(&cmd
->eid
));
2085 static struct raft_command
* OVS_WARN_UNUSED_RESULT
2086 raft_command_execute__(struct raft
*raft
, const struct json
*data
,
2087 const struct json
*servers
, uint64_t election_timer
,
2088 const struct uuid
*prereq
, struct uuid
*result
)
2090 if (raft
->joining
|| raft
->leaving
|| raft
->left
|| raft
->failed
) {
2091 return raft_command_create_completed(RAFT_CMD_SHUTDOWN
);
2094 if (raft
->role
!= RAFT_LEADER
) {
2095 /* Consider proxying the command to the leader. We can only do that if
2096 * we know the leader and the command does not change the set of
2097 * servers. We do not proxy commands without prerequisites, even
2098 * though we could, because in an OVSDB context a log entry doesn't
2099 * make sense without context. */
2100 if (servers
|| election_timer
|| !data
2101 || raft
->role
!= RAFT_FOLLOWER
|| uuid_is_zero(&raft
->leader_sid
)
2103 return raft_command_create_completed(RAFT_CMD_NOT_LEADER
);
2107 struct uuid eid
= data
? uuid_random() : UUID_ZERO
;
2112 if (raft
->role
!= RAFT_LEADER
) {
2113 const union raft_rpc rpc
= {
2114 .execute_command_request
= {
2116 .type
= RAFT_RPC_EXECUTE_COMMAND_REQUEST
,
2117 .sid
= raft
->leader_sid
,
2119 .data
= CONST_CAST(struct json
*, data
),
2124 if (failure_test
== FT_CRASH_BEFORE_SEND_EXEC_REQ
) {
2125 ovs_fatal(0, "Raft test: crash before sending "
2126 "execute_command_request");
2128 if (!raft_send(raft
, &rpc
)) {
2129 /* Couldn't send command, so it definitely failed. */
2130 return raft_command_create_completed(RAFT_CMD_NOT_LEADER
);
2132 if (failure_test
== FT_CRASH_AFTER_SEND_EXEC_REQ
) {
2133 ovs_fatal(0, "Raft test: crash after sending "
2134 "execute_command_request");
2137 struct raft_command
*cmd
= raft_command_create_incomplete(raft
, 0);
2138 cmd
->timestamp
= time_msec();
2140 log_all_commands(raft
);
2144 const struct uuid
*current_eid
= raft_current_eid(raft
);
2145 if (prereq
&& !uuid_equals(prereq
, current_eid
)) {
2146 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
2147 VLOG_INFO_RL(&rl
, "current entry eid "UUID_FMT
" does not match "
2148 "prerequisite "UUID_FMT
,
2149 UUID_ARGS(current_eid
), UUID_ARGS(prereq
));
2150 return raft_command_create_completed(RAFT_CMD_BAD_PREREQ
);
2153 return raft_command_initiate(raft
, data
, servers
, election_timer
, &eid
);
2156 /* Initiates appending a log entry to 'raft'. The log entry consists of 'data'
2157 * and, if 'prereq' is nonnull, it is only added to the log if the previous
2158 * entry in the log has entry ID 'prereq'. If 'result' is nonnull, it is
2159 * populated with the entry ID for the new log entry.
2161 * Returns a "struct raft_command" that may be used to track progress adding
2162 * the log entry. The caller must eventually free the returned structure, with
2163 * raft_command_unref(). */
2164 struct raft_command
* OVS_WARN_UNUSED_RESULT
2165 raft_command_execute(struct raft
*raft
, const struct json
*data
,
2166 const struct uuid
*prereq
, struct uuid
*result
)
2168 return raft_command_execute__(raft
, data
, NULL
, 0, prereq
, result
);
2171 /* Returns the status of 'cmd'. */
2172 enum raft_command_status
2173 raft_command_get_status(const struct raft_command
*cmd
)
2175 ovs_assert(cmd
->n_refs
> 0);
2179 /* Returns the index of the log entry at which 'cmd' was committed.
2181 * This function works only with successful commands. */
2183 raft_command_get_commit_index(const struct raft_command
*cmd
)
2185 ovs_assert(cmd
->n_refs
> 0);
2186 ovs_assert(cmd
->status
== RAFT_CMD_SUCCESS
);
2192 raft_command_unref(struct raft_command
*cmd
)
2195 ovs_assert(cmd
->n_refs
> 0);
2196 if (!--cmd
->n_refs
) {
2202 /* Causes poll_block() to wake up when 'cmd' has status to report. */
2204 raft_command_wait(const struct raft_command
*cmd
)
2206 if (cmd
->status
!= RAFT_CMD_INCOMPLETE
) {
2207 poll_immediate_wake();
2212 raft_command_complete(struct raft
*raft
,
2213 struct raft_command
*cmd
,
2214 enum raft_command_status status
)
2216 VLOG_DBG("raft_command_complete eid "UUID_FMT
" status: %s",
2217 UUID_ARGS(&cmd
->eid
), raft_command_status_to_string(status
));
2218 if (!uuid_is_zero(&cmd
->sid
)) {
2219 uint64_t commit_index
= status
== RAFT_CMD_SUCCESS
? cmd
->index
: 0;
2220 raft_send_execute_command_reply(raft
, &cmd
->sid
, &cmd
->eid
, status
,
2224 ovs_assert(cmd
->status
== RAFT_CMD_INCOMPLETE
);
2225 ovs_assert(cmd
->n_refs
> 0);
2226 hmap_remove(&raft
->commands
, &cmd
->hmap_node
);
2227 cmd
->status
= status
;
2228 raft_command_unref(cmd
);
2232 raft_complete_all_commands(struct raft
*raft
, enum raft_command_status status
)
2234 struct raft_command
*cmd
, *next
;
2235 HMAP_FOR_EACH_SAFE (cmd
, next
, hmap_node
, &raft
->commands
) {
2236 raft_command_complete(raft
, cmd
, status
);
2240 static struct raft_command
*
2241 raft_find_command_by_eid(struct raft
*raft
, const struct uuid
*eid
)
2243 struct raft_command
*cmd
;
2245 HMAP_FOR_EACH (cmd
, hmap_node
, &raft
->commands
) {
2246 if (uuid_equals(&cmd
->eid
, eid
)) {
2253 #define RAFT_RPC(ENUM, NAME) \
2254 static void raft_handle_##NAME(struct raft *, const struct raft_##NAME *);
2259 raft_handle_hello_request(struct raft
*raft OVS_UNUSED
,
2260 const struct raft_hello_request
*hello OVS_UNUSED
)
2264 /* 'sid' is the server being added. */
2266 raft_send_add_server_reply__(struct raft
*raft
, const struct uuid
*sid
,
2267 const char *address
,
2268 bool success
, const char *comment
)
2270 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2271 if (!VLOG_DROP_INFO(&rl
)) {
2272 struct ds s
= DS_EMPTY_INITIALIZER
;
2273 char buf
[SID_LEN
+ 1];
2274 ds_put_format(&s
, "adding %s ("SID_FMT
" at %s) "
2275 "to cluster "CID_FMT
" %s",
2276 raft_get_nickname(raft
, sid
, buf
, sizeof buf
),
2277 SID_ARGS(sid
), address
, CID_ARGS(&raft
->cid
),
2278 success
? "succeeded" : "failed");
2280 ds_put_format(&s
, " (%s)", comment
);
2282 VLOG_INFO("%s", ds_cstr(&s
));
2286 union raft_rpc rpy
= {
2287 .add_server_reply
= {
2289 .type
= RAFT_RPC_ADD_SERVER_REPLY
,
2291 .comment
= CONST_CAST(char *, comment
),
2297 struct sset
*remote_addresses
= &rpy
.add_server_reply
.remote_addresses
;
2298 sset_init(remote_addresses
);
2299 if (!raft
->joining
) {
2300 struct raft_server
*s
;
2301 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
2302 if (!uuid_equals(&s
->sid
, &raft
->sid
)) {
2303 sset_add(remote_addresses
, s
->address
);
2308 raft_send(raft
, &rpy
);
2310 sset_destroy(remote_addresses
);
2314 raft_send_remove_server_reply_rpc(struct raft
*raft
,
2315 const struct uuid
*dst_sid
,
2316 const struct uuid
*target_sid
,
2317 bool success
, const char *comment
)
2319 if (uuid_equals(&raft
->sid
, dst_sid
)) {
2320 if (success
&& uuid_equals(&raft
->sid
, target_sid
)) {
2321 raft_finished_leaving_cluster(raft
);
2326 const union raft_rpc rpy
= {
2327 .remove_server_reply
= {
2329 .type
= RAFT_RPC_REMOVE_SERVER_REPLY
,
2331 .comment
= CONST_CAST(char *, comment
),
2333 .target_sid
= (uuid_equals(dst_sid
, target_sid
)
2339 raft_send(raft
, &rpy
);
2343 raft_send_remove_server_reply__(struct raft
*raft
,
2344 const struct uuid
*target_sid
,
2345 const struct uuid
*requester_sid
,
2346 struct unixctl_conn
*requester_conn
,
2347 bool success
, const char *comment
)
2349 struct ds s
= DS_EMPTY_INITIALIZER
;
2350 ds_put_format(&s
, "request ");
2351 if (!uuid_is_zero(requester_sid
)) {
2352 char buf
[SID_LEN
+ 1];
2353 ds_put_format(&s
, "by %s",
2354 raft_get_nickname(raft
, requester_sid
, buf
, sizeof buf
));
2356 ds_put_cstr(&s
, "via unixctl");
2358 ds_put_cstr(&s
, " to remove ");
2359 if (!requester_conn
&& uuid_equals(target_sid
, requester_sid
)) {
2360 ds_put_cstr(&s
, "itself");
2362 char buf
[SID_LEN
+ 1];
2363 ds_put_cstr(&s
, raft_get_nickname(raft
, target_sid
, buf
, sizeof buf
));
2364 if (uuid_equals(target_sid
, &raft
->sid
)) {
2365 ds_put_cstr(&s
, " (ourselves)");
2368 ds_put_format(&s
, " from cluster "CID_FMT
" %s",
2369 CID_ARGS(&raft
->cid
),
2370 success
? "succeeded" : "failed");
2372 ds_put_format(&s
, " (%s)", comment
);
2375 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2376 VLOG_INFO_RL(&rl
, "%s", ds_cstr(&s
));
2378 /* Send RemoveServerReply to the requester (which could be a server or a
2379 * unixctl connection. Also always send it to the removed server; this
2380 * allows it to be sure that it's really removed and update its log and
2381 * disconnect permanently. */
2382 if (!uuid_is_zero(requester_sid
)) {
2383 raft_send_remove_server_reply_rpc(raft
, requester_sid
, target_sid
,
2386 if (!uuid_equals(requester_sid
, target_sid
)) {
2387 raft_send_remove_server_reply_rpc(raft
, target_sid
, target_sid
,
2390 if (requester_conn
) {
2392 unixctl_command_reply(requester_conn
, ds_cstr(&s
));
2394 unixctl_command_reply_error(requester_conn
, ds_cstr(&s
));
2402 raft_send_add_server_reply(struct raft
*raft
,
2403 const struct raft_add_server_request
*rq
,
2404 bool success
, const char *comment
)
2406 return raft_send_add_server_reply__(raft
, &rq
->common
.sid
, rq
->address
,
2411 raft_send_remove_server_reply(struct raft
*raft
,
2412 const struct raft_remove_server_request
*rq
,
2413 bool success
, const char *comment
)
2415 return raft_send_remove_server_reply__(raft
, &rq
->sid
, &rq
->common
.sid
,
2416 rq
->requester_conn
, success
,
2421 raft_become_follower(struct raft
*raft
)
2423 raft
->leader_sid
= UUID_ZERO
;
2424 if (raft
->role
== RAFT_FOLLOWER
) {
2428 raft
->role
= RAFT_FOLLOWER
;
2429 raft_reset_election_timer(raft
);
2431 /* Notify clients about lost leadership.
2433 * We do not reverse our changes to 'raft->servers' because the new
2434 * configuration is already part of the log. Possibly the configuration
2435 * log entry will not be committed, but until we know that we must use the
2436 * new configuration. Our AppendEntries processing will properly update
2437 * the server configuration later, if necessary. */
2438 struct raft_server
*s
;
2439 HMAP_FOR_EACH (s
, hmap_node
, &raft
->add_servers
) {
2440 raft_send_add_server_reply__(raft
, &s
->sid
, s
->address
, false,
2441 RAFT_SERVER_LOST_LEADERSHIP
);
2443 if (raft
->remove_server
) {
2444 raft_send_remove_server_reply__(raft
, &raft
->remove_server
->sid
,
2445 &raft
->remove_server
->requester_sid
,
2446 raft
->remove_server
->requester_conn
,
2447 false, RAFT_SERVER_LOST_LEADERSHIP
);
2448 raft_server_destroy(raft
->remove_server
);
2449 raft
->remove_server
= NULL
;
2452 raft_complete_all_commands(raft
, RAFT_CMD_LOST_LEADERSHIP
);
2456 raft_send_append_request(struct raft
*raft
,
2457 struct raft_server
*peer
, unsigned int n
,
2458 const char *comment
)
2460 ovs_assert(raft
->role
== RAFT_LEADER
);
2462 const union raft_rpc rq
= {
2465 .type
= RAFT_RPC_APPEND_REQUEST
,
2467 .comment
= CONST_CAST(char *, comment
),
2470 .prev_log_index
= peer
->next_index
- 1,
2471 .prev_log_term
= (peer
->next_index
- 1 >= raft
->log_start
2472 ? raft
->entries
[peer
->next_index
- 1
2473 - raft
->log_start
].term
2475 .leader_commit
= raft
->commit_index
,
2476 .entries
= &raft
->entries
[peer
->next_index
- raft
->log_start
],
2480 raft_send(raft
, &rq
);
2484 raft_send_heartbeats(struct raft
*raft
)
2486 struct raft_server
*s
;
2487 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
2488 if (!uuid_equals(&raft
->sid
, &s
->sid
)) {
2489 raft_send_append_request(raft
, s
, 0, "heartbeat");
2493 /* Send anyone waiting for a command to complete a ping to let them
2494 * know we're still working on it. */
2495 struct raft_command
*cmd
;
2496 HMAP_FOR_EACH (cmd
, hmap_node
, &raft
->commands
) {
2497 if (!uuid_is_zero(&cmd
->sid
)) {
2498 raft_send_execute_command_reply(raft
, &cmd
->sid
,
2500 RAFT_CMD_INCOMPLETE
, 0);
2504 raft_reset_ping_timer(raft
);
2507 /* Initializes the fields in 's' that represent the leader's view of the
2510 raft_server_init_leader(struct raft
*raft
, struct raft_server
*s
)
2512 s
->next_index
= raft
->log_end
;
2514 s
->phase
= RAFT_PHASE_STABLE
;
2519 raft_set_leader(struct raft
*raft
, const struct uuid
*sid
)
2521 raft
->leader_sid
= *sid
;
2522 raft
->had_leader
= true;
2523 raft
->candidate_retrying
= false;
2527 raft_become_leader(struct raft
*raft
)
2529 log_all_commands(raft
);
2531 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2532 VLOG_INFO_RL(&rl
, "term %"PRIu64
": elected leader by %d+ of "
2533 "%"PRIuSIZE
" servers", raft
->term
,
2534 raft
->n_votes
, hmap_count(&raft
->servers
));
2536 ovs_assert(raft
->role
!= RAFT_LEADER
);
2537 raft
->role
= RAFT_LEADER
;
2538 raft_set_leader(raft
, &raft
->sid
);
2539 raft_reset_election_timer(raft
);
2540 raft_reset_ping_timer(raft
);
2542 struct raft_server
*s
;
2543 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
2544 raft_server_init_leader(raft
, s
);
2547 raft
->election_timer_new
= 0;
2549 raft_update_our_match_index(raft
, raft
->log_end
- 1);
2550 raft_send_heartbeats(raft
);
2552 /* Write the fact that we are leader to the log. This is not used by the
2553 * algorithm (although it could be, for quick restart), but it is used for
2554 * offline analysis to check for conformance with the properties that Raft
2556 struct raft_record r
= {
2557 .type
= RAFT_REC_LEADER
,
2561 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
2563 /* Initiate a no-op commit. Otherwise we might never find out what's in
2564 * the log. See section 6.4 item 1:
2566 * The Leader Completeness Property guarantees that a leader has all
2567 * committed entries, but at the start of its term, it may not know
2568 * which those are. To find out, it needs to commit an entry from its
2569 * term. Raft handles this by having each leader commit a blank no-op
2570 * entry into the log at the start of its term. As soon as this no-op
2571 * entry is committed, the leader’s commit index will be at least as
2572 * large as any other servers’ during its term.
2574 raft_command_unref(raft_command_execute__(raft
, NULL
, NULL
, 0, NULL
,
2578 /* Processes term 'term' received as part of RPC 'common'. Returns true if the
2579 * caller should continue processing the RPC, false if the caller should reject
2580 * it due to a stale term. */
2582 raft_receive_term__(struct raft
*raft
, const struct raft_rpc_common
*common
,
2585 /* Section 3.3 says:
2587 * Current terms are exchanged whenever servers communicate; if one
2588 * server’s current term is smaller than the other’s, then it updates
2589 * its current term to the larger value. If a candidate or leader
2590 * discovers that its term is out of date, it immediately reverts to
2591 * follower state. If a server receives a request with a stale term
2592 * number, it rejects the request.
2594 if (term
> raft
->term
) {
2595 if (!raft_set_term(raft
, term
, NULL
)) {
2596 /* Failed to update the term to 'term'. */
2599 raft_become_follower(raft
);
2600 } else if (term
< raft
->term
) {
2601 char buf
[SID_LEN
+ 1];
2602 VLOG_INFO("rejecting term %"PRIu64
" < current term %"PRIu64
" received "
2603 "in %s message from server %s",
2605 raft_rpc_type_to_string(common
->type
),
2606 raft_get_nickname(raft
, &common
->sid
, buf
, sizeof buf
));
2613 raft_get_servers_from_log(struct raft
*raft
, enum vlog_level level
)
2615 const struct json
*servers_json
= raft
->snap
.servers
;
2616 for (uint64_t index
= raft
->log_end
- 1; index
>= raft
->log_start
;
2618 struct raft_entry
*e
= &raft
->entries
[index
- raft
->log_start
];
2620 servers_json
= e
->servers
;
2625 struct hmap servers
;
2626 struct ovsdb_error
*error
= raft_servers_from_json(servers_json
, &servers
);
2628 raft_set_servers(raft
, &servers
, level
);
2629 raft_servers_destroy(&servers
);
2632 /* Truncates the log, so that raft->log_end becomes 'new_end'.
2634 * Doesn't write anything to disk. In theory, we could truncate the on-disk
2635 * log file, but we don't have the right information to know how long it should
2636 * be. What we actually do is to append entries for older indexes to the
2637 * on-disk log; when we re-read it later, these entries truncate the log.
2639 * Returns true if any of the removed log entries were server configuration
2640 * entries, false otherwise. */
2642 raft_truncate(struct raft
*raft
, uint64_t new_end
)
2644 ovs_assert(new_end
>= raft
->log_start
);
2645 if (raft
->log_end
> new_end
) {
2646 char buf
[SID_LEN
+ 1];
2647 VLOG_INFO("%s truncating %"PRIu64
" entries from end of log",
2648 raft_get_nickname(raft
, &raft
->sid
, buf
, sizeof buf
),
2649 raft
->log_end
- new_end
);
2652 bool servers_changed
= false;
2653 while (raft
->log_end
> new_end
) {
2654 struct raft_entry
*entry
= &raft
->entries
[--raft
->log_end
2656 if (entry
->servers
) {
2657 servers_changed
= true;
2659 raft_entry_uninit(entry
);
2661 return servers_changed
;
2664 static const struct json
*
2665 raft_peek_next_entry(struct raft
*raft
, struct uuid
*eid
)
2667 /* Invariant: log_start - 2 <= last_applied <= commit_index < log_end. */
2668 ovs_assert(raft
->log_start
<= raft
->last_applied
+ 2);
2669 ovs_assert(raft
->last_applied
<= raft
->commit_index
);
2670 ovs_assert(raft
->commit_index
< raft
->log_end
);
2672 if (raft
->joining
|| raft
->failed
) {
2676 if (raft
->log_start
== raft
->last_applied
+ 2) {
2677 *eid
= raft
->snap
.eid
;
2678 return raft
->snap
.data
;
2681 while (raft
->last_applied
< raft
->commit_index
) {
2682 const struct raft_entry
*e
= raft_get_entry(raft
,
2683 raft
->last_applied
+ 1);
2688 raft
->last_applied
++;
2693 static const struct json
*
2694 raft_get_next_entry(struct raft
*raft
, struct uuid
*eid
)
2696 const struct json
*data
= raft_peek_next_entry(raft
, eid
);
2698 raft
->last_applied
++;
2703 /* Updates commit index in raft log. If commit index is already up-to-date
2704 * it does nothing and return false, otherwise, returns true. */
2706 raft_update_commit_index(struct raft
*raft
, uint64_t new_commit_index
)
2708 if (new_commit_index
<= raft
->commit_index
) {
2712 if (raft
->role
== RAFT_LEADER
) {
2713 while (raft
->commit_index
< new_commit_index
) {
2714 uint64_t index
= ++raft
->commit_index
;
2715 const struct raft_entry
*e
= raft_get_entry(raft
, index
);
2717 struct raft_command
*cmd
2718 = raft_find_command_by_eid(raft
, &e
->eid
);
2721 VLOG_DBG("Command completed after role change from"
2722 " follower to leader "UUID_FMT
,
2723 UUID_ARGS(&e
->eid
));
2726 raft_command_complete(raft
, cmd
, RAFT_CMD_SUCCESS
);
2729 if (e
->election_timer
) {
2730 VLOG_INFO("Election timer changed from %"PRIu64
" to %"PRIu64
,
2731 raft
->election_timer
, e
->election_timer
);
2732 raft
->election_timer
= e
->election_timer
;
2733 raft
->election_timer_new
= 0;
2736 /* raft_run_reconfigure() can write a new Raft entry, which can
2737 * reallocate raft->entries, which would invalidate 'e', so
2738 * this case must be last, after the one for 'e->data'. */
2739 raft_run_reconfigure(raft
);
2743 while (raft
->commit_index
< new_commit_index
) {
2744 uint64_t index
= ++raft
->commit_index
;
2745 const struct raft_entry
*e
= raft_get_entry(raft
, index
);
2746 if (e
->election_timer
) {
2747 VLOG_INFO("Election timer changed from %"PRIu64
" to %"PRIu64
,
2748 raft
->election_timer
, e
->election_timer
);
2749 raft
->election_timer
= e
->election_timer
;
2752 /* Check if any pending command can be completed, and complete it.
2753 * This can happen when leader fail-over before sending
2754 * execute_command_reply. */
2755 const struct uuid
*eid
= raft_get_eid(raft
, new_commit_index
);
2756 struct raft_command
*cmd
= raft_find_command_by_eid(raft
, eid
);
2758 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
2760 "Command completed without reply (eid: "UUID_FMT
", "
2761 "commit index: %"PRIu64
")",
2762 UUID_ARGS(eid
), new_commit_index
);
2763 cmd
->index
= new_commit_index
;
2764 raft_command_complete(raft
, cmd
, RAFT_CMD_SUCCESS
);
2768 /* Write the commit index to the log. The next time we restart, this
2769 * allows us to start exporting a reasonably fresh log, instead of a log
2770 * that only contains the snapshot. */
2771 struct raft_record r
= {
2772 .type
= RAFT_REC_COMMIT_INDEX
,
2773 .commit_index
= raft
->commit_index
,
2775 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
2779 /* This doesn't use rq->entries (but it does use rq->n_entries). */
2781 raft_send_append_reply(struct raft
*raft
, const struct raft_append_request
*rq
,
2782 enum raft_append_result result
, const char *comment
)
2784 /* Figure 3.1: "If leaderCommit > commitIndex, set commitIndex =
2785 * min(leaderCommit, index of last new entry)" */
2786 if (result
== RAFT_APPEND_OK
&& rq
->leader_commit
> raft
->commit_index
) {
2787 raft_update_commit_index(
2788 raft
, MIN(rq
->leader_commit
, rq
->prev_log_index
+ rq
->n_entries
));
2792 union raft_rpc reply
= {
2795 .type
= RAFT_RPC_APPEND_REPLY
,
2796 .sid
= rq
->common
.sid
,
2797 .comment
= CONST_CAST(char *, comment
),
2800 .log_end
= raft
->log_end
,
2801 .prev_log_index
= rq
->prev_log_index
,
2802 .prev_log_term
= rq
->prev_log_term
,
2803 .n_entries
= rq
->n_entries
,
2807 raft_send(raft
, &reply
);
2810 /* If 'prev_log_index' exists in 'raft''s log, in term 'prev_log_term', returns
2811 * NULL. Otherwise, returns an explanation for the mismatch. */
2813 match_index_and_term(const struct raft
*raft
,
2814 uint64_t prev_log_index
, uint64_t prev_log_term
)
2816 if (prev_log_index
< raft
->log_start
- 1) {
2817 return "mismatch before start of log";
2818 } else if (prev_log_index
== raft
->log_start
- 1) {
2819 if (prev_log_term
!= raft
->snap
.term
) {
2820 return "prev_term mismatch";
2822 } else if (prev_log_index
< raft
->log_end
) {
2823 if (raft
->entries
[prev_log_index
- raft
->log_start
].term
2825 return "term mismatch";
2828 /* prev_log_index >= raft->log_end */
2829 return "mismatch past end of log";
2835 raft_handle_append_entries(struct raft
*raft
,
2836 const struct raft_append_request
*rq
,
2837 uint64_t prev_log_index
, uint64_t prev_log_term
,
2838 const struct raft_entry
*entries
,
2839 unsigned int n_entries
)
2841 /* Section 3.5: "When sending an AppendEntries RPC, the leader includes
2842 * the index and term of the entry in its log that immediately precedes
2843 * the new entries. If the follower does not find an entry in its log
2844 * with the same index and term, then it refuses the new entries." */
2845 const char *mismatch
= match_index_and_term(raft
, prev_log_index
,
2848 VLOG_INFO("rejecting append_request because previous entry "
2849 "%"PRIu64
",%"PRIu64
" not in local log (%s)",
2850 prev_log_term
, prev_log_index
, mismatch
);
2851 raft_send_append_reply(raft
, rq
, RAFT_APPEND_INCONSISTENCY
, mismatch
);
2855 /* Figure 3.1: "If an existing entry conflicts with a new one (same
2856 * index but different terms), delete the existing entry and all that
2859 bool servers_changed
= false;
2860 for (i
= 0; ; i
++) {
2861 if (i
>= n_entries
) {
2863 if (rq
->common
.comment
2864 && !strcmp(rq
->common
.comment
, "heartbeat")) {
2865 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "heartbeat");
2867 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "no change");
2872 uint64_t log_index
= (prev_log_index
+ 1) + i
;
2873 if (log_index
>= raft
->log_end
) {
2876 if (raft
->entries
[log_index
- raft
->log_start
].term
2877 != entries
[i
].term
) {
2878 if (raft_truncate(raft
, log_index
)) {
2879 servers_changed
= true;
2885 if (failure_test
== FT_CRASH_AFTER_RECV_APPEND_REQ_UPDATE
) {
2886 ovs_fatal(0, "Raft test: crash after receiving append_request with "
2889 /* Figure 3.1: "Append any entries not already in the log." */
2890 struct ovsdb_error
*error
= NULL
;
2891 bool any_written
= false;
2892 for (; i
< n_entries
; i
++) {
2893 const struct raft_entry
*e
= &entries
[i
];
2894 error
= raft_write_entry(raft
, e
->term
,
2895 json_nullable_clone(e
->data
), &e
->eid
,
2896 json_nullable_clone(e
->servers
),
2903 servers_changed
= true;
2908 raft_waiter_create(raft
, RAFT_W_ENTRY
, true)->entry
.index
2909 = raft
->log_end
- 1;
2911 if (servers_changed
) {
2912 /* The set of servers might have changed; check. */
2913 raft_get_servers_from_log(raft
, VLL_INFO
);
2917 char *s
= ovsdb_error_to_string_free(error
);
2920 raft_send_append_reply(raft
, rq
, RAFT_APPEND_IO_ERROR
, "I/O error");
2924 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "log updated");
2928 raft_update_leader(struct raft
*raft
, const struct uuid
*sid
)
2930 if (raft
->role
== RAFT_LEADER
) {
2931 char buf
[SID_LEN
+ 1];
2932 VLOG_ERR("this server is leader but server %s claims to be",
2933 raft_get_nickname(raft
, sid
, buf
, sizeof buf
));
2935 } else if (!uuid_equals(sid
, &raft
->leader_sid
)) {
2936 if (!uuid_is_zero(&raft
->leader_sid
)) {
2937 char buf1
[SID_LEN
+ 1];
2938 char buf2
[SID_LEN
+ 1];
2939 VLOG_ERR("leader for term %"PRIu64
" changed from %s to %s",
2941 raft_get_nickname(raft
, &raft
->leader_sid
,
2943 raft_get_nickname(raft
, sid
, buf2
, sizeof buf2
));
2945 char buf
[SID_LEN
+ 1];
2946 VLOG_INFO("server %s is leader for term %"PRIu64
,
2947 raft_get_nickname(raft
, sid
, buf
, sizeof buf
),
2950 raft_set_leader(raft
, sid
);
2952 /* Record the leader to the log. This is not used by the algorithm
2953 * (although it could be, for quick restart), but it is used for
2954 * offline analysis to check for conformance with the properties
2955 * that Raft guarantees. */
2956 struct raft_record r
= {
2957 .type
= RAFT_REC_LEADER
,
2961 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
2967 raft_handle_append_request(struct raft
*raft
,
2968 const struct raft_append_request
*rq
)
2970 /* We do not check whether the server that sent the request is part of the
2971 * cluster. As section 4.1 says, "A server accepts AppendEntries requests
2972 * from a leader that is not part of the server’s latest configuration.
2973 * Otherwise, a new server could never be added to the cluster (it would
2974 * never accept any log entries preceding the configuration entry that adds
2976 if (!raft_update_leader(raft
, &rq
->common
.sid
)) {
2977 raft_send_append_reply(raft
, rq
, RAFT_APPEND_INCONSISTENCY
,
2978 "usurped leadership");
2981 raft_reset_election_timer(raft
);
2983 /* First check for the common case, where the AppendEntries request is
2984 * entirely for indexes covered by 'log_start' ... 'log_end - 1', something
2987 * rq->prev_log_index
2988 * | first_entry_index
2989 * | | nth_entry_index
2993 * T | T | T | T | T |
2996 * T | T | T | T | T |
3002 uint64_t first_entry_index
= rq
->prev_log_index
+ 1;
3003 uint64_t nth_entry_index
= rq
->prev_log_index
+ rq
->n_entries
;
3004 if (OVS_LIKELY(first_entry_index
>= raft
->log_start
)) {
3005 raft_handle_append_entries(raft
, rq
,
3006 rq
->prev_log_index
, rq
->prev_log_term
,
3007 rq
->entries
, rq
->n_entries
);
3011 /* Now a series of checks for odd cases, where the AppendEntries request
3012 * extends earlier than the beginning of our log, into the log entries
3013 * discarded by the most recent snapshot. */
3016 * Handle the case where the indexes covered by rq->entries[] are entirely
3017 * disjoint with 'log_start - 1' ... 'log_end - 1', as shown below. So,
3018 * everything in the AppendEntries request must already have been
3019 * committed, and we might as well return true.
3021 * rq->prev_log_index
3022 * | first_entry_index
3023 * | | nth_entry_index
3027 * T | T | T | T | T |
3030 * T | T | T | T | T |
3036 if (nth_entry_index
< raft
->log_start
- 1) {
3037 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
,
3038 "append before log start");
3043 * Handle the case where the last entry in rq->entries[] has the same index
3044 * as 'log_start - 1', so we can compare their terms:
3046 * rq->prev_log_index
3047 * | first_entry_index
3048 * | | nth_entry_index
3052 * T | T | T | T | T |
3055 * T | T | T | T | T |
3061 * There's actually a sub-case where rq->n_entries == 0, in which we
3062 * compare rq->prev_term:
3064 * rq->prev_log_index
3072 * T | T | T | T | T |
3078 if (nth_entry_index
== raft
->log_start
- 1) {
3080 ? raft
->snap
.term
== rq
->entries
[rq
->n_entries
- 1].term
3081 : raft
->snap
.term
== rq
->prev_log_term
) {
3082 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "no change");
3084 raft_send_append_reply(raft
, rq
, RAFT_APPEND_INCONSISTENCY
,
3091 * We now know that the data in rq->entries[] overlaps the data in
3092 * raft->entries[], as shown below, with some positive 'ofs':
3094 * rq->prev_log_index
3095 * | first_entry_index
3096 * | | nth_entry_index
3099 * +---+---+---+---+---+
3100 * T | T | T | T | T | T |
3101 * +---+-------+---+---+
3103 * T | T | T | T | T |
3111 * We transform this into the following by trimming the first 'ofs'
3112 * elements off of rq->entries[], ending up with the following. Notice how
3113 * we retain the term but not the data for rq->entries[ofs - 1]:
3115 * first_entry_index + ofs - 1
3116 * | first_entry_index + ofs
3117 * | | nth_entry_index + ofs
3124 * T | T | T | T | T |
3130 uint64_t ofs
= raft
->log_start
- first_entry_index
;
3131 raft_handle_append_entries(raft
, rq
,
3132 raft
->log_start
- 1, rq
->entries
[ofs
- 1].term
,
3133 &rq
->entries
[ofs
], rq
->n_entries
- ofs
);
3136 /* Returns true if 'raft' has another log entry or snapshot to read. */
3138 raft_has_next_entry(const struct raft
*raft_
)
3140 struct raft
*raft
= CONST_CAST(struct raft
*, raft_
);
3142 return raft_peek_next_entry(raft
, &eid
) != NULL
;
3145 /* Returns the next log entry or snapshot from 'raft', or NULL if there are
3146 * none left to read. Stores the entry ID of the log entry in '*eid'. Stores
3147 * true in '*is_snapshot' if the returned data is a snapshot, false if it is a
3150 raft_next_entry(struct raft
*raft
, struct uuid
*eid
, bool *is_snapshot
)
3152 const struct json
*data
= raft_get_next_entry(raft
, eid
);
3153 *is_snapshot
= data
== raft
->snap
.data
;
3157 /* Returns the log index of the last-read snapshot or log entry. */
3159 raft_get_applied_index(const struct raft
*raft
)
3161 return raft
->last_applied
;
3164 /* Returns the log index of the last snapshot or log entry that is available to
3167 raft_get_commit_index(const struct raft
*raft
)
3169 return raft
->commit_index
;
3172 static struct raft_server
*
3173 raft_find_peer(struct raft
*raft
, const struct uuid
*uuid
)
3175 struct raft_server
*s
= raft_find_server(raft
, uuid
);
3176 return s
&& !uuid_equals(&raft
->sid
, &s
->sid
) ? s
: NULL
;
3179 static struct raft_server
*
3180 raft_find_new_server(struct raft
*raft
, const struct uuid
*uuid
)
3182 return raft_server_find(&raft
->add_servers
, uuid
);
3185 /* Figure 3.1: "If there exists an N such that N > commitIndex, a
3186 * majority of matchIndex[i] >= N, and log[N].term == currentTerm, set
3187 * commitIndex = N (sections 3.5 and 3.6)." */
3189 raft_consider_updating_commit_index(struct raft
*raft
)
3191 /* This loop cannot just bail out when it comes across a log entry that
3192 * does not match the criteria. For example, Figure 3.7(d2) shows a
3193 * case where the log entry for term 2 cannot be committed directly
3194 * (because it is not for the current term) but it can be committed as
3195 * a side effect of commit the entry for term 4 (the current term).
3196 * XXX Is there a more efficient way to do this? */
3197 ovs_assert(raft
->role
== RAFT_LEADER
);
3199 uint64_t new_commit_index
= raft
->commit_index
;
3200 for (uint64_t idx
= MAX(raft
->commit_index
+ 1, raft
->log_start
);
3201 idx
< raft
->log_end
; idx
++) {
3202 if (raft
->entries
[idx
- raft
->log_start
].term
== raft
->term
) {
3204 struct raft_server
*s2
;
3205 HMAP_FOR_EACH (s2
, hmap_node
, &raft
->servers
) {
3206 if (s2
->match_index
>= idx
) {
3210 if (count
> hmap_count(&raft
->servers
) / 2) {
3211 VLOG_DBG("index %"PRIu64
" committed to %"PRIuSIZE
" servers, "
3212 "applying", idx
, count
);
3213 new_commit_index
= idx
;
3217 if (raft_update_commit_index(raft
, new_commit_index
)) {
3218 raft_send_heartbeats(raft
);
3223 raft_update_match_index(struct raft
*raft
, struct raft_server
*s
,
3226 ovs_assert(raft
->role
== RAFT_LEADER
);
3227 if (min_index
> s
->match_index
) {
3228 s
->match_index
= min_index
;
3229 raft_consider_updating_commit_index(raft
);
3234 raft_update_our_match_index(struct raft
*raft
, uint64_t min_index
)
3236 struct raft_server
*server
= raft_find_server(raft
, &raft
->sid
);
3238 raft_update_match_index(raft
, server
, min_index
);
3243 raft_send_install_snapshot_request(struct raft
*raft
,
3244 const struct raft_server
*s
,
3245 const char *comment
)
3247 union raft_rpc rpc
= {
3248 .install_snapshot_request
= {
3250 .type
= RAFT_RPC_INSTALL_SNAPSHOT_REQUEST
,
3252 .comment
= CONST_CAST(char *, comment
),
3255 .last_index
= raft
->log_start
- 1,
3256 .last_term
= raft
->snap
.term
,
3257 .last_servers
= raft
->snap
.servers
,
3258 .last_eid
= raft
->snap
.eid
,
3259 .data
= raft
->snap
.data
,
3260 .election_timer
= raft
->election_timer
,
3263 raft_send(raft
, &rpc
);
3267 raft_handle_append_reply(struct raft
*raft
,
3268 const struct raft_append_reply
*rpy
)
3270 if (raft
->role
!= RAFT_LEADER
) {
3271 VLOG_INFO("rejected append_reply (not leader)");
3275 /* Most commonly we'd be getting an AppendEntries reply from a configured
3276 * server (e.g. a peer), but we can also get them from servers in the
3277 * process of being added. */
3278 struct raft_server
*s
= raft_find_peer(raft
, &rpy
->common
.sid
);
3280 s
= raft_find_new_server(raft
, &rpy
->common
.sid
);
3282 VLOG_INFO("rejected append_reply from unknown server "SID_FMT
,
3283 SID_ARGS(&rpy
->common
.sid
));
3289 if (rpy
->result
== RAFT_APPEND_OK
) {
3290 /* Figure 3.1: "If successful, update nextIndex and matchIndex for
3291 * follower (section 3.5)." */
3292 uint64_t min_index
= rpy
->prev_log_index
+ rpy
->n_entries
+ 1;
3293 if (s
->next_index
< min_index
) {
3294 s
->next_index
= min_index
;
3296 raft_update_match_index(raft
, s
, min_index
- 1);
3298 /* Figure 3.1: "If AppendEntries fails because of log inconsistency,
3299 * decrement nextIndex and retry (section 3.5)."
3301 * We also implement the optimization suggested in section 4.2.1:
3302 * "Various approaches can make nextIndex converge to its correct value
3303 * more quickly, including those described in Chapter 3. The simplest
3304 * approach to solving this particular problem of adding a new server,
3305 * however, is to have followers return the length of their logs in the
3306 * AppendEntries response; this allows the leader to cap the follower’s
3307 * nextIndex accordingly." */
3308 s
->next_index
= (s
->next_index
> 0
3309 ? MIN(s
->next_index
- 1, rpy
->log_end
)
3312 if (rpy
->result
== RAFT_APPEND_IO_ERROR
) {
3313 /* Append failed but not because of a log inconsistency. Because
3314 * of the I/O error, there's no point in re-sending the append
3316 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3317 VLOG_INFO_RL(&rl
, "%s reported I/O error", s
->nickname
);
3323 * Our behavior here must depend on the value of next_index relative to
3324 * log_start and log_end. There are three cases:
3326 * Case 1 | Case 2 | Case 3
3327 * <---------------->|<------------->|<------------------>
3331 * T | T | T | T | T |
3337 if (s
->next_index
< raft
->log_start
) {
3339 raft_send_install_snapshot_request(raft
, s
, NULL
);
3340 } else if (s
->next_index
< raft
->log_end
) {
3342 raft_send_append_request(raft
, s
, 1, NULL
);
3345 if (s
->phase
== RAFT_PHASE_CATCHUP
) {
3346 s
->phase
= RAFT_PHASE_CAUGHT_UP
;
3347 raft_run_reconfigure(raft
);
3353 raft_should_suppress_disruptive_server(struct raft
*raft
,
3354 const union raft_rpc
*rpc
)
3356 if (rpc
->type
!= RAFT_RPC_VOTE_REQUEST
) {
3360 /* Section 4.2.3 "Disruptive Servers" says:
3362 * ...if a server receives a RequestVote request within the minimum
3363 * election timeout of hearing from a current leader, it does not update
3364 * its term or grant its vote...
3366 * ...This change conflicts with the leadership transfer mechanism as
3367 * described in Chapter 3, in which a server legitimately starts an
3368 * election without waiting an election timeout. In that case,
3369 * RequestVote messages should be processed by other servers even when
3370 * they believe a current cluster leader exists. Those RequestVote
3371 * requests can include a special flag to indicate this behavior (“I
3372 * have permission to disrupt the leader--it told me to!”).
3374 * This clearly describes how the followers should act, but not the leader.
3375 * We just ignore vote requests that arrive at a current leader. This
3376 * seems to be fairly safe, since a majority other than the current leader
3377 * can still elect a new leader and the first AppendEntries from that new
3378 * leader will depose the current leader. */
3379 const struct raft_vote_request
*rq
= raft_vote_request_cast(rpc
);
3380 if (rq
->leadership_transfer
) {
3384 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3385 long long int now
= time_msec();
3386 switch (raft
->role
) {
3388 VLOG_WARN_RL(&rl
, "ignoring vote request received as leader");
3392 if (now
< raft
->election_base
+ raft
->election_timer
) {
3393 VLOG_WARN_RL(&rl
, "ignoring vote request received after only "
3394 "%lld ms (minimum election time is %"PRIu64
" ms)",
3395 now
- raft
->election_base
, raft
->election_timer
);
3400 case RAFT_CANDIDATE
:
3408 /* Returns true if a reply should be sent. */
3410 raft_handle_vote_request__(struct raft
*raft
,
3411 const struct raft_vote_request
*rq
)
3413 /* Figure 3.1: "If votedFor is null or candidateId, and candidate's vote is
3414 * at least as up-to-date as receiver's log, grant vote (sections 3.4,
3416 if (uuid_equals(&raft
->vote
, &rq
->common
.sid
)) {
3417 /* Already voted for this candidate in this term. Resend vote. */
3419 } else if (!uuid_is_zero(&raft
->vote
)) {
3420 /* Already voted for different candidate in this term. Send a reply
3421 * saying what candidate we did vote for. This isn't a necessary part
3422 * of the Raft protocol but it can make debugging easier. */
3426 /* Section 3.6.1: "The RequestVote RPC implements this restriction: the RPC
3427 * includes information about the candidate’s log, and the voter denies its
3428 * vote if its own log is more up-to-date than that of the candidate. Raft
3429 * determines which of two logs is more up-to-date by comparing the index
3430 * and term of the last entries in the logs. If the logs have last entries
3431 * with different terms, then the log with the later term is more
3432 * up-to-date. If the logs end with the same term, then whichever log is
3433 * longer is more up-to-date." */
3434 uint64_t last_term
= (raft
->log_end
> raft
->log_start
3435 ? raft
->entries
[raft
->log_end
- 1
3436 - raft
->log_start
].term
3438 if (last_term
> rq
->last_log_term
3439 || (last_term
== rq
->last_log_term
3440 && raft
->log_end
- 1 > rq
->last_log_index
)) {
3441 /* Our log is more up-to-date than the peer's. Withhold vote. */
3445 /* Record a vote for the peer. */
3446 if (!raft_set_term(raft
, raft
->term
, &rq
->common
.sid
)) {
3450 raft_reset_election_timer(raft
);
3456 raft_send_vote_reply(struct raft
*raft
, const struct uuid
*dst
,
3457 const struct uuid
*vote
)
3459 union raft_rpc rpy
= {
3462 .type
= RAFT_RPC_VOTE_REPLY
,
3469 raft_send(raft
, &rpy
);
3473 raft_handle_vote_request(struct raft
*raft
,
3474 const struct raft_vote_request
*rq
)
3476 if (raft_handle_vote_request__(raft
, rq
)) {
3477 raft_send_vote_reply(raft
, &rq
->common
.sid
, &raft
->vote
);
3482 raft_handle_vote_reply(struct raft
*raft
,
3483 const struct raft_vote_reply
*rpy
)
3485 if (!raft_receive_term__(raft
, &rpy
->common
, rpy
->term
)) {
3489 if (raft
->role
!= RAFT_CANDIDATE
) {
3493 struct raft_server
*s
= raft_find_peer(raft
, &rpy
->common
.sid
);
3495 raft_accept_vote(raft
, s
, &rpy
->vote
);
3499 /* Returns true if 'raft''s log contains reconfiguration entries that have not
3500 * yet been committed. */
3502 raft_has_uncommitted_configuration(const struct raft
*raft
)
3504 for (uint64_t i
= raft
->commit_index
+ 1; i
< raft
->log_end
; i
++) {
3505 ovs_assert(i
>= raft
->log_start
);
3506 const struct raft_entry
*e
= &raft
->entries
[i
- raft
->log_start
];
3515 raft_log_reconfiguration(struct raft
*raft
)
3517 struct json
*servers_json
= raft_servers_to_json(&raft
->servers
);
3518 raft_command_unref(raft_command_execute__(
3519 raft
, NULL
, servers_json
, 0, NULL
, NULL
));
3520 json_destroy(servers_json
);
3524 raft_run_reconfigure(struct raft
*raft
)
3526 ovs_assert(raft
->role
== RAFT_LEADER
);
3528 /* Reconfiguration only progresses when configuration changes commit. */
3529 if (raft_has_uncommitted_configuration(raft
)) {
3533 /* If we were waiting for a configuration change to commit, it's done. */
3534 struct raft_server
*s
;
3535 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
3536 if (s
->phase
== RAFT_PHASE_COMMITTING
) {
3537 raft_send_add_server_reply__(raft
, &s
->sid
, s
->address
,
3538 true, RAFT_SERVER_COMPLETED
);
3539 s
->phase
= RAFT_PHASE_STABLE
;
3542 if (raft
->remove_server
) {
3543 raft_send_remove_server_reply__(raft
, &raft
->remove_server
->sid
,
3544 &raft
->remove_server
->requester_sid
,
3545 raft
->remove_server
->requester_conn
,
3546 true, RAFT_SERVER_COMPLETED
);
3547 raft_server_destroy(raft
->remove_server
);
3548 raft
->remove_server
= NULL
;
3551 /* If a new server is caught up, add it to the configuration. */
3552 HMAP_FOR_EACH (s
, hmap_node
, &raft
->add_servers
) {
3553 if (s
->phase
== RAFT_PHASE_CAUGHT_UP
) {
3554 /* Move 's' from 'raft->add_servers' to 'raft->servers'. */
3555 hmap_remove(&raft
->add_servers
, &s
->hmap_node
);
3556 hmap_insert(&raft
->servers
, &s
->hmap_node
, uuid_hash(&s
->sid
));
3558 /* Mark 's' as waiting for commit. */
3559 s
->phase
= RAFT_PHASE_COMMITTING
;
3561 raft_log_reconfiguration(raft
);
3563 /* When commit completes we'll transition to RAFT_PHASE_STABLE and
3564 * send a RAFT_SERVER_OK reply. */
3570 /* Remove a server, if one is scheduled for removal. */
3571 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
3572 if (s
->phase
== RAFT_PHASE_REMOVE
) {
3573 hmap_remove(&raft
->servers
, &s
->hmap_node
);
3574 raft
->remove_server
= s
;
3576 raft_log_reconfiguration(raft
);
3584 raft_handle_add_server_request(struct raft
*raft
,
3585 const struct raft_add_server_request
*rq
)
3587 /* Figure 4.1: "1. Reply NOT_LEADER if not leader (section 6.2)." */
3588 if (raft
->role
!= RAFT_LEADER
) {
3589 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_NOT_LEADER
);
3593 /* Check for an existing server. */
3594 struct raft_server
*s
= raft_find_server(raft
, &rq
->common
.sid
);
3596 /* If the server is scheduled to be removed, cancel it. */
3597 if (s
->phase
== RAFT_PHASE_REMOVE
) {
3598 s
->phase
= RAFT_PHASE_STABLE
;
3599 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_CANCELED
);
3603 /* If the server is being added, then it's in progress. */
3604 if (s
->phase
!= RAFT_PHASE_STABLE
) {
3605 raft_send_add_server_reply(raft
, rq
,
3606 false, RAFT_SERVER_IN_PROGRESS
);
3609 /* Nothing to do--server is already part of the configuration. */
3610 raft_send_add_server_reply(raft
, rq
,
3611 true, RAFT_SERVER_ALREADY_PRESENT
);
3615 /* Check for a server being removed. */
3616 if (raft
->remove_server
3617 && uuid_equals(&rq
->common
.sid
, &raft
->remove_server
->sid
)) {
3618 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_COMMITTING
);
3622 /* Check for a server already being added. */
3623 if (raft_find_new_server(raft
, &rq
->common
.sid
)) {
3624 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_IN_PROGRESS
);
3628 /* Add server to 'add_servers'. */
3629 s
= raft_server_add(&raft
->add_servers
, &rq
->common
.sid
, rq
->address
);
3630 raft_server_init_leader(raft
, s
);
3631 s
->requester_sid
= rq
->common
.sid
;
3632 s
->requester_conn
= NULL
;
3633 s
->phase
= RAFT_PHASE_CATCHUP
;
3635 /* Start sending the log. If this is the first time we've tried to add
3636 * this server, then this will quickly degenerate into an InstallSnapshot
3637 * followed by a series of AddEntries, but if it's a retry of an earlier
3638 * AddRequest that was interrupted (e.g. by a timeout or a loss of
3639 * leadership) then it will gracefully resume populating the log.
3641 * See the last few paragraphs of section 4.2.1 for further insight. */
3642 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3644 "starting to add server %s ("SID_FMT
" at %s) "
3645 "to cluster "CID_FMT
, s
->nickname
, SID_ARGS(&s
->sid
),
3646 rq
->address
, CID_ARGS(&raft
->cid
));
3647 raft_send_append_request(raft
, s
, 0, "initialize new server");
3651 raft_handle_add_server_reply(struct raft
*raft
,
3652 const struct raft_add_server_reply
*rpy
)
3654 if (!raft
->joining
) {
3655 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3656 VLOG_WARN_RL(&rl
, "received add_server_reply even though we're "
3657 "already part of the cluster");
3662 raft
->joining
= false;
3664 /* It is tempting, at this point, to check that this server is part of
3665 * the current configuration. However, this is not necessarily the
3666 * case, because the log entry that added this server to the cluster
3667 * might have been committed by a majority of the cluster that does not
3668 * include this one. This actually happens in testing. */
3670 const char *address
;
3671 SSET_FOR_EACH (address
, &rpy
->remote_addresses
) {
3672 if (sset_add(&raft
->remote_addresses
, address
)) {
3673 VLOG_INFO("%s: learned new server address for joining cluster",
3680 /* This is called by raft_unixctl_kick() as well as via RPC. */
3682 raft_handle_remove_server_request(struct raft
*raft
,
3683 const struct raft_remove_server_request
*rq
)
3685 /* Figure 4.1: "1. Reply NOT_LEADER if not leader (section 6.2)." */
3686 if (raft
->role
!= RAFT_LEADER
) {
3687 raft_send_remove_server_reply(raft
, rq
, false, RAFT_SERVER_NOT_LEADER
);
3691 /* If the server to remove is currently waiting to be added, cancel it. */
3692 struct raft_server
*target
= raft_find_new_server(raft
, &rq
->sid
);
3694 raft_send_add_server_reply__(raft
, &target
->sid
, target
->address
,
3695 false, RAFT_SERVER_CANCELED
);
3696 hmap_remove(&raft
->add_servers
, &target
->hmap_node
);
3697 raft_server_destroy(target
);
3701 /* If the server isn't configured, report that. */
3702 target
= raft_find_server(raft
, &rq
->sid
);
3704 raft_send_remove_server_reply(raft
, rq
,
3705 true, RAFT_SERVER_ALREADY_GONE
);
3709 /* Check whether we're waiting for the addition of the server to commit. */
3710 if (target
->phase
== RAFT_PHASE_COMMITTING
) {
3711 raft_send_remove_server_reply(raft
, rq
, false, RAFT_SERVER_COMMITTING
);
3715 /* Check whether the server is already scheduled for removal. */
3716 if (target
->phase
== RAFT_PHASE_REMOVE
) {
3717 raft_send_remove_server_reply(raft
, rq
,
3718 false, RAFT_SERVER_IN_PROGRESS
);
3722 /* Make sure that if we remove this server then that at least one other
3723 * server will be left. We don't count servers currently being added (in
3724 * 'add_servers') since those could fail. */
3725 struct raft_server
*s
;
3727 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
3728 if (s
!= target
&& s
->phase
!= RAFT_PHASE_REMOVE
) {
3733 raft_send_remove_server_reply(raft
, rq
, false, RAFT_SERVER_EMPTY
);
3737 /* Mark the server for removal. */
3738 target
->phase
= RAFT_PHASE_REMOVE
;
3739 if (rq
->requester_conn
) {
3740 target
->requester_sid
= UUID_ZERO
;
3741 unixctl_command_reply(rq
->requester_conn
, "started removal");
3743 target
->requester_sid
= rq
->common
.sid
;
3744 target
->requester_conn
= NULL
;
3747 raft_run_reconfigure(raft
);
3748 /* Operation in progress, reply will be sent later. */
3752 raft_finished_leaving_cluster(struct raft
*raft
)
3754 VLOG_INFO(SID_FMT
": finished leaving cluster "CID_FMT
,
3755 SID_ARGS(&raft
->sid
), CID_ARGS(&raft
->cid
));
3757 raft_record_note(raft
, "left", "this server left the cluster");
3759 raft
->leaving
= false;
3764 raft_handle_remove_server_reply(struct raft
*raft
,
3765 const struct raft_remove_server_reply
*rpc
)
3768 && (uuid_is_zero(&rpc
->target_sid
)
3769 || uuid_equals(&rpc
->target_sid
, &raft
->sid
))) {
3770 raft_finished_leaving_cluster(raft
);
3775 raft_handle_write_error(struct raft
*raft
, struct ovsdb_error
*error
)
3777 if (error
&& !raft
->failed
) {
3778 raft
->failed
= true;
3780 char *s
= ovsdb_error_to_string_free(error
);
3781 VLOG_WARN("%s: entering failure mode due to I/O error (%s)",
3785 return !raft
->failed
;
3788 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
3789 raft_write_snapshot(struct raft
*raft
, struct ovsdb_log
*log
,
3790 uint64_t new_log_start
,
3791 const struct raft_entry
*new_snapshot
)
3793 struct raft_header h
= {
3797 .local_address
= raft
->local_address
,
3798 .snap_index
= new_log_start
- 1,
3799 .snap
= *new_snapshot
,
3801 struct ovsdb_error
*error
= ovsdb_log_write_and_free(
3802 log
, raft_header_to_json(&h
));
3806 ovsdb_log_mark_base(raft
->log
);
3808 /* Write log records. */
3809 for (uint64_t index
= new_log_start
; index
< raft
->log_end
; index
++) {
3810 const struct raft_entry
*e
= &raft
->entries
[index
- raft
->log_start
];
3811 struct raft_record r
= {
3812 .type
= RAFT_REC_ENTRY
,
3817 .servers
= e
->servers
,
3818 .election_timer
= e
->election_timer
,
3822 error
= ovsdb_log_write_and_free(log
, raft_record_to_json(&r
));
3828 /* Write term and vote (if any).
3830 * The term is redundant if we wrote a log record for that term above. The
3831 * vote, if any, is never redundant.
3833 error
= raft_write_state(log
, raft
->term
, &raft
->vote
);
3838 /* Write commit_index if it's beyond the new start of the log. */
3839 if (raft
->commit_index
>= new_log_start
) {
3840 struct raft_record r
= {
3841 .type
= RAFT_REC_COMMIT_INDEX
,
3842 .commit_index
= raft
->commit_index
,
3844 return ovsdb_log_write_and_free(log
, raft_record_to_json(&r
));
3849 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
3850 raft_save_snapshot(struct raft
*raft
,
3851 uint64_t new_start
, const struct raft_entry
*new_snapshot
)
3854 struct ovsdb_log
*new_log
;
3855 struct ovsdb_error
*error
;
3856 error
= ovsdb_log_replace_start(raft
->log
, &new_log
);
3861 error
= raft_write_snapshot(raft
, new_log
, new_start
, new_snapshot
);
3863 ovsdb_log_replace_abort(new_log
);
3867 return ovsdb_log_replace_commit(raft
->log
, new_log
);
3871 raft_handle_install_snapshot_request__(
3872 struct raft
*raft
, const struct raft_install_snapshot_request
*rq
)
3874 raft_reset_election_timer(raft
);
3877 * Our behavior here depend on new_log_start in the snapshot compared to
3878 * log_start and log_end. There are three cases:
3880 * Case 1 | Case 2 | Case 3
3881 * <---------------->|<------------->|<------------------>
3885 * T | T | T | T | T |
3891 uint64_t new_log_start
= rq
->last_index
+ 1;
3892 if (new_log_start
< raft
->log_start
) {
3893 /* Case 1: The new snapshot covers less than our current one. Nothing
3896 } else if (new_log_start
< raft
->log_end
) {
3897 /* Case 2: The new snapshot starts in the middle of our log. We could
3898 * discard the first 'new_log_start - raft->log_start' entries in the
3899 * log. But there's not much value in that, since snapshotting is
3900 * supposed to be a local decision. Just skip it. */
3904 /* Case 3: The new snapshot starts past the end of our current log, so
3905 * discard all of our current log. */
3906 const struct raft_entry new_snapshot
= {
3907 .term
= rq
->last_term
,
3909 .eid
= rq
->last_eid
,
3910 .servers
= rq
->last_servers
,
3911 .election_timer
= rq
->election_timer
,
3913 struct ovsdb_error
*error
= raft_save_snapshot(raft
, new_log_start
,
3916 char *error_s
= ovsdb_error_to_string(error
);
3917 VLOG_WARN("could not save snapshot: %s", error_s
);
3922 for (size_t i
= 0; i
< raft
->log_end
- raft
->log_start
; i
++) {
3923 raft_entry_uninit(&raft
->entries
[i
]);
3925 raft
->log_start
= raft
->log_end
= new_log_start
;
3926 raft
->log_synced
= raft
->log_end
- 1;
3927 raft
->commit_index
= raft
->log_start
- 1;
3928 if (raft
->last_applied
< raft
->commit_index
) {
3929 raft
->last_applied
= raft
->log_start
- 2;
3932 raft_entry_uninit(&raft
->snap
);
3933 raft_entry_clone(&raft
->snap
, &new_snapshot
);
3935 raft_get_servers_from_log(raft
, VLL_INFO
);
3936 raft_get_election_timer_from_log(raft
);
3942 raft_handle_install_snapshot_request(
3943 struct raft
*raft
, const struct raft_install_snapshot_request
*rq
)
3945 if (raft_handle_install_snapshot_request__(raft
, rq
)) {
3946 union raft_rpc rpy
= {
3947 .install_snapshot_reply
= {
3949 .type
= RAFT_RPC_INSTALL_SNAPSHOT_REPLY
,
3950 .sid
= rq
->common
.sid
,
3953 .last_index
= rq
->last_index
,
3954 .last_term
= rq
->last_term
,
3957 raft_send(raft
, &rpy
);
3962 raft_handle_install_snapshot_reply(
3963 struct raft
*raft
, const struct raft_install_snapshot_reply
*rpy
)
3965 /* We might get an InstallSnapshot reply from a configured server (e.g. a
3966 * peer) or a server in the process of being added. */
3967 struct raft_server
*s
= raft_find_peer(raft
, &rpy
->common
.sid
);
3969 s
= raft_find_new_server(raft
, &rpy
->common
.sid
);
3971 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3972 VLOG_INFO_RL(&rl
, "cluster "CID_FMT
": received %s from "
3973 "unknown server "SID_FMT
, CID_ARGS(&raft
->cid
),
3974 raft_rpc_type_to_string(rpy
->common
.type
),
3975 SID_ARGS(&rpy
->common
.sid
));
3980 if (rpy
->last_index
!= raft
->log_start
- 1 ||
3981 rpy
->last_term
!= raft
->snap
.term
) {
3982 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3983 VLOG_INFO_RL(&rl
, "cluster "CID_FMT
": server %s installed "
3984 "out-of-date snapshot, starting over",
3985 CID_ARGS(&raft
->cid
), s
->nickname
);
3986 raft_send_install_snapshot_request(raft
, s
,
3987 "installed obsolete snapshot");
3991 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3992 VLOG_INFO_RL(&rl
, "cluster "CID_FMT
": installed snapshot on server %s "
3993 " up to %"PRIu64
":%"PRIu64
, CID_ARGS(&raft
->cid
),
3994 s
->nickname
, rpy
->last_term
, rpy
->last_index
);
3995 s
->next_index
= raft
->log_end
;
3996 raft_send_append_request(raft
, s
, 0, "snapshot installed");
3999 /* Returns true if 'raft' has grown enough since the last snapshot that
4000 * reducing the log to a snapshot would be valuable, false otherwise. */
4002 raft_grew_lots(const struct raft
*raft
)
4004 return ovsdb_log_grew_lots(raft
->log
);
4007 /* Returns the number of log entries that could be trimmed off the on-disk log
4008 * by snapshotting. */
4010 raft_get_log_length(const struct raft
*raft
)
4012 return (raft
->last_applied
< raft
->log_start
4014 : raft
->last_applied
- raft
->log_start
+ 1);
4017 /* Returns true if taking a snapshot of 'raft', with raft_store_snapshot(), is
4020 raft_may_snapshot(const struct raft
*raft
)
4022 return (!raft
->joining
4026 && raft
->last_applied
>= raft
->log_start
);
4029 /* Replaces the log for 'raft', up to the last log entry read, by
4030 * 'new_snapshot_data'. Returns NULL if successful, otherwise an error that
4031 * the caller must eventually free.
4033 * This function can only succeed if raft_may_snapshot() returns true. It is
4034 * only valuable to call it if raft_get_log_length() is significant and
4035 * especially if raft_grew_lots() returns true. */
4036 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
4037 raft_store_snapshot(struct raft
*raft
, const struct json
*new_snapshot_data
)
4039 if (raft
->joining
) {
4040 return ovsdb_error(NULL
,
4041 "cannot store a snapshot while joining cluster");
4042 } else if (raft
->leaving
) {
4043 return ovsdb_error(NULL
,
4044 "cannot store a snapshot while leaving cluster");
4045 } else if (raft
->left
) {
4046 return ovsdb_error(NULL
,
4047 "cannot store a snapshot after leaving cluster");
4048 } else if (raft
->failed
) {
4049 return ovsdb_error(NULL
,
4050 "cannot store a snapshot following failure");
4053 if (raft
->last_applied
< raft
->log_start
) {
4054 return ovsdb_error(NULL
, "not storing a duplicate snapshot");
4057 uint64_t new_log_start
= raft
->last_applied
+ 1;
4058 struct raft_entry new_snapshot
= {
4059 .term
= raft_get_term(raft
, new_log_start
- 1),
4060 .data
= json_clone(new_snapshot_data
),
4061 .eid
= *raft_get_eid(raft
, new_log_start
- 1),
4062 .servers
= json_clone(raft_servers_for_index(raft
, new_log_start
- 1)),
4063 .election_timer
= raft
->election_timer
,
4065 struct ovsdb_error
*error
= raft_save_snapshot(raft
, new_log_start
,
4068 raft_entry_uninit(&new_snapshot
);
4072 raft
->log_synced
= raft
->log_end
- 1;
4073 raft_entry_uninit(&raft
->snap
);
4074 raft
->snap
= new_snapshot
;
4075 for (size_t i
= 0; i
< new_log_start
- raft
->log_start
; i
++) {
4076 raft_entry_uninit(&raft
->entries
[i
]);
4078 memmove(&raft
->entries
[0], &raft
->entries
[new_log_start
- raft
->log_start
],
4079 (raft
->log_end
- new_log_start
) * sizeof *raft
->entries
);
4080 raft
->log_start
= new_log_start
;
4085 raft_handle_become_leader(struct raft
*raft
,
4086 const struct raft_become_leader
*rq
)
4088 if (raft
->role
== RAFT_FOLLOWER
) {
4089 char buf
[SID_LEN
+ 1];
4090 VLOG_INFO("received leadership transfer from %s in term %"PRIu64
,
4091 raft_get_nickname(raft
, &rq
->common
.sid
, buf
, sizeof buf
),
4093 raft_start_election(raft
, true);
4098 raft_send_execute_command_reply(struct raft
*raft
,
4099 const struct uuid
*sid
,
4100 const struct uuid
*eid
,
4101 enum raft_command_status status
,
4102 uint64_t commit_index
)
4104 if (failure_test
== FT_CRASH_BEFORE_SEND_EXEC_REP
) {
4105 ovs_fatal(0, "Raft test: crash before sending execute_command_reply");
4107 union raft_rpc rpc
= {
4108 .execute_command_reply
= {
4110 .type
= RAFT_RPC_EXECUTE_COMMAND_REPLY
,
4115 .commit_index
= commit_index
,
4118 raft_send(raft
, &rpc
);
4119 if (failure_test
== FT_CRASH_AFTER_SEND_EXEC_REP
) {
4120 ovs_fatal(0, "Raft test: crash after sending execute_command_reply.");
4124 static enum raft_command_status
4125 raft_handle_execute_command_request__(
4126 struct raft
*raft
, const struct raft_execute_command_request
*rq
)
4128 if (raft
->role
!= RAFT_LEADER
) {
4129 return RAFT_CMD_NOT_LEADER
;
4132 const struct uuid
*current_eid
= raft_current_eid(raft
);
4133 if (!uuid_equals(&rq
->prereq
, current_eid
)) {
4134 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
4135 VLOG_INFO_RL(&rl
, "current entry eid "UUID_FMT
" does not match "
4136 "prerequisite "UUID_FMT
" in execute_command_request",
4137 UUID_ARGS(current_eid
), UUID_ARGS(&rq
->prereq
));
4138 return RAFT_CMD_BAD_PREREQ
;
4141 struct raft_command
*cmd
= raft_command_initiate(raft
, rq
->data
,
4142 NULL
, 0, &rq
->result
);
4143 cmd
->sid
= rq
->common
.sid
;
4145 enum raft_command_status status
= cmd
->status
;
4146 if (status
!= RAFT_CMD_INCOMPLETE
) {
4147 raft_command_unref(cmd
);
4153 raft_handle_execute_command_request(
4154 struct raft
*raft
, const struct raft_execute_command_request
*rq
)
4156 enum raft_command_status status
4157 = raft_handle_execute_command_request__(raft
, rq
);
4158 if (status
!= RAFT_CMD_INCOMPLETE
) {
4159 raft_send_execute_command_reply(raft
, &rq
->common
.sid
, &rq
->result
,
4165 raft_handle_execute_command_reply(
4166 struct raft
*raft
, const struct raft_execute_command_reply
*rpy
)
4168 struct raft_command
*cmd
= raft_find_command_by_eid(raft
, &rpy
->result
);
4170 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
4171 char buf
[SID_LEN
+ 1];
4173 "%s received \"%s\" reply from %s for unknown command",
4174 raft
->local_nickname
,
4175 raft_command_status_to_string(rpy
->status
),
4176 raft_get_nickname(raft
, &rpy
->common
.sid
,
4181 if (rpy
->status
== RAFT_CMD_INCOMPLETE
) {
4182 cmd
->timestamp
= time_msec();
4184 cmd
->index
= rpy
->commit_index
;
4185 raft_command_complete(raft
, cmd
, rpy
->status
);
4190 raft_handle_rpc(struct raft
*raft
, const union raft_rpc
*rpc
)
4192 uint64_t term
= raft_rpc_get_term(rpc
);
4194 && !raft_should_suppress_disruptive_server(raft
, rpc
)
4195 && !raft_receive_term__(raft
, &rpc
->common
, term
)) {
4196 if (rpc
->type
== RAFT_RPC_APPEND_REQUEST
) {
4197 /* Section 3.3: "If a server receives a request with a stale term
4198 * number, it rejects the request." */
4199 raft_send_append_reply(raft
, raft_append_request_cast(rpc
),
4200 RAFT_APPEND_INCONSISTENCY
, "stale term");
4205 switch (rpc
->type
) {
4206 #define RAFT_RPC(ENUM, NAME) \
4208 raft_handle_##NAME(raft, &rpc->NAME); \
4218 raft_rpc_is_heartbeat(const union raft_rpc
*rpc
)
4220 return ((rpc
->type
== RAFT_RPC_APPEND_REQUEST
4221 || rpc
->type
== RAFT_RPC_APPEND_REPLY
)
4222 && rpc
->common
.comment
4223 && !strcmp(rpc
->common
.comment
, "heartbeat"));
4228 raft_send_to_conn_at(struct raft
*raft
, const union raft_rpc
*rpc
,
4229 struct raft_conn
*conn
, int line_number
)
4231 log_rpc(rpc
, "-->", conn
, line_number
);
4232 return !jsonrpc_session_send(
4233 conn
->js
, raft_rpc_to_jsonrpc(&raft
->cid
, &raft
->sid
, rpc
));
4237 raft_is_rpc_synced(const struct raft
*raft
, const union raft_rpc
*rpc
)
4239 uint64_t term
= raft_rpc_get_term(rpc
);
4240 uint64_t index
= raft_rpc_get_min_sync_index(rpc
);
4241 const struct uuid
*vote
= raft_rpc_get_vote(rpc
);
4243 return (term
<= raft
->synced_term
4244 && index
<= raft
->log_synced
4245 && (!vote
|| uuid_equals(vote
, &raft
->synced_vote
)));
4249 raft_send_at(struct raft
*raft
, const union raft_rpc
*rpc
, int line_number
)
4251 const struct uuid
*dst
= &rpc
->common
.sid
;
4252 if (uuid_equals(dst
, &raft
->sid
)) {
4253 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
4254 VLOG_WARN_RL(&rl
, "attempted to send RPC to self from raft.c:%d",
4259 struct raft_conn
*conn
= raft_find_conn_by_sid(raft
, dst
);
4261 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
4262 char buf
[SID_LEN
+ 1];
4263 VLOG_DBG_RL(&rl
, "%s: no connection to %s, cannot send RPC "
4264 "from raft.c:%d", raft
->local_nickname
,
4265 raft_get_nickname(raft
, dst
, buf
, sizeof buf
),
4270 if (!raft_is_rpc_synced(raft
, rpc
)) {
4271 raft_waiter_create(raft
, RAFT_W_RPC
, false)->rpc
= raft_rpc_clone(rpc
);
4275 return raft_send_to_conn_at(raft
, rpc
, conn
, line_number
);
4278 static struct raft
*
4279 raft_lookup_by_name(const char *name
)
4283 HMAP_FOR_EACH_WITH_HASH (raft
, hmap_node
, hash_string(name
, 0),
4285 if (!strcmp(raft
->name
, name
)) {
4293 raft_unixctl_cid(struct unixctl_conn
*conn
,
4294 int argc OVS_UNUSED
, const char *argv
[],
4295 void *aux OVS_UNUSED
)
4297 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4299 unixctl_command_reply_error(conn
, "unknown cluster");
4300 } else if (uuid_is_zero(&raft
->cid
)) {
4301 unixctl_command_reply_error(conn
, "cluster id not yet known");
4303 char *uuid
= xasprintf(UUID_FMT
, UUID_ARGS(&raft
->cid
));
4304 unixctl_command_reply(conn
, uuid
);
4310 raft_unixctl_sid(struct unixctl_conn
*conn
,
4311 int argc OVS_UNUSED
, const char *argv
[],
4312 void *aux OVS_UNUSED
)
4314 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4316 unixctl_command_reply_error(conn
, "unknown cluster");
4318 char *uuid
= xasprintf(UUID_FMT
, UUID_ARGS(&raft
->sid
));
4319 unixctl_command_reply(conn
, uuid
);
4325 raft_put_sid(const char *title
, const struct uuid
*sid
,
4326 const struct raft
*raft
, struct ds
*s
)
4328 ds_put_format(s
, "%s: ", title
);
4329 if (uuid_equals(sid
, &raft
->sid
)) {
4330 ds_put_cstr(s
, "self");
4331 } else if (uuid_is_zero(sid
)) {
4332 ds_put_cstr(s
, "unknown");
4334 char buf
[SID_LEN
+ 1];
4335 ds_put_cstr(s
, raft_get_nickname(raft
, sid
, buf
, sizeof buf
));
4337 ds_put_char(s
, '\n');
4341 raft_unixctl_status(struct unixctl_conn
*conn
,
4342 int argc OVS_UNUSED
, const char *argv
[],
4343 void *aux OVS_UNUSED
)
4345 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4347 unixctl_command_reply_error(conn
, "unknown cluster");
4351 struct ds s
= DS_EMPTY_INITIALIZER
;
4352 ds_put_format(&s
, "%s\n", raft
->local_nickname
);
4353 ds_put_format(&s
, "Name: %s\n", raft
->name
);
4354 ds_put_format(&s
, "Cluster ID: ");
4355 if (!uuid_is_zero(&raft
->cid
)) {
4356 ds_put_format(&s
, CID_FMT
" ("UUID_FMT
")\n",
4357 CID_ARGS(&raft
->cid
), UUID_ARGS(&raft
->cid
));
4359 ds_put_format(&s
, "not yet known\n");
4361 ds_put_format(&s
, "Server ID: "SID_FMT
" ("UUID_FMT
")\n",
4362 SID_ARGS(&raft
->sid
), UUID_ARGS(&raft
->sid
));
4363 ds_put_format(&s
, "Address: %s\n", raft
->local_address
);
4364 ds_put_format(&s
, "Status: %s\n",
4365 raft
->joining
? "joining cluster"
4366 : raft
->leaving
? "leaving cluster"
4367 : raft
->left
? "left cluster"
4368 : raft
->failed
? "failed"
4369 : "cluster member");
4370 if (raft
->joining
) {
4371 ds_put_format(&s
, "Remotes for joining:");
4372 const char *address
;
4373 SSET_FOR_EACH (address
, &raft
->remote_addresses
) {
4374 ds_put_format(&s
, " %s", address
);
4376 ds_put_char(&s
, '\n');
4378 if (raft
->role
== RAFT_LEADER
) {
4379 struct raft_server
*as
;
4380 HMAP_FOR_EACH (as
, hmap_node
, &raft
->add_servers
) {
4381 ds_put_format(&s
, "Adding server %s ("SID_FMT
" at %s) (%s)\n",
4382 as
->nickname
, SID_ARGS(&as
->sid
), as
->address
,
4383 raft_server_phase_to_string(as
->phase
));
4386 struct raft_server
*rs
= raft
->remove_server
;
4388 ds_put_format(&s
, "Removing server %s ("SID_FMT
" at %s) (%s)\n",
4389 rs
->nickname
, SID_ARGS(&rs
->sid
), rs
->address
,
4390 raft_server_phase_to_string(rs
->phase
));
4394 ds_put_format(&s
, "Role: %s\n",
4395 raft
->role
== RAFT_LEADER
? "leader"
4396 : raft
->role
== RAFT_CANDIDATE
? "candidate"
4397 : raft
->role
== RAFT_FOLLOWER
? "follower"
4399 ds_put_format(&s
, "Term: %"PRIu64
"\n", raft
->term
);
4400 raft_put_sid("Leader", &raft
->leader_sid
, raft
, &s
);
4401 raft_put_sid("Vote", &raft
->vote
, raft
, &s
);
4402 ds_put_char(&s
, '\n');
4404 ds_put_format(&s
, "Election timer: %"PRIu64
, raft
->election_timer
);
4405 if (raft
->role
== RAFT_LEADER
&& raft
->election_timer_new
) {
4406 ds_put_format(&s
, " (changing to %"PRIu64
")",
4407 raft
->election_timer_new
);
4409 ds_put_char(&s
, '\n');
4411 ds_put_format(&s
, "Log: [%"PRIu64
", %"PRIu64
"]\n",
4412 raft
->log_start
, raft
->log_end
);
4414 uint64_t n_uncommitted
= raft
->log_end
- raft
->commit_index
- 1;
4415 ds_put_format(&s
, "Entries not yet committed: %"PRIu64
"\n", n_uncommitted
);
4417 uint64_t n_unapplied
= raft
->log_end
- raft
->last_applied
- 1;
4418 ds_put_format(&s
, "Entries not yet applied: %"PRIu64
"\n", n_unapplied
);
4420 const struct raft_conn
*c
;
4421 ds_put_cstr(&s
, "Connections:");
4422 LIST_FOR_EACH (c
, list_node
, &raft
->conns
) {
4423 bool connected
= jsonrpc_session_is_connected(c
->js
);
4424 ds_put_format(&s
, " %s%s%s%s",
4425 connected
? "" : "(",
4426 c
->incoming
? "<-" : "->", c
->nickname
,
4427 connected
? "" : ")");
4429 ds_put_char(&s
, '\n');
4431 ds_put_cstr(&s
, "Servers:\n");
4432 struct raft_server
*server
;
4433 HMAP_FOR_EACH (server
, hmap_node
, &raft
->servers
) {
4434 ds_put_format(&s
, " %s ("SID_FMT
" at %s)",
4436 SID_ARGS(&server
->sid
), server
->address
);
4437 if (uuid_equals(&server
->sid
, &raft
->sid
)) {
4438 ds_put_cstr(&s
, " (self)");
4440 if (server
->phase
!= RAFT_PHASE_STABLE
) {
4441 ds_put_format (&s
, " (%s)",
4442 raft_server_phase_to_string(server
->phase
));
4444 if (raft
->role
== RAFT_CANDIDATE
) {
4445 if (!uuid_is_zero(&server
->vote
)) {
4446 char buf
[SID_LEN
+ 1];
4447 ds_put_format(&s
, " (voted for %s)",
4448 raft_get_nickname(raft
, &server
->vote
,
4451 } else if (raft
->role
== RAFT_LEADER
) {
4452 ds_put_format(&s
, " next_index=%"PRIu64
" match_index=%"PRIu64
,
4453 server
->next_index
, server
->match_index
);
4455 ds_put_char(&s
, '\n');
4458 unixctl_command_reply(conn
, ds_cstr(&s
));
4463 raft_unixctl_leave__(struct unixctl_conn
*conn
, struct raft
*raft
)
4465 if (raft_is_leaving(raft
)) {
4466 unixctl_command_reply_error(conn
,
4467 "already in progress leaving cluster");
4468 } else if (raft_is_joining(raft
)) {
4469 unixctl_command_reply_error(conn
,
4470 "can't leave while join in progress");
4471 } else if (raft_failed(raft
)) {
4472 unixctl_command_reply_error(conn
,
4473 "can't leave after failure");
4476 unixctl_command_reply(conn
, NULL
);
4481 raft_unixctl_leave(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4482 const char *argv
[], void *aux OVS_UNUSED
)
4484 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4486 unixctl_command_reply_error(conn
, "unknown cluster");
4490 raft_unixctl_leave__(conn
, raft
);
4493 static struct raft_server
*
4494 raft_lookup_server_best_match(struct raft
*raft
, const char *id
)
4496 struct raft_server
*best
= NULL
;
4497 int best_score
= -1;
4500 struct raft_server
*s
;
4501 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
4502 int score
= (!strcmp(id
, s
->address
)
4504 : uuid_is_partial_match(&s
->sid
, id
));
4505 if (score
> best_score
) {
4509 } else if (score
== best_score
) {
4513 return n_best
== 1 ? best
: NULL
;
4517 raft_unixctl_kick(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4518 const char *argv
[], void *aux OVS_UNUSED
)
4520 const char *cluster_name
= argv
[1];
4521 const char *server_name
= argv
[2];
4523 struct raft
*raft
= raft_lookup_by_name(cluster_name
);
4525 unixctl_command_reply_error(conn
, "unknown cluster");
4529 struct raft_server
*server
= raft_lookup_server_best_match(raft
,
4532 unixctl_command_reply_error(conn
, "unknown server");
4536 if (uuid_equals(&server
->sid
, &raft
->sid
)) {
4537 raft_unixctl_leave__(conn
, raft
);
4538 } else if (raft
->role
== RAFT_LEADER
) {
4539 const struct raft_remove_server_request rq
= {
4541 .requester_conn
= conn
,
4543 raft_handle_remove_server_request(raft
, &rq
);
4545 const union raft_rpc rpc
= {
4546 .remove_server_request
= {
4548 .type
= RAFT_RPC_REMOVE_SERVER_REQUEST
,
4549 .sid
= raft
->leader_sid
,
4550 .comment
= "via unixctl"
4555 if (raft_send(raft
, &rpc
)) {
4556 unixctl_command_reply(conn
, "sent removal request to leader");
4558 unixctl_command_reply_error(conn
,
4559 "failed to send removal request");
4565 raft_get_election_timer_from_log(struct raft
*raft
)
4567 if (raft
->snap
.election_timer
) {
4568 raft
->election_timer
= raft
->snap
.election_timer
;
4570 for (uint64_t index
= raft
->commit_index
; index
>= raft
->log_start
;
4572 struct raft_entry
*e
= &raft
->entries
[index
- raft
->log_start
];
4573 if (e
->election_timer
) {
4574 raft
->election_timer
= e
->election_timer
;
4581 raft_log_election_timer(struct raft
*raft
)
4583 raft_command_unref(raft_command_execute__(raft
, NULL
, NULL
,
4584 raft
->election_timer_new
, NULL
,
4589 raft_unixctl_change_election_timer(struct unixctl_conn
*conn
,
4590 int argc OVS_UNUSED
, const char *argv
[],
4591 void *aux OVS_UNUSED
)
4593 const char *cluster_name
= argv
[1];
4594 const char *election_timer_str
= argv
[2];
4596 struct raft
*raft
= raft_lookup_by_name(cluster_name
);
4598 unixctl_command_reply_error(conn
, "unknown cluster");
4602 if (raft
->role
!= RAFT_LEADER
) {
4603 unixctl_command_reply_error(conn
, "election timer must be changed"
4604 " through leader.");
4608 /* If there are pending changes for election timer, reject it. */
4609 if (raft
->election_timer_new
) {
4610 unixctl_command_reply_error(conn
, "election timer change pending.");
4614 uint64_t election_timer
= atoll(election_timer_str
);
4615 if (election_timer
== raft
->election_timer
) {
4616 unixctl_command_reply(conn
, "change election timer to current value.");
4620 /* Election timer smaller than 100ms or bigger than 10min doesn't make
4622 if (election_timer
< 100 || election_timer
> 600000) {
4623 unixctl_command_reply_error(conn
, "election timer must be between "
4624 "100 and 600000, in msec.");
4628 /* If election timer is to be enlarged, it should be done gradually so that
4629 * it won't cause timeout when new value is applied on leader but not yet
4630 * applied on some of the followers. */
4631 if (election_timer
> raft
->election_timer
* 2) {
4632 unixctl_command_reply_error(conn
, "election timer increase should not "
4633 "exceed the current value x 2.");
4637 raft
->election_timer_new
= election_timer
;
4638 raft_log_election_timer(raft
);
4639 unixctl_command_reply(conn
, "change of election timer initiated.");
4643 raft_unixctl_failure_test(struct unixctl_conn
*conn OVS_UNUSED
,
4644 int argc OVS_UNUSED
, const char *argv
[],
4645 void *aux OVS_UNUSED
)
4647 const char *test
= argv
[1];
4648 if (!strcmp(test
, "crash-before-sending-append-request")) {
4649 failure_test
= FT_CRASH_BEFORE_SEND_APPEND_REQ
;
4650 } else if (!strcmp(test
, "crash-after-sending-append-request")) {
4651 failure_test
= FT_CRASH_AFTER_SEND_APPEND_REQ
;
4652 } else if (!strcmp(test
, "crash-before-sending-execute-command-reply")) {
4653 failure_test
= FT_CRASH_BEFORE_SEND_EXEC_REP
;
4654 } else if (!strcmp(test
, "crash-after-sending-execute-command-reply")) {
4655 failure_test
= FT_CRASH_AFTER_SEND_EXEC_REP
;
4656 } else if (!strcmp(test
, "crash-before-sending-execute-command-request")) {
4657 failure_test
= FT_CRASH_BEFORE_SEND_EXEC_REQ
;
4658 } else if (!strcmp(test
, "crash-after-sending-execute-command-request")) {
4659 failure_test
= FT_CRASH_AFTER_SEND_EXEC_REQ
;
4660 } else if (!strcmp(test
, "crash-after-receiving-append-request-update")) {
4661 failure_test
= FT_CRASH_AFTER_RECV_APPEND_REQ_UPDATE
;
4662 } else if (!strcmp(test
, "delay-election")) {
4663 failure_test
= FT_DELAY_ELECTION
;
4665 HMAP_FOR_EACH (raft
, hmap_node
, &all_rafts
) {
4666 if (raft
->role
== RAFT_FOLLOWER
) {
4667 raft_reset_election_timer(raft
);
4670 } else if (!strcmp(test
, "clear")) {
4671 failure_test
= FT_NO_TEST
;
4672 unixctl_command_reply(conn
, "test dismissed");
4675 unixctl_command_reply_error(conn
, "unknown test scenario");
4678 unixctl_command_reply(conn
, "test engaged");
4684 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
4685 if (!ovsthread_once_start(&once
)) {
4688 unixctl_command_register("cluster/cid", "DB", 1, 1,
4689 raft_unixctl_cid
, NULL
);
4690 unixctl_command_register("cluster/sid", "DB", 1, 1,
4691 raft_unixctl_sid
, NULL
);
4692 unixctl_command_register("cluster/status", "DB", 1, 1,
4693 raft_unixctl_status
, NULL
);
4694 unixctl_command_register("cluster/leave", "DB", 1, 1,
4695 raft_unixctl_leave
, NULL
);
4696 unixctl_command_register("cluster/kick", "DB SERVER", 2, 2,
4697 raft_unixctl_kick
, NULL
);
4698 unixctl_command_register("cluster/change-election-timer", "DB TIME", 2, 2,
4699 raft_unixctl_change_election_timer
, NULL
);
4700 unixctl_command_register("cluster/failure-test", "FAILURE SCENARIO", 1, 1,
4701 raft_unixctl_failure_test
, NULL
);
4702 ovsthread_once_done(&once
);