Search in sources :

Example 1 with StreamsMetadataImpl

use of org.apache.kafka.streams.state.internals.StreamsMetadataImpl in project kafka by apache.

the class StreamsMetadataStateTest method shouldGetAllStreamInstances.

@Test
public void shouldGetAllStreamInstances() {
    final StreamsMetadata one = new StreamsMetadataImpl(hostOne, mkSet(globalTable, "table-one", "table-two", "merged-table"), mkSet(topic1P0, topic2P1, topic4P0), mkSet("table-one", "table-two", "merged-table"), mkSet(topic2P0, topic1P1));
    final StreamsMetadata two = new StreamsMetadataImpl(hostTwo, mkSet(globalTable, "table-two", "table-one", "merged-table"), mkSet(topic2P0, topic1P1), mkSet("table-three"), mkSet(topic3P0));
    final StreamsMetadata three = new StreamsMetadataImpl(hostThree, mkSet(globalTable, "table-three"), Collections.singleton(topic3P0), mkSet("table-one", "table-two", "merged-table"), mkSet(topic1P0, topic2P1, topic4P0));
    final Collection<StreamsMetadata> actual = metadataState.getAllMetadata();
    assertEquals(3, actual.size());
    assertTrue("expected " + actual + " to contain " + one, actual.contains(one));
    assertTrue("expected " + actual + " to contain " + two, actual.contains(two));
    assertTrue("expected " + actual + " to contain " + three, actual.contains(three));
}
Also used : StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) Test(org.junit.Test)

Example 2 with StreamsMetadataImpl

use of org.apache.kafka.streams.state.internals.StreamsMetadataImpl in project kafka by apache.

the class StreamsMetadataStateTest method shouldGetInstancesForStoreName.

@Test
public void shouldGetInstancesForStoreName() {
    final StreamsMetadata one = new StreamsMetadataImpl(hostOne, mkSet(globalTable, "table-one", "table-two", "merged-table"), mkSet(topic1P0, topic2P1, topic4P0), mkSet("table-one", "table-two", "merged-table"), mkSet(topic2P0, topic1P1));
    final StreamsMetadata two = new StreamsMetadataImpl(hostTwo, mkSet(globalTable, "table-two", "table-one", "merged-table"), mkSet(topic2P0, topic1P1), mkSet("table-three"), mkSet(topic3P0));
    final Collection<StreamsMetadata> actual = metadataState.getAllMetadataForStore("table-one");
    final Map<HostInfo, StreamsMetadata> actualAsMap = actual.stream().collect(Collectors.toMap(StreamsMetadata::hostInfo, Function.identity()));
    assertEquals(3, actual.size());
    assertTrue("expected " + actual + " to contain " + one, actual.contains(one));
    assertTrue("expected " + actual + " to contain " + two, actual.contains(two));
    assertTrue("expected " + hostThree + " to contain as standby", actualAsMap.get(hostThree).standbyStateStoreNames().contains("table-one"));
}
Also used : StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Example 3 with StreamsMetadataImpl

use of org.apache.kafka.streams.state.internals.StreamsMetadataImpl in project kafka by apache.

the class StreamsMetadataStateTest method shouldGetAllStreamsInstancesWithNoStores.

@Test
public void shouldGetAllStreamsInstancesWithNoStores() {
    builder.stream("topic-five").filter((key, value) -> true).to("some-other-topic");
    final TopicPartition tp5 = new TopicPartition("topic-five", 1);
    final HostInfo hostFour = new HostInfo("host-four", 8080);
    hostToActivePartitions.put(hostFour, mkSet(tp5));
    metadataState.onChange(hostToActivePartitions, Collections.emptyMap(), cluster.withPartitions(Collections.singletonMap(tp5, new PartitionInfo("topic-five", 1, null, null, null))));
    final StreamsMetadata expected = new StreamsMetadataImpl(hostFour, Collections.singleton(globalTable), Collections.singleton(tp5), Collections.emptySet(), Collections.emptySet());
    final Collection<StreamsMetadata> actual = metadataState.getAllMetadata();
    assertTrue("expected " + actual + " to contain " + expected, actual.contains(expected));
}
Also used : Arrays(java.util.Arrays) HostInfo(org.apache.kafka.streams.state.HostInfo) Assert.assertThrows(org.junit.Assert.assertThrows) HashMap(java.util.HashMap) KStream(org.apache.kafka.streams.kstream.KStream) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Cluster(org.apache.kafka.common.Cluster) LogContext(org.apache.kafka.common.utils.LogContext) TopologyWrapper(org.apache.kafka.streams.TopologyWrapper) Map(java.util.Map) Serdes(org.apache.kafka.common.serialization.Serdes) Before(org.junit.Before) TopicPartition(org.apache.kafka.common.TopicPartition) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamPartitioner(org.apache.kafka.streams.processor.StreamPartitioner) Consumed(org.apache.kafka.streams.kstream.Consumed) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PartitionInfo(org.apache.kafka.common.PartitionInfo) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) KeyQueryMetadata(org.apache.kafka.streams.KeyQueryMetadata) Serializer(org.apache.kafka.common.serialization.Serializer) Assert.assertFalse(org.junit.Assert.assertFalse) Materialized(org.apache.kafka.streams.kstream.Materialized) Node(org.apache.kafka.common.Node) DummyStreamsConfig(org.apache.kafka.streams.processor.internals.testutil.DummyStreamsConfig) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TopicPartition(org.apache.kafka.common.TopicPartition) StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) PartitionInfo(org.apache.kafka.common.PartitionInfo) HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Example 4 with StreamsMetadataImpl

