rreddy-22 commented on code in PR #14182: URL: https://github.com/apache/kafka/pull/14182#discussion_r1312392493
########## group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignmentBuilderTest.java: ########## @@ -0,0 +1,1070 @@ +/* + * 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.assignor; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.coordinator.group.consumer.SubscribedTopicMetadata; +import org.apache.kafka.coordinator.group.consumer.TopicMetadata; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +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 java.util.TreeMap; + +import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkAssignment; +import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkTopicAssignment; +import static org.apache.kafka.coordinator.group.RecordHelpersTest.mkMapOfPartitionRacks; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class OptimizedUniformAssignmentBuilderTest { + private final UniformAssignor assignor = new UniformAssignor(); + private final Uuid topic1Uuid = Uuid.fromString("T1-A4s3VTwiI5CTbEp6POw"); + private final Uuid topic2Uuid = Uuid.fromString("T2-B4s3VTwiI5YHbPp6YUe"); + private final Uuid topic3Uuid = Uuid.fromString("T3-CU8fVTLCz5YMkLoDQsa"); + private final String topic1Name = "topic1"; + private final String topic2Name = "topic2"; + private final String topic3Name = "topic3"; + private final String memberA = "A"; + private final String memberB = "B"; + private final String memberC = "C"; + + @Test + public void testOneMemberNoTopicSubscription() { + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata( + Collections.singletonMap( + topic1Uuid, + new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + mkMapOfPartitionRacks(3) + ) + ) + ); + + Map<String, AssignmentMemberSpec> members = Collections.singletonMap( + memberA, + new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Collections.emptyList(), + Collections.emptyMap() + ) + ); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + GroupAssignment groupAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + assertEquals(Collections.emptyMap(), groupAssignment.members()); + } + + @Test + public void testOneMemberSubscribedToNonexistentTopic() { + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata( + Collections.singletonMap( + topic1Uuid, + new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + mkMapOfPartitionRacks(3) + ) + ) + ); + + Map<String, AssignmentMemberSpec> members = Collections.singletonMap( + memberA, + new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Collections.singletonList(topic2Uuid), + Collections.emptyMap() + ) + ); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + GroupAssignment groupAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + assertEquals(Collections.emptyMap(), groupAssignment.members()); + } + + @Test + public void testFirstAssignmentTwoMembersSubscribedToTwoTopicsNoMemberRacks() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + topicMetadata.put(topic1Uuid, new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + mkMapOfPartitionRacks(3) + )); + topicMetadata.put(topic3Uuid, new TopicMetadata( + topic3Uuid, + topic3Name, + 2, + mkMapOfPartitionRacks(2) + )); + + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + members.put(memberA, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Arrays.asList(topic1Uuid, topic3Uuid), + Collections.emptyMap() + )); + members.put(memberB, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Arrays.asList(topic1Uuid, topic3Uuid), + Collections.emptyMap() + )); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata(topicMetadata); + + GroupAssignment computedAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + Map<String, Map<Uuid, Set<Integer>>> expectedAssignment = new HashMap<>(); + expectedAssignment.put(memberA, mkAssignment( + mkTopicAssignment(topic1Uuid, 0, 2), + mkTopicAssignment(topic3Uuid, 1) + )); + expectedAssignment.put(memberB, mkAssignment( + mkTopicAssignment(topic1Uuid, 1), + mkTopicAssignment(topic3Uuid, 0) + )); + + assertAssignment(expectedAssignment, computedAssignment); + checkValidityAndBalance(members, computedAssignment); + } + + @Test + public void testFirstAssignmentTwoMembersSubscribedToTwoTopicsNoPartitionRacks() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + topicMetadata.put(topic1Uuid, new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + Collections.emptyMap() + )); + topicMetadata.put(topic3Uuid, new TopicMetadata( + topic3Uuid, + topic3Name, + 2, + Collections.emptyMap() + )); + + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + members.put(memberA, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack1"), + Arrays.asList(topic1Uuid, topic3Uuid), + Collections.emptyMap() + )); + members.put(memberB, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack2"), + Arrays.asList(topic1Uuid, topic3Uuid), + Collections.emptyMap() + )); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata(topicMetadata); + + GroupAssignment computedAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + Map<String, Map<Uuid, Set<Integer>>> expectedAssignment = new HashMap<>(); + expectedAssignment.put(memberA, mkAssignment( + mkTopicAssignment(topic1Uuid, 0, 2), + mkTopicAssignment(topic3Uuid, 1) + )); + expectedAssignment.put(memberB, mkAssignment( + mkTopicAssignment(topic1Uuid, 1), + mkTopicAssignment(topic3Uuid, 0) + )); + + assertAssignment(expectedAssignment, computedAssignment); + checkValidityAndBalance(members, computedAssignment); + } + + @Test + public void testFirstAssignmentThreeMembersSubscribedToThreeTopicsWithMemberAndPartitionRacks() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + topicMetadata.put(topic1Uuid, new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + mkMapOfPartitionRacks(3) + )); + topicMetadata.put(topic2Uuid, new TopicMetadata( + topic2Uuid, + topic2Name, + 3, + mkMapOfPartitionRacks(3) + )); + topicMetadata.put(topic3Uuid, new TopicMetadata( + topic3Uuid, + topic3Name, + 2, + mkMapOfPartitionRacks(2) + )); + + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + members.put(memberA, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack1"), + Arrays.asList(topic1Uuid, topic2Uuid, topic3Uuid), + Collections.emptyMap() + )); + members.put(memberB, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack2"), + Arrays.asList(topic1Uuid, topic2Uuid, topic3Uuid), + Collections.emptyMap() + )); + members.put(memberC, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack3"), + Arrays.asList(topic1Uuid, topic2Uuid, topic3Uuid), + Collections.emptyMap() + )); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata(topicMetadata); + + GroupAssignment computedAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + Map<String, Map<Uuid, Set<Integer>>> expectedAssignment = new HashMap<>(); + expectedAssignment.put(memberA, mkAssignment( + mkTopicAssignment(topic1Uuid, 0), + mkTopicAssignment(topic2Uuid, 0), + mkTopicAssignment(topic3Uuid, 0) + )); + expectedAssignment.put(memberB, mkAssignment( + mkTopicAssignment(topic1Uuid, 1), + mkTopicAssignment(topic2Uuid, 1), + mkTopicAssignment(topic3Uuid, 1) + )); + expectedAssignment.put(memberC, mkAssignment( + mkTopicAssignment(topic1Uuid, 2), + mkTopicAssignment(topic2Uuid, 2) + )); + + assertAssignment(expectedAssignment, computedAssignment); + checkValidityAndBalance(members, computedAssignment); + } + + @Test + public void testFirstAssignmentThreeMembersSubscribedToThreeTopicsWithSomeMemberAndPartitionRacks() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + topicMetadata.put(topic1Uuid, new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + mkMapOfPartitionRacks(3) + )); + topicMetadata.put(topic2Uuid, new TopicMetadata( + topic2Uuid, + topic2Name, + 3, + mkMapOfPartitionRacks(3) + )); + topicMetadata.put(topic3Uuid, new TopicMetadata( + topic3Uuid, + topic3Name, + 2, + Collections.emptyMap() + )); + + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + members.put(memberA, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Arrays.asList(topic1Uuid, topic2Uuid, topic3Uuid), + Collections.emptyMap() + )); + members.put(memberB, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack2"), + Arrays.asList(topic1Uuid, topic2Uuid, topic3Uuid), + Collections.emptyMap() + )); + members.put(memberC, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Arrays.asList(topic1Uuid, topic2Uuid, topic3Uuid), + Collections.emptyMap() + )); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata(topicMetadata); + + GroupAssignment computedAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + Map<String, Map<Uuid, Set<Integer>>> expectedAssignment = new HashMap<>(); + expectedAssignment.put(memberA, mkAssignment( + mkTopicAssignment(topic1Uuid, 0), + mkTopicAssignment(topic2Uuid, 2), + mkTopicAssignment(topic3Uuid, 1) + )); + expectedAssignment.put(memberB, mkAssignment( + mkTopicAssignment(topic1Uuid, 1, 2), + mkTopicAssignment(topic2Uuid, 1) + )); + expectedAssignment.put(memberC, mkAssignment( + mkTopicAssignment(topic2Uuid, 0), + mkTopicAssignment(topic3Uuid, 0) + )); + + assertAssignment(expectedAssignment, computedAssignment); + checkValidityAndBalance(members, computedAssignment); + } + + @Test + public void testFirstAssignmentNumMembersGreaterThanTotalNumPartitions() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + topicMetadata.put(topic3Uuid, new TopicMetadata( + topic3Uuid, + topic3Name, + 2, + mkMapOfPartitionRacks(2) + )); + + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + members.put(memberA, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Collections.singletonList(topic3Uuid), + Collections.emptyMap() + )); + members.put(memberB, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Collections.singletonList(topic3Uuid), + Collections.emptyMap() + )); + members.put(memberC, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Collections.singletonList(topic3Uuid), + Collections.emptyMap() + )); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata(topicMetadata); + + GroupAssignment computedAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + // Topic 3 has 2 partitions but three members subscribed to it - one of them should not get an assignment. + Map<String, Map<Uuid, Set<Integer>>> expectedAssignment = new HashMap<>(); + expectedAssignment.put(memberA, mkAssignment( + mkTopicAssignment(topic3Uuid, 0) + )); + expectedAssignment.put(memberB, mkAssignment( + mkTopicAssignment(topic3Uuid, 1) + )); + expectedAssignment.put(memberC, + Collections.emptyMap() + ); + + assertAssignment(expectedAssignment, computedAssignment); + checkValidityAndBalance(members, computedAssignment); + } + + @Test + public void testSameSubscriptionsLargeSampleSet() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + for (int i = 1; i < 100; i++) { + Uuid topicId = Uuid.randomUuid(); + topicMetadata.put(topicId, new TopicMetadata( + topicId, + "topic-" + i, + 3, + mkMapOfPartitionRacks(3) + )); + } + + List<Uuid> subscribedTopics = new ArrayList<>(topicMetadata.keySet()); + + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + for (int i = 1; i < 50; i++) { + members.put("member" + i, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + subscribedTopics, + Collections.emptyMap() + )); + } + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata(topicMetadata); + + GroupAssignment computedAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + checkValidityAndBalance(members, computedAssignment); + } + + @Test + public void testReassignmentForTwoMembersTwoTopicsGivenUnbalancedPrevAssignment() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + topicMetadata.put(topic1Uuid, new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + mkMapOfPartitionRacks(3) + )); + topicMetadata.put(topic2Uuid, new TopicMetadata( + topic2Uuid, + topic2Name, + 3, + mkMapOfPartitionRacks(3) + )); + + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + + Map<Uuid, Set<Integer>> currentAssignmentForA = new TreeMap<>( + mkAssignment( + mkTopicAssignment(topic1Uuid, 0, 1), + mkTopicAssignment(topic2Uuid, 0, 1) + ) + ); + members.put(memberA, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Arrays.asList(topic1Uuid, topic2Uuid), + currentAssignmentForA + )); + + Map<Uuid, Set<Integer>> currentAssignmentForB = new TreeMap<>( + mkAssignment( + mkTopicAssignment(topic1Uuid, 2), + mkTopicAssignment(topic2Uuid, 2) + ) + ); + members.put(memberB, new AssignmentMemberSpec( + Optional.empty(), + Optional.empty(), + Arrays.asList(topic1Uuid, topic2Uuid), + currentAssignmentForB + )); + + AssignmentSpec assignmentSpec = new AssignmentSpec(members); + SubscribedTopicMetadata subscribedTopicMetadata = new SubscribedTopicMetadata(topicMetadata); + + GroupAssignment computedAssignment = assignor.assign(assignmentSpec, subscribedTopicMetadata); + + Map<String, Map<Uuid, Set<Integer>>> expectedAssignment = new HashMap<>(); + expectedAssignment.put(memberA, mkAssignment( + mkTopicAssignment(topic1Uuid, 0, 1), + mkTopicAssignment(topic2Uuid, 0) + )); + expectedAssignment.put(memberB, mkAssignment( + mkTopicAssignment(topic1Uuid, 2), + mkTopicAssignment(topic2Uuid, 1, 2) + )); + + assertAssignment(expectedAssignment, computedAssignment); + checkValidityAndBalance(members, computedAssignment); + } + + @Test + public void testReassignmentOnRackChangesWithMemberAndPartitionRacks() { + Map<Uuid, TopicMetadata> topicMetadata = new HashMap<>(); + topicMetadata.put(topic1Uuid, new TopicMetadata( + topic1Uuid, + topic1Name, + 3, + mkMapOfPartitionRacks(3) + )); + topicMetadata.put(topic2Uuid, new TopicMetadata( + topic2Uuid, + topic2Name, + 3, + mkMapOfPartitionRacks(3) + )); + + // Initially A was in rack1 and B was in rack 2, now let's switch them. + Map<String, AssignmentMemberSpec> members = new TreeMap<>(); + + Map<Uuid, Set<Integer>> currentAssignmentForA = new TreeMap<>( + mkAssignment( + mkTopicAssignment(topic1Uuid, 0), + mkTopicAssignment(topic2Uuid, 0, 2) + ) + ); + members.put(memberA, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack2"), + Arrays.asList(topic1Uuid, topic2Uuid), + currentAssignmentForA + )); + + Map<Uuid, Set<Integer>> currentAssignmentForB = new TreeMap<>( + mkAssignment( + mkTopicAssignment(topic1Uuid, 1, 2), + mkTopicAssignment(topic2Uuid, 1) + ) + ); + members.put(memberB, new AssignmentMemberSpec( + Optional.empty(), + Optional.of("rack1"), + Arrays.asList(topic1Uuid, topic2Uuid), + currentAssignmentForB + )); Review Comment: Changed the current assignment to what this assignor would've returned -- 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