Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FLINK-32020] Enable Dynamic Partition Discovery by Default in Kafka Source based on FLIP-288 #40

Closed
wants to merge 4 commits into from
Closed
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 @@ -22,6 +22,7 @@
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;

import java.time.Duration;
import java.util.Properties;
import java.util.function.Function;

Expand All @@ -38,10 +39,11 @@ public class KafkaSourceOptions {
public static final ConfigOption<Long> PARTITION_DISCOVERY_INTERVAL_MS =
ConfigOptions.key("partition.discovery.interval.ms")
.longType()
.noDefaultValue()
.defaultValue(Duration.ofMinutes(5).toMillis())
.withDescription(
"The interval in milliseconds for the Kafka source to discover "
+ "the new partitions. A non-positive value disables the partition discovery.");
+ "the new partitions. A non-positive value disables the partition discovery."
+ "The default value is 5 minutes, which is equal to the default value of metadata.max.age.ms in Kafka.");

public static final ConfigOption<Boolean> REGISTER_KAFKA_CONSUMER_METRICS =
ConfigOptions.key("register.consumer.metrics")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ public class KafkaConnectorOptions {
public static final ConfigOption<Duration> SCAN_TOPIC_PARTITION_DISCOVERY =
ConfigOptions.key("scan.topic-partition-discovery.interval")
.durationType()
.noDefaultValue()
.defaultValue(Duration.ofMinutes(5))
.withDescription(
"Optional interval for consumer to discover dynamically created Kafka partitions periodically.");
"Optional interval for consumer to discover dynamically created Kafka partitions periodically."
+ "The value 0 disables the partition discovery."
+ "The default value is 5 minutes, which is equal to the default value of metadata.max.age.ms in Kafka.");

// --------------------------------------------------------------------------------------------
// Sink specific options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ public DynamicTableSource createDynamicTableSource(Context context) {
final Properties properties = getKafkaProperties(context.getCatalogTable().getOptions());

// add topic-partition discovery
final Optional<Long> partitionDiscoveryInterval =
tableOptions.getOptional(SCAN_TOPIC_PARTITION_DISCOVERY).map(Duration::toMillis);
final Duration partitionDiscoveryInterval =
tableOptions.get(SCAN_TOPIC_PARTITION_DISCOVERY);
properties.setProperty(
KafkaSourceOptions.PARTITION_DISCOVERY_INTERVAL_MS.key(),
partitionDiscoveryInterval.orElse(-1L).toString());
Long.toString(partitionDiscoveryInterval.toMillis()));

