Search in sources :

Example 1 with KafkaPartitionSplit

use of org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit in project flink by apache.

the class KafkaSourceTestEnv method getSplitsByOwners.

// ------------------- topic information helpers -------------------
public static Map<Integer, Map<String, KafkaPartitionSplit>> getSplitsByOwners(final Collection<String> topics, final int numSubtasks) {
    final Map<Integer, Map<String, KafkaPartitionSplit>> splitsByOwners = new HashMap<>();
    for (String topic : topics) {
        getPartitionsForTopic(topic).forEach(tp -> {
            int ownerReader = Math.abs(tp.hashCode()) % numSubtasks;
            KafkaPartitionSplit split = new KafkaPartitionSplit(tp, getEarliestOffset(tp), (long) NUM_RECORDS_PER_PARTITION);
            splitsByOwners.computeIfAbsent(ownerReader, r -> new HashMap<>()).put(KafkaPartitionSplit.toSplitId(tp), split);
        });
    }
    return splitsByOwners;
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaTestBase(org.apache.flink.streaming.connectors.kafka.KafkaTestBase) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Properties(java.util.Properties) ListConsumerGroupOffsetsOptions(org.apache.kafka.clients.admin.ListConsumerGroupOffsetsOptions) Collection(java.util.Collection) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) RecordsToDelete(org.apache.kafka.clients.admin.RecordsToDelete) ArrayList(java.util.ArrayList) AdminClient(org.apache.kafka.clients.admin.AdminClient) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Map(java.util.Map) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) Assert.assertEquals(org.junit.Assert.assertEquals) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with KafkaPartitionSplit

use of org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit in project flink by apache.

the class KafkaEnumeratorTest method testKafkaClientProperties.

@Test
public void testKafkaClientProperties() throws Exception {
    Properties properties = new Properties();
    String clientIdPrefix = "test-prefix";
    Integer defaultTimeoutMs = 99999;
    properties.setProperty(KafkaSourceOptions.CLIENT_ID_PREFIX.key(), clientIdPrefix);
    properties.setProperty(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG, String.valueOf(defaultTimeoutMs));
    try (MockSplitEnumeratorContext<KafkaPartitionSplit> context = new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
        KafkaSourceEnumerator enumerator = createEnumerator(context, ENABLE_PERIODIC_PARTITION_DISCOVERY, PRE_EXISTING_TOPICS, Collections.emptySet(), properties)) {
        enumerator.start();
        AdminClient adminClient = (AdminClient) Whitebox.getInternalState(enumerator, "adminClient");
        assertNotNull(adminClient);
        String clientId = (String) Whitebox.getInternalState(adminClient, "clientId");
        assertNotNull(clientId);
        assertTrue(clientId.startsWith(clientIdPrefix));
        assertEquals(defaultTimeoutMs, Whitebox.getInternalState(adminClient, "defaultApiTimeoutMs"));
        assertNotNull(clientId);
        assertTrue(clientId.startsWith(clientIdPrefix));
    }
}
Also used : KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) MockSplitEnumeratorContext(org.apache.flink.api.connector.source.mocks.MockSplitEnumeratorContext) Properties(java.util.Properties) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Example 3 with KafkaPartitionSplit

use of org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit in project flink by apache.

the class KafkaEnumeratorTest method verifyAssignments.

private void verifyAssignments(Map<Integer, Set<TopicPartition>> expectedAssignments, Map<Integer, List<KafkaPartitionSplit>> actualAssignments) {
    actualAssignments.forEach((reader, splits) -> {
        Set<TopicPartition> expectedAssignmentsForReader = expectedAssignments.get(reader);
        assertNotNull(expectedAssignmentsForReader);
        assertEquals(expectedAssignmentsForReader.size(), splits.size());
        for (KafkaPartitionSplit split : splits) {
            assertTrue(expectedAssignmentsForReader.contains(split.getTopicPartition()));
        }
    });
}
Also used : KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) TopicPartition(org.apache.kafka.common.TopicPartition)

Example 4 with KafkaPartitionSplit

use of org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit in project flink by apache.

the class KafkaEnumeratorTest method testWorkWithPreexistingAssignments.

