You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "apoorvmittal10 (via GitHub)" <gi...@apache.org> on 2024/11/13 13:39:14 UTC
[PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
apoorvmittal10 opened a new pull request, #17796:
URL: https://github.com/apache/kafka/pull/17796
### Note: I haven't added tests yet and plan to do once get confirmation on the approach, however tested manually with multi broker setup.
The PR adds listeners to Partition class which might invoke the clean up of share partitions on Partition changes.
I considered introducing new Listener class which defines methods per SharePartitionKey but avoided as that would have required wiring up at the calls at different places in ReplicaManager. Additionally replica manager needed handling to store the new listener, etc. I found the current PR approach better as SharePartition is abstracted Shared view of Partition hence attaching the change events on Partition is helpful.
### Committer Checklist (excluded from commit message)
- [ ] Verify design and implementation
- [ ] Verify test coverage and CI build status
- [ ] Verify documentation (including upgrade notes)
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1842703563
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +669,83 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey) {
+ SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
+ if (sharePartition != null) {
+ sharePartition.markFenced();
+ }
+ }
+
+ /**
+ * The SharePartitionListener is used to listen for partition events. The share partition is associated with
+ * the topic-partition, we need to handle the partition events for the share partition.
+ * <p>
+ * The partition cache map stores share partitions against share partition key which comprises
+ * group and topic-partition. Instead of maintaining a separate map for topic-partition to share partitions,
+ * we can maintain the share partition key in the listener and create a new listener for each share partition.
+ */
+ private class SharePartitionListener implements PartitionListener {
+
+ private final SharePartitionKey sharePartitionKey;
+
+ private SharePartitionListener(SharePartitionKey sharePartitionKey) {
+ this.sharePartitionKey = sharePartitionKey;
+ }
+
+ /**
+ * The onFailed method is called when a Partition is marked offline.
+ *
+ * @param topicPartition The topic-partition that has been marked offline.
+ */
+ @Override
+ public void onFailed(TopicPartition topicPartition) {
+ log.info("The share partition failed listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ /**
+ * The onDeleted method is called when a Partition is deleted.
+ *
+ * @param topicPartition The topic-partition that has been deleted.
+ */
+ @Override
+ public void onDeleted(TopicPartition topicPartition) {
+ log.info("The share partition delete listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ /**
+ * The onFollower method is called when a Partition is marked follower.
+ *
+ * @param topicPartition The topic-partition that has been marked as follower.
+ */
+ @Override
+ public void onFollower(TopicPartition topicPartition) {
+ log.info("The share partition leader change listener is invoked for the topic-partition: {}, share-partition: {}",
Review Comment:
INFO is probably too noisy for this as we expect it any time the topic leadership changes.
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -609,6 +610,12 @@ private SharePartition getOrCreateSharePartition(SharePartitionKey sharePartitio
k -> {
long start = time.hiResClockMs();
int leaderEpoch = ShareFetchUtils.leaderEpoch(replicaManager, sharePartitionKey.topicIdPartition().topicPartition());
+ // Attach listener to Partition which shall invoke partition change handlers.
+ // However, as there could be multiple share partitions (per group name) for a single topic-partition,
+ // hence create separate listeners per share partition which holds the share partition key
+ // to identify the respective share partition.
+ replicaManager.maybeAddListener(sharePartitionKey.topicIdPartition().topicPartition(),
Review Comment:
This will only add the listener if the Partition is online. Is it possible for us to attempt to initialize a SharePartition before a partition is loaded? Or do we have some guards in SPM/KafkaApis?
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1847228860
##########
core/src/main/scala/kafka/cluster/Partition.scala:
##########
@@ -82,6 +82,11 @@ trait PartitionListener {
* that the partition was deleted but only that this broker does not host a replica of it any more.
*/
def onDeleted(partition: TopicPartition): Unit = {}
+
+ /**
+ * Called when the Partition on this broker is marked as follower.
+ */
+ def onFollower(partition: TopicPartition): Unit = {}
Review Comment:
Do you think either onLeaderToFollower or onBecomeFollower could be the better names?
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1868505445
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +670,78 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey, partitionCacheMap, replicaManager);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private static void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> map, ReplicaManager replicaManager) {
+ SharePartition sharePartition = map.remove(sharePartitionKey);
+ if (sharePartition != null) {
+ sharePartition.markFenced();
+ replicaManager.removeListener(sharePartitionKey.topicIdPartition().topicPartition(), sharePartition.listener());
+ }
+ }
+
+ /**
+ * The SharePartitionListener is used to listen for partition events. The share partition is associated with
+ * the topic-partition, we need to handle the partition events for the share partition.
+ * <p>
+ * The partition cache map stores share partitions against share partition key which comprises
+ * group and topic-partition. Instead of maintaining a separate map for topic-partition to share partitions,
+ * we can maintain the share partition key in the listener and create a new listener for each share partition.
+ */
+ static class SharePartitionListener implements PartitionListener {
+
+ private final SharePartitionKey sharePartitionKey;
+ private final ReplicaManager replicaManager;
+ private final Map<SharePartitionKey, SharePartition> partitionCacheMap;
+
+ SharePartitionListener(
+ SharePartitionKey sharePartitionKey,
+ ReplicaManager replicaManager,
+ Map<SharePartitionKey, SharePartition> partitionCacheMap
+ ) {
+ this.sharePartitionKey = sharePartitionKey;
+ this.replicaManager = replicaManager;
+ this.partitionCacheMap = partitionCacheMap;
+ }
+
+ @Override
+ public void onFailed(TopicPartition topicPartition) {
+ log.debug("The share partition failed listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ @Override
+ public void onDeleted(TopicPartition topicPartition) {
+ log.debug("The share partition delete listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ @Override
+ public void onBecomingFollower(TopicPartition topicPartition) {
+ log.debug("The share partition leader change listener is invoked for the topic-partition: {}, share-partition: {}",
Review Comment:
Done.
##########
core/src/test/scala/unit/kafka/server/ReplicaManagerTest.scala:
##########
@@ -6798,6 +6798,105 @@ class ReplicaManagerTest {
assertEquals(Double.NaN, maxMetric.metricValue)
}
+ @Test
+ def testBecomeFollowerInvokeOnBecomingFollowerListener(): Unit = {
+ val localId = 1
+ val topicPartition = new TopicPartition("foo", 0)
+ val replicaManager = setupReplicaManagerWithMockedPurgatories(new MockTimer(time), localId)
+ // Attach listener to partition.
+ val offsetCheckpoints = new LazyOffsetCheckpoints(replicaManager.highWatermarkCheckpoints.asJava)
+ replicaManager.createPartition(topicPartition).createLogIfNotExists(isNew = false, isFutureReplica = false, offsetCheckpoints, None)
+ val listener = new MockPartitionListener
+ assertTrue(replicaManager.maybeAddListener(topicPartition, listener))
+ listener.verify()
+
+ try {
+ // Make the local replica the leader
+ val leaderTopicsDelta = topicsCreateDelta(localId, true)
+ val leaderMetadataImage = imageFromTopics(leaderTopicsDelta.apply())
+
+ replicaManager.applyDelta(leaderTopicsDelta, leaderMetadataImage)
+
+ // Check the state of that partition and fetcher
+ val HostedPartition.Online(leaderPartition) = replicaManager.getPartition(topicPartition)
+ assertTrue(leaderPartition.isLeader)
+ assertEquals(0, leaderPartition.getLeaderEpoch)
+ // On becoming follower listener should not be invoked yet.
+ listener.verify()
+
+ // Change the local replica to follower
+ val followerTopicsDelta = topicsChangeDelta(leaderMetadataImage.topics(), localId, false)
+ val followerMetadataImage = imageFromTopics(followerTopicsDelta.apply())
+ replicaManager.applyDelta(followerTopicsDelta, followerMetadataImage)
+
+ // On becoming follower listener should be invoked.
+ listener.verify(expectedFollower = true)
+
+ // Check the state of that partition.
+ val HostedPartition.Online(followerPartition) = replicaManager.getPartition(topicPartition)
+ assertFalse(followerPartition.isLeader)
+ assertEquals(1, followerPartition.getLeaderEpoch)
+ } finally {
+ replicaManager.shutdown(checkpointHW = false)
+ }
+ }
+
+ @Test
+ def testBecomeFollowerNotInvokeOnBecomingFollowerListenerOnZk(): Unit = {
Review Comment:
Done, removed.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2505944088
@AndrewJSchofield @mumrah @junrao I have updated the code with tests. Also removed listener call from locked `markFollower` method, @AndrewJSchofield mentioned, rather moved to `replicaManager` itself. Please review.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1842838376
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +669,83 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey) {
+ SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
+ if (sharePartition != null) {
+ sharePartition.markFenced();
+ }
+ }
+
+ /**
+ * The SharePartitionListener is used to listen for partition events. The share partition is associated with
+ * the topic-partition, we need to handle the partition events for the share partition.
+ * <p>
+ * The partition cache map stores share partitions against share partition key which comprises
+ * group and topic-partition. Instead of maintaining a separate map for topic-partition to share partitions,
+ * we can maintain the share partition key in the listener and create a new listener for each share partition.
+ */
+ private class SharePartitionListener implements PartitionListener {
+
+ private final SharePartitionKey sharePartitionKey;
+
+ private SharePartitionListener(SharePartitionKey sharePartitionKey) {
+ this.sharePartitionKey = sharePartitionKey;
+ }
+
+ /**
+ * The onFailed method is called when a Partition is marked offline.
+ *
+ * @param topicPartition The topic-partition that has been marked offline.
+ */
+ @Override
+ public void onFailed(TopicPartition topicPartition) {
+ log.info("The share partition failed listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ /**
+ * The onDeleted method is called when a Partition is deleted.
+ *
+ * @param topicPartition The topic-partition that has been deleted.
+ */
+ @Override
+ public void onDeleted(TopicPartition topicPartition) {
+ log.info("The share partition delete listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ /**
+ * The onFollower method is called when a Partition is marked follower.
+ *
+ * @param topicPartition The topic-partition that has been marked as follower.
+ */
+ @Override
+ public void onFollower(TopicPartition topicPartition) {
+ log.info("The share partition leader change listener is invoked for the topic-partition: {}, share-partition: {}",
Review Comment:
Sure, can make it debug as well.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2484067533
> @apoorvmittal10 : Thanks for the PR. Left a comment.
>
> Regarding whether to use a listener at the Partition level or from BrokerMetadataPublisher, it's probably better to use a Partition level listener. It's true that the source of truth of the metadata is BrokerMetadataPublisher. However, the share partition requests only interact with metadata at the Partition level. So, it's more natural to take an action to share partitions after the metadata has been reflected at the Partition level. Otherwise, what could happen is that we remove a partition after the metadata changes in BrokerMetadataPublisher, but not in Partition yet. When we try to create the new SharePartition, it's still created with the outdated leaderEpoch.
Thanks a lot for confirming. Then I will go ahead with this approach and ll add tests to the PR.
cc: @mumrah @dajac
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2518795005
> Yes, if the number of groups in really huge then it might cause a bit delay. Do you think that is a concern?
In fact, I don't have strong evidence about the performance issue. I may run a benchmark for the scenario, but before conducting the experiment, I agree with @junrao's comment that the size is limited by the groups.
By the way, this patch is LGTM :)
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2474212658
Thanks @AndrewJSchofield for review and good points.
> Can the partition become a leader after it has previously become a follower? I just wonder whether the state machine is more complicated than this PR implies?
Yes, that can always happen. Once it does then next share fetch request should load the share partition. The scenario is not very different then partition being shuffled across brokers. The broker who is the leader of the partition should be able to load share partition.
> The listener is called under a lock in the Partition. The listener implementation needs to be very careful not to do anything brave under that lock. I expect there is potential for a deadlock here.
The lock is for each Partition, but I do get the point. I can make the call outside lock as well, I ll wait for @junrao's comments as he can help.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1866757013
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -609,6 +610,12 @@ private SharePartition getOrCreateSharePartition(SharePartitionKey sharePartitio
k -> {
long start = time.hiResClockMs();
int leaderEpoch = ShareFetchUtils.leaderEpoch(replicaManager, sharePartitionKey.topicIdPartition().topicPartition());
+ // Attach listener to Partition which shall invoke partition change handlers.
+ // However, as there could be multiple share partitions (per group name) for a single topic-partition,
+ // hence create separate listeners per share partition which holds the share partition key
+ // to identify the respective share partition.
+ replicaManager.maybeAddListener(sharePartitionKey.topicIdPartition().topicPartition(),
Review Comment:
Valid point, I have attached a removListener method on cleanup. Done.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2518579925
@chia7712 @junrao Please if I can get re-review.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1842746190
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -609,6 +610,12 @@ private SharePartition getOrCreateSharePartition(SharePartitionKey sharePartitio
k -> {
long start = time.hiResClockMs();
int leaderEpoch = ShareFetchUtils.leaderEpoch(replicaManager, sharePartitionKey.topicIdPartition().topicPartition());
+ // Attach listener to Partition which shall invoke partition change handlers.
+ // However, as there could be multiple share partitions (per group name) for a single topic-partition,
+ // hence create separate listeners per share partition which holds the share partition key
+ // to identify the respective share partition.
+ replicaManager.maybeAddListener(sharePartitionKey.topicIdPartition().topicPartition(),
Review Comment:
This will only add the listener if the Partition is online. Is it possible for us to attempt to initialize a SharePartition before a partition is loaded? Or do we have some guards in SPM/KafkaApis?
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1866757255
##########
core/src/main/scala/kafka/server/ReplicaManager.scala:
##########
@@ -2455,6 +2455,8 @@ class ReplicaManager(val config: KafkaConfig,
if (metadataCache.hasAliveBroker(newLeaderBrokerId)) {
// Only change partition state when the leader is available
if (partition.makeFollower(partitionState, highWatermarkCheckpoints, topicIds(partitionState.topicName))) {
+ // Invoke the follower transition listeners for the partition.
+ partition.invokeLeaderToFollowerListeners()
Review Comment:
Thanks for that, I have removed it and validated in tests as well.
##########
core/src/main/scala/kafka/cluster/Partition.scala:
##########
@@ -82,6 +82,11 @@ trait PartitionListener {
* that the partition was deleted but only that this broker does not host a replica of it any more.
*/
def onDeleted(partition: TopicPartition): Unit = {}
+
+ /**
+ * Called when the Partition on this broker is transitioned to follower from leader.
+ */
+ def onLeaderToFollower(partition: TopicPartition): Unit = {}
Review Comment:
Done.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "junrao (via GitHub)" <gi...@apache.org>.
junrao commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1866371497
##########
core/src/main/scala/kafka/server/ReplicaManager.scala:
##########
@@ -2455,6 +2455,8 @@ class ReplicaManager(val config: KafkaConfig,
if (metadataCache.hasAliveBroker(newLeaderBrokerId)) {
// Only change partition state when the leader is available
if (partition.makeFollower(partitionState, highWatermarkCheckpoints, topicIds(partitionState.topicName))) {
+ // Invoke the follower transition listeners for the partition.
+ partition.invokeLeaderToFollowerListeners()
Review Comment:
This is the ZK path, which won't be supported in 4.0.
##########
core/src/main/scala/kafka/cluster/Partition.scala:
##########
@@ -82,6 +82,11 @@ trait PartitionListener {
* that the partition was deleted but only that this broker does not host a replica of it any more.
*/
def onDeleted(partition: TopicPartition): Unit = {}
+
+ /**
+ * Called when the Partition on this broker is transitioned to follower from leader.
+ */
+ def onLeaderToFollower(partition: TopicPartition): Unit = {}
Review Comment:
This method is invoke when a replica becomes a follower and may not be previously a leader. So, we could just name this method sth like `onBecomingFollower`.
##########
core/src/test/scala/unit/kafka/server/ReplicaManagerTest.scala:
##########
@@ -6798,6 +6798,62 @@ class ReplicaManagerTest {
assertEquals(Double.NaN, maxMetric.metricValue)
}
+ @Test
+ def testBecomeFollowerInvokeLeaderToFollowerListener(): Unit = {
Review Comment:
LeaderAndIsrRequest is used in ZK-based controller, which won't be supported in 4.0. We want to test KRaft-based controller logic instead.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1862046525
##########
core/src/main/scala/kafka/cluster/Partition.scala:
##########
@@ -82,6 +82,11 @@ trait PartitionListener {
* that the partition was deleted but only that this broker does not host a replica of it any more.
*/
def onDeleted(partition: TopicPartition): Unit = {}
+
+ /**
+ * Called when the Partition on this broker is marked as follower.
+ */
+ def onFollower(partition: TopicPartition): Unit = {}
Review Comment:
Done.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1863901810
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -609,6 +610,12 @@ private SharePartition getOrCreateSharePartition(SharePartitionKey sharePartitio
k -> {
long start = time.hiResClockMs();
int leaderEpoch = ShareFetchUtils.leaderEpoch(replicaManager, sharePartitionKey.topicIdPartition().topicPartition());
+ // Attach listener to Partition which shall invoke partition change handlers.
+ // However, as there could be multiple share partitions (per group name) for a single topic-partition,
+ // hence create separate listeners per share partition which holds the share partition key
+ // to identify the respective share partition.
+ replicaManager.maybeAddListener(sharePartitionKey.topicIdPartition().topicPartition(),
Review Comment:
Do we have a mechanism to remove unused listeners? Since a listener is created for each group, the list of listeners in a partition could grow significantly over time. Especially considering that the listener includes a hot method `onHighWatermarkUpdated`
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2474731772
> @apoorvmittal10 what is the purpose of this listener? Is is so that SPM can clean up its in-memory state when it is no longer the leader for a partition?
Yes, that's the purpose.
> If that's the main use case, I think we should consider tying into the metadata system directly rather than coupling ourselves to Partition.
So as far as I see the code there exists 1 approach taken by coordinators: in replica manager where we have a method `becomeLeaderOrFollower` where we pass `onLeadershipChange` method. The `onLeadershipChange` method triggers `groupCoordinator` and `txnCordinators`, `onElection` and `onResignation` methods. The 2) approach is by invoking the listeners on partition change.
I am not sure if we do get leadership change notifications from metadata system. Not sure if we could have some kind of leaderEpochChange listener to see the partition epoch bump and do some actions. But I found the listeners approach much sensible.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "mumrah (via GitHub)" <gi...@apache.org>.
mumrah commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2474632453
@apoorvmittal10 what is the purpose of this listener? Is is so that SPM can clean up its in-memory state when it is no longer the leader for a partition?
If that's the main use case, I think we should consider tying into the metadata system directly rather than coupling ourselves to Partition.
@dajac, since its looks like you added it, maybe you can comment on the intended usage of PartitionListener?
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "junrao (via GitHub)" <gi...@apache.org>.
junrao commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1847129644
##########
core/src/main/scala/kafka/cluster/Partition.scala:
##########
@@ -82,6 +82,11 @@ trait PartitionListener {
* that the partition was deleted but only that this broker does not host a replica of it any more.
*/
def onDeleted(partition: TopicPartition): Unit = {}
+
+ /**
+ * Called when the Partition on this broker is marked as follower.
+ */
+ def onFollower(partition: TopicPartition): Unit = {}
Review Comment:
If a broker switches from a follower to a follower, there is no need to remove the share partition. It's only when a broker is no longer a leader, a share partition should be removed. So we could add a more specific listener.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2513181044
@junrao @chia7712 Thanks for reviewing the changes, I have addressed the comments.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "AndrewJSchofield (via GitHub)" <gi...@apache.org>.
AndrewJSchofield commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1867999107
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +670,78 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey, partitionCacheMap, replicaManager);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private static void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> map, ReplicaManager replicaManager) {
+ SharePartition sharePartition = map.remove(sharePartitionKey);
Review Comment:
Nit: A line break before the ) would separate the argument declarations from the statements more clearly.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2515825189
> @apoorvmittal10 The listeners utilize a `CopyOnWriteArrayList`, meaning that both "add" and "remove" operations can trigger array copies. I am concerned that this could lead to performance issues when a partition becomes a follower, as it may result in `n * (n + 1) / 2` copies when listeners are removed one by one.
@chia7712 Yes, you are right it's `CopyOnWriteArrayList`. I considered that initially and my thought process was that `listeners (CopyOnWriteArrayList)` is per Partition. Hence a partition listener can contain listeners at most to the number of share groups on a broker. Yes, if the number of groups in really huge then it might cause a bit delay. Do you think that is a concern?
If yes, then I can have a single SharePartitionListener which keeps a mapping of `topicPartitions to set of sharePartitionKeys`.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1868506381
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +670,78 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey, partitionCacheMap, replicaManager);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private static void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> map, ReplicaManager replicaManager) {
+ SharePartition sharePartition = map.remove(sharePartitionKey);
+ if (sharePartition != null) {
+ sharePartition.markFenced();
+ replicaManager.removeListener(sharePartitionKey.topicIdPartition().topicPartition(), sharePartition.listener());
+ }
+ }
+
+ /**
+ * The SharePartitionListener is used to listen for partition events. The share partition is associated with
+ * the topic-partition, we need to handle the partition events for the share partition.
+ * <p>
+ * The partition cache map stores share partitions against share partition key which comprises
+ * group and topic-partition. Instead of maintaining a separate map for topic-partition to share partitions,
+ * we can maintain the share partition key in the listener and create a new listener for each share partition.
+ */
+ static class SharePartitionListener implements PartitionListener {
Review Comment:
That's what I was initially thinking to do but it made writing tests hard as now I have to create an instance of SharePartitionManager and invoke calls to create SharePartition as well which will provide me the listener. Hence thought to make it static itself.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2477329009
Thanks @mumrah, works well with me. I was more thinking to tie it up with Partition listener initially but can work either way. @dajac @junrao What do you think?
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "junrao (via GitHub)" <gi...@apache.org>.
junrao merged PR #17796:
URL: https://github.com/apache/kafka/pull/17796
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "junrao (via GitHub)" <gi...@apache.org>.
junrao commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1868150531
##########
core/src/test/scala/unit/kafka/server/ReplicaManagerTest.scala:
##########
@@ -6798,6 +6798,105 @@ class ReplicaManagerTest {
assertEquals(Double.NaN, maxMetric.metricValue)
}
+ @Test
+ def testBecomeFollowerInvokeOnBecomingFollowerListener(): Unit = {
+ val localId = 1
+ val topicPartition = new TopicPartition("foo", 0)
+ val replicaManager = setupReplicaManagerWithMockedPurgatories(new MockTimer(time), localId)
+ // Attach listener to partition.
+ val offsetCheckpoints = new LazyOffsetCheckpoints(replicaManager.highWatermarkCheckpoints.asJava)
+ replicaManager.createPartition(topicPartition).createLogIfNotExists(isNew = false, isFutureReplica = false, offsetCheckpoints, None)
+ val listener = new MockPartitionListener
+ assertTrue(replicaManager.maybeAddListener(topicPartition, listener))
+ listener.verify()
+
+ try {
+ // Make the local replica the leader
+ val leaderTopicsDelta = topicsCreateDelta(localId, true)
+ val leaderMetadataImage = imageFromTopics(leaderTopicsDelta.apply())
+
+ replicaManager.applyDelta(leaderTopicsDelta, leaderMetadataImage)
+
+ // Check the state of that partition and fetcher
+ val HostedPartition.Online(leaderPartition) = replicaManager.getPartition(topicPartition)
+ assertTrue(leaderPartition.isLeader)
+ assertEquals(0, leaderPartition.getLeaderEpoch)
+ // On becoming follower listener should not be invoked yet.
+ listener.verify()
+
+ // Change the local replica to follower
+ val followerTopicsDelta = topicsChangeDelta(leaderMetadataImage.topics(), localId, false)
+ val followerMetadataImage = imageFromTopics(followerTopicsDelta.apply())
+ replicaManager.applyDelta(followerTopicsDelta, followerMetadataImage)
+
+ // On becoming follower listener should be invoked.
+ listener.verify(expectedFollower = true)
+
+ // Check the state of that partition.
+ val HostedPartition.Online(followerPartition) = replicaManager.getPartition(topicPartition)
+ assertFalse(followerPartition.isLeader)
+ assertEquals(1, followerPartition.getLeaderEpoch)
+ } finally {
+ replicaManager.shutdown(checkpointHW = false)
+ }
+ }
+
+ @Test
+ def testBecomeFollowerNotInvokeOnBecomingFollowerListenerOnZk(): Unit = {
Review Comment:
No need to add this test since `replicaManager.becomeLeaderOrFollower` will be eventually removed.
##########
core/src/test/java/kafka/server/share/SharePartitionManagerTest.java:
##########
@@ -2496,6 +2498,98 @@ public void testReplicaManagerFetchMultipleSharePartitionsException() {
assertTrue(partitionCacheMap.isEmpty());
}
+ @Test
+ public void testListenerRegistration() {
+ String groupId = "grp";
+ Uuid memberId = Uuid.randomUuid();
+
+ TopicIdPartition tp0 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0));
+ TopicIdPartition tp1 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("bar", 0));
+ Map<TopicIdPartition, Integer> partitionMaxBytes = new HashMap<>();
+ partitionMaxBytes.put(tp0, PARTITION_MAX_BYTES);
+ partitionMaxBytes.put(tp1, PARTITION_MAX_BYTES);
+
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+ Partition partition = mockPartition();
+ when(mockReplicaManager.getPartitionOrException(Mockito.any())).thenReturn(partition);
+
+ SharePartitionManager sharePartitionManager = SharePartitionManagerBuilder.builder()
+ .withReplicaManager(mockReplicaManager)
+ .withTimer(mockTimer)
+ .build();
+
+ sharePartitionManager.fetchMessages(groupId, memberId.toString(), FETCH_PARAMS, partitionMaxBytes);
+ // Validate that the listener is registered.
+ verify(mockReplicaManager, times(2)).maybeAddListener(any(), any());
+ }
+
+ @Test
+ public void testSharePartitionListenerOnFailed() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onFailed);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnDeleted() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onDeleted);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnBecomingFollower() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onBecomingFollower);
+ }
+
+ private void testSharePartitionListener(
+ SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> partitionCacheMap,
+ ReplicaManager mockReplicaManager,
+ Consumer<TopicPartition> listenerConsumer
+ ) {
+ // Add another share partition to the cache.
+ TopicPartition tp = new TopicPartition("foo", 1);
+ TopicIdPartition tpId = new TopicIdPartition(Uuid.randomUuid(), tp);
+ SharePartitionKey spk = new SharePartitionKey("grp", tpId);
+
+ SharePartition sp0 = mock(SharePartition.class);
+ SharePartition sp1 = mock(SharePartition.class);
+ partitionCacheMap.put(sharePartitionKey, sp0);
+ partitionCacheMap.put(spk, sp1);
+
+ // Invoke listener for first share partition.
+ listenerConsumer.accept(sharePartitionKey.topicIdPartition().topicPartition());
+
+ // Validate that the share partition is removed from the cache.
+ assertEquals(1, partitionCacheMap.size());
+ assertFalse(partitionCacheMap.containsKey(sharePartitionKey));
+ verify(sp0, times(1)).markFenced();
+ verify(mockReplicaManager, times(1)).removeListener(any(), any());
+
+ // Invoke listener for second share partition.
+ listenerConsumer.accept(tp);
+ // The second share partition should not be removed as the listener is attached to single topic partition.
Review Comment:
attached to single topic partition => attached to a different topic partition ?
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +670,78 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey, partitionCacheMap, replicaManager);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private static void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> map, ReplicaManager replicaManager) {
+ SharePartition sharePartition = map.remove(sharePartitionKey);
+ if (sharePartition != null) {
+ sharePartition.markFenced();
+ replicaManager.removeListener(sharePartitionKey.topicIdPartition().topicPartition(), sharePartition.listener());
+ }
+ }
+
+ /**
+ * The SharePartitionListener is used to listen for partition events. The share partition is associated with
+ * the topic-partition, we need to handle the partition events for the share partition.
+ * <p>
+ * The partition cache map stores share partitions against share partition key which comprises
+ * group and topic-partition. Instead of maintaining a separate map for topic-partition to share partitions,
+ * we can maintain the share partition key in the listener and create a new listener for each share partition.
+ */
+ static class SharePartitionListener implements PartitionListener {
Review Comment:
Would it be better to make this a private class? This way, we don't need to pass in the states in SharePartitionManager.
##########
core/src/test/java/kafka/server/share/SharePartitionManagerTest.java:
##########
@@ -2496,6 +2498,98 @@ public void testReplicaManagerFetchMultipleSharePartitionsException() {
assertTrue(partitionCacheMap.isEmpty());
}
+ @Test
+ public void testListenerRegistration() {
+ String groupId = "grp";
+ Uuid memberId = Uuid.randomUuid();
+
+ TopicIdPartition tp0 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0));
+ TopicIdPartition tp1 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("bar", 0));
+ Map<TopicIdPartition, Integer> partitionMaxBytes = new HashMap<>();
+ partitionMaxBytes.put(tp0, PARTITION_MAX_BYTES);
+ partitionMaxBytes.put(tp1, PARTITION_MAX_BYTES);
+
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+ Partition partition = mockPartition();
+ when(mockReplicaManager.getPartitionOrException(Mockito.any())).thenReturn(partition);
+
+ SharePartitionManager sharePartitionManager = SharePartitionManagerBuilder.builder()
+ .withReplicaManager(mockReplicaManager)
+ .withTimer(mockTimer)
+ .build();
+
+ sharePartitionManager.fetchMessages(groupId, memberId.toString(), FETCH_PARAMS, partitionMaxBytes);
+ // Validate that the listener is registered.
+ verify(mockReplicaManager, times(2)).maybeAddListener(any(), any());
+ }
+
+ @Test
+ public void testSharePartitionListenerOnFailed() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onFailed);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnDeleted() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onDeleted);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnBecomingFollower() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onBecomingFollower);
+ }
+
+ private void testSharePartitionListener(
+ SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> partitionCacheMap,
+ ReplicaManager mockReplicaManager,
+ Consumer<TopicPartition> listenerConsumer
+ ) {
+ // Add another share partition to the cache.
+ TopicPartition tp = new TopicPartition("foo", 1);
+ TopicIdPartition tpId = new TopicIdPartition(Uuid.randomUuid(), tp);
+ SharePartitionKey spk = new SharePartitionKey("grp", tpId);
+
+ SharePartition sp0 = mock(SharePartition.class);
+ SharePartition sp1 = mock(SharePartition.class);
+ partitionCacheMap.put(sharePartitionKey, sp0);
+ partitionCacheMap.put(spk, sp1);
+
+ // Invoke listener for first share partition.
+ listenerConsumer.accept(sharePartitionKey.topicIdPartition().topicPartition());
+
+ // Validate that the share partition is removed from the cache.
+ assertEquals(1, partitionCacheMap.size());
+ assertFalse(partitionCacheMap.containsKey(sharePartitionKey));
+ verify(sp0, times(1)).markFenced();
+ verify(mockReplicaManager, times(1)).removeListener(any(), any());
+
+ // Invoke listener for second share partition.
Review Comment:
second share partition => non-matching share partition?
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +670,78 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey, partitionCacheMap, replicaManager);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private static void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> map, ReplicaManager replicaManager) {
+ SharePartition sharePartition = map.remove(sharePartitionKey);
+ if (sharePartition != null) {
+ sharePartition.markFenced();
+ replicaManager.removeListener(sharePartitionKey.topicIdPartition().topicPartition(), sharePartition.listener());
+ }
+ }
+
+ /**
+ * The SharePartitionListener is used to listen for partition events. The share partition is associated with
+ * the topic-partition, we need to handle the partition events for the share partition.
+ * <p>
+ * The partition cache map stores share partitions against share partition key which comprises
+ * group and topic-partition. Instead of maintaining a separate map for topic-partition to share partitions,
+ * we can maintain the share partition key in the listener and create a new listener for each share partition.
+ */
+ static class SharePartitionListener implements PartitionListener {
+
+ private final SharePartitionKey sharePartitionKey;
+ private final ReplicaManager replicaManager;
+ private final Map<SharePartitionKey, SharePartition> partitionCacheMap;
+
+ SharePartitionListener(
+ SharePartitionKey sharePartitionKey,
+ ReplicaManager replicaManager,
+ Map<SharePartitionKey, SharePartition> partitionCacheMap
+ ) {
+ this.sharePartitionKey = sharePartitionKey;
+ this.replicaManager = replicaManager;
+ this.partitionCacheMap = partitionCacheMap;
+ }
+
+ @Override
+ public void onFailed(TopicPartition topicPartition) {
+ log.debug("The share partition failed listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ @Override
+ public void onDeleted(TopicPartition topicPartition) {
+ log.debug("The share partition delete listener is invoked for the topic-partition: {}, share-partition: {}",
+ topicPartition, sharePartitionKey);
+ onUpdate(topicPartition);
+ }
+
+ @Override
+ public void onBecomingFollower(TopicPartition topicPartition) {
+ log.debug("The share partition leader change listener is invoked for the topic-partition: {}, share-partition: {}",
Review Comment:
leader change => becoming follower ?
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on PR #17796:
URL: https://github.com/apache/kafka/pull/17796#issuecomment-2515334084
@apoorvmittal10 The listeners utilize a `CopyOnWriteArrayList`, meaning that both "add" and "remove" operations can trigger array copies. I am concerned that this could lead to performance issues when a partition becomes a follower, as it may result in `n * (n + 1) / 2` copies when listeners are removed one by one.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1868505586
##########
core/src/test/java/kafka/server/share/SharePartitionManagerTest.java:
##########
@@ -2496,6 +2498,98 @@ public void testReplicaManagerFetchMultipleSharePartitionsException() {
assertTrue(partitionCacheMap.isEmpty());
}
+ @Test
+ public void testListenerRegistration() {
+ String groupId = "grp";
+ Uuid memberId = Uuid.randomUuid();
+
+ TopicIdPartition tp0 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0));
+ TopicIdPartition tp1 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("bar", 0));
+ Map<TopicIdPartition, Integer> partitionMaxBytes = new HashMap<>();
+ partitionMaxBytes.put(tp0, PARTITION_MAX_BYTES);
+ partitionMaxBytes.put(tp1, PARTITION_MAX_BYTES);
+
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+ Partition partition = mockPartition();
+ when(mockReplicaManager.getPartitionOrException(Mockito.any())).thenReturn(partition);
+
+ SharePartitionManager sharePartitionManager = SharePartitionManagerBuilder.builder()
+ .withReplicaManager(mockReplicaManager)
+ .withTimer(mockTimer)
+ .build();
+
+ sharePartitionManager.fetchMessages(groupId, memberId.toString(), FETCH_PARAMS, partitionMaxBytes);
+ // Validate that the listener is registered.
+ verify(mockReplicaManager, times(2)).maybeAddListener(any(), any());
+ }
+
+ @Test
+ public void testSharePartitionListenerOnFailed() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onFailed);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnDeleted() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onDeleted);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnBecomingFollower() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onBecomingFollower);
+ }
+
+ private void testSharePartitionListener(
+ SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> partitionCacheMap,
+ ReplicaManager mockReplicaManager,
+ Consumer<TopicPartition> listenerConsumer
+ ) {
+ // Add another share partition to the cache.
+ TopicPartition tp = new TopicPartition("foo", 1);
+ TopicIdPartition tpId = new TopicIdPartition(Uuid.randomUuid(), tp);
+ SharePartitionKey spk = new SharePartitionKey("grp", tpId);
+
+ SharePartition sp0 = mock(SharePartition.class);
+ SharePartition sp1 = mock(SharePartition.class);
+ partitionCacheMap.put(sharePartitionKey, sp0);
+ partitionCacheMap.put(spk, sp1);
+
+ // Invoke listener for first share partition.
+ listenerConsumer.accept(sharePartitionKey.topicIdPartition().topicPartition());
+
+ // Validate that the share partition is removed from the cache.
+ assertEquals(1, partitionCacheMap.size());
+ assertFalse(partitionCacheMap.containsKey(sharePartitionKey));
+ verify(sp0, times(1)).markFenced();
+ verify(mockReplicaManager, times(1)).removeListener(any(), any());
+
+ // Invoke listener for second share partition.
Review Comment:
Done.
##########
core/src/test/java/kafka/server/share/SharePartitionManagerTest.java:
##########
@@ -2496,6 +2498,98 @@ public void testReplicaManagerFetchMultipleSharePartitionsException() {
assertTrue(partitionCacheMap.isEmpty());
}
+ @Test
+ public void testListenerRegistration() {
+ String groupId = "grp";
+ Uuid memberId = Uuid.randomUuid();
+
+ TopicIdPartition tp0 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0));
+ TopicIdPartition tp1 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("bar", 0));
+ Map<TopicIdPartition, Integer> partitionMaxBytes = new HashMap<>();
+ partitionMaxBytes.put(tp0, PARTITION_MAX_BYTES);
+ partitionMaxBytes.put(tp1, PARTITION_MAX_BYTES);
+
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+ Partition partition = mockPartition();
+ when(mockReplicaManager.getPartitionOrException(Mockito.any())).thenReturn(partition);
+
+ SharePartitionManager sharePartitionManager = SharePartitionManagerBuilder.builder()
+ .withReplicaManager(mockReplicaManager)
+ .withTimer(mockTimer)
+ .build();
+
+ sharePartitionManager.fetchMessages(groupId, memberId.toString(), FETCH_PARAMS, partitionMaxBytes);
+ // Validate that the listener is registered.
+ verify(mockReplicaManager, times(2)).maybeAddListener(any(), any());
+ }
+
+ @Test
+ public void testSharePartitionListenerOnFailed() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onFailed);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnDeleted() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onDeleted);
+ }
+
+ @Test
+ public void testSharePartitionListenerOnBecomingFollower() {
+ SharePartitionKey sharePartitionKey = new SharePartitionKey("grp",
+ new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)));
+ Map<SharePartitionKey, SharePartition> partitionCacheMap = new HashMap<>();
+ ReplicaManager mockReplicaManager = mock(ReplicaManager.class);
+
+ SharePartitionListener partitionListener = new SharePartitionListener(sharePartitionKey, mockReplicaManager, partitionCacheMap);
+ testSharePartitionListener(sharePartitionKey, partitionCacheMap, mockReplicaManager, partitionListener::onBecomingFollower);
+ }
+
+ private void testSharePartitionListener(
+ SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> partitionCacheMap,
+ ReplicaManager mockReplicaManager,
+ Consumer<TopicPartition> listenerConsumer
+ ) {
+ // Add another share partition to the cache.
+ TopicPartition tp = new TopicPartition("foo", 1);
+ TopicIdPartition tpId = new TopicIdPartition(Uuid.randomUuid(), tp);
+ SharePartitionKey spk = new SharePartitionKey("grp", tpId);
+
+ SharePartition sp0 = mock(SharePartition.class);
+ SharePartition sp1 = mock(SharePartition.class);
+ partitionCacheMap.put(sharePartitionKey, sp0);
+ partitionCacheMap.put(spk, sp1);
+
+ // Invoke listener for first share partition.
+ listenerConsumer.accept(sharePartitionKey.topicIdPartition().topicPartition());
+
+ // Validate that the share partition is removed from the cache.
+ assertEquals(1, partitionCacheMap.size());
+ assertFalse(partitionCacheMap.containsKey(sharePartitionKey));
+ verify(sp0, times(1)).markFenced();
+ verify(mockReplicaManager, times(1)).removeListener(any(), any());
+
+ // Invoke listener for second share partition.
+ listenerConsumer.accept(tp);
+ // The second share partition should not be removed as the listener is attached to single topic partition.
Review Comment:
Done.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1868507444
##########
core/src/main/java/kafka/server/share/SharePartitionManager.java:
##########
@@ -665,17 +670,78 @@ public void handleFencedSharePartitionException(
// The share partition is fenced hence remove the partition from map and let the client retry.
// But surface the error to the client so client might take some action i.e. re-fetch
// the metadata and retry the fetch on new leader.
- SharePartition sharePartition = partitionCacheMap.remove(sharePartitionKey);
- if (sharePartition != null) {
- sharePartition.markFenced();
- }
+ removeSharePartitionFromCache(sharePartitionKey, partitionCacheMap, replicaManager);
}
}
private SharePartitionKey sharePartitionKey(String groupId, TopicIdPartition topicIdPartition) {
return new SharePartitionKey(groupId, topicIdPartition);
}
+ private static void removeSharePartitionFromCache(SharePartitionKey sharePartitionKey,
+ Map<SharePartitionKey, SharePartition> map, ReplicaManager replicaManager) {
+ SharePartition sharePartition = map.remove(sharePartitionKey);
Review Comment:
Done.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-17783: Adding listeners to remove share partition on partition changes [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #17796:
URL: https://github.com/apache/kafka/pull/17796#discussion_r1866757543
##########
core/src/test/scala/unit/kafka/server/ReplicaManagerTest.scala:
##########
@@ -6798,6 +6798,62 @@ class ReplicaManagerTest {
assertEquals(Double.NaN, maxMetric.metricValue)
}
+ @Test
+ def testBecomeFollowerInvokeLeaderToFollowerListener(): Unit = {
Review Comment:
Agree, removed and added corrected tests to verify.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org