dannycranmer commented on a change in pull request #18553:
URL: https://github.com/apache/flink/pull/18553#discussion_r800830020



##########
File path: 
flink-connectors/flink-connector-aws-kinesis-data-streams/src/main/java/org/apache/flink/connector/kinesis/sink/KinesisDataStreamsSinkWriter.java
##########
@@ -56,6 +61,31 @@
 class KinesisDataStreamsSinkWriter<InputT> extends AsyncSinkWriter<InputT, 
PutRecordsRequestEntry> {
     private static final Logger LOG = 
LoggerFactory.getLogger(KinesisDataStreamsSinkWriter.class);
 
+    private static final RetryableExceptionClassifier 
RESOURCE_NOT_FOUND_STRATEGY =
+            RetryableExceptionClassifier.withRootCauseOfType(
+                    ResourceNotFoundException.class,
+                    err ->
+                            new KinesisDataStreamsException(
+                                    "Encountered non-recoverable exception 
relating to not being able to find the specified resources",
+                                    err));
+
+    private static final RetryableExceptionClassifier 
NON_RECOVERABLE_EXCEPTION_STRATEGY =
+            RetryableExceptionClassifier.withRootCauseOfType(
+                    Error.class,
+                    err ->
+                            new KinesisDataStreamsException(
+                                    "Encountered non-recoverable exception in 
the Kinesis Data Streams Sink",
+                                    err));
+
+    private static final RetryableExceptionClassifier 
KINESIS_RETRY_VALIDATION_STRATEGY =
+            RetryableExceptionClassifier.createChain(
+                    GENERAL_ERROR_STRATEGY.clone(),

Review comment:
       Cloning a singleton instance is an odd way to achieve this. Factories 
with builder methods or Builders are a more standard approach. For instance 
`AsyncSinkThrowableWrappers.generalErrorStrategy()`, can we please refactor 
this?

##########
File path: 
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/util/RetryableExceptionClassifier.java
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.connector.base.sink.util;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+/** Util class acting as a classifier for exception to suppress. */
+@Internal
+public class RetryableExceptionClassifier {
+    private final Function<Throwable, Exception> throwableMapper;
+    private final Predicate<Throwable> validator;
+    private RetryableExceptionClassifier chainedClassifier;
+
+    public RetryableExceptionClassifier(
+            Predicate<Throwable> validator, Function<Throwable, Exception> 
throwableMapper) {
+        this.throwableMapper = throwableMapper;
+        this.validator = validator;
+        this.chainedClassifier = null;
+    }
+
+    public boolean shouldSuppress(Throwable err, Consumer<Exception> 
throwableConsumer) {
+        if (validator.test(err)) {
+            throwableConsumer.accept(throwableMapper.apply(err));
+            return false;
+        }
+
+        if (chainedClassifier != null) {
+            return chainedClassifier.shouldSuppress(err, throwableConsumer);
+        } else {
+            return true;
+        }
+    }
+
+    @Override
+    public RetryableExceptionClassifier clone() {
+        return new RetryableExceptionClassifier(validator, throwableMapper);
+    }
+
+    public static RetryableExceptionClassifier withRootCauseOfType(
+            Class<? extends Throwable> type, Function<Throwable, Exception> 
mapper) {
+        return new RetryableExceptionClassifier(
+                err -> ExceptionUtils.findThrowable(err, type).isPresent(), 
mapper);
+    }
+
+    public static RetryableExceptionClassifier withRootCauseWithMessage(
+            String message, Function<Throwable, Exception> mapper) {
+        return new RetryableExceptionClassifier(
+                err -> ExceptionUtils.findThrowableWithMessage(err, 
message).isPresent(), mapper);
+    }
+
+    public static RetryableExceptionClassifier createChain(
+            RetryableExceptionClassifier... classifiers) {
+        Set<RetryableExceptionClassifier> importedWrappers = new HashSet<>();
+        RetryableExceptionClassifier taleClassifier = classifiers[0];
+        importedWrappers.add(taleClassifier);
+        for (int i = 1; i < classifiers.length; ++i) {
+            if (importedWrappers.contains(classifiers[i])) {
+                continue;

Review comment:
       I think we should throw an `InvalidArgumentException` here




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