Search in sources :

Example 6 with MockKeyValueStoreBuilder

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

the class HighAvailabilityStreamsPartitionAssignorTest method shouldScheduleProbingRebalanceOnThisClientIfWarmupTasksRequired.

@Test
public void shouldScheduleProbingRebalanceOnThisClientIfWarmupTasksRequired() {
    final long rebalanceInterval = 5 * 60 * 1000L;
    builder.addSource(null, "source1", null, null, null, "topic1");
    builder.addProcessor("processor1", new MockApiProcessorSupplier<>(), "source1");
    builder.addStateStore(new MockKeyValueStoreBuilder("store1", false), "processor1");
    final Set<TaskId> allTasks = mkSet(TASK_0_0, TASK_0_1, TASK_0_2);
    createMockTaskManager(allTasks);
    createMockAdminClient(getTopicPartitionOffsetsMap(singletonList(APPLICATION_ID + "-store1-changelog"), singletonList(3)));
    configurePartitionAssignorWith(singletonMap(StreamsConfig.PROBING_REBALANCE_INTERVAL_MS_CONFIG, rebalanceInterval));
    final String firstConsumer = "consumer1";
    final String newConsumer = "consumer2";
    subscriptions.put(firstConsumer, new Subscription(singletonList("source1"), getInfo(UUID_1, allTasks).encode()));
    subscriptions.put(newConsumer, new Subscription(singletonList("source1"), getInfo(UUID_2, EMPTY_TASKS).encode()));
    final Map<String, Assignment> assignments = partitionAssignor.assign(metadata, new GroupSubscription(subscriptions)).groupAssignment();
    final List<TaskId> firstConsumerActiveTasks = AssignmentInfo.decode(assignments.get(firstConsumer).userData()).activeTasks();
    final List<TaskId> newConsumerActiveTasks = AssignmentInfo.decode(assignments.get(newConsumer).userData()).activeTasks();
    final ArrayList<TaskId> sortedExpectedTasks = new ArrayList<>(allTasks);
    Collections.sort(sortedExpectedTasks);
    assertThat(firstConsumerActiveTasks, equalTo(sortedExpectedTasks));
    assertThat(newConsumerActiveTasks, empty());
    assertThat(referenceContainer.assignmentErrorCode.get(), equalTo(AssignorError.NONE.code()));
    final long nextScheduledRebalanceOnThisClient = AssignmentInfo.decode(assignments.get(firstConsumer).userData()).nextRebalanceMs();
    final long nextScheduledRebalanceOnOtherClient = AssignmentInfo.decode(assignments.get(newConsumer).userData()).nextRebalanceMs();
    assertThat(nextScheduledRebalanceOnThisClient, equalTo(time.milliseconds() + rebalanceInterval));
    assertThat(nextScheduledRebalanceOnOtherClient, equalTo(Long.MAX_VALUE));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) ArrayList(java.util.ArrayList) Assignment(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) Test(org.junit.Test)

Example 7 with MockKeyValueStoreBuilder

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

the class InternalTopologyBuilderTest method testPatternSourceTopicsWithGlobalTopics.

@Test
public void testPatternSourceTopicsWithGlobalTopics() {
    builder.setApplicationId("X");
    builder.addSource(null, "source-1", null, null, null, Pattern.compile("topic-1"));
    builder.addSource(null, "source-2", null, null, null, Pattern.compile("topic-2"));
    builder.addGlobalStore(new MockKeyValueStoreBuilder("global-store", false).withLoggingDisabled(), "globalSource", null, null, null, "globalTopic", "global-processor", new MockApiProcessorSupplier<>());
    builder.initializeSubscription();
    final Pattern expectedPattern = Pattern.compile("topic-1|topic-2");
    final String patternString = builder.sourceTopicsPatternString();
    assertEquals(expectedPattern.pattern(), Pattern.compile(patternString).pattern());
}
Also used : Pattern(java.util.regex.Pattern) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) Test(org.junit.Test)

Example 8 with MockKeyValueStoreBuilder

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

the class InternalTopologyBuilderTest method shouldNotAllowToAddGlobalStoresWithSameName.

@Test
public void shouldNotAllowToAddGlobalStoresWithSameName() {
    final StoreBuilder<KeyValueStore<Object, Object>> firstGlobalBuilder = new MockKeyValueStoreBuilder("testStore", false).withLoggingDisabled();
    final StoreBuilder<KeyValueStore<Object, Object>> secondGlobalBuilder = new MockKeyValueStoreBuilder("testStore", false).withLoggingDisabled();
    builder.addGlobalStore(firstGlobalBuilder, "global-store", null, null, null, "global-topic", "global-processor", new MockApiProcessorSupplier<>());
    final TopologyException exception = assertThrows(TopologyException.class, () -> builder.addGlobalStore(secondGlobalBuilder, "global-store-2", null, null, null, "global-topic", "global-processor-2", new MockApiProcessorSupplier<>()));
    assertThat(exception.getMessage(), equalTo("Invalid topology: A different GlobalStateStore has already been added with the name testStore"));
}
Also used : MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) 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 9 with MockKeyValueStoreBuilder

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

the class RegexSourceIntegrationTest method shouldAddStateStoreToRegexDefinedSource.

@Test
public void shouldAddStateStoreToRegexDefinedSource() throws Exception {
    final StoreBuilder<KeyValueStore<Object, Object>> storeBuilder = new MockKeyValueStoreBuilder("testStateStore", false);
    final long thirtySecondTimeout = 30 * 1000;
    final TopologyWrapper topology = new TopologyWrapper();
    topology.addSource("ingest", Pattern.compile("topic-\\d+"));
    topology.addProcessor("my-processor", new MockApiProcessorSupplier<>(), "ingest");
    topology.addStateStore(storeBuilder, "my-processor");
    streams = new KafkaStreams(topology, streamsConfiguration);
    streams.start();
    final TestCondition stateStoreNameBoundToSourceTopic = () -> {
        final Map<String, List<String>> stateStoreToSourceTopic = topology.getInternalBuilder().stateStoreNameToFullSourceTopicNames();
        final List<String> topicNamesList = stateStoreToSourceTopic.get("testStateStore");
        return topicNamesList != null && !topicNamesList.isEmpty() && topicNamesList.get(0).equals("topic-1");
    };
    TestUtils.waitForCondition(stateStoreNameBoundToSourceTopic, thirtySecondTimeout, "Did not find topic: [topic-1] connected to state store: [testStateStore]");
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) TestCondition(org.apache.kafka.test.TestCondition) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TopologyWrapper(org.apache.kafka.streams.TopologyWrapper) Map(java.util.Map) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 10 with MockKeyValueStoreBuilder

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

the class InternalTopologyBuilderTest method shouldNotAllowToAddStoresWithSameNameWhenFirstStoreIsGlobal.

@Test
public void shouldNotAllowToAddStoresWithSameNameWhenFirstStoreIsGlobal() {
    final StoreBuilder<KeyValueStore<Object, Object>> globalBuilder = new MockKeyValueStoreBuilder("testStore", false).withLoggingDisabled();
    builder.addGlobalStore(globalBuilder, "global-store", null, null, null, "global-topic", "global-processor", new MockApiProcessorSupplier<>());
    final TopologyException exception = assertThrows(TopologyException.class, () -> builder.addStateStore(storeBuilder));
    assertThat(exception.getMessage(), equalTo("Invalid topology: A different GlobalStateStore 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)

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