Search in sources :

Example 6 with ConsumerPartitionAssignor

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor in project kafka by apache.

the class ConsumerCoordinatorTest method testPerformAssignmentShouldSkipValidateCooperativeAssignmentForBuiltInCooperativeStickyAssignor.

@Test
public void testPerformAssignmentShouldSkipValidateCooperativeAssignmentForBuiltInCooperativeStickyAssignor() {
    SubscriptionState mockSubscriptionState = Mockito.mock(SubscriptionState.class);
    List<JoinGroupResponseData.JoinGroupResponseMember> metadata = validateCooperativeAssignmentTestSetup();
    List<ConsumerPartitionAssignor> assignorsWithCooperativeStickyAssignor = new ArrayList<>(assignors);
    // create a mockPartitionAssignor with the same name as cooperative sticky assignor
    MockPartitionAssignor mockCooperativeStickyAssignor = new MockPartitionAssignor(Collections.singletonList(protocol)) {

        @Override
        public String name() {
            return COOPERATIVE_STICKY_ASSIGNOR_NAME;
        }
    };
    assignorsWithCooperativeStickyAssignor.add(mockCooperativeStickyAssignor);
    // simulate the cooperative sticky assignor do the assignment with out-of-date ownedPartition
    Map<String, List<TopicPartition>> assignment = new HashMap<>();
    assignment.put(consumerId, Arrays.asList(t1p));
    assignment.put(consumerId2, Arrays.asList(t2p));
    mockCooperativeStickyAssignor.prepare(assignment);
    try (ConsumerCoordinator coordinator = buildCoordinator(rebalanceConfig, new Metrics(), assignorsWithCooperativeStickyAssignor, false, mockSubscriptionState)) {
        // should not validate assignment for built-in cooperative sticky assignor
        coordinator.onLeaderElected("1", mockCooperativeStickyAssignor.name(), metadata, false);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Metrics(org.apache.kafka.common.metrics.Metrics) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ConsumerPartitionAssignor(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor) Test(org.junit.jupiter.api.Test)

Example 7 with ConsumerPartitionAssignor

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor in project kafka by apache.

the class ConsumerCoordinatorTest method testOnLeaderElectedShouldSkipAssignment.

@Test
public void testOnLeaderElectedShouldSkipAssignment() {
    SubscriptionState mockSubscriptionState = Mockito.mock(SubscriptionState.class);
    ConsumerPartitionAssignor assignor = Mockito.mock(ConsumerPartitionAssignor.class);
    String assignorName = "mock-assignor";
    Mockito.when(assignor.name()).thenReturn(assignorName);
    Mockito.when(assignor.supportedProtocols()).thenReturn(Collections.singletonList(protocol));
    Map<String, List<String>> memberSubscriptions = singletonMap(consumerId, singletonList(topic1));
    List<JoinGroupResponseData.JoinGroupResponseMember> metadata = new ArrayList<>();
    for (Map.Entry<String, List<String>> subscriptionEntry : memberSubscriptions.entrySet()) {
        ConsumerPartitionAssignor.Subscription subscription = new ConsumerPartitionAssignor.Subscription(subscriptionEntry.getValue());
        ByteBuffer buf = ConsumerProtocol.serializeSubscription(subscription);
        metadata.add(new JoinGroupResponseData.JoinGroupResponseMember().setMemberId(subscriptionEntry.getKey()).setMetadata(buf.array()));
    }
    try (ConsumerCoordinator coordinator = buildCoordinator(rebalanceConfig, new Metrics(), Collections.singletonList(assignor), false, mockSubscriptionState)) {
        assertEquals(Collections.emptyMap(), coordinator.onLeaderElected("1", assignorName, metadata, true));
        assertTrue(coordinator.isLeader());
    }
    Mockito.verify(assignor, Mockito.never()).assign(Mockito.any(), Mockito.any());
}
Also used : JoinGroupResponseData(org.apache.kafka.common.message.JoinGroupResponseData) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Metrics(org.apache.kafka.common.metrics.Metrics) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ConsumerPartitionAssignor(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.jupiter.api.Test)

Aggregations

ConsumerPartitionAssignor (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor)7 ArrayList (java.util.ArrayList)5 List (java.util.List)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 Test (org.junit.jupiter.api.Test)3 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 Map (java.util.Map)2 Assignment (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment)2 GroupSubscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription)2 Subscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription)2 JoinGroupResponseData (org.apache.kafka.common.message.JoinGroupResponseData)2 Utils.mkMap (org.apache.kafka.common.utils.Utils.mkMap)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1