use of org.apache.kafka.streams.state.internals.StreamsMetadataImpl in project kafka by apache.

the class StreamsMetadataState method getAllMetadataForStore.

/**
 * Find all of the {@link StreamsMetadata}s for a given storeName in the given topology
 *
 * @param storeName the storeName to find metadata for
 * @param topologyName the storeName to find metadata for
 * @return A collection of {@link StreamsMetadata} that have the provided storeName
 */
public synchronized Collection<StreamsMetadata> getAllMetadataForStore(final String storeName, final String topologyName) {
    Objects.requireNonNull(storeName, "storeName cannot be null");
    Objects.requireNonNull(topologyName, "topologyName cannot be null");
    if (!isInitialized()) {
        return Collections.emptyList();
    }
    final Collection<String> sourceTopics = topologyMetadata.sourceTopicsForStore(storeName, topologyName);
    if (sourceTopics.isEmpty()) {
        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) && metadata.stateStoreNames().contains(storeName) || metadata.standbyStateStoreNames().contains(storeName)) {
            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 5 with StreamsMetadataImpl

use of org.apache.kafka.streams.state.internals.StreamsMetadataImpl in project kafka by apache.

the class StreamsMetadataState method rebuildMetadataForSingleTopology.

private List<StreamsMetadata> rebuildMetadataForSingleTopology(final Map<HostInfo, Set<TopicPartition>> activePartitionHostMap, final Map<HostInfo, Set<TopicPartition>> standbyPartitionHostMap) {
    final List<StreamsMetadata> rebuiltMetadata = new ArrayList<>();
    final Map<String, List<String>> storeToSourceTopics = topologyMetadata.stateStoreNameToSourceTopics();
    Stream.concat(activePartitionHostMap.keySet().stream(), standbyPartitionHostMap.keySet().stream()).distinct().sorted(Comparator.comparing(HostInfo::host).thenComparingInt(HostInfo::port)).forEach(hostInfo -> {
        final Set<TopicPartition> activePartitionsOnHost = new HashSet<>();
        final Set<String> activeStoresOnHost = new HashSet<>();
        if (activePartitionHostMap.containsKey(hostInfo)) {
            activePartitionsOnHost.addAll(activePartitionHostMap.get(hostInfo));
            activeStoresOnHost.addAll(getStoresOnHost(storeToSourceTopics, activePartitionsOnHost));
        }
        activeStoresOnHost.addAll(globalStores);
        final Set<TopicPartition> standbyPartitionsOnHost = new HashSet<>();
        final Set<String> standbyStoresOnHost = new HashSet<>();
        if (standbyPartitionHostMap.containsKey(hostInfo)) {
            standbyPartitionsOnHost.addAll(standbyPartitionHostMap.get(hostInfo));
            standbyStoresOnHost.addAll(getStoresOnHost(storeToSourceTopics, standbyPartitionsOnHost));
        }
        final StreamsMetadata metadata = new StreamsMetadataImpl(hostInfo, activeStoresOnHost, activePartitionsOnHost, standbyStoresOnHost, standbyPartitionsOnHost);
        rebuiltMetadata.add(metadata);
        if (hostInfo.equals(thisHost)) {
            localMetadata.set(metadata);
        }
    });
    return rebuiltMetadata;
}
Also used : StreamsMetadata(org.apache.kafka.streams.StreamsMetadata) TopicPartition(org.apache.kafka.common.TopicPartition) StreamsMetadataImpl(org.apache.kafka.streams.state.internals.StreamsMetadataImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HostInfo(org.apache.kafka.streams.state.HostInfo) HashSet(java.util.HashSet)

Aggregations

StreamsMetadataImpl (org.apache.kafka.streams.state.internals.StreamsMetadataImpl)15 StreamsMetadata (org.apache.kafka.streams.StreamsMetadata)14 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)5 HostInfo (org.apache.kafka.streams.state.HostInfo)4 List (java.util.List)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 HashSet (java.util.HashSet)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Cluster (org.apache.kafka.common.Cluster)1 Node (org.apache.kafka.common.Node)1 PartitionInfo (org.apache.kafka.common.PartitionInfo)1 Serdes (org.apache.kafka.common.serialization.Serdes)1