slachiewicz commented on code in PR #347:
URL: 
https://github.com/apache/flink-connector-kafka/pull/347#discussion_r4786159922


##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/KafkaPropertiesUtil.java:
##########
@@ -43,50 +45,80 @@ 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>{@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 
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) {
+        Optional<OffsetResetStrategy> strategy = 
parseOffsetResetStrategy(offsetResetConfig);

Review Comment:
   Done: `normalizeAutoOffsetReset` and `parseOffsetResetStrategy` trim first, 
and the trimmed value is what reaches the consumer.



##########
flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/source/KafkaPropertiesUtilTest.java:
##########
@@ -26,47 +26,85 @@
 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.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.LATEST);
+                .isEqualTo("latest");
     }
 
     @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 = {"errorStrategy", "by_duration:", 
"by_duration:-PT1H", " latest"})

Review Comment:
   Moved: `testTrimsEnumStrategiesLikeTheConsumer` and 
`testTrimsByDurationLikeTheConsumer` cover padded values.



##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/KafkaPropertiesUtil.java:
##########
@@ -43,50 +45,80 @@ 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>{@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 
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) {
+        Optional<OffsetResetStrategy> strategy = 
parseOffsetResetStrategy(offsetResetConfig);
+        if (strategy.isPresent()) {
+            return strategy.get().name().toLowerCase(Locale.ROOT);
+        }
+        ConfigDef.Validator validator =
+                ConsumerConfig.configDef()
+                        .configKeys()
+                        .get(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG)
+                        .validator;
+        try {
+            validator.ensureValid(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, 
offsetResetConfig);
+        } catch (ConfigException e) {
+            throw new IllegalArgumentException(
+                    String.format(
+                            "%s can not be set to %s. Valid values: %s",
+                            ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, 
offsetResetConfig, validator),
+                    e);
+        }
+        return offsetResetConfig;
+    }
+
+    /**
+     * Returns the {@link OffsetResetStrategy} named by {@code 
offsetResetConfig}, ignoring case, or
+     * an empty result for a strategy the enum cannot represent, such as 
{@code by_duration}. It
+     * does not validate the value; see {@link 
#normalizeAutoOffsetReset(String)}.
+     */
+    public static Optional<OffsetResetStrategy> parseOffsetResetStrategy(
+            @Nonnull String offsetResetConfig) {
         return Arrays.stream(OffsetResetStrategy.values())
-                .filter(
-                        offsetResetStrategy ->
-                                offsetResetStrategy
-                                        .name()
-                                        
.equals(offsetResetConfig.toUpperCase(Locale.ROOT)))
-                .findAny()
-                .orElseThrow(
-                        () ->
-                                new IllegalArgumentException(
-                                        String.format(
-                                                "%s can not be set to %s. 
Valid values: [%s]",
-                                                
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
-                                                offsetResetConfig,
-                                                
Arrays.stream(OffsetResetStrategy.values())
-                                                        .map(Enum::name)
-                                                        
.map(String::toLowerCase)
-                                                        
.collect(Collectors.joining(",")))));
+                .filter(strategy -> 
strategy.name().equalsIgnoreCase(offsetResetConfig))
+                .findAny();
     }
 
-    /** Returns whether the configured strategy opposes a positional 
initializer strategy. */
+    /** Returns whether the configured reset opposes a positional initializer 
strategy. */
     public static boolean hasOpposingOffsetResetStrategies(
-            @Nonnull OffsetResetStrategy configuredResetStrategy,
+            @Nonnull String configuredOffsetReset,
             @Nonnull OffsetsInitializer startingOffsetsInitializer) {
         OffsetResetStrategy initializerResetStrategy =
                 startingOffsetsInitializer.getAutoOffsetResetStrategy();
-        return isPositionalResetStrategy(configuredResetStrategy)
-                && isPositionalResetStrategy(initializerResetStrategy)
-                && configuredResetStrategy != initializerResetStrategy;
+        return parseOffsetResetStrategy(configuredOffsetReset)
+                .map(
+                        configuredResetStrategy ->
+                                
isPositionalResetStrategy(configuredResetStrategy)
+                                        && 
isPositionalResetStrategy(initializerResetStrategy)
+                                        && configuredResetStrategy != 
initializerResetStrategy)
+                .orElse(false);

Review Comment:
   Agreed: `by_duration` now opposes an `earliest` or `latest` initializer 
strategy, still not one that resolves concrete offsets.



##########
flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/dynamic/source/enumerator/DynamicKafkaSourceEnumerator.java:
##########
@@ -700,12 +699,10 @@ private KafkaSourceEnumerator 
createEnumeratorWithAssignedTopicPartitions(
         KafkaPropertiesUtil.copyProperties(properties, consumerProps);
         
DynamicKafkaSourceOptions.removeRemovedClusterRetentionOption(consumerProps);
         KafkaPropertiesUtil.setClientIdPrefix(consumerProps, kafkaClusterId);
-        OffsetResetStrategy effectiveOffsetResetStrategy =
-                KafkaPropertiesUtil.resolveAutoOffsetResetStrategy(
-                        properties, fetchedProperties, 
effectiveStartingOffsetsInitializer);
         consumerProps.setProperty(
                 ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
-                effectiveOffsetResetStrategy.name().toLowerCase());
+                KafkaPropertiesUtil.resolveAutoOffsetReset(

Review Comment:
   Added `DynamicKafkaSourceITTest#testByDurationResetFromClusterProperties`: 
`committedOffsets()` with `by_duration:PT1H` in the cluster properties must 
skip records timestamped two hours ago. Reverting either the enumerator or the 
reader line fails it with `IllegalArgumentException`.



-- 
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]

Reply via email to