jolshan commented on code in PR #13639: URL: https://github.com/apache/kafka/pull/13639#discussion_r1203150079
########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/ConsumerGroup.java: ########## @@ -0,0 +1,535 @@ +/* + * 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.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.function.Consumer; + +/** + * 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 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.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) { + ConsumerGroupMember oldMember = members.put(newMember.memberId(), 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 A 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. + * + * @return A 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 Review Comment: If the partition did not exist it would also be -1 though right? There's just two possible cases? -- 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