rkhachatryan commented on a change in pull request #15371: URL: https://github.com/apache/flink/pull/15371#discussion_r603174752
########## File path: flink-dstl/flink-dstl-dfs/src/main/java/org/apache/flink/changelog/fs/BatchingStateChangeStore.java ########## @@ -0,0 +1,173 @@ +/* + * 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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.concurrent.GuardedBy; +import javax.annotation.concurrent.ThreadSafe; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Queue; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.atomic.AtomicReference; + +import static java.lang.Thread.holdsLock; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.flink.util.ExceptionUtils.findThrowable; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * A {@link StateChangeStore} that waits for some configured amount of time before passing the + * accumulated state changes to the actual store. The writes are executed asynchronously. + */ +@ThreadSafe +class BatchingStateChangeStore implements StateChangeStore { + private static final Logger LOG = LoggerFactory.getLogger(BatchingStateChangeStore.class); + + private final StateChangeStore delegate; + private final ScheduledExecutorService scheduler; + private final long scheduleDelayMs; + private final long sizeThresholdBytes; + + @GuardedBy("scheduled") + private final Queue<StoreTask> scheduled; + + @GuardedBy("scheduled") + private long scheduledSizeInBytes; + + @GuardedBy("scheduled") + private Future<?> scheduledFuture = CompletableFuture.completedFuture(null); + + private AtomicReference<Throwable> error = new AtomicReference<>(null); + + BatchingStateChangeStore( + long persistDelayMs, long sizeThresholdBytes, StateChangeStore delegate) { + this( + persistDelayMs, + sizeThresholdBytes, + delegate, + SchedulerFactory.create(1, "ChangelogRetryScheduler", LOG)); + } + + BatchingStateChangeStore( + long persistDelayMs, + long sizeThresholdBytes, + StateChangeStore delegate, + ScheduledExecutorService scheduler) { + this.scheduleDelayMs = persistDelayMs; + this.scheduled = new LinkedList<>(); + this.scheduler = scheduler; + this.sizeThresholdBytes = sizeThresholdBytes; + this.delegate = delegate; + } + + @Override + public void save(StoreTask storeTask) { + Collection<StateChangeSet> changeSets = storeTask.changeSets; + if (error.get() != null) { + LOG.debug("don't persist {} changesets, already failed", changeSets.size()); + storeTask.fail(error.get()); + return; + } + LOG.debug("persist {} changeSets", changeSets.size()); + try { + synchronized (scheduled) { + scheduled.add(storeTask); + scheduledSizeInBytes += storeTask.getSize(); + scheduleUploadIfNeeded(); + } + } catch (Exception e) { + storeTask.fail(e); + throw e; + } + } + + private void scheduleUploadIfNeeded() { + checkState(holdsLock(scheduled)); + if (scheduledFuture.isDone() || isOverSizeThresholdAndCancellationSucceded()) { + scheduleUpload(); + } + } + + private boolean isOverSizeThresholdAndCancellationSucceded() { + return scheduledSizeInBytes >= sizeThresholdBytes && scheduledFuture.cancel(false); + } Review comment: IIUC, if `drainAndSave` is being executing but it already passed the re-scheduling section (2) then nothing will be scheduled. -- 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. For queries about this service, please contact Infrastructure at: [email protected]