final DataType physicalDataType = context.getPhysicalRowDataType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ public void testRunWithPeriodicPartitionDiscoveryOnceToCheckNoMoreSplit() throws
public void testRunWithDiscoverPartitionsOnceWithZeroMsToCheckNoMoreSplit() throws Throwable {
try (MockSplitEnumeratorContext<KafkaPartitionSplit> context =
new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
// set partitionDiscoveryIntervalMs = 0
KafkaSourceEnumerator enumerator = createEnumerator(context, 0L)) {
// Disable periodic partition discovery
KafkaSourceEnumerator enumerator = createEnumerator(context, false)) {

// Start the enumerator, and it should schedule a one time task to discover and assign
// partitions.
Expand Down Expand Up @@ -358,7 +358,6 @@ public void testWorkWithPreexistingAssignments() throws Throwable {
KafkaSourceEnumerator enumerator =
createEnumerator(
context2,
ENABLE_PERIODIC_PARTITION_DISCOVERY ? 1 : -1,
OffsetsInitializer.earliest(),
PRE_EXISTING_TOPICS,
preexistingAssignments,
Expand Down Expand Up @@ -390,7 +389,6 @@ public void testKafkaClientProperties() throws Exception {
KafkaSourceEnumerator enumerator =
createEnumerator(
context,
ENABLE_PERIODIC_PARTITION_DISCOVERY ? 1 : -1,
OffsetsInitializer.earliest(),
PRE_EXISTING_TOPICS,
Collections.emptySet(),
Expand Down Expand Up @@ -501,6 +499,33 @@ public void testPartitionChangeChecking() throws Throwable {
}
}

@Test
public void testEnablePartitionDiscoveryByDefault() throws Throwable {
try (MockSplitEnumeratorContext<KafkaPartitionSplit> context =
new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
KafkaSourceEnumerator enumerator = createEnumerator(context, new Properties())) {
enumerator.start();
long partitionDiscoveryIntervalMs =
(long) Whitebox.getInternalState(enumerator, "partitionDiscoveryIntervalMs");
assertThat(partitionDiscoveryIntervalMs)
.isEqualTo(KafkaSourceOptions.PARTITION_DISCOVERY_INTERVAL_MS.defaultValue());
assertThat(context.getPeriodicCallables()).isNotEmpty();
}
}

@Test
public void testDisablePartitionDiscovery() throws Throwable {
Properties props = new Properties();
props.setProperty(
KafkaSourceOptions.PARTITION_DISCOVERY_INTERVAL_MS.key(), String.valueOf(0));
try (MockSplitEnumeratorContext<KafkaPartitionSplit> context =
new MockSplitEnumeratorContext<>(NUM_SUBTASKS);
KafkaSourceEnumerator enumerator = createEnumerator(context, props)) {
enumerator.start();
assertThat(context.getPeriodicCallables()).isEmpty();
}
}

// -------------- some common startup sequence ---------------

private void startEnumeratorAndRegisterReaders(
Expand Down Expand Up @@ -539,13 +564,9 @@ private KafkaSourceEnumerator createEnumerator(
}

private KafkaSourceEnumerator createEnumerator(
MockSplitEnumeratorContext<KafkaPartitionSplit> enumContext,
long partitionDiscoveryIntervalMs) {
MockSplitEnumeratorContext<KafkaPartitionSplit> enumContext, Properties properties) {
return createEnumerator(
enumContext,
partitionDiscoveryIntervalMs,
EXCLUDE_DYNAMIC_TOPIC,
OffsetsInitializer.earliest());
enumContext, properties, EXCLUDE_DYNAMIC_TOPIC, OffsetsInitializer.earliest());
}

private KafkaSourceEnumerator createEnumerator(
Expand All @@ -557,20 +578,23 @@ private KafkaSourceEnumerator createEnumerator(
if (includeDynamicTopic) {
topics.add(DYNAMIC_TOPIC_NAME);
}
Properties props = new Properties();
props.setProperty(
KafkaSourceOptions.PARTITION_DISCOVERY_INTERVAL_MS.key(),
enablePeriodicPartitionDiscovery ? "1" : "-1");
return createEnumerator(
enumContext,
enablePeriodicPartitionDiscovery ? 1 : -1,
startingOffsetsInitializer,
topics,
Collections.emptySet(),
Collections.emptySet(),
false,
new Properties());
props);
}

private KafkaSourceEnumerator createEnumerator(
MockSplitEnumeratorContext<KafkaPartitionSplit> enumContext,
long partitionDiscoveryIntervalMs,
Properties props,
boolean includeDynamicTopic,
OffsetsInitializer startingOffsetsInitializer) {
List<String> topics = new ArrayList<>(PRE_EXISTING_TOPICS);
Expand All @@ -579,13 +603,12 @@ private KafkaSourceEnumerator createEnumerator(
}
return createEnumerator(
enumContext,
partitionDiscoveryIntervalMs,
startingOffsetsInitializer,
topics,
Collections.emptySet(),
Collections.emptySet(),
false,
new Properties());
props);
}

/**
Expand All @@ -594,7 +617,6 @@ private KafkaSourceEnumerator createEnumerator(
*/
private KafkaSourceEnumerator createEnumerator(
MockSplitEnumeratorContext<KafkaPartitionSplit> enumContext,
long partitionDiscoveryIntervalMs,
OffsetsInitializer startingOffsetsInitializer,
Collection<String> topicsToSubscribe,
Set<TopicPartition> assignedPartitions,
Expand All @@ -613,9 +635,6 @@ private KafkaSourceEnumerator createEnumerator(
Properties props =
new Properties(KafkaSourceTestEnv.getConsumerProperties(StringDeserializer.class));
KafkaSourceEnumerator.deepCopyProperties(overrideProperties, props);
props.setProperty(
KafkaSourceOptions.PARTITION_DISCOVERY_INTERVAL_MS.key(),
String.valueOf(partitionDiscoveryIntervalMs));

return new KafkaSourceEnumerator(
subscriber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,82 @@ public void testPrimaryKeyValidation() {
+ " guarantee the semantic of primary key.");
}

@Test
public void testDiscoverPartitionByDefault() {
Map<String, String> tableSourceOptions =
getModifiedOptions(
getBasicSourceOptions(),
options -> options.remove("scan.topic-partition-discovery.interval"));
final KafkaDynamicSource actualSource =
(KafkaDynamicSource) createTableSource(SCHEMA, tableSourceOptions);
Properties props = new Properties();
props.putAll(KAFKA_SOURCE_PROPERTIES);
// The default partition discovery interval is 5 minutes
props.setProperty("partition.discovery.interval.ms", "300000");
final Map<KafkaTopicPartition, Long> specificOffsets = new HashMap<>();
specificOffsets.put(new KafkaTopicPartition(TOPIC, PARTITION_0), OFFSET_0);
specificOffsets.put(new KafkaTopicPartition(TOPIC, PARTITION_1), OFFSET_1);
final DecodingFormat<DeserializationSchema<RowData>> valueDecodingFormat =
new DecodingFormatMock(",", true);
// Test scan source equals
final KafkaDynamicSource expectedKafkaSource =
createExpectedScanSource(
SCHEMA_DATA_TYPE,
null,
valueDecodingFormat,
new int[0],
new int[] {0, 1, 2},
null,
Collections.singletonList(TOPIC),
null,
props,
StartupMode.SPECIFIC_OFFSETS,
specificOffsets,
0);
assertThat(actualSource).isEqualTo(expectedKafkaSource);
ScanTableSource.ScanRuntimeProvider provider =
actualSource.getScanRuntimeProvider(ScanRuntimeProviderContext.INSTANCE);
assertKafkaSource(provider);
}

@Test
public void testDisableDiscoverPartition() {
Map<String, String> tableSourceOptions =
getModifiedOptions(
getBasicSourceOptions(),
options -> options.put("scan.topic-partition-discovery.interval", "0"));
final KafkaDynamicSource actualSource =
(KafkaDynamicSource) createTableSource(SCHEMA, tableSourceOptions);
Properties props = new Properties();
props.putAll(KAFKA_SOURCE_PROPERTIES);
// Disable discovery if the partition discovery interval is 0 minutes
props.setProperty("partition.discovery.interval.ms", "0");
final Map<KafkaTopicPartition, Long> specificOffsets = new HashMap<>();
specificOffsets.put(new KafkaTopicPartition(TOPIC, PARTITION_0), OFFSET_0);
specificOffsets.put(new KafkaTopicPartition(TOPIC, PARTITION_1), OFFSET_1);
final DecodingFormat<DeserializationSchema<RowData>> valueDecodingFormat =
new DecodingFormatMock(",", true);
// Test scan source equals
final KafkaDynamicSource expectedKafkaSource =
createExpectedScanSource(
SCHEMA_DATA_TYPE,
null,
valueDecodingFormat,
new int[0],
new int[] {0, 1, 2},
null,
Collections.singletonList(TOPIC),
null,
props,
StartupMode.SPECIFIC_OFFSETS,
specificOffsets,
0);
assertThat(actualSource).isEqualTo(expectedKafkaSource);
ScanTableSource.ScanRuntimeProvider provider =
actualSource.getScanRuntimeProvider(ScanRuntimeProviderContext.INSTANCE);
assertKafkaSource(provider);
}

// --------------------------------------------------------------------------------------------
// Utilities
// --------------------------------------------------------------------------------------------
Expand Down
Loading