jeffkbkim commented on code in PR #13639: URL: https://github.com/apache/kafka/pull/13639#discussion_r1204467377
########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. + */ + private final TimelineHashMap<String, Integer> serverAssignors; + + /** + * The number of subscribers per topic. + */ + private final TimelineHashMap<String, Integer> subscribedTopicNames; + + /** + * The metadata of the subscribed topics. Review Comment: nit: The metadata associated with each subscribed topic name ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. + */ + private final TimelineHashMap<String, Integer> serverAssignors; + + /** + * The number of subscribers per topic. + */ + private final TimelineHashMap<String, Integer> subscribedTopicNames; + + /** + * The metadata of the subscribed topics. + */ + private final TimelineHashMap<String, TopicMetadata> subscribedTopicMetadata; + + /** + * The assignment epoch. An assignment epoch smaller than the group epoch means + * that a new assignment is required. The assignment epoch is updated when a new + * assignment is installed. + */ + private final TimelineInteger assignmentEpoch; + + /** + * The target assignment. Review Comment: nit: The target assignment per member ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. + */ + private final TimelineHashMap<String, Integer> serverAssignors; + + /** + * The number of subscribers per topic. + */ + private final TimelineHashMap<String, Integer> subscribedTopicNames; + + /** + * The metadata of the subscribed topics. + */ + private final TimelineHashMap<String, TopicMetadata> subscribedTopicMetadata; + + /** + * The assignment epoch. An assignment epoch smaller than the group epoch means + * that a new assignment is required. The assignment epoch is updated when a new + * assignment is installed. + */ + private final TimelineInteger assignmentEpoch; + + /** + * The target assignment. + */ + private final TimelineHashMap<String, Assignment> assignments; + + /** + * The current partition epoch maps each topic-partitions to their current epoch where + * the epoch is the epoch of their owners. When a member revokes a partition, it removes + * itself from this map. When a member gets a partition, it adds itself to this map. + */ + private final TimelineHashMap<Uuid, TimelineHashMap<Integer, Integer>> currentPartitionEpoch; + + public ConsumerGroup( + SnapshotRegistry snapshotRegistry, + String groupId + ) { + this.snapshotRegistry = Objects.requireNonNull(snapshotRegistry); + this.groupId = Objects.requireNonNull(groupId); + this.state = new TimelineObject<>(snapshotRegistry, ConsumerGroupState.EMPTY); + this.groupEpoch = new TimelineInteger(snapshotRegistry); + this.members = new TimelineHashMap<>(snapshotRegistry, 0); + this.serverAssignors = new TimelineHashMap<>(snapshotRegistry, 0); + this.subscribedTopicNames = new TimelineHashMap<>(snapshotRegistry, 0); + this.subscribedTopicMetadata = new TimelineHashMap<>(snapshotRegistry, 0); + this.assignmentEpoch = new TimelineInteger(snapshotRegistry); + this.assignments = new TimelineHashMap<>(snapshotRegistry, 0); + this.currentPartitionEpoch = new TimelineHashMap<>(snapshotRegistry, 0); + } + + /** + * The type of this group. + * + * @return The group type (Consumer). + */ + @Override + public GroupType type() { + return GroupType.CONSUMER; + } + + /** + * The state of this group. + * + * @return The current state as a String. + */ + @Override + public String stateAsString() { + return state.get().toString(); + } + + /** + * The group id. + * + * @return The group id. + */ + @Override + public String groupId() { + return groupId; + } + + /** + * The state of this group. + * + * @return The current state. + */ + public ConsumerGroupState state() { + return state.get(); + } + + /** + * Returns the current group epoch. + * + * @return The group epoch. + */ + public int groupEpoch() { + return groupEpoch.get(); + } + + /** + * Sets the group epoch. + * + * @param groupEpoch The new group epoch. + */ + public void setGroupEpoch(int groupEpoch) { + this.groupEpoch.set(groupEpoch); + maybeUpdateGroupState(); + } + + /** + * Returns the current assignment epoch. + * + * @return The current assignment epoch. + */ + public int assignmentEpoch() { + return assignmentEpoch.get(); + } + + /** + * Sets the assignment epoch. + * + * @param assignmentEpoch The new assignment epoch. + */ + public void setAssignmentEpoch(int assignmentEpoch) { + this.assignmentEpoch.set(assignmentEpoch); + maybeUpdateGroupState(); + } + + /** + * Gets or creates a member. + * + * @param memberId The member id. + * @param createIfNotExists Booleans indicating whether the member must be + * created if it does not exist. + * + * @return A ConsumerGroupMember. + */ + public ConsumerGroupMember getOrMaybeCreateMember( + String memberId, + boolean createIfNotExists + ) { + ConsumerGroupMember member = members.get(memberId); + if (member == null) { + if (!createIfNotExists) { + throw new UnknownMemberIdException(String.format("Member %s is not a member of group %s.", + memberId, groupId)); + } + member = new ConsumerGroupMember.Builder(memberId).build(); + members.put(memberId, member); + } + + return member; + } + + /** + * Updates the member. + * + * @param newMember The new member state. + */ + public void updateMember(ConsumerGroupMember newMember) { + if (newMember == null) { + throw new IllegalArgumentException("newMember cannot be null."); + } + ConsumerGroupMember oldMember = members.put(newMember.memberId(), newMember); + maybeUpdateSubscribedTopicNames(oldMember, newMember); + maybeUpdateServerAssignors(oldMember, newMember); + maybeUpdatePartitionEpoch(oldMember, newMember); + maybeUpdateGroupState(); + } + + /** + * Remove the member from the group. + * + * @param memberId The member id to remove. + */ + public void removeMember(String memberId) { + ConsumerGroupMember member = members.remove(memberId); + maybeRemovePartitionEpoch(member); + maybeUpdateGroupState(); + } + + /** + * Returns true if the member exists. + * + * @param memberId The member id. + * + * @return A boolean indicating whether the member exists or not. + */ + public boolean hasMember(String memberId) { + return members.containsKey(memberId); + } + + /** + * Returns the number of members in the group. + * + * @return The number of members. + */ + public int numMembers() { + return members.size(); + } + + /** + * Returns the members keyed by their id. + * + * @return An immutable Map containing all the members. + */ + public Map<String, ConsumerGroupMember> members() { + return Collections.unmodifiableMap(members); + } + + /** + * Returns the current target assignment of the member. Review Comment: nit: Returns the existing ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. Review Comment: nit: The number of members supporting each server assignor name. is more readable to me ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. + */ + private final TimelineHashMap<String, Integer> serverAssignors; + + /** + * The number of subscribers per topic. + */ + private final TimelineHashMap<String, Integer> subscribedTopicNames; + + /** + * The metadata of the subscribed topics. + */ + private final TimelineHashMap<String, TopicMetadata> subscribedTopicMetadata; + + /** + * The assignment epoch. An assignment epoch smaller than the group epoch means + * that a new assignment is required. The assignment epoch is updated when a new + * assignment is installed. + */ + private final TimelineInteger assignmentEpoch; + + /** + * The target assignment. + */ + private final TimelineHashMap<String, Assignment> assignments; + + /** + * The current partition epoch maps each topic-partitions to their current epoch where + * the epoch is the epoch of their owners. When a member revokes a partition, it removes + * itself from this map. When a member gets a partition, it adds itself to this map. Review Comment: nit: it removes its partition epochs from this map. When a member gets a partition, it adds the epoch to this map. ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. + */ + private final TimelineHashMap<String, Integer> serverAssignors; + + /** + * The number of subscribers per topic. + */ + private final TimelineHashMap<String, Integer> subscribedTopicNames; + + /** + * The metadata of the subscribed topics. + */ + private final TimelineHashMap<String, TopicMetadata> subscribedTopicMetadata; + + /** + * The assignment epoch. An assignment epoch smaller than the group epoch means + * that a new assignment is required. The assignment epoch is updated when a new + * assignment is installed. + */ + private final TimelineInteger assignmentEpoch; + + /** + * The target assignment. + */ + private final TimelineHashMap<String, Assignment> assignments; + + /** + * The current partition epoch maps each topic-partitions to their current epoch where + * the epoch is the epoch of their owners. When a member revokes a partition, it removes + * itself from this map. When a member gets a partition, it adds itself to this map. + */ + private final TimelineHashMap<Uuid, TimelineHashMap<Integer, Integer>> currentPartitionEpoch; + + public ConsumerGroup( + SnapshotRegistry snapshotRegistry, + String groupId + ) { + this.snapshotRegistry = Objects.requireNonNull(snapshotRegistry); + this.groupId = Objects.requireNonNull(groupId); + this.state = new TimelineObject<>(snapshotRegistry, ConsumerGroupState.EMPTY); + this.groupEpoch = new TimelineInteger(snapshotRegistry); + this.members = new TimelineHashMap<>(snapshotRegistry, 0); + this.serverAssignors = new TimelineHashMap<>(snapshotRegistry, 0); + this.subscribedTopicNames = new TimelineHashMap<>(snapshotRegistry, 0); + this.subscribedTopicMetadata = new TimelineHashMap<>(snapshotRegistry, 0); + this.assignmentEpoch = new TimelineInteger(snapshotRegistry); + this.assignments = new TimelineHashMap<>(snapshotRegistry, 0); + this.currentPartitionEpoch = new TimelineHashMap<>(snapshotRegistry, 0); + } + + /** + * The type of this group. + * + * @return The group type (Consumer). + */ + @Override + public GroupType type() { + return GroupType.CONSUMER; + } + + /** + * The state of this group. + * + * @return The current state as a String. + */ + @Override + public String stateAsString() { + return state.get().toString(); + } + + /** + * The group id. + * + * @return The group id. + */ + @Override + public String groupId() { + return groupId; + } + + /** + * The state of this group. + * + * @return The current state. + */ + public ConsumerGroupState state() { + return state.get(); + } + + /** + * Returns the current group epoch. + * + * @return The group epoch. + */ + public int groupEpoch() { + return groupEpoch.get(); + } + + /** + * Sets the group epoch. + * + * @param groupEpoch The new group epoch. + */ + public void setGroupEpoch(int groupEpoch) { + this.groupEpoch.set(groupEpoch); + maybeUpdateGroupState(); + } + + /** + * Returns the current assignment epoch. + * + * @return The current assignment epoch. + */ + public int assignmentEpoch() { + return assignmentEpoch.get(); + } + + /** + * Sets the assignment epoch. + * + * @param assignmentEpoch The new assignment epoch. + */ + public void setAssignmentEpoch(int assignmentEpoch) { + this.assignmentEpoch.set(assignmentEpoch); + maybeUpdateGroupState(); + } + + /** + * Gets or creates a member. + * + * @param memberId The member id. + * @param createIfNotExists Booleans indicating whether the member must be + * created if it does not exist. + * + * @return A ConsumerGroupMember. + */ + public ConsumerGroupMember getOrMaybeCreateMember( + String memberId, + boolean createIfNotExists + ) { + ConsumerGroupMember member = members.get(memberId); + if (member == null) { + if (!createIfNotExists) { + throw new UnknownMemberIdException(String.format("Member %s is not a member of group %s.", + memberId, groupId)); + } + member = new ConsumerGroupMember.Builder(memberId).build(); + members.put(memberId, member); + } + + return member; + } + + /** + * Updates the member. + * + * @param newMember The new member state. + */ + public void updateMember(ConsumerGroupMember newMember) { + if (newMember == null) { + throw new IllegalArgumentException("newMember cannot be null."); + } + ConsumerGroupMember oldMember = members.put(newMember.memberId(), newMember); + maybeUpdateSubscribedTopicNames(oldMember, newMember); + maybeUpdateServerAssignors(oldMember, newMember); + maybeUpdatePartitionEpoch(oldMember, newMember); + maybeUpdateGroupState(); + } + + /** + * Remove the member from the group. + * + * @param memberId The member id to remove. + */ + public void removeMember(String memberId) { + ConsumerGroupMember member = members.remove(memberId); + maybeRemovePartitionEpoch(member); + maybeUpdateGroupState(); + } + + /** + * Returns true if the member exists. + * + * @param memberId The member id. + * + * @return A boolean indicating whether the member exists or not. + */ + public boolean hasMember(String memberId) { + return members.containsKey(memberId); + } + + /** + * Returns the number of members in the group. + * + * @return The number of members. + */ + public int numMembers() { + return members.size(); + } + + /** + * Returns the members keyed by their id. + * + * @return An immutable Map containing all the members. + */ + public Map<String, ConsumerGroupMember> members() { + return Collections.unmodifiableMap(members); + } + + /** + * Returns the current target assignment of the member. + * + * @return The ConsumerGroupMemberAssignment or an EMPTY one if it does not + * exist. + */ + public Assignment targetAssignment(String memberId) { + return assignments.getOrDefault(memberId, Assignment.EMPTY); + } + + /** + * Updates target assignment of a member. + * + * @param memberId The member id. + * @param newTargetAssignment The new target assignment. + */ + public void updateTargetAssignment(String memberId, Assignment newTargetAssignment) { + assignments.put(memberId, newTargetAssignment); + } + + /** + * Removes the target assignment of a member. + * + * @param memberId The member id. + */ + public void removeTargetAssignment(String memberId) { + assignments.remove(memberId); + } + + /** + * Returns the target assignments for the entire group keyed by member id. + * + * @return An immutable Map containing all the target assignments. + */ + public Map<String, Assignment> targetAssignments() { + return Collections.unmodifiableMap(assignments); + } + + /** + * Returns the current epoch of a partition or -1 if the partition + * does not have one. + * + * @param topicId The topic id. + * @param partitionId The partition id. + * + * @return The epoch or -1. + */ + public int currentPartitionEpoch( + Uuid topicId, int partitionId + ) { + Map<Integer, Integer> partitions = currentPartitionEpoch.get(topicId); + if (partitions == null) { + return -1; + } else { + return partitions.getOrDefault(partitionId, -1); + } + } + + /** + * Compute the preferred (server side) assignor for the group while + * taking into account the updated member. The computation relies + * on {{@link ConsumerGroup#serverAssignors}} persisted structure + * but it does not update it. + * + * @param oldMember The old member. + * @param newMember The new member. + * + * @return An Optional containing the preferred assignor. + */ + public Optional<String> preferredServerAssignor( + ConsumerGroupMember oldMember, + ConsumerGroupMember newMember + ) { + // Copy the current count and update it. + Map<String, Integer> counts = new HashMap<>(this.serverAssignors); + maybeUpdateServerAssignors(counts, oldMember, newMember); + + return counts.entrySet().stream() + .max(Map.Entry.comparingByValue()) + .map(Map.Entry::getKey); + } + + /** + * Returns the subscription metadata for all the topics whose + * members are subscribed to. + * + * @return An immutable Map containing the subscription metadata. + */ + public Map<String, TopicMetadata> subscriptionMetadata() { + return Collections.unmodifiableMap(subscribedTopicMetadata); + } + + /** + * Updates the subscription metadata. This replaces the previous one. + * + * @param subscriptionMetadata The new subscription metadata. + */ + public void setSubscriptionMetadata( + Map<String, TopicMetadata> subscriptionMetadata + ) { + this.subscribedTopicMetadata.clear(); + this.subscribedTopicMetadata.putAll(subscriptionMetadata); + } + + /** + * Computes the subscription metadata based on the current subscription and + * an updated member. + * + * @param oldMember The old member. + * @param newMember The new member. + * @param topicsImage The topic metadata. + * + * @return The new subscription metadata as an immutable Map. + */ + public Map<String, TopicMetadata> computeSubscriptionMetadata( + ConsumerGroupMember oldMember, + ConsumerGroupMember newMember, + TopicsImage topicsImage + ) { + // Copy and update the current subscriptions. + Map<String, Integer> subscribedTopicNames = new HashMap<>(this.subscribedTopicNames); + maybeUpdateSubscribedTopicNames(subscribedTopicNames, oldMember, newMember); + + // Create the topic metadata for each subscribed topic. + Map<String, TopicMetadata> newSubscriptionMetadata = new HashMap<>(subscribedTopicNames.size()); + subscribedTopicNames.forEach((topicName, count) -> { + TopicImage topicImage = topicsImage.getTopic(topicName); + if (topicImage != null) { Review Comment: do you think we should log something if topicImage == null? can both the topicsImage and/or subscribed topic names be outdated here? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. + */ + private final TimelineHashMap<String, Integer> serverAssignors; + + /** + * The number of subscribers per topic. + */ + private final TimelineHashMap<String, Integer> subscribedTopicNames; + + /** + * The metadata of the subscribed topics. + */ + private final TimelineHashMap<String, TopicMetadata> subscribedTopicMetadata; + + /** + * The assignment epoch. An assignment epoch smaller than the group epoch means + * that a new assignment is required. The assignment epoch is updated when a new + * assignment is installed. + */ + private final TimelineInteger assignmentEpoch; + + /** + * The target assignment. + */ + private final TimelineHashMap<String, Assignment> assignments; + + /** + * The current partition epoch maps each topic-partitions to their current epoch where + * the epoch is the epoch of their owners. When a member revokes a partition, it removes + * itself from this map. When a member gets a partition, it adds itself to this map. + */ + private final TimelineHashMap<Uuid, TimelineHashMap<Integer, Integer>> currentPartitionEpoch; + + public ConsumerGroup( + SnapshotRegistry snapshotRegistry, + String groupId + ) { + this.snapshotRegistry = Objects.requireNonNull(snapshotRegistry); + this.groupId = Objects.requireNonNull(groupId); + this.state = new TimelineObject<>(snapshotRegistry, ConsumerGroupState.EMPTY); + this.groupEpoch = new TimelineInteger(snapshotRegistry); + this.members = new TimelineHashMap<>(snapshotRegistry, 0); Review Comment: for this line and the following couple lines, does expected size of 0 create a hash table with size 1 as it's a power of 2? i'm confused because the expected size intuitively should not be 0. ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,633 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.coordinator.group.Group; +import org.apache.kafka.image.TopicImage; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.apache.kafka.timeline.TimelineHashMap; +import org.apache.kafka.timeline.TimelineInteger; +import org.apache.kafka.timeline.TimelineObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * A Consumer Group. All the metadata in this class are backed by + * records in the __consumer_offsets partitions. + */ +public class ConsumerGroup implements Group { + + public enum ConsumerGroupState { + EMPTY("empty"), + ASSIGNING("assigning"), + RECONCILING("reconciling"), + STABLE("stable"), + DEAD("dead"); + + private final String name; + + ConsumerGroupState(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + /** + * The snapshot registry. + */ + private final SnapshotRegistry snapshotRegistry; + + /** + * The group id. + */ + private final String groupId; + + /** + * The group state. + */ + private final TimelineObject<ConsumerGroupState> state; + + /** + * The group epoch. The epoch is incremented whenever the subscriptions + * are updated and it will trigger the computation of a new assignment + * for the group. + */ + private final TimelineInteger groupEpoch; + + /** + * The group members. + */ + private final TimelineHashMap<String, ConsumerGroupMember> members; + + /** + * The number of members per server assignor name. + */ + private final TimelineHashMap<String, Integer> serverAssignors; + + /** + * The number of subscribers per topic. + */ + private final TimelineHashMap<String, Integer> subscribedTopicNames; + + /** + * The metadata of the subscribed topics. + */ + private final TimelineHashMap<String, TopicMetadata> subscribedTopicMetadata; + + /** + * The assignment epoch. An assignment epoch smaller than the group epoch means + * that a new assignment is required. The assignment epoch is updated when a new + * assignment is installed. + */ + private final TimelineInteger assignmentEpoch; + + /** + * The target assignment. + */ + private final TimelineHashMap<String, Assignment> assignments; + + /** + * The current partition epoch maps each topic-partitions to their current epoch where + * the epoch is the epoch of their owners. When a member revokes a partition, it removes + * itself from this map. When a member gets a partition, it adds itself to this map. + */ + private final TimelineHashMap<Uuid, TimelineHashMap<Integer, Integer>> currentPartitionEpoch; + + public ConsumerGroup( + SnapshotRegistry snapshotRegistry, + String groupId + ) { + this.snapshotRegistry = Objects.requireNonNull(snapshotRegistry); + this.groupId = Objects.requireNonNull(groupId); + this.state = new TimelineObject<>(snapshotRegistry, ConsumerGroupState.EMPTY); + this.groupEpoch = new TimelineInteger(snapshotRegistry); + this.members = new TimelineHashMap<>(snapshotRegistry, 0); + this.serverAssignors = new TimelineHashMap<>(snapshotRegistry, 0); + this.subscribedTopicNames = new TimelineHashMap<>(snapshotRegistry, 0); + this.subscribedTopicMetadata = new TimelineHashMap<>(snapshotRegistry, 0); + this.assignmentEpoch = new TimelineInteger(snapshotRegistry); + this.assignments = new TimelineHashMap<>(snapshotRegistry, 0); + this.currentPartitionEpoch = new TimelineHashMap<>(snapshotRegistry, 0); + } + + /** + * The type of this group. + * + * @return The group type (Consumer). + */ + @Override + public GroupType type() { + return GroupType.CONSUMER; + } + + /** + * The state of this group. + * + * @return The current state as a String. + */ + @Override + public String stateAsString() { + return state.get().toString(); + } + + /** + * The group id. + * + * @return The group id. + */ + @Override + public String groupId() { + return groupId; + } + + /** + * The state of this group. + * + * @return The current state. + */ + public ConsumerGroupState state() { + return state.get(); + } + + /** + * Returns the current group epoch. Review Comment: I think justine or someone else may have mentioned in another PR but for getters, just having the `@return The current group epoch` makes more sense. i'm not sure if this line adds much value as well as for other getter methods ########## group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroupTest.java: ########## @@ -0,0 +1,500 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.consumer; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownMemberIdException; +import org.apache.kafka.common.utils.LogContext; +import org.apache.kafka.coordinator.group.GroupMetadataManagerTest; +import org.apache.kafka.image.TopicsImage; +import org.apache.kafka.timeline.SnapshotRegistry; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Optional; + +import static org.apache.kafka.common.utils.Utils.mkEntry; +import static org.apache.kafka.common.utils.Utils.mkMap; +import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkAssignment; +import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkTopicAssignment; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ConsumerGroupTest { + + private ConsumerGroup createConsumerGroup(String groupId) { + SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext()); + return new ConsumerGroup(snapshotRegistry, groupId); + } + + @Test + public void testGetOrCreateMember() { + ConsumerGroup consumerGroup = createConsumerGroup("foo"); + ConsumerGroupMember member; + + // Create a group. + member = consumerGroup.getOrMaybeCreateMember("member-id", true); + assertEquals("member-id", member.memberId()); + + // Get that group back. + member = consumerGroup.getOrMaybeCreateMember("member-id", false); + assertEquals("member-id", member.memberId()); + + assertThrows(UnknownMemberIdException.class, () -> + consumerGroup.getOrMaybeCreateMember("does-not-exist", false)); + } + + @Test + public void testUpdateMember() { + ConsumerGroup consumerGroup = createConsumerGroup("foo"); + ConsumerGroupMember member; + + member = consumerGroup.getOrMaybeCreateMember("member", true); + + member = new ConsumerGroupMember.Builder(member) + .setSubscribedTopicNames(Arrays.asList("foo", "bar")) + .build(); + + consumerGroup.updateMember(member); + + assertEquals(member, consumerGroup.getOrMaybeCreateMember("member", false)); + } + + @Test + public void testRemoveMember() { + ConsumerGroup consumerGroup = createConsumerGroup("foo"); + + consumerGroup.getOrMaybeCreateMember("member", true); + assertTrue(consumerGroup.hasMember("member")); + + consumerGroup.removeMember("member"); + assertFalse(consumerGroup.hasMember("member")); + + } + + @Test + public void testUpdatingMemberUpdatesPartitionEpoch() { + Uuid fooTopicId = Uuid.randomUuid(); + Uuid barTopicId = Uuid.randomUuid(); + Uuid zarTopicId = Uuid.randomUuid(); + + ConsumerGroup consumerGroup = createConsumerGroup("foo"); + ConsumerGroupMember member; + + member = new ConsumerGroupMember.Builder("member") + .setMemberEpoch(10) + .setAssignedPartitions(mkAssignment( + mkTopicAssignment(fooTopicId, 1, 2, 3))) + .setPartitionsPendingRevocation(mkAssignment( + mkTopicAssignment(barTopicId, 4, 5, 6))) + .setPartitionsPendingAssignment(mkAssignment( + mkTopicAssignment(zarTopicId, 7, 8, 9))) + .build(); + + consumerGroup.updateMember(member); + + assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 1)); + assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 2)); + assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 3)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 4)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 5)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 6)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 7)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 8)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 9)); + + member = new ConsumerGroupMember.Builder(member) + .setMemberEpoch(10) + .setAssignedPartitions(mkAssignment( + mkTopicAssignment(barTopicId, 1, 2, 3))) + .setPartitionsPendingRevocation(mkAssignment( + mkTopicAssignment(zarTopicId, 4, 5, 6))) + .setPartitionsPendingAssignment(mkAssignment( + mkTopicAssignment(fooTopicId, 7, 8, 9))) + .build(); + + consumerGroup.updateMember(member); + + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 1)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 2)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 3)); + assertEquals(10, consumerGroup.currentPartitionEpoch(zarTopicId, 4)); + assertEquals(10, consumerGroup.currentPartitionEpoch(zarTopicId, 5)); + assertEquals(10, consumerGroup.currentPartitionEpoch(zarTopicId, 6)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 7)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 8)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 9)); + } + + @Test + public void testDeletingMemberRemovesPartitionEpoch() { + Uuid fooTopicId = Uuid.randomUuid(); + Uuid barTopicId = Uuid.randomUuid(); + Uuid zarTopicId = Uuid.randomUuid(); + + ConsumerGroup consumerGroup = createConsumerGroup("foo"); + ConsumerGroupMember member; + + member = new ConsumerGroupMember.Builder("member") + .setMemberEpoch(10) + .setAssignedPartitions(mkAssignment( + mkTopicAssignment(fooTopicId, 1, 2, 3))) + .setPartitionsPendingRevocation(mkAssignment( + mkTopicAssignment(barTopicId, 4, 5, 6))) + .setPartitionsPendingAssignment(mkAssignment( + mkTopicAssignment(zarTopicId, 7, 8, 9))) + .build(); + + consumerGroup.updateMember(member); + + assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 1)); + assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 2)); + assertEquals(10, consumerGroup.currentPartitionEpoch(fooTopicId, 3)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 4)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 5)); + assertEquals(10, consumerGroup.currentPartitionEpoch(barTopicId, 6)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 7)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 8)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 9)); + + consumerGroup.removeMember(member.memberId()); + + assertEquals(-1, consumerGroup.currentPartitionEpoch(barTopicId, 1)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(barTopicId, 2)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(barTopicId, 3)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 4)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 5)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(zarTopicId, 6)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 7)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 8)); + assertEquals(-1, consumerGroup.currentPartitionEpoch(fooTopicId, 9)); + } + + @Test + public void testGroupState() { + ConsumerGroup consumerGroup = createConsumerGroup("foo"); + assertEquals(ConsumerGroup.ConsumerGroupState.EMPTY, consumerGroup.state()); + + ConsumerGroupMember member1 = new ConsumerGroupMember.Builder("member1") + .setMemberEpoch(1) + .setPreviousMemberEpoch(0) + .setNextMemberEpoch(1) + .build(); + + consumerGroup.updateMember(member1); + consumerGroup.setGroupEpoch(1); + + assertEquals(ConsumerGroup.ConsumerGroupState.ASSIGNING, consumerGroup.state()); + + ConsumerGroupMember member2 = new ConsumerGroupMember.Builder("member2") + .setMemberEpoch(1) + .setPreviousMemberEpoch(0) + .setNextMemberEpoch(1) + .build(); + + consumerGroup.updateMember(member2); + consumerGroup.setGroupEpoch(2); + + assertEquals(ConsumerGroup.ConsumerGroupState.ASSIGNING, consumerGroup.state()); + + consumerGroup.setAssignmentEpoch(2); + + assertEquals(ConsumerGroup.ConsumerGroupState.RECONCILING, consumerGroup.state()); + + member1 = new ConsumerGroupMember.Builder(member1) + .setMemberEpoch(2) + .setPreviousMemberEpoch(1) + .setNextMemberEpoch(2) + .build(); + consumerGroup.updateMember(member1); + + assertEquals(ConsumerGroup.ConsumerGroupState.RECONCILING, consumerGroup.state()); + + member2 = new ConsumerGroupMember.Builder(member2) + .setMemberEpoch(2) + .setPreviousMemberEpoch(1) + .setNextMemberEpoch(2) + .build(); + consumerGroup.updateMember(member2); + + assertEquals(ConsumerGroup.ConsumerGroupState.STABLE, consumerGroup.state()); + + consumerGroup.removeMember("member1"); + consumerGroup.removeMember("member2"); + + assertEquals(ConsumerGroup.ConsumerGroupState.EMPTY, consumerGroup.state()); + } + + @Test + public void testPreferredServerAssignor() { + ConsumerGroup consumerGroup = createConsumerGroup("foo"); + + ConsumerGroupMember member1 = new ConsumerGroupMember.Builder("member1") + .setServerAssignorName("range") + .build(); + ConsumerGroupMember member2 = new ConsumerGroupMember.Builder("member2") + .setServerAssignorName("range") + .build(); + ConsumerGroupMember member3 = new ConsumerGroupMember.Builder("member3") + .setServerAssignorName("uniform") + .build(); + + assertEquals( + Optional.empty(), + consumerGroup.preferredServerAssignor(null, null) + ); + + assertEquals( + Optional.of("range"), + consumerGroup.preferredServerAssignor(null, member1) + ); + + consumerGroup.updateMember(member1); + + assertEquals( + Optional.of("range"), + consumerGroup.preferredServerAssignor(null, null) + ); + + assertEquals( + Optional.empty(), + consumerGroup.preferredServerAssignor(member1, null) + ); + + assertEquals( + Optional.of("range"), + consumerGroup.preferredServerAssignor(null, member2) + ); + + consumerGroup.updateMember(member2); + + assertEquals( + Optional.of("range"), + consumerGroup.preferredServerAssignor(null, null) + ); + + consumerGroup.updateMember(member3); + + assertEquals( + Optional.of("range"), + consumerGroup.preferredServerAssignor(null, null) + ); + + // Members without assignors + ConsumerGroupMember updatedMember1 = new ConsumerGroupMember.Builder("member1") + .setServerAssignorName(null) + .build(); + ConsumerGroupMember updatedMember2 = new ConsumerGroupMember.Builder("member2") + .setServerAssignorName(null) + .build(); + ConsumerGroupMember updatedMember3 = new ConsumerGroupMember.Builder("member3") + .setServerAssignorName(null) + .build(); + + + Optional<String> assignor = consumerGroup.preferredServerAssignor(member1, updatedMember1); + assertTrue(assignor.equals(Optional.of("range")) || assignor.equals(Optional.of("uniform"))); + + consumerGroup.updateMember(updatedMember1); + + assignor = consumerGroup.preferredServerAssignor(member1, updatedMember1); + assertTrue(assignor.equals(Optional.of("range")) || assignor.equals(Optional.of("uniform"))); Review Comment: after updating member 1 in L315, we have ``` range: 1 uniform: 1 ``` in serverAssignors. preferredServerAssignor() in L317 decrements "range" so the copy should have ``` range: 0 uniform: 1 ``` shouldn't this be `assertTrue(assignor.equals(Optional.of("uniform")));`? -- 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-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org