Search in sources :

Example 6 with MemberInfo

use of org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo in project kafka by apache.

the class RangeAssignorTest method setUp.

@BeforeEach
public void setUp() {
    staticMemberInfos = new ArrayList<>();
    staticMemberInfos.add(new MemberInfo(consumer1, Optional.of(instance1)));
    staticMemberInfos.add(new MemberInfo(consumer2, Optional.of(instance2)));
    staticMemberInfos.add(new MemberInfo(consumer3, Optional.of(instance3)));
}
Also used : MemberInfo(org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with MemberInfo

use of org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo in project kafka by apache.

the class RangeAssignorTest method testStaticMemberRangeAssignmentPersistent.

@Test
public void testStaticMemberRangeAssignmentPersistent() {
    Map<String, Integer> partitionsPerTopic = setupPartitionsPerTopicWithTwoTopics(5, 4);
    Map<String, Subscription> consumers = new HashMap<>();
    for (MemberInfo m : staticMemberInfos) {
        Subscription subscription = new Subscription(topics(topic1, topic2), null, Collections.emptyList());
        subscription.setGroupInstanceId(m.groupInstanceId);
        consumers.put(m.memberId, subscription);
    }
    // Consumer 4 is a dynamic member.
    String consumer4 = "consumer4";
    consumers.put(consumer4, new Subscription(topics(topic1, topic2)));
    Map<String, List<TopicPartition>> expectedAssignment = new HashMap<>();
    // Have 3 static members instance1, instance2, instance3 to be persistent
    // across generations. Their assignment shall be the same.
    expectedAssignment.put(consumer1, partitions(tp(topic1, 0), tp(topic1, 1), tp(topic2, 0)));
    expectedAssignment.put(consumer2, partitions(tp(topic1, 2), tp(topic2, 1)));
    expectedAssignment.put(consumer3, partitions(tp(topic1, 3), tp(topic2, 2)));
    expectedAssignment.put(consumer4, partitions(tp(topic1, 4), tp(topic2, 3)));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, consumers);
    assertEquals(expectedAssignment, assignment);
    // Replace dynamic member 4 with a new dynamic member 5.
    consumers.remove(consumer4);
    String consumer5 = "consumer5";
    consumers.put(consumer5, new Subscription(topics(topic1, topic2)));
    expectedAssignment.remove(consumer4);
    expectedAssignment.put(consumer5, partitions(tp(topic1, 4), tp(topic2, 3)));
    assignment = assignor.assign(partitionsPerTopic, consumers);
    assertEquals(expectedAssignment, assignment);
}
Also used : MemberInfo(org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test)

Example 8 with MemberInfo

use of org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo in project kafka by apache.

the class RangeAssignorTest method testStaticMemberRangeAssignmentPersistentAfterMemberIdChanges.

@Test
public void testStaticMemberRangeAssignmentPersistentAfterMemberIdChanges() {
    Map<String, Integer> partitionsPerTopic = setupPartitionsPerTopicWithTwoTopics(5, 5);
    Map<String, Subscription> consumers = new HashMap<>();
    for (MemberInfo m : staticMemberInfos) {
        Subscription subscription = new Subscription(topics(topic1, topic2), null, Collections.emptyList());
        subscription.setGroupInstanceId(m.groupInstanceId);
        consumers.put(m.memberId, subscription);
    }
    Map<String, List<TopicPartition>> expectedInstanceAssignment = new HashMap<>();
    expectedInstanceAssignment.put(instance1, partitions(tp(topic1, 0), tp(topic1, 1), tp(topic2, 0), tp(topic2, 1)));
    expectedInstanceAssignment.put(instance2, partitions(tp(topic1, 2), tp(topic1, 3), tp(topic2, 2), tp(topic2, 3)));
    expectedInstanceAssignment.put(instance3, partitions(tp(topic1, 4), tp(topic2, 4)));
    Map<String, List<TopicPartition>> staticAssignment = checkStaticAssignment(assignor, partitionsPerTopic, consumers);
    assertEquals(expectedInstanceAssignment, staticAssignment);
    // Now switch the member.id fields for each member info, the assignment should
    // stay the same as last time.
    String consumer4 = "consumer4";
    String consumer5 = "consumer5";
    consumers.put(consumer4, consumers.get(consumer3));
    consumers.remove(consumer3);
    consumers.put(consumer5, consumers.get(consumer2));
    consumers.remove(consumer2);
    Map<String, List<TopicPartition>> newStaticAssignment = checkStaticAssignment(assignor, partitionsPerTopic, consumers);
    assertEquals(staticAssignment, newStaticAssignment);
}
Also used : MemberInfo(org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test)

Example 9 with MemberInfo

use of org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo in project kafka by apache.

the class RoundRobinAssignorTest method testStaticMemberRoundRobinAssignmentPersistent.

@Test
public void testStaticMemberRoundRobinAssignmentPersistent() {
    // Have 3 static members instance1, instance2, instance3 to be persistent
    // across generations. Their assignment shall be the same.
    String consumer1 = "consumer1";
    String instance1 = "instance1";
    String consumer2 = "consumer2";
    String instance2 = "instance2";
    String consumer3 = "consumer3";
    String instance3 = "instance3";
    List<MemberInfo> staticMemberInfos = new ArrayList<>();
    staticMemberInfos.add(new MemberInfo(consumer1, Optional.of(instance1)));
    staticMemberInfos.add(new MemberInfo(consumer2, Optional.of(instance2)));
    staticMemberInfos.add(new MemberInfo(consumer3, Optional.of(instance3)));
    // Consumer 4 is a dynamic member.
    String consumer4 = "consumer4";
    Map<String, Integer> partitionsPerTopic = setupPartitionsPerTopicWithTwoTopics(3, 3);
    Map<String, Subscription> consumers = new HashMap<>();
    for (MemberInfo m : staticMemberInfos) {
        Subscription subscription = new Subscription(topics(topic1, topic2), null);
        subscription.setGroupInstanceId(m.groupInstanceId);
        consumers.put(m.memberId, subscription);
    }
    consumers.put(consumer4, new Subscription(topics(topic1, topic2)));
    Map<String, List<TopicPartition>> expectedAssignment = new HashMap<>();
    expectedAssignment.put(consumer1, partitions(tp(topic1, 0), tp(topic2, 1)));
    expectedAssignment.put(consumer2, partitions(tp(topic1, 1), tp(topic2, 2)));
    expectedAssignment.put(consumer3, partitions(tp(topic1, 2)));
    expectedAssignment.put(consumer4, partitions(tp(topic2, 0)));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, consumers);
    assertEquals(expectedAssignment, assignment);
    // Replace dynamic member 4 with a new dynamic member 5.
    consumers.remove(consumer4);
    String consumer5 = "consumer5";
    consumers.put(consumer5, new Subscription(topics(topic1, topic2)));
    expectedAssignment.remove(consumer4);
    expectedAssignment.put(consumer5, partitions(tp(topic2, 0)));
    assignment = assignor.assign(partitionsPerTopic, consumers);
    assertEquals(expectedAssignment, assignment);
}
Also used : MemberInfo(org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

MemberInfo (org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor.MemberInfo)9 Test (org.junit.jupiter.api.Test)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Subscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription)4 Map (java.util.Map)1 Random (java.util.Random)1 AbstractPartitionAssignor (org.apache.kafka.clients.consumer.internals.AbstractPartitionAssignor)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1