Search in sources :

Example 6 with MockStateRestoreListener

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

the class StreamThreadTest method shouldEnforceRebalanceAfterNextScheduledProbingRebalanceTime.

@Test
public void shouldEnforceRebalanceAfterNextScheduledProbingRebalanceTime() throws InterruptedException {
    final StreamsConfig config = new StreamsConfig(configProps(false));
    internalTopologyBuilder.buildTopology();
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, APPLICATION_ID, config.getString(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG), mockTime);
    final Consumer<byte[], byte[]> mockConsumer = EasyMock.createNiceMock(Consumer.class);
    expect(mockConsumer.poll(anyObject())).andStubReturn(ConsumerRecords.empty());
    final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
    expect(mockConsumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
    expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
    EasyMock.replay(consumerGroupMetadata);
    final EasyMockConsumerClientSupplier mockClientSupplier = new EasyMockConsumerClientSupplier(mockConsumer);
    mockClientSupplier.setCluster(createCluster());
    EasyMock.replay(mockConsumer);
    final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
    topologyMetadata.buildAndRewriteTopology();
    final StreamThread thread = StreamThread.create(topologyMetadata, config, mockClientSupplier, mockClientSupplier.getAdmin(config.getAdminConfigs(CLIENT_ID)), PROCESS_ID, CLIENT_ID, streamsMetrics, mockTime, streamsMetadataState, 0, stateDirectory, new MockStateRestoreListener(), threadIdx, null, null);
    mockConsumer.enforceRebalance();
    mockClientSupplier.nextRebalanceMs().set(mockTime.milliseconds() - 1L);
    thread.start();
    TestUtils.waitForCondition(() -> thread.state() == StreamThread.State.STARTING, 10 * 1000, "Thread never started.");
    TestUtils.retryOnExceptionWithTimeout(() -> verify(mockConsumer));
    thread.shutdown();
    TestUtils.waitForCondition(() -> thread.state() == StreamThread.State.DEAD, 10 * 1000, "Thread never shut down.");
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 7 with MockStateRestoreListener

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

the class StreamThreadStateStoreProviderTest method createStreamsTask.

private StreamTask createStreamsTask(final StreamsConfig streamsConfig, final MockClientSupplier clientSupplier, final ProcessorTopology topology, final TaskId taskId) {
    final Metrics metrics = new Metrics();
    final LogContext logContext = new LogContext("test-stream-task ");
    final Set<TopicPartition> partitions = Collections.singleton(new TopicPartition(topicName, taskId.partition()));
    final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, Task.TaskType.ACTIVE, StreamsConfigUtils.eosEnabled(streamsConfig), logContext, stateDirectory, new StoreChangelogReader(new MockTime(), streamsConfig, logContext, clientSupplier.adminClient, clientSupplier.restoreConsumer, new MockStateRestoreListener()), topology.storeToChangelogTopic(), partitions);
    final RecordCollector recordCollector = new RecordCollectorImpl(logContext, taskId, new StreamsProducer(streamsConfig, "threadId", clientSupplier, new TaskId(0, 0), UUID.randomUUID(), logContext, Time.SYSTEM), streamsConfig.defaultProductionExceptionHandler(), new MockStreamsMetrics(metrics));
    final StreamsMetricsImpl streamsMetrics = new MockStreamsMetrics(metrics);
    final InternalProcessorContext context = new ProcessorContextImpl(taskId, streamsConfig, stateManager, streamsMetrics, null);
    return new StreamTask(taskId, partitions, topology, clientSupplier.consumer, new TopologyConfig(null, streamsConfig, new Properties()).getTaskConfig(), streamsMetrics, stateDirectory, EasyMock.createNiceMock(ThreadCache.class), new MockTime(), stateManager, recordCollector, context, logContext);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) RecordCollector(org.apache.kafka.streams.processor.internals.RecordCollector) StreamsProducer(org.apache.kafka.streams.processor.internals.StreamsProducer) InternalProcessorContext(org.apache.kafka.streams.processor.internals.InternalProcessorContext) LogContext(org.apache.kafka.common.utils.LogContext) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) ProcessorContextImpl(org.apache.kafka.streams.processor.internals.ProcessorContextImpl) Properties(java.util.Properties) RecordCollectorImpl(org.apache.kafka.streams.processor.internals.RecordCollectorImpl) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) TopicPartition(org.apache.kafka.common.TopicPartition) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) StoreChangelogReader(org.apache.kafka.streams.processor.internals.StoreChangelogReader) ProcessorStateManager(org.apache.kafka.streams.processor.internals.ProcessorStateManager) MockTime(org.apache.kafka.common.utils.MockTime) StreamTask(org.apache.kafka.streams.processor.internals.StreamTask) TopologyConfig(org.apache.kafka.streams.processor.internals.namedtopology.TopologyConfig)

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