Search in sources :

Example 1 with PartitionMetadata

use of org.apache.kafka.common.requests.MetadataResponse.PartitionMetadata in project kafka by apache.

the class MetadataCache method mergeWith.

/**
 * Merges the metadata cache's contents with the provided metadata, returning a new metadata cache. The provided
 * metadata is presumed to be more recent than the cache's metadata, and therefore all overlapping metadata will
 * be overridden.
 *
 * @param newClusterId the new cluster Id
 * @param newNodes the new set of nodes
 * @param addPartitions partitions to add
 * @param addUnauthorizedTopics unauthorized topics to add
 * @param addInternalTopics internal topics to add
 * @param newController the new controller node
 * @param topicIds the mapping from topic name to topic ID from the MetadataResponse
 * @param retainTopic returns whether a topic's metadata should be retained
 * @return the merged metadata cache
 */
MetadataCache mergeWith(String newClusterId, Map<Integer, Node> newNodes, Collection<PartitionMetadata> addPartitions, Set<String> addUnauthorizedTopics, Set<String> addInvalidTopics, Set<String> addInternalTopics, Node newController, Map<String, Uuid> topicIds, BiPredicate<String, Boolean> retainTopic) {
    Predicate<String> shouldRetainTopic = topic -> retainTopic.test(topic, internalTopics.contains(topic));
    Map<TopicPartition, PartitionMetadata> newMetadataByPartition = new HashMap<>(addPartitions.size());
    // We want the most recent topic ID. We start with the previous ID stored for retained topics and then
    // update with newest information from the MetadataResponse. We always take the latest state, removing existing
    // topic IDs if the latest state contains the topic name but not a topic ID.
    Map<String, Uuid> newTopicIds = topicIds.entrySet().stream().filter(entry -> shouldRetainTopic.test(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    for (PartitionMetadata partition : addPartitions) {
        newMetadataByPartition.put(partition.topicPartition, partition);
        Uuid id = topicIds.get(partition.topic());
        if (id != null)
            newTopicIds.put(partition.topic(), id);
        else
            // Remove if the latest metadata does not have a topic ID
            newTopicIds.remove(partition.topic());
    }
    for (Map.Entry<TopicPartition, PartitionMetadata> entry : metadataByPartition.entrySet()) {
        if (shouldRetainTopic.test(entry.getKey().topic())) {
            newMetadataByPartition.putIfAbsent(entry.getKey(), entry.getValue());
        }
    }
    Set<String> newUnauthorizedTopics = fillSet(addUnauthorizedTopics, unauthorizedTopics, shouldRetainTopic);
    Set<String> newInvalidTopics = fillSet(addInvalidTopics, invalidTopics, shouldRetainTopic);
    Set<String> newInternalTopics = fillSet(addInternalTopics, internalTopics, shouldRetainTopic);
    return new MetadataCache(newClusterId, newNodes, newMetadataByPartition.values(), newUnauthorizedTopics, newInvalidTopics, newInternalTopics, newController, newTopicIds);
}
Also used : Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) ClusterResource(org.apache.kafka.common.ClusterResource) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) PartitionInfo(org.apache.kafka.common.PartitionInfo) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) PartitionMetadata(org.apache.kafka.common.requests.MetadataResponse.PartitionMetadata) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) Cluster(org.apache.kafka.common.Cluster) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Collections(java.util.Collections) HashMap(java.util.HashMap) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionMetadata(org.apache.kafka.common.requests.MetadataResponse.PartitionMetadata) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 BiPredicate (java.util.function.BiPredicate)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Cluster (org.apache.kafka.common.Cluster)1 ClusterResource (org.apache.kafka.common.ClusterResource)1 Node (org.apache.kafka.common.Node)1 PartitionInfo (org.apache.kafka.common.PartitionInfo)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Uuid (org.apache.kafka.common.Uuid)1 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)1 PartitionMetadata (org.apache.kafka.common.requests.MetadataResponse.PartitionMetadata)1