Search in sources :

Example 1 with ManualPartitioner

use of org.apache.storm.kafka.spout.subscription.ManualPartitioner in project storm by apache.

the class KafkaTridentSpoutEmitterPartitioningTest method testGetPartitionsForTask.

@Test
public void testGetPartitionsForTask() {
    // Verify correct wrapping/unwrapping of partition and delegation of partition assignment
    ManualPartitioner partitionerMock = mock(ManualPartitioner.class);
    when(partitionerMock.getPartitionsForThisTask(any(), any())).thenAnswer(invocation -> {
        List<TopicPartition> partitions = new ArrayList<>(invocation.getArgument(0));
        partitions.remove(0);
        return new HashSet<>(partitions);
    });
    KafkaTridentSpoutEmitter<String, String> emitter = new KafkaTridentSpoutEmitter<>(SingleTopicKafkaTridentSpoutConfiguration.createKafkaSpoutConfigBuilder(mock(TopicFilter.class), partitionerMock, -1).build(), topologyContextMock, config -> consumer, new TopicAssigner());
    List<KafkaTridentSpoutTopicPartition> allPartitions = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        allPartitions.add(new KafkaTridentSpoutTopicPartition(SingleTopicKafkaSpoutConfiguration.TOPIC, i));
    }
    List<TopicPartition> unwrappedPartitions = allPartitions.stream().map(kttp -> kttp.getTopicPartition()).collect(Collectors.toList());
    List<KafkaTridentSpoutTopicPartition> partitionsForTask = emitter.getPartitionsForTask(0, 2, allPartitions);
    verify(partitionerMock).getPartitionsForThisTask(eq(unwrappedPartitions), any(TopologyContext.class));
    allPartitions.remove(0);
    assertThat("Should have assigned all except the first partition to this task", new HashSet<>(partitionsForTask), is(new HashSet<>(allPartitions)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SingleTopicKafkaSpoutConfiguration(org.apache.storm.kafka.spout.config.builder.SingleTopicKafkaSpoutConfiguration) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) ManualPartitioner(org.apache.storm.kafka.spout.subscription.ManualPartitioner) Mock(org.mockito.Mock) TopologyContext(org.apache.storm.task.TopologyContext) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) TopicPartition(org.apache.kafka.common.TopicPartition) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) List(java.util.List) TopicFilter(org.apache.storm.kafka.spout.subscription.TopicFilter) Matchers.is(org.hamcrest.Matchers.is) SingleTopicKafkaTridentSpoutConfiguration(org.apache.storm.kafka.spout.trident.config.builder.SingleTopicKafkaTridentSpoutConfiguration) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) TopicAssigner(org.apache.storm.kafka.spout.subscription.TopicAssigner) TopicFilter(org.apache.storm.kafka.spout.subscription.TopicFilter) ArrayList(java.util.ArrayList) ManualPartitioner(org.apache.storm.kafka.spout.subscription.ManualPartitioner) TopicPartition(org.apache.kafka.common.TopicPartition) TopicAssigner(org.apache.storm.kafka.spout.subscription.TopicAssigner) TopologyContext(org.apache.storm.task.TopologyContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with ManualPartitioner

use of org.apache.storm.kafka.spout.subscription.ManualPartitioner in project storm by apache.

the class SpoutWithMockedConsumerSetupHelper method setupSpout.

/**
 * Creates, opens and activates a KafkaSpout using a mocked consumer. The TopicFilter and ManualPartitioner should be mock objects,
 * since this method shortcircuits the TopicPartition assignment process and just calls onPartitionsAssigned on the rebalance listener.
 *
 * @param <K> The Kafka key type
 * @param <V> The Kafka value type
 * @param spoutConfig The spout config to use
 * @param topoConf The topo conf to pass to the spout
 * @param contextMock The topo context to pass to the spout
 * @param collectorMock The mocked collector to pass to the spout
 * @param consumerMock The mocked consumer
 * @param assignedPartitions The partitions to assign to this spout. The consumer will act like these partitions are assigned to it.
 * @return The spout
 */
public static <K, V> KafkaSpout<K, V> setupSpout(KafkaSpoutConfig<K, V> spoutConfig, Map<String, Object> topoConf, TopologyContext contextMock, SpoutOutputCollector collectorMock, KafkaConsumer<K, V> consumerMock, TopicPartition... assignedPartitions) {
    TopicFilter topicFilter = spoutConfig.getTopicFilter();
    ManualPartitioner topicPartitioner = spoutConfig.getTopicPartitioner();
    if (!mockingDetails(topicFilter).isMock() || !mockingDetails(topicPartitioner).isMock()) {
        throw new IllegalStateException("Use a mocked TopicFilter and a mocked ManualPartitioner when using this method, it helps avoid complex stubbing");
    }
    Set<TopicPartition> assignedPartitionsSet = new HashSet<>(Arrays.asList(assignedPartitions));
    TopicAssigner assigner = mock(TopicAssigner.class);
    doAnswer(invocation -> {
        ConsumerRebalanceListener listener = invocation.getArgument(2);
        listener.onPartitionsAssigned(assignedPartitionsSet);
        return null;
    }).when(assigner).assignPartitions(any(), any(), any());
    when(consumerMock.assignment()).thenReturn(assignedPartitionsSet);
    ConsumerFactory<K, V> consumerFactory = (kafkaSpoutConfig) -> consumerMock;
    KafkaSpout<K, V> spout = new KafkaSpout<>(spoutConfig, consumerFactory, assigner);
    spout.open(topoConf, contextMock, collectorMock);
    spout.activate();
    return spout;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mockito.mockingDetails(org.mockito.Mockito.mockingDetails) ManualPartitioner(org.apache.storm.kafka.spout.subscription.ManualPartitioner) TopologyContext(org.apache.storm.task.TopologyContext) HashMap(java.util.HashMap) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) TopicPartition(org.apache.kafka.common.TopicPartition) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Mockito.verify(org.mockito.Mockito.verify) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) List(java.util.List) TopicFilter(org.apache.storm.kafka.spout.subscription.TopicFilter) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Entry(java.util.Map.Entry) ConsumerFactory(org.apache.storm.kafka.spout.internal.ConsumerFactory) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) TopicAssigner(org.apache.storm.kafka.spout.subscription.TopicAssigner) TopicFilter(org.apache.storm.kafka.spout.subscription.TopicFilter) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) ManualPartitioner(org.apache.storm.kafka.spout.subscription.ManualPartitioner) TopicPartition(org.apache.kafka.common.TopicPartition) TopicAssigner(org.apache.storm.kafka.spout.subscription.TopicAssigner) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 ConsumerRebalanceListener (org.apache.kafka.clients.consumer.ConsumerRebalanceListener)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 ManualPartitioner (org.apache.storm.kafka.spout.subscription.ManualPartitioner)2 TopicAssigner (org.apache.storm.kafka.spout.subscription.TopicAssigner)2 TopicFilter (org.apache.storm.kafka.spout.subscription.TopicFilter)2 TopologyContext (org.apache.storm.task.TopologyContext)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.when (org.mockito.Mockito.when)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 Collectors (java.util.stream.Collectors)1