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

2021-01-25 Thread GitBox


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



##
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  type of the snapshot result.
+ */
+public final class SnapshotStrategyRunner {
+/** 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 snapshotStrategy;
+@Nonnull private final CloseableRegistry cancelStreamRegistry;
+
+@Nonnull private final ExecutionType executionType;
+
+public SnapshotStrategyRunner(
+@Nonnull String description,
+@Nonnull SnapshotStrategy snapshotStrategy,
+@Nonnull CloseableRegistry cancelStreamRegistry,
+@Nonnull ExecutionType executionType) {
+this.description = description;
+this.snapshotStrategy = snapshotStrategy;
+this.cancelStreamRegistry = cancelStreamRegistry;
+this.executionType = executionType;
+}
+
+@Nonnull
+public final RunnableFuture> 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 asyncSnapshot =
+snapshotStrategy.asyncSnapshot(
+snapshotResources,
+checkpointId,
+timestamp,
+streamFactory,
+checkpointOptions);
+
+switch (executionType) {
+case SYNCHRONOUS:
+return DoneFuture.of(asyncSnapshot.get(cancelStreamRegistry));

Review comment:
   Is this using the same registry as before? (Say in the 
`HeapSnapshotStrategy`)





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




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

2021-01-25 Thread GitBox


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



##
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  type of the snapshot result.
+ */
+public final class SnapshotStrategyRunner {
+/** 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 snapshotStrategy;
+@Nonnull private final CloseableRegistry cancelStreamRegistry;
+
+@Nonnull private final ExecutionType executionType;
+
+public SnapshotStrategyRunner(
+@Nonnull String description,
+@Nonnull SnapshotStrategy snapshotStrategy,
+@Nonnull CloseableRegistry cancelStreamRegistry,
+@Nonnull ExecutionType executionType) {
+this.description = description;
+this.snapshotStrategy = snapshotStrategy;
+this.cancelStreamRegistry = cancelStreamRegistry;
+this.executionType = executionType;
+}
+
+@Nonnull
+public final RunnableFuture> 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 asyncSnapshot =
+snapshotStrategy.asyncSnapshot(
+snapshotResources,
+checkpointId,
+timestamp,
+streamFactory,
+checkpointOptions);
+
+switch (executionType) {
+case SYNCHRONOUS:
+return DoneFuture.of(asyncSnapshot.get(cancelStreamRegistry));

Review comment:
   Is this using the same registry as before? (Say in the 
`HeapSnapshotStrategy`)





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




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

2021-01-21 Thread GitBox


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



##
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/state/heap/StateTableFactory.java
##
@@ -22,18 +22,14 @@
 import org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo;
 
 /**
- * Interface for synchronicity behavior of heap snapshot strategy.
+ * A factory method for creating a {@link StateTable}.

Review comment:
   ```suggestion
* A factory for {@link StateTable StateTables}.
   ```

##
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/state/DefaultOperatorStateBackendSnapshotStrategy.java
##
@@ -106,119 +99,133 @@ protected DefaultOperatorStateBackendSnapshotStrategy(
 Thread.currentThread().setContextClassLoader(snapshotClassLoader);
 }
 
-AsyncSnapshotCallable> 
snapshotCallable =
-new 
AsyncSnapshotCallable>() {
-
-@Override
-protected SnapshotResult 
callInternal() throws Exception {
-
-CheckpointStreamFactory.CheckpointStateOutputStream 
localOut =
-
streamFactory.createCheckpointStateOutputStream(
-CheckpointedStateScope.EXCLUSIVE);
-snapshotCloseableRegistry.registerCloseable(localOut);
-
-// get the registered operator state infos ...
-List operatorMetaInfoSnapshots =
-new 
ArrayList<>(registeredOperatorStatesDeepCopies.size());
-
-for (Map.Entry> 
entry :
-registeredOperatorStatesDeepCopies.entrySet()) 
{
-operatorMetaInfoSnapshots.add(
-
entry.getValue().getStateMetaInfo().snapshot());
-}
-
-// ... get the registered broadcast operator state 
infos ...
-List broadcastMetaInfoSnapshots 
=
-new 
ArrayList<>(registeredBroadcastStatesDeepCopies.size());
-
-for (Map.Entry> entry :
-
registeredBroadcastStatesDeepCopies.entrySet()) {
-broadcastMetaInfoSnapshots.add(
-
entry.getValue().getStateMetaInfo().snapshot());
-}
-
-// ... write them all in the checkpoint stream ...
-DataOutputView dov = new 
DataOutputViewStreamWrapper(localOut);
-
-OperatorBackendSerializationProxy 
backendSerializationProxy =
-new OperatorBackendSerializationProxy(
-operatorMetaInfoSnapshots, 
broadcastMetaInfoSnapshots);
-
-backendSerializationProxy.write(dov);
-
-// ... and then go for the states ...
-
-// we put BOTH normal and broadcast state metadata here
-int initialMapCapacity =
-registeredOperatorStatesDeepCopies.size()
-+ 
registeredBroadcastStatesDeepCopies.size();
-final Map 
writtenStatesMetaData =
-new HashMap<>(initialMapCapacity);
-
-for (Map.Entry> 
entry :
-registeredOperatorStatesDeepCopies.entrySet()) 
{
-
-PartitionableListState value = entry.getValue();
-long[] partitionOffsets = value.write(localOut);
-OperatorStateHandle.Mode mode =
-
value.getStateMetaInfo().getAssignmentMode();
-writtenStatesMetaData.put(
-entry.getKey(),
-new 
OperatorStateHandle.StateMetaInfo(partitionOffsets, mode));
-}
-
-// ... and the broadcast states themselves ...
-for (Map.Entry> entry :
-
registeredBroadcastStatesDeepCopies.entrySet()) {
-
-BackendWritableBroadcastState value = 
entry.getValue();
-long[] partitionOffsets = {value.write(localOut)};
-OperatorStateHandle.Mode mode =
-
value.getStateMetaInfo().getAssignmentMode();
-writtenStatesMetaData.put(
-entry.getKey(),
-new 
OperatorStateHandle.StateMetaInfo(partitionOffsets, mode));
-}
-
-// ... and, finally, create 

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

2021-01-21 Thread GitBox


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



##
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/state/heap/StateTableFactory.java
##
@@ -22,18 +22,14 @@
 import org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo;
 
 /**
- * Interface for synchronicity behavior of heap snapshot strategy.
+ * A factory method for creating a {@link StateTable}.

Review comment:
   ```suggestion
* A factory for {@link StateTable StateTables}.
   ```

##
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/state/DefaultOperatorStateBackendSnapshotStrategy.java
##
@@ -106,119 +99,133 @@ protected DefaultOperatorStateBackendSnapshotStrategy(
 Thread.currentThread().setContextClassLoader(snapshotClassLoader);
 }
 
-AsyncSnapshotCallable> 
snapshotCallable =
-new 
AsyncSnapshotCallable>() {
-
-@Override
-protected SnapshotResult 
callInternal() throws Exception {
-
-CheckpointStreamFactory.CheckpointStateOutputStream 
localOut =
-
streamFactory.createCheckpointStateOutputStream(
-CheckpointedStateScope.EXCLUSIVE);
-snapshotCloseableRegistry.registerCloseable(localOut);
-
-// get the registered operator state infos ...
-List operatorMetaInfoSnapshots =
-new 
ArrayList<>(registeredOperatorStatesDeepCopies.size());
-
-for (Map.Entry> 
entry :
-registeredOperatorStatesDeepCopies.entrySet()) 
{
-operatorMetaInfoSnapshots.add(
-
entry.getValue().getStateMetaInfo().snapshot());
-}
-
-// ... get the registered broadcast operator state 
infos ...
-List broadcastMetaInfoSnapshots 
=
-new 
ArrayList<>(registeredBroadcastStatesDeepCopies.size());
-
-for (Map.Entry> entry :
-
registeredBroadcastStatesDeepCopies.entrySet()) {
-broadcastMetaInfoSnapshots.add(
-
entry.getValue().getStateMetaInfo().snapshot());
-}
-
-// ... write them all in the checkpoint stream ...
-DataOutputView dov = new 
DataOutputViewStreamWrapper(localOut);
-
-OperatorBackendSerializationProxy 
backendSerializationProxy =
-new OperatorBackendSerializationProxy(
-operatorMetaInfoSnapshots, 
broadcastMetaInfoSnapshots);
-
-backendSerializationProxy.write(dov);
-
-// ... and then go for the states ...
-
-// we put BOTH normal and broadcast state metadata here
-int initialMapCapacity =
-registeredOperatorStatesDeepCopies.size()
-+ 
registeredBroadcastStatesDeepCopies.size();
-final Map 
writtenStatesMetaData =
-new HashMap<>(initialMapCapacity);
-
-for (Map.Entry> 
entry :
-registeredOperatorStatesDeepCopies.entrySet()) 
{
-
-PartitionableListState value = entry.getValue();
-long[] partitionOffsets = value.write(localOut);
-OperatorStateHandle.Mode mode =
-
value.getStateMetaInfo().getAssignmentMode();
-writtenStatesMetaData.put(
-entry.getKey(),
-new 
OperatorStateHandle.StateMetaInfo(partitionOffsets, mode));
-}
-
-// ... and the broadcast states themselves ...
-for (Map.Entry> entry :
-
registeredBroadcastStatesDeepCopies.entrySet()) {
-
-BackendWritableBroadcastState value = 
entry.getValue();
-long[] partitionOffsets = {value.write(localOut)};
-OperatorStateHandle.Mode mode =
-
value.getStateMetaInfo().getAssignmentMode();
-writtenStatesMetaData.put(
-entry.getKey(),
-new 
OperatorStateHandle.StateMetaInfo(partitionOffsets, mode));
-}
-
-// ... and, finally, create