Efrat19 commented on code in PR #347:
URL:
https://github.com/apache/flink-connector-kafka/pull/347#discussion_r4773947913
##########
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:
Also add a reject test?
##########
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:
I wonder if instead of a `String`, we should come up with our own
replacement for the deprecated `OffsetResetStrategy` which will also be able
to process by_duration and
(though its quite a change, maybe worth a small discussion in the mailing
list)
We can support `OffsetResetStrategy` conversion if can't be dome in a single
pr, wdyt?
##########
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:
Can we also cover negative duration and non-ISO8601 format to avoid runtime
failures? according to
[docs](https://kafka.apache.org/40/configuration/consumer-configs/#consumerconfigs_auto.offset.reset)
##########
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:
```suggestion
Optional<OffsetResetStrategy> strategy =
parseOffsetResetStrategy(offsetResetConfig);
```
Already trimmed inside `parseOffsetResetStrategy`
--
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]