diff --git a/raft/fsm.cc b/raft/fsm.cc index 124d1d29a0..16e823afb5 100644 --- a/raft/fsm.cc +++ b/raft/fsm.cc @@ -28,6 +28,9 @@ fsm::fsm(server_id id, term_t current_term, server_id voted_for, log log, failure_detector& failure_detector, fsm_config config) : _my_id(id), _current_term(current_term), _voted_for(voted_for), _log(std::move(log)), _failure_detector(failure_detector), _config(config) { + if (id == raft::server_id{}) { + throw std::invalid_argument("raft::fsm: raft instance cannot have id zero"); + } // The snapshot can not contain uncommitted entries _commit_idx = _log.get_snapshot().idx; _observed.advance(*this); diff --git a/raft/raft.hh b/raft/raft.hh index e004c23c1e..8ba50167d9 100644 --- a/raft/raft.hh +++ b/raft/raft.hh @@ -104,12 +104,19 @@ struct configuration { configuration(std::initializer_list ids) { current.reserve(ids.size()); for (auto&& id : ids) { + if (id == server_id{}) { + throw std::invalid_argument("raft::configuration: id zero is not supported"); + } current.emplace(server_address{std::move(id)}); } } configuration(server_address_set current_arg = {}, server_address_set previous_arg = {}) - : current(std::move(current_arg)), previous(std::move(previous_arg)) {} + : current(std::move(current_arg)), previous(std::move(previous_arg)) { + if (current.count(server_address{server_id()}) || previous.count(server_address{server_id()})) { + throw std::invalid_argument("raft::configuration: id zero is not supported"); + } + } // Return true if the previous configuration is still // in use