Search in sources :

Example 11 with StreamsMetadata

use of org.apache.kafka.streams.StreamsMetadata in project kafka by apache.

the class NamedTopologyIntegrationTest method verifyMetadataForTopology.

/**
 * Validates that each metadata object has only partitions & state stores for its specific topology name and
 * asserts that {@code left} and {@code right} differ only by {@link StreamsMetadata#hostInfo()}
 */
private void verifyMetadataForTopology(final String topologyName, final Collection<StreamsMetadata> left, final Collection<StreamsMetadata> right) {
    assertThat(left.size(), equalTo(right.size()));
    final Iterator<StreamsMetadata> leftIter = left.iterator();
    final Iterator<StreamsMetadata> rightIter = right.iterator();
    while (leftIter.hasNext()) {
        final StreamsMetadataImpl leftMetadata = (StreamsMetadataImpl) leftIter.next();
        final StreamsMetadataImpl rightMetadata = (StreamsMetadataImpl) rightIter.next();
        verifyPartitionsAndStoresForTopology(topologyName, leftMetadata);
        verifyPartitionsAndStoresForTopology(topologyName, rightMetadata);
        assertThat(verifyEquivalentMetadataForHost(leftMetadata, rightMetadata), is(true));
    }
}
Also used : StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl)

Example 12 with StreamsMetadata

use of org.apache.kafka.streams.StreamsMetadata in project kafka by apache.

the class StreamsMetadataState method getKeyQueryMetadataForKey.

private <K> KeyQueryMetadata getKeyQueryMetadataForKey(final String storeName, final K key, final StreamPartitioner<? super K, ?> partitioner, final SourceTopicsInfo sourceTopicsInfo) {
    final Integer partition = partitioner.partition(sourceTopicsInfo.topicWithMostPartitions, key, null, sourceTopicsInfo.maxPartitions);
    final Set<TopicPartition> matchingPartitions = new HashSet<>();
    for (final String sourceTopic : sourceTopicsInfo.sourceTopics) {
        matchingPartitions.add(new TopicPartition(sourceTopic, partition));
    }
    HostInfo activeHost = UNKNOWN_HOST;
    final Set<HostInfo> standbyHosts = new HashSet<>();
    for (final StreamsMetadata streamsMetadata : allMetadata) {
        final Set<String> activeStateStoreNames = streamsMetadata.stateStoreNames();
        final Set<TopicPartition> topicPartitions = new HashSet<>(streamsMetadata.topicPartitions());
        final Set<String> standbyStateStoreNames = streamsMetadata.standbyStateStoreNames();
        final Set<TopicPartition> standbyTopicPartitions = new HashSet<>(streamsMetadata.standbyTopicPartitions());
        topicPartitions.retainAll(matchingPartitions);
        if (activeStateStoreNames.contains(storeName) && !topicPartitions.isEmpty()) {
            activeHost = streamsMetadata.hostInfo();
        }
        standbyTopicPartitions.retainAll(matchingPartitions);
        if (standbyStateStoreNames.contains(storeName) && !standbyTopicPartitions.isEmpty()) {
            standbyHosts.add(streamsMetadata.hostInfo());
        }
    }
    return new KeyQueryMetadata(activeHost, standbyHosts, partition);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) HostInfo(org.apache.kafka.streams.state.HostInfo) KeyQueryMetadata(org.apache.kafka.streams.KeyQueryMetadata) HashSet(java.util.HashSet)

Example 13 with StreamsMetadata

use of org.apache.kafka.streams.StreamsMetadata in project kafka by apache.

the class StreamsMetadataState method getAllMetadataForTopology.

public synchronized Collection<StreamsMetadata> getAllMetadataForTopology(final String topologyName) {
    Objects.requireNonNull(topologyName, "topologyName cannot be null");
    if (!isInitialized()) {
        return Collections.emptyList();
    }
    final ArrayList<StreamsMetadata> results = new ArrayList<>();
    for (final StreamsMetadata metadata : allMetadata) {
        final String metadataTopologyName = ((StreamsMetadataImpl) metadata).topologyName();
        if (metadataTopologyName != null && metadataTopologyName.equals(topologyName)) {
            results.add(metadata);
        }
    }
    return results;
}
Also used : StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) ArrayList(java.util.ArrayList)

Example 14 with StreamsMetadata

use of org.apache.kafka.streams.StreamsMetadata in project kafka by apache.

the class StreamsMetadataTest method shouldNotBeEqualIfDifferInHostInfo.

@Test
public void shouldNotBeEqualIfDifferInHostInfo() {
    final StreamsMetadata differHostInfo = new StreamsMetadataImpl(new HostInfo("different", 122), STATE_STORE_NAMES, TOPIC_PARTITIONS, STAND_BY_STORE_NAMES, STANDBY_TOPIC_PARTITIONS);
    assertThat(streamsMetadata, not(equalTo(differHostInfo)));
    assertThat(streamsMetadata.hashCode(), not(equalTo(differHostInfo.hashCode())));
}
Also used : StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) Test(org.junit.Test)

Example 15 with StreamsMetadata

use of org.apache.kafka.streams.StreamsMetadata in project kafka by apache.

the class StreamsMetadataTest method shouldNotBeEqualIfDifferInStandByTopicPartitions.

@Test
public void shouldNotBeEqualIfDifferInStandByTopicPartitions() {
    final StreamsMetadata differStandByTopicPartitions = new StreamsMetadataImpl(HOST_INFO, STATE_STORE_NAMES, TOPIC_PARTITIONS, STAND_BY_STORE_NAMES, mkSet(TP_0));
    assertThat(streamsMetadata, not(equalTo(differStandByTopicPartitions)));
    assertThat(streamsMetadata.hashCode(), not(equalTo(differStandByTopicPartitions.hashCode())));
}
Also used : StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) Test(org.junit.Test)

Aggregations

StreamsMetadata (org.apache.kafka.streams.StreamsMetadata)18 StreamsMetadataImpl (org.apache.kafka.streams.state.internals.StreamsMetadataImpl)15 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)5 TopicPartition (org.apache.kafka.common.TopicPartition)5 List (java.util.List)4 HostInfo (org.apache.kafka.streams.state.HostInfo)4 HashSet (java.util.HashSet)3 KeyQueryMetadata (org.apache.kafka.streams.KeyQueryMetadata)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Serdes (org.apache.kafka.common.serialization.Serdes)2 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)2 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1