Skip to content

Commit

Permalink
k/metadata: guesstimate leader when information is not yet present
Browse files Browse the repository at this point in the history
When partition is first created in Redpanda some of the cluster nodes
which are not hosting partition replicas may not yet have leadership
metadata. In this case Redpanda still has to return partition metadata.
In order not to disturb the client (returning -1 as a leader id may
cause some clients to stop) Redpanda has to return a leader id. If the
information is not present we will always return the first node from
replica set in leader epoch equal to 0. This way client will either
communicate with the actual leader or issue a metadata request to other
node that may contain up to date information.

Fixes: #15949

Signed-off-by: Michal Maslanka <michal@redpanda.com>
(cherry picked from commit 7a60b75)
  • Loading branch information
mmaslankaprv authored and Michal Maslanka committed Jan 24, 2024
1 parent ecd4030 commit 5f13a37
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/v/kafka/server/handlers/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ std::optional<cluster::leader_term> get_leader_term(
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.
* fallback to leader guesstimating. We return first replica from the
* replica set and term 0. (This is the same logic that has been a part of
* cluster::topic_dispatcher before)
*/
if (!leader_term) {
auto idx = fast_prng_source() % replicas.size();
leader_term.emplace(replicas[idx], model::term_id(-1));
leader_term.emplace(replicas[0], model::term_id(0));
return leader_term;
}
if (!leader_term->leader.has_value()) {
Expand Down

0 comments on commit 5f13a37

Please sign in to comment.