Search in sources :

Example 11 with MockKeyValueStoreBuilder

use of org.apache.kafka.test.MockKeyValueStoreBuilder in project kafka by apache.

the class InternalTopologyBuilderTest method testTopicGroupsByStateStore.

@Test
public void testTopicGroupsByStateStore() {
    builder.setApplicationId("X");
    builder.addSource(null, "source-1", null, null, null, "topic-1", "topic-1x");
    builder.addSource(null, "source-2", null, null, null, "topic-2");
    builder.addSource(null, "source-3", null, null, null, "topic-3");
    builder.addSource(null, "source-4", null, null, null, "topic-4");
    builder.addSource(null, "source-5", null, null, null, "topic-5");
    builder.addProcessor("processor-1", new MockApiProcessorSupplier<>(), "source-1");
    builder.addProcessor("processor-2", new MockApiProcessorSupplier<>(), "source-2");
    builder.addStateStore(new MockKeyValueStoreBuilder("store-1", false), "processor-1", "processor-2");
    builder.addProcessor("processor-3", new MockApiProcessorSupplier<>(), "source-3");
    builder.addProcessor("processor-4", new MockApiProcessorSupplier<>(), "source-4");
    builder.addStateStore(new MockKeyValueStoreBuilder("store-2", false), "processor-3", "processor-4");
    builder.addProcessor("processor-5", new MockApiProcessorSupplier<>(), "source-5");
    builder.addStateStore(new MockKeyValueStoreBuilder("store-3", false));
    builder.connectProcessorAndStateStores("processor-5", "store-3");
    builder.buildTopology();
    final Map<Subtopology, InternalTopologyBuilder.TopicsInfo> topicGroups = builder.subtopologyToTopicsInfo();
    final Map<Subtopology, InternalTopologyBuilder.TopicsInfo> expectedTopicGroups = new HashMap<>();
    final String store1 = ProcessorStateManager.storeChangelogTopic("X", "store-1", builder.topologyName());
    final String store2 = ProcessorStateManager.storeChangelogTopic("X", "store-2", builder.topologyName());
    final String store3 = ProcessorStateManager.storeChangelogTopic("X", "store-3", builder.topologyName());
    expectedTopicGroups.put(SUBTOPOLOGY_0, new InternalTopologyBuilder.TopicsInfo(Collections.emptySet(), mkSet("topic-1", "topic-1x", "topic-2"), Collections.emptyMap(), Collections.singletonMap(store1, new UnwindowedChangelogTopicConfig(store1, Collections.emptyMap()))));
    expectedTopicGroups.put(SUBTOPOLOGY_1, new InternalTopologyBuilder.TopicsInfo(Collections.emptySet(), mkSet("topic-3", "topic-4"), Collections.emptyMap(), Collections.singletonMap(store2, new UnwindowedChangelogTopicConfig(store2, Collections.emptyMap()))));
    expectedTopicGroups.put(SUBTOPOLOGY_2, new InternalTopologyBuilder.TopicsInfo(Collections.emptySet(), mkSet("topic-5"), Collections.emptyMap(), Collections.singletonMap(store3, new UnwindowedChangelogTopicConfig(store3, Collections.emptyMap()))));
    assertEquals(3, topicGroups.size());
    assertEquals(expectedTopicGroups, topicGroups);
}
Also used : HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) Test(org.junit.Test)

Example 12 with MockKeyValueStoreBuilder

use of org.apache.kafka.test.MockKeyValueStoreBuilder in project kafka by apache.

the class InternalTopologyBuilderTest method shouldNotAllowToAddStoresWithSameName.

@Test
public void shouldNotAllowToAddStoresWithSameName() {
    final StoreBuilder<KeyValueStore<Object, Object>> otherBuilder = new MockKeyValueStoreBuilder("testStore", false);
    builder.addStateStore(storeBuilder);
    final TopologyException exception = assertThrows(TopologyException.class, () -> builder.addStateStore(otherBuilder));
    assertThat(exception.getMessage(), equalTo("Invalid topology: A different StateStore has already been added with the name testStore"));
}
Also used : KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) TopologyException(org.apache.kafka.streams.errors.TopologyException) Test(org.junit.Test)

Example 13 with MockKeyValueStoreBuilder

use of org.apache.kafka.test.MockKeyValueStoreBuilder in project kafka by apache.

the class InternalTopologyBuilderTest method shouldAllowIncrementalBuilds.

