lianetm commented on code in PR #23664:
URL: https://github.com/apache/kafka/pull/23664#discussion_r4539746351
##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ShareConsumeRequestManagerTest.java:
##########
@@ -3047,6 +3063,116 @@ void testWhenLeadershipChangedAfterDisconnected() {
fetchedRecords = partitionRecords.get(tp0);
assertEquals(1, fetchedRecords.size());
+
+ // The redirect leaves tp0 on node 1. The next poll removes tp0 from
the share session on node 0.
+ assertEquals(nodeId1.id(),
shareConsumeRequestManager.shareSessionNodeId(tip0));
+ pollResult = shareConsumeRequestManager.sendFetchesReturnPollResult();
+ requestsByNode.clear();
+ pollResult.unsentRequests.forEach(unsentRequest ->
+ requestsByNode.put(unsentRequest.node().get().id(),
((ShareFetchRequest.Builder) unsentRequest.requestBuilder()).data()));
+ node0Request = requestsByNode.get(nodeId0.id());
+ assertNotNull(node0Request);
+ assertTrue(node0Request.topics().isEmpty());
+ assertEquals(1, node0Request.forgottenTopicsData().size());
+ assertEquals(List.of(tip0.partition()),
node0Request.forgottenTopicsData().get(0).partitions());
+ }
+
+ /**
+ * A disconnect forgets the cached leader, but if the metadata still names
the same node, the next fetch simply
+ * retries that node with a new share session containing the partition.
+ */
+ @Test
+ void testDisconnectWithoutLeadershipChangeRetriesSameNode() {
+ buildRequestManager();
+
+ subscriptions.subscribeToShareGroup(Set.of(topicName));
Review Comment:
uhm we just renamed this with the SubscriptionState refactoring right? let's
rebase
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareConsumeRequestManager.java:
##########
@@ -310,13 +283,59 @@ public PollResult poll(long currentTimeMs) {
removeSessionHandlersForMissingNodes(missingNodes);
}
+ // Fail any acknowledgements which are queued for nodes without a
share session, since they can never be sent.
+ failAcknowledgementsForNodesWithoutSession();
+
return new PollResult(requests);
}
private boolean isShareAcquireModeRecordLimit() {
return shareFetchConfig.shareAcquireMode ==
ShareAcquireMode.RECORD_LIMIT;
}
+ /**
+ * Resolve the topic ID and leader for each partition to fetch, caching
the information in {@link #shareSessionTopicIdMap}
+ * and {@link #shareSessionLeaderMap}. The cached leader is normally kept
up to date by redirects from the brokers
+ * but it is replaced from the metadata if the cached node is no longer in
the cluster, or if the metadata holds a
+ * strictly newer leader epoch. A metadata update is requested if the
topic ID or leader cannot be resolved.
+ */
+ private void resolveShareSessionLeaders(Cluster cluster) {
+ Map<String, Uuid> topicIds = metadata.topicIds();
+ for (TopicPartition partition : subscriptions.fetchablePartitions()) {
+ TopicIdPartition tip = shareSessionTopicIdMap.get(partition);
+ if (tip == null) {
+ Uuid topicId = topicIds.get(partition.topic());
+ if (topicId == null) {
+ log.debug("Requesting metadata update for partition {}
since topic ID is missing", partition);
+ metadata.requestUpdate(false);
+ continue;
+ }
+
+ tip = new TopicIdPartition(topicId, partition);
+ shareSessionTopicIdMap.put(partition, tip);
+ }
+
+ LeaderIdAndEpoch leader = shareSessionLeaderMap.get(tip);
+ Metadata.LeaderAndEpoch metadataLeader =
metadata.currentLeader(partition);
+ if (leader == null || cluster.nodeById(leader.leaderId) == null) {
+ if (metadataLeader.leader.isEmpty()) {
+ log.debug("Requesting metadata update for partition {}
since current leader node is missing", partition);
+ metadata.requestUpdate(false);
+ shareSessionLeaderMap.remove(tip);
+ continue;
+ }
+
+ shareSessionLeaderMap.put(tip, new
LeaderIdAndEpoch(metadataLeader.leader.get().id(),
metadataLeader.epoch.orElse(-1)));
+ } else if (metadataLeader.leader.isPresent() &&
metadataLeader.epoch.isPresent() && metadataLeader.epoch.get() > leader.epoch) {
+ // The metadata has a newer leader epoch than the cached
leader, so the cached leader is stale.
+ // This happens when the leader changes while the cached
leader is unreachable and so cannot redirect.
Review Comment:
this case fixed here was a gap in the original cache impl right?
(https://issues.apache.org/jira/browse/KAFKA-20736). Should we link the jiras
or PRs to make clear that the issue affects 4.4 and is fixed here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]