pnowojski commented on a change in pull request #15322: URL: https://github.com/apache/flink/pull/15322#discussion_r670483433
########## File path: flink-dstl/flink-dstl-dfs/src/main/java/org/apache/flink/changelog/fs/RetryingExecutor.java ########## @@ -0,0 +1,145 @@ +/* + * 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.changelog.fs; + +import org.apache.flink.util.function.RunnableWithException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Optional; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +/** + * A {@link RunnableWithException} executor that schedules a next attempt upon timeout based on + * {@link RetryPolicy}. Aimed to curb tail latencies + */ +class RetryingExecutor implements AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(RetryingExecutor.class); + + private final ScheduledExecutorService scheduler; + + RetryingExecutor(int nThreads) { + this(SchedulerFactory.create(nThreads, "ChangelogRetryScheduler", LOG)); + } + + RetryingExecutor(ScheduledExecutorService scheduler) { + this.scheduler = scheduler; + } + + void execute(RetryPolicy retryPolicy, RunnableWithException action) { + LOG.debug("execute with retryPolicy: {}", retryPolicy); + RetriableTask task = new RetriableTask(action, retryPolicy, scheduler); + scheduler.submit(task); + } + + @Override + public void close() throws Exception { + LOG.debug("close"); + scheduler.shutdownNow(); + if (!scheduler.awaitTermination(1, TimeUnit.SECONDS)) { + LOG.warn("Unable to cleanly shutdown executorService in 1s"); + } + } + + private static final class RetriableTask implements Runnable { + private final RunnableWithException runnable; + private final ScheduledExecutorService executorService; + private final int current; + private final RetryPolicy retryPolicy; + private final AtomicBoolean actionCompleted; + private final AtomicBoolean attemptCompleted = new AtomicBoolean(false); + + RetriableTask( + RunnableWithException runnable, + RetryPolicy retryPolicy, + ScheduledExecutorService executorService) { + this(1, new AtomicBoolean(false), runnable, retryPolicy, executorService); + } + + private RetriableTask( + int current, + AtomicBoolean actionCompleted, + RunnableWithException runnable, + RetryPolicy retryPolicy, + ScheduledExecutorService executorService) { + this.current = current; + this.runnable = runnable; + this.retryPolicy = retryPolicy; + this.executorService = executorService; + this.actionCompleted = actionCompleted; + } + + @Override + public void run() { + if (!actionCompleted.get()) { + Optional<ScheduledFuture<?>> timeoutFuture = scheduleTimeout(); + try { + runnable.run(); Review comment: Can you introduce a separate interface for that? `IdempotentRunnable` or `IdempotentRunnableWithException`? I believe this is so important requirement that it should be marked better not just via a java doc. Or maybe instead of using generic interface, define `RetriableAction` as a static interface in `RetryingExecutor`? I mean this would help to raise awareness of the idempotence requirement and also significantly improved IDE support (looking for usages, implementations etc). -- 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]
