hachikuji commented on a change in pull request #9441:
URL: https://github.com/apache/kafka/pull/9441#discussion_r607374397



##########
File path: core/src/main/scala/kafka/coordinator/group/GroupCoordinator.scala
##########
@@ -905,19 +907,33 @@ class GroupCoordinator(val brokerId: Int,
    *
    * @param offsetTopicPartitionId The partition we are now leading
    */
-  def onElection(offsetTopicPartitionId: Int): Unit = {
-    info(s"Elected as the group coordinator for partition 
$offsetTopicPartitionId")
-    groupManager.scheduleLoadGroupAndOffsets(offsetTopicPartitionId, 
onGroupLoaded)
+  def onElection(offsetTopicPartitionId: Int, coordinatorEpoch: Int): Unit = {
+    val currentEpoch = epochForPartitionId.get(offsetTopicPartitionId)
+    if (currentEpoch.forall(currentEpoch => coordinatorEpoch > currentEpoch)) {
+      info(s"Elected as the group coordinator for partition 
$offsetTopicPartitionId in epoch $coordinatorEpoch")
+      groupManager.scheduleLoadGroupAndOffsets(offsetTopicPartitionId, 
onGroupLoaded)
+      epochForPartitionId.put(offsetTopicPartitionId, coordinatorEpoch)
+    } else {
+      warn(s"Ignored election as group coordinator for partition 
$offsetTopicPartitionId " +
+        s"in epoch $coordinatorEpoch since current epoch is $currentEpoch")
+    }
   }
 
   /**
    * Unload cached state for the given partition and stop handling requests 
for groups which map to it.
    *
    * @param offsetTopicPartitionId The partition we are no longer leading
    */
-  def onResignation(offsetTopicPartitionId: Int): Unit = {
-    info(s"Resigned as the group coordinator for partition 
$offsetTopicPartitionId")
-    groupManager.removeGroupsForPartition(offsetTopicPartitionId, 
onGroupUnloaded)
+  def onResignation(offsetTopicPartitionId: Int, coordinatorEpoch: 
Option[Int]): Unit = {
+    val currentEpoch = epochForPartitionId.get(offsetTopicPartitionId)
+    if (currentEpoch.forall(currentEpoch => currentEpoch <= 
coordinatorEpoch.getOrElse(Int.MaxValue))) {
+      info(s"Resigned as the group coordinator for partition 
$offsetTopicPartitionId in epoch $coordinatorEpoch")
+      groupManager.removeGroupsForPartition(offsetTopicPartitionId, 
onGroupUnloaded)
+      epochForPartitionId.remove(offsetTopicPartitionId)

Review comment:
       Hmm.. Why remove the epoch after resignation? It seems like it would be 
useful to keep tracking it. Maybe it's useful to distinguish the case where the 
replica is to be deleted?

##########
File path: core/src/main/scala/kafka/coordinator/group/GroupCoordinator.scala
##########
@@ -87,6 +87,8 @@ class GroupCoordinator(val brokerId: Int,
 
   private val isActive = new AtomicBoolean(false)
 
+  val epochForPartitionId = mutable.Map[Int, Int]()

Review comment:
       Does this need to be a concurrent collection? It does not look like we 
can count on a lock protecting `onElection` and `onResignation`.




-- 
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to