@Test
public void shouldAllowIncrementalBuilds() {
    Map<Integer, Set<String>> oldNodeGroups, newNodeGroups;
    oldNodeGroups = builder.nodeGroups();
    builder.addSource(null, "source-1", null, null, null, "topic-1");
    builder.addSource(null, "source-2", null, null, null, "topic-2");
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
    oldNodeGroups = newNodeGroups;
    builder.addSource(null, "source-3", null, null, null, Pattern.compile(""));
    builder.addSource(null, "source-4", null, null, null, Pattern.compile(""));
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
    oldNodeGroups = newNodeGroups;
    builder.addProcessor("processor-1", new MockApiProcessorSupplier<>(), "source-1");
    builder.addProcessor("processor-2", new MockApiProcessorSupplier<>(), "source-2");
    builder.addProcessor("processor-3", new MockApiProcessorSupplier<>(), "source-3");
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
    oldNodeGroups = newNodeGroups;
    builder.addSink("sink-1", "sink-topic", null, null, null, "processor-1");
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
    oldNodeGroups = newNodeGroups;
    builder.addSink("sink-2", (k, v, ctx) -> "sink-topic", null, null, null, "processor-2");
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
    oldNodeGroups = newNodeGroups;
    builder.addStateStore(new MockKeyValueStoreBuilder("store-1", false), "processor-1", "processor-2");
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
    oldNodeGroups = newNodeGroups;
    builder.addStateStore(new MockKeyValueStoreBuilder("store-2", false));
    builder.connectProcessorAndStateStores("processor-2", "store-2");
    builder.connectProcessorAndStateStores("processor-3", "store-2");
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
    oldNodeGroups = newNodeGroups;
    builder.addGlobalStore(new MockKeyValueStoreBuilder("global-store", false).withLoggingDisabled(), "globalSource", null, null, null, "globalTopic", "global-processor", new MockApiProcessorSupplier<>());
    newNodeGroups = builder.nodeGroups();
    assertNotEquals(oldNodeGroups, newNodeGroups);
}
Also used : Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) HashSet(java.util.HashSet) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) Test(org.junit.Test)

Example 14 with MockKeyValueStoreBuilder

use of org.apache.kafka.test.MockKeyValueStoreBuilder in project kafka by apache.

the class InternalTopologyBuilderTest method shouldConnectGlobalStateStoreToInputTopic.

@Test
public void shouldConnectGlobalStateStoreToInputTopic() {
    final String globalStoreName = "global-store";
    final String globalTopic = "global-topic";
    builder.setApplicationId("X");
    builder.addGlobalStore(new MockKeyValueStoreBuilder(globalStoreName, false).withLoggingDisabled(), "globalSource", null, null, null, globalTopic, "global-processor", new MockApiProcessorSupplier<>());
    builder.initializeSubscription();
    builder.rewriteTopology(new StreamsConfig(mkProperties(mkMap(mkEntry(StreamsConfig.APPLICATION_ID_CONFIG, "asdf"), mkEntry(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "asdf")))));
    assertThat(builder.buildGlobalStateTopology().storeToChangelogTopic().get(globalStoreName), is(globalTopic));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 15 with MockKeyValueStoreBuilder

use of org.apache.kafka.test.MockKeyValueStoreBuilder in project kafka by apache.

the class InternalTopologyBuilderTest method testAddGlobalStoreWithBadSupplier.

@Test
public void testAddGlobalStoreWithBadSupplier() {
    final org.apache.kafka.streams.processor.api.Processor<?, ?, Void, Void> processor = new MockApiProcessorSupplier<Object, Object, Void, Void>().get();
    final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> builder.addGlobalStore(new MockKeyValueStoreBuilder("global-store", false).withLoggingDisabled(), "globalSource", null, null, null, "globalTopic", "global-processor", () -> processor));
    assertThat(exception.getMessage(), containsString("#get() must return a new object each time it is called."));
}
Also used : MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) Test(org.junit.Test)

Aggregations

MockKeyValueStoreBuilder (org.apache.kafka.test.MockKeyValueStoreBuilder)22 Test (org.junit.Test)21 GroupSubscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription)11 Subscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription)11 Assignment (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment)7 AssignmentInfo (org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo)6 HashSet (java.util.HashSet)5 TaskId (org.apache.kafka.streams.processor.TaskId)5 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 AdminClient (org.apache.kafka.clients.admin.AdminClient)4 TopologyException (org.apache.kafka.streams.errors.TopologyException)4 Map (java.util.Map)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Collections.emptyMap (java.util.Collections.emptyMap)2 Collections.singletonMap (java.util.Collections.singletonMap)2 Set (java.util.Set)2 ListOffsetsResult (org.apache.kafka.clients.admin.ListOffsetsResult)2