org.apache.flink.runtime.state.gemini.engine.snapshot.SnapshotCompletableFuture Maven / Gradle / Ivy
/*
* 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.runtime.state.gemini.engine.snapshot;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.util.Preconditions;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
* An implementation of {@link CompletableFuture} for snapshot. This future
* includes multiple stages for the async part of the snapshot , which will
* be executed in the order they added. The number of running tasks will be
* 0 when a stage finishes, and then the next stage will be started if needed.
* The future will complete when all stages are finished or an exception
* happened in any stage. All stages must be added to the future before start
* via {@link #addSnapshotStage(SnapshotStage)}.
*/
public class SnapshotCompletableFuture extends CompletableFuture {
private final AtomicInteger runningTasks;
private final AtomicBoolean endSnapshot;
private SnapshotManager.PendingSnapshot pendingSnapshot;
/**
* Snapshot stages to run in order.
*/
private List snapshotStages;
/**
* Index of next stage in the {@link #snapshotStages}.
*/
private AtomicInteger nextStageIndex;
private ExecutorService snapshotExecutor;
public SnapshotCompletableFuture(ExecutorService snapshotExecutor) {
this.snapshotExecutor = snapshotExecutor;
this.runningTasks = new AtomicInteger(0);
this.endSnapshot = new AtomicBoolean(false);
this.snapshotStages = new ArrayList<>();
this.nextStageIndex = new AtomicInteger(0);
}
public void incRunningTask() {
runningTasks.incrementAndGet();
}
public void decRunningTask() {
int left = runningTasks.decrementAndGet();
Preconditions.checkState(left >= 0, "Number of left running tasks can't be negative.");
if (left == 0) {
// if the future has been cancelled or complete with exception, just return
if (isCancelled() || isCompletedExceptionally()) {
return;
}
// all stages finish with no exception, and just complete the future
if (nextStageIndex.get() >= snapshotStages.size()) {
complete(true);
return;
}
// there are stages to run
incRunningTask();
int index = nextStageIndex.getAndAdd(1);
runStage(snapshotStages.get(index));
// there is no code to execute after this decRunningTask, and it's safe
// to call decRunningTask recursively
decRunningTask();
}
}
public void setPendingSnapshot(SnapshotManager.PendingSnapshot pendingSnapshot) {
this.pendingSnapshot = pendingSnapshot;
}
public SnapshotManager.PendingSnapshot getPendingSnapshot() {
return this.pendingSnapshot;
}
public boolean isEndSnapshot() {
return endSnapshot.get();
}
public void setEndSnapshot() {
endSnapshot.set(true);
}
public void addSnapshotStage(SnapshotStage snapshotStage) {
snapshotStages.add(snapshotStage);
}
public SnapshotStage getSnapshotStage(int index) {
return index >= 0 && index < snapshotStages.size() ? snapshotStages.get(index) : null;
}
@VisibleForTesting
List getSnapshotStageList() {
return snapshotStages;
}
@VisibleForTesting
int getNextStateIndex() {
return nextStageIndex.get();
}
@VisibleForTesting
int getRunningTasks() {
return runningTasks.get();
}
private void runStage(SnapshotStage snapshotStage) {
if (snapshotStage.isAsync()) {
runAsyncStage(snapshotStage);
} else {
runSyncStage(snapshotStage);
}
}
private void runSyncStage(SnapshotStage snapshotStage) {
incRunningTask();
try {
snapshotStage.run();
} catch (Exception e) {
setEndSnapshot();
completeExceptionally(e);
} finally {
decRunningTask();
}
}
private void runAsyncStage(SnapshotStage snapshotStage) {
incRunningTask();
snapshotExecutor.submit(() -> {
try {
snapshotStage.run();
} catch (Exception e) {
setEndSnapshot();
completeExceptionally(e);
} finally {
decRunningTask();
}
});
}
}