Search in sources :

Example 36 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project kafka by apache.

the class DefaultPartitionGrouper method partitionGroups.

/**
     * Generate tasks with the assigned topic partitions.
     *
     * @param topicGroups   group of topics that need to be joined together
     * @param metadata      metadata of the consuming cluster
     * @return The map from generated task ids to the assigned partitions
     */
public Map<TaskId, Set<TopicPartition>> partitionGroups(Map<Integer, Set<String>> topicGroups, Cluster metadata) {
    Map<TaskId, Set<TopicPartition>> groups = new HashMap<>();
    for (Map.Entry<Integer, Set<String>> entry : topicGroups.entrySet()) {
        Integer topicGroupId = entry.getKey();
        Set<String> topicGroup = entry.getValue();
        int maxNumPartitions = maxNumPartitions(metadata, topicGroup);
        for (int partitionId = 0; partitionId < maxNumPartitions; partitionId++) {
            Set<TopicPartition> group = new HashSet<>(topicGroup.size());
            for (String topic : topicGroup) {
                List<PartitionInfo> partitions = metadata.partitionsForTopic(topic);
                if (partitionId < partitions.size()) {
                    group.add(new TopicPartition(topic, partitionId));
                }
            }
            groups.put(new TaskId(topicGroupId, partitionId), Collections.unmodifiableSet(group));
        }
    }
    return Collections.unmodifiableMap(groups);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionInfo(org.apache.kafka.common.PartitionInfo) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 37 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project storm by apache.

the class KafkaOffsetLagUtil method getOffsetLags.

/**
     *
     * @param newKafkaSpoutOffsetQuery represents the information needed to query kafka for log head and spout offsets
     * @return log head offset, spout offset and lag for each partition
     */
public static List<KafkaOffsetLagResult> getOffsetLags(NewKafkaSpoutOffsetQuery newKafkaSpoutOffsetQuery) {
    KafkaConsumer<String, String> consumer = null;
    List<KafkaOffsetLagResult> result = new ArrayList<>();
    try {
        Properties props = new Properties();
        props.put("bootstrap.servers", newKafkaSpoutOffsetQuery.getBootStrapBrokers());
        props.put("group.id", newKafkaSpoutOffsetQuery.getConsumerGroupId());
        props.put("enable.auto.commit", "false");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        if (newKafkaSpoutOffsetQuery.getSecurityProtocol() != null) {
            props.put("security.protocol", newKafkaSpoutOffsetQuery.getSecurityProtocol());
        }
        List<TopicPartition> topicPartitionList = new ArrayList<>();
        consumer = new KafkaConsumer<>(props);
        for (String topic : newKafkaSpoutOffsetQuery.getTopics().split(",")) {
            List<PartitionInfo> partitionInfoList = consumer.partitionsFor(topic);
            if (partitionInfoList != null) {
                for (PartitionInfo partitionInfo : partitionInfoList) {
                    topicPartitionList.add(new TopicPartition(partitionInfo.topic(), partitionInfo.partition()));
                }
            }
        }
        consumer.assign(topicPartitionList);
        for (TopicPartition topicPartition : topicPartitionList) {
            OffsetAndMetadata offsetAndMetadata = consumer.committed(topicPartition);
            long committedOffset = offsetAndMetadata != null ? offsetAndMetadata.offset() : -1;
            consumer.seekToEnd(toArrayList(topicPartition));
            result.add(new KafkaOffsetLagResult(topicPartition.topic(), topicPartition.partition(), committedOffset, consumer.position(topicPartition)));
        }
    } finally {
        if (consumer != null) {
            consumer.close();
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Properties(java.util.Properties) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) PartitionInfo(org.apache.kafka.common.PartitionInfo)

Example 38 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project storm by apache.

the class ManualPartitionNamedSubscription method refreshAssignment.

@Override
public void refreshAssignment() {
    List<TopicPartition> allPartitions = new ArrayList<>();
    for (String topic : topics) {
        for (PartitionInfo partitionInfo : consumer.partitionsFor(topic)) {
            allPartitions.add(new TopicPartition(partitionInfo.topic(), partitionInfo.partition()));
        }
    }
    Collections.sort(allPartitions, TopicPartitionComparator.INSTANCE);
    Set<TopicPartition> newAssignment = new HashSet<>(partitioner.partition(allPartitions, context));
    if (!newAssignment.equals(currentAssignment)) {
        if (currentAssignment != null) {
            listener.onPartitionsRevoked(currentAssignment);
            listener.onPartitionsAssigned(newAssignment);
        }
        currentAssignment = newAssignment;
        consumer.assign(currentAssignment);
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) PartitionInfo(org.apache.kafka.common.PartitionInfo) HashSet(java.util.HashSet)

Example 39 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project storm by apache.

the class ManualPartitionPatternSubscription method refreshAssignment.

@Override
public void refreshAssignment() {
    List<TopicPartition> allPartitions = new ArrayList<>();
    for (Map.Entry<String, List<PartitionInfo>> entry : consumer.listTopics().entrySet()) {
        if (pattern.matcher(entry.getKey()).matches()) {
            for (PartitionInfo partitionInfo : entry.getValue()) {
                allPartitions.add(new TopicPartition(partitionInfo.topic(), partitionInfo.partition()));
            }
        }
    }
    Collections.sort(allPartitions, TopicPartitionComparator.INSTANCE);
    Set<TopicPartition> newAssignment = new HashSet<>(parter.partition(allPartitions, context));
    if (!newAssignment.equals(currentAssignment)) {
        if (currentAssignment != null) {
            listener.onPartitionsRevoked(currentAssignment);
            listener.onPartitionsAssigned(newAssignment);
        }
        currentAssignment = newAssignment;
        consumer.assign(currentAssignment);
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) PartitionInfo(org.apache.kafka.common.PartitionInfo) Map(java.util.Map) HashSet(java.util.HashSet)

Example 40 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project kafka by apache.

the class KafkaProducerTest method testMetadataFetchOnStaleMetadata.

@PrepareOnlyThisForTest(Metadata.class)
@Test
public void testMetadataFetchOnStaleMetadata() throws Exception {
    Properties props = new Properties();
    props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    KafkaProducer<String, String> producer = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer());
    Metadata metadata = PowerMock.createNiceMock(Metadata.class);
    MemberModifier.field(KafkaProducer.class, "metadata").set(producer, metadata);
    String topic = "topic";
    ProducerRecord<String, String> initialRecord = new ProducerRecord<>(topic, "value");
    // Create a record with a partition higher than the initial (outdated) partition range
    ProducerRecord<String, String> extendedRecord = new ProducerRecord<>(topic, 2, null, "value");
    Collection<Node> nodes = Collections.singletonList(new Node(0, "host1", 1000));
    final Cluster emptyCluster = new Cluster(null, nodes, Collections.<PartitionInfo>emptySet(), Collections.<String>emptySet(), Collections.<String>emptySet());
    final Cluster initialCluster = new Cluster("dummy", Collections.singletonList(new Node(0, "host1", 1000)), Arrays.asList(new PartitionInfo(topic, 0, null, null, null)), Collections.<String>emptySet(), Collections.<String>emptySet());
    final Cluster extendedCluster = new Cluster("dummy", Collections.singletonList(new Node(0, "host1", 1000)), Arrays.asList(new PartitionInfo(topic, 0, null, null, null), new PartitionInfo(topic, 1, null, null, null), new PartitionInfo(topic, 2, null, null, null)), Collections.<String>emptySet(), Collections.<String>emptySet());
    // Expect exactly one fetch for each attempt to refresh while topic metadata is not available
    final int refreshAttempts = 5;
    EasyMock.expect(metadata.fetch()).andReturn(emptyCluster).times(refreshAttempts - 1);
    EasyMock.expect(metadata.fetch()).andReturn(initialCluster).once();
    EasyMock.expect(metadata.fetch()).andThrow(new IllegalStateException("Unexpected call to metadata.fetch()")).anyTimes();
    PowerMock.replay(metadata);
    producer.send(initialRecord);
    PowerMock.verify(metadata);
    // Expect exactly one fetch if topic metadata is available and records are still within range
    PowerMock.reset(metadata);
    EasyMock.expect(metadata.fetch()).andReturn(initialCluster).once();
    EasyMock.expect(metadata.fetch()).andThrow(new IllegalStateException("Unexpected call to metadata.fetch()")).anyTimes();
    PowerMock.replay(metadata);
    producer.send(initialRecord, null);
    PowerMock.verify(metadata);
    // Expect exactly two fetches if topic metadata is available but metadata response still returns
    // the same partition size (either because metadata are still stale at the broker too or because
    // there weren't any partitions added in the first place).
    PowerMock.reset(metadata);
    EasyMock.expect(metadata.fetch()).andReturn(initialCluster).once();
    EasyMock.expect(metadata.fetch()).andReturn(initialCluster).once();
    EasyMock.expect(metadata.fetch()).andThrow(new IllegalStateException("Unexpected call to metadata.fetch()")).anyTimes();
    PowerMock.replay(metadata);
    try {
        producer.send(extendedRecord, null);
        fail("Expected KafkaException to be raised");
    } catch (KafkaException e) {
    // expected
    }
    PowerMock.verify(metadata);
    // Expect exactly two fetches if topic metadata is available but outdated for the given record
    PowerMock.reset(metadata);
    EasyMock.expect(metadata.fetch()).andReturn(initialCluster).once();
    EasyMock.expect(metadata.fetch()).andReturn(extendedCluster).once();
    EasyMock.expect(metadata.fetch()).andThrow(new IllegalStateException("Unexpected call to metadata.fetch()")).anyTimes();
    PowerMock.replay(metadata);
    producer.send(extendedRecord, null);
    PowerMock.verify(metadata);
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) Properties(java.util.Properties) KafkaException(org.apache.kafka.common.KafkaException) PartitionInfo(org.apache.kafka.common.PartitionInfo) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Aggregations

PartitionInfo (org.apache.kafka.common.PartitionInfo)56 TopicPartition (org.apache.kafka.common.TopicPartition)34 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 Node (org.apache.kafka.common.Node)14 Map (java.util.Map)13 HashSet (java.util.HashSet)12 Cluster (org.apache.kafka.common.Cluster)11 Set (java.util.Set)9 List (java.util.List)8 TaskId (org.apache.kafka.streams.processor.TaskId)7 StreamsConfig (org.apache.kafka.streams.StreamsConfig)6 MockTime (org.apache.kafka.common.utils.MockTime)5 Properties (java.util.Properties)4 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)4 HostInfo (org.apache.kafka.streams.state.HostInfo)4 StreamsMetadata (org.apache.kafka.streams.state.StreamsMetadata)4 File (java.io.File)3 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)3