use of org.apache.flink.streaming.connectors.kafka.internals.KafkaTopicsDescriptor in project flink by apache.
the class FlinkKafkaConsumerBaseTest method checkFilterRestoredPartitionsWithDisovered.
private void checkFilterRestoredPartitionsWithDisovered(List<String> restoredKafkaTopics, List<String> initKafkaTopics, List<String> expectedSubscribedPartitions, Boolean disableFiltering) throws Exception {
final AbstractPartitionDiscoverer discoverer = new TestPartitionDiscoverer(new KafkaTopicsDescriptor(initKafkaTopics, null), 0, 1, TestPartitionDiscoverer.createMockGetAllTopicsSequenceFromFixedReturn(initKafkaTopics), TestPartitionDiscoverer.createMockGetAllPartitionsFromTopicsSequenceFromFixedReturn(initKafkaTopics.stream().map(topic -> new KafkaTopicPartition(topic, 0)).collect(Collectors.toList())));
final FlinkKafkaConsumerBase<String> consumer = new DummyFlinkKafkaConsumer<>(initKafkaTopics, discoverer);
if (disableFiltering) {
consumer.disableFilterRestoredPartitionsWithSubscribedTopics();
}
final TestingListState<Tuple2<KafkaTopicPartition, Long>> listState = new TestingListState<>();
for (int i = 0; i < restoredKafkaTopics.size(); i++) {
listState.add(new Tuple2<>(new KafkaTopicPartition(restoredKafkaTopics.get(i), 0), 12345L));
}
setupConsumer(consumer, true, listState, true, 0, 1);
Map<KafkaTopicPartition, Long> subscribedPartitionsToStartOffsets = consumer.getSubscribedPartitionsToStartOffsets();
assertEquals(new HashSet<>(expectedSubscribedPartitions), subscribedPartitionsToStartOffsets.keySet().stream().map(partition -> partition.getTopic()).collect(Collectors.toSet()));
}
use of org.apache.flink.streaming.connectors.kafka.internals.KafkaTopicsDescriptor in project flink by apache.
the class FlinkKafkaConsumerBaseTest method testRescaling.
/**
* Tests whether the Kafka consumer behaves correctly when scaling the parallelism up/down,
* which means that operator state is being reshuffled.
*
* <p>This also verifies that a restoring source is always impervious to changes in the list of
* topics fetched from Kafka.
*/
@SuppressWarnings("unchecked")
private void testRescaling(final int initialParallelism, final int numPartitions, final int restoredParallelism, final int restoredNumPartitions) throws Exception {
Preconditions.checkArgument(restoredNumPartitions >= numPartitions, "invalid test case for Kafka repartitioning; Kafka only allows increasing partitions.");
List<KafkaTopicPartition> mockFetchedPartitionsOnStartup = new ArrayList<>();
for (int i = 0; i < numPartitions; i++) {
mockFetchedPartitionsOnStartup.add(new KafkaTopicPartition("test-topic", i));
}
DummyFlinkKafkaConsumer<String>[] consumers = new DummyFlinkKafkaConsumer[initialParallelism];
AbstractStreamOperatorTestHarness<String>[] testHarnesses = new AbstractStreamOperatorTestHarness[initialParallelism];
List<String> testTopics = Collections.singletonList("test-topic");
for (int i = 0; i < initialParallelism; i++) {
TestPartitionDiscoverer partitionDiscoverer = new TestPartitionDiscoverer(new KafkaTopicsDescriptor(testTopics, null), i, initialParallelism, TestPartitionDiscoverer.createMockGetAllTopicsSequenceFromFixedReturn(testTopics), TestPartitionDiscoverer.createMockGetAllPartitionsFromTopicsSequenceFromFixedReturn(mockFetchedPartitionsOnStartup));
consumers[i] = new DummyFlinkKafkaConsumer<>(testTopics, partitionDiscoverer);
testHarnesses[i] = createTestHarness(consumers[i], initialParallelism, i);
// initializeState() is always called, null signals that we didn't restore
testHarnesses[i].initializeEmptyState();
testHarnesses[i].open();
}
Map<KafkaTopicPartition, Long> globalSubscribedPartitions = new HashMap<>();
for (int i = 0; i < initialParallelism; i++) {
Map<KafkaTopicPartition, Long> subscribedPartitions = consumers[i].getSubscribedPartitionsToStartOffsets();
// make sure that no one else is subscribed to these partitions
for (KafkaTopicPartition partition : subscribedPartitions.keySet()) {
assertThat(globalSubscribedPartitions, not(hasKey(partition)));
}
globalSubscribedPartitions.putAll(subscribedPartitions);
}
assertThat(globalSubscribedPartitions.values(), hasSize(numPartitions));
assertThat(mockFetchedPartitionsOnStartup, everyItem(isIn(globalSubscribedPartitions.keySet())));
OperatorSubtaskState[] state = new OperatorSubtaskState[initialParallelism];
for (int i = 0; i < initialParallelism; i++) {
state[i] = testHarnesses[i].snapshot(0, 0);
}
OperatorSubtaskState mergedState = AbstractStreamOperatorTestHarness.repackageState(state);
// -----------------------------------------------------------------------------------------
// restore
List<KafkaTopicPartition> mockFetchedPartitionsAfterRestore = new ArrayList<>();
for (int i = 0; i < restoredNumPartitions; i++) {
mockFetchedPartitionsAfterRestore.add(new KafkaTopicPartition("test-topic", i));
}
DummyFlinkKafkaConsumer<String>[] restoredConsumers = new DummyFlinkKafkaConsumer[restoredParallelism];
AbstractStreamOperatorTestHarness<String>[] restoredTestHarnesses = new AbstractStreamOperatorTestHarness[restoredParallelism];
for (int i = 0; i < restoredParallelism; i++) {
OperatorSubtaskState initState = AbstractStreamOperatorTestHarness.repartitionOperatorState(mergedState, maxParallelism, initialParallelism, restoredParallelism, i);
TestPartitionDiscoverer partitionDiscoverer = new TestPartitionDiscoverer(new KafkaTopicsDescriptor(testTopics, null), i, restoredParallelism, TestPartitionDiscoverer.createMockGetAllTopicsSequenceFromFixedReturn(testTopics), TestPartitionDiscoverer.createMockGetAllPartitionsFromTopicsSequenceFromFixedReturn(mockFetchedPartitionsAfterRestore));
restoredConsumers[i] = new DummyFlinkKafkaConsumer<>(testTopics, partitionDiscoverer);
restoredTestHarnesses[i] = createTestHarness(restoredConsumers[i], restoredParallelism, i);
// initializeState() is always called, null signals that we didn't restore
restoredTestHarnesses[i].initializeState(initState);
restoredTestHarnesses[i].open();
}
Map<KafkaTopicPartition, Long> restoredGlobalSubscribedPartitions = new HashMap<>();
for (int i = 0; i < restoredParallelism; i++) {
Map<KafkaTopicPartition, Long> subscribedPartitions = restoredConsumers[i].getSubscribedPartitionsToStartOffsets();
// make sure that no one else is subscribed to these partitions
for (KafkaTopicPartition partition : subscribedPartitions.keySet()) {
assertThat(restoredGlobalSubscribedPartitions, not(hasKey(partition)));
}
restoredGlobalSubscribedPartitions.putAll(subscribedPartitions);
}
assertThat(restoredGlobalSubscribedPartitions.values(), hasSize(restoredNumPartitions));
assertThat(mockFetchedPartitionsOnStartup, everyItem(isIn(restoredGlobalSubscribedPartitions.keySet())));
}
Aggregations