Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raft: don't promote to voter if previous config is not committed #17675

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/v/archival/tests/archival_metadata_stm_gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,17 @@ TEST_F_CORO(
10s,
[&reached_dispatch_append,
&may_resume_append](raft::raft_node_instance& node) {
node.on_dispatch(
[&reached_dispatch_append, &may_resume_append](raft::msg_type t) {
if (t == raft::msg_type::append_entries) {
if (!reached_dispatch_append.available()) {
reached_dispatch_append.set_value(true);
}
return may_resume_append.get_shared_future();
}

return ss::now();
});
node.on_dispatch([&reached_dispatch_append, &may_resume_append](
model::node_id, raft::msg_type t) {
if (t == raft::msg_type::append_entries) {
if (!reached_dispatch_append.available()) {
reached_dispatch_append.set_value(true);
}
return may_resume_append.get_shared_future();
}

return ss::now();
});

return node.get_vnode();
});
Expand Down
39 changes: 27 additions & 12 deletions src/v/raft/consensus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,19 +520,34 @@ void consensus::maybe_promote_to_voter(vnode id) {
return ss::now();
}

vlog(_ctxlog.trace, "promoting node {} to voter", id);
return _op_lock.get_units()
.then([this, id](ssx::semaphore_units u) mutable {
auto latest_cfg = _configuration_manager.get_latest();
latest_cfg.promote_to_voter(id);
// do not promote if the previous configuration is still uncommitted,
// otherwise we may add several new voters in quick succession, that the
// old voters will not know of, resulting in a possibility of
// non-intersecting quorums.
if (_configuration_manager.get_latest_offset() > _commit_index) {
return ss::now();
}

return replicate_configuration(
std::move(u), std::move(latest_cfg));
})
.then([this, id](std::error_code ec) {
vlog(
_ctxlog.trace, "node {} promotion result {}", id, ec.message());
});
return _op_lock.get_units().then([this,
id](ssx::semaphore_units u) mutable {
// check once more under _op_lock to protect against races with
// concurrent voter promotions.
if (_configuration_manager.get_latest_offset() > _commit_index) {
return ss::now();
}

vlog(_ctxlog.trace, "promoting node {} to voter", id);
auto latest_cfg = _configuration_manager.get_latest();
latest_cfg.promote_to_voter(id);
return replicate_configuration(std::move(u), std::move(latest_cfg))
.then([this, id](std::error_code ec) {
vlog(
_ctxlog.trace,
"node {} promotion result {}",
id,
ec.message());
});
});
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/v/raft/tests/basic_raft_fixture_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ TEST_P_CORO(
co_await set_write_caching(params.write_caching);

for (auto& [_, node] : nodes()) {
node->on_dispatch([](raft::msg_type t) {
node->on_dispatch([](model::node_id, raft::msg_type t) {
if (
t == raft::msg_type::append_entries
&& random_generators::get_int(1000) > 800) {
Expand Down Expand Up @@ -405,7 +405,7 @@ TEST_P_CORO(quorum_acks_fixture, test_progress_on_truncation) {
// truncation.
for (auto& [id, node] : nodes()) {
if (id == leader_id) {
node->on_dispatch([](raft::msg_type t) {
node->on_dispatch([](model::node_id, raft::msg_type t) {
if (
t == raft::msg_type::append_entries
|| t == raft::msg_type::vote) {
Expand Down
13 changes: 7 additions & 6 deletions src/v/raft/tests/raft_fixture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ channel& in_memory_test_protocol::get_channel(model::node_id id) {
return *it->second;
}

void in_memory_test_protocol::on_dispatch(
ss::noncopyable_function<ss::future<>(msg_type)> f) {
void in_memory_test_protocol::on_dispatch(dispatch_callback_t f) {
_on_dispatch_handlers.push_back(std::move(f));
}

Expand Down Expand Up @@ -301,7 +300,7 @@ in_memory_test_protocol::dispatch(model::node_id id, ReqT req) {

const auto msg_type = map_msg_type<ReqT>();
for (const auto& f : _on_dispatch_handlers) {
co_await f(msg_type);
co_await f(id, msg_type);
}

try {
Expand Down Expand Up @@ -557,8 +556,7 @@ raft_node_instance::random_batch_base_offset(model::offset max) {
co_return batches.front().base_offset();
}

void raft_node_instance::on_dispatch(
ss::noncopyable_function<ss::future<>(msg_type)> f) {
void raft_node_instance::on_dispatch(dispatch_callback_t f) {
_protocol->on_dispatch(std::move(f));
}

Expand All @@ -569,6 +567,9 @@ seastar::future<> raft_fixture::TearDownAsync() {
co_await seastar::coroutine::parallel_for_each(
_nodes, [](auto& pair) { return pair.second->stop(); });

co_await seastar::coroutine::parallel_for_each(
_nodes, [](auto& pair) { return pair.second->remove_data(); });

co_await _features.stop();
}
seastar::future<> raft_fixture::SetUpAsync() {
Expand Down Expand Up @@ -620,7 +621,7 @@ raft_fixture::stop_node(model::node_id id, remove_data_dir remove) {
}

raft_node_instance& raft_fixture::node(model::node_id id) {
return *_nodes.find(id)->second;
return *_nodes.at(id);
}

ss::future<model::node_id>
Expand Down
10 changes: 6 additions & 4 deletions src/v/raft/tests/raft_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ struct raft_node_map {
node_for(model::node_id) = 0;
};

using dispatch_callback_t
= ss::noncopyable_function<ss::future<>(model::node_id, msg_type)>;

class in_memory_test_protocol : public consensus_client_protocol::impl {
public:
explicit in_memory_test_protocol(raft_node_map&, prefix_logger&);
Expand Down Expand Up @@ -129,7 +132,7 @@ class in_memory_test_protocol : public consensus_client_protocol::impl {

channel& get_channel(model::node_id id);

void on_dispatch(ss::noncopyable_function<ss::future<>(msg_type)> f);
void on_dispatch(dispatch_callback_t f);

ss::future<> stop();

Expand All @@ -138,8 +141,7 @@ class in_memory_test_protocol : public consensus_client_protocol::impl {
ss::future<result<RespT>> dispatch(model::node_id, ReqT req);
ss::gate _gate;
absl::flat_hash_map<model::node_id, std::unique_ptr<channel>> _channels;
std::vector<ss::noncopyable_function<ss::future<>(msg_type)>>
_on_dispatch_handlers;
std::vector<dispatch_callback_t> _on_dispatch_handlers;
raft_node_map& _nodes;
prefix_logger& _logger;
};
Expand Down Expand Up @@ -227,7 +229,7 @@ class raft_node_instance : public ss::weakly_referencable<raft_node_instance> {
///
//// \param f The callback function to be invoked when a message is
/// dispatched.
void on_dispatch(ss::noncopyable_function<ss::future<>(msg_type)> f);
void on_dispatch(dispatch_callback_t);

private:
model::node_id _id;
Expand Down
Loading
Loading