raft: disallow adding and creating servers with id zero

Id zero has special meaning in the code and cannot be valid server id.
Message-Id: <20210407134853.1964226-1-gleb@scylladb.com>
This commit is contained in:
Gleb Natapov
2021-04-07 16:48:52 +03:00
committed by Tomasz Grabiec
parent 3687757115
commit fb938a36d4
2 changed files with 11 additions and 1 deletions

View File

@@ -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);

View File

@@ -104,12 +104,19 @@ struct configuration {
configuration(std::initializer_list<server_id> 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