slachiewicz commented on code in PR #347:
URL:
https://github.com/apache/flink-connector-kafka/pull/347#discussion_r4786384783
##########
flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/dynamic/source/DynamicKafkaSourceBuilderTest.java:
##########
@@ -82,6 +82,18 @@ void testAutoOffsetResetUsesExplicitProperty() {
.isEqualTo("none");
}
+ @Test
+ void testAutoOffsetResetAcceptsByDuration() {
Review Comment:
Added `testAutoOffsetResetRejectsValueTheConsumerRejects`, matching the one
in `KafkaSourceBuilderTest`.
##########
flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/source/KafkaPropertiesUtilTest.java:
##########
@@ -27,48 +27,101 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Locale;
import java.util.Properties;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** Tests for {@link KafkaPropertiesUtil}. */
class KafkaPropertiesUtilTest {
@Test
void testUsesInitializerStrategyWhenResetPropertiesAreAbsent() {
assertThat(
- KafkaPropertiesUtil.resolveAutoOffsetResetStrategy(
+ KafkaPropertiesUtil.resolveAutoOffsetReset(
new Properties(), new Properties(),
OffsetsInitializer.earliest()))
- .isEqualTo(OffsetResetStrategy.EARLIEST);
+ .isEqualTo("earliest");
assertThat(
- KafkaPropertiesUtil.resolveAutoOffsetResetStrategy(
+ KafkaPropertiesUtil.resolveAutoOffsetReset(
new Properties(), new Properties(),
OffsetsInitializer.latest()))
- .isEqualTo(OffsetResetStrategy.EARLIEST);
+ .isEqualTo("earliest");
}
@Test
void testClusterResetPropertyOverridesInitializerStrategy() {
assertThat(
- KafkaPropertiesUtil.resolveAutoOffsetResetStrategy(
+ KafkaPropertiesUtil.resolveAutoOffsetReset(
new Properties(),
resetProperties("none"),
OffsetsInitializer.earliest()))
- .isEqualTo(OffsetResetStrategy.NONE);
+ .isEqualTo("none");
}
@Test
void testClusterResetPropertyOverridesGlobalAndInitializerStrategies() {
assertThat(
- KafkaPropertiesUtil.resolveAutoOffsetResetStrategy(
+ KafkaPropertiesUtil.resolveAutoOffsetReset(
resetProperties("none"),
resetProperties("earliest"),
OffsetsInitializer.latest()))
- .isEqualTo(OffsetResetStrategy.EARLIEST);
+ .isEqualTo("earliest");
+ }
+
+ @Test
+ void testResolvesByDurationReset() {
+ assertThat(
+ KafkaPropertiesUtil.resolveAutoOffsetReset(
+ resetProperties("by_duration:PT1H"),
+ new Properties(),
+ OffsetsInitializer.earliest()))
+ .isEqualTo("by_duration:PT1H");
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"earliest", "EARLIEST", "Latest", "none"})
+ void testNormalizesEnumStrategiesToLowerCase(String offsetReset) {
+ assertThat(KafkaPropertiesUtil.normalizeAutoOffsetReset(offsetReset))
+ .isEqualTo(offsetReset.toLowerCase(Locale.ROOT));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"by_duration:PT1H", "by_duration:P1DT30M"})
+ void testAcceptsByDurationUnchanged(String offsetReset) {
+ assertThat(KafkaPropertiesUtil.normalizeAutoOffsetReset(offsetReset))
+ .isEqualTo(offsetReset);
+
assertThat(KafkaPropertiesUtil.parseOffsetResetStrategy(offsetReset)).isEmpty();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {" latest", "latest ", "\tEarliest\n"})
+ void testTrimsEnumStrategiesLikeTheConsumer(String offsetReset) {
+ assertThat(KafkaPropertiesUtil.normalizeAutoOffsetReset(offsetReset))
+ .isEqualTo(offsetReset.trim().toLowerCase(Locale.ROOT));
+
assertThat(KafkaPropertiesUtil.parseOffsetResetStrategy(offsetReset)).isPresent();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"by_duration:PT1H ", " by_duration:PT1H"})
+ void testTrimsByDurationLikeTheConsumer(String offsetReset) {
+ assertThat(KafkaPropertiesUtil.normalizeAutoOffsetReset(offsetReset))
+ .isEqualTo("by_duration:PT1H");
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"errorStrategy", "by_duration:",
"by_duration:-PT1H", "late st"})
Review Comment:
`by_duration:-PT1H` and `by_duration:` were already there; added
`by_duration:1h` and `by_duration:PT-1H`. The connector runs the consumer's own
validator, so it rejects exactly what kafka-clients rejects.
##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/KafkaPropertiesUtil.java:
##########
@@ -43,57 +45,95 @@ public static void copyProperties(@Nonnull Properties from,
@Nonnull Properties
}
}
- /** Resolves an explicit cluster or global reset strategy before the
initializer default. */
- public static OffsetResetStrategy resolveAutoOffsetResetStrategy(
+ /**
+ * Resolves an explicit cluster or global {@code auto.offset.reset} before
the initializer
+ * default, and returns it in the form {@link
#normalizeAutoOffsetReset(String)} produces.
+ */
+ public static String resolveAutoOffsetReset(
@Nonnull Properties globalProperties,
@Nonnull Properties clusterProperties,
@Nonnull OffsetsInitializer startingOffsetsInitializer) {
- return getResetStrategy(
+ return normalizeAutoOffsetReset(
clusterProperties.getProperty(
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
globalProperties.getProperty(
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
startingOffsetsInitializer.getAutoOffsetResetStrategy().name())));
}
- /** Parses the configured auto offset reset strategy. */
- public static OffsetResetStrategy getResetStrategy(@Nonnull String
offsetResetConfig) {
+ /**
+ * Validates a configured {@code auto.offset.reset} and returns the value
to pass to the
+ * consumer.
+ *
+ * <p>Leading and trailing whitespace is removed first, as the consumer
does. {@code earliest},
+ * {@code latest} and {@code none} are accepted in any case and returned
in lower case. Any
+ * other value, such as {@code by_duration:PT1H}, is checked by the
validator of the {@code
+ * kafka-clients} version on the classpath and returned otherwise
unchanged, so the connector
+ * accepts exactly the strategies the consumer accepts.
+ *
+ * @throws IllegalArgumentException if the consumer would reject the value
+ */
+ public static String normalizeAutoOffsetReset(@Nonnull String
offsetResetConfig) {
+ String trimmed = offsetResetConfig.trim();
+ Optional<OffsetResetStrategy> strategy =
parseOffsetResetStrategy(trimmed);
Review Comment:
Taken.
##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/KafkaPropertiesUtil.java:
##########
@@ -43,57 +45,95 @@ public static void copyProperties(@Nonnull Properties from,
@Nonnull Properties
}
}
- /** Resolves an explicit cluster or global reset strategy before the
initializer default. */
- public static OffsetResetStrategy resolveAutoOffsetResetStrategy(
+ /**
+ * Resolves an explicit cluster or global {@code auto.offset.reset} before
the initializer
+ * default, and returns it in the form {@link
#normalizeAutoOffsetReset(String)} produces.
+ */
+ public static String resolveAutoOffsetReset(
Review Comment:
Agreed that's where this should go. Kafka's replacement,
`AutoOffsetResetStrategy`, is internal to kafka-clients, so the connector would
need its own type, and that changes `@PublicEvolving` API:
`OffsetsInitializer#getAutoOffsetResetStrategy()` and
`committedOffsets(OffsetResetStrategy)`. I'd keep this PR to the fix, since it
also goes to v5.1, and open a follow-up ticket for the type, with a
mailing-list thread if the API change needs one. Does that work for you?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]