@Test
public void testWorkWithPreexistingAssignments() throws Throwable {
    Set<TopicPartition> preexistingAssignments;
    try (MockSplitEnumeratorContext<KafkaPartitionSplit> context1 = new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
        KafkaSourceEnumerator enumerator = createEnumerator(context1, ENABLE_PERIODIC_PARTITION_DISCOVERY)) {
        startEnumeratorAndRegisterReaders(context1, enumerator);
        preexistingAssignments = asEnumState(context1.getSplitsAssignmentSequence().get(0).assignment());
    }
    try (MockSplitEnumeratorContext<KafkaPartitionSplit> context2 = new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
        KafkaSourceEnumerator enumerator = createEnumerator(context2, ENABLE_PERIODIC_PARTITION_DISCOVERY, PRE_EXISTING_TOPICS, preexistingAssignments, new Properties())) {
        enumerator.start();
        runPeriodicPartitionDiscovery(context2);
        registerReader(context2, enumerator, READER0);
        assertTrue(context2.getSplitsAssignmentSequence().isEmpty());
        registerReader(context2, enumerator, READER1);
        verifyLastReadersAssignments(context2, Collections.singleton(READER1), PRE_EXISTING_TOPICS, 1);
    }
}
Also used : KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) MockSplitEnumeratorContext(org.apache.flink.api.connector.source.mocks.MockSplitEnumeratorContext) TopicPartition(org.apache.kafka.common.TopicPartition) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with KafkaPartitionSplit

use of org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit in project flink by apache.

the class KafkaEnumeratorTest method testPartitionChangeChecking.

@Test
public void testPartitionChangeChecking() throws Throwable {
    try (MockSplitEnumeratorContext<KafkaPartitionSplit> context = new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
        KafkaSourceEnumerator enumerator = createEnumerator(context, DISABLE_PERIODIC_PARTITION_DISCOVERY)) {
        enumerator.start();
        runOneTimePartitionDiscovery(context);
        registerReader(context, enumerator, READER0);
        verifyLastReadersAssignments(context, Collections.singleton(READER0), PRE_EXISTING_TOPICS, 1);
        // All partitions of TOPIC1 and TOPIC2 should have been discovered now
        // Check partition change using only DYNAMIC_TOPIC_NAME-0
        TopicPartition newPartition = new TopicPartition(DYNAMIC_TOPIC_NAME, 0);
        Set<TopicPartition> fetchedPartitions = new HashSet<>();
        fetchedPartitions.add(newPartition);
        final KafkaSourceEnumerator.PartitionChange partitionChange = enumerator.getPartitionChange(fetchedPartitions);
        // Since enumerator never met DYNAMIC_TOPIC_NAME-0, it should be mark as a new partition
        Set<TopicPartition> expectedNewPartitions = Collections.singleton(newPartition);
        // All existing topics are not in the fetchedPartitions, so they should be marked as
        // removed
        Set<TopicPartition> expectedRemovedPartitions = new HashSet<>();
        for (int i = 0; i < KafkaSourceTestEnv.NUM_PARTITIONS; i++) {
            expectedRemovedPartitions.add(new TopicPartition(TOPIC1, i));
            expectedRemovedPartitions.add(new TopicPartition(TOPIC2, i));
        }
        assertEquals(expectedNewPartitions, partitionChange.getNewPartitions());
        assertEquals(expectedRemovedPartitions, partitionChange.getRemovedPartitions());
    }
}
Also used : KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) MockSplitEnumeratorContext(org.apache.flink.api.connector.source.mocks.MockSplitEnumeratorContext) TopicPartition(org.apache.kafka.common.TopicPartition) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

KafkaPartitionSplit (org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit)25 TopicPartition (org.apache.kafka.common.TopicPartition)20 Properties (java.util.Properties)10 Test (org.junit.jupiter.api.Test)9 HashSet (java.util.HashSet)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 AdminClient (org.apache.kafka.clients.admin.AdminClient)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Test (org.junit.Test)4 Collections (java.util.Collections)3 List (java.util.List)3 Set (java.util.Set)3 Consumer (java.util.function.Consumer)3 MockSplitEnumeratorContext (org.apache.flink.api.connector.source.mocks.MockSplitEnumeratorContext)3 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)3 Arrays (java.util.Arrays)2 Optional (java.util.Optional)2