Search in sources :

Example 21 with ConsumerRecords

use of org.apache.kafka.clients.consumer.ConsumerRecords in project samza by apache.

the class TestZkStreamProcessorBase method verifyNumMessages.

/**
 * Consumes data from the topic until there are no new messages for a while
 * and asserts that the number of consumed messages is as expected.
 */
protected void verifyNumMessages(String topic, final Map<Integer, Boolean> expectedValues, int expectedNumMessages) {
    consumer.subscribe(Collections.singletonList(topic));
    Map<Integer, Boolean> map = new HashMap<>(expectedValues);
    int count = 0;
    int emptyPollCount = 0;
    while (count < expectedNumMessages && emptyPollCount < 5) {
        ConsumerRecords records = consumer.poll(5000);
        if (!records.isEmpty()) {
            Iterator<ConsumerRecord> iterator = records.iterator();
            while (iterator.hasNext()) {
                ConsumerRecord record = iterator.next();
                String val = new String((byte[]) record.value());
                LOG.info("Got value " + val + "; count = " + count + "; out of " + expectedNumMessages);
                Integer valI = Integer.valueOf(val);
                if (valI < BAD_MESSAGE_KEY) {
                    map.put(valI, true);
                    count++;
                }
            }
        } else {
            emptyPollCount++;
            LOG.warn("empty polls " + emptyPollCount);
        }
    }
    // filter out numbers we did not get
    long numFalse = map.values().stream().filter(v -> !v).count();
    Assert.assertEquals("didn't get this number of events ", 0, numFalse);
    Assert.assertEquals(expectedNumMessages, count);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ZkConnection(org.I0Itec.zkclient.ZkConnection) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) LoggerFactory(org.slf4j.LoggerFactory) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) TestZkUtils(org.apache.samza.zk.TestZkUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InitableTask(org.apache.samza.task.InitableTask) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Map(java.util.Map) StreamTask(org.apache.samza.task.StreamTask) ApplicationConfig(org.apache.samza.config.ApplicationConfig) StandaloneTestUtils(org.apache.samza.test.StandaloneTestUtils) MapConfig(org.apache.samza.config.MapConfig) Before(org.junit.Before) ZooKeeper(org.apache.zookeeper.ZooKeeper) Properties(java.util.Properties) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) TaskConfig(org.apache.samza.config.TaskConfig) ZkClient(org.I0Itec.zkclient.ZkClient) StreamTaskFactory(org.apache.samza.task.StreamTaskFactory) ZkConfig(org.apache.samza.config.ZkConfig) Field(java.lang.reflect.Field) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) TaskCoordinator(org.apache.samza.task.TaskCoordinator) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Context(org.apache.samza.context.Context) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ReflectionUtil(org.apache.samza.util.ReflectionUtil) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) IntegrationTestHarness(org.apache.samza.test.harness.IntegrationTestHarness) TestUtils(kafka.utils.TestUtils) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Collections(java.util.Collections) JobCoordinatorFactory(org.apache.samza.coordinator.JobCoordinatorFactory) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) HashMap(java.util.HashMap) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 22 with ConsumerRecords

use of org.apache.kafka.clients.consumer.ConsumerRecords in project samza by apache.

the class TestStreamProcessor method verifyNumMessages.

/**
 * Consumes data from the topic until there are no new messages for a while
 * and asserts that the number of consumed messages is as expected.
 */
@SuppressWarnings("unchecked")
private void verifyNumMessages(KafkaConsumer consumer, String topic, int expectedNumMessages) {
    consumer.subscribe(Collections.singletonList(topic));
    int count = 0;
    int emptyPollCount = 0;
    while (count < expectedNumMessages && emptyPollCount < 5) {
        ConsumerRecords records = consumer.poll(5000);
        if (!records.isEmpty()) {
            for (ConsumerRecord record : (Iterable<ConsumerRecord>) records) {
                Assert.assertEquals(new String((byte[]) record.value()), String.valueOf(count));
                count++;
            }
        } else {
            emptyPollCount++;
        }
    }
    Assert.assertEquals(count, expectedNumMessages);
}
Also used : ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 23 with ConsumerRecords

use of org.apache.kafka.clients.consumer.ConsumerRecords in project storm by apache.

the class KafkaSpoutRebalanceTest method emitOneMessagePerPartitionThenRevokeOnePartition.

// Returns messageIds in order of emission
private List<KafkaSpoutMessageId> emitOneMessagePerPartitionThenRevokeOnePartition(KafkaSpout<String, String> spout, TopicPartition partitionThatWillBeRevoked, TopicPartition assignedPartition, TopicAssigner topicAssigner) {
    // Setup spout with mock consumer so we can get at the rebalance listener
    spout.open(conf, contextMock, collectorMock);
    spout.activate();
    // Assign partitions to the spout
    ArgumentCaptor<ConsumerRebalanceListener> rebalanceListenerCapture = ArgumentCaptor.forClass(ConsumerRebalanceListener.class);
    verify(topicAssigner).assignPartitions(any(), any(), rebalanceListenerCapture.capture());
    ConsumerRebalanceListener consumerRebalanceListener = rebalanceListenerCapture.getValue();
    Set<TopicPartition> assignedPartitions = new HashSet<>();
    assignedPartitions.add(partitionThatWillBeRevoked);
    assignedPartitions.add(assignedPartition);
    consumerRebalanceListener.onPartitionsAssigned(assignedPartitions);
    when(consumerMock.assignment()).thenReturn(assignedPartitions);
    // Make the consumer return a single message for each partition
    when(consumerMock.poll(anyLong())).thenReturn(new ConsumerRecords<>(Collections.singletonMap(partitionThatWillBeRevoked, SpoutWithMockedConsumerSetupHelper.createRecords(partitionThatWillBeRevoked, 0, 1)))).thenReturn(new ConsumerRecords<>(Collections.singletonMap(assignedPartition, SpoutWithMockedConsumerSetupHelper.createRecords(assignedPartition, 0, 1)))).thenReturn(new ConsumerRecords<>(Collections.emptyMap()));
    // Emit the messages
    spout.nextTuple();
    ArgumentCaptor<KafkaSpoutMessageId> messageIdForRevokedPartition = ArgumentCaptor.forClass(KafkaSpoutMessageId.class);
    verify(collectorMock).emit(anyString(), anyList(), messageIdForRevokedPartition.capture());
    reset(collectorMock);
    spout.nextTuple();
    ArgumentCaptor<KafkaSpoutMessageId> messageIdForAssignedPartition = ArgumentCaptor.forClass(KafkaSpoutMessageId.class);
    verify(collectorMock).emit(anyString(), anyList(), messageIdForAssignedPartition.capture());
    // Now rebalance
    consumerRebalanceListener.onPartitionsRevoked(assignedPartitions);
    consumerRebalanceListener.onPartitionsAssigned(Collections.singleton(assignedPartition));
    when(consumerMock.assignment()).thenReturn(Collections.singleton(assignedPartition));
    List<KafkaSpoutMessageId> emittedMessageIds = new ArrayList<>();
    emittedMessageIds.add(messageIdForRevokedPartition.getValue());
    emittedMessageIds.add(messageIdForAssignedPartition.getValue());
    return emittedMessageIds;
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) HashSet(java.util.HashSet)

Example 24 with ConsumerRecords

use of org.apache.kafka.clients.consumer.ConsumerRecords in project storm by apache.

the class KafkaSpoutEmitTest method testSpoutWillSkipPartitionsAtTheMaxUncommittedOffsetsLimit.

@Test
public void testSpoutWillSkipPartitionsAtTheMaxUncommittedOffsetsLimit() {
    // This verifies that partitions can't prevent each other from retrying tuples due to the maxUncommittedOffsets limit.
    try (SimulatedTime simulatedTime = new SimulatedTime()) {
        TopicPartition partitionTwo = new TopicPartition(SingleTopicKafkaSpoutConfiguration.TOPIC, 2);
        KafkaSpout<String, String> spout = SpoutWithMockedConsumerSetupHelper.setupSpout(spoutConfig, conf, contextMock, collectorMock, consumerMock, partition, partitionTwo);
        Map<TopicPartition, List<ConsumerRecord<String, String>>> records = new HashMap<>();
        // This is cheating a bit since maxPollRecords would normally spread this across multiple polls
        records.put(partition, SpoutWithMockedConsumerSetupHelper.createRecords(partition, 0, spoutConfig.getMaxUncommittedOffsets()));
        records.put(partitionTwo, SpoutWithMockedConsumerSetupHelper.createRecords(partitionTwo, 0, spoutConfig.getMaxUncommittedOffsets() + 1));
        int numMessages = spoutConfig.getMaxUncommittedOffsets() * 2 + 1;
        when(consumerMock.poll(anyLong())).thenReturn(new ConsumerRecords<>(records));
        for (int i = 0; i < numMessages; i++) {
            spout.nextTuple();
        }
        ArgumentCaptor<KafkaSpoutMessageId> messageIds = ArgumentCaptor.forClass(KafkaSpoutMessageId.class);
        verify(collectorMock, times(numMessages)).emit(anyString(), anyList(), messageIds.capture());
        // Now fail a tuple on partition one and verify that it is allowed to retry, because the failed tuple is below the maxUncommittedOffsets limit
        Optional<KafkaSpoutMessageId> failedMessageIdPartitionOne = messageIds.getAllValues().stream().filter(messageId -> messageId.partition() == partition.partition()).findAny();
        spout.fail(failedMessageIdPartitionOne.get());
        // Also fail the last tuple from partition two. Since the failed tuple is beyond the maxUncommittedOffsets limit, it should not be retried until earlier messages are acked.
        Optional<KafkaSpoutMessageId> failedMessagePartitionTwo = messageIds.getAllValues().stream().filter(messageId -> messageId.partition() == partitionTwo.partition()).max((msgId, msgId2) -> (int) (msgId.offset() - msgId2.offset()));
        spout.fail(failedMessagePartitionTwo.get());
        reset(collectorMock);
        Time.advanceTime(50);
        when(consumerMock.poll(anyLong())).thenReturn(new ConsumerRecords<>(Collections.singletonMap(partition, SpoutWithMockedConsumerSetupHelper.createRecords(partition, failedMessageIdPartitionOne.get().offset(), 1))));
        spout.nextTuple();
        verify(collectorMock, times(1)).emit(anyObject(), anyObject(), anyObject());
        InOrder inOrder = inOrder(consumerMock);
        inOrder.verify(consumerMock).seek(partition, failedMessageIdPartitionOne.get().offset());
        // Should not seek on the paused partition
        inOrder.verify(consumerMock, never()).seek(eq(partitionTwo), anyLong());
        inOrder.verify(consumerMock).pause(Collections.singleton(partitionTwo));
        inOrder.verify(consumerMock).poll(anyLong());
        inOrder.verify(consumerMock).resume(Collections.singleton(partitionTwo));
        reset(collectorMock);
        // Now also check that no more tuples are polled for, since both partitions are at their limits
        spout.nextTuple();
        verify(collectorMock, never()).emit(anyObject(), anyObject(), anyObject());
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) SingleTopicKafkaSpoutConfiguration(org.apache.storm.kafka.spout.config.builder.SingleTopicKafkaSpoutConfiguration) SimulatedTime(org.apache.storm.utils.Time.SimulatedTime) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) 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) SingleTopicKafkaSpoutConfiguration.createKafkaSpoutConfigBuilder(org.apache.storm.kafka.spout.config.builder.SingleTopicKafkaSpoutConfiguration.createKafkaSpoutConfigBuilder) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) TopicPartition(org.apache.kafka.common.TopicPartition) InOrder(org.mockito.InOrder) ArgumentMatchers.anyObject(org.mockito.ArgumentMatchers.anyObject) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Mockito.verify(org.mockito.Mockito.verify) Time(org.apache.storm.utils.Time) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) List(java.util.List) TopicFilter(org.apache.storm.kafka.spout.subscription.TopicFilter) Mockito.inOrder(org.mockito.Mockito.inOrder) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Optional(java.util.Optional) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Mockito.reset(org.mockito.Mockito.reset) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) Mockito.mock(org.mockito.Mockito.mock) SimulatedTime(org.apache.storm.utils.Time.SimulatedTime) InOrder(org.mockito.InOrder) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 25 with ConsumerRecords

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

the class MockConsumerInterceptor method onConsume.

@Override
public ConsumerRecords<String, String> onConsume(ConsumerRecords<String, String> records) {
    // This will ensure that we get the cluster metadata when onConsume is called for the first time
    // as subsequent compareAndSet operations will fail.
    CLUSTER_ID_BEFORE_ON_CONSUME.compareAndSet(NO_CLUSTER_ID, CLUSTER_META.get());
    Map<TopicPartition, List<ConsumerRecord<String, String>>> recordMap = new HashMap<>();
    for (TopicPartition tp : records.partitions()) {
        List<ConsumerRecord<String, String>> lst = new ArrayList<>();
        for (ConsumerRecord<String, String> record : records.records(tp)) {
            lst.add(new ConsumerRecord<>(record.topic(), record.partition(), record.offset(), record.timestamp(), record.timestampType(), record.serializedKeySize(), record.serializedValueSize(), record.key(), record.value().toUpperCase(Locale.ROOT), new RecordHeaders(), Optional.empty()));
        }
        recordMap.put(tp, lst);
    }
    return new ConsumerRecords<>(recordMap);
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Aggregations

ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)71 TopicPartition (org.apache.kafka.common.TopicPartition)59 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)48 HashMap (java.util.HashMap)37 ArrayList (java.util.ArrayList)32 List (java.util.List)32 Test (org.junit.Test)32 Map (java.util.Map)21 Properties (java.util.Properties)16 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)16 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)16 Duration (java.time.Duration)15 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)12 Collection (java.util.Collection)11 Collections (java.util.Collections)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 Set (java.util.Set)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Collectors (java.util.stream.Collectors)9 HashSet (java.util.HashSet)8