Search in sources :

Example 6 with ConsumerRebalanceListener

use of org.apache.kafka.clients.consumer.ConsumerRebalanceListener 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) {
    //Setup spout with mock consumer so we can get at the rebalance listener
    spout.open(conf, contextMock, collectorMock);
    spout.activate();
    ArgumentCaptor<ConsumerRebalanceListener> rebalanceListenerCapture = ArgumentCaptor.forClass(ConsumerRebalanceListener.class);
    verify(consumerMock).subscribe(anyCollection(), rebalanceListenerCapture.capture());
    //Assign partitions to the spout
    ConsumerRebalanceListener consumerRebalanceListener = rebalanceListenerCapture.getValue();
    List<TopicPartition> assignedPartitions = new ArrayList<>();
    assignedPartitions.add(partitionThatWillBeRevoked);
    assignedPartitions.add(assignedPartition);
    consumerRebalanceListener.onPartitionsAssigned(assignedPartitions);
    //Make the consumer return a single message for each partition
    Map<TopicPartition, List<ConsumerRecord<String, String>>> firstPartitionRecords = new HashMap<>();
    firstPartitionRecords.put(partitionThatWillBeRevoked, Collections.singletonList(new ConsumerRecord(partitionThatWillBeRevoked.topic(), partitionThatWillBeRevoked.partition(), 0L, "key", "value")));
    Map<TopicPartition, List<ConsumerRecord<String, String>>> secondPartitionRecords = new HashMap<>();
    secondPartitionRecords.put(assignedPartition, Collections.singletonList(new ConsumerRecord(assignedPartition.topic(), assignedPartition.partition(), 0L, "key", "value")));
    when(consumerMock.poll(anyLong())).thenReturn(new ConsumerRecords(firstPartitionRecords)).thenReturn(new ConsumerRecords(secondPartitionRecords)).thenReturn(new ConsumerRecords(Collections.emptyMap()));
    //Emit the messages
    spout.nextTuple();
    ArgumentCaptor<KafkaSpoutMessageId> messageIdForRevokedPartition = ArgumentCaptor.forClass(KafkaSpoutMessageId.class);
    verify(collectorMock).emit(anyObject(), anyObject(), messageIdForRevokedPartition.capture());
    reset(collectorMock);
    spout.nextTuple();
    ArgumentCaptor<KafkaSpoutMessageId> messageIdForAssignedPartition = ArgumentCaptor.forClass(KafkaSpoutMessageId.class);
    verify(collectorMock).emit(anyObject(), anyObject(), messageIdForAssignedPartition.capture());
    //Now rebalance
    consumerRebalanceListener.onPartitionsRevoked(assignedPartitions);
    consumerRebalanceListener.onPartitionsAssigned(Collections.singleton(assignedPartition));
    List<KafkaSpoutMessageId> emittedMessageIds = new ArrayList<>();
    emittedMessageIds.add(messageIdForRevokedPartition.getValue());
    emittedMessageIds.add(messageIdForAssignedPartition.getValue());
    return emittedMessageIds;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ConsumerRebalanceListener (org.apache.kafka.clients.consumer.ConsumerRebalanceListener)6 TopicPartition (org.apache.kafka.common.TopicPartition)6 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 StreamsConfig (org.apache.kafka.streams.StreamsConfig)3 StreamsMetrics (org.apache.kafka.streams.StreamsMetrics)3 TaskId (org.apache.kafka.streams.processor.TaskId)3 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)3 MockClientSupplier (org.apache.kafka.test.MockClientSupplier)3 Test (org.junit.Test)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 CommitFailedException (org.apache.kafka.clients.consumer.CommitFailedException)2 RetriableCommitFailedException (org.apache.kafka.clients.consumer.RetriableCommitFailedException)2 KafkaException (org.apache.kafka.common.KafkaException)2 GroupAuthorizationException (org.apache.kafka.common.errors.GroupAuthorizationException)2 InterruptException (org.apache.kafka.common.errors.InterruptException)2 RetriableException (org.apache.kafka.common.errors.RetriableException)2