Search in sources :

Example 1 with MockStateRestoreListener

use of org.apache.kafka.test.MockStateRestoreListener in project apache-kafka-on-k8s by banzaicloud.

the class StoreChangelogReaderTest method shouldRestoreAndNotifyMultipleStores.

@Test
public void shouldRestoreAndNotifyMultipleStores() throws Exception {
    final TopicPartition one = new TopicPartition("one", 0);
    final TopicPartition two = new TopicPartition("two", 0);
    final MockStateRestoreListener callbackOne = new MockStateRestoreListener();
    final MockStateRestoreListener callbackTwo = new MockStateRestoreListener();
    final CompositeRestoreListener restoreListener1 = new CompositeRestoreListener(callbackOne);
    final CompositeRestoreListener restoreListener2 = new CompositeRestoreListener(callbackTwo);
    setupConsumer(10, topicPartition);
    setupConsumer(5, one);
    setupConsumer(3, two);
    changelogReader.register(new StateRestorer(topicPartition, restoreListener, null, Long.MAX_VALUE, true, "storeName1"));
    changelogReader.register(new StateRestorer(one, restoreListener1, null, Long.MAX_VALUE, true, "storeName2"));
    changelogReader.register(new StateRestorer(two, restoreListener2, null, Long.MAX_VALUE, true, "storeName3"));
    expect(active.restoringTaskFor(one)).andReturn(null);
    expect(active.restoringTaskFor(two)).andReturn(null);
    replay(active);
    changelogReader.restore(active);
    assertThat(callback.restored.size(), equalTo(10));
    assertThat(callbackOne.restored.size(), equalTo(5));
    assertThat(callbackTwo.restored.size(), equalTo(3));
    assertAllCallbackStatesExecuted(callback, "storeName1");
    assertCorrectOffsetsReportedByListener(callback, 0L, 9L, 10L);
    assertAllCallbackStatesExecuted(callbackOne, "storeName2");
    assertCorrectOffsetsReportedByListener(callbackOne, 0L, 4L, 5L);
    assertAllCallbackStatesExecuted(callbackTwo, "storeName3");
    assertCorrectOffsetsReportedByListener(callbackTwo, 0L, 2L, 3L);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) Test(org.junit.Test)

Example 2 with MockStateRestoreListener

use of org.apache.kafka.test.MockStateRestoreListener in project apache-kafka-on-k8s by banzaicloud.

the class AbstractTaskTest method createTask.

@SuppressWarnings("unchecked")
private AbstractTask createTask(final Consumer consumer, final Map<StateStore, String> stateStoresToChangelogTopics, final StateDirectory stateDirectory) {
    final Properties properties = new Properties();
    properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "app");
    properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummyhost:9092");
    final StreamsConfig config = new StreamsConfig(properties);
    final Map<String, String> storeNamesToChangelogTopics = new HashMap<>(stateStoresToChangelogTopics.size());
    for (final Map.Entry<StateStore, String> e : stateStoresToChangelogTopics.entrySet()) {
        storeNamesToChangelogTopics.put(e.getKey().name(), e.getValue());
    }
    return new AbstractTask(id, storeTopicPartitions, ProcessorTopology.withLocalStores(new ArrayList<>(stateStoresToChangelogTopics.keySet()), storeNamesToChangelogTopics), consumer, new StoreChangelogReader(consumer, new MockStateRestoreListener(), new LogContext("stream-task-test ")), false, stateDirectory, config) {

        @Override
        public void resume() {
        }

        @Override
        public void commit() {
        }

        @Override
        public void suspend() {
        }

        @Override
        public void close(final boolean clean, final boolean isZombie) {
        }

        @Override
        public void closeSuspended(final boolean clean, final boolean isZombie, final RuntimeException e) {
        }

        @Override
        public boolean initializeStateStores() {
            return false;
        }

        @Override
        public void initializeTopology() {
        }
    };
}
Also used : HashMap(java.util.HashMap) StateStore(org.apache.kafka.streams.processor.StateStore) ArrayList(java.util.ArrayList) LogContext(org.apache.kafka.common.utils.LogContext) MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) StreamsConfig(org.apache.kafka.streams.StreamsConfig)

Example 3 with MockStateRestoreListener

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

the class StreamThreadTest method shouldReturnActiveTaskMetadataWhileRunningState.

@Test
public void shouldReturnActiveTaskMetadataWhileRunningState() {
    internalTopologyBuilder.addSource(null, "source", null, null, null, topic1);
    clientSupplier.setCluster(createCluster());
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, APPLICATION_ID, config.getString(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG), mockTime);
    final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
    topologyMetadata.buildAndRewriteTopology();
    final StreamThread thread = StreamThread.create(topologyMetadata, config, clientSupplier, clientSupplier.getAdmin(config.getAdminConfigs(CLIENT_ID)), PROCESS_ID, CLIENT_ID, streamsMetrics, mockTime, streamsMetadataState, 0, stateDirectory, new MockStateRestoreListener(), threadIdx, null, HANDLER);
    thread.setState(StreamThread.State.STARTING);
    thread.rebalanceListener().onPartitionsRevoked(Collections.emptySet());
    final Map<TaskId, Set<TopicPartition>> activeTasks = new HashMap<>();
    final List<TopicPartition> assignedPartitions = new ArrayList<>();
    // assign single partition
    assignedPartitions.add(t1p1);
    activeTasks.put(task1, Collections.singleton(t1p1));
    thread.taskManager().handleAssignment(activeTasks, emptyMap());
    final MockConsumer<byte[], byte[]> mockConsumer = (MockConsumer<byte[], byte[]>) thread.mainConsumer();
    mockConsumer.assign(assignedPartitions);
    mockConsumer.updateBeginningOffsets(Collections.singletonMap(t1p1, 0L));
    thread.rebalanceListener().onPartitionsAssigned(assignedPartitions);
    thread.runOnce();
    final ThreadMetadata metadata = thread.threadMetadata();
    assertEquals(StreamThread.State.RUNNING.name(), metadata.threadState());
    assertTrue(metadata.activeTasks().contains(new TaskMetadataImpl(task1, Utils.mkSet(t1p1), new HashMap<>(), new HashMap<>(), Optional.empty())));
    assertTrue(metadata.standbyTasks().isEmpty());
    assertTrue("#threadState() was: " + metadata.threadState() + "; expected either RUNNING, STARTING, PARTITIONS_REVOKED, PARTITIONS_ASSIGNED, or CREATED", Arrays.asList("RUNNING", "STARTING", "PARTITIONS_REVOKED", "PARTITIONS_ASSIGNED", "CREATED").contains(metadata.threadState()));
    final String threadName = metadata.threadName();
    assertThat(threadName, startsWith(CLIENT_ID + "-StreamThread-" + threadIdx));
    assertEquals(threadName + "-consumer", metadata.consumerClientId());
    assertEquals(threadName + "-restore-consumer", metadata.restoreConsumerClientId());
    assertEquals(Collections.singleton(threadName + "-producer"), metadata.producerClientIds());
    assertEquals(CLIENT_ID + "-admin", metadata.adminClientId());
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) HashSet(java.util.HashSet) Collections.emptySet(java.util.Collections.emptySet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) TopicPartition(org.apache.kafka.common.TopicPartition) ThreadMetadata(org.apache.kafka.streams.ThreadMetadata) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) Test(org.junit.Test)

Example 4 with MockStateRestoreListener

use of org.apache.kafka.test.MockStateRestoreListener in project apache-kafka-on-k8s by banzaicloud.

the class KafkaStreamsTest method shouldNotSetGlobalRestoreListenerAfterStarting.

@Test
public void shouldNotSetGlobalRestoreListenerAfterStarting() {
    streams.start();
    try {
        streams.setGlobalStateRestoreListener(new MockStateRestoreListener());
        fail("Should throw an IllegalStateException");
    } catch (final IllegalStateException e) {
    // expected
    } finally {
        streams.close();
    }
}
Also used : MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 5 with MockStateRestoreListener

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

the class StreamThreadTest method createStreamThread.

private StreamThread createStreamThread(@SuppressWarnings("SameParameterValue") final String clientId, final StreamsConfig config, final boolean eosEnabled) {
    if (eosEnabled) {
        clientSupplier.setApplicationIdForProducer(APPLICATION_ID);
    }
    clientSupplier.setCluster(createCluster());
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, APPLICATION_ID, config.getString(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG), mockTime);
    final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
    topologyMetadata.buildAndRewriteTopology();
    return StreamThread.create(topologyMetadata, config, clientSupplier, clientSupplier.getAdmin(config.getAdminConfigs(clientId)), PROCESS_ID, clientId, streamsMetrics, mockTime, streamsMetadataState, 0, stateDirectory, new MockStateRestoreListener(), threadIdx, null, HANDLER);
}
Also used : MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)

Aggregations

MockStateRestoreListener (org.apache.kafka.test.MockStateRestoreListener)7 StreamsMetricsImpl (org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)4 Test (org.junit.Test)4 TopicPartition (org.apache.kafka.common.TopicPartition)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 LogContext (org.apache.kafka.common.utils.LogContext)2 StreamsConfig (org.apache.kafka.streams.StreamsConfig)2 TaskId (org.apache.kafka.streams.processor.TaskId)2 Collections.emptySet (java.util.Collections.emptySet)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConsumerGroupMetadata (org.apache.kafka.clients.consumer.ConsumerGroupMetadata)1 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)1 Metrics (org.apache.kafka.common.metrics.Metrics)1 MockTime (org.apache.kafka.common.utils.MockTime)1 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)1 ThreadMetadata (org.apache.kafka.streams.ThreadMetadata)1