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);
}
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() {
}
};
}
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());
}
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();
}
}
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);
}
Aggregations