From 5042dfbdcbbc18963fe71a5f831798cefc4df816 Mon Sep 17 00:00:00 2001 From: Michal Maslanka Date: Fri, 9 Feb 2024 11:05:51 +0100 Subject: [PATCH] k/replicated_partition: use confirmed term as a source of leader epoch A leader epoch is used by Kafka clients to determine if a replica is up to date with the leader and to detect truncation. The leader epoch differs from Raft term as the term is updated when leader election starts. Whereas the leader epoch is updated after the state of the replica is determined. Therefore the leader epoch uses confirmed term instead of the simple term which is incremented every time the leader election starts. Signed-off-by: Michal Maslanka (cherry picked from commit e746f79bd85a288b378a1dcbcdb6d0c9cf25f853) --- src/v/kafka/server/replicated_partition.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/v/kafka/server/replicated_partition.h b/src/v/kafka/server/replicated_partition.h index 3916e6992ffab..7905cbbd83168 100644 --- a/src/v/kafka/server/replicated_partition.h +++ b/src/v/kafka/server/replicated_partition.h @@ -173,9 +173,18 @@ class replicated_partition final : public kafka::partition_proxy::impl { ss::future> get_leader_epoch_last_offset(kafka::leader_epoch) const final; - + /** + * A leader epoch is used by Kafka clients to determine if a replica is up + * to date with the leader and to detect truncation. + * + * The leader epoch differs from Raft term as the term is updated when + * leader election starts. Whereas the leader epoch is updated after the + * state of the replica is determined. Therefore the leader epoch uses + * confirmed term instead of the simple term which is incremented every time + * the leader election starts. + */ kafka::leader_epoch leader_epoch() const final { - return leader_epoch_from_term(_partition->term()); + return leader_epoch_from_term(_partition->raft()->confirmed_term()); } ss::future validate_fetch_offset(