Skip to content

Commit

Permalink
k/metadata: do not return no leader when leader is not yet available
Browse files Browse the repository at this point in the history
Instead of returning `-1` indicating that there is an ongoing leader
election we return randomly selected node as a partition leader. This is
much less interrupting for the client as it simply forces it to refresh
metadata. Some clients do not tolerate `-1` returned from Metadata
handler and simply stop working.

Signed-off-by: Michal Maslanka <michal@redpanda.com>
  • Loading branch information
mmaslankaprv committed Jan 2, 2024
1 parent 8f81843 commit 86d583b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/v/kafka/server/handlers/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ std::optional<cluster::leader_term> get_leader_term(
const cluster::metadata_cache& md_cache,
const std::vector<model::node_id>& replicas) {
auto leader_term = md_cache.get_leader_term(tp_ns, p_id);
/**
* If current broker do not yet have any information about leadership we
* simply return random node to force the client metadata update.
*/
if (!leader_term) {
return std::nullopt;
auto idx = fast_prng_source() % replicas.size();
leader_term.emplace(replicas[idx], model::term_id(-1));
return leader_term;
}
if (!leader_term->leader.has_value()) {
const auto previous = md_cache.get_previous_leader_id(tp_ns, p_id);
Expand Down

0 comments on commit 86d583b

Please sign in to comment.