jeffkbkim commented on code in PR #13637:
URL: https://github.com/apache/kafka/pull/13637#discussion_r1180707601


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilder.java:
##########
@@ -0,0 +1,327 @@
+/*
+ * 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.coordinator.group.Record;
+import org.apache.kafka.coordinator.group.assignor.AssignmentMemberSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentTopicMetadata;
+import org.apache.kafka.coordinator.group.assignor.GroupAssignment;
+import org.apache.kafka.coordinator.group.assignor.MemberAssignment;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignorException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentEpochRecord;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentRecord;
+
+/**
+ * Build a new Target Assignment based on the provided parameters. As a result,
+ * it yields the records that must be persisted to the log and the new member
+ * assignments as a map.
+ *
+ * Records are only created for members which have a new target assignment. If
+ * their assignment did not change, no new record is needed.
+ *
+ * When a member is deleted, it is assumed that its target assignment record
+ * is deleted as part of the member deletion process. In other words, this 
class
+ * does not yield a tombstone for removed members.
+ */
+public class TargetAssignmentBuilder {
+    /**
+     * The assignment result returned by {{@link 
TargetAssignmentBuilder#build()}}.
+     */
+    public static class TargetAssignmentResult {
+        /**
+         * The records that must be applied to the __consumer_offsets
+         * topics to persist the new target assignment.
+         */
+        private final List<Record> records;
+
+        /**
+         * The new target assignment for all members.
+         */
+        private final Map<String, Assignment> assignments;
+
+        TargetAssignmentResult(
+            List<org.apache.kafka.coordinator.group.Record> records,
+            Map<String, Assignment> assignments
+        ) {
+            Objects.requireNonNull(records);
+            Objects.requireNonNull(assignments);
+            this.records = records;
+            this.assignments = assignments;
+        }
+
+        /**
+         * @return The records.
+         */
+        public List<Record> records() {
+            return records;
+        }
+
+        /**
+         * @return The assignments.
+         */
+        public Map<String, Assignment> assignments() {
+            return assignments;
+        }
+    }
+
+    /**
+     * The group id.
+     */
+    private final String groupId;
+
+    /**
+     * The group epoch.
+     */
+    private final int groupEpoch;
+
+    /**
+     * The partition assignor used to compute the assignment.
+     */
+    private final PartitionAssignor assignor;
+
+    /**
+     * The members in the group.
+     */
+    private Map<String, ConsumerGroupMember> members = Collections.emptyMap();
+
+    /**
+     * The subscription metadata.
+     */
+    private Map<String, TopicMetadata> subscriptionMetadata = 
Collections.emptyMap();
+
+    /**
+     * The existing target assignment.
+     */
+    private Map<String, Assignment> assignments = Collections.emptyMap();
+
+    /**
+     * The members which have been updated or deleted. Deleted members
+     * are signaled by a null value.
+     */
+    private final Map<String, ConsumerGroupMember> updatedMembers = new 
HashMap<>();
+
+    /**
+     * Constructs the object.
+     *
+     * @param groupId       The group id.
+     * @param groupEpoch    The group epoch to compute a target assignment for.
+     * @param assignor      The assignor to use to compute the target 
assignment.
+     */
+    public TargetAssignmentBuilder(
+        String groupId,
+        int groupEpoch,
+        PartitionAssignor assignor
+    ) {
+        this.groupId = Objects.requireNonNull(groupId);
+        this.groupEpoch = groupEpoch;
+        this.assignor = Objects.requireNonNull(assignor);
+    }
+
+    /**
+     * Adds all the existing members.
+     *
+     * @param members   The existing members in the consumer group.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withMembers(
+        Map<String, ConsumerGroupMember> members
+    ) {
+        this.members = members;
+        return this;
+    }
+
+    /**
+     * Adds the subscription metadata to use.
+     *
+     * @param subscriptionMetadata  The subscription metadata.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withSubscriptionMetadata(
+        Map<String, TopicMetadata> subscriptionMetadata
+    ) {
+        this.subscriptionMetadata = subscriptionMetadata;
+        return this;
+    }
+
+    /**
+     * Adds the existing target assignments.
+     *
+     * @param assignments   The existing target assignments.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withTargetAssignments(
+        Map<String, Assignment> assignments
+    ) {
+        this.assignments = assignments;
+        return this;
+    }
+
+    /**
+     * Adds or updates a member. This is useful when the updated member is
+     * not yet materialized in memory.
+     *
+     * @param memberId  The member id.
+     * @param member    The member to add or update.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder addOrUpdateMember(
+        String memberId,
+        ConsumerGroupMember member
+    ) {
+        this.updatedMembers.put(memberId, member);
+        return this;
+    }
+
+    /**
+     * Removes a member. This is useful when the removed member
+     * is not yet materialized in memory.
+     *
+     * @param memberId The member id.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder removeMember(
+        String memberId
+    ) {
+        return addOrUpdateMember(memberId, null);
+    }
+
+    /**
+     * Builds the new target assignment.
+     *
+     * @return A TargetAssignmentResult which contains the records to update
+     *         the existing target assignment.
+     * @throws PartitionAssignorException if the assignment can not be 
computed.

Review Comment:
   nit: cannot



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilder.java:
##########
@@ -0,0 +1,327 @@
+/*
+ * 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.coordinator.group.Record;
+import org.apache.kafka.coordinator.group.assignor.AssignmentMemberSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentTopicMetadata;
+import org.apache.kafka.coordinator.group.assignor.GroupAssignment;
+import org.apache.kafka.coordinator.group.assignor.MemberAssignment;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignorException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentEpochRecord;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentRecord;
+
+/**
+ * Build a new Target Assignment based on the provided parameters. As a result,
+ * it yields the records that must be persisted to the log and the new member
+ * assignments as a map.
+ *
+ * Records are only created for members which have a new target assignment. If
+ * their assignment did not change, no new record is needed.
+ *
+ * When a member is deleted, it is assumed that its target assignment record
+ * is deleted as part of the member deletion process. In other words, this 
class
+ * does not yield a tombstone for removed members.
+ */
+public class TargetAssignmentBuilder {
+    /**
+     * The assignment result returned by {{@link 
TargetAssignmentBuilder#build()}}.
+     */
+    public static class TargetAssignmentResult {
+        /**
+         * The records that must be applied to the __consumer_offsets
+         * topics to persist the new target assignment.
+         */
+        private final List<Record> records;
+
+        /**
+         * The new target assignment for all members.
+         */
+        private final Map<String, Assignment> assignments;
+
+        TargetAssignmentResult(
+            List<org.apache.kafka.coordinator.group.Record> records,
+            Map<String, Assignment> assignments
+        ) {
+            Objects.requireNonNull(records);
+            Objects.requireNonNull(assignments);
+            this.records = records;
+            this.assignments = assignments;
+        }
+
+        /**
+         * @return The records.
+         */
+        public List<Record> records() {
+            return records;
+        }
+
+        /**
+         * @return The assignments.
+         */
+        public Map<String, Assignment> assignments() {
+            return assignments;
+        }
+    }
+
+    /**
+     * The group id.
+     */
+    private final String groupId;
+
+    /**
+     * The group epoch.
+     */
+    private final int groupEpoch;
+
+    /**
+     * The partition assignor used to compute the assignment.
+     */
+    private final PartitionAssignor assignor;
+
+    /**
+     * The members in the group.
+     */
+    private Map<String, ConsumerGroupMember> members = Collections.emptyMap();
+
+    /**
+     * The subscription metadata.
+     */
+    private Map<String, TopicMetadata> subscriptionMetadata = 
Collections.emptyMap();

Review Comment:
   to confirm, this subscriptionMetadata holds topicId -> topic metadata and is 
for all members, i.e. a change in a member's subscription will also update this 
field



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilder.java:
##########
@@ -0,0 +1,327 @@
+/*
+ * 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.coordinator.group.Record;
+import org.apache.kafka.coordinator.group.assignor.AssignmentMemberSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentTopicMetadata;
+import org.apache.kafka.coordinator.group.assignor.GroupAssignment;
+import org.apache.kafka.coordinator.group.assignor.MemberAssignment;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignorException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentEpochRecord;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentRecord;
+
+/**
+ * Build a new Target Assignment based on the provided parameters. As a result,
+ * it yields the records that must be persisted to the log and the new member
+ * assignments as a map.
+ *
+ * Records are only created for members which have a new target assignment. If
+ * their assignment did not change, no new record is needed.
+ *
+ * When a member is deleted, it is assumed that its target assignment record
+ * is deleted as part of the member deletion process. In other words, this 
class
+ * does not yield a tombstone for removed members.
+ */
+public class TargetAssignmentBuilder {
+    /**
+     * The assignment result returned by {{@link 
TargetAssignmentBuilder#build()}}.
+     */
+    public static class TargetAssignmentResult {
+        /**
+         * The records that must be applied to the __consumer_offsets
+         * topics to persist the new target assignment.
+         */
+        private final List<Record> records;
+
+        /**
+         * The new target assignment for all members.
+         */
+        private final Map<String, Assignment> assignments;
+
+        TargetAssignmentResult(
+            List<org.apache.kafka.coordinator.group.Record> records,
+            Map<String, Assignment> assignments
+        ) {
+            Objects.requireNonNull(records);
+            Objects.requireNonNull(assignments);
+            this.records = records;
+            this.assignments = assignments;
+        }
+
+        /**
+         * @return The records.
+         */
+        public List<Record> records() {
+            return records;
+        }
+
+        /**
+         * @return The assignments.
+         */
+        public Map<String, Assignment> assignments() {
+            return assignments;
+        }
+    }
+
+    /**
+     * The group id.
+     */
+    private final String groupId;
+
+    /**
+     * The group epoch.
+     */
+    private final int groupEpoch;
+
+    /**
+     * The partition assignor used to compute the assignment.
+     */
+    private final PartitionAssignor assignor;
+
+    /**
+     * The members in the group.
+     */
+    private Map<String, ConsumerGroupMember> members = Collections.emptyMap();
+
+    /**
+     * The subscription metadata.
+     */
+    private Map<String, TopicMetadata> subscriptionMetadata = 
Collections.emptyMap();
+
+    /**
+     * The existing target assignment.
+     */
+    private Map<String, Assignment> assignments = Collections.emptyMap();
+
+    /**
+     * The members which have been updated or deleted. Deleted members
+     * are signaled by a null value.
+     */
+    private final Map<String, ConsumerGroupMember> updatedMembers = new 
HashMap<>();
+
+    /**
+     * Constructs the object.
+     *
+     * @param groupId       The group id.
+     * @param groupEpoch    The group epoch to compute a target assignment for.
+     * @param assignor      The assignor to use to compute the target 
assignment.
+     */
+    public TargetAssignmentBuilder(
+        String groupId,
+        int groupEpoch,
+        PartitionAssignor assignor
+    ) {
+        this.groupId = Objects.requireNonNull(groupId);
+        this.groupEpoch = groupEpoch;
+        this.assignor = Objects.requireNonNull(assignor);
+    }
+
+    /**
+     * Adds all the existing members.
+     *
+     * @param members   The existing members in the consumer group.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withMembers(
+        Map<String, ConsumerGroupMember> members
+    ) {
+        this.members = members;
+        return this;
+    }
+
+    /**
+     * Adds the subscription metadata to use.
+     *
+     * @param subscriptionMetadata  The subscription metadata.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withSubscriptionMetadata(
+        Map<String, TopicMetadata> subscriptionMetadata
+    ) {
+        this.subscriptionMetadata = subscriptionMetadata;
+        return this;
+    }
+
+    /**
+     * Adds the existing target assignments.
+     *
+     * @param assignments   The existing target assignments.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withTargetAssignments(
+        Map<String, Assignment> assignments
+    ) {
+        this.assignments = assignments;
+        return this;
+    }
+
+    /**
+     * Adds or updates a member. This is useful when the updated member is
+     * not yet materialized in memory.
+     *
+     * @param memberId  The member id.
+     * @param member    The member to add or update.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder addOrUpdateMember(
+        String memberId,
+        ConsumerGroupMember member
+    ) {
+        this.updatedMembers.put(memberId, member);
+        return this;
+    }
+
+    /**
+     * Removes a member. This is useful when the removed member
+     * is not yet materialized in memory.
+     *
+     * @param memberId The member id.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder removeMember(
+        String memberId
+    ) {
+        return addOrUpdateMember(memberId, null);
+    }
+
+    /**
+     * Builds the new target assignment.

Review Comment:
   i think we should conform to use either assignments or assignment. i think 
assignment makes sense; it's inferred that the assignment covers all members 
and the object is called Assignment/TargetAssignment.
   
   on the other hand, Assignment class is specific to a member so maybe 
assignments is more appropriate.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilderTest.java:
##########
@@ -0,0 +1,698 @@
+/*
+ * 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.coordinator.group.assignor.AssignmentMemberSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentTopicMetadata;
+import org.apache.kafka.coordinator.group.assignor.GroupAssignment;
+import org.apache.kafka.coordinator.group.assignor.MemberAssignment;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.coordinator.group.consumer.AssignmentTestUtil.mkAssignment;
+import static 
org.apache.kafka.coordinator.group.consumer.AssignmentTestUtil.mkTopicAssignment;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentEpochRecord;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentRecord;
+import static 
org.apache.kafka.coordinator.group.consumer.TargetAssignmentBuilder.createAssignmentMemberSpec;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class TargetAssignmentBuilderTest {
+
+    public static class TargetAssignmentBuilderTestContext {
+        private final String groupId;
+        private final int groupEpoch;
+        private final PartitionAssignor assignor = 
mock(PartitionAssignor.class);
+        private final Map<String, ConsumerGroupMember> members = new 
HashMap<>();
+        private final Map<String, TopicMetadata> subscriptionMetadata = new 
HashMap<>();
+        private final Map<String, ConsumerGroupMember> updatedMembers = new 
HashMap<>();
+        private final Map<String, Assignment> targetAssignments = new 
HashMap<>();
+        private final Map<String, MemberAssignment> assignments = new 
HashMap<>();
+
+        public TargetAssignmentBuilderTestContext(
+            String groupId,
+            int groupEpoch
+        ) {
+            this.groupId = groupId;
+            this.groupEpoch = groupEpoch;
+        }
+
+        public void addGroupMember(
+            String memberId,
+            List<String> subscriptions,
+            Map<Uuid, Set<Integer>> targetPartitions
+        ) {
+            members.put(memberId, new ConsumerGroupMember.Builder(memberId)
+                .setSubscribedTopicNames(subscriptions)
+                .setRebalanceTimeoutMs(5000)

Review Comment:
   do we need this?



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilder.java:
##########
@@ -0,0 +1,327 @@
+/*
+ * 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.coordinator.group.Record;
+import org.apache.kafka.coordinator.group.assignor.AssignmentMemberSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentTopicMetadata;
+import org.apache.kafka.coordinator.group.assignor.GroupAssignment;
+import org.apache.kafka.coordinator.group.assignor.MemberAssignment;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignorException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentEpochRecord;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentRecord;
+
+/**
+ * Build a new Target Assignment based on the provided parameters. As a result,
+ * it yields the records that must be persisted to the log and the new member
+ * assignments as a map.
+ *
+ * Records are only created for members which have a new target assignment. If
+ * their assignment did not change, no new record is needed.
+ *
+ * When a member is deleted, it is assumed that its target assignment record
+ * is deleted as part of the member deletion process. In other words, this 
class
+ * does not yield a tombstone for removed members.
+ */
+public class TargetAssignmentBuilder {
+    /**
+     * The assignment result returned by {{@link 
TargetAssignmentBuilder#build()}}.
+     */
+    public static class TargetAssignmentResult {
+        /**
+         * The records that must be applied to the __consumer_offsets
+         * topics to persist the new target assignment.
+         */
+        private final List<Record> records;
+
+        /**
+         * The new target assignment for all members.
+         */
+        private final Map<String, Assignment> assignments;
+
+        TargetAssignmentResult(
+            List<org.apache.kafka.coordinator.group.Record> records,
+            Map<String, Assignment> assignments
+        ) {
+            Objects.requireNonNull(records);
+            Objects.requireNonNull(assignments);
+            this.records = records;
+            this.assignments = assignments;
+        }
+
+        /**
+         * @return The records.
+         */
+        public List<Record> records() {
+            return records;
+        }
+
+        /**
+         * @return The assignments.
+         */
+        public Map<String, Assignment> assignments() {
+            return assignments;
+        }
+    }
+
+    /**
+     * The group id.
+     */
+    private final String groupId;
+
+    /**
+     * The group epoch.
+     */
+    private final int groupEpoch;
+
+    /**
+     * The partition assignor used to compute the assignment.
+     */
+    private final PartitionAssignor assignor;
+
+    /**
+     * The members in the group.
+     */
+    private Map<String, ConsumerGroupMember> members = Collections.emptyMap();
+
+    /**
+     * The subscription metadata.
+     */
+    private Map<String, TopicMetadata> subscriptionMetadata = 
Collections.emptyMap();
+
+    /**
+     * The existing target assignment.
+     */
+    private Map<String, Assignment> assignments = Collections.emptyMap();
+
+    /**
+     * The members which have been updated or deleted. Deleted members
+     * are signaled by a null value.
+     */
+    private final Map<String, ConsumerGroupMember> updatedMembers = new 
HashMap<>();
+
+    /**
+     * Constructs the object.
+     *
+     * @param groupId       The group id.
+     * @param groupEpoch    The group epoch to compute a target assignment for.
+     * @param assignor      The assignor to use to compute the target 
assignment.
+     */
+    public TargetAssignmentBuilder(
+        String groupId,
+        int groupEpoch,
+        PartitionAssignor assignor
+    ) {
+        this.groupId = Objects.requireNonNull(groupId);
+        this.groupEpoch = groupEpoch;
+        this.assignor = Objects.requireNonNull(assignor);
+    }
+
+    /**
+     * Adds all the existing members.
+     *
+     * @param members   The existing members in the consumer group.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withMembers(
+        Map<String, ConsumerGroupMember> members
+    ) {
+        this.members = members;
+        return this;
+    }
+
+    /**
+     * Adds the subscription metadata to use.
+     *
+     * @param subscriptionMetadata  The subscription metadata.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withSubscriptionMetadata(
+        Map<String, TopicMetadata> subscriptionMetadata
+    ) {
+        this.subscriptionMetadata = subscriptionMetadata;
+        return this;
+    }
+
+    /**
+     * Adds the existing target assignments.
+     *
+     * @param assignments   The existing target assignments.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder withTargetAssignments(
+        Map<String, Assignment> assignments
+    ) {
+        this.assignments = assignments;
+        return this;
+    }
+
+    /**
+     * Adds or updates a member. This is useful when the updated member is
+     * not yet materialized in memory.
+     *
+     * @param memberId  The member id.
+     * @param member    The member to add or update.
+     * @return This object.
+     */
+    public TargetAssignmentBuilder addOrUpdateMember(

Review Comment:
   i'm having a bit of trouble understanding how this will be used. i would 
think when we build a target assignment, we would already have all members 
updated/removed and snapshot the members state. this implies the state changes 
while we build the target assignment right?



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/consumer/TargetAssignmentBuilderTest.java:
##########
@@ -0,0 +1,698 @@
+/*
+ * 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.coordinator.group.assignor.AssignmentMemberSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentSpec;
+import org.apache.kafka.coordinator.group.assignor.AssignmentTopicMetadata;
+import org.apache.kafka.coordinator.group.assignor.GroupAssignment;
+import org.apache.kafka.coordinator.group.assignor.MemberAssignment;
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.coordinator.group.consumer.AssignmentTestUtil.mkAssignment;
+import static 
org.apache.kafka.coordinator.group.consumer.AssignmentTestUtil.mkTopicAssignment;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentEpochRecord;
+import static 
org.apache.kafka.coordinator.group.RecordHelpers.newTargetAssignmentRecord;
+import static 
org.apache.kafka.coordinator.group.consumer.TargetAssignmentBuilder.createAssignmentMemberSpec;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class TargetAssignmentBuilderTest {
+
+    public static class TargetAssignmentBuilderTestContext {
+        private final String groupId;
+        private final int groupEpoch;
+        private final PartitionAssignor assignor = 
mock(PartitionAssignor.class);
+        private final Map<String, ConsumerGroupMember> members = new 
HashMap<>();
+        private final Map<String, TopicMetadata> subscriptionMetadata = new 
HashMap<>();
+        private final Map<String, ConsumerGroupMember> updatedMembers = new 
HashMap<>();
+        private final Map<String, Assignment> targetAssignments = new 
HashMap<>();
+        private final Map<String, MemberAssignment> assignments = new 
HashMap<>();
+
+        public TargetAssignmentBuilderTestContext(
+            String groupId,
+            int groupEpoch
+        ) {
+            this.groupId = groupId;
+            this.groupEpoch = groupEpoch;
+        }
+
+        public void addGroupMember(
+            String memberId,
+            List<String> subscriptions,
+            Map<Uuid, Set<Integer>> targetPartitions
+        ) {
+            members.put(memberId, new ConsumerGroupMember.Builder(memberId)
+                .setSubscribedTopicNames(subscriptions)
+                .setRebalanceTimeoutMs(5000)
+                .build());
+
+            targetAssignments.put(memberId, new Assignment(
+                (byte) 0,
+                targetPartitions,
+                VersionedMetadata.EMPTY
+            ));
+        }
+
+        public Uuid addTopicMetadata(
+            String topicName,
+            int numPartitions
+        ) {
+            Uuid topicId = Uuid.randomUuid();
+            subscriptionMetadata.put(topicName, new TopicMetadata(
+                topicId,
+                topicName,
+                numPartitions
+            ));
+            return topicId;
+        }
+
+        public void updateMemberSubscription(
+            String memberId,
+            List<String> subscriptions
+        ) {
+            updateMemberSubscription(
+                memberId,
+                subscriptions,
+                Optional.empty(),
+                Optional.empty()
+            );
+        }
+
+        public void updateMemberSubscription(
+            String memberId,
+            List<String> subscriptions,
+            Optional<String> instanceId,
+            Optional<String> rackId
+        ) {
+            ConsumerGroupMember existingMember = members.get(memberId);
+            ConsumerGroupMember.Builder builder;
+            if (existingMember != null) {
+                builder = new ConsumerGroupMember.Builder(existingMember);
+            } else {
+                builder = new ConsumerGroupMember.Builder(memberId);
+            }
+            updatedMembers.put(memberId, builder
+                .setSubscribedTopicNames(subscriptions)
+                .maybeUpdateInstanceId(instanceId)
+                .maybeUpdateRackId(rackId)
+                .build());
+        }
+
+        public void removeMemberSubscription(
+            String memberId
+        ) {
+            this.updatedMembers.put(memberId, null);
+        }
+
+        public void prepareMemberAssignment(
+            String memberId,
+            Map<Uuid, Set<Integer>> assignment
+        ) {
+            assignments.put(memberId, new MemberAssignment(assignment));
+        }
+
+        public TargetAssignmentBuilder.TargetAssignmentResult build() {
+            // Prepare expected member specs.
+            Map<String, AssignmentMemberSpec> memberSpecs = new HashMap<>();
+
+            // All the existing members are prepared.
+            members.forEach((memberId, member) -> {
+                memberSpecs.put(memberId, createAssignmentMemberSpec(
+                    member,
+                    targetAssignments.getOrDefault(memberId, Assignment.EMPTY),
+                    subscriptionMetadata
+                ));
+            });
+
+            // All the updated are added and all the deleted
+            // members are removed.
+            updatedMembers.forEach((memberId, updatedMemberOrNull) -> {
+                if (updatedMemberOrNull == null) {
+                    memberSpecs.remove(memberId);
+                } else {
+                    memberSpecs.put(memberId, createAssignmentMemberSpec(
+                        updatedMemberOrNull,
+                        targetAssignments.getOrDefault(memberId, 
Assignment.EMPTY),
+                        subscriptionMetadata
+                    ));
+                }
+            });
+
+            // Prepare the expected topic metadata.
+            Map<Uuid, AssignmentTopicMetadata> topicMetadata = new HashMap<>();
+            subscriptionMetadata.forEach((topicName, metadata) -> {
+                topicMetadata.put(metadata.id(), new 
AssignmentTopicMetadata(metadata.numPartitions()));
+            });
+
+            // Prepare the expected assignment spec.
+            AssignmentSpec assignmentSpec = new AssignmentSpec(
+                memberSpecs,
+                topicMetadata
+            );
+
+            // We use `any` here to always return an assignment but use 
`verify` later on

Review Comment:
   still not sure why we can't use `assign(assignmentSpec)` here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

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


Reply via email to