bbejeck commented on code in PR #18676: URL: https://github.com/apache/kafka/pull/18676#discussion_r1936153413
########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/TargetAssignmentBuilder.java: ########## @@ -0,0 +1,366 @@ +/* + * 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.streams; + +import org.apache.kafka.coordinator.common.runtime.CoordinatorRecord; +import org.apache.kafka.coordinator.group.streams.assignor.AssignmentMemberSpec; +import org.apache.kafka.coordinator.group.streams.assignor.GroupAssignment; +import org.apache.kafka.coordinator.group.streams.assignor.GroupSpecImpl; +import org.apache.kafka.coordinator.group.streams.assignor.MemberAssignment; +import org.apache.kafka.coordinator.group.streams.assignor.TaskAssignor; +import org.apache.kafka.coordinator.group.streams.assignor.TaskAssignorException; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredTopology; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * Build a new Target TasksTuple 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. + * <p> + * Records are only created for members which have a new target assignment. If their assignment did not change, no new record is needed. + * <p> + * 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 group id. + */ + private final String groupId; + /** + * The group epoch. + */ + private final int groupEpoch; + /** + * The partition assignor used to compute the assignment. + */ + private final TaskAssignor assignor; + /** + * The assignment configs. + */ + private final Map<String, String> assignmentConfigs; + /** + * The members which have been updated or deleted. Deleted members are signaled by a null value. + */ + private final Map<String, StreamsGroupMember> updatedMembers = new HashMap<>(); + /** + * The members in the group. + */ + private Map<String, StreamsGroupMember> members = Map.of(); + + /** + * The subscription metadata. + */ + private Map<String, org.apache.kafka.coordinator.group.streams.TopicMetadata> subscriptionMetadata = Map.of(); + + /** + * The existing target assignment. + */ + private Map<String, org.apache.kafka.coordinator.group.streams.TasksTuple> targetAssignment = Map.of(); + + /** + * The topology. + */ + private ConfiguredTopology topology; + /** + * The static members in the group. + */ + private Map<String, String> staticMembers = Map.of(); + + /** + * 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, + TaskAssignor assignor, + Map<String, String> assignmentConfigs + ) { + this.groupId = Objects.requireNonNull(groupId); + this.groupEpoch = groupEpoch; + this.assignor = Objects.requireNonNull(assignor); + this.assignmentConfigs = Objects.requireNonNull(assignmentConfigs); + } + + static AssignmentMemberSpec createAssignmentMemberSpec( + StreamsGroupMember member, + TasksTuple targetAssignment + ) { + return new AssignmentMemberSpec( + member.instanceId(), + member.rackId(), + targetAssignment.activeTasks(), + targetAssignment.standbyTasks(), + targetAssignment.warmupTasks(), + member.processId(), + member.clientTags(), + Map.of(), + Map.of() + ); + } + + /** + * Adds all the existing members. + * + * @param members The existing members in the streams group. + * @return This object. + */ + public TargetAssignmentBuilder withMembers( + Map<String, StreamsGroupMember> members + ) { + this.members = members; + return this; + } + + /** + * Adds all the existing static members. + * + * @param staticMembers The existing static members in the streams group. + * @return This object. + */ + public TargetAssignmentBuilder withStaticMembers( + Map<String, String> staticMembers + ) { + this.staticMembers = staticMembers; + return this; + } + + /** + * Adds the subscription metadata to use. + * + * @param partitionMetadata The subscription metadata. + * @return This object. + */ + public TargetAssignmentBuilder withPartitionMetadata( + Map<String, org.apache.kafka.coordinator.group.streams.TopicMetadata> partitionMetadata + ) { + this.subscriptionMetadata = partitionMetadata; + return this; + } + + /** + * Adds the existing target assignment. + * + * @param targetAssignment The existing target assignment. + * @return This object. + */ + public TargetAssignmentBuilder withTargetAssignment( + Map<String, org.apache.kafka.coordinator.group.streams.TasksTuple> targetAssignment + ) { + this.targetAssignment = targetAssignment; + return this; + } + + /** + * Adds the topology image. + * + * @param topology The topology. + * @return This object. + */ + public TargetAssignmentBuilder withTopology( + ConfiguredTopology topology + ) { + this.topology = topology; + 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, + StreamsGroupMember 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); Review Comment: Why not just outright remove the member at this point? I get that in the `build()` method will filter it out, but does that have any advantage other than a removal here. ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/TopologyMetadata.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.streams; + +import org.apache.kafka.coordinator.group.streams.assignor.TopologyDescriber; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredSubtopology; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredTopology; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +/** + * The topology metadata class is used by the {@link org.apache.kafka.coordinator.group.streams.assignor.TaskAssignor} to obtain topic and + * partition metadata for the topology that the streams group using. + * + * @param topicMetadata The topic Ids mapped to their corresponding {@link TopicMetadata} object, which contains topic and partition + * metadata. + */ +public record TopologyMetadata(Map<String, TopicMetadata> topicMetadata, ConfiguredTopology topology) implements TopologyDescriber { + + public TopologyMetadata { + Objects.requireNonNull(topicMetadata); + Objects.requireNonNull(topology); + } + + /** + * Map of topic names to topic metadata. + * + * @return The map of topic Ids to topic metadata. + */ + @Override + public Map<String, TopicMetadata> topicMetadata() { + return this.topicMetadata; + } + + @Override + public boolean isStateful(String subtopologyId) { Review Comment: For the methods that use `getXOrFail` would it be worth adding a brief javadoc about the possibility of an `IllegalStateException` being thrown? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/TopologyMetadata.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.streams; + +import org.apache.kafka.coordinator.group.streams.assignor.TopologyDescriber; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredSubtopology; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredTopology; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +/** + * The topology metadata class is used by the {@link org.apache.kafka.coordinator.group.streams.assignor.TaskAssignor} to obtain topic and + * partition metadata for the topology that the streams group using. + * + * @param topicMetadata The topic Ids mapped to their corresponding {@link TopicMetadata} object, which contains topic and partition + * metadata. + */ +public record TopologyMetadata(Map<String, TopicMetadata> topicMetadata, ConfiguredTopology topology) implements TopologyDescriber { + + public TopologyMetadata { + Objects.requireNonNull(topicMetadata); + Objects.requireNonNull(topology); + } + + /** + * Map of topic names to topic metadata. + * + * @return The map of topic Ids to topic metadata. + */ + @Override + public Map<String, TopicMetadata> topicMetadata() { + return this.topicMetadata; + } + + @Override + public boolean isStateful(String subtopologyId) { + final ConfiguredSubtopology subtopology = getSubtopologyOrFail(subtopologyId); + return !subtopology.stateChangelogTopics().isEmpty(); + } + + @Override + public List<String> subtopologies() { + return getSubtopologiesOrFail().keySet().stream().toList(); + } + + /** + * The number of partitions for the given subtopology ID. + * + * @param subtopologyId ID of the corresponding subtopology + * @return The number of partitions corresponding to the given subtopology ID, or -1 if the subtopology ID does not exist. + */ + @Override + public int numTasks(String subtopologyId) { + final ConfiguredSubtopology subtopology = getSubtopologyOrFail(subtopologyId); + return Stream.concat( + subtopology.sourceTopics().stream(), + subtopology.repartitionSourceTopics().keySet().stream() + ).map(topic -> this.topicMetadata.get(topic).numPartitions()).max(Integer::compareTo).orElse(-1); + } + + private ConfiguredSubtopology getSubtopologyOrFail(String subtopologyId) { + final Map<String, ConfiguredSubtopology> subtopologies = getSubtopologiesOrFail(); + if (!subtopologies.containsKey(subtopologyId)) { + throw new IllegalStateException(String.format("Topology does not contain subtopology %s", subtopologyId)); Review Comment: This branch isn't tested. Same for below in `getSubtopologiesOrFail()` ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/TopologyMetadata.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.streams; + +import org.apache.kafka.coordinator.group.streams.assignor.TopologyDescriber; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredSubtopology; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredTopology; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +/** + * The topology metadata class is used by the {@link org.apache.kafka.coordinator.group.streams.assignor.TaskAssignor} to obtain topic and + * partition metadata for the topology that the streams group using. + * + * @param topicMetadata The topic Ids mapped to their corresponding {@link TopicMetadata} object, which contains topic and partition + * metadata. + */ +public record TopologyMetadata(Map<String, TopicMetadata> topicMetadata, ConfiguredTopology topology) implements TopologyDescriber { + + public TopologyMetadata { + Objects.requireNonNull(topicMetadata); + Objects.requireNonNull(topology); + } + + /** + * Map of topic names to topic metadata. + * + * @return The map of topic Ids to topic metadata. + */ + @Override + public Map<String, TopicMetadata> topicMetadata() { + return this.topicMetadata; + } + + @Override + public boolean isStateful(String subtopologyId) { + final ConfiguredSubtopology subtopology = getSubtopologyOrFail(subtopologyId); + return !subtopology.stateChangelogTopics().isEmpty(); + } + + @Override + public List<String> subtopologies() { + return getSubtopologiesOrFail().keySet().stream().toList(); + } + + /** + * The number of partitions for the given subtopology ID. + * + * @param subtopologyId ID of the corresponding subtopology + * @return The number of partitions corresponding to the given subtopology ID, or -1 if the subtopology ID does not exist. + */ + @Override + public int numTasks(String subtopologyId) { Review Comment: Given the javadoc says `number of partitions` should the method be named `numPartitions`? It's a minor point, so I'll leave it to you to decide. ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/TargetAssignmentBuilder.java: ########## @@ -0,0 +1,366 @@ +/* + * 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.streams; + +import org.apache.kafka.coordinator.common.runtime.CoordinatorRecord; +import org.apache.kafka.coordinator.group.streams.assignor.AssignmentMemberSpec; +import org.apache.kafka.coordinator.group.streams.assignor.GroupAssignment; +import org.apache.kafka.coordinator.group.streams.assignor.GroupSpecImpl; +import org.apache.kafka.coordinator.group.streams.assignor.MemberAssignment; +import org.apache.kafka.coordinator.group.streams.assignor.TaskAssignor; +import org.apache.kafka.coordinator.group.streams.assignor.TaskAssignorException; +import org.apache.kafka.coordinator.group.streams.topics.ConfiguredTopology; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * Build a new Target TasksTuple 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. + * <p> + * Records are only created for members which have a new target assignment. If their assignment did not change, no new record is needed. + * <p> + * 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 group id. + */ + private final String groupId; + /** + * The group epoch. + */ + private final int groupEpoch; + /** + * The partition assignor used to compute the assignment. + */ + private final TaskAssignor assignor; + /** + * The assignment configs. + */ + private final Map<String, String> assignmentConfigs; + /** + * The members which have been updated or deleted. Deleted members are signaled by a null value. + */ + private final Map<String, StreamsGroupMember> updatedMembers = new HashMap<>(); + /** + * The members in the group. + */ + private Map<String, StreamsGroupMember> members = Map.of(); + + /** + * The subscription metadata. + */ + private Map<String, org.apache.kafka.coordinator.group.streams.TopicMetadata> subscriptionMetadata = Map.of(); + + /** + * The existing target assignment. + */ + private Map<String, org.apache.kafka.coordinator.group.streams.TasksTuple> targetAssignment = Map.of(); + + /** + * The topology. + */ + private ConfiguredTopology topology; + /** + * The static members in the group. + */ + private Map<String, String> staticMembers = Map.of(); + + /** + * 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, + TaskAssignor assignor, + Map<String, String> assignmentConfigs + ) { + this.groupId = Objects.requireNonNull(groupId); + this.groupEpoch = groupEpoch; + this.assignor = Objects.requireNonNull(assignor); + this.assignmentConfigs = Objects.requireNonNull(assignmentConfigs); + } + + static AssignmentMemberSpec createAssignmentMemberSpec( + StreamsGroupMember member, + TasksTuple targetAssignment + ) { + return new AssignmentMemberSpec( + member.instanceId(), + member.rackId(), + targetAssignment.activeTasks(), + targetAssignment.standbyTasks(), + targetAssignment.warmupTasks(), + member.processId(), + member.clientTags(), + Map.of(), + Map.of() + ); + } + + /** + * Adds all the existing members. + * + * @param members The existing members in the streams group. + * @return This object. + */ + public TargetAssignmentBuilder withMembers( + Map<String, StreamsGroupMember> members + ) { + this.members = members; + return this; + } + + /** + * Adds all the existing static members. + * + * @param staticMembers The existing static members in the streams group. + * @return This object. + */ + public TargetAssignmentBuilder withStaticMembers( + Map<String, String> staticMembers + ) { + this.staticMembers = staticMembers; + return this; + } + + /** + * Adds the subscription metadata to use. + * + * @param partitionMetadata The subscription metadata. + * @return This object. + */ + public TargetAssignmentBuilder withPartitionMetadata( + Map<String, org.apache.kafka.coordinator.group.streams.TopicMetadata> partitionMetadata + ) { + this.subscriptionMetadata = partitionMetadata; + return this; + } + + /** + * Adds the existing target assignment. + * + * @param targetAssignment The existing target assignment. + * @return This object. + */ + public TargetAssignmentBuilder withTargetAssignment( + Map<String, org.apache.kafka.coordinator.group.streams.TasksTuple> targetAssignment + ) { + this.targetAssignment = targetAssignment; + return this; + } + + /** + * Adds the topology image. + * + * @param topology The topology. + * @return This object. + */ + public TargetAssignmentBuilder withTopology( + ConfiguredTopology topology + ) { + this.topology = topology; + 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, + StreamsGroupMember 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 TaskAssignorException if the target assignment cannot be computed. + */ + public TargetAssignmentResult build() throws TaskAssignorException { + Map<String, AssignmentMemberSpec> memberSpecs = new HashMap<>(); + + // Prepare the member spec for all members. + members.forEach((memberId, member) -> memberSpecs.put(memberId, createAssignmentMemberSpec( + member, + targetAssignment.getOrDefault(memberId, org.apache.kafka.coordinator.group.streams.TasksTuple.EMPTY) + ))); + + // Update the member spec if updated or deleted members. + updatedMembers.forEach((memberId, updatedMemberOrNull) -> { + if (updatedMemberOrNull == null) { + memberSpecs.remove(memberId); + } else { + org.apache.kafka.coordinator.group.streams.TasksTuple assignment = targetAssignment.getOrDefault(memberId, + org.apache.kafka.coordinator.group.streams.TasksTuple.EMPTY); + + // A new static member joins and needs to replace an existing departed one. + if (updatedMemberOrNull.instanceId().isPresent()) { + String previousMemberId = staticMembers.get(updatedMemberOrNull.instanceId().get()); + if (previousMemberId != null && !previousMemberId.equals(memberId)) { + assignment = targetAssignment.getOrDefault(previousMemberId, + org.apache.kafka.coordinator.group.streams.TasksTuple.EMPTY); + } + } + + memberSpecs.put(memberId, createAssignmentMemberSpec( + updatedMemberOrNull, + assignment + )); + } + }); + + // Compute the assignment. + GroupAssignment newGroupAssignment; + if (topology.isReady()) { + newGroupAssignment = assignor.assign( + new GroupSpecImpl( + Collections.unmodifiableMap(memberSpecs), + assignmentConfigs + ), + new TopologyMetadata(subscriptionMetadata, topology) + ); + } else { + newGroupAssignment = new GroupAssignment( Review Comment: This branch isn't covered by the unit test. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
