You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/01/25 17:01:31 UTC

[GitHub] [flink] dawidwys commented on a change in pull request #14719: [FLINK-21072] Refactor the SnapshotStrategy hierarchy

dawidwys commented on a change in pull request #14719:
URL: https://github.com/apache/flink/pull/14719#discussion_r563884695



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/state/SnapshotStrategyRunner.java
##########
@@ -0,0 +1,132 @@
+/*
+ * 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;
+
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.util.concurrent.RunnableFuture;
+
+/**
+ * A class to execute a {@link SnapshotStrategy}. It can execute a strategy either synchronously or
+ * asynchronously. It takes care of common logging and resource cleaning.
+ *
+ * @param <T> type of the snapshot result.
+ */
+public final class SnapshotStrategyRunner<T extends StateObject, SR extends SnapshotResources> {
+    /** Flag to tell how the strategy should be executed. */
+    public enum ExecutionType {
+        SYNCHRONOUS,
+        ASYNCHRONOUS
+    }
+
+    private static final Logger LOG = LoggerFactory.getLogger(SnapshotStrategyRunner.class);
+
+    private static final String LOG_SYNC_COMPLETED_TEMPLATE =
+            "{} ({}, synchronous part) in thread {} took {} ms.";
+    private static final String LOG_ASYNC_COMPLETED_TEMPLATE =
+            "{} ({}, asynchronous part) in thread {} took {} ms.";
+
+    /**
+     * Descriptive name of the snapshot strategy that will appear in the log outputs and {@link
+     * #toString()}.
+     */
+    @Nonnull private final String description;
+
+    @Nonnull private final SnapshotStrategy<T, SR> snapshotStrategy;
+    @Nonnull private final CloseableRegistry cancelStreamRegistry;
+
+    @Nonnull private final ExecutionType executionType;
+
+    public SnapshotStrategyRunner(
+            @Nonnull String description,
+            @Nonnull SnapshotStrategy<T, SR> snapshotStrategy,
+            @Nonnull CloseableRegistry cancelStreamRegistry,
+            @Nonnull ExecutionType executionType) {
+        this.description = description;
+        this.snapshotStrategy = snapshotStrategy;
+        this.cancelStreamRegistry = cancelStreamRegistry;
+        this.executionType = executionType;
+    }
+
+    @Nonnull
+    public final RunnableFuture<SnapshotResult<T>> snapshot(
+            long checkpointId,
+            long timestamp,
+            @Nonnull CheckpointStreamFactory streamFactory,
+            @Nonnull CheckpointOptions checkpointOptions)
+            throws Exception {
+        long startTime = System.currentTimeMillis();
+        SR snapshotResources = snapshotStrategy.syncPrepareResources(checkpointId);
+        logCompletedInternal(LOG_SYNC_COMPLETED_TEMPLATE, streamFactory, startTime);
+        SnapshotStrategy.SnapshotResultSupplier<T> asyncSnapshot =
+                snapshotStrategy.asyncSnapshot(
+                        snapshotResources,
+                        checkpointId,
+                        timestamp,
+                        streamFactory,
+                        checkpointOptions);
+
+        switch (executionType) {
+            case SYNCHRONOUS:
+                return DoneFuture.of(asyncSnapshot.get(cancelStreamRegistry));

Review comment:
       Good catch. I think this is actually a bug, as it is using a different one. With this registry the main checkpointing thread(from which the `snapshot` method is executed) might not get cancelled before shutting down the io streams.
   
   I will rewrite the code to always go through the `AsyncSnapshotTask`




----------------------------------------------------------------
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:
users@infra.apache.org