Skip to content

KAFKA-20736: Improve share session handling on leader change#22766

Merged
AndrewJSchofield merged 7 commits into
apache:trunkfrom
AndrewJSchofield:KAFKA-20736-2
Jul 20, 2026
Merged

KAFKA-20736: Improve share session handling on leader change#22766
AndrewJSchofield merged 7 commits into
apache:trunkfrom
AndrewJSchofield:KAFKA-20736-2

Conversation

@AndrewJSchofield

@AndrewJSchofield AndrewJSchofield commented Jul 6, 2026

Copy link
Copy Markdown
Member

In some situations, the share consumer does not tidy up share sessions
sufficiently on leadership change. In particular, if a broker loses
leadership of a partition, the share consumer should send a further
ShareFetch/ShareAcknowledge to remove the partition from its share
session. In most cases, this removal happens naturally, but it depends
upon whether the share consumer has other requests to send to that
broker. This PR tidies up the maintenance of share sessions when
leadership changes occur.

Reviewers: Lianet Magrans lmagrans@confluent.io, Shivsundar R
shr@confluent.io, Apoorv Mittal apoorvmittal10@gmail.com

@AndrewJSchofield
AndrewJSchofield marked this pull request as draft July 6, 2026 14:52
@AndrewJSchofield
AndrewJSchofield marked this pull request as ready for review July 13, 2026 13:52
@AndrewJSchofield
AndrewJSchofield requested a review from lianetm July 13, 2026 19:34
@AndrewJSchofield AndrewJSchofield added the KIP-932 Queues for Kafka label Jul 13, 2026

@lianetm lianetm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Took a first pass

partitionsWithUpdatedLeaderInfo.put(tip.topicPartition(), new Metadata.LeaderIdAndEpoch(
Optional.of(partitionData.currentLeader().leaderId()), Optional.of(partitionData.currentLeader().leaderEpoch())));

maybeUpdateLeaderCache(tip, partitionData.currentLeader().leaderId(), partitionData.currentLeader().leaderEpoch());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we have the case that the partition is not assigned anymore by the time we hit this? Metadata does consider the subscription and retains accordingly (the retainTopic logic), so wondering if we need something similar to be aware of the subscription while maintaining this cache?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The awareness of the assigned partitions comes from SubscriptionState in the fetchablePartitions() call at the start of the poll. So, yes, I think it is possible that a partition might not be assigned any more by the time we hit this. However, because share groups have intentionally looser semantics than consumer groups, this is expected. If a share consumer receives some records from an assigned partition, and then the partition is revoked, it is still able to continue delivery of those records and complete their acknowledgements, potentially for a significant length of time (renew acks).

int nodeId = leader.leaderId;
Node node = cluster.nodeById(nodeId);
if (node == null) {
continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the case that we had the leader in the sessionLeaderMap, but the node is not in the cluster metadata, don't we need to clear our local leader cache that has stale info? and also request metadata I guess, something changed from the view we had.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I've been reworking this code in parallel to ensure I clean up as the cluster changes. For example, if a broker is decommissioned under a running share consumer, it needs to complete pending acks which it will never be able to send. I'll push a new commit shortly.

@AndrewJSchofield

Copy link
Copy Markdown
Member Author

Flaky ShareConsumerTest > testLeaderRestartWithoutLeadershipChangeImplicitAcknowledgement not introduced by this PR. A fix will be in the next PR aligning the exceptions thrown in two related error cases.

@AndrewJSchofield

Copy link
Copy Markdown
Member Author

Flaky ShareConsumerTest > testLeaderRestartWithoutLeadershipChangeImplicitAcknowledgement not introduced by this PR. A fix will be in the next PR aligning the exceptions thrown in two related error cases.

On further investigation, this PR probably DOES introduce the flakiness. I'll sort it out in this PR.

@ShivsundarR ShivsundarR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @AndrewJSchofield ! Looks like a very nice improvement. Just a minor nit comment. Otherwise looks good.

@ShivsundarR ShivsundarR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thanks for the PR.

@apoorvmittal10 apoorvmittal10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be helpful, thanks for the PR. LGTM!

@AndrewJSchofield
AndrewJSchofield merged commit 33ea66b into apache:trunk Jul 20, 2026
23 checks passed
@AndrewJSchofield
AndrewJSchofield deleted the KAFKA-20736-2 branch July 20, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants