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 /* A connection between this Raft server and another one. */
68 struct ovs_list list_node
; /* In struct raft's 'conns' list. */
69 struct jsonrpc_session
*js
; /* JSON-RPC connection. */
70 struct uuid sid
; /* This server's unique ID. */
71 char *nickname
; /* Short name for use in log messages. */
72 bool incoming
; /* True if incoming, false if outgoing. */
73 unsigned int js_seqno
; /* Seqno for noticing (re)connections. */
76 static void raft_conn_close(struct raft_conn
*);
78 /* A "command", that is, a request to append an entry to the log.
80 * The Raft specification only allows clients to issue commands to the leader.
81 * With this implementation, clients may issue a command on any server, which
82 * then relays the command to the leader if necessary.
84 * This structure is thus used in three cases:
86 * 1. We are the leader and the command was issued to us directly.
88 * 2. We are a follower and relayed the command to the leader.
90 * 3. We are the leader and a follower relayed the command to us.
94 struct hmap_node hmap_node
; /* In struct raft's 'commands' hmap. */
95 unsigned int n_refs
; /* Reference count. */
96 enum raft_command_status status
; /* Execution status. */
99 uint64_t index
; /* Index in log (0 if being relayed). */
102 struct uuid eid
; /* Entry ID of result. */
105 long long int timestamp
; /* Issue or last ping time, for expiration. */
108 struct uuid sid
; /* The follower (otherwise UUID_ZERO). */
111 static void raft_command_complete(struct raft
*, struct raft_command
*,
112 enum raft_command_status
);
114 static void raft_complete_all_commands(struct raft
*,
115 enum raft_command_status
);
117 /* Type of deferred action, see struct raft_waiter. */
118 enum raft_waiter_type
{
124 /* An action deferred until a log write commits to disk. */
126 struct ovs_list list_node
;
127 uint64_t commit_ticket
;
129 enum raft_waiter_type type
;
133 * Waits for a RAFT_REC_ENTRY write to our local log to commit. Upon
134 * completion, updates 'log_synced' to indicate that the new log entry
135 * or entries are committed and, if we are leader, also updates our
136 * local 'match_index'. */
143 * Waits for a RAFT_REC_TERM or RAFT_REC_VOTE record write to commit.
144 * Upon completion, updates 'synced_term' and 'synced_vote', which
145 * triggers sending RPCs deferred by the uncommitted 'term' and
154 * Sometimes, sending an RPC to a peer must be delayed until an entry,
155 * a term, or a vote mentioned in the RPC is synced to disk. This
156 * waiter keeps a copy of such an RPC until the previous waiters have
162 static struct raft_waiter
*raft_waiter_create(struct raft
*,
163 enum raft_waiter_type
,
165 static void raft_waiters_destroy(struct raft
*);
167 /* The Raft state machine. */
169 struct hmap_node hmap_node
; /* In 'all_rafts'. */
170 struct ovsdb_log
*log
;
172 /* Persistent derived state.
174 * This must be updated on stable storage before responding to RPCs. It can be
175 * derived from the header, snapshot, and log in 'log'. */
177 struct uuid cid
; /* Cluster ID (immutable for the cluster). */
178 struct uuid sid
; /* Server ID (immutable for the server). */
179 char *local_address
; /* Local address (immutable for the server). */
180 char *local_nickname
; /* Used for local server in log messages. */
181 char *name
; /* Schema name (immutable for the cluster). */
183 /* Contains "struct raft_server"s and represents the server configuration
184 * most recently added to 'log'. */
187 /* Persistent state on all servers.
189 * Must be updated on stable storage before responding to RPCs. */
191 /* Current term and the vote for that term. These might be on the way to
193 uint64_t term
; /* Initialized to 0 and only increases. */
194 struct uuid vote
; /* All-zeros if no vote yet in 'term'. */
196 /* The term and vote that have been synced to disk. */
197 uint64_t synced_term
;
198 struct uuid synced_vote
;
202 * A log entry with index 1 never really exists; the initial snapshot for a
203 * Raft is considered to include this index. The first real log entry has
206 * A new Raft instance contains an empty log: log_start=2, log_end=2.
207 * Over time, the log grows: log_start=2, log_end=N.
208 * At some point, the server takes a snapshot: log_start=N, log_end=N.
209 * The log continues to grow: log_start=N, log_end=N+1...
211 * Must be updated on stable storage before responding to RPCs. */
212 struct raft_entry
*entries
; /* Log entry i is in log[i - log_start]. */
213 uint64_t log_start
; /* Index of first entry in log. */
214 uint64_t log_end
; /* Index of last entry in log, plus 1. */
215 uint64_t log_synced
; /* Index of last synced entry. */
216 size_t allocated_log
; /* Allocated entries in 'log'. */
218 /* Snapshot state (see Figure 5.1)
220 * This is the state of the cluster as of the last discarded log entry,
221 * that is, at log index 'log_start - 1' (called prevIndex in Figure 5.1).
222 * Only committed log entries can be included in a snapshot. */
223 struct raft_entry snap
;
227 * The snapshot is always committed, but the rest of the log might not be yet.
228 * 'last_applied' tracks what entries have been passed to the client. If the
229 * client hasn't yet read the latest snapshot, then even the snapshot isn't
230 * applied yet. Thus, the invariants are different for these members:
232 * log_start - 2 <= last_applied <= commit_index < log_end.
233 * log_start - 1 <= commit_index < log_end.
236 enum raft_role role
; /* Current role. */
237 uint64_t commit_index
; /* Max log index known to be committed. */
238 uint64_t last_applied
; /* Max log index applied to state machine. */
239 struct uuid leader_sid
; /* Server ID of leader (zero, if unknown). */
241 /* Followers and candidates only. */
242 #define ELECTION_BASE_MSEC 1024
243 #define ELECTION_RANGE_MSEC 1024
244 long long int election_base
; /* Time of last heartbeat from leader. */
245 long long int election_timeout
; /* Time at which we start an election. */
247 /* Used for joining a cluster. */
248 bool joining
; /* Attempting to join the cluster? */
249 struct sset remote_addresses
; /* Addresses to try to find other servers. */
250 long long int join_timeout
; /* Time to re-send add server request. */
252 /* Used for leaving a cluster. */
253 bool leaving
; /* True if we are leaving the cluster. */
254 bool left
; /* True if we have finished leaving. */
255 long long int leave_timeout
; /* Time to re-send remove server request. */
258 bool failed
; /* True if unrecoverable error has occurred. */
260 /* File synchronization. */
261 struct ovs_list waiters
; /* Contains "struct raft_waiter"s. */
263 /* Network connections. */
264 struct pstream
*listener
; /* For connections from other Raft servers. */
265 long long int listen_backoff
; /* For retrying creating 'listener'. */
266 struct ovs_list conns
; /* Contains struct raft_conns. */
268 /* Leaders only. Reinitialized after becoming leader. */
269 struct hmap add_servers
; /* Contains "struct raft_server"s to add. */
270 struct raft_server
*remove_server
; /* Server being removed. */
271 struct hmap commands
; /* Contains "struct raft_command"s. */
272 #define PING_TIME_MSEC (ELECTION_BASE_MSEC / 3)
273 long long int ping_timeout
; /* Time at which to send a heartbeat */
275 /* Candidates only. Reinitialized at start of election. */
276 int n_votes
; /* Number of votes for me. */
279 /* All Raft structures. */
280 static struct hmap all_rafts
= HMAP_INITIALIZER(&all_rafts
);
282 static void raft_init(void);
284 static struct ovsdb_error
*raft_read_header(struct raft
*)
285 OVS_WARN_UNUSED_RESULT
;
287 static void raft_send_execute_command_reply(struct raft
*,
288 const struct uuid
*sid
,
289 const struct uuid
*eid
,
290 enum raft_command_status
,
291 uint64_t commit_index
);
293 static void raft_update_our_match_index(struct raft
*, uint64_t min_index
);
295 static void raft_send_remove_server_reply__(
296 struct raft
*, const struct uuid
*target_sid
,
297 const struct uuid
*requester_sid
, struct unixctl_conn
*requester_conn
,
298 bool success
, const char *comment
);
299 static void raft_finished_leaving_cluster(struct raft
*);
301 static void raft_server_init_leader(struct raft
*, struct raft_server
*);
303 static bool raft_rpc_is_heartbeat(const union raft_rpc
*);
304 static bool raft_is_rpc_synced(const struct raft
*, const union raft_rpc
*);
306 static void raft_handle_rpc(struct raft
*, const union raft_rpc
*);
308 static bool raft_send_at(struct raft
*, const union raft_rpc
*,
310 #define raft_send(raft, rpc) raft_send_at(raft, rpc, __LINE__)
312 static bool raft_send_to_conn_at(struct raft
*, const union raft_rpc
*,
313 struct raft_conn
*, int line_number
);
314 #define raft_send_to_conn(raft, rpc, conn) \
315 raft_send_to_conn_at(raft, rpc, conn, __LINE__)
317 static void raft_send_append_request(struct raft
*,
318 struct raft_server
*, unsigned int n
,
319 const char *comment
);
321 static void raft_become_leader(struct raft
*);
322 static void raft_become_follower(struct raft
*);
323 static void raft_reset_timer(struct raft
*);
324 static void raft_send_heartbeats(struct raft
*);
325 static void raft_start_election(struct raft
*, bool leadership_transfer
);
326 static bool raft_truncate(struct raft
*, uint64_t new_end
);
327 static void raft_get_servers_from_log(struct raft
*, enum vlog_level
);
329 static bool raft_handle_write_error(struct raft
*, struct ovsdb_error
*);
331 static void raft_run_reconfigure(struct raft
*);
333 static struct raft_server
*
334 raft_find_server(const struct raft
*raft
, const struct uuid
*sid
)
336 return raft_server_find(&raft
->servers
, sid
);
340 raft_make_address_passive(const char *address_
)
342 if (!strncmp(address_
, "unix:", 5)) {
343 return xasprintf("p%s", address_
);
345 char *address
= xstrdup(address_
);
347 inet_parse_host_port_tokens(strchr(address
, ':') + 1, &host
, &port
);
349 struct ds paddr
= DS_EMPTY_INITIALIZER
;
350 ds_put_format(&paddr
, "p%.3s:%s:", address
, port
);
351 if (strchr(host
, ':')) {
352 ds_put_format(&paddr
, "[%s]", host
);
354 ds_put_cstr(&paddr
, host
);
357 return ds_steal_cstr(&paddr
);
366 struct raft
*raft
= xzalloc(sizeof *raft
);
367 hmap_node_nullify(&raft
->hmap_node
);
368 hmap_init(&raft
->servers
);
369 raft
->log_start
= raft
->log_end
= 1;
370 raft
->role
= RAFT_FOLLOWER
;
371 sset_init(&raft
->remote_addresses
);
372 raft
->join_timeout
= LLONG_MAX
;
373 ovs_list_init(&raft
->waiters
);
374 raft
->listen_backoff
= LLONG_MIN
;
375 ovs_list_init(&raft
->conns
);
376 hmap_init(&raft
->add_servers
);
377 hmap_init(&raft
->commands
);
379 raft
->ping_timeout
= time_msec() + PING_TIME_MSEC
;
380 raft_reset_timer(raft
);
385 /* Creates an on-disk file that represents a new Raft cluster and initializes
386 * it to consist of a single server, the one on which this function is called.
388 * Creates the local copy of the cluster's log in 'file_name', which must not
389 * already exist. Gives it the name 'name', which should be the database
390 * schema name and which is used only to match up this database with the server
391 * added to the cluster later if the cluster ID is unavailable.
393 * The new server is located at 'local_address', which must take one of the
394 * forms "tcp:IP:PORT" or "ssl:IP:PORT", where IP is an IPv4 address or a
395 * square bracket enclosed IPv6 address and PORT is a TCP port number.
397 * This only creates the on-disk file. Use raft_open() to start operating the
400 * Returns null if successful, otherwise an ovsdb_error describing the
402 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
403 raft_create_cluster(const char *file_name
, const char *name
,
404 const char *local_address
, const struct json
*data
)
406 /* Parse and verify validity of the local address. */
407 struct ovsdb_error
*error
= raft_address_validate(local_address
);
412 /* Create log file. */
413 struct ovsdb_log
*log
;
414 error
= ovsdb_log_open(file_name
, RAFT_MAGIC
, OVSDB_LOG_CREATE_EXCL
,
420 /* Write log file. */
421 struct raft_header h
= {
422 .sid
= uuid_random(),
423 .cid
= uuid_random(),
424 .name
= xstrdup(name
),
425 .local_address
= xstrdup(local_address
),
427 .remote_addresses
= SSET_INITIALIZER(&h
.remote_addresses
),
431 .data
= json_nullable_clone(data
),
432 .eid
= uuid_random(),
433 .servers
= json_object_create(),
436 shash_add_nocopy(json_object(h
.snap
.servers
),
437 xasprintf(UUID_FMT
, UUID_ARGS(&h
.sid
)),
438 json_string_create(local_address
));
439 error
= ovsdb_log_write_and_free(log
, raft_header_to_json(&h
));
440 raft_header_uninit(&h
);
442 error
= ovsdb_log_commit_block(log
);
444 ovsdb_log_close(log
);
449 /* Creates a database file that represents a new server in an existing Raft
452 * Creates the local copy of the cluster's log in 'file_name', which must not
453 * already exist. Gives it the name 'name', which must be the same name
454 * passed in to raft_create_cluster() earlier.
456 * 'cid' is optional. If specified, the new server will join only the cluster
457 * with the given cluster ID.
459 * The new server is located at 'local_address', which must take one of the
460 * forms "tcp:IP:PORT" or "ssl:IP:PORT", where IP is an IPv4 address or a
461 * square bracket enclosed IPv6 address and PORT is a TCP port number.
463 * Joining the cluster requires contacting it. Thus, 'remote_addresses'
464 * specifies the addresses of existing servers in the cluster. One server out
465 * of the existing cluster is sufficient, as long as that server is reachable
466 * and not partitioned from the current cluster leader. If multiple servers
467 * from the cluster are specified, then it is sufficient for any of them to
468 * meet this criterion.
470 * This only creates the on-disk file and does no network access. Use
471 * raft_open() to start operating the new server. (Until this happens, the
472 * new server has not joined the cluster.)
474 * Returns null if successful, otherwise an ovsdb_error describing the
476 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
477 raft_join_cluster(const char *file_name
,
478 const char *name
, const char *local_address
,
479 const struct sset
*remote_addresses
,
480 const struct uuid
*cid
)
482 ovs_assert(!sset_is_empty(remote_addresses
));
484 /* Parse and verify validity of the addresses. */
485 struct ovsdb_error
*error
= raft_address_validate(local_address
);
490 SSET_FOR_EACH (addr
, remote_addresses
) {
491 error
= raft_address_validate(addr
);
495 if (!strcmp(addr
, local_address
)) {
496 return ovsdb_error(NULL
, "remote addresses cannot be the same "
497 "as the local address");
501 /* Verify validity of the cluster ID (if provided). */
502 if (cid
&& uuid_is_zero(cid
)) {
503 return ovsdb_error(NULL
, "all-zero UUID is not valid cluster ID");
506 /* Create log file. */
507 struct ovsdb_log
*log
;
508 error
= ovsdb_log_open(file_name
, RAFT_MAGIC
, OVSDB_LOG_CREATE_EXCL
,
514 /* Write log file. */
515 struct raft_header h
= {
516 .sid
= uuid_random(),
517 .cid
= cid
? *cid
: UUID_ZERO
,
518 .name
= xstrdup(name
),
519 .local_address
= xstrdup(local_address
),
521 /* No snapshot yet. */
523 sset_clone(&h
.remote_addresses
, remote_addresses
);
524 error
= ovsdb_log_write_and_free(log
, raft_header_to_json(&h
));
525 raft_header_uninit(&h
);
527 error
= ovsdb_log_commit_block(log
);
529 ovsdb_log_close(log
);
534 /* Reads the initial header record from 'log', which must be a Raft clustered
535 * database log, and populates '*md' with the information read from it. The
536 * caller must eventually destroy 'md' with raft_metadata_destroy().
538 * On success, returns NULL. On failure, returns an error that the caller must
539 * eventually destroy and zeros '*md'. */
540 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
541 raft_read_metadata(struct ovsdb_log
*log
, struct raft_metadata
*md
)
543 struct raft
*raft
= raft_alloc();
546 struct ovsdb_error
*error
= raft_read_header(raft
);
549 md
->name
= xstrdup(raft
->name
);
550 md
->local
= xstrdup(raft
->local_address
);
553 memset(md
, 0, sizeof *md
);
561 /* Frees the metadata in 'md'. */
563 raft_metadata_destroy(struct raft_metadata
*md
)
571 static const struct raft_entry
*
572 raft_get_entry(const struct raft
*raft
, uint64_t index
)
574 ovs_assert(index
>= raft
->log_start
);
575 ovs_assert(index
< raft
->log_end
);
576 return &raft
->entries
[index
- raft
->log_start
];
580 raft_get_term(const struct raft
*raft
, uint64_t index
)
582 return (index
== raft
->log_start
- 1
584 : raft_get_entry(raft
, index
)->term
);
587 static const struct json
*
588 raft_servers_for_index(const struct raft
*raft
, uint64_t index
)
590 ovs_assert(index
>= raft
->log_start
- 1);
591 ovs_assert(index
< raft
->log_end
);
593 const struct json
*servers
= raft
->snap
.servers
;
594 for (uint64_t i
= raft
->log_start
; i
<= index
; i
++) {
595 const struct raft_entry
*e
= raft_get_entry(raft
, i
);
597 servers
= e
->servers
;
604 raft_set_servers(struct raft
*raft
, const struct hmap
*new_servers
,
605 enum vlog_level level
)
607 struct raft_server
*s
, *next
;
608 HMAP_FOR_EACH_SAFE (s
, next
, hmap_node
, &raft
->servers
) {
609 if (!raft_server_find(new_servers
, &s
->sid
)) {
610 ovs_assert(s
!= raft
->remove_server
);
612 hmap_remove(&raft
->servers
, &s
->hmap_node
);
613 VLOG(level
, "server %s removed from configuration", s
->nickname
);
614 raft_server_destroy(s
);
618 HMAP_FOR_EACH_SAFE (s
, next
, hmap_node
, new_servers
) {
619 if (!raft_find_server(raft
, &s
->sid
)) {
620 VLOG(level
, "server %s added to configuration", s
->nickname
);
622 struct raft_server
*new
623 = raft_server_add(&raft
->servers
, &s
->sid
, s
->address
);
624 raft_server_init_leader(raft
, new);
630 raft_add_entry(struct raft
*raft
,
631 uint64_t term
, struct json
*data
, const struct uuid
*eid
,
632 struct json
*servers
)
634 if (raft
->log_end
- raft
->log_start
>= raft
->allocated_log
) {
635 raft
->entries
= x2nrealloc(raft
->entries
, &raft
->allocated_log
,
636 sizeof *raft
->entries
);
639 uint64_t index
= raft
->log_end
++;
640 struct raft_entry
*entry
= &raft
->entries
[index
- raft
->log_start
];
643 entry
->eid
= eid
? *eid
: UUID_ZERO
;
644 entry
->servers
= servers
;
648 /* Writes a RAFT_REC_ENTRY record for 'term', 'data', 'eid', 'servers' to
649 * 'raft''s log and returns an error indication. */
650 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
651 raft_write_entry(struct raft
*raft
, uint64_t term
, struct json
*data
,
652 const struct uuid
*eid
, struct json
*servers
)
654 struct raft_record r
= {
655 .type
= RAFT_REC_ENTRY
,
658 .index
= raft_add_entry(raft
, term
, data
, eid
, servers
),
661 .eid
= eid
? *eid
: UUID_ZERO
,
664 return ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
));
667 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
668 raft_write_state(struct ovsdb_log
*log
,
669 uint64_t term
, const struct uuid
*vote
)
671 struct raft_record r
= { .term
= term
};
672 if (vote
&& !uuid_is_zero(vote
)) {
673 r
.type
= RAFT_REC_VOTE
;
676 r
.type
= RAFT_REC_TERM
;
678 return ovsdb_log_write_and_free(log
, raft_record_to_json(&r
));
681 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
682 raft_apply_record(struct raft
*raft
, unsigned long long int rec_idx
,
683 const struct raft_record
*r
)
685 /* Apply "term", which is present in most kinds of records (and otherwise
688 * A Raft leader can replicate entries from previous terms to the other
689 * servers in the cluster, retaining the original terms on those entries
690 * (see section 3.6.2 "Committing entries from previous terms" for more
691 * information), so it's OK for the term in a log record to precede the
693 if (r
->term
> raft
->term
) {
694 raft
->term
= raft
->synced_term
= r
->term
;
695 raft
->vote
= raft
->synced_vote
= UUID_ZERO
;
700 if (r
->entry
.index
< raft
->commit_index
) {
701 return ovsdb_error(NULL
, "record %llu attempts to truncate log "
702 "from %"PRIu64
" to %"PRIu64
" entries, but "
703 "commit index is already %"PRIu64
,
704 rec_idx
, raft
->log_end
, r
->entry
.index
,
706 } else if (r
->entry
.index
> raft
->log_end
) {
707 return ovsdb_error(NULL
, "record %llu with index %"PRIu64
" skips "
708 "past expected index %"PRIu64
,
709 rec_idx
, r
->entry
.index
, raft
->log_end
);
712 if (r
->entry
.index
< raft
->log_end
) {
713 /* This can happen, but it is notable. */
714 VLOG_DBG("record %llu truncates log from %"PRIu64
" to %"PRIu64
715 " entries", rec_idx
, raft
->log_end
, r
->entry
.index
);
716 raft_truncate(raft
, r
->entry
.index
);
719 uint64_t prev_term
= (raft
->log_end
> raft
->log_start
720 ? raft
->entries
[raft
->log_end
721 - raft
->log_start
- 1].term
723 if (r
->term
< prev_term
) {
724 return ovsdb_error(NULL
, "record %llu with index %"PRIu64
" term "
725 "%"PRIu64
" precedes previous entry's term "
727 rec_idx
, r
->entry
.index
, r
->term
, prev_term
);
730 raft
->log_synced
= raft_add_entry(
732 json_nullable_clone(r
->entry
.data
), &r
->entry
.eid
,
733 json_nullable_clone(r
->entry
.servers
));
740 if (r
->term
< raft
->term
) {
741 return ovsdb_error(NULL
, "record %llu votes for term %"PRIu64
" "
742 "but current term is %"PRIu64
,
743 rec_idx
, r
->term
, raft
->term
);
744 } else if (!uuid_is_zero(&raft
->vote
)
745 && !uuid_equals(&raft
->vote
, &r
->sid
)) {
746 return ovsdb_error(NULL
, "record %llu votes for "SID_FMT
" in term "
747 "%"PRIu64
" but a previous record for the "
748 "same term voted for "SID_FMT
, rec_idx
,
749 SID_ARGS(&raft
->vote
), r
->term
,
752 raft
->vote
= raft
->synced_vote
= r
->sid
;
758 if (!strcmp(r
->note
, "left")) {
759 return ovsdb_error(NULL
, "record %llu indicates server has left "
760 "the cluster; it cannot be added back (use "
761 "\"ovsdb-tool join-cluster\" to add a new "
766 case RAFT_REC_COMMIT_INDEX
:
767 if (r
->commit_index
< raft
->commit_index
) {
768 return ovsdb_error(NULL
, "record %llu regresses commit index "
769 "from %"PRIu64
" to %"PRIu64
,
770 rec_idx
, raft
->commit_index
, r
->commit_index
);
771 } else if (r
->commit_index
>= raft
->log_end
) {
772 return ovsdb_error(NULL
, "record %llu advances commit index to "
773 "%"PRIu64
" but last log index is %"PRIu64
,
774 rec_idx
, r
->commit_index
, raft
->log_end
- 1);
776 raft
->commit_index
= r
->commit_index
;
781 case RAFT_REC_LEADER
:
782 /* XXX we could use this to take back leadership for quick restart */
790 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
791 raft_read_header(struct raft
*raft
)
793 /* Read header record. */
795 struct ovsdb_error
*error
= ovsdb_log_read(raft
->log
, &json
);
796 if (error
|| !json
) {
797 /* Report error or end-of-file. */
800 ovsdb_log_mark_base(raft
->log
);
802 struct raft_header h
;
803 error
= raft_header_from_json(&h
, json
);
811 raft
->name
= xstrdup(h
.name
);
812 raft
->local_address
= xstrdup(h
.local_address
);
813 raft
->local_nickname
= raft_address_to_nickname(h
.local_address
, &h
.sid
);
814 raft
->joining
= h
.joining
;
817 sset_clone(&raft
->remote_addresses
, &h
.remote_addresses
);
819 raft_entry_clone(&raft
->snap
, &h
.snap
);
820 raft
->log_start
= raft
->log_end
= h
.snap_index
+ 1;
821 raft
->commit_index
= h
.snap_index
;
822 raft
->last_applied
= h
.snap_index
- 1;
825 raft_header_uninit(&h
);
830 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
831 raft_read_log(struct raft
*raft
)
833 for (unsigned long long int i
= 1; ; i
++) {
835 struct ovsdb_error
*error
= ovsdb_log_read(raft
->log
, &json
);
838 /* We assume that the error is due to a partial write while
839 * appending to the file before a crash, so log it and
841 char *error_string
= ovsdb_error_to_string_free(error
);
842 VLOG_WARN("%s", error_string
);
849 struct raft_record r
;
850 error
= raft_record_from_json(&r
, json
);
852 error
= raft_apply_record(raft
, i
, &r
);
853 raft_record_uninit(&r
);
856 return ovsdb_wrap_error(error
, "error reading record %llu from "
857 "%s log", i
, raft
->name
);
861 /* Set the most recent servers. */
862 raft_get_servers_from_log(raft
, VLL_DBG
);
868 raft_reset_timer(struct raft
*raft
)
870 unsigned int duration
= (ELECTION_BASE_MSEC
871 + random_range(ELECTION_RANGE_MSEC
));
872 raft
->election_base
= time_msec();
873 raft
->election_timeout
= raft
->election_base
+ duration
;
877 raft_add_conn(struct raft
*raft
, struct jsonrpc_session
*js
,
878 const struct uuid
*sid
, bool incoming
)
880 struct raft_conn
*conn
= xzalloc(sizeof *conn
);
881 ovs_list_push_back(&raft
->conns
, &conn
->list_node
);
886 conn
->nickname
= raft_address_to_nickname(jsonrpc_session_get_name(js
),
888 conn
->incoming
= incoming
;
889 conn
->js_seqno
= jsonrpc_session_get_seqno(conn
->js
);
892 /* Starts the local server in an existing Raft cluster, using the local copy of
893 * the cluster's log in 'file_name'. Takes ownership of 'log', whether
894 * successful or not. */
895 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
896 raft_open(struct ovsdb_log
*log
, struct raft
**raftp
)
898 struct raft
*raft
= raft_alloc();
901 struct ovsdb_error
*error
= raft_read_header(raft
);
906 if (!raft
->joining
) {
907 error
= raft_read_log(raft
);
912 /* Find our own server. */
913 if (!raft_find_server(raft
, &raft
->sid
)) {
914 error
= ovsdb_error(NULL
, "server does not belong to cluster");
918 /* If there's only one server, start an election right away so that the
919 * cluster bootstraps quickly. */
920 if (hmap_count(&raft
->servers
) == 1) {
921 raft_start_election(raft
, false);
924 raft
->join_timeout
= time_msec() + 1000;
928 hmap_insert(&all_rafts
, &raft
->hmap_node
, hash_string(raft
->name
, 0));
937 /* Returns the name of 'raft', which in OVSDB is the database schema name. */
939 raft_get_name(const struct raft
*raft
)
944 /* Returns the cluster ID of 'raft'. If 'raft' has not yet completed joining
945 * its cluster, then 'cid' will be all-zeros (unless the administrator
946 * specified a cluster ID running "ovsdb-tool join-cluster").
948 * Each cluster has a unique cluster ID. */
950 raft_get_cid(const struct raft
*raft
)
955 /* Returns the server ID of 'raft'. Each server has a unique server ID. */
957 raft_get_sid(const struct raft
*raft
)
962 /* Returns true if 'raft' has completed joining its cluster, has not left or
963 * initiated leaving the cluster, does not have failed disk storage, and is
964 * apparently connected to the leader in a healthy way (or is itself the
967 raft_is_connected(const struct raft
*raft
)
969 return (raft
->role
!= RAFT_CANDIDATE
976 /* Returns true if 'raft' is the cluster leader. */
978 raft_is_leader(const struct raft
*raft
)
980 return raft
->role
== RAFT_LEADER
;
983 /* Returns true if 'raft' is the process of joining its cluster. */
985 raft_is_joining(const struct raft
*raft
)
987 return raft
->joining
;
990 /* Only returns *connected* connections. */
991 static struct raft_conn
*
992 raft_find_conn_by_sid(struct raft
*raft
, const struct uuid
*sid
)
994 if (!uuid_is_zero(sid
)) {
995 struct raft_conn
*conn
;
996 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
997 if (uuid_equals(sid
, &conn
->sid
)
998 && jsonrpc_session_is_connected(conn
->js
)) {
1006 static struct raft_conn
*
1007 raft_find_conn_by_address(struct raft
*raft
, const char *address
)
1009 struct raft_conn
*conn
;
1010 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1011 if (!strcmp(jsonrpc_session_get_name(conn
->js
), address
)) {
1018 static void OVS_PRINTF_FORMAT(3, 4)
1019 raft_record_note(struct raft
*raft
, const char *note
,
1020 const char *comment_format
, ...)
1023 va_start(args
, comment_format
);
1024 char *comment
= xvasprintf(comment_format
, args
);
1027 struct raft_record r
= {
1028 .type
= RAFT_REC_NOTE
,
1030 .note
= CONST_CAST(char *, note
),
1032 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
1037 /* If we're leader, try to transfer leadership to another server, logging
1038 * 'reason' as the human-readable reason (it should be a phrase suitable for
1039 * following "because") . */
1041 raft_transfer_leadership(struct raft
*raft
, const char *reason
)
1043 if (raft
->role
!= RAFT_LEADER
) {
1047 struct raft_server
*s
;
1048 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
1049 if (!uuid_equals(&raft
->sid
, &s
->sid
)
1050 && s
->phase
== RAFT_PHASE_STABLE
) {
1051 struct raft_conn
*conn
= raft_find_conn_by_sid(raft
, &s
->sid
);
1056 union raft_rpc rpc
= {
1059 .comment
= CONST_CAST(char *, reason
),
1060 .type
= RAFT_RPC_BECOME_LEADER
,
1066 raft_send_to_conn(raft
, &rpc
, conn
);
1068 raft_record_note(raft
, "transfer leadership",
1069 "transferring leadership to %s because %s",
1070 s
->nickname
, reason
);
1076 /* Send a RemoveServerRequest to the rest of the servers in the cluster.
1078 * If we know which server is the leader, we can just send the request to it.
1079 * However, we might not know which server is the leader, and we might never
1080 * find out if the remove request was actually previously committed by a
1081 * majority of the servers (because in that case the new leader will not send
1082 * AppendRequests or heartbeats to us). Therefore, we instead send
1083 * RemoveRequests to every server. This theoretically has the same problem, if
1084 * the current cluster leader was not previously a member of the cluster, but
1085 * it seems likely to be more robust in practice. */
1087 raft_send_remove_server_requests(struct raft
*raft
)
1089 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
1090 VLOG_INFO_RL(&rl
, "sending remove request (joining=%s, leaving=%s)",
1091 raft
->joining
? "true" : "false",
1092 raft
->leaving
? "true" : "false");
1093 const struct raft_server
*s
;
1094 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
1095 if (!uuid_equals(&s
->sid
, &raft
->sid
)) {
1096 union raft_rpc rpc
= (union raft_rpc
) {
1097 .remove_server_request
= {
1099 .type
= RAFT_RPC_REMOVE_SERVER_REQUEST
,
1105 raft_send(raft
, &rpc
);
1109 raft
->leave_timeout
= time_msec() + ELECTION_BASE_MSEC
;
1112 /* Attempts to start 'raft' leaving its cluster. The caller can check progress
1113 * using raft_is_leaving() and raft_left(). */
1115 raft_leave(struct raft
*raft
)
1117 if (raft
->joining
|| raft
->failed
|| raft
->leaving
|| raft
->left
) {
1120 VLOG_INFO(SID_FMT
": starting to leave cluster "CID_FMT
,
1121 SID_ARGS(&raft
->sid
), CID_ARGS(&raft
->cid
));
1122 raft
->leaving
= true;
1123 raft_transfer_leadership(raft
, "this server is leaving the cluster");
1124 raft_become_follower(raft
);
1125 raft_send_remove_server_requests(raft
);
1126 raft
->leave_timeout
= time_msec() + ELECTION_BASE_MSEC
;
1129 /* Returns true if 'raft' is currently attempting to leave its cluster. */
1131 raft_is_leaving(const struct raft
*raft
)
1133 return raft
->leaving
;
1136 /* Returns true if 'raft' successfully left its cluster. */
1138 raft_left(const struct raft
*raft
)
1143 /* Returns true if 'raft' has experienced a disk I/O failure. When this
1144 * returns true, only closing and reopening 'raft' allows for recovery. */
1146 raft_failed(const struct raft
*raft
)
1148 return raft
->failed
;
1151 /* Forces 'raft' to attempt to take leadership of the cluster by deposing the
1152 * current cluster. */
1154 raft_take_leadership(struct raft
*raft
)
1156 if (raft
->role
!= RAFT_LEADER
) {
1157 raft_start_election(raft
, true);
1161 /* Closes everything owned by 'raft' that might be visible outside the process:
1162 * network connections, commands, etc. This is part of closing 'raft'; it is
1163 * also used if 'raft' has failed in an unrecoverable way. */
1165 raft_close__(struct raft
*raft
)
1167 if (!hmap_node_is_null(&raft
->hmap_node
)) {
1168 hmap_remove(&all_rafts
, &raft
->hmap_node
);
1169 hmap_node_nullify(&raft
->hmap_node
);
1172 raft_complete_all_commands(raft
, RAFT_CMD_SHUTDOWN
);
1174 struct raft_server
*rs
= raft
->remove_server
;
1176 raft_send_remove_server_reply__(raft
, &rs
->sid
, &rs
->requester_sid
,
1177 rs
->requester_conn
, false,
1178 RAFT_SERVER_SHUTDOWN
);
1179 raft_server_destroy(raft
->remove_server
);
1180 raft
->remove_server
= NULL
;
1183 struct raft_conn
*conn
, *next
;
1184 LIST_FOR_EACH_SAFE (conn
, next
, list_node
, &raft
->conns
) {
1185 raft_conn_close(conn
);
1189 /* Closes and frees 'raft'.
1191 * A server's cluster membership is independent of whether the server is
1192 * actually running. When a server that is a member of a cluster closes, the
1193 * cluster treats this as a server failure. */
1195 raft_close(struct raft
*raft
)
1201 raft_transfer_leadership(raft
, "this server is shutting down");
1205 ovsdb_log_close(raft
->log
);
1207 raft_servers_destroy(&raft
->servers
);
1209 for (uint64_t index
= raft
->log_start
; index
< raft
->log_end
; index
++) {
1210 struct raft_entry
*e
= &raft
->entries
[index
- raft
->log_start
];
1211 raft_entry_uninit(e
);
1213 free(raft
->entries
);
1215 raft_entry_uninit(&raft
->snap
);
1217 raft_waiters_destroy(raft
);
1219 raft_servers_destroy(&raft
->add_servers
);
1221 hmap_destroy(&raft
->commands
);
1223 pstream_close(raft
->listener
);
1225 sset_destroy(&raft
->remote_addresses
);
1226 free(raft
->local_address
);
1227 free(raft
->local_nickname
);
1234 raft_conn_receive(struct raft
*raft
, struct raft_conn
*conn
,
1235 union raft_rpc
*rpc
)
1237 struct jsonrpc_msg
*msg
= jsonrpc_session_recv(conn
->js
);
1242 struct ovsdb_error
*error
= raft_rpc_from_jsonrpc(&raft
->cid
, &raft
->sid
,
1244 jsonrpc_msg_destroy(msg
);
1246 char *s
= ovsdb_error_to_string_free(error
);
1247 VLOG_INFO("%s: %s", jsonrpc_session_get_name(conn
->js
), s
);
1252 if (uuid_is_zero(&conn
->sid
)) {
1253 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(50, 50);
1254 conn
->sid
= rpc
->common
.sid
;
1255 VLOG_INFO_RL(&rl
, "%s: learned server ID "SID_FMT
,
1256 jsonrpc_session_get_name(conn
->js
), SID_ARGS(&conn
->sid
));
1257 } else if (!uuid_equals(&conn
->sid
, &rpc
->common
.sid
)) {
1258 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1259 VLOG_WARN_RL(&rl
, "%s: ignoring message with unexpected server ID "
1260 SID_FMT
" (expected "SID_FMT
")",
1261 jsonrpc_session_get_name(conn
->js
),
1262 SID_ARGS(&rpc
->common
.sid
), SID_ARGS(&conn
->sid
));
1263 raft_rpc_uninit(rpc
);
1267 const char *address
= (rpc
->type
== RAFT_RPC_HELLO_REQUEST
1268 ? rpc
->hello_request
.address
1269 : rpc
->type
== RAFT_RPC_ADD_SERVER_REQUEST
1270 ? rpc
->add_server_request
.address
1273 char *new_nickname
= raft_address_to_nickname(address
, &conn
->sid
);
1274 if (strcmp(conn
->nickname
, new_nickname
)) {
1275 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(50, 50);
1276 VLOG_INFO_RL(&rl
, "%s: learned remote address %s",
1277 jsonrpc_session_get_name(conn
->js
), address
);
1279 free(conn
->nickname
);
1280 conn
->nickname
= new_nickname
;
1290 raft_get_nickname(const struct raft
*raft
, const struct uuid
*sid
,
1291 char buf
[SID_LEN
+ 1], size_t bufsize
)
1293 if (uuid_equals(sid
, &raft
->sid
)) {
1294 return raft
->local_nickname
;
1297 const char *s
= raft_servers_get_nickname__(&raft
->servers
, sid
);
1302 return raft_servers_get_nickname(&raft
->add_servers
, sid
, buf
, bufsize
);
1306 log_rpc(const union raft_rpc
*rpc
, const char *direction
,
1307 const struct raft_conn
*conn
, int line_number
)
1309 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(600, 600);
1310 if (!raft_rpc_is_heartbeat(rpc
) && !VLOG_DROP_DBG(&rl
)) {
1311 struct ds s
= DS_EMPTY_INITIALIZER
;
1313 ds_put_format(&s
, "raft.c:%d ", line_number
);
1315 ds_put_format(&s
, "%s%s ", direction
, conn
->nickname
);
1316 raft_rpc_format(rpc
, &s
);
1317 VLOG_DBG("%s", ds_cstr(&s
));
1323 raft_send_add_server_request(struct raft
*raft
, struct raft_conn
*conn
)
1325 union raft_rpc rq
= {
1326 .add_server_request
= {
1328 .type
= RAFT_RPC_ADD_SERVER_REQUEST
,
1332 .address
= raft
->local_address
,
1335 raft_send_to_conn(raft
, &rq
, conn
);
1339 raft_conn_run(struct raft
*raft
, struct raft_conn
*conn
)
1341 jsonrpc_session_run(conn
->js
);
1343 unsigned int new_seqno
= jsonrpc_session_get_seqno(conn
->js
);
1344 bool just_connected
= (new_seqno
!= conn
->js_seqno
1345 && jsonrpc_session_is_connected(conn
->js
));
1346 conn
->js_seqno
= new_seqno
;
1347 if (just_connected
) {
1348 if (raft
->joining
) {
1349 raft_send_add_server_request(raft
, conn
);
1350 } else if (raft
->leaving
) {
1351 union raft_rpc rq
= {
1352 .remove_server_request
= {
1354 .type
= RAFT_RPC_REMOVE_SERVER_REQUEST
,
1360 raft_send_to_conn(raft
, &rq
, conn
);
1362 union raft_rpc rq
= (union raft_rpc
) {
1365 .type
= RAFT_RPC_HELLO_REQUEST
,
1368 .address
= raft
->local_address
,
1371 raft_send_to_conn(raft
, &rq
, conn
);
1375 for (size_t i
= 0; i
< 50; i
++) {
1377 if (!raft_conn_receive(raft
, conn
, &rpc
)) {
1381 log_rpc(&rpc
, "<--", conn
, 0);
1382 raft_handle_rpc(raft
, &rpc
);
1383 raft_rpc_uninit(&rpc
);
1388 raft_waiter_complete_rpc(struct raft
*raft
, const union raft_rpc
*rpc
)
1390 uint64_t term
= raft_rpc_get_term(rpc
);
1391 if (term
&& term
< raft
->term
) {
1392 /* Drop the message because it's for an expired term. */
1396 if (!raft_is_rpc_synced(raft
, rpc
)) {
1397 /* This is a bug. A reply message is deferred because some state in
1398 * the message, such as a term or index, has not been committed to
1399 * disk, and they should only be completed when that commit is done.
1400 * But this message is being completed before the commit is finished.
1401 * Complain, and hope that someone reports the bug. */
1402 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
1403 if (VLOG_DROP_ERR(&rl
)) {
1407 struct ds s
= DS_EMPTY_INITIALIZER
;
1409 if (term
> raft
->synced_term
) {
1410 ds_put_format(&s
, " because message term %"PRIu64
" is "
1411 "past synced term %"PRIu64
,
1412 term
, raft
->synced_term
);
1415 uint64_t index
= raft_rpc_get_min_sync_index(rpc
);
1416 if (index
> raft
->log_synced
) {
1417 ds_put_format(&s
, " %s message index %"PRIu64
" is past last "
1418 "synced index %"PRIu64
,
1419 s
.length
? "and" : "because",
1420 index
, raft
->log_synced
);
1423 const struct uuid
*vote
= raft_rpc_get_vote(rpc
);
1424 if (vote
&& !uuid_equals(vote
, &raft
->synced_vote
)) {
1425 char buf1
[SID_LEN
+ 1];
1426 char buf2
[SID_LEN
+ 1];
1427 ds_put_format(&s
, " %s vote %s differs from synced vote %s",
1428 s
.length
? "and" : "because",
1429 raft_get_nickname(raft
, vote
, buf1
, sizeof buf1
),
1430 raft_get_nickname(raft
, &raft
->synced_vote
,
1431 buf2
, sizeof buf2
));
1434 char buf
[SID_LEN
+ 1];
1435 ds_put_format(&s
, ": %s ",
1436 raft_get_nickname(raft
, &rpc
->common
.sid
,
1438 raft_rpc_format(rpc
, &s
);
1439 VLOG_ERR("internal error: deferred %s message completed "
1440 "but not ready to send%s",
1441 raft_rpc_type_to_string(rpc
->type
), ds_cstr(&s
));
1447 struct raft_conn
*dst
= raft_find_conn_by_sid(raft
, &rpc
->common
.sid
);
1449 raft_send_to_conn(raft
, rpc
, dst
);
1454 raft_waiter_complete(struct raft
*raft
, struct raft_waiter
*w
)
1456 VLOG_INFO("%s:%d", __FILE__
, __LINE__
);
1459 if (raft
->role
== RAFT_LEADER
) {
1460 raft_update_our_match_index(raft
, w
->entry
.index
);
1462 raft
->log_synced
= w
->entry
.index
;
1466 raft
->synced_term
= w
->term
.term
;
1467 raft
->synced_vote
= w
->term
.vote
;
1471 raft_waiter_complete_rpc(raft
, w
->rpc
);
1477 raft_waiter_destroy(struct raft_waiter
*w
)
1483 ovs_list_remove(&w
->list_node
);
1491 raft_rpc_uninit(w
->rpc
);
1499 raft_waiters_run(struct raft
*raft
)
1501 if (ovs_list_is_empty(&raft
->waiters
)) {
1505 uint64_t cur
= ovsdb_log_commit_progress(raft
->log
);
1506 struct raft_waiter
*w
, *next
;
1507 LIST_FOR_EACH_SAFE (w
, next
, list_node
, &raft
->waiters
) {
1508 if (cur
< w
->commit_ticket
) {
1511 raft_waiter_complete(raft
, w
);
1512 raft_waiter_destroy(w
);
1517 raft_waiters_wait(struct raft
*raft
)
1519 struct raft_waiter
*w
;
1520 LIST_FOR_EACH (w
, list_node
, &raft
->waiters
) {
1521 ovsdb_log_commit_wait(raft
->log
, w
->commit_ticket
);
1527 raft_waiters_destroy(struct raft
*raft
)
1529 struct raft_waiter
*w
, *next
;
1530 LIST_FOR_EACH_SAFE (w
, next
, list_node
, &raft
->waiters
) {
1531 raft_waiter_destroy(w
);
1535 static bool OVS_WARN_UNUSED_RESULT
1536 raft_set_term(struct raft
*raft
, uint64_t term
, const struct uuid
*vote
)
1538 struct ovsdb_error
*error
= raft_write_state(raft
->log
, term
, vote
);
1539 if (!raft_handle_write_error(raft
, error
)) {
1543 struct raft_waiter
*w
= raft_waiter_create(raft
, RAFT_W_TERM
, true);
1544 raft
->term
= w
->term
.term
= term
;
1545 raft
->vote
= w
->term
.vote
= vote
? *vote
: UUID_ZERO
;
1550 raft_accept_vote(struct raft
*raft
, struct raft_server
*s
,
1551 const struct uuid
*vote
)
1553 if (uuid_equals(&s
->vote
, vote
)) {
1556 if (!uuid_is_zero(&s
->vote
)) {
1557 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1558 char buf1
[SID_LEN
+ 1];
1559 char buf2
[SID_LEN
+ 1];
1560 VLOG_WARN_RL(&rl
, "server %s changed its vote from %s to %s",
1562 raft_get_nickname(raft
, &s
->vote
, buf1
, sizeof buf1
),
1563 raft_get_nickname(raft
, vote
, buf2
, sizeof buf2
));
1566 if (uuid_equals(vote
, &raft
->sid
)
1567 && ++raft
->n_votes
> hmap_count(&raft
->servers
) / 2) {
1568 raft_become_leader(raft
);
1573 raft_start_election(struct raft
*raft
, bool leadership_transfer
)
1575 if (raft
->leaving
) {
1579 struct raft_server
*me
= raft_find_server(raft
, &raft
->sid
);
1584 if (!raft_set_term(raft
, raft
->term
+ 1, &raft
->sid
)) {
1588 raft_complete_all_commands(raft
, RAFT_CMD_LOST_LEADERSHIP
);
1590 ovs_assert(raft
->role
!= RAFT_LEADER
);
1591 ovs_assert(hmap_is_empty(&raft
->commands
));
1592 raft
->role
= RAFT_CANDIDATE
;
1596 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1597 if (!VLOG_DROP_INFO(&rl
)) {
1598 long long int now
= time_msec();
1599 if (now
>= raft
->election_timeout
) {
1600 VLOG_INFO("term %"PRIu64
": %lld ms timeout expired, "
1601 "starting election",
1602 raft
->term
, now
- raft
->election_base
);
1604 VLOG_INFO("term %"PRIu64
": starting election", raft
->term
);
1607 raft_reset_timer(raft
);
1609 struct raft_server
*peer
;
1610 HMAP_FOR_EACH (peer
, hmap_node
, &raft
->servers
) {
1611 peer
->vote
= UUID_ZERO
;
1612 if (uuid_equals(&raft
->sid
, &peer
->sid
)) {
1616 union raft_rpc rq
= {
1619 .type
= RAFT_RPC_VOTE_REQUEST
,
1623 .last_log_index
= raft
->log_end
- 1,
1625 raft
->log_end
> raft
->log_start
1626 ? raft
->entries
[raft
->log_end
- raft
->log_start
- 1].term
1628 .leadership_transfer
= leadership_transfer
,
1631 raft_send(raft
, &rq
);
1634 /* Vote for ourselves. */
1635 raft_accept_vote(raft
, me
, &raft
->sid
);
1639 raft_open_conn(struct raft
*raft
, const char *address
, const struct uuid
*sid
)
1641 if (strcmp(address
, raft
->local_address
)
1642 && !raft_find_conn_by_address(raft
, address
)) {
1643 raft_add_conn(raft
, jsonrpc_session_open(address
, true), sid
, false);
1648 raft_conn_close(struct raft_conn
*conn
)
1650 jsonrpc_session_close(conn
->js
);
1651 ovs_list_remove(&conn
->list_node
);
1652 free(conn
->nickname
);
1656 /* Returns true if 'conn' should stay open, false if it should be closed. */
1658 raft_conn_should_stay_open(struct raft
*raft
, struct raft_conn
*conn
)
1660 /* Close the connection if it's actually dead. If necessary, we'll
1661 * initiate a new session later. */
1662 if (!jsonrpc_session_is_alive(conn
->js
)) {
1666 /* Keep incoming sessions. We trust the originator to decide to drop
1668 if (conn
->incoming
) {
1672 /* If we are joining the cluster, keep sessions to the remote addresses
1673 * that are supposed to be part of the cluster we're joining. */
1674 if (raft
->joining
&& sset_contains(&raft
->remote_addresses
,
1675 jsonrpc_session_get_name(conn
->js
))) {
1679 /* We have joined the cluster. If we did that "recently", then there is a
1680 * chance that we do not have the most recent server configuration log
1681 * entry. If so, it's a waste to disconnect from the servers that were in
1682 * remote_addresses and that will probably appear in the configuration,
1683 * just to reconnect to them a moment later when we do get the
1684 * configuration update. If we are not ourselves in the configuration,
1685 * then we know that there must be a new configuration coming up, so in
1686 * that case keep the connection. */
1687 if (!raft_find_server(raft
, &raft
->sid
)) {
1691 /* Keep the connection only if the server is part of the configuration. */
1692 return raft_find_server(raft
, &conn
->sid
);
1695 /* Allows 'raft' to maintain the distributed log. Call this function as part
1696 * of the process's main loop. */
1698 raft_run(struct raft
*raft
)
1700 if (raft
->left
|| raft
->failed
) {
1704 raft_waiters_run(raft
);
1706 if (!raft
->listener
&& time_msec() >= raft
->listen_backoff
) {
1707 char *paddr
= raft_make_address_passive(raft
->local_address
);
1708 int error
= pstream_open(paddr
, &raft
->listener
, DSCP_DEFAULT
);
1710 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1711 VLOG_WARN_RL(&rl
, "%s: listen failed (%s)",
1712 paddr
, ovs_strerror(error
));
1713 raft
->listen_backoff
= time_msec() + 1000;
1718 if (raft
->listener
) {
1719 struct stream
*stream
;
1720 int error
= pstream_accept(raft
->listener
, &stream
);
1722 raft_add_conn(raft
, jsonrpc_session_open_unreliably(
1723 jsonrpc_open(stream
), DSCP_DEFAULT
), NULL
,
1725 } else if (error
!= EAGAIN
) {
1726 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1727 VLOG_WARN_RL(&rl
, "%s: accept failed: %s",
1728 pstream_get_name(raft
->listener
),
1729 ovs_strerror(error
));
1733 /* Run RPCs for all open sessions. */
1734 struct raft_conn
*conn
;
1735 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1736 raft_conn_run(raft
, conn
);
1739 /* Close unneeded sessions. */
1740 struct raft_conn
*next
;
1741 LIST_FOR_EACH_SAFE (conn
, next
, list_node
, &raft
->conns
) {
1742 if (!raft_conn_should_stay_open(raft
, conn
)) {
1743 raft_conn_close(conn
);
1747 /* Open needed sessions. */
1748 struct raft_server
*server
;
1749 HMAP_FOR_EACH (server
, hmap_node
, &raft
->servers
) {
1750 raft_open_conn(raft
, server
->address
, &server
->sid
);
1752 if (raft
->joining
) {
1753 const char *address
;
1754 SSET_FOR_EACH (address
, &raft
->remote_addresses
) {
1755 raft_open_conn(raft
, address
, NULL
);
1759 if (!raft
->joining
&& time_msec() >= raft
->election_timeout
) {
1760 raft_start_election(raft
, false);
1763 if (raft
->leaving
&& time_msec() >= raft
->leave_timeout
) {
1764 raft_send_remove_server_requests(raft
);
1767 if (raft
->joining
&& time_msec() >= raft
->join_timeout
) {
1768 raft
->join_timeout
= time_msec() + 1000;
1769 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1770 raft_send_add_server_request(raft
, conn
);
1774 if (time_msec() >= raft
->ping_timeout
) {
1775 if (raft
->role
== RAFT_LEADER
) {
1776 raft_send_heartbeats(raft
);
1778 long long int now
= time_msec();
1779 struct raft_command
*cmd
, *next_cmd
;
1780 HMAP_FOR_EACH_SAFE (cmd
, next_cmd
, hmap_node
, &raft
->commands
) {
1782 && now
- cmd
->timestamp
> ELECTION_BASE_MSEC
) {
1783 raft_command_complete(raft
, cmd
, RAFT_CMD_TIMEOUT
);
1787 raft
->ping_timeout
= time_msec() + PING_TIME_MSEC
;
1790 /* Do this only at the end; if we did it as soon as we set raft->left or
1791 * raft->failed in handling the RemoveServerReply, then it could easily
1792 * cause references to freed memory in RPC sessions, etc. */
1793 if (raft
->left
|| raft
->failed
) {
1799 raft_wait_session(struct jsonrpc_session
*js
)
1802 jsonrpc_session_wait(js
);
1803 jsonrpc_session_recv_wait(js
);
1807 /* Causes the next call to poll_block() to wake up when 'raft' needs to do
1810 raft_wait(struct raft
*raft
)
1812 if (raft
->left
|| raft
->failed
) {
1816 raft_waiters_wait(raft
);
1818 if (raft
->listener
) {
1819 pstream_wait(raft
->listener
);
1821 poll_timer_wait_until(raft
->listen_backoff
);
1824 struct raft_conn
*conn
;
1825 LIST_FOR_EACH (conn
, list_node
, &raft
->conns
) {
1826 raft_wait_session(conn
->js
);
1829 if (!raft
->joining
) {
1830 poll_timer_wait_until(raft
->election_timeout
);
1832 poll_timer_wait_until(raft
->join_timeout
);
1834 if (raft
->leaving
) {
1835 poll_timer_wait_until(raft
->leave_timeout
);
1837 if (raft
->role
== RAFT_LEADER
|| !hmap_is_empty(&raft
->commands
)) {
1838 poll_timer_wait_until(raft
->ping_timeout
);
1842 static struct raft_waiter
*
1843 raft_waiter_create(struct raft
*raft
, enum raft_waiter_type type
,
1846 struct raft_waiter
*w
= xzalloc(sizeof *w
);
1847 ovs_list_push_back(&raft
->waiters
, &w
->list_node
);
1848 w
->commit_ticket
= start_commit
? ovsdb_log_commit_start(raft
->log
) : 0;
1853 /* Returns a human-readable representation of 'status' (or NULL if 'status' is
1856 raft_command_status_to_string(enum raft_command_status status
)
1859 case RAFT_CMD_INCOMPLETE
:
1860 return "operation still in progress";
1861 case RAFT_CMD_SUCCESS
:
1863 case RAFT_CMD_NOT_LEADER
:
1864 return "not leader";
1865 case RAFT_CMD_BAD_PREREQ
:
1866 return "prerequisite check failed";
1867 case RAFT_CMD_LOST_LEADERSHIP
:
1868 return "lost leadership";
1869 case RAFT_CMD_SHUTDOWN
:
1870 return "server shutdown";
1871 case RAFT_CMD_IO_ERROR
:
1873 case RAFT_CMD_TIMEOUT
:
1880 /* Converts human-readable status in 's' into status code in '*statusp'.
1881 * Returns true if successful, false if 's' is unknown. */
1883 raft_command_status_from_string(const char *s
,
1884 enum raft_command_status
*statusp
)
1886 for (enum raft_command_status status
= 0; ; status
++) {
1887 const char *s2
= raft_command_status_to_string(status
);
1891 } else if (!strcmp(s
, s2
)) {
1898 static const struct uuid
*
1899 raft_get_eid(const struct raft
*raft
, uint64_t index
)
1901 for (; index
>= raft
->log_start
; index
--) {
1902 const struct raft_entry
*e
= raft_get_entry(raft
, index
);
1907 return &raft
->snap
.eid
;
1910 static const struct uuid
*
1911 raft_current_eid(const struct raft
*raft
)
1913 return raft_get_eid(raft
, raft
->log_end
- 1);
1916 static struct raft_command
*
1917 raft_command_create_completed(enum raft_command_status status
)
1919 ovs_assert(status
!= RAFT_CMD_INCOMPLETE
);
1921 struct raft_command
*cmd
= xzalloc(sizeof *cmd
);
1923 cmd
->status
= status
;
1927 static struct raft_command
*
1928 raft_command_create_incomplete(struct raft
*raft
, uint64_t index
)
1930 struct raft_command
*cmd
= xzalloc(sizeof *cmd
);
1931 cmd
->n_refs
= 2; /* One for client, one for raft->commands. */
1933 cmd
->status
= RAFT_CMD_INCOMPLETE
;
1934 hmap_insert(&raft
->commands
, &cmd
->hmap_node
, cmd
->index
);
1938 static struct raft_command
* OVS_WARN_UNUSED_RESULT
1939 raft_command_initiate(struct raft
*raft
,
1940 const struct json
*data
, const struct json
*servers
,
1941 const struct uuid
*eid
)
1943 /* Write to local log. */
1944 uint64_t index
= raft
->log_end
;
1945 if (!raft_handle_write_error(
1946 raft
, raft_write_entry(
1947 raft
, raft
->term
, json_nullable_clone(data
), eid
,
1948 json_nullable_clone(servers
)))) {
1949 return raft_command_create_completed(RAFT_CMD_IO_ERROR
);
1952 struct raft_command
*cmd
= raft_command_create_incomplete(raft
, index
);
1957 raft_waiter_create(raft
, RAFT_W_ENTRY
, true)->entry
.index
= cmd
->index
;
1959 /* Write to remote logs. */
1960 struct raft_server
*s
;
1961 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
1962 if (!uuid_equals(&s
->sid
, &raft
->sid
) && s
->next_index
== index
) {
1963 raft_send_append_request(raft
, s
, 1, "execute command");
1971 static struct raft_command
* OVS_WARN_UNUSED_RESULT
1972 raft_command_execute__(struct raft
*raft
,
1973 const struct json
*data
, const struct json
*servers
,
1974 const struct uuid
*prereq
, struct uuid
*result
)
1976 if (raft
->joining
|| raft
->leaving
|| raft
->left
|| raft
->failed
) {
1977 return raft_command_create_completed(RAFT_CMD_SHUTDOWN
);
1980 if (raft
->role
!= RAFT_LEADER
) {
1981 /* Consider proxying the command to the leader. We can only do that if
1982 * we know the leader and the command does not change the set of
1983 * servers. We do not proxy commands without prerequisites, even
1984 * though we could, because in an OVSDB context a log entry doesn't
1985 * make sense without context. */
1986 if (servers
|| !data
1987 || raft
->role
!= RAFT_FOLLOWER
|| uuid_is_zero(&raft
->leader_sid
)
1989 return raft_command_create_completed(RAFT_CMD_NOT_LEADER
);
1993 struct uuid eid
= data
? uuid_random() : UUID_ZERO
;
1998 if (raft
->role
!= RAFT_LEADER
) {
1999 const union raft_rpc rpc
= {
2000 .execute_command_request
= {
2002 .type
= RAFT_RPC_EXECUTE_COMMAND_REQUEST
,
2003 .sid
= raft
->leader_sid
,
2005 .data
= CONST_CAST(struct json
*, data
),
2010 if (!raft_send(raft
, &rpc
)) {
2011 /* Couldn't send command, so it definitely failed. */
2012 return raft_command_create_completed(RAFT_CMD_NOT_LEADER
);
2015 struct raft_command
*cmd
= raft_command_create_incomplete(raft
, 0);
2016 cmd
->timestamp
= time_msec();
2021 const struct uuid
*current_eid
= raft_current_eid(raft
);
2022 if (prereq
&& !uuid_equals(prereq
, current_eid
)) {
2023 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
2024 VLOG_INFO_RL(&rl
, "current entry eid "UUID_FMT
" does not match "
2025 "prerequisite "UUID_FMT
,
2026 UUID_ARGS(current_eid
), UUID_ARGS(prereq
));
2027 return raft_command_create_completed(RAFT_CMD_BAD_PREREQ
);
2030 return raft_command_initiate(raft
, data
, servers
, &eid
);
2033 /* Initiates appending a log entry to 'raft'. The log entry consists of 'data'
2034 * and, if 'prereq' is nonnull, it is only added to the log if the previous
2035 * entry in the log has entry ID 'prereq'. If 'result' is nonnull, it is
2036 * populated with the entry ID for the new log entry.
2038 * Returns a "struct raft_command" that may be used to track progress adding
2039 * the log entry. The caller must eventually free the returned structure, with
2040 * raft_command_unref(). */
2041 struct raft_command
* OVS_WARN_UNUSED_RESULT
2042 raft_command_execute(struct raft
*raft
, const struct json
*data
,
2043 const struct uuid
*prereq
, struct uuid
*result
)
2045 return raft_command_execute__(raft
, data
, NULL
, prereq
, result
);
2048 /* Returns the status of 'cmd'. */
2049 enum raft_command_status
2050 raft_command_get_status(const struct raft_command
*cmd
)
2052 ovs_assert(cmd
->n_refs
> 0);
2056 /* Returns the index of the log entry at which 'cmd' was committed.
2058 * This function works only with successful commands. */
2060 raft_command_get_commit_index(const struct raft_command
*cmd
)
2062 ovs_assert(cmd
->n_refs
> 0);
2063 ovs_assert(cmd
->status
== RAFT_CMD_SUCCESS
);
2069 raft_command_unref(struct raft_command
*cmd
)
2072 ovs_assert(cmd
->n_refs
> 0);
2073 if (!--cmd
->n_refs
) {
2079 /* Causes poll_block() to wake up when 'cmd' has status to report. */
2081 raft_command_wait(const struct raft_command
*cmd
)
2083 if (cmd
->status
!= RAFT_CMD_INCOMPLETE
) {
2084 poll_immediate_wake();
2089 raft_command_complete(struct raft
*raft
,
2090 struct raft_command
*cmd
,
2091 enum raft_command_status status
)
2093 if (!uuid_is_zero(&cmd
->sid
)) {
2094 uint64_t commit_index
= status
== RAFT_CMD_SUCCESS
? cmd
->index
: 0;
2095 raft_send_execute_command_reply(raft
, &cmd
->sid
, &cmd
->eid
, status
,
2099 ovs_assert(cmd
->status
== RAFT_CMD_INCOMPLETE
);
2100 ovs_assert(cmd
->n_refs
> 0);
2101 hmap_remove(&raft
->commands
, &cmd
->hmap_node
);
2102 cmd
->status
= status
;
2103 raft_command_unref(cmd
);
2107 raft_complete_all_commands(struct raft
*raft
, enum raft_command_status status
)
2109 struct raft_command
*cmd
, *next
;
2110 HMAP_FOR_EACH_SAFE (cmd
, next
, hmap_node
, &raft
->commands
) {
2111 raft_command_complete(raft
, cmd
, status
);
2115 static struct raft_command
*
2116 raft_find_command_by_index(struct raft
*raft
, uint64_t index
)
2118 struct raft_command
*cmd
;
2120 HMAP_FOR_EACH_IN_BUCKET (cmd
, hmap_node
, index
, &raft
->commands
) {
2121 if (cmd
->index
== index
) {
2128 static struct raft_command
*
2129 raft_find_command_by_eid(struct raft
*raft
, const struct uuid
*eid
)
2131 struct raft_command
*cmd
;
2133 HMAP_FOR_EACH (cmd
, hmap_node
, &raft
->commands
) {
2134 if (uuid_equals(&cmd
->eid
, eid
)) {
2141 #define RAFT_RPC(ENUM, NAME) \
2142 static void raft_handle_##NAME(struct raft *, const struct raft_##NAME *);
2147 raft_handle_hello_request(struct raft
*raft OVS_UNUSED
,
2148 const struct raft_hello_request
*hello OVS_UNUSED
)
2152 /* 'sid' is the server being added. */
2154 raft_send_add_server_reply__(struct raft
*raft
, const struct uuid
*sid
,
2155 const char *address
,
2156 bool success
, const char *comment
)
2158 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2159 if (!VLOG_DROP_INFO(&rl
)) {
2160 struct ds s
= DS_EMPTY_INITIALIZER
;
2161 char buf
[SID_LEN
+ 1];
2162 ds_put_format(&s
, "adding %s ("SID_FMT
" at %s) "
2163 "to cluster "CID_FMT
" %s",
2164 raft_get_nickname(raft
, sid
, buf
, sizeof buf
),
2165 SID_ARGS(sid
), address
, CID_ARGS(&raft
->cid
),
2166 success
? "succeeded" : "failed");
2168 ds_put_format(&s
, " (%s)", comment
);
2170 VLOG_INFO("%s", ds_cstr(&s
));
2174 union raft_rpc rpy
= {
2175 .add_server_reply
= {
2177 .type
= RAFT_RPC_ADD_SERVER_REPLY
,
2179 .comment
= CONST_CAST(char *, comment
),
2185 struct sset
*remote_addresses
= &rpy
.add_server_reply
.remote_addresses
;
2186 sset_init(remote_addresses
);
2187 if (!raft
->joining
) {
2188 struct raft_server
*s
;
2189 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
2190 if (!uuid_equals(&s
->sid
, &raft
->sid
)) {
2191 sset_add(remote_addresses
, s
->address
);
2196 raft_send(raft
, &rpy
);
2198 sset_destroy(remote_addresses
);
2202 raft_send_remove_server_reply_rpc(struct raft
*raft
,
2203 const struct uuid
*dst_sid
,
2204 const struct uuid
*target_sid
,
2205 bool success
, const char *comment
)
2207 if (uuid_equals(&raft
->sid
, dst_sid
)) {
2208 if (success
&& uuid_equals(&raft
->sid
, target_sid
)) {
2209 raft_finished_leaving_cluster(raft
);
2214 const union raft_rpc rpy
= {
2215 .remove_server_reply
= {
2217 .type
= RAFT_RPC_REMOVE_SERVER_REPLY
,
2219 .comment
= CONST_CAST(char *, comment
),
2221 .target_sid
= (uuid_equals(dst_sid
, target_sid
)
2227 raft_send(raft
, &rpy
);
2231 raft_send_remove_server_reply__(struct raft
*raft
,
2232 const struct uuid
*target_sid
,
2233 const struct uuid
*requester_sid
,
2234 struct unixctl_conn
*requester_conn
,
2235 bool success
, const char *comment
)
2237 struct ds s
= DS_EMPTY_INITIALIZER
;
2238 ds_put_format(&s
, "request ");
2239 if (!uuid_is_zero(requester_sid
)) {
2240 char buf
[SID_LEN
+ 1];
2241 ds_put_format(&s
, "by %s",
2242 raft_get_nickname(raft
, requester_sid
, buf
, sizeof buf
));
2244 ds_put_cstr(&s
, "via unixctl");
2246 ds_put_cstr(&s
, " to remove ");
2247 if (!requester_conn
&& uuid_equals(target_sid
, requester_sid
)) {
2248 ds_put_cstr(&s
, "itself");
2250 char buf
[SID_LEN
+ 1];
2251 ds_put_cstr(&s
, raft_get_nickname(raft
, target_sid
, buf
, sizeof buf
));
2252 if (uuid_equals(target_sid
, &raft
->sid
)) {
2253 ds_put_cstr(&s
, " (ourselves)");
2256 ds_put_format(&s
, " from cluster "CID_FMT
" %s",
2257 CID_ARGS(&raft
->cid
),
2258 success
? "succeeded" : "failed");
2260 ds_put_format(&s
, " (%s)", comment
);
2263 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2264 VLOG_INFO_RL(&rl
, "%s", ds_cstr(&s
));
2266 /* Send RemoveServerReply to the requester (which could be a server or a
2267 * unixctl connection. Also always send it to the removed server; this
2268 * allows it to be sure that it's really removed and update its log and
2269 * disconnect permanently. */
2270 if (!uuid_is_zero(requester_sid
)) {
2271 raft_send_remove_server_reply_rpc(raft
, requester_sid
, target_sid
,
2274 if (!uuid_equals(requester_sid
, target_sid
)) {
2275 raft_send_remove_server_reply_rpc(raft
, target_sid
, target_sid
,
2278 if (requester_conn
) {
2280 unixctl_command_reply(requester_conn
, ds_cstr(&s
));
2282 unixctl_command_reply_error(requester_conn
, ds_cstr(&s
));
2290 raft_send_add_server_reply(struct raft
*raft
,
2291 const struct raft_add_server_request
*rq
,
2292 bool success
, const char *comment
)
2294 return raft_send_add_server_reply__(raft
, &rq
->common
.sid
, rq
->address
,
2299 raft_send_remove_server_reply(struct raft
*raft
,
2300 const struct raft_remove_server_request
*rq
,
2301 bool success
, const char *comment
)
2303 return raft_send_remove_server_reply__(raft
, &rq
->sid
, &rq
->common
.sid
,
2304 rq
->requester_conn
, success
,
2309 raft_become_follower(struct raft
*raft
)
2311 raft
->leader_sid
= UUID_ZERO
;
2312 if (raft
->role
== RAFT_FOLLOWER
) {
2316 raft
->role
= RAFT_FOLLOWER
;
2317 raft_reset_timer(raft
);
2319 /* Notify clients about lost leadership.
2321 * We do not reverse our changes to 'raft->servers' because the new
2322 * configuration is already part of the log. Possibly the configuration
2323 * log entry will not be committed, but until we know that we must use the
2324 * new configuration. Our AppendEntries processing will properly update
2325 * the server configuration later, if necessary. */
2326 struct raft_server
*s
;
2327 HMAP_FOR_EACH (s
, hmap_node
, &raft
->add_servers
) {
2328 raft_send_add_server_reply__(raft
, &s
->sid
, s
->address
, false,
2329 RAFT_SERVER_LOST_LEADERSHIP
);
2331 if (raft
->remove_server
) {
2332 raft_send_remove_server_reply__(raft
, &raft
->remove_server
->sid
,
2333 &raft
->remove_server
->requester_sid
,
2334 raft
->remove_server
->requester_conn
,
2335 false, RAFT_SERVER_LOST_LEADERSHIP
);
2336 raft_server_destroy(raft
->remove_server
);
2337 raft
->remove_server
= NULL
;
2340 raft_complete_all_commands(raft
, RAFT_CMD_LOST_LEADERSHIP
);
2344 raft_send_append_request(struct raft
*raft
,
2345 struct raft_server
*peer
, unsigned int n
,
2346 const char *comment
)
2348 ovs_assert(raft
->role
== RAFT_LEADER
);
2350 const union raft_rpc rq
= {
2353 .type
= RAFT_RPC_APPEND_REQUEST
,
2355 .comment
= CONST_CAST(char *, comment
),
2358 .prev_log_index
= peer
->next_index
- 1,
2359 .prev_log_term
= (peer
->next_index
- 1 >= raft
->log_start
2360 ? raft
->entries
[peer
->next_index
- 1
2361 - raft
->log_start
].term
2363 .leader_commit
= raft
->commit_index
,
2364 .entries
= &raft
->entries
[peer
->next_index
- raft
->log_start
],
2368 raft_send(raft
, &rq
);
2372 raft_send_heartbeats(struct raft
*raft
)
2374 struct raft_server
*s
;
2375 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
2376 if (!uuid_equals(&raft
->sid
, &s
->sid
)) {
2377 raft_send_append_request(raft
, s
, 0, "heartbeat");
2381 /* Send anyone waiting for a command to complete a ping to let them
2382 * know we're still working on it. */
2383 struct raft_command
*cmd
;
2384 HMAP_FOR_EACH (cmd
, hmap_node
, &raft
->commands
) {
2385 if (!uuid_is_zero(&cmd
->sid
)) {
2386 raft_send_execute_command_reply(raft
, &cmd
->sid
,
2388 RAFT_CMD_INCOMPLETE
, 0);
2393 /* Initializes the fields in 's' that represent the leader's view of the
2396 raft_server_init_leader(struct raft
*raft
, struct raft_server
*s
)
2398 s
->next_index
= raft
->log_end
;
2400 s
->phase
= RAFT_PHASE_STABLE
;
2404 raft_become_leader(struct raft
*raft
)
2406 raft_complete_all_commands(raft
, RAFT_CMD_LOST_LEADERSHIP
);
2408 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2409 VLOG_INFO_RL(&rl
, "term %"PRIu64
": elected leader by %d+ of "
2410 "%"PRIuSIZE
" servers", raft
->term
,
2411 raft
->n_votes
, hmap_count(&raft
->servers
));
2413 ovs_assert(raft
->role
!= RAFT_LEADER
);
2414 raft
->role
= RAFT_LEADER
;
2415 raft
->leader_sid
= raft
->sid
;
2416 raft
->election_timeout
= LLONG_MAX
;
2417 raft
->ping_timeout
= time_msec() + PING_TIME_MSEC
;
2419 struct raft_server
*s
;
2420 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
2421 raft_server_init_leader(raft
, s
);
2424 raft_update_our_match_index(raft
, raft
->log_end
- 1);
2425 raft_send_heartbeats(raft
);
2427 /* Write the fact that we are leader to the log. This is not used by the
2428 * algorithm (although it could be, for quick restart), but it is used for
2429 * offline analysis to check for conformance with the properties that Raft
2431 struct raft_record r
= {
2432 .type
= RAFT_REC_LEADER
,
2436 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
2438 /* Initiate a no-op commit. Otherwise we might never find out what's in
2439 * the log. See section 6.4 item 1:
2441 * The Leader Completeness Property guarantees that a leader has all
2442 * committed entries, but at the start of its term, it may not know
2443 * which those are. To find out, it needs to commit an entry from its
2444 * term. Raft handles this by having each leader commit a blank no-op
2445 * entry into the log at the start of its term. As soon as this no-op
2446 * entry is committed, the leader’s commit index will be at least as
2447 * large as any other servers’ during its term.
2449 raft_command_unref(raft_command_execute__(raft
, NULL
, NULL
, NULL
, NULL
));
2452 /* Processes term 'term' received as part of RPC 'common'. Returns true if the
2453 * caller should continue processing the RPC, false if the caller should reject
2454 * it due to a stale term. */
2456 raft_receive_term__(struct raft
*raft
, const struct raft_rpc_common
*common
,
2459 /* Section 3.3 says:
2461 * Current terms are exchanged whenever servers communicate; if one
2462 * server’s current term is smaller than the other’s, then it updates
2463 * its current term to the larger value. If a candidate or leader
2464 * discovers that its term is out of date, it immediately reverts to
2465 * follower state. If a server receives a request with a stale term
2466 * number, it rejects the request.
2468 if (term
> raft
->term
) {
2469 if (!raft_set_term(raft
, term
, NULL
)) {
2470 /* Failed to update the term to 'term'. */
2473 raft_become_follower(raft
);
2474 } else if (term
< raft
->term
) {
2475 char buf
[SID_LEN
+ 1];
2476 VLOG_INFO("rejecting term %"PRIu64
" < current term %"PRIu64
" received "
2477 "in %s message from server %s",
2479 raft_rpc_type_to_string(common
->type
),
2480 raft_get_nickname(raft
, &common
->sid
, buf
, sizeof buf
));
2487 raft_get_servers_from_log(struct raft
*raft
, enum vlog_level level
)
2489 const struct json
*servers_json
= raft
->snap
.servers
;
2490 for (uint64_t index
= raft
->log_end
- 1; index
>= raft
->log_start
;
2492 struct raft_entry
*e
= &raft
->entries
[index
- raft
->log_start
];
2494 servers_json
= e
->servers
;
2499 struct hmap servers
;
2500 struct ovsdb_error
*error
= raft_servers_from_json(servers_json
, &servers
);
2502 raft_set_servers(raft
, &servers
, level
);
2503 raft_servers_destroy(&servers
);
2506 /* Truncates the log, so that raft->log_end becomes 'new_end'.
2508 * Doesn't write anything to disk. In theory, we could truncate the on-disk
2509 * log file, but we don't have the right information to know how long it should
2510 * be. What we actually do is to append entries for older indexes to the
2511 * on-disk log; when we re-read it later, these entries truncate the log.
2513 * Returns true if any of the removed log entries were server configuration
2514 * entries, false otherwise. */
2516 raft_truncate(struct raft
*raft
, uint64_t new_end
)
2518 ovs_assert(new_end
>= raft
->log_start
);
2519 if (raft
->log_end
> new_end
) {
2520 char buf
[SID_LEN
+ 1];
2521 VLOG_INFO("%s truncating %"PRIu64
" entries from end of log",
2522 raft_get_nickname(raft
, &raft
->sid
, buf
, sizeof buf
),
2523 raft
->log_end
- new_end
);
2526 bool servers_changed
= false;
2527 while (raft
->log_end
> new_end
) {
2528 struct raft_entry
*entry
= &raft
->entries
[--raft
->log_end
2530 if (entry
->servers
) {
2531 servers_changed
= true;
2533 raft_entry_uninit(entry
);
2535 return servers_changed
;
2538 static const struct json
*
2539 raft_peek_next_entry(struct raft
*raft
, struct uuid
*eid
)
2541 /* Invariant: log_start - 2 <= last_applied <= commit_index < log_end. */
2542 ovs_assert(raft
->log_start
<= raft
->last_applied
+ 2);
2543 ovs_assert(raft
->last_applied
<= raft
->commit_index
);
2544 ovs_assert(raft
->commit_index
< raft
->log_end
);
2546 if (raft
->joining
|| raft
->failed
) {
2550 if (raft
->log_start
== raft
->last_applied
+ 2) {
2551 *eid
= raft
->snap
.eid
;
2552 return raft
->snap
.data
;
2555 while (raft
->last_applied
< raft
->commit_index
) {
2556 const struct raft_entry
*e
= raft_get_entry(raft
,
2557 raft
->last_applied
+ 1);
2562 raft
->last_applied
++;
2567 static const struct json
*
2568 raft_get_next_entry(struct raft
*raft
, struct uuid
*eid
)
2570 const struct json
*data
= raft_peek_next_entry(raft
, eid
);
2572 raft
->last_applied
++;
2578 raft_update_commit_index(struct raft
*raft
, uint64_t new_commit_index
)
2580 if (new_commit_index
<= raft
->commit_index
) {
2584 if (raft
->role
== RAFT_LEADER
) {
2585 while (raft
->commit_index
< new_commit_index
) {
2586 uint64_t index
= ++raft
->commit_index
;
2587 const struct raft_entry
*e
= raft_get_entry(raft
, index
);
2589 struct raft_command
*cmd
2590 = raft_find_command_by_index(raft
, index
);
2592 raft_command_complete(raft
, cmd
, RAFT_CMD_SUCCESS
);
2596 /* raft_run_reconfigure() can write a new Raft entry, which can
2597 * reallocate raft->entries, which would invalidate 'e', so
2598 * this case must be last, after the one for 'e->data'. */
2599 raft_run_reconfigure(raft
);
2603 raft
->commit_index
= new_commit_index
;
2606 /* Write the commit index to the log. The next time we restart, this
2607 * allows us to start exporting a reasonably fresh log, instead of a log
2608 * that only contains the snapshot. */
2609 struct raft_record r
= {
2610 .type
= RAFT_REC_COMMIT_INDEX
,
2611 .commit_index
= raft
->commit_index
,
2613 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
2616 /* This doesn't use rq->entries (but it does use rq->n_entries). */
2618 raft_send_append_reply(struct raft
*raft
, const struct raft_append_request
*rq
,
2619 enum raft_append_result result
, const char *comment
)
2621 /* Figure 3.1: "If leaderCommit > commitIndex, set commitIndex =
2622 * min(leaderCommit, index of last new entry)" */
2623 if (result
== RAFT_APPEND_OK
&& rq
->leader_commit
> raft
->commit_index
) {
2624 raft_update_commit_index(
2625 raft
, MIN(rq
->leader_commit
, rq
->prev_log_index
+ rq
->n_entries
));
2629 union raft_rpc reply
= {
2632 .type
= RAFT_RPC_APPEND_REPLY
,
2633 .sid
= rq
->common
.sid
,
2634 .comment
= CONST_CAST(char *, comment
),
2637 .log_end
= raft
->log_end
,
2638 .prev_log_index
= rq
->prev_log_index
,
2639 .prev_log_term
= rq
->prev_log_term
,
2640 .n_entries
= rq
->n_entries
,
2644 raft_send(raft
, &reply
);
2647 /* If 'prev_log_index' exists in 'raft''s log, in term 'prev_log_term', returns
2648 * NULL. Otherwise, returns an explanation for the mismatch. */
2650 match_index_and_term(const struct raft
*raft
,
2651 uint64_t prev_log_index
, uint64_t prev_log_term
)
2653 if (prev_log_index
< raft
->log_start
- 1) {
2654 return "mismatch before start of log";
2655 } else if (prev_log_index
== raft
->log_start
- 1) {
2656 if (prev_log_term
!= raft
->snap
.term
) {
2657 return "prev_term mismatch";
2659 } else if (prev_log_index
< raft
->log_end
) {
2660 if (raft
->entries
[prev_log_index
- raft
->log_start
].term
2662 return "term mismatch";
2665 /* prev_log_index >= raft->log_end */
2666 return "mismatch past end of log";
2672 raft_handle_append_entries(struct raft
*raft
,
2673 const struct raft_append_request
*rq
,
2674 uint64_t prev_log_index
, uint64_t prev_log_term
,
2675 const struct raft_entry
*entries
,
2676 unsigned int n_entries
)
2678 /* Section 3.5: "When sending an AppendEntries RPC, the leader includes
2679 * the index and term of the entry in its log that immediately precedes
2680 * the new entries. If the follower does not find an entry in its log
2681 * with the same index and term, then it refuses the new entries." */
2682 const char *mismatch
= match_index_and_term(raft
, prev_log_index
,
2685 VLOG_INFO("rejecting append_request because previous entry "
2686 "%"PRIu64
",%"PRIu64
" not in local log (%s)",
2687 prev_log_term
, prev_log_index
, mismatch
);
2688 raft_send_append_reply(raft
, rq
, RAFT_APPEND_INCONSISTENCY
, mismatch
);
2692 /* Figure 3.1: "If an existing entry conflicts with a new one (same
2693 * index but different terms), delete the existing entry and all that
2696 bool servers_changed
= false;
2697 for (i
= 0; ; i
++) {
2698 if (i
>= n_entries
) {
2700 if (rq
->common
.comment
2701 && !strcmp(rq
->common
.comment
, "heartbeat")) {
2702 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "heartbeat");
2704 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "no change");
2709 uint64_t log_index
= (prev_log_index
+ 1) + i
;
2710 if (log_index
>= raft
->log_end
) {
2713 if (raft
->entries
[log_index
- raft
->log_start
].term
2714 != entries
[i
].term
) {
2715 if (raft_truncate(raft
, log_index
)) {
2716 servers_changed
= true;
2722 /* Figure 3.1: "Append any entries not already in the log." */
2723 struct ovsdb_error
*error
= NULL
;
2724 bool any_written
= false;
2725 for (; i
< n_entries
; i
++) {
2726 const struct raft_entry
*e
= &entries
[i
];
2727 error
= raft_write_entry(raft
, e
->term
,
2728 json_nullable_clone(e
->data
), &e
->eid
,
2729 json_nullable_clone(e
->servers
));
2735 servers_changed
= true;
2740 raft_waiter_create(raft
, RAFT_W_ENTRY
, true)->entry
.index
2741 = raft
->log_end
- 1;
2743 if (servers_changed
) {
2744 /* The set of servers might have changed; check. */
2745 raft_get_servers_from_log(raft
, VLL_INFO
);
2749 char *s
= ovsdb_error_to_string_free(error
);
2752 raft_send_append_reply(raft
, rq
, RAFT_APPEND_IO_ERROR
, "I/O error");
2756 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "log updated");
2760 raft_update_leader(struct raft
*raft
, const struct uuid
*sid
)
2762 if (raft
->role
== RAFT_LEADER
) {
2763 char buf
[SID_LEN
+ 1];
2764 VLOG_ERR("this server is leader but server %s claims to be",
2765 raft_get_nickname(raft
, sid
, buf
, sizeof buf
));
2767 } else if (!uuid_equals(sid
, &raft
->leader_sid
)) {
2768 if (!uuid_is_zero(&raft
->leader_sid
)) {
2769 char buf1
[SID_LEN
+ 1];
2770 char buf2
[SID_LEN
+ 1];
2771 VLOG_ERR("leader for term %"PRIu64
" changed from %s to %s",
2773 raft_get_nickname(raft
, &raft
->leader_sid
,
2775 raft_get_nickname(raft
, sid
, buf2
, sizeof buf2
));
2777 char buf
[SID_LEN
+ 1];
2778 VLOG_INFO("server %s is leader for term %"PRIu64
,
2779 raft_get_nickname(raft
, sid
, buf
, sizeof buf
),
2782 raft
->leader_sid
= *sid
;
2784 /* Record the leader to the log. This is not used by the algorithm
2785 * (although it could be, for quick restart), but it is used for
2786 * offline analysis to check for conformance with the properties
2787 * that Raft guarantees. */
2788 struct raft_record r
= {
2789 .type
= RAFT_REC_LEADER
,
2793 ignore(ovsdb_log_write_and_free(raft
->log
, raft_record_to_json(&r
)));
2799 raft_handle_append_request(struct raft
*raft
,
2800 const struct raft_append_request
*rq
)
2802 /* We do not check whether the server that sent the request is part of the
2803 * cluster. As section 4.1 says, "A server accepts AppendEntries requests
2804 * from a leader that is not part of the server’s latest configuration.
2805 * Otherwise, a new server could never be added to the cluster (it would
2806 * never accept any log entries preceding the configuration entry that adds
2808 if (!raft_update_leader(raft
, &rq
->common
.sid
)) {
2809 raft_send_append_reply(raft
, rq
, RAFT_APPEND_INCONSISTENCY
,
2810 "usurped leadership");
2813 raft_reset_timer(raft
);
2815 /* First check for the common case, where the AppendEntries request is
2816 * entirely for indexes covered by 'log_start' ... 'log_end - 1', something
2819 * rq->prev_log_index
2820 * | first_entry_index
2821 * | | nth_entry_index
2825 * T | T | T | T | T |
2828 * T | T | T | T | T |
2834 uint64_t first_entry_index
= rq
->prev_log_index
+ 1;
2835 uint64_t nth_entry_index
= rq
->prev_log_index
+ rq
->n_entries
;
2836 if (OVS_LIKELY(first_entry_index
>= raft
->log_start
)) {
2837 raft_handle_append_entries(raft
, rq
,
2838 rq
->prev_log_index
, rq
->prev_log_term
,
2839 rq
->entries
, rq
->n_entries
);
2843 /* Now a series of checks for odd cases, where the AppendEntries request
2844 * extends earlier than the beginning of our log, into the log entries
2845 * discarded by the most recent snapshot. */
2848 * Handle the case where the indexes covered by rq->entries[] are entirely
2849 * disjoint with 'log_start - 1' ... 'log_end - 1', as shown below. So,
2850 * everything in the AppendEntries request must already have been
2851 * committed, and we might as well return true.
2853 * rq->prev_log_index
2854 * | first_entry_index
2855 * | | nth_entry_index
2859 * T | T | T | T | T |
2862 * T | T | T | T | T |
2868 if (nth_entry_index
< raft
->log_start
- 1) {
2869 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
,
2870 "append before log start");
2875 * Handle the case where the last entry in rq->entries[] has the same index
2876 * as 'log_start - 1', so we can compare their terms:
2878 * rq->prev_log_index
2879 * | first_entry_index
2880 * | | nth_entry_index
2884 * T | T | T | T | T |
2887 * T | T | T | T | T |
2893 * There's actually a sub-case where rq->n_entries == 0, in which we
2894 * compare rq->prev_term:
2896 * rq->prev_log_index
2904 * T | T | T | T | T |
2910 if (nth_entry_index
== raft
->log_start
- 1) {
2912 ? raft
->snap
.term
== rq
->entries
[rq
->n_entries
- 1].term
2913 : raft
->snap
.term
== rq
->prev_log_term
) {
2914 raft_send_append_reply(raft
, rq
, RAFT_APPEND_OK
, "no change");
2916 raft_send_append_reply(raft
, rq
, RAFT_APPEND_INCONSISTENCY
,
2923 * We now know that the data in rq->entries[] overlaps the data in
2924 * raft->entries[], as shown below, with some positive 'ofs':
2926 * rq->prev_log_index
2927 * | first_entry_index
2928 * | | nth_entry_index
2931 * +---+---+---+---+---+
2932 * T | T | T | T | T | T |
2933 * +---+-------+---+---+
2935 * T | T | T | T | T |
2943 * We transform this into the following by trimming the first 'ofs'
2944 * elements off of rq->entries[], ending up with the following. Notice how
2945 * we retain the term but not the data for rq->entries[ofs - 1]:
2947 * first_entry_index + ofs - 1
2948 * | first_entry_index + ofs
2949 * | | nth_entry_index + ofs
2956 * T | T | T | T | T |
2962 uint64_t ofs
= raft
->log_start
- first_entry_index
;
2963 raft_handle_append_entries(raft
, rq
,
2964 raft
->log_start
- 1, rq
->entries
[ofs
- 1].term
,
2965 &rq
->entries
[ofs
], rq
->n_entries
- ofs
);
2968 /* Returns true if 'raft' has another log entry or snapshot to read. */
2970 raft_has_next_entry(const struct raft
*raft_
)
2972 struct raft
*raft
= CONST_CAST(struct raft
*, raft_
);
2974 return raft_peek_next_entry(raft
, &eid
) != NULL
;
2977 /* Returns the next log entry or snapshot from 'raft', or NULL if there are
2978 * none left to read. Stores the entry ID of the log entry in '*eid'. Stores
2979 * true in '*is_snapshot' if the returned data is a snapshot, false if it is a
2982 raft_next_entry(struct raft
*raft
, struct uuid
*eid
, bool *is_snapshot
)
2984 const struct json
*data
= raft_get_next_entry(raft
, eid
);
2985 *is_snapshot
= data
== raft
->snap
.data
;
2989 /* Returns the log index of the last-read snapshot or log entry. */
2991 raft_get_applied_index(const struct raft
*raft
)
2993 return raft
->last_applied
;
2996 /* Returns the log index of the last snapshot or log entry that is available to
2999 raft_get_commit_index(const struct raft
*raft
)
3001 return raft
->commit_index
;
3004 static struct raft_server
*
3005 raft_find_peer(struct raft
*raft
, const struct uuid
*uuid
)
3007 struct raft_server
*s
= raft_find_server(raft
, uuid
);
3008 return s
&& !uuid_equals(&raft
->sid
, &s
->sid
) ? s
: NULL
;
3011 static struct raft_server
*
3012 raft_find_new_server(struct raft
*raft
, const struct uuid
*uuid
)
3014 return raft_server_find(&raft
->add_servers
, uuid
);
3017 /* Figure 3.1: "If there exists an N such that N > commitIndex, a
3018 * majority of matchIndex[i] >= N, and log[N].term == currentTerm, set
3019 * commitIndex = N (sections 3.5 and 3.6)." */
3021 raft_consider_updating_commit_index(struct raft
*raft
)
3023 /* This loop cannot just bail out when it comes across a log entry that
3024 * does not match the criteria. For example, Figure 3.7(d2) shows a
3025 * case where the log entry for term 2 cannot be committed directly
3026 * (because it is not for the current term) but it can be committed as
3027 * a side effect of commit the entry for term 4 (the current term).
3028 * XXX Is there a more efficient way to do this? */
3029 ovs_assert(raft
->role
== RAFT_LEADER
);
3031 uint64_t new_commit_index
= raft
->commit_index
;
3032 for (uint64_t idx
= MAX(raft
->commit_index
+ 1, raft
->log_start
);
3033 idx
< raft
->log_end
; idx
++) {
3034 if (raft
->entries
[idx
- raft
->log_start
].term
== raft
->term
) {
3036 struct raft_server
*s2
;
3037 HMAP_FOR_EACH (s2
, hmap_node
, &raft
->servers
) {
3038 if (s2
->match_index
>= idx
) {
3042 if (count
> hmap_count(&raft
->servers
) / 2) {
3043 VLOG_DBG("index %"PRIu64
" committed to %"PRIuSIZE
" servers, "
3044 "applying", idx
, count
);
3045 new_commit_index
= idx
;
3049 raft_update_commit_index(raft
, new_commit_index
);
3053 raft_update_match_index(struct raft
*raft
, struct raft_server
*s
,
3056 ovs_assert(raft
->role
== RAFT_LEADER
);
3057 if (min_index
> s
->match_index
) {
3058 s
->match_index
= min_index
;
3059 raft_consider_updating_commit_index(raft
);
3064 raft_update_our_match_index(struct raft
*raft
, uint64_t min_index
)
3066 struct raft_server
*server
= raft_find_server(raft
, &raft
->sid
);
3068 raft_update_match_index(raft
, server
, min_index
);
3073 raft_send_install_snapshot_request(struct raft
*raft
,
3074 const struct raft_server
*s
,
3075 const char *comment
)
3077 union raft_rpc rpc
= {
3078 .install_snapshot_request
= {
3080 .type
= RAFT_RPC_INSTALL_SNAPSHOT_REQUEST
,
3082 .comment
= CONST_CAST(char *, comment
),
3085 .last_index
= raft
->log_start
- 1,
3086 .last_term
= raft
->snap
.term
,
3087 .last_servers
= raft
->snap
.servers
,
3088 .last_eid
= raft
->snap
.eid
,
3089 .data
= raft
->snap
.data
,
3092 raft_send(raft
, &rpc
);
3096 raft_handle_append_reply(struct raft
*raft
,
3097 const struct raft_append_reply
*rpy
)
3099 if (raft
->role
!= RAFT_LEADER
) {
3100 VLOG_INFO("rejected append_reply (not leader)");
3104 /* Most commonly we'd be getting an AppendEntries reply from a configured
3105 * server (e.g. a peer), but we can also get them from servers in the
3106 * process of being added. */
3107 struct raft_server
*s
= raft_find_peer(raft
, &rpy
->common
.sid
);
3109 s
= raft_find_new_server(raft
, &rpy
->common
.sid
);
3111 VLOG_INFO("rejected append_reply from unknown server "SID_FMT
,
3112 SID_ARGS(&rpy
->common
.sid
));
3117 if (rpy
->result
== RAFT_APPEND_OK
) {
3118 /* Figure 3.1: "If successful, update nextIndex and matchIndex for
3119 * follower (section 3.5)." */
3120 uint64_t min_index
= rpy
->prev_log_index
+ rpy
->n_entries
+ 1;
3121 if (s
->next_index
< min_index
) {
3122 s
->next_index
= min_index
;
3124 raft_update_match_index(raft
, s
, min_index
- 1);
3126 /* Figure 3.1: "If AppendEntries fails because of log inconsistency,
3127 * decrement nextIndex and retry (section 3.5)."
3129 * We also implement the optimization suggested in section 4.2.1:
3130 * "Various approaches can make nextIndex converge to its correct value
3131 * more quickly, including those described in Chapter 3. The simplest
3132 * approach to solving this particular problem of adding a new server,
3133 * however, is to have followers return the length of their logs in the
3134 * AppendEntries response; this allows the leader to cap the follower’s
3135 * nextIndex accordingly." */
3136 s
->next_index
= (s
->next_index
> 0
3137 ? MIN(s
->next_index
- 1, rpy
->log_end
)
3140 if (rpy
->result
== RAFT_APPEND_IO_ERROR
) {
3141 /* Append failed but not because of a log inconsistency. Because
3142 * of the I/O error, there's no point in re-sending the append
3144 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3145 VLOG_INFO_RL(&rl
, "%s reported I/O error", s
->nickname
);
3151 * Our behavior here must depend on the value of next_index relative to
3152 * log_start and log_end. There are three cases:
3154 * Case 1 | Case 2 | Case 3
3155 * <---------------->|<------------->|<------------------>
3159 * T | T | T | T | T |
3165 if (s
->next_index
< raft
->log_start
) {
3167 raft_send_install_snapshot_request(raft
, s
, NULL
);
3168 } else if (s
->next_index
< raft
->log_end
) {
3170 raft_send_append_request(raft
, s
, 1, NULL
);
3173 if (s
->phase
== RAFT_PHASE_CATCHUP
) {
3174 s
->phase
= RAFT_PHASE_CAUGHT_UP
;
3175 raft_run_reconfigure(raft
);
3181 raft_should_suppress_disruptive_server(struct raft
*raft
,
3182 const union raft_rpc
*rpc
)
3184 if (rpc
->type
!= RAFT_RPC_VOTE_REQUEST
) {
3188 /* Section 4.2.3 "Disruptive Servers" says:
3190 * ...if a server receives a RequestVote request within the minimum
3191 * election timeout of hearing from a current leader, it does not update
3192 * its term or grant its vote...
3194 * ...This change conflicts with the leadership transfer mechanism as
3195 * described in Chapter 3, in which a server legitimately starts an
3196 * election without waiting an election timeout. In that case,
3197 * RequestVote messages should be processed by other servers even when
3198 * they believe a current cluster leader exists. Those RequestVote
3199 * requests can include a special flag to indicate this behavior (“I
3200 * have permission to disrupt the leader--it told me to!”).
3202 * This clearly describes how the followers should act, but not the leader.
3203 * We just ignore vote requests that arrive at a current leader. This
3204 * seems to be fairly safe, since a majority other than the current leader
3205 * can still elect a new leader and the first AppendEntries from that new
3206 * leader will depose the current leader. */
3207 const struct raft_vote_request
*rq
= raft_vote_request_cast(rpc
);
3208 if (rq
->leadership_transfer
) {
3212 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3213 long long int now
= time_msec();
3214 switch (raft
->role
) {
3216 VLOG_WARN_RL(&rl
, "ignoring vote request received as leader");
3220 if (now
< raft
->election_base
+ ELECTION_BASE_MSEC
) {
3221 VLOG_WARN_RL(&rl
, "ignoring vote request received after only "
3222 "%lld ms (minimum election time is %d ms)",
3223 now
- raft
->election_base
, ELECTION_BASE_MSEC
);
3228 case RAFT_CANDIDATE
:
3236 /* Returns true if a reply should be sent. */
3238 raft_handle_vote_request__(struct raft
*raft
,
3239 const struct raft_vote_request
*rq
)
3241 /* Figure 3.1: "If votedFor is null or candidateId, and candidate's vote is
3242 * at least as up-to-date as receiver's log, grant vote (sections 3.4,
3244 if (uuid_equals(&raft
->vote
, &rq
->common
.sid
)) {
3245 /* Already voted for this candidate in this term. Resend vote. */
3247 } else if (!uuid_is_zero(&raft
->vote
)) {
3248 /* Already voted for different candidate in this term. Send a reply
3249 * saying what candidate we did vote for. This isn't a necessary part
3250 * of the Raft protocol but it can make debugging easier. */
3254 /* Section 3.6.1: "The RequestVote RPC implements this restriction: the RPC
3255 * includes information about the candidate’s log, and the voter denies its
3256 * vote if its own log is more up-to-date than that of the candidate. Raft
3257 * determines which of two logs is more up-to-date by comparing the index
3258 * and term of the last entries in the logs. If the logs have last entries
3259 * with different terms, then the log with the later term is more
3260 * up-to-date. If the logs end with the same term, then whichever log is
3261 * longer is more up-to-date." */
3262 uint64_t last_term
= (raft
->log_end
> raft
->log_start
3263 ? raft
->entries
[raft
->log_end
- 1
3264 - raft
->log_start
].term
3266 if (last_term
> rq
->last_log_term
3267 || (last_term
== rq
->last_log_term
3268 && raft
->log_end
- 1 > rq
->last_log_index
)) {
3269 /* Our log is more up-to-date than the peer's. Withhold vote. */
3273 /* Record a vote for the peer. */
3274 if (!raft_set_term(raft
, raft
->term
, &rq
->common
.sid
)) {
3278 raft_reset_timer(raft
);
3284 raft_send_vote_reply(struct raft
*raft
, const struct uuid
*dst
,
3285 const struct uuid
*vote
)
3287 union raft_rpc rpy
= {
3290 .type
= RAFT_RPC_VOTE_REPLY
,
3297 raft_send(raft
, &rpy
);
3301 raft_handle_vote_request(struct raft
*raft
,
3302 const struct raft_vote_request
*rq
)
3304 if (raft_handle_vote_request__(raft
, rq
)) {
3305 raft_send_vote_reply(raft
, &rq
->common
.sid
, &raft
->vote
);
3310 raft_handle_vote_reply(struct raft
*raft
,
3311 const struct raft_vote_reply
*rpy
)
3313 if (!raft_receive_term__(raft
, &rpy
->common
, rpy
->term
)) {
3317 if (raft
->role
!= RAFT_CANDIDATE
) {
3321 struct raft_server
*s
= raft_find_peer(raft
, &rpy
->common
.sid
);
3323 raft_accept_vote(raft
, s
, &rpy
->vote
);
3327 /* Returns true if 'raft''s log contains reconfiguration entries that have not
3328 * yet been committed. */
3330 raft_has_uncommitted_configuration(const struct raft
*raft
)
3332 for (uint64_t i
= raft
->commit_index
+ 1; i
< raft
->log_end
; i
++) {
3333 ovs_assert(i
>= raft
->log_start
);
3334 const struct raft_entry
*e
= &raft
->entries
[i
- raft
->log_start
];
3343 raft_log_reconfiguration(struct raft
*raft
)
3345 struct json
*servers_json
= raft_servers_to_json(&raft
->servers
);
3346 raft_command_unref(raft_command_execute__(
3347 raft
, NULL
, servers_json
, NULL
, NULL
));
3348 json_destroy(servers_json
);
3352 raft_run_reconfigure(struct raft
*raft
)
3354 ovs_assert(raft
->role
== RAFT_LEADER
);
3356 /* Reconfiguration only progresses when configuration changes commit. */
3357 if (raft_has_uncommitted_configuration(raft
)) {
3361 /* If we were waiting for a configuration change to commit, it's done. */
3362 struct raft_server
*s
;
3363 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
3364 if (s
->phase
== RAFT_PHASE_COMMITTING
) {
3365 raft_send_add_server_reply__(raft
, &s
->sid
, s
->address
,
3366 true, RAFT_SERVER_COMPLETED
);
3367 s
->phase
= RAFT_PHASE_STABLE
;
3370 if (raft
->remove_server
) {
3371 raft_send_remove_server_reply__(raft
, &raft
->remove_server
->sid
,
3372 &raft
->remove_server
->requester_sid
,
3373 raft
->remove_server
->requester_conn
,
3374 true, RAFT_SERVER_COMPLETED
);
3375 raft_server_destroy(raft
->remove_server
);
3376 raft
->remove_server
= NULL
;
3379 /* If a new server is caught up, add it to the configuration. */
3380 HMAP_FOR_EACH (s
, hmap_node
, &raft
->add_servers
) {
3381 if (s
->phase
== RAFT_PHASE_CAUGHT_UP
) {
3382 /* Move 's' from 'raft->add_servers' to 'raft->servers'. */
3383 hmap_remove(&raft
->add_servers
, &s
->hmap_node
);
3384 hmap_insert(&raft
->servers
, &s
->hmap_node
, uuid_hash(&s
->sid
));
3386 /* Mark 's' as waiting for commit. */
3387 s
->phase
= RAFT_PHASE_COMMITTING
;
3389 raft_log_reconfiguration(raft
);
3391 /* When commit completes we'll transition to RAFT_PHASE_STABLE and
3392 * send a RAFT_SERVER_OK reply. */
3398 /* Remove a server, if one is scheduled for removal. */
3399 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
3400 if (s
->phase
== RAFT_PHASE_REMOVE
) {
3401 hmap_remove(&raft
->servers
, &s
->hmap_node
);
3402 raft
->remove_server
= s
;
3404 raft_log_reconfiguration(raft
);
3412 raft_handle_add_server_request(struct raft
*raft
,
3413 const struct raft_add_server_request
*rq
)
3415 /* Figure 4.1: "1. Reply NOT_LEADER if not leader (section 6.2)." */
3416 if (raft
->role
!= RAFT_LEADER
) {
3417 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_NOT_LEADER
);
3421 /* Check for an existing server. */
3422 struct raft_server
*s
= raft_find_server(raft
, &rq
->common
.sid
);
3424 /* If the server is scheduled to be removed, cancel it. */
3425 if (s
->phase
== RAFT_PHASE_REMOVE
) {
3426 s
->phase
= RAFT_PHASE_STABLE
;
3427 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_CANCELED
);
3431 /* If the server is being added, then it's in progress. */
3432 if (s
->phase
!= RAFT_PHASE_STABLE
) {
3433 raft_send_add_server_reply(raft
, rq
,
3434 false, RAFT_SERVER_IN_PROGRESS
);
3437 /* Nothing to do--server is already part of the configuration. */
3438 raft_send_add_server_reply(raft
, rq
,
3439 true, RAFT_SERVER_ALREADY_PRESENT
);
3443 /* Check for a server being removed. */
3444 if (raft
->remove_server
3445 && uuid_equals(&rq
->common
.sid
, &raft
->remove_server
->sid
)) {
3446 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_COMMITTING
);
3450 /* Check for a server already being added. */
3451 if (raft_find_new_server(raft
, &rq
->common
.sid
)) {
3452 raft_send_add_server_reply(raft
, rq
, false, RAFT_SERVER_IN_PROGRESS
);
3456 /* Add server to 'add_servers'. */
3457 s
= raft_server_add(&raft
->add_servers
, &rq
->common
.sid
, rq
->address
);
3458 raft_server_init_leader(raft
, s
);
3459 s
->requester_sid
= rq
->common
.sid
;
3460 s
->requester_conn
= NULL
;
3461 s
->phase
= RAFT_PHASE_CATCHUP
;
3463 /* Start sending the log. If this is the first time we've tried to add
3464 * this server, then this will quickly degenerate into an InstallSnapshot
3465 * followed by a series of AddEntries, but if it's a retry of an earlier
3466 * AddRequest that was interrupted (e.g. by a timeout or a loss of
3467 * leadership) then it will gracefully resume populating the log.
3469 * See the last few paragraphs of section 4.2.1 for further insight. */
3470 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3472 "starting to add server %s ("SID_FMT
" at %s) "
3473 "to cluster "CID_FMT
, s
->nickname
, SID_ARGS(&s
->sid
),
3474 rq
->address
, CID_ARGS(&raft
->cid
));
3475 raft_send_append_request(raft
, s
, 0, "initialize new server");
3479 raft_handle_add_server_reply(struct raft
*raft
,
3480 const struct raft_add_server_reply
*rpy
)
3482 if (!raft
->joining
) {
3483 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3484 VLOG_WARN_RL(&rl
, "received add_server_reply even though we're "
3485 "already part of the cluster");
3490 raft
->joining
= false;
3492 /* It is tempting, at this point, to check that this server is part of
3493 * the current configuration. However, this is not necessarily the
3494 * case, because the log entry that added this server to the cluster
3495 * might have been committed by a majority of the cluster that does not
3496 * include this one. This actually happens in testing. */
3498 const char *address
;
3499 SSET_FOR_EACH (address
, &rpy
->remote_addresses
) {
3500 if (sset_add(&raft
->remote_addresses
, address
)) {
3501 VLOG_INFO("%s: learned new server address for joining cluster",
3508 /* This is called by raft_unixctl_kick() as well as via RPC. */
3510 raft_handle_remove_server_request(struct raft
*raft
,
3511 const struct raft_remove_server_request
*rq
)
3513 /* Figure 4.1: "1. Reply NOT_LEADER if not leader (section 6.2)." */
3514 if (raft
->role
!= RAFT_LEADER
) {
3515 raft_send_remove_server_reply(raft
, rq
, false, RAFT_SERVER_NOT_LEADER
);
3519 /* If the server to remove is currently waiting to be added, cancel it. */
3520 struct raft_server
*target
= raft_find_new_server(raft
, &rq
->sid
);
3522 raft_send_add_server_reply__(raft
, &target
->sid
, target
->address
,
3523 false, RAFT_SERVER_CANCELED
);
3524 hmap_remove(&raft
->add_servers
, &target
->hmap_node
);
3525 raft_server_destroy(target
);
3529 /* If the server isn't configured, report that. */
3530 target
= raft_find_server(raft
, &rq
->sid
);
3532 raft_send_remove_server_reply(raft
, rq
,
3533 true, RAFT_SERVER_ALREADY_GONE
);
3537 /* Check whether we're waiting for the addition of the server to commit. */
3538 if (target
->phase
== RAFT_PHASE_COMMITTING
) {
3539 raft_send_remove_server_reply(raft
, rq
, false, RAFT_SERVER_COMMITTING
);
3543 /* Check whether the server is already scheduled for removal. */
3544 if (target
->phase
== RAFT_PHASE_REMOVE
) {
3545 raft_send_remove_server_reply(raft
, rq
,
3546 false, RAFT_SERVER_IN_PROGRESS
);
3550 /* Make sure that if we remove this server then that at least one other
3551 * server will be left. We don't count servers currently being added (in
3552 * 'add_servers') since those could fail. */
3553 struct raft_server
*s
;
3555 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
3556 if (s
!= target
&& s
->phase
!= RAFT_PHASE_REMOVE
) {
3561 raft_send_remove_server_reply(raft
, rq
, false, RAFT_SERVER_EMPTY
);
3565 /* Mark the server for removal. */
3566 target
->phase
= RAFT_PHASE_REMOVE
;
3567 if (rq
->requester_conn
) {
3568 target
->requester_sid
= UUID_ZERO
;
3569 unixctl_command_reply(rq
->requester_conn
, "started removal");
3571 target
->requester_sid
= rq
->common
.sid
;
3572 target
->requester_conn
= NULL
;
3575 raft_run_reconfigure(raft
);
3576 /* Operation in progress, reply will be sent later. */
3580 raft_finished_leaving_cluster(struct raft
*raft
)
3582 VLOG_INFO(SID_FMT
": finished leaving cluster "CID_FMT
,
3583 SID_ARGS(&raft
->sid
), CID_ARGS(&raft
->cid
));
3585 raft_record_note(raft
, "left", "this server left the cluster");
3587 raft
->leaving
= false;
3592 raft_handle_remove_server_reply(struct raft
*raft
,
3593 const struct raft_remove_server_reply
*rpc
)
3596 && (uuid_is_zero(&rpc
->target_sid
)
3597 || uuid_equals(&rpc
->target_sid
, &raft
->sid
))) {
3598 raft_finished_leaving_cluster(raft
);
3603 raft_handle_write_error(struct raft
*raft
, struct ovsdb_error
*error
)
3605 if (error
&& !raft
->failed
) {
3606 raft
->failed
= true;
3608 char *s
= ovsdb_error_to_string_free(error
);
3609 VLOG_WARN("%s: entering failure mode due to I/O error (%s)",
3613 return !raft
->failed
;
3616 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
3617 raft_write_snapshot(struct raft
*raft
, struct ovsdb_log
*log
,
3618 uint64_t new_log_start
,
3619 const struct raft_entry
*new_snapshot
)
3621 struct raft_header h
= {
3625 .local_address
= raft
->local_address
,
3626 .snap_index
= new_log_start
- 1,
3627 .snap
= *new_snapshot
,
3629 struct ovsdb_error
*error
= ovsdb_log_write_and_free(
3630 log
, raft_header_to_json(&h
));
3634 ovsdb_log_mark_base(raft
->log
);
3636 /* Write log records. */
3637 for (uint64_t index
= new_log_start
; index
< raft
->log_end
; index
++) {
3638 const struct raft_entry
*e
= &raft
->entries
[index
- raft
->log_start
];
3639 struct raft_record r
= {
3640 .type
= RAFT_REC_ENTRY
,
3645 .servers
= e
->servers
,
3649 error
= ovsdb_log_write_and_free(log
, raft_record_to_json(&r
));
3655 /* Write term and vote (if any).
3657 * The term is redundant if we wrote a log record for that term above. The
3658 * vote, if any, is never redundant.
3660 error
= raft_write_state(log
, raft
->term
, &raft
->vote
);
3665 /* Write commit_index if it's beyond the new start of the log. */
3666 if (raft
->commit_index
>= new_log_start
) {
3667 struct raft_record r
= {
3668 .type
= RAFT_REC_COMMIT_INDEX
,
3669 .commit_index
= raft
->commit_index
,
3671 return ovsdb_log_write_and_free(log
, raft_record_to_json(&r
));
3676 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
3677 raft_save_snapshot(struct raft
*raft
,
3678 uint64_t new_start
, const struct raft_entry
*new_snapshot
)
3681 struct ovsdb_log
*new_log
;
3682 struct ovsdb_error
*error
;
3683 error
= ovsdb_log_replace_start(raft
->log
, &new_log
);
3688 error
= raft_write_snapshot(raft
, new_log
, new_start
, new_snapshot
);
3690 ovsdb_log_replace_abort(new_log
);
3694 return ovsdb_log_replace_commit(raft
->log
, new_log
);
3698 raft_handle_install_snapshot_request__(
3699 struct raft
*raft
, const struct raft_install_snapshot_request
*rq
)
3701 raft_reset_timer(raft
);
3704 * Our behavior here depend on new_log_start in the snapshot compared to
3705 * log_start and log_end. There are three cases:
3707 * Case 1 | Case 2 | Case 3
3708 * <---------------->|<------------->|<------------------>
3712 * T | T | T | T | T |
3718 uint64_t new_log_start
= rq
->last_index
+ 1;
3719 if (new_log_start
< raft
->log_start
) {
3720 /* Case 1: The new snapshot covers less than our current one. Nothing
3723 } else if (new_log_start
< raft
->log_end
) {
3724 /* Case 2: The new snapshot starts in the middle of our log. We could
3725 * discard the first 'new_log_start - raft->log_start' entries in the
3726 * log. But there's not much value in that, since snapshotting is
3727 * supposed to be a local decision. Just skip it. */
3731 /* Case 3: The new snapshot starts past the end of our current log, so
3732 * discard all of our current log. */
3733 const struct raft_entry new_snapshot
= {
3734 .term
= rq
->last_term
,
3736 .eid
= rq
->last_eid
,
3737 .servers
= rq
->last_servers
,
3739 struct ovsdb_error
*error
= raft_save_snapshot(raft
, new_log_start
,
3742 char *error_s
= ovsdb_error_to_string(error
);
3743 VLOG_WARN("could not save snapshot: %s", error_s
);
3748 for (size_t i
= 0; i
< raft
->log_end
- raft
->log_start
; i
++) {
3749 raft_entry_uninit(&raft
->entries
[i
]);
3751 raft
->log_start
= raft
->log_end
= new_log_start
;
3752 raft
->log_synced
= raft
->log_end
- 1;
3753 raft
->commit_index
= raft
->log_start
- 1;
3754 if (raft
->last_applied
< raft
->commit_index
) {
3755 raft
->last_applied
= raft
->log_start
- 2;
3758 raft_entry_uninit(&raft
->snap
);
3759 raft_entry_clone(&raft
->snap
, &new_snapshot
);
3761 raft_get_servers_from_log(raft
, VLL_INFO
);
3767 raft_handle_install_snapshot_request(
3768 struct raft
*raft
, const struct raft_install_snapshot_request
*rq
)
3770 if (raft_handle_install_snapshot_request__(raft
, rq
)) {
3771 union raft_rpc rpy
= {
3772 .install_snapshot_reply
= {
3774 .type
= RAFT_RPC_INSTALL_SNAPSHOT_REPLY
,
3775 .sid
= rq
->common
.sid
,
3778 .last_index
= rq
->last_index
,
3779 .last_term
= rq
->last_term
,
3782 raft_send(raft
, &rpy
);
3787 raft_handle_install_snapshot_reply(
3788 struct raft
*raft
, const struct raft_install_snapshot_reply
*rpy
)
3790 /* We might get an InstallSnapshot reply from a configured server (e.g. a
3791 * peer) or a server in the process of being added. */
3792 struct raft_server
*s
= raft_find_peer(raft
, &rpy
->common
.sid
);
3794 s
= raft_find_new_server(raft
, &rpy
->common
.sid
);
3796 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3797 VLOG_INFO_RL(&rl
, "cluster "CID_FMT
": received %s from "
3798 "unknown server "SID_FMT
, CID_ARGS(&raft
->cid
),
3799 raft_rpc_type_to_string(rpy
->common
.type
),
3800 SID_ARGS(&rpy
->common
.sid
));
3805 if (rpy
->last_index
!= raft
->log_start
- 1 ||
3806 rpy
->last_term
!= raft
->snap
.term
) {
3807 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3808 VLOG_INFO_RL(&rl
, "cluster "CID_FMT
": server %s installed "
3809 "out-of-date snapshot, starting over",
3810 CID_ARGS(&raft
->cid
), s
->nickname
);
3811 raft_send_install_snapshot_request(raft
, s
,
3812 "installed obsolete snapshot");
3816 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3817 VLOG_INFO_RL(&rl
, "cluster "CID_FMT
": installed snapshot on server %s "
3818 " up to %"PRIu64
":%"PRIu64
, CID_ARGS(&raft
->cid
),
3819 s
->nickname
, rpy
->last_term
, rpy
->last_index
);
3820 s
->next_index
= raft
->log_end
;
3821 raft_send_append_request(raft
, s
, 0, "snapshot installed");
3824 /* Returns true if 'raft' has grown enough since the last snapshot that
3825 * reducing the log to a snapshot would be valuable, false otherwise. */
3827 raft_grew_lots(const struct raft
*raft
)
3829 return ovsdb_log_grew_lots(raft
->log
);
3832 /* Returns the number of log entries that could be trimmed off the on-disk log
3833 * by snapshotting. */
3835 raft_get_log_length(const struct raft
*raft
)
3837 return (raft
->last_applied
< raft
->log_start
3839 : raft
->last_applied
- raft
->log_start
+ 1);
3842 /* Returns true if taking a snapshot of 'raft', with raft_store_snapshot(), is
3845 raft_may_snapshot(const struct raft
*raft
)
3847 return (!raft
->joining
3851 && raft
->last_applied
>= raft
->log_start
);
3854 /* Replaces the log for 'raft', up to the last log entry read, by
3855 * 'new_snapshot_data'. Returns NULL if successful, otherwise an error that
3856 * the caller must eventually free.
3858 * This function can only succeed if raft_may_snapshot() returns true. It is
3859 * only valuable to call it if raft_get_log_length() is significant and
3860 * especially if raft_grew_lots() returns true. */
3861 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
3862 raft_store_snapshot(struct raft
*raft
, const struct json
*new_snapshot_data
)
3864 if (raft
->joining
) {
3865 return ovsdb_error(NULL
,
3866 "cannot store a snapshot while joining cluster");
3867 } else if (raft
->leaving
) {
3868 return ovsdb_error(NULL
,
3869 "cannot store a snapshot while leaving cluster");
3870 } else if (raft
->left
) {
3871 return ovsdb_error(NULL
,
3872 "cannot store a snapshot after leaving cluster");
3873 } else if (raft
->failed
) {
3874 return ovsdb_error(NULL
,
3875 "cannot store a snapshot following failure");
3878 if (raft
->last_applied
< raft
->log_start
) {
3879 return ovsdb_error(NULL
, "not storing a duplicate snapshot");
3882 uint64_t new_log_start
= raft
->last_applied
+ 1;
3883 struct raft_entry new_snapshot
= {
3884 .term
= raft_get_term(raft
, new_log_start
- 1),
3885 .data
= json_clone(new_snapshot_data
),
3886 .eid
= *raft_get_eid(raft
, new_log_start
- 1),
3887 .servers
= json_clone(raft_servers_for_index(raft
, new_log_start
- 1)),
3889 struct ovsdb_error
*error
= raft_save_snapshot(raft
, new_log_start
,
3892 raft_entry_uninit(&new_snapshot
);
3896 raft
->log_synced
= raft
->log_end
- 1;
3897 raft_entry_uninit(&raft
->snap
);
3898 raft
->snap
= new_snapshot
;
3899 for (size_t i
= 0; i
< new_log_start
- raft
->log_start
; i
++) {
3900 raft_entry_uninit(&raft
->entries
[i
]);
3902 memmove(&raft
->entries
[0], &raft
->entries
[new_log_start
- raft
->log_start
],
3903 (raft
->log_end
- new_log_start
) * sizeof *raft
->entries
);
3904 raft
->log_start
= new_log_start
;
3909 raft_handle_become_leader(struct raft
*raft
,
3910 const struct raft_become_leader
*rq
)
3912 if (raft
->role
== RAFT_FOLLOWER
) {
3913 char buf
[SID_LEN
+ 1];
3914 VLOG_INFO("received leadership transfer from %s in term %"PRIu64
,
3915 raft_get_nickname(raft
, &rq
->common
.sid
, buf
, sizeof buf
),
3917 raft_start_election(raft
, true);
3922 raft_send_execute_command_reply(struct raft
*raft
,
3923 const struct uuid
*sid
,
3924 const struct uuid
*eid
,
3925 enum raft_command_status status
,
3926 uint64_t commit_index
)
3928 union raft_rpc rpc
= {
3929 .execute_command_reply
= {
3931 .type
= RAFT_RPC_EXECUTE_COMMAND_REPLY
,
3936 .commit_index
= commit_index
,
3939 raft_send(raft
, &rpc
);
3942 static enum raft_command_status
3943 raft_handle_execute_command_request__(
3944 struct raft
*raft
, const struct raft_execute_command_request
*rq
)
3946 if (raft
->role
!= RAFT_LEADER
) {
3947 return RAFT_CMD_NOT_LEADER
;
3950 const struct uuid
*current_eid
= raft_current_eid(raft
);
3951 if (!uuid_equals(&rq
->prereq
, current_eid
)) {
3952 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3953 VLOG_INFO_RL(&rl
, "current entry eid "UUID_FMT
" does not match "
3954 "prerequisite "UUID_FMT
" in execute_command_request",
3955 UUID_ARGS(current_eid
), UUID_ARGS(&rq
->prereq
));
3956 return RAFT_CMD_BAD_PREREQ
;
3959 struct raft_command
*cmd
= raft_command_initiate(raft
, rq
->data
,
3961 cmd
->sid
= rq
->common
.sid
;
3963 enum raft_command_status status
= cmd
->status
;
3964 if (status
!= RAFT_CMD_INCOMPLETE
) {
3965 raft_command_unref(cmd
);
3971 raft_handle_execute_command_request(
3972 struct raft
*raft
, const struct raft_execute_command_request
*rq
)
3974 enum raft_command_status status
3975 = raft_handle_execute_command_request__(raft
, rq
);
3976 if (status
!= RAFT_CMD_INCOMPLETE
) {
3977 raft_send_execute_command_reply(raft
, &rq
->common
.sid
, &rq
->result
,
3983 raft_handle_execute_command_reply(
3984 struct raft
*raft
, const struct raft_execute_command_reply
*rpy
)
3986 struct raft_command
*cmd
= raft_find_command_by_eid(raft
, &rpy
->result
);
3988 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3989 char buf
[SID_LEN
+ 1];
3991 "%s received \"%s\" reply from %s for unknown command",
3992 raft
->local_nickname
,
3993 raft_command_status_to_string(rpy
->status
),
3994 raft_get_nickname(raft
, &rpy
->common
.sid
,
3999 if (rpy
->status
== RAFT_CMD_INCOMPLETE
) {
4000 cmd
->timestamp
= time_msec();
4002 cmd
->index
= rpy
->commit_index
;
4003 raft_command_complete(raft
, cmd
, rpy
->status
);
4008 raft_handle_rpc(struct raft
*raft
, const union raft_rpc
*rpc
)
4010 uint64_t term
= raft_rpc_get_term(rpc
);
4012 && !raft_should_suppress_disruptive_server(raft
, rpc
)
4013 && !raft_receive_term__(raft
, &rpc
->common
, term
)) {
4014 if (rpc
->type
== RAFT_RPC_APPEND_REQUEST
) {
4015 /* Section 3.3: "If a server receives a request with a stale term
4016 * number, it rejects the request." */
4017 raft_send_append_reply(raft
, raft_append_request_cast(rpc
),
4018 RAFT_APPEND_INCONSISTENCY
, "stale term");
4023 switch (rpc
->type
) {
4024 #define RAFT_RPC(ENUM, NAME) \
4026 raft_handle_##NAME(raft, &rpc->NAME); \
4036 raft_rpc_is_heartbeat(const union raft_rpc
*rpc
)
4038 return ((rpc
->type
== RAFT_RPC_APPEND_REQUEST
4039 || rpc
->type
== RAFT_RPC_APPEND_REPLY
)
4040 && rpc
->common
.comment
4041 && !strcmp(rpc
->common
.comment
, "heartbeat"));
4046 raft_send_to_conn_at(struct raft
*raft
, const union raft_rpc
*rpc
,
4047 struct raft_conn
*conn
, int line_number
)
4049 log_rpc(rpc
, "-->", conn
, line_number
);
4050 return !jsonrpc_session_send(
4051 conn
->js
, raft_rpc_to_jsonrpc(&raft
->cid
, &raft
->sid
, rpc
));
4055 raft_is_rpc_synced(const struct raft
*raft
, const union raft_rpc
*rpc
)
4057 uint64_t term
= raft_rpc_get_term(rpc
);
4058 uint64_t index
= raft_rpc_get_min_sync_index(rpc
);
4059 const struct uuid
*vote
= raft_rpc_get_vote(rpc
);
4061 return (term
<= raft
->synced_term
4062 && index
<= raft
->log_synced
4063 && (!vote
|| uuid_equals(vote
, &raft
->synced_vote
)));
4067 raft_send_at(struct raft
*raft
, const union raft_rpc
*rpc
, int line_number
)
4069 const struct uuid
*dst
= &rpc
->common
.sid
;
4070 if (uuid_equals(dst
, &raft
->sid
)) {
4071 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
4072 VLOG_WARN_RL(&rl
, "attempted to send RPC to self from raft.c:%d",
4077 struct raft_conn
*conn
= raft_find_conn_by_sid(raft
, dst
);
4079 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
4080 char buf
[SID_LEN
+ 1];
4081 VLOG_DBG_RL(&rl
, "%s: no connection to %s, cannot send RPC "
4082 "from raft.c:%d", raft
->local_nickname
,
4083 raft_get_nickname(raft
, dst
, buf
, sizeof buf
),
4088 if (!raft_is_rpc_synced(raft
, rpc
)) {
4089 raft_waiter_create(raft
, RAFT_W_RPC
, false)->rpc
= raft_rpc_clone(rpc
);
4093 return raft_send_to_conn_at(raft
, rpc
, conn
, line_number
);
4096 static struct raft
*
4097 raft_lookup_by_name(const char *name
)
4101 HMAP_FOR_EACH_WITH_HASH (raft
, hmap_node
, hash_string(name
, 0),
4103 if (!strcmp(raft
->name
, name
)) {
4111 raft_unixctl_cid(struct unixctl_conn
*conn
,
4112 int argc OVS_UNUSED
, const char *argv
[],
4113 void *aux OVS_UNUSED
)
4115 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4117 unixctl_command_reply_error(conn
, "unknown cluster");
4118 } else if (uuid_is_zero(&raft
->cid
)) {
4119 unixctl_command_reply_error(conn
, "cluster id not yet known");
4121 char *uuid
= xasprintf(UUID_FMT
, UUID_ARGS(&raft
->cid
));
4122 unixctl_command_reply(conn
, uuid
);
4128 raft_unixctl_sid(struct unixctl_conn
*conn
,
4129 int argc OVS_UNUSED
, const char *argv
[],
4130 void *aux OVS_UNUSED
)
4132 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4134 unixctl_command_reply_error(conn
, "unknown cluster");
4136 char *uuid
= xasprintf(UUID_FMT
, UUID_ARGS(&raft
->sid
));
4137 unixctl_command_reply(conn
, uuid
);
4143 raft_put_sid(const char *title
, const struct uuid
*sid
,
4144 const struct raft
*raft
, struct ds
*s
)
4146 ds_put_format(s
, "%s: ", title
);
4147 if (uuid_equals(sid
, &raft
->sid
)) {
4148 ds_put_cstr(s
, "self");
4149 } else if (uuid_is_zero(sid
)) {
4150 ds_put_cstr(s
, "unknown");
4152 char buf
[SID_LEN
+ 1];
4153 ds_put_cstr(s
, raft_get_nickname(raft
, sid
, buf
, sizeof buf
));
4155 ds_put_char(s
, '\n');
4159 raft_unixctl_status(struct unixctl_conn
*conn
,
4160 int argc OVS_UNUSED
, const char *argv
[],
4161 void *aux OVS_UNUSED
)
4163 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4165 unixctl_command_reply_error(conn
, "unknown cluster");
4169 struct ds s
= DS_EMPTY_INITIALIZER
;
4170 ds_put_format(&s
, "%s\n", raft
->local_nickname
);
4171 ds_put_format(&s
, "Name: %s\n", raft
->name
);
4172 ds_put_format(&s
, "Cluster ID: ");
4173 if (!uuid_is_zero(&raft
->cid
)) {
4174 ds_put_format(&s
, CID_FMT
" ("UUID_FMT
")\n",
4175 CID_ARGS(&raft
->cid
), UUID_ARGS(&raft
->cid
));
4177 ds_put_format(&s
, "not yet known\n");
4179 ds_put_format(&s
, "Server ID: "SID_FMT
" ("UUID_FMT
")\n",
4180 SID_ARGS(&raft
->sid
), UUID_ARGS(&raft
->sid
));
4181 ds_put_format(&s
, "Address: %s\n", raft
->local_address
);
4182 ds_put_format(&s
, "Status: %s\n",
4183 raft
->joining
? "joining cluster"
4184 : raft
->leaving
? "leaving cluster"
4185 : raft
->left
? "left cluster"
4186 : raft
->failed
? "failed"
4187 : "cluster member");
4188 if (raft
->joining
) {
4189 ds_put_format(&s
, "Remotes for joining:");
4190 const char *address
;
4191 SSET_FOR_EACH (address
, &raft
->remote_addresses
) {
4192 ds_put_format(&s
, " %s", address
);
4194 ds_put_char(&s
, '\n');
4196 if (raft
->role
== RAFT_LEADER
) {
4197 struct raft_server
*as
;
4198 HMAP_FOR_EACH (as
, hmap_node
, &raft
->add_servers
) {
4199 ds_put_format(&s
, "Adding server %s ("SID_FMT
" at %s) (%s)\n",
4200 as
->nickname
, SID_ARGS(&as
->sid
), as
->address
,
4201 raft_server_phase_to_string(as
->phase
));
4204 struct raft_server
*rs
= raft
->remove_server
;
4206 ds_put_format(&s
, "Removing server %s ("SID_FMT
" at %s) (%s)\n",
4207 rs
->nickname
, SID_ARGS(&rs
->sid
), rs
->address
,
4208 raft_server_phase_to_string(rs
->phase
));
4212 ds_put_format(&s
, "Role: %s\n",
4213 raft
->role
== RAFT_LEADER
? "leader"
4214 : raft
->role
== RAFT_CANDIDATE
? "candidate"
4215 : raft
->role
== RAFT_FOLLOWER
? "follower"
4217 ds_put_format(&s
, "Term: %"PRIu64
"\n", raft
->term
);
4218 raft_put_sid("Leader", &raft
->leader_sid
, raft
, &s
);
4219 raft_put_sid("Vote", &raft
->vote
, raft
, &s
);
4220 ds_put_char(&s
, '\n');
4222 ds_put_format(&s
, "Log: [%"PRIu64
", %"PRIu64
"]\n",
4223 raft
->log_start
, raft
->log_end
);
4225 uint64_t n_uncommitted
= raft
->log_end
- raft
->commit_index
- 1;
4226 ds_put_format(&s
, "Entries not yet committed: %"PRIu64
"\n", n_uncommitted
);
4228 uint64_t n_unapplied
= raft
->log_end
- raft
->last_applied
- 1;
4229 ds_put_format(&s
, "Entries not yet applied: %"PRIu64
"\n", n_unapplied
);
4231 const struct raft_conn
*c
;
4232 ds_put_cstr(&s
, "Connections:");
4233 LIST_FOR_EACH (c
, list_node
, &raft
->conns
) {
4234 bool connected
= jsonrpc_session_is_connected(c
->js
);
4235 ds_put_format(&s
, " %s%s%s%s",
4236 connected
? "" : "(",
4237 c
->incoming
? "<-" : "->", c
->nickname
,
4238 connected
? "" : ")");
4240 ds_put_char(&s
, '\n');
4242 ds_put_cstr(&s
, "Servers:\n");
4243 struct raft_server
*server
;
4244 HMAP_FOR_EACH (server
, hmap_node
, &raft
->servers
) {
4245 ds_put_format(&s
, " %s ("SID_FMT
" at %s)",
4247 SID_ARGS(&server
->sid
), server
->address
);
4248 if (uuid_equals(&server
->sid
, &raft
->sid
)) {
4249 ds_put_cstr(&s
, " (self)");
4251 if (server
->phase
!= RAFT_PHASE_STABLE
) {
4252 ds_put_format (&s
, " (%s)",
4253 raft_server_phase_to_string(server
->phase
));
4255 if (raft
->role
== RAFT_CANDIDATE
) {
4256 if (!uuid_is_zero(&server
->vote
)) {
4257 char buf
[SID_LEN
+ 1];
4258 ds_put_format(&s
, " (voted for %s)",
4259 raft_get_nickname(raft
, &server
->vote
,
4262 } else if (raft
->role
== RAFT_LEADER
) {
4263 ds_put_format(&s
, " next_index=%"PRIu64
" match_index=%"PRIu64
,
4264 server
->next_index
, server
->match_index
);
4266 ds_put_char(&s
, '\n');
4269 unixctl_command_reply(conn
, ds_cstr(&s
));
4274 raft_unixctl_leave__(struct unixctl_conn
*conn
, struct raft
*raft
)
4276 if (raft_is_leaving(raft
)) {
4277 unixctl_command_reply_error(conn
,
4278 "already in progress leaving cluster");
4279 } else if (raft_is_joining(raft
)) {
4280 unixctl_command_reply_error(conn
,
4281 "can't leave while join in progress");
4282 } else if (raft_failed(raft
)) {
4283 unixctl_command_reply_error(conn
,
4284 "can't leave after failure");
4287 unixctl_command_reply(conn
, NULL
);
4292 raft_unixctl_leave(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4293 const char *argv
[], void *aux OVS_UNUSED
)
4295 struct raft
*raft
= raft_lookup_by_name(argv
[1]);
4297 unixctl_command_reply_error(conn
, "unknown cluster");
4301 raft_unixctl_leave__(conn
, raft
);
4304 static struct raft_server
*
4305 raft_lookup_server_best_match(struct raft
*raft
, const char *id
)
4307 struct raft_server
*best
= NULL
;
4308 int best_score
= -1;
4311 struct raft_server
*s
;
4312 HMAP_FOR_EACH (s
, hmap_node
, &raft
->servers
) {
4313 int score
= (!strcmp(id
, s
->address
)
4315 : uuid_is_partial_match(&s
->sid
, id
));
4316 if (score
> best_score
) {
4320 } else if (score
== best_score
) {
4324 return n_best
== 1 ? best
: NULL
;
4328 raft_unixctl_kick(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4329 const char *argv
[], void *aux OVS_UNUSED
)
4331 const char *cluster_name
= argv
[1];
4332 const char *server_name
= argv
[2];
4334 struct raft
*raft
= raft_lookup_by_name(cluster_name
);
4336 unixctl_command_reply_error(conn
, "unknown cluster");
4340 struct raft_server
*server
= raft_lookup_server_best_match(raft
,
4343 unixctl_command_reply_error(conn
, "unknown server");
4347 if (uuid_equals(&server
->sid
, &raft
->sid
)) {
4348 raft_unixctl_leave__(conn
, raft
);
4349 } else if (raft
->role
== RAFT_LEADER
) {
4350 const struct raft_remove_server_request rq
= {
4352 .requester_conn
= conn
,
4354 raft_handle_remove_server_request(raft
, &rq
);
4356 const union raft_rpc rpc
= {
4357 .remove_server_request
= {
4359 .type
= RAFT_RPC_REMOVE_SERVER_REQUEST
,
4360 .sid
= raft
->leader_sid
,
4361 .comment
= "via unixctl"
4366 if (raft_send(raft
, &rpc
)) {
4367 unixctl_command_reply(conn
, "sent removal request to leader");
4369 unixctl_command_reply_error(conn
,
4370 "failed to send removal request");
4378 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
4379 if (!ovsthread_once_start(&once
)) {
4382 unixctl_command_register("cluster/cid", "DB", 1, 1,
4383 raft_unixctl_cid
, NULL
);
4384 unixctl_command_register("cluster/sid", "DB", 1, 1,
4385 raft_unixctl_sid
, NULL
);
4386 unixctl_command_register("cluster/status", "DB", 1, 1,
4387 raft_unixctl_status
, NULL
);
4388 unixctl_command_register("cluster/leave", "DB", 1, 1,
4389 raft_unixctl_leave
, NULL
);
4390 unixctl_command_register("cluster/kick", "DB SERVER", 2, 2,
4391 raft_unixctl_kick
, NULL
);
4392 ovsthread_once_done(&once
);