Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2130,12 +2130,16 @@ public void processElement(OutputReceiver<KafkaSourceDescriptor> receiver) {
} else {
for (String topic : topics) {
List<PartitionInfo> partitionInfoList = consumer.partitionsFor(topic);
if (partitionInfoList == null || partitionInfoList.isEmpty()) {
if (logTopicVerification == null || !logTopicVerification) {
checkState(
partitionInfoList != null && !partitionInfoList.isEmpty(),
"Could not find any partitions info for topic %s. Please check Kafka configuration and make sure that provided topics exist.",
topic);
} else {
LOG.warn(
"Could not find any partitions info for topic {}. Please check Kafka "
+ "configuration and make sure that the provided topics exist.",
"Could not find any partitions info for topic {}. Please check Kafka configuration "
+ "and make sure that the provided topics exist.",
topic);
continue;
}
Comment on lines +2133 to 2143

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The logic in this revert is incorrect. If logTopicVerification is true, the else block executes for every topic, logging a warning even when partitionInfoList is valid. Furthermore, if partitionInfoList is null, the code will proceed to the loop at line 2145 and throw a NullPointerException because the continue statement was removed. The warning and skip logic should only trigger if partitionInfoList is actually null or empty.

Suggested change
if (logTopicVerification == null || !logTopicVerification) {
checkState(
partitionInfoList != null && !partitionInfoList.isEmpty(),
"Could not find any partitions info for topic %s. Please check Kafka configuration and make sure that provided topics exist.",
topic);
} else {
LOG.warn(
"Could not find any partitions info for topic {}. Please check Kafka "
+ "configuration and make sure that the provided topics exist.",
"Could not find any partitions info for topic {}. Please check Kafka configuration "
+ "and make sure that the provided topics exist.",
topic);
continue;
}
if (logTopicVerification == null || !logTopicVerification) {
checkState(
partitionInfoList != null && !partitionInfoList.isEmpty(),
"Could not find any partitions info for topic %s. Please check Kafka configuration and make sure that provided topics exist.",
topic);
} else if (partitionInfoList == null || partitionInfoList.isEmpty()) {
LOG.warn(
"Could not find any partitions info for topic {}. Please check Kafka configuration "
+ "and make sure that the provided topics exist.",
topic);
continue;
}


for (PartitionInfo p : partitionInfoList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.beam.sdk.io.kafka;

import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.MoreObjects.firstNonNull;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -44,8 +45,6 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.joda.time.Duration;
import org.joda.time.Instant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A {@link PTransform} for continuously querying Kafka for new partitions, and emitting those
Expand All @@ -58,7 +57,6 @@
*/
class WatchForKafkaTopicPartitions extends PTransform<PBegin, PCollection<KafkaSourceDescriptor>> {

private static final Logger LOG = LoggerFactory.getLogger(WatchForKafkaTopicPartitions.class);
private static final Duration DEFAULT_CHECK_DURATION = Duration.standardHours(1);
private static final String COUNTER_NAMESPACE = "watch_kafka_topic_partition";

Expand Down Expand Up @@ -193,13 +191,10 @@ static List<TopicPartition> getAllTopicPartitions(
if (topics != null && !topics.isEmpty()) {
for (String topic : topics) {
List<PartitionInfo> partitionInfoList = kafkaConsumer.partitionsFor(topic);
if (partitionInfoList == null || partitionInfoList.isEmpty()) {
LOG.warn(
"Could not find any partitions info for topic {}. Please check Kafka "
+ "configuration and make sure that the provided topics exist.",
topic);
continue;
}
checkState(
partitionInfoList != null && !partitionInfoList.isEmpty(),
"Could not find any partitions info for topic %s. Please check Kafka configuration and make sure that provided topics exist.",
topic);
for (PartitionInfo partition : partitionInfoList) {
current.add(new TopicPartition(topic, partition.partition()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
package org.apache.beam.sdk.io.kafka;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.beam.sdk.io.kafka.KafkaMocks.PartitionGrowthMockConsumer;
Expand Down Expand Up @@ -110,47 +108,6 @@ public void testGetAllTopicPartitionsWithGivenTopics() throws Exception {
(input) -> mockConsumer, null, givenTopics, null));
}

@Test
public void testGetAllTopicPartitionsWithNullPartitionInfo() throws Exception {
Set<String> givenTopics = ImmutableSet.of("topic1");

Consumer<byte[], byte[]> mockConsumer = Mockito.mock(Consumer.class);
when(mockConsumer.partitionsFor("topic1")).thenReturn(null);
assertTrue(
WatchForKafkaTopicPartitions.getAllTopicPartitions(
(input) -> mockConsumer, null, givenTopics, null)
.isEmpty());
}

@Test
public void testGetAllTopicPartitionsWithEmptyPartitionInfo() throws Exception {
Set<String> givenTopics = ImmutableSet.of("topic1");

Consumer<byte[], byte[]> mockConsumer = Mockito.mock(Consumer.class);
when(mockConsumer.partitionsFor("topic1")).thenReturn(Collections.emptyList());
assertTrue(
WatchForKafkaTopicPartitions.getAllTopicPartitions(
(input) -> mockConsumer, null, givenTopics, null)
.isEmpty());
}

@Test
public void testGetAllTopicPartitionsSkipsMissingTopics() throws Exception {
Set<String> givenTopics = ImmutableSet.of("topic1", "topic2");

Consumer<byte[], byte[]> mockConsumer = Mockito.mock(Consumer.class);
when(mockConsumer.partitionsFor("topic1")).thenReturn(null);
when(mockConsumer.partitionsFor("topic2"))
.thenReturn(
ImmutableList.of(
new PartitionInfo("topic2", 0, null, null, null),
new PartitionInfo("topic2", 1, null, null, null)));
assertEquals(
ImmutableList.of(new TopicPartition("topic2", 0), new TopicPartition("topic2", 1)),
WatchForKafkaTopicPartitions.getAllTopicPartitions(
(input) -> mockConsumer, null, givenTopics, null));
}

@Test
public void testGetAllTopicPartitionsWithGivenPattern() throws Exception {
Consumer<byte[], byte[]> mockConsumer = Mockito.mock(Consumer.class);
Expand Down
Loading