You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2019/07/04 07:10:31 UTC
[flink] branch master updated (f54f774 -> a39ade1)
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git.
from f54f774 [FLINK-11775][table-runtime-blink] Use MemorySegmentWritable to BinaryRowSerializer
new b05d389 [FLINK-12963] [state-processor] Add savepoint writer for bootstrapping new savepoints
new 337c749 [FLINK-12963] [travis] Add state processor api to travis library module
new 03bb097 [FLINK-12963] [docs] Document savepoint writer for bootstrapping new savepoints
new 31e827e [FLINK-12963] [state-processor] Refactor operator state access concerns into ModifiableSavepointMetadata
new 9f4e69f [FLINK-12963] [state-processor] Introduce OperatorStateSpec wrapper class
new 6f820ea [FLINK-12963] [state-processor] Reduce dependency on SavepointMetadata in runtime components
new f16fed8 [FLINK-12963] [state-processor] Simplify SavepointMetadata hierarchy
new cb535d9 [FLINK-12963] [state-processor] Add missing serialVersionUIDs
new c9815a7 [FLINK-12693] [state-processor] Improve Javadocs for user-facing APIs
new 699c1f8 [FLINK-12963] [state-processor] Allow providing user defined SavepointWriterOperatorFactory when creating keyed BootstrapTransformations
new a39ade1 [FLINK-12963] [state-processor] Make OperatorTransformation non-instantiable
The 11 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
docs/dev/libs/state_processor_api.md | 151 ++++++-
..._processor_api.md => state_processor_api.zh.md} | 151 ++++++-
.../flink/state/api/BootstrapTransformation.java | 189 +++++++++
.../apache/flink/state/api/ExistingSavepoint.java | 29 +-
.../state/api/KeyedOperatorTransformation.java | 91 +++++
.../api/{Savepoint.java => NewSavepoint.java} | 28 +-
.../state/api/OneInputOperatorTransformation.java | 178 +++++++++
.../flink/state/api/OperatorTransformation.java | 70 ++++
.../java/org/apache/flink/state/api/Savepoint.java | 46 ++-
...ta.java => SavepointWriterOperatorFactory.java} | 39 +-
.../apache/flink/state/api/WritableSavepoint.java | 127 ++++++
.../functions/BroadcastStateBootstrapFunction.java | 66 +++
.../api/functions/KeyedStateBootstrapFunction.java | 71 ++++
.../api/functions/StateBootstrapFunction.java | 57 +++
.../output/BoundedOneInputStreamTaskRunner.java | 79 ++++
.../flink/state/api/output/BoundedStreamTask.java | 124 ++++++
.../state/api/output/MergeOperatorStates.java | 61 +++
.../api/output/OperatorSubtaskStateReducer.java | 75 ++++
.../state/api/output/SavepointOutputFormat.java | 100 +++++
.../flink/state/api/output/SnapshotUtils.java | 70 ++++
.../api/output/TaggedOperatorSubtaskState.java | 69 ++++
.../operators/BroadcastStateBootstrapOperator.java | 105 +++++
.../operators/KeyedStateBootstrapOperator.java | 114 ++++++
.../output/operators/StateBootstrapOperator.java | 93 +++++
.../partitioner/HashSelector.java} | 49 ++-
.../partitioner/KeyGroupRangePartitioner.java} | 41 +-
.../api/runtime/BootstrapTransformationWithID.java | 49 +++
...pointMetadata.java => BoundedStreamConfig.java} | 52 +--
.../runtime/metadata/OnDiskSavepointMetadata.java | 96 -----
.../api/runtime/metadata/OperatorStateSpec.java | 83 ++++
.../api/runtime/metadata/SavepointMetadata.java | 98 ++++-
.../state/api/BootstrapTransformationTest.java | 167 ++++++++
.../org/apache/flink/state/api/SavepointTest.java | 118 ++++++
.../flink/state/api/SavepointWriterITCase.java | 442 +++++++++++++++++++++
.../api/output/SavepointOutputFormatTest.java | 99 +++++
.../flink/state/api/output/SnapshotUtilsTest.java | 155 ++++++++
tools/travis/stage.sh | 1 +
37 files changed, 3374 insertions(+), 259 deletions(-)
copy docs/dev/libs/{state_processor_api.md => state_processor_api.zh.md} (63%)
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
copy flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/{Savepoint.java => NewSavepoint.java} (51%)
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
copy flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/{runtime/metadata/SavepointMetadata.java => SavepointWriterOperatorFactory.java} (50%)
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/BroadcastStateBootstrapFunction.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/KeyedStateBootstrapFunction.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/StateBootstrapFunction.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedStreamTask.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SavepointOutputFormat.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SnapshotUtils.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/TaggedOperatorSubtaskState.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/BroadcastStateBootstrapOperator.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/KeyedStateBootstrapOperator.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/StateBootstrapOperator.java
copy flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/{runtime/metadata/SavepointMetadata.java => output/partitioner/HashSelector.java} (53%)
copy flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/{runtime/metadata/SavepointMetadata.java => output/partitioner/KeyGroupRangePartitioner.java} (53%)
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BootstrapTransformationWithID.java
copy flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/{metadata/SavepointMetadata.java => BoundedStreamConfig.java} (50%)
delete mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OnDiskSavepointMetadata.java
create mode 100644 flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OperatorStateSpec.java
create mode 100644 flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java
create mode 100644 flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java
create mode 100644 flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointWriterITCase.java
create mode 100644 flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SavepointOutputFormatTest.java
create mode 100644 flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SnapshotUtilsTest.java
[flink] 02/11: [FLINK-12963] [travis] Add state processor api to
travis library module
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 337c7497740eb6d2670dfa5b0a69b4b59c152fc1
Author: Seth Wiesman <sj...@gmail.com>
AuthorDate: Mon Jun 24 14:16:06 2019 -0500
[FLINK-12963] [travis] Add state processor api to travis library module
---
tools/travis/stage.sh | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/travis/stage.sh b/tools/travis/stage.sh
index f202bf8..7bd7a97 100644
--- a/tools/travis/stage.sh
+++ b/tools/travis/stage.sh
@@ -47,6 +47,7 @@ flink-metrics/flink-metrics-core"
MODULES_LIBRARIES="\
flink-libraries/flink-cep,\
flink-libraries/flink-cep-scala,\
+flink-libraries/flink-state-processing-api,\
flink-table/flink-table-common,\
flink-table/flink-table-api-java,\
flink-table/flink-table-api-scala,\
[flink] 05/11: [FLINK-12963] [state-processor] Introduce
OperatorStateSpec wrapper class
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 9f4e69fe25c9563a4e429e3aabc077c6a0962872
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Wed Jul 3 16:26:33 2019 +0800
[FLINK-12963] [state-processor] Introduce OperatorStateSpec wrapper class
This class is a simple container class to represent an operator state
that is either still defined by a BootstrapTransformation, i.e. new
state that has not been written out yet, or an existing OperatorState.
Introducing this class improves readability of the code, instead of
using Eithers and Tuples that would not have clear semantics for the
user.
---
.../apache/flink/state/api/WritableSavepoint.java | 13 ++--
.../api/runtime/BootstrapTransformationWithID.java | 49 +++++++++++++
.../metadata/ModifiableSavepointMetadata.java | 38 +++++-----
.../api/runtime/metadata/OperatorStateSpec.java | 83 ++++++++++++++++++++++
4 files changed, 156 insertions(+), 27 deletions(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
index 0f68cce..512edeb 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
@@ -19,13 +19,12 @@ package org.apache.flink.state.api;
import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.java.DataSet;
-import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.checkpoint.OperatorState;
-import org.apache.flink.runtime.jobgraph.OperatorID;
import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.state.api.output.MergeOperatorStates;
import org.apache.flink.state.api.output.SavepointOutputFormat;
+import org.apache.flink.state.api.runtime.BootstrapTransformationWithID;
import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
import org.apache.flink.util.Preconditions;
@@ -80,7 +79,7 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
public final void write(String path) {
final Path savepointPath = new Path(path);
- List<Tuple2<OperatorID, BootstrapTransformation<?>>> newOperatorTransformations = metadata.getNewOperatorTransformations();
+ List<BootstrapTransformationWithID<?>> newOperatorTransformations = metadata.getNewOperators();
DataSet<OperatorState> newOperatorStates = writeOperatorStates(newOperatorTransformations, savepointPath);
List<OperatorState> existingOperators = metadata.getExistingOperators();
@@ -109,11 +108,13 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
}
private DataSet<OperatorState> writeOperatorStates(
- List<Tuple2<OperatorID, BootstrapTransformation<?>>> newOperatorTransformations,
+ List<BootstrapTransformationWithID<?>> newOperatorStates,
Path savepointWritePath) {
- return newOperatorTransformations
+ return newOperatorStates
.stream()
- .map(transformation -> transformation.f1.writeOperatorState(transformation.f0, stateBackend, metadata, savepointWritePath))
+ .map(newOperatorState -> newOperatorState
+ .getBootstrapTransformation()
+ .writeOperatorState(newOperatorState.getOperatorID(), stateBackend, metadata, savepointWritePath))
.reduce(DataSet::union)
.orElseThrow(() -> new IllegalStateException("Savepoint's must contain at least one operator"));
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BootstrapTransformationWithID.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BootstrapTransformationWithID.java
new file mode 100644
index 0000000..cb5b5af
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BootstrapTransformationWithID.java
@@ -0,0 +1,49 @@
+/*
+ * 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.state.api.runtime;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.state.api.BootstrapTransformation;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * A simple container class that represents a newly bootstrapped operator state within savepoints.
+ * It wraps the target {@link OperatorID} for the bootstrapped operator, as well as the {@link BootstrapTransformation}
+ * that defines how the state is bootstrapped.
+ */
+@Internal
+public class BootstrapTransformationWithID<T> {
+
+ private final OperatorID operatorID;
+ private final BootstrapTransformation<T> bootstrapTransformation;
+
+ public BootstrapTransformationWithID(OperatorID operatorID, BootstrapTransformation<T> bootstrapTransformation) {
+ this.operatorID = Preconditions.checkNotNull(operatorID);
+ this.bootstrapTransformation = Preconditions.checkNotNull(bootstrapTransformation);
+ }
+
+ public OperatorID getOperatorID() {
+ return operatorID;
+ }
+
+ public BootstrapTransformation<T> getBootstrapTransformation() {
+ return bootstrapTransformation;
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
index 8df8723..3484a59 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
@@ -1,13 +1,12 @@
package org.apache.flink.state.api.runtime.metadata;
import org.apache.flink.annotation.Internal;
-import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.checkpoint.MasterState;
import org.apache.flink.runtime.checkpoint.OperatorState;
import org.apache.flink.runtime.jobgraph.OperatorID;
import org.apache.flink.state.api.BootstrapTransformation;
+import org.apache.flink.state.api.runtime.BootstrapTransformationWithID;
import org.apache.flink.state.api.runtime.OperatorIDGenerator;
-import org.apache.flink.types.Either;
import java.io.IOException;
import java.util.Collection;
@@ -15,7 +14,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
-import java.util.stream.Stream;
/**
* Savepoint metadata that can be modified.
@@ -23,16 +21,15 @@ import java.util.stream.Stream;
@Internal
public class ModifiableSavepointMetadata extends SavepointMetadata {
- private transient Map<OperatorID, Either<OperatorState, BootstrapTransformation<?>>> operatorStateIndex;
+ private transient Map<OperatorID, OperatorStateSpec> operatorStateIndex;
public ModifiableSavepointMetadata(int maxParallelism, Collection<MasterState> masterStates, Collection<OperatorState> initialStates) {
super(maxParallelism, masterStates);
this.operatorStateIndex = new HashMap<>(initialStates.size());
-
- for (OperatorState operatorState : initialStates) {
- operatorStateIndex.put(operatorState.getOperatorID(), Either.Left(operatorState));
- }
+ initialStates.forEach(existingState -> operatorStateIndex.put(
+ existingState.getOperatorID(),
+ OperatorStateSpec.existing(existingState)));
}
/**
@@ -43,12 +40,12 @@ public class ModifiableSavepointMetadata extends SavepointMetadata {
public OperatorState getOperatorState(String uid) throws IOException {
OperatorID operatorID = OperatorIDGenerator.fromUid(uid);
- Either<OperatorState, BootstrapTransformation<?>> operatorState = operatorStateIndex.get(operatorID);
- if (operatorState == null || operatorState.isRight()) {
+ OperatorStateSpec operatorState = operatorStateIndex.get(operatorID);
+ if (operatorState == null || operatorState.isNewStateTransformation()) {
throw new IOException("Savepoint does not contain state with operator uid " + uid);
}
- return operatorState.left();
+ return operatorState.asExistingState();
}
public void removeOperator(String uid) {
@@ -62,7 +59,7 @@ public class ModifiableSavepointMetadata extends SavepointMetadata {
throw new IllegalArgumentException("The savepoint already contains uid " + uid + ". All uid's must be unique");
}
- operatorStateIndex.put(id, Either.Right(transformation));
+ operatorStateIndex.put(id, OperatorStateSpec.newWithTransformation(new BootstrapTransformationWithID<>(id, transformation)));
}
/**
@@ -72,21 +69,20 @@ public class ModifiableSavepointMetadata extends SavepointMetadata {
return operatorStateIndex
.values()
.stream()
- .filter(Either::isLeft)
- .map(Either::left)
+ .filter(OperatorStateSpec::isExistingState)
+ .map(OperatorStateSpec::asExistingState)
.collect(Collectors.toList());
}
/**
* @return List of new operator states for the savepoint, represented by their target {@link OperatorID} and {@link BootstrapTransformation}.
*/
- public List<Tuple2<OperatorID, BootstrapTransformation<?>>> getNewOperatorTransformations() {
- Stream<Tuple2<OperatorID, BootstrapTransformation<?>>> transformations = operatorStateIndex
- .entrySet()
+ public List<BootstrapTransformationWithID<?>> getNewOperators() {
+ return operatorStateIndex
+ .values()
.stream()
- .filter(entry -> entry.getValue().isRight())
- .map(entry -> Tuple2.of(entry.getKey(), entry.getValue().right()));
-
- return transformations.collect(Collectors.toList());
+ .filter(OperatorStateSpec::isNewStateTransformation)
+ .map(OperatorStateSpec::asNewStateTransformation)
+ .collect(Collectors.toList());
}
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OperatorStateSpec.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OperatorStateSpec.java
new file mode 100644
index 0000000..cb87e4c
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OperatorStateSpec.java
@@ -0,0 +1,83 @@
+/*
+ * 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.state.api.runtime.metadata;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.state.api.runtime.BootstrapTransformationWithID;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+/**
+ * This class specifies an operator state maintained by {@link SavepointMetadata}.
+ * An operator state is either represented as an existing {@link OperatorState}, or a
+ * {@link org.apache.flink.state.api.BootstrapTransformation} that will be used to create it.
+ */
+@Internal
+class OperatorStateSpec {
+
+ private final OperatorID id;
+
+ @Nullable
+ private final OperatorState existingState;
+
+ @Nullable
+ private final BootstrapTransformationWithID<?> newOperatorStateTransformation;
+
+ static OperatorStateSpec existing(OperatorState existingState) {
+ return new OperatorStateSpec(Preconditions.checkNotNull(existingState));
+ }
+
+ static OperatorStateSpec newWithTransformation(BootstrapTransformationWithID<?> transformation) {
+ return new OperatorStateSpec(Preconditions.checkNotNull(transformation));
+ }
+
+ private OperatorStateSpec(OperatorState existingState) {
+ this.id = existingState.getOperatorID();
+ this.existingState = existingState;
+ this.newOperatorStateTransformation = null;
+ }
+
+ private OperatorStateSpec(BootstrapTransformationWithID<?> transformation) {
+ this.id = transformation.getOperatorID();
+ this.newOperatorStateTransformation = transformation;
+ this.existingState = null;
+ }
+
+ boolean isExistingState() {
+ return existingState != null;
+ }
+
+ boolean isNewStateTransformation() {
+ return !isExistingState();
+ }
+
+ OperatorState asExistingState() {
+ Preconditions.checkState(isExistingState(), "OperatorState %s is not an existing state.", id);
+ return existingState;
+ }
+
+ @SuppressWarnings("unchecked")
+ <T> BootstrapTransformationWithID<T> asNewStateTransformation() {
+ Preconditions.checkState(isNewStateTransformation(), "OperatorState %s is not a new state defined with BootstrapTransformation", id);
+ return (BootstrapTransformationWithID<T>) newOperatorStateTransformation;
+ }
+}
[flink] 06/11: [FLINK-12963] [state-processor] Reduce dependency on
SavepointMetadata in runtime components
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 6f820eacba4f1340e0fd2fba3b092f37f4e9c626
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Thu Jul 4 03:05:59 2019 +0800
[FLINK-12963] [state-processor] Reduce dependency on SavepointMetadata in runtime components
Previously, we were passing around SavepointMetadata across different
runtime classes, such as MergeOperatorStates operator and
BoostrapTransformation.write().
This isn't necessary, as these runtime classes actually do not need
functionality that the SavepointMetadata provides. The SavepointMetadata
should essentially just be a data structure that maintains what existing
operator states there are, and which operator states are added, in order
to build the job graph.
This commit changes this by only passing in the necessary information
wrapped by SavepointMetadata to the runtime classes. By doing so, we can
avoid making SavepointMetadata a Serializable, since it is now no longer
part of the built job graph.
---
.../org/apache/flink/state/api/BootstrapTransformation.java | 11 +++++------
.../java/org/apache/flink/state/api/WritableSavepoint.java | 4 ++--
.../apache/flink/state/api/output/MergeOperatorStates.java | 13 +++++++------
.../api/runtime/metadata/ModifiableSavepointMetadata.java | 2 +-
.../flink/state/api/runtime/metadata/SavepointMetadata.java | 5 +----
.../apache/flink/state/api/BootstrapTransformationTest.java | 11 ++++-------
6 files changed, 20 insertions(+), 26 deletions(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
index edba718..c5ee559 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
@@ -37,7 +37,6 @@ import org.apache.flink.state.api.output.operators.BroadcastStateBootstrapOperat
import org.apache.flink.state.api.output.partitioner.HashSelector;
import org.apache.flink.state.api.output.partitioner.KeyGroupRangePartitioner;
import org.apache.flink.state.api.runtime.BoundedStreamConfig;
-import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
import org.apache.flink.streaming.api.graph.StreamConfig;
import org.apache.flink.streaming.api.operators.StreamOperator;
@@ -92,23 +91,23 @@ public class BootstrapTransformation<T> {
/**
* @return The max parallelism for this operator.
*/
- int getMaxParallelism(SavepointMetadata metadata) {
- return operatorMaxParallelism.orElse(metadata.maxParallelism());
+ int getMaxParallelism(int globalMaxParallelism) {
+ return operatorMaxParallelism.orElse(globalMaxParallelism);
}
/**
* @param operatorID The operator id for the stream operator.
* @param stateBackend The state backend for the job.
- * @param metadata Metadata about the resulting savepoint.
+ * @param globalMaxParallelism Global max parallelism set for the savepoint.
* @param savepointPath The path where the savepoint will be written.
* @return The operator subtask states for this bootstrap transformation.
*/
DataSet<OperatorState> writeOperatorState(
OperatorID operatorID,
StateBackend stateBackend,
- SavepointMetadata metadata,
+ int globalMaxParallelism,
Path savepointPath) {
- int localMaxParallelism = getMaxParallelism(metadata);
+ int localMaxParallelism = getMaxParallelism(globalMaxParallelism);
return writeOperatorSubtaskStates(operatorID, stateBackend, savepointPath, localMaxParallelism)
.reduceGroup(new OperatorSubtaskStateReducer(operatorID, localMaxParallelism))
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
index 512edeb..b4a29e4 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
@@ -87,7 +87,7 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
DataSet<OperatorState> finalOperatorStates = unionOperatorStates(newOperatorStates, existingOperators);
finalOperatorStates
- .reduceGroup(new MergeOperatorStates(metadata))
+ .reduceGroup(new MergeOperatorStates(metadata.getMasterStates()))
.name("reduce(OperatorState)")
.output(new SavepointOutputFormat(savepointPath))
.name(path);
@@ -114,7 +114,7 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
.stream()
.map(newOperatorState -> newOperatorState
.getBootstrapTransformation()
- .writeOperatorState(newOperatorState.getOperatorID(), stateBackend, metadata, savepointWritePath))
+ .writeOperatorState(newOperatorState.getOperatorID(), stateBackend, metadata.maxParallelism(), savepointWritePath))
.reduce(DataSet::union)
.orElseThrow(() -> new IllegalStateException("Savepoint's must contain at least one operator"));
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
index f0a7793..81b49b2 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
@@ -20,13 +20,14 @@ package org.apache.flink.state.api.output;
import org.apache.flink.annotation.Internal;
import org.apache.flink.api.common.functions.GroupReduceFunction;
+import org.apache.flink.runtime.checkpoint.MasterState;
import org.apache.flink.runtime.checkpoint.OperatorState;
import org.apache.flink.runtime.checkpoint.savepoint.Savepoint;
import org.apache.flink.runtime.checkpoint.savepoint.SavepointV2;
-import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
import org.apache.flink.util.Collector;
import org.apache.flink.util.Preconditions;
+import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
@@ -35,12 +36,12 @@ import java.util.stream.StreamSupport;
*/
@Internal
public class MergeOperatorStates implements GroupReduceFunction<OperatorState, Savepoint> {
- private final SavepointMetadata metadata;
+ private final Collection<MasterState> masterStates;
- public MergeOperatorStates(SavepointMetadata metadata) {
- Preconditions.checkNotNull(metadata, "Savepoint metadata must not be null");
+ public MergeOperatorStates(Collection<MasterState> masterStates) {
+ Preconditions.checkNotNull(masterStates, "Master state metadata must not be null");
- this.metadata = metadata;
+ this.masterStates = masterStates;
}
@Override
@@ -49,7 +50,7 @@ public class MergeOperatorStates implements GroupReduceFunction<OperatorState, S
new SavepointV2(
SnapshotUtils.CHECKPOINT_ID,
StreamSupport.stream(values.spliterator(), false).collect(Collectors.toList()),
- metadata.getMasterStates());
+ masterStates);
out.collect(savepoint);
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
index 3484a59..eada085 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
@@ -21,7 +21,7 @@ import java.util.stream.Collectors;
@Internal
public class ModifiableSavepointMetadata extends SavepointMetadata {
- private transient Map<OperatorID, OperatorStateSpec> operatorStateIndex;
+ private final Map<OperatorID, OperatorStateSpec> operatorStateIndex;
public ModifiableSavepointMetadata(int maxParallelism, Collection<MasterState> masterStates, Collection<OperatorState> initialStates) {
super(maxParallelism, masterStates);
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
index 40ce66a..91efa04 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
@@ -21,16 +21,13 @@ package org.apache.flink.state.api.runtime.metadata;
import org.apache.flink.annotation.Internal;
import org.apache.flink.runtime.checkpoint.MasterState;
-import java.io.Serializable;
import java.util.Collection;
/**
* Returns metadata about a savepoint.
*/
@Internal
-public class SavepointMetadata implements Serializable {
-
- private static final long serialVersionUID = 1L;
+public class SavepointMetadata {
private final int maxParallelism;
diff --git a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java
index 707ccde..64994c4 100644
--- a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java
+++ b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java
@@ -31,14 +31,11 @@ import org.apache.flink.state.api.functions.BroadcastStateBootstrapFunction;
import org.apache.flink.state.api.functions.StateBootstrapFunction;
import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
import org.apache.flink.state.api.runtime.OperatorIDGenerator;
-import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
import org.apache.flink.test.util.AbstractTestBase;
import org.junit.Assert;
import org.junit.Test;
-import java.util.Collections;
-
/**
* Tests for bootstrap transformations.
*/
@@ -55,7 +52,7 @@ public class BootstrapTransformationTest extends AbstractTestBase {
.bootstrapWith(input)
.transform(new ExampleBroadcastStateBootstrapFunction());
- int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(4, Collections.emptyList()));
+ int maxParallelism = transformation.getMaxParallelism(4);
DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
OperatorIDGenerator.fromUid("uid"),
new MemoryStateBackend(),
@@ -79,7 +76,7 @@ public class BootstrapTransformationTest extends AbstractTestBase {
.bootstrapWith(input)
.transform(new ExampleStateBootstrapFunction());
- int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(10, Collections.emptyList()));
+ int maxParallelism = transformation.getMaxParallelism(10);
DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
OperatorIDGenerator.fromUid("uid"),
new MemoryStateBackend(),
@@ -104,7 +101,7 @@ public class BootstrapTransformationTest extends AbstractTestBase {
.bootstrapWith(input)
.transform(new ExampleStateBootstrapFunction());
- int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(4, Collections.emptyList()));
+ int maxParallelism = transformation.getMaxParallelism(4);
DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
OperatorIDGenerator.fromUid("uid"),
new MemoryStateBackend(),
@@ -130,7 +127,7 @@ public class BootstrapTransformationTest extends AbstractTestBase {
.setMaxParallelism(1)
.transform(new ExampleStateBootstrapFunction());
- int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(4, Collections.emptyList()));
+ int maxParallelism = transformation.getMaxParallelism(4);
DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
OperatorIDGenerator.fromUid("uid"),
new MemoryStateBackend(),
[flink] 08/11: [FLINK-12963] [state-processor] Add missing
serialVersionUIDs
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit cb535d9b6ea24b8419bb5dffb7a090da8e709589
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Thu Jul 4 03:19:20 2019 +0800
[FLINK-12963] [state-processor] Add missing serialVersionUIDs
---
.../apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java | 3 +++
.../java/org/apache/flink/state/api/output/MergeOperatorStates.java | 3 +++
.../org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java | 2 ++
.../java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java | 3 +++
4 files changed, 11 insertions(+)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java
index f6d5093..c975e6d 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java
@@ -38,6 +38,9 @@ import org.apache.flink.util.Collector;
*/
@Internal
public class BoundedOneInputStreamTaskRunner<IN> extends RichMapPartitionFunction<IN, TaggedOperatorSubtaskState> {
+
+ private static final long serialVersionUID = 1L;
+
private final StreamConfig streamConfig;
private final int maxParallelism;
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
index 81b49b2..d5789a3 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
@@ -36,6 +36,9 @@ import java.util.stream.StreamSupport;
*/
@Internal
public class MergeOperatorStates implements GroupReduceFunction<OperatorState, Savepoint> {
+
+ private static final long serialVersionUID = 1L;
+
private final Collection<MasterState> masterStates;
public MergeOperatorStates(Collection<MasterState> masterStates) {
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java
index bfd06b3..1ac57f5 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java
@@ -39,6 +39,8 @@ import java.util.stream.StreamSupport;
public class OperatorSubtaskStateReducer
extends RichGroupReduceFunction<TaggedOperatorSubtaskState, OperatorState> {
+ private static final long serialVersionUID = 1L;
+
private final OperatorID operatorID;
private final int maxParallelism;
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java
index 6306447..5f3b38b 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java
@@ -30,6 +30,9 @@ import org.apache.flink.streaming.api.graph.StreamConfig;
*/
@Internal
public class BoundedStreamConfig extends StreamConfig {
+
+ private static final long serialVersionUID = 1L;
+
public BoundedStreamConfig() {
super(new Configuration());
[flink] 01/11: [FLINK-12963] [state-processor] Add savepoint writer
for bootstrapping new savepoints
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit b05d389f063b67edd253836fb71bb50f9f4ade06
Author: Seth Wiesman <sj...@gmail.com>
AuthorDate: Mon Jun 24 10:47:34 2019 -0500
[FLINK-12963] [state-processor] Add savepoint writer for bootstrapping new savepoints
---
.../flink/state/api/BootstrapTransformation.java | 177 +++++++++
.../apache/flink/state/api/ExistingSavepoint.java | 14 +-
.../state/api/KeyedOperatorTransformation.java | 86 ++++
.../api/{Savepoint.java => NewSavepoint.java} | 25 +-
.../state/api/OneInputOperatorTransformation.java | 176 ++++++++
...ntMetadata.java => OperatorTransformation.java} | 39 +-
.../java/org/apache/flink/state/api/Savepoint.java | 40 +-
...ta.java => SavepointWriterOperatorFactory.java} | 36 +-
.../apache/flink/state/api/WritableSavepoint.java | 129 ++++++
.../functions/BroadcastStateBootstrapFunction.java | 66 +++
.../api/functions/KeyedStateBootstrapFunction.java | 71 ++++
.../api/functions/StateBootstrapFunction.java | 57 +++
.../output/BoundedOneInputStreamTaskRunner.java | 76 ++++
.../flink/state/api/output/BoundedStreamTask.java | 124 ++++++
.../state/api/output/MergeOperatorStates.java | 57 +++
.../api/output/OperatorSubtaskStateReducer.java | 73 ++++
.../state/api/output/SavepointOutputFormat.java | 100 +++++
.../flink/state/api/output/SnapshotUtils.java | 70 ++++
.../api/output/TaggedOperatorSubtaskState.java | 69 ++++
.../operators/BroadcastStateBootstrapOperator.java | 105 +++++
.../operators/KeyedStateBootstrapOperator.java | 114 ++++++
.../output/operators/StateBootstrapOperator.java | 93 +++++
.../partitioner/HashSelector.java} | 49 ++-
.../partitioner/KeyGroupRangePartitioner.java} | 41 +-
...pointMetadata.java => BoundedStreamConfig.java} | 43 +-
.../metadata/ModifiableSavepointMetadata.java | 67 ++++
.../runtime/metadata/OnDiskSavepointMetadata.java | 96 -----
.../api/runtime/metadata/SavepointMetadata.java | 30 +-
.../state/api/BootstrapTransformationTest.java | 170 ++++++++
.../org/apache/flink/state/api/SavepointTest.java | 118 ++++++
.../flink/state/api/SavepointWriterITCase.java | 442 +++++++++++++++++++++
.../api/output/SavepointOutputFormatTest.java | 99 +++++
.../flink/state/api/output/SnapshotUtilsTest.java | 155 ++++++++
33 files changed, 2845 insertions(+), 262 deletions(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
new file mode 100644
index 0000000..edba718
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
@@ -0,0 +1,177 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.api.java.operators.MapPartitionOperator;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.state.StateBackend;
+import org.apache.flink.state.api.output.BoundedOneInputStreamTaskRunner;
+import org.apache.flink.state.api.output.OperatorSubtaskStateReducer;
+import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
+import org.apache.flink.state.api.output.operators.BroadcastStateBootstrapOperator;
+import org.apache.flink.state.api.output.partitioner.HashSelector;
+import org.apache.flink.state.api.output.partitioner.KeyGroupRangePartitioner;
+import org.apache.flink.state.api.runtime.BoundedStreamConfig;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
+import org.apache.flink.streaming.api.graph.StreamConfig;
+import org.apache.flink.streaming.api.operators.StreamOperator;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.util.OptionalInt;
+
+/**
+ * Bootstrapped data that can be written into a {@code Savepoint}.
+ * @param <T> The input type of the transformation.
+ */
+@PublicEvolving
+@SuppressWarnings("WeakerAccess")
+public class BootstrapTransformation<T> {
+ private final DataSet<T> dataSet;
+
+ private final SavepointWriterOperatorFactory factory;
+
+ @Nullable
+ private final HashSelector<T> keySelector;
+
+ @Nullable
+ private final TypeInformation<?> keyType;
+
+ private final OptionalInt operatorMaxParallelism;
+
+ BootstrapTransformation(
+ DataSet<T> dataSet,
+ OptionalInt operatorMaxParallelism,
+ SavepointWriterOperatorFactory factory) {
+ this.dataSet = dataSet;
+ this.operatorMaxParallelism = operatorMaxParallelism;
+ this.factory = factory;
+ this.keySelector = null;
+ this.keyType = null;
+ }
+
+ <K> BootstrapTransformation(
+ DataSet<T> dataSet,
+ OptionalInt operatorMaxParallelism,
+ SavepointWriterOperatorFactory factory,
+ @Nonnull KeySelector<T, K> keySelector,
+ @Nonnull TypeInformation<K> keyType) {
+ this.dataSet = dataSet;
+ this.operatorMaxParallelism = operatorMaxParallelism;
+ this.factory = factory;
+ this.keySelector = new HashSelector<>(keySelector);
+ this.keyType = keyType;
+ }
+
+ /**
+ * @return The max parallelism for this operator.
+ */
+ int getMaxParallelism(SavepointMetadata metadata) {
+ return operatorMaxParallelism.orElse(metadata.maxParallelism());
+ }
+
+ /**
+ * @param operatorID The operator id for the stream operator.
+ * @param stateBackend The state backend for the job.
+ * @param metadata Metadata about the resulting savepoint.
+ * @param savepointPath The path where the savepoint will be written.
+ * @return The operator subtask states for this bootstrap transformation.
+ */
+ DataSet<OperatorState> writeOperatorState(
+ OperatorID operatorID,
+ StateBackend stateBackend,
+ SavepointMetadata metadata,
+ Path savepointPath) {
+ int localMaxParallelism = getMaxParallelism(metadata);
+
+ return writeOperatorSubtaskStates(operatorID, stateBackend, savepointPath, localMaxParallelism)
+ .reduceGroup(new OperatorSubtaskStateReducer(operatorID, localMaxParallelism))
+ .name("reduce(OperatorSubtaskState)");
+ }
+
+ @VisibleForTesting
+ MapPartitionOperator<T, TaggedOperatorSubtaskState> writeOperatorSubtaskStates(
+ OperatorID operatorID,
+ StateBackend stateBackend,
+ Path savepointPath,
+ int localMaxParallelism) {
+
+ DataSet<T> input = dataSet;
+ if (keySelector != null) {
+ input = dataSet.partitionCustom(new KeyGroupRangePartitioner(localMaxParallelism), keySelector);
+ }
+
+ final StreamConfig config;
+ if (keyType == null) {
+ config = new BoundedStreamConfig();
+ } else {
+ TypeSerializer<?> keySerializer = keyType.createSerializer(dataSet.getExecutionEnvironment().getConfig());
+ config = new BoundedStreamConfig(keySerializer, keySelector);
+ }
+
+ StreamOperator<TaggedOperatorSubtaskState> operator = factory.getOperator(
+ System.currentTimeMillis(),
+ savepointPath);
+
+ operator = dataSet.clean(operator);
+ config.setStreamOperator(operator);
+
+ config.setOperatorName(operatorID.toHexString());
+ config.setOperatorID(operatorID);
+ config.setStateBackend(stateBackend);
+
+ BoundedOneInputStreamTaskRunner<T> operatorRunner = new BoundedOneInputStreamTaskRunner<>(
+ config,
+ localMaxParallelism
+ );
+
+ MapPartitionOperator<T, TaggedOperatorSubtaskState> subtaskStates = input
+ .mapPartition(operatorRunner)
+ .name(operatorID.toHexString());
+
+ if (operator instanceof BroadcastStateBootstrapOperator) {
+ subtaskStates = subtaskStates.setParallelism(1);
+ } else {
+ int currentParallelism = getParallelism(subtaskStates);
+ if (currentParallelism > localMaxParallelism) {
+ subtaskStates.setParallelism(localMaxParallelism);
+ }
+ }
+ return subtaskStates;
+ }
+
+ private static <T> int getParallelism(MapPartitionOperator<T, TaggedOperatorSubtaskState> subtaskStates) {
+ int parallelism = subtaskStates.getParallelism();
+ if (parallelism == ExecutionConfig.PARALLELISM_DEFAULT) {
+ parallelism = subtaskStates.getExecutionEnvironment().getParallelism();
+ }
+
+ return parallelism;
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
index 9bfd997..a169662 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
@@ -37,8 +37,7 @@ import org.apache.flink.state.api.input.BroadcastStateInputFormat;
import org.apache.flink.state.api.input.KeyedStateInputFormat;
import org.apache.flink.state.api.input.ListStateInputFormat;
import org.apache.flink.state.api.input.UnionStateInputFormat;
-import org.apache.flink.state.api.runtime.metadata.OnDiskSavepointMetadata;
-import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
+import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
import org.apache.flink.util.Preconditions;
import java.io.IOException;
@@ -48,20 +47,21 @@ import java.io.IOException;
*/
@PublicEvolving
@SuppressWarnings("WeakerAccess")
-public class ExistingSavepoint {
+public class ExistingSavepoint extends WritableSavepoint<ExistingSavepoint> {
private final ExecutionEnvironment env;
- private final SavepointMetadata metadata;
+ private final ModifiableSavepointMetadata metadata;
private final StateBackend stateBackend;
- ExistingSavepoint(ExecutionEnvironment env, String path, StateBackend stateBackend) throws IOException {
+ ExistingSavepoint(ExecutionEnvironment env, ModifiableSavepointMetadata metadata, StateBackend stateBackend) throws IOException {
+ super(metadata, stateBackend);
Preconditions.checkNotNull(env, "The execution environment must not be null");
- Preconditions.checkNotNull(path, "The savepoint path must not be null");
+ Preconditions.checkNotNull(metadata, "The savepoint metadata must not be null");
Preconditions.checkNotNull(stateBackend, "The state backend must not be null");
this.env = env;
- this.metadata = new OnDiskSavepointMetadata(path);
+ this.metadata = metadata;
this.stateBackend = stateBackend;
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
new file mode 100644
index 0000000..49825cf
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
@@ -0,0 +1,86 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.state.api.functions.KeyedStateBootstrapFunction;
+import org.apache.flink.state.api.output.operators.KeyedStateBootstrapOperator;
+
+import java.util.OptionalInt;
+
+/**
+ * A {@link KeyedOperatorTransformation} represents a {@link OneInputOperatorTransformation} on which operator state is
+ * partitioned by key using a provided {@link KeySelector}.
+ *
+ * @param <K> The type of the key in the Keyed OperatorTransformation.
+ * @param <T> The type of the elements in the Keyed OperatorTransformation.
+ */
+@PublicEvolving
+@SuppressWarnings("WeakerAccess")
+public class KeyedOperatorTransformation<K, T> {
+ private final DataSet<T> dataSet;
+
+ private final OptionalInt operatorMaxParallelism;
+
+ private final KeySelector<T, K> keySelector;
+
+ private final TypeInformation<K> keyType;
+
+ KeyedOperatorTransformation(
+ DataSet<T> dataSet,
+ OptionalInt operatorMaxParallelism,
+ KeySelector<T, K> keySelector,
+ TypeInformation<K> keyType) {
+ this.dataSet = dataSet;
+ this.operatorMaxParallelism = operatorMaxParallelism;
+ this.keySelector = keySelector;
+ this.keyType = keyType;
+ }
+
+ /**
+ * Applies the given {@link KeyedStateBootstrapFunction} on the keyed input.
+ *
+ * <p>The function will be called for every element in the input and can be used for writing both
+ * keyed and operator state into a {@link Savepoint}.
+ *
+ * @param processFunction The {@link KeyedStateBootstrapFunction} that is called for each element.
+ * @return An {@link OperatorTransformation} that can be added to a {@link Savepoint}.
+ */
+ public BootstrapTransformation<T> transform(KeyedStateBootstrapFunction<K, T> processFunction) {
+ SavepointWriterOperatorFactory factory = (timestamp, path) -> new KeyedStateBootstrapOperator<>(timestamp, path, processFunction);
+ return transform(factory);
+ }
+
+ /**
+ * Method for passing user defined operators along with the type information that will transform
+ * the OperatorTransformation.
+ *
+ * <p><b>IMPORTANT:</b> Any output from this operator will be discarded.
+ *
+ * @param factory A factory returning transformation logic type of the return stream
+ * @return An {@link BootstrapTransformation} that can be added to a {@link Savepoint}.
+ */
+ private BootstrapTransformation<T> transform(SavepointWriterOperatorFactory factory) {
+ return new BootstrapTransformation<>(dataSet, operatorMaxParallelism, factory, keySelector, keyType);
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
similarity index 51%
copy from flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
copy to flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
index beea113..f42e886 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
@@ -19,30 +19,15 @@
package org.apache.flink.state.api;
import org.apache.flink.annotation.PublicEvolving;
-import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.runtime.state.StateBackend;
-
-import java.io.IOException;
+import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
/**
- * A {@link Savepoint} is a collection of operator states that can be used to supply initial state
- * when starting a {@link org.apache.flink.streaming.api.datastream.DataStream} job.
+ * A new savepoint.
*/
@PublicEvolving
-public final class Savepoint {
-
- private Savepoint() {}
-
- /**
- * Loads an existing savepoint. Useful if you want to query, modify, or extend
- * the state of an existing application.
- *
- * @param env The execution enviornment used to transform the savepoint.
- * @param path The path to an existing savepoint on disk.
- * @param stateBackend The state backend of the savepoint used for keyed state.
- * @return An existing savepoint that can be queried.
- */
- public static ExistingSavepoint load(ExecutionEnvironment env, String path, StateBackend stateBackend) throws IOException {
- return new ExistingSavepoint(env, path, stateBackend);
+public class NewSavepoint extends WritableSavepoint<NewSavepoint> {
+ NewSavepoint(ModifiableSavepointMetadata metadata, StateBackend stateBackend) {
+ super(metadata, stateBackend);
}
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java
new file mode 100644
index 0000000..687eced
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java
@@ -0,0 +1,176 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.operators.Keys;
+import org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo;
+import org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.api.java.tuple.Tuple;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.state.api.functions.BroadcastStateBootstrapFunction;
+import org.apache.flink.state.api.functions.StateBootstrapFunction;
+import org.apache.flink.state.api.output.operators.BroadcastStateBootstrapOperator;
+import org.apache.flink.state.api.output.operators.StateBootstrapOperator;
+import org.apache.flink.streaming.util.keys.KeySelectorUtil;
+
+import java.util.OptionalInt;
+
+/**
+ * {@code OneInputOperatorTransformation} represents a user defined transformation applied on
+ * an {@link OperatorTransformation} with one input.
+ *
+ * @param <T> The type of the elements in this operator.
+ */
+@PublicEvolving
+@SuppressWarnings("WeakerAccess")
+public class OneInputOperatorTransformation<T> {
+ private final DataSet<T> dataSet;
+
+ private OptionalInt operatorMaxParallelism = OptionalInt.empty();
+
+ OneInputOperatorTransformation(DataSet<T> dataSet) {
+ this.dataSet = dataSet;
+ }
+
+
+ /**
+ * Sets the maximum parallelism of this operator.
+ *
+ * <p>The maximum parallelism specifies the upper bound for dynamic scaling. It also defines the
+ * number of key groups used for partitioned state.
+ *
+ * @param maxParallelism Maximum parallelism
+ * @return The operator with set maximum parallelism
+ */
+ @PublicEvolving
+ public OneInputOperatorTransformation<T> setMaxParallelism(int maxParallelism) {
+ this.operatorMaxParallelism = OptionalInt.of(maxParallelism);
+ return this;
+ }
+
+ /**
+ * Applies the given {@link StateBootstrapFunction} on the non-keyed input.
+ *
+ * <p>The function will be called for every element in the input and can be used for writing
+ * operator state into a {@link Savepoint}.
+ *
+ * @param processFunction The {@link StateBootstrapFunction} that is called for each element.
+ * @return An {@link OperatorTransformation} that can be added to a {@link Savepoint}.
+ */
+ public BootstrapTransformation<T> transform(StateBootstrapFunction<T> processFunction) {
+ SavepointWriterOperatorFactory factory = (timestamp, path) -> new StateBootstrapOperator<>(timestamp, path, processFunction);
+
+ return transform(factory);
+ }
+
+ /**
+ * Applies the given {@link BroadcastStateBootstrapFunction} on the non-keyed input.
+ *
+ * <p>The function will be called for every element in the input and can be used for writing
+ * broadcast state into a {@link Savepoint}.
+ *
+ * @param processFunction The {@link BroadcastStateBootstrapFunction} that is called for each element.
+ * @return An {@link BootstrapTransformation} that can be added to a {@link Savepoint}.
+ */
+ public BootstrapTransformation<T> transform(BroadcastStateBootstrapFunction<T> processFunction) {
+ SavepointWriterOperatorFactory factory = (timestamp, path) -> new BroadcastStateBootstrapOperator<>(timestamp, path, processFunction);
+
+ return transform(factory);
+ }
+
+ /**
+ * Method for passing user defined operators along with the type information that will transform
+ * the OperatorTransformation.
+ *
+ * <p><b>IMPORTANT:</b> Any output from this operator will be discarded.
+ *
+ * @param factory A factory returning transformation logic type of the return stream
+ * @return An {@link BootstrapTransformation} that can be added to a {@link Savepoint}.
+ */
+ public BootstrapTransformation<T> transform(SavepointWriterOperatorFactory factory) {
+ return new BootstrapTransformation<>(dataSet, operatorMaxParallelism, factory);
+ }
+
+ /**
+ * It creates a new {@link KeyedOperatorTransformation} that uses the provided key for partitioning its operator
+ * states.
+ *
+ * @param keySelector The KeySelector to be used for extracting the key for partitioning.
+ * @return The {@code BootstrapTransformation} with partitioned state.
+ */
+ public <K> KeyedOperatorTransformation<K, T> keyBy(KeySelector<T, K> keySelector) {
+ TypeInformation<K> keyType = TypeExtractor.getKeySelectorTypes(keySelector, dataSet.getType());
+ return new KeyedOperatorTransformation<>(dataSet, operatorMaxParallelism, keySelector, keyType);
+ }
+
+ /**
+ * It creates a new {@link KeyedOperatorTransformation} that uses the provided key with explicit type
+ * information for partitioning its operator states.
+ *
+ * @param keySelector The KeySelector to be used for extracting the key for partitioning.
+ * @param keyType The type information describing the key type.
+ * @return The {@code BootstrapTransformation} with partitioned state.
+ */
+ public <K> KeyedOperatorTransformation<K, T> keyBy(KeySelector<T, K> keySelector, TypeInformation<K> keyType) {
+ return new KeyedOperatorTransformation<>(dataSet, operatorMaxParallelism, keySelector, keyType);
+ }
+
+ /**
+ * Partitions the operator state of a {@link OperatorTransformation} by the given key positions.
+ *
+ * @param fields The position of the fields on which the {@code OperatorTransformation} will be grouped.
+ * @return The {@code OperatorTransformation} with partitioned state.
+ */
+ public KeyedOperatorTransformation<Tuple, T> keyBy(int... fields) {
+ if (dataSet.getType() instanceof BasicArrayTypeInfo || dataSet.getType() instanceof PrimitiveArrayTypeInfo) {
+ return keyBy(KeySelectorUtil.getSelectorForArray(fields, dataSet.getType()));
+ } else {
+ return keyBy(new Keys.ExpressionKeys<>(fields, dataSet.getType()));
+ }
+ }
+
+ /**
+ * Partitions the operator state of a {@link OperatorTransformation} using field expressions. A field expression
+ * is either the name of a public field or a getter method with parentheses of the {@code
+ * OperatorTransformation}'s underlying type. A dot can be used to drill down into objects, as in {@code
+ * "field1.getInnerField2()" }.
+ *
+ * @param fields One or more field expressions on which the state of the {@link OperatorTransformation}
+ * operators will be partitioned.
+ * @return The {@code OperatorTransformation} with partitioned state (i.e. KeyedStream)
+ */
+ public KeyedOperatorTransformation<Tuple, T> keyBy(String... fields) {
+ return keyBy(new Keys.ExpressionKeys<>(fields, dataSet.getType()));
+ }
+
+ private KeyedOperatorTransformation<Tuple, T> keyBy(Keys<T> keys) {
+ KeySelector<T, Tuple> keySelector = KeySelectorUtil.getSelectorForKeys(
+ keys,
+ dataSet.getType(),
+ dataSet.getExecutionEnvironment().getConfig());
+
+ TypeInformation<Tuple> keyType = TypeExtractor.getKeySelectorTypes(keySelector, dataSet.getType());
+ return new KeyedOperatorTransformation<>(dataSet, operatorMaxParallelism, keySelector, keyType);
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
similarity index 53%
copy from flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
copy to flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
index 3a2b7cb..7bba046 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
@@ -16,36 +16,27 @@
* limitations under the License.
*/
-package org.apache.flink.state.api.runtime.metadata;
+package org.apache.flink.state.api;
-import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Collection;
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.java.DataSet;
/**
- * Returns metadata about a savepoint.
+ * An OperatorTransformation represents a single operator within a {@link Savepoint}.
*/
-@Internal
-public interface SavepointMetadata extends Serializable {
-
- /**
- * @return The max parallelism for the savepoint.
- */
- int maxParallelism();
+@PublicEvolving
+@SuppressWarnings("WeakerAccess")
+public abstract class OperatorTransformation {
/**
- * @return Masters states for the savepoint.
- */
- Collection<MasterState> getMasterStates();
-
- /**
- * @return Operator state for the given UID.
+ * Create a new {@link OperatorTransformation} from a {@link DataSet}.
*
- * @throws IOException If the savepoint does not contain operator state with the given uid.
+ * @param dataSet A dataset of elements.
+ * @param <T> The type of the input.
+ * @return A {@link OneInputOperatorTransformation}.
*/
- OperatorState getOperatorState(String uid) throws IOException;
+ public static <T> OneInputOperatorTransformation<T> bootstrapWith(DataSet<T> dataSet) {
+ return new OneInputOperatorTransformation<>(dataSet);
+ }
}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
index beea113..9754c5b 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
@@ -20,9 +20,17 @@ package org.apache.flink.state.api;
import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.java.ExecutionEnvironment;
+import org.apache.flink.runtime.checkpoint.OperatorState;
import org.apache.flink.runtime.state.StateBackend;
+import org.apache.flink.state.api.runtime.SavepointLoader;
+import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+import org.apache.flink.util.Preconditions;
import java.io.IOException;
+import java.util.Collections;
+import java.util.Comparator;
+
+import static org.apache.flink.runtime.state.KeyGroupRangeAssignment.UPPER_BOUND_MAX_PARALLELISM;
/**
* A {@link Savepoint} is a collection of operator states that can be used to supply initial state
@@ -39,10 +47,36 @@ public final class Savepoint {
*
* @param env The execution enviornment used to transform the savepoint.
* @param path The path to an existing savepoint on disk.
- * @param stateBackend The state backend of the savepoint used for keyed state.
- * @return An existing savepoint that can be queried.
+ * @param stateBackend The state backend of the savepoint.
*/
public static ExistingSavepoint load(ExecutionEnvironment env, String path, StateBackend stateBackend) throws IOException {
- return new ExistingSavepoint(env, path, stateBackend);
+ org.apache.flink.runtime.checkpoint.savepoint.Savepoint savepoint = SavepointLoader.loadSavepoint(path);
+
+ int maxParallelism = savepoint
+ .getOperatorStates()
+ .stream()
+ .map(OperatorState::getMaxParallelism)
+ .max(Comparator.naturalOrder())
+ .orElseThrow(() -> new RuntimeException("Savepoint's must contain at least one operator"));
+
+ ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(maxParallelism, savepoint.getMasterStates(), savepoint.getOperatorStates());
+ return new ExistingSavepoint(env, metadata, stateBackend);
+ }
+
+ /**
+ * Creates a new savepoint.
+ *
+ * @param stateBackend The state backend of the savepoint used for keyed state.
+ * @param maxParallelism The max parallelism of the savepoint.
+ * @return A new savepoint.
+ */
+ public static NewSavepoint create(StateBackend stateBackend, int maxParallelism) {
+ Preconditions.checkArgument(maxParallelism > 0
+ && maxParallelism <= UPPER_BOUND_MAX_PARALLELISM,
+ "Maximum parallelism must be between 1 and " + UPPER_BOUND_MAX_PARALLELISM
+ + ". Found: " + maxParallelism);
+
+ ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(maxParallelism, Collections.emptyList(), Collections.emptyList());
+ return new NewSavepoint(metadata, stateBackend);
}
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/SavepointWriterOperatorFactory.java
similarity index 50%
copy from flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
copy to flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/SavepointWriterOperatorFactory.java
index 3a2b7cb..a2b3a60 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/SavepointWriterOperatorFactory.java
@@ -16,36 +16,16 @@
* limitations under the License.
*/
-package org.apache.flink.state.api.runtime.metadata;
+package org.apache.flink.state.api;
-import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Collection;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
+import org.apache.flink.streaming.api.operators.StreamOperator;
/**
- * Returns metadata about a savepoint.
+ * Creates a savepoint writing operator from a savepoint path.
*/
-@Internal
-public interface SavepointMetadata extends Serializable {
-
- /**
- * @return The max parallelism for the savepoint.
- */
- int maxParallelism();
-
- /**
- * @return Masters states for the savepoint.
- */
- Collection<MasterState> getMasterStates();
-
- /**
- * @return Operator state for the given UID.
- *
- * @throws IOException If the savepoint does not contain operator state with the given uid.
- */
- OperatorState getOperatorState(String uid) throws IOException;
+@FunctionalInterface
+public interface SavepointWriterOperatorFactory {
+ StreamOperator<TaggedOperatorSubtaskState> getOperator(long timestamp, Path savepointPath);
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
new file mode 100644
index 0000000..300d1ae
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
@@ -0,0 +1,129 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.state.StateBackend;
+import org.apache.flink.state.api.output.MergeOperatorStates;
+import org.apache.flink.state.api.output.SavepointOutputFormat;
+import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+import org.apache.flink.util.Preconditions;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Any savepoint that can be written to from a batch context.
+ * @param <F> The implementation type.
+ */
+@PublicEvolving
+@SuppressWarnings("WeakerAccess")
+public abstract class WritableSavepoint<F extends WritableSavepoint> {
+
+ protected final ModifiableSavepointMetadata metadata;
+
+ protected final StateBackend stateBackend;
+
+ WritableSavepoint(ModifiableSavepointMetadata metadata, StateBackend stateBackend) {
+ Preconditions.checkNotNull(metadata, "The savepoint metadata must not be null");
+ Preconditions.checkNotNull(stateBackend, "The state backend must not be null");
+ this.metadata = metadata;
+ this.stateBackend = stateBackend;
+ }
+
+ /**
+ * Drop an existing operator from the savepoint.
+ * @param uid The uid of the operator.
+ * @return A modified savepoint.
+ */
+ @SuppressWarnings("unchecked")
+ public F removeOperator(String uid) {
+ metadata.removeOperator(uid);
+ return (F) this;
+ }
+
+ /**
+ * Adds a new operator to the savepoint.
+ * @param uid The uid of the operator.
+ * @param transformation The operator to be included.
+ * @return The modified savepoint.
+ */
+ @SuppressWarnings("unchecked")
+ public <T> F withOperator(String uid, BootstrapTransformation<T> transformation) {
+ metadata.addOperator(uid, transformation);
+ return (F) this;
+ }
+
+ /**
+ * Write out a new or updated savepoint.
+ * @param path The path to where the savepoint should be written.
+ */
+ public final void write(String path) {
+ final Path savepointPath = new Path(path);
+
+ DataSet<OperatorState> newOperatorStates = getOperatorStates(savepointPath);
+
+ List<OperatorState> existingOperators = getExistingOperatorStates();
+
+ DataSet<OperatorState> finalOperatorStates = unionOperatorStates(newOperatorStates, existingOperators);
+
+ finalOperatorStates
+ .reduceGroup(new MergeOperatorStates(metadata))
+ .name("reduce(OperatorState)")
+ .output(new SavepointOutputFormat(savepointPath))
+ .name(path);
+ }
+
+ private List<OperatorState> getExistingOperatorStates() {
+ return metadata
+ .getOperatorStates()
+ .entrySet()
+ .stream()
+ .filter(entry -> entry.getValue().isLeft())
+ .map(entry -> entry.getValue().left())
+ .collect(Collectors.toList());
+ }
+
+ private DataSet<OperatorState> getOperatorStates(Path savepointPath) {
+ return metadata
+ .getOperatorStates()
+ .entrySet()
+ .stream()
+ .filter(entry -> entry.getValue().isRight())
+ .map(entry -> entry.getValue().right().writeOperatorState(entry.getKey(), stateBackend, metadata, savepointPath))
+ .reduce(DataSet::union)
+ .orElseThrow(() -> new IllegalStateException("Savepoint's must contain at least one operator"));
+ }
+
+ private DataSet<OperatorState> unionOperatorStates(DataSet<OperatorState> newOperatorStates, List<OperatorState> existingOperators) {
+ DataSet<OperatorState> finalOperatorStates;
+ if (existingOperators.isEmpty()) {
+ finalOperatorStates = newOperatorStates;
+ } else {
+ DataSet<OperatorState> wrappedCollection = newOperatorStates
+ .getExecutionEnvironment()
+ .fromCollection(existingOperators);
+
+ finalOperatorStates = newOperatorStates.union(wrappedCollection);
+ }
+ return finalOperatorStates;
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/BroadcastStateBootstrapFunction.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/BroadcastStateBootstrapFunction.java
new file mode 100644
index 0000000..5ac0d81
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/BroadcastStateBootstrapFunction.java
@@ -0,0 +1,66 @@
+/*
+ * 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.state.api.functions;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.functions.AbstractRichFunction;
+import org.apache.flink.api.common.state.BroadcastState;
+import org.apache.flink.api.common.state.MapStateDescriptor;
+
+/**
+ * Interface for writing elements to broadcast state.
+ *
+ * @param <IN> The type of the input.
+ */
+@PublicEvolving
+public abstract class BroadcastStateBootstrapFunction<IN> extends AbstractRichFunction {
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Writes the given value to operator state. This function is called for every record.
+ *
+ * @param value The input record.
+ * @throws Exception This method may throw exceptions. Throwing an exception will cause the
+ * operation to fail and may trigger recovery.
+ */
+ public abstract void processElement(IN value, Context ctx) throws Exception;
+
+ /**
+ * Context that {@link StateBootstrapFunction}'s can use for getting additional data about an input
+ * record.
+ *
+ * <p>The context is only valid for the duration of a {@link
+ * #processElement(Object, Context)} call. Do not store the context and use
+ * afterwards!
+ */
+ public interface Context {
+
+ /** Returns the current processing time. */
+ long currentProcessingTime();
+
+ /**
+ * Fetches the {@link BroadcastState} with the specified name.
+ *
+ * @param descriptor the {@link MapStateDescriptor} of the state to be fetched.
+ * @return The required {@link BroadcastState broadcast state}.
+ */
+ <K, V> BroadcastState<K, V> getBroadcastState(MapStateDescriptor<K, V> descriptor);
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/KeyedStateBootstrapFunction.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/KeyedStateBootstrapFunction.java
new file mode 100644
index 0000000..7703a58
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/KeyedStateBootstrapFunction.java
@@ -0,0 +1,71 @@
+/*
+ * 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.state.api.functions;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.functions.AbstractRichFunction;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.streaming.api.TimerService;
+
+/**
+ * A function that writes keyed state to a new operator.
+ *
+ * <p>For every element {@link #processElement(Object, Context)} is invoked. This can write data to
+ * state and set timers.
+ *
+ * <p><b>NOTE:</b> A {@code KeyedStateBootstrapFunction} is always a {@link
+ * org.apache.flink.api.common.functions.RichFunction}. Therefore, access to the {@link
+ * org.apache.flink.api.common.functions.RuntimeContext} is always available and setup and teardown
+ * methods can be implemented. See {@link
+ * org.apache.flink.api.common.functions.RichFunction#open(Configuration)})} and {@link
+ * org.apache.flink.api.common.functions.RichFunction#close()}.
+ *
+ * @param <K> Type of the keys.
+ * @param <IN> Type of the input.
+ */
+@PublicEvolving
+public abstract class KeyedStateBootstrapFunction<K, IN> extends AbstractRichFunction {
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Process one element from the input stream.
+ *
+ * <p>This function can update internal state or set timers using the {@link Context} parameter.
+ *
+ * @param value The input value.
+ * @param ctx A {@link Context} that allows querying the timestamp of the element and getting a
+ * {@link TimerService} for registering timers and querying the time. The context is only
+ * valid during the invocation of this method, do not store it.
+ * @throws Exception This method may throw exceptions. Throwing an exception will cause the
+ * operation to fail and may trigger recovery.
+ */
+ public abstract void processElement(IN value, Context ctx) throws Exception;
+
+ /** Information available in an invocation of {@link #processElement(Object, Context)}. */
+ public abstract class Context {
+
+ /** A {@link TimerService} for querying time and registering timers. */
+ public abstract TimerService timerService();
+
+ /** Get key of the element being processed. */
+ public abstract K getCurrentKey();
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/StateBootstrapFunction.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/StateBootstrapFunction.java
new file mode 100644
index 0000000..a91de22
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/functions/StateBootstrapFunction.java
@@ -0,0 +1,57 @@
+/*
+ * 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.state.api.functions;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.functions.AbstractRichFunction;
+import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
+
+/**
+ * Interface for writing elements to operator state.
+ *
+ * @param <IN> The type of the input.
+ */
+@PublicEvolving
+public abstract class StateBootstrapFunction<IN> extends AbstractRichFunction implements CheckpointedFunction {
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Writes the given value to operator state. This function is called for every record.
+ *
+ * @param value The input record.
+ * @throws Exception This method may throw exceptions. Throwing an exception will cause the
+ * operation to fail and may trigger recovery.
+ */
+ public abstract void processElement(IN value, Context ctx) throws Exception;
+
+ /**
+ * Context that {@link StateBootstrapFunction}'s can use for getting additional data about an input
+ * record.
+ *
+ * <p>The context is only valid for the duration of a {@link
+ * StateBootstrapFunction#processElement(Object, Context)} call. Do not store the context and use
+ * afterwards!
+ */
+ public interface Context {
+
+ /** Returns the current processing time. */
+ long currentProcessingTime();
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java
new file mode 100644
index 0000000..f6d5093
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedOneInputStreamTaskRunner.java
@@ -0,0 +1,76 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.functions.RichMapPartitionFunction;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.state.api.runtime.SavepointEnvironment;
+import org.apache.flink.streaming.api.graph.StreamConfig;
+import org.apache.flink.util.Collector;
+
+/**
+ * A {@link RichMapPartitionFunction} that serves as the runtime for a {@link
+ * BoundedStreamTask}.
+ *
+ * <p>The task is executed processing the data in a particular partition instead of the pulling from
+ * the network stack. After all data has been processed the runner will output the {@link
+ * OperatorSubtaskState} from the snapshot of the bounded task.
+ *
+ * @param <IN> Type of the input to the partition
+ */
+@Internal
+public class BoundedOneInputStreamTaskRunner<IN> extends RichMapPartitionFunction<IN, TaggedOperatorSubtaskState> {
+ private final StreamConfig streamConfig;
+
+ private final int maxParallelism;
+
+ private transient SavepointEnvironment env;
+
+ /**
+ * Create a new {@link BoundedOneInputStreamTaskRunner}.
+ *
+ * @param streamConfig The internal configuration for the task.
+ * @param maxParallelism The max parallelism of the operator.
+ */
+ public BoundedOneInputStreamTaskRunner(
+ StreamConfig streamConfig,
+ int maxParallelism) {
+
+ this.streamConfig = streamConfig;
+ this.maxParallelism = maxParallelism;
+ }
+
+ @Override
+ public void open(Configuration parameters) throws Exception {
+ super.open(parameters);
+
+ env = new SavepointEnvironment
+ .Builder(getRuntimeContext(), maxParallelism)
+ .setConfiguration(streamConfig.getConfiguration())
+ .build();
+ }
+
+ @Override
+ public void mapPartition(Iterable<IN> values, Collector<TaggedOperatorSubtaskState> out) throws Exception {
+ new BoundedStreamTask<>(env, values, out).invoke();
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedStreamTask.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedStreamTask.java
new file mode 100644
index 0000000..db663da
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BoundedStreamTask.java
@@ -0,0 +1,124 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.runtime.execution.Environment;
+import org.apache.flink.state.api.runtime.NeverFireProcessingTimeService;
+import org.apache.flink.streaming.api.operators.BoundedOneInput;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.api.operators.Output;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.watermark.Watermark;
+import org.apache.flink.streaming.runtime.streamrecord.LatencyMarker;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.runtime.tasks.StreamTask;
+import org.apache.flink.util.Collector;
+import org.apache.flink.util.OutputTag;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Iterator;
+
+/**
+ * A stream task that pulls elements from an {@link Iterable} instead of the network. After all
+ * elements are processed the task takes a snapshot of the subtask operator state. This is a shim
+ * until stream tasks support bounded inputs.
+ *
+ * @param <IN> Type of the input.
+ * @param <OUT> Type of the output.
+ * @param <OP> Type of the operator this task runs.
+ */
+class BoundedStreamTask<IN, OUT, OP extends OneInputStreamOperator<IN, OUT> & BoundedOneInput>
+ extends StreamTask<OUT, OP> {
+
+ private final Iterator<IN> input;
+
+ private final Collector<OUT> collector;
+
+ private final StreamRecord<IN> reuse;
+
+ BoundedStreamTask(
+ Environment environment,
+ Iterable<IN> input,
+ Collector<OUT> collector) {
+ super(environment, new NeverFireProcessingTimeService());
+ this.input = input.iterator();
+ this.collector = collector;
+ this.reuse = new StreamRecord<>(null);
+ }
+
+ @Override
+ protected void init() throws Exception {
+ Preconditions.checkState(
+ operatorChain.getAllOperators().length == 1,
+ "BoundedStreamTask's should only run a single operator");
+
+ // re-initialize the operator with the correct collector.
+ StreamOperatorFactory<OUT> operatorFactory = configuration.getStreamOperatorFactory(getUserCodeClassLoader());
+ headOperator = operatorFactory.createStreamOperator(this, configuration, new CollectorWrapper<>(collector));
+ headOperator.initializeState();
+ headOperator.open();
+ }
+
+ @Override
+ protected void performDefaultAction(ActionContext context) throws Exception {
+ if (input.hasNext()) {
+ reuse.replace(input.next());
+ headOperator.setKeyContextElement1(reuse);
+ headOperator.processElement(reuse);
+ } else {
+ headOperator.endInput();
+ context.allActionsCompleted();
+ }
+ }
+
+ @Override
+ protected void cancelTask() {}
+
+ @Override
+ protected void cleanup() throws Exception {
+ headOperator.close();
+ headOperator.dispose();
+ }
+
+ private static class CollectorWrapper<OUT> implements Output<StreamRecord<OUT>> {
+
+ private final Collector<OUT> inner;
+
+ private CollectorWrapper(Collector<OUT> inner) {
+ this.inner = inner;
+ }
+
+ @Override
+ public void emitWatermark(Watermark mark) { }
+
+ @Override
+ public <X> void collect(OutputTag<X> outputTag, StreamRecord<X> record) { }
+
+ @Override
+ public void emitLatencyMarker(LatencyMarker latencyMarker) { }
+
+ @Override
+ public void collect(StreamRecord<OUT> record) {
+ inner.collect(record.getValue());
+ }
+
+ @Override
+ public void close() { }
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
new file mode 100644
index 0000000..f0a7793
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/MergeOperatorStates.java
@@ -0,0 +1,57 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.functions.GroupReduceFunction;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.checkpoint.savepoint.Savepoint;
+import org.apache.flink.runtime.checkpoint.savepoint.SavepointV2;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
+import org.apache.flink.util.Collector;
+import org.apache.flink.util.Preconditions;
+
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+
+/**
+ * A reducer that aggregates multiple {@link OperatorState}'s into a single {@link Savepoint}.
+ */
+@Internal
+public class MergeOperatorStates implements GroupReduceFunction<OperatorState, Savepoint> {
+ private final SavepointMetadata metadata;
+
+ public MergeOperatorStates(SavepointMetadata metadata) {
+ Preconditions.checkNotNull(metadata, "Savepoint metadata must not be null");
+
+ this.metadata = metadata;
+ }
+
+ @Override
+ public void reduce(Iterable<OperatorState> values, Collector<Savepoint> out) {
+ Savepoint savepoint =
+ new SavepointV2(
+ SnapshotUtils.CHECKPOINT_ID,
+ StreamSupport.stream(values.spliterator(), false).collect(Collectors.toList()),
+ metadata.getMasterStates());
+
+ out.collect(savepoint);
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java
new file mode 100644
index 0000000..bfd06b3
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/OperatorSubtaskStateReducer.java
@@ -0,0 +1,73 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.functions.RichGroupReduceFunction;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.util.Collector;
+import org.apache.flink.util.Preconditions;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+
+/**
+ * A reducer that aggregates all {@link OperatorSubtaskState}'s for a particular operator into a
+ * single {@link OperatorState}.
+ */
+@Internal
+public class OperatorSubtaskStateReducer
+ extends RichGroupReduceFunction<TaggedOperatorSubtaskState, OperatorState> {
+
+ private final OperatorID operatorID;
+
+ private final int maxParallelism;
+
+ public OperatorSubtaskStateReducer(OperatorID operatorID, int maxParallelism) {
+ Preconditions.checkNotNull(operatorID, "Operator id must not be null.");
+ Preconditions.checkState(maxParallelism > 1);
+
+ this.operatorID = operatorID;
+ this.maxParallelism = maxParallelism;
+ }
+
+ @Override
+ public void open(Configuration parameters) throws Exception {
+ super.open(parameters);
+ }
+
+ @Override
+ public void reduce(Iterable<TaggedOperatorSubtaskState> values, Collector<OperatorState> out) {
+ List<TaggedOperatorSubtaskState> subtasks = StreamSupport
+ .stream(values.spliterator(), false)
+ .collect(Collectors.toList());
+
+ OperatorState operatorState = new OperatorState(operatorID, subtasks.size(), maxParallelism);
+
+ for (TaggedOperatorSubtaskState value : subtasks) {
+ operatorState.putState(value.index, value.state);
+ }
+
+ out.collect(operatorState);
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SavepointOutputFormat.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SavepointOutputFormat.java
new file mode 100644
index 0000000..190de52
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SavepointOutputFormat.java
@@ -0,0 +1,100 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.io.RichOutputFormat;
+import org.apache.flink.configuration.CheckpointingOptions;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.runtime.checkpoint.Checkpoints;
+import org.apache.flink.runtime.checkpoint.savepoint.Savepoint;
+import org.apache.flink.runtime.state.CheckpointMetadataOutputStream;
+import org.apache.flink.runtime.state.CheckpointStorageLocation;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.CompletedCheckpointStorageLocation;
+import org.apache.flink.runtime.state.filesystem.AbstractFsCheckpointStorage;
+import org.apache.flink.runtime.state.filesystem.FsCheckpointStorageLocation;
+import org.apache.flink.util.LambdaUtil;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+/**
+ * An output format to serialize {@link Savepoint} metadata to distributed storage.
+ *
+ * <p>This format may only be executed with parallelism 1.
+ */
+@Internal
+public class SavepointOutputFormat extends RichOutputFormat<Savepoint> {
+
+ private static final long serialVersionUID = 1L;
+
+ private static final Logger LOG = LoggerFactory.getLogger(SavepointOutputFormat.class);
+
+ private final Path savepointPath;
+
+ private transient CheckpointStorageLocation targetLocation;
+
+ public SavepointOutputFormat(Path savepointPath) {
+ this.savepointPath = savepointPath;
+ }
+
+ @Override
+ public void configure(Configuration parameters) {}
+
+ @Override
+ public void open(int taskNumber, int numTasks) throws IOException {
+ Preconditions.checkState(numTasks == 1, "SavepointOutputFormat should only be executed with parallelism 1");
+
+ targetLocation = createSavepointLocation(savepointPath);
+ }
+
+ @Override
+ public void writeRecord(Savepoint savepoint) throws IOException {
+ String path = LambdaUtil.withContextClassLoader(getRuntimeContext().getUserCodeClassLoader(), () -> {
+ try (CheckpointMetadataOutputStream out = targetLocation.createMetadataOutputStream()) {
+ Checkpoints.storeCheckpointMetadata(savepoint, out);
+ CompletedCheckpointStorageLocation finalizedLocation = out.closeAndFinalizeCheckpoint();
+ return finalizedLocation.getExternalPointer();
+ }
+ });
+
+ LOG.info("Savepoint written to " + path);
+ }
+
+ @Override
+ public void close() {}
+
+ private static CheckpointStorageLocation createSavepointLocation(Path location) throws IOException {
+ final CheckpointStorageLocationReference reference = AbstractFsCheckpointStorage.encodePathAsReference(location);
+ return new FsCheckpointStorageLocation(
+ location.getFileSystem(),
+ location,
+ location,
+ location,
+ reference,
+ CheckpointingOptions.FS_SMALL_FILE_THRESHOLD.defaultValue(),
+ CheckpointingOptions.FS_WRITE_BUFFER_SIZE.defaultValue());
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SnapshotUtils.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SnapshotUtils.java
new file mode 100644
index 0000000..69f1605
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/SnapshotUtils.java
@@ -0,0 +1,70 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.checkpoint.CheckpointType;
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.runtime.state.CheckpointStorageWorkerView;
+import org.apache.flink.runtime.state.CheckpointStreamFactory;
+import org.apache.flink.runtime.state.filesystem.AbstractFsCheckpointStorage;
+import org.apache.flink.streaming.api.operators.OperatorSnapshotFinalizer;
+import org.apache.flink.streaming.api.operators.OperatorSnapshotFutures;
+import org.apache.flink.streaming.api.operators.StreamOperator;
+
+/**
+ * Takes a final snapshot of the state of an operator subtask.
+ */
+@Internal
+public final class SnapshotUtils {
+ static final long CHECKPOINT_ID = 0L;
+
+ private SnapshotUtils() {}
+
+ public static <OUT, OP extends StreamOperator<OUT>> TaggedOperatorSubtaskState snapshot(
+ OP operator,
+ int index,
+ long timestamp,
+ CheckpointStorageWorkerView checkpointStorage,
+ Path savepointPath) throws Exception {
+
+ CheckpointOptions options = new CheckpointOptions(
+ CheckpointType.SAVEPOINT,
+ AbstractFsCheckpointStorage.encodePathAsReference(savepointPath));
+
+ operator.prepareSnapshotPreBarrier(CHECKPOINT_ID);
+
+ CheckpointStreamFactory storage = checkpointStorage.resolveCheckpointStorageLocation(
+ CHECKPOINT_ID,
+ options.getTargetLocation());
+
+ OperatorSnapshotFutures snapshotInProgress = operator.snapshotState(
+ CHECKPOINT_ID,
+ timestamp,
+ options,
+ storage);
+
+ OperatorSubtaskState state = new OperatorSnapshotFinalizer(snapshotInProgress).getJobManagerOwnedState();
+
+ operator.notifyCheckpointComplete(CHECKPOINT_ID);
+ return new TaggedOperatorSubtaskState(index, state);
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/TaggedOperatorSubtaskState.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/TaggedOperatorSubtaskState.java
new file mode 100644
index 0000000..bf9aacc
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/TaggedOperatorSubtaskState.java
@@ -0,0 +1,69 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+
+import java.util.Objects;
+
+/**
+ * A simple wrapper pojo that tags {@link OperatorSubtaskState} with metadata.
+ */
+@Internal
+@SuppressWarnings("WeakerAccess")
+public final class TaggedOperatorSubtaskState {
+
+ public int index;
+
+ public OperatorSubtaskState state;
+
+ public TaggedOperatorSubtaskState(int index, OperatorSubtaskState state) {
+ this.index = index;
+ this.state = state;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ } else if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ TaggedOperatorSubtaskState that = (TaggedOperatorSubtaskState) o;
+ return index == that.index &&
+ Objects.equals(state, that.state);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(index, state);
+ }
+
+ @Override
+ public String toString() {
+ return "TaggedOperatorSubtaskState{" +
+ "index=" + index +
+ ", state=" + state +
+ '}';
+ }
+}
+
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/BroadcastStateBootstrapOperator.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/BroadcastStateBootstrapOperator.java
new file mode 100644
index 0000000..9c6c866
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/BroadcastStateBootstrapOperator.java
@@ -0,0 +1,105 @@
+/*
+ * 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.state.api.output.operators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.state.BroadcastState;
+import org.apache.flink.api.common.state.MapStateDescriptor;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.state.api.functions.BroadcastStateBootstrapFunction;
+import org.apache.flink.state.api.output.SnapshotUtils;
+import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
+import org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator;
+import org.apache.flink.streaming.api.operators.BoundedOneInput;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.runtime.tasks.ProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+/**
+ * A {@link org.apache.flink.streaming.api.operators.StreamOperator} for executing {@link
+ * BroadcastStateBootstrapFunction}'s.
+ */
+@Internal
+public class BroadcastStateBootstrapOperator<IN>
+ extends AbstractUdfStreamOperator<TaggedOperatorSubtaskState, BroadcastStateBootstrapFunction<IN>>
+ implements OneInputStreamOperator<IN, TaggedOperatorSubtaskState>,
+ BoundedOneInput {
+
+ private static final long serialVersionUID = 1L;
+
+ private final long timestamp;
+
+ private final Path savepointPath;
+
+ private transient ContextImpl context;
+
+ public BroadcastStateBootstrapOperator(long timestamp, Path savepointPath, BroadcastStateBootstrapFunction<IN> function) {
+ super(function);
+ this.timestamp = timestamp;
+
+ this.savepointPath = savepointPath;
+ }
+
+ @Override
+ public void open() throws Exception {
+ super.open();
+ context = new ContextImpl(getProcessingTimeService());
+ }
+
+ @Override
+ public void processElement(StreamRecord<IN> element) throws Exception {
+ userFunction.processElement(element.getValue(), context);
+ }
+
+ @Override
+ public void endInput() throws Exception {
+ TaggedOperatorSubtaskState state = SnapshotUtils.snapshot(
+ this,
+ getRuntimeContext().getIndexOfThisSubtask(),
+ timestamp,
+ getContainingTask().getCheckpointStorage(),
+ savepointPath);
+
+ output.collect(new StreamRecord<>(state));
+ }
+
+ private class ContextImpl implements BroadcastStateBootstrapFunction.Context {
+ private final ProcessingTimeService processingTimeService;
+
+ ContextImpl(ProcessingTimeService processingTimeService) {
+ this.processingTimeService = processingTimeService;
+ }
+
+ @Override
+ public long currentProcessingTime() {
+ return processingTimeService.getCurrentProcessingTime();
+ }
+
+ @Override
+ public <K, V> BroadcastState<K, V> getBroadcastState(MapStateDescriptor<K, V> descriptor) {
+ try {
+ return getOperatorStateBackend().getBroadcastState(descriptor);
+ } catch (Exception e) {
+ throw new FlinkRuntimeException(e);
+ }
+ }
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/KeyedStateBootstrapOperator.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/KeyedStateBootstrapOperator.java
new file mode 100644
index 0000000..53cedac
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/KeyedStateBootstrapOperator.java
@@ -0,0 +1,114 @@
+/*
+ * 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.state.api.output.operators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.runtime.state.VoidNamespace;
+import org.apache.flink.runtime.state.VoidNamespaceSerializer;
+import org.apache.flink.state.api.functions.KeyedStateBootstrapFunction;
+import org.apache.flink.state.api.output.SnapshotUtils;
+import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
+import org.apache.flink.state.api.runtime.VoidTriggerable;
+import org.apache.flink.streaming.api.SimpleTimerService;
+import org.apache.flink.streaming.api.TimerService;
+import org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator;
+import org.apache.flink.streaming.api.operators.BoundedOneInput;
+import org.apache.flink.streaming.api.operators.InternalTimerService;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * A {@link org.apache.flink.streaming.api.operators.StreamOperator} for executing {@link
+ * KeyedStateBootstrapFunction}'s.
+ */
+@Internal
+public class KeyedStateBootstrapOperator<K, IN>
+ extends AbstractUdfStreamOperator<TaggedOperatorSubtaskState, KeyedStateBootstrapFunction<K, IN>>
+ implements OneInputStreamOperator<IN, TaggedOperatorSubtaskState>,
+ BoundedOneInput {
+
+ private static final long serialVersionUID = 1L;
+
+ private final long timestamp;
+
+ private final Path savepointPath;
+
+ private transient KeyedStateBootstrapOperator<K, IN>.ContextImpl context;
+
+ public KeyedStateBootstrapOperator(long timestamp, Path savepointPath, KeyedStateBootstrapFunction<K, IN> function) {
+ super(function);
+
+ this.timestamp = timestamp;
+ this.savepointPath = savepointPath;
+ }
+
+ @Override
+ public void open() throws Exception {
+ super.open();
+
+ InternalTimerService<VoidNamespace> internalTimerService = getInternalTimerService(
+ "user-timers",
+ VoidNamespaceSerializer.INSTANCE,
+ VoidTriggerable.instance());
+
+ TimerService timerService = new SimpleTimerService(internalTimerService);
+
+ context = new KeyedStateBootstrapOperator<K, IN>.ContextImpl(userFunction, timerService);
+ }
+
+ @Override
+ public void processElement(StreamRecord<IN> element) throws Exception {
+ userFunction.processElement(element.getValue(), context);
+ }
+
+ @Override
+ public void endInput() throws Exception {
+ TaggedOperatorSubtaskState state = SnapshotUtils.snapshot(
+ this,
+ getRuntimeContext().getIndexOfThisSubtask(),
+ timestamp,
+ getContainingTask().getCheckpointStorage(),
+ savepointPath);
+
+ output.collect(new StreamRecord<>(state));
+ }
+
+ private class ContextImpl extends KeyedStateBootstrapFunction<K, IN>.Context {
+
+ private final TimerService timerService;
+
+ ContextImpl(KeyedStateBootstrapFunction<K, IN> function, TimerService timerService) {
+ function.super();
+ this.timerService = Preconditions.checkNotNull(timerService);
+ }
+
+ @Override
+ public TimerService timerService() {
+ return timerService;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public K getCurrentKey() {
+ return (K) KeyedStateBootstrapOperator.this.getCurrentKey();
+ }
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/StateBootstrapOperator.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/StateBootstrapOperator.java
new file mode 100644
index 0000000..ca78f87
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/operators/StateBootstrapOperator.java
@@ -0,0 +1,93 @@
+/*
+ * 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.state.api.output.operators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.state.api.functions.StateBootstrapFunction;
+import org.apache.flink.state.api.output.SnapshotUtils;
+import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
+import org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator;
+import org.apache.flink.streaming.api.operators.BoundedOneInput;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.runtime.tasks.ProcessingTimeService;
+
+/**
+ * A {@link org.apache.flink.streaming.api.operators.StreamOperator} for executing {@link
+ * StateBootstrapFunction}'s.
+ */
+@Internal
+public class StateBootstrapOperator<IN>
+ extends AbstractUdfStreamOperator<TaggedOperatorSubtaskState, StateBootstrapFunction<IN>>
+ implements OneInputStreamOperator<IN, TaggedOperatorSubtaskState>,
+ BoundedOneInput {
+
+ private static final long serialVersionUID = 1L;
+
+ private final long timestamp;
+
+ private final Path savepointPath;
+
+ private transient ContextImpl context;
+
+ public StateBootstrapOperator(long timestamp, Path savepointPath, StateBootstrapFunction<IN> function) {
+ super(function);
+
+ this.timestamp = timestamp;
+ this.savepointPath = savepointPath;
+ }
+
+ @Override
+ public void open() throws Exception {
+ super.open();
+ context = new ContextImpl(getProcessingTimeService());
+ }
+
+ @Override
+ public void processElement(StreamRecord<IN> element) throws Exception {
+ userFunction.processElement(element.getValue(), context);
+ }
+
+ @Override
+ public void endInput() throws Exception {
+ TaggedOperatorSubtaskState state = SnapshotUtils.snapshot(
+ this,
+ getRuntimeContext().getIndexOfThisSubtask(),
+ timestamp,
+ getContainingTask().getCheckpointStorage(),
+ savepointPath);
+
+ output.collect(new StreamRecord<>(state));
+ }
+
+ private class ContextImpl implements StateBootstrapFunction.Context {
+ private final ProcessingTimeService processingTimeService;
+
+ ContextImpl(ProcessingTimeService processingTimeService) {
+ this.processingTimeService = processingTimeService;
+ }
+
+ @Override
+ public long currentProcessingTime() {
+ return processingTimeService.getCurrentProcessingTime();
+ }
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/partitioner/HashSelector.java
similarity index 53%
copy from flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
copy to flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/partitioner/HashSelector.java
index 3a2b7cb..1d7d4c4 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/partitioner/HashSelector.java
@@ -16,36 +16,33 @@
* limitations under the License.
*/
-package org.apache.flink.state.api.runtime.metadata;
+package org.apache.flink.state.api.output.partitioner;
import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Collection;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.util.Preconditions;
/**
- * Returns metadata about a savepoint.
+ * A wrapper around a {@link KeySelector} that returns the {@link Object#hashCode()} of the returned
+ * key.
+ *
+ * @param <IN> Type of objects to extract the key from.
*/
@Internal
-public interface SavepointMetadata extends Serializable {
-
- /**
- * @return The max parallelism for the savepoint.
- */
- int maxParallelism();
-
- /**
- * @return Masters states for the savepoint.
- */
- Collection<MasterState> getMasterStates();
-
- /**
- * @return Operator state for the given UID.
- *
- * @throws IOException If the savepoint does not contain operator state with the given uid.
- */
- OperatorState getOperatorState(String uid) throws IOException;
+public class HashSelector<IN> implements KeySelector<IN, Integer> {
+
+ private static final long serialVersionUID = 1L;
+
+ private final KeySelector<IN, ?> keySelector;
+
+ public HashSelector(KeySelector<IN, ?> keySelector) {
+ Preconditions.checkNotNull(keySelector);
+ this.keySelector = keySelector;
+ }
+
+ @Override
+ public Integer getKey(IN value) throws Exception {
+ return keySelector.getKey(value).hashCode();
+ }
}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/partitioner/KeyGroupRangePartitioner.java
similarity index 53%
copy from flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
copy to flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/partitioner/KeyGroupRangePartitioner.java
index 3a2b7cb..64bffa0 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/partitioner/KeyGroupRangePartitioner.java
@@ -16,36 +16,31 @@
* limitations under the License.
*/
-package org.apache.flink.state.api.runtime.metadata;
+package org.apache.flink.state.api.output.partitioner;
import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Collection;
+import org.apache.flink.api.common.functions.Partitioner;
+import org.apache.flink.runtime.state.KeyGroupRangeAssignment;
/**
- * Returns metadata about a savepoint.
+ * A partitioner that selects the target channel based on the key group index.
*/
@Internal
-public interface SavepointMetadata extends Serializable {
+public class KeyGroupRangePartitioner implements Partitioner<Integer> {
+
+ private static final long serialVersionUID = 1L;
- /**
- * @return The max parallelism for the savepoint.
- */
- int maxParallelism();
+ private final int maxParallelism;
- /**
- * @return Masters states for the savepoint.
- */
- Collection<MasterState> getMasterStates();
+ public KeyGroupRangePartitioner(int maxParallelism) {
+ this.maxParallelism = maxParallelism;
+ }
- /**
- * @return Operator state for the given UID.
- *
- * @throws IOException If the savepoint does not contain operator state with the given uid.
- */
- OperatorState getOperatorState(String uid) throws IOException;
+ @Override
+ public int partition(Integer key, int numPartitions) {
+ return KeyGroupRangeAssignment.computeOperatorIndexForKeyGroup(
+ maxParallelism,
+ numPartitions,
+ KeyGroupRangeAssignment.computeKeyGroupForKeyHash(key, maxParallelism));
+ }
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java
similarity index 52%
copy from flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
copy to flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java
index 3a2b7cb..6306447 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/BoundedStreamConfig.java
@@ -16,36 +16,33 @@
* limitations under the License.
*/
-package org.apache.flink.state.api.runtime.metadata;
+package org.apache.flink.state.api.runtime;
import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Collection;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.streaming.api.CheckpointingMode;
+import org.apache.flink.streaming.api.graph.StreamConfig;
/**
- * Returns metadata about a savepoint.
+ * A {@link StreamConfig} with default settings.
*/
@Internal
-public interface SavepointMetadata extends Serializable {
+public class BoundedStreamConfig extends StreamConfig {
+ public BoundedStreamConfig() {
+ super(new Configuration());
- /**
- * @return The max parallelism for the savepoint.
- */
- int maxParallelism();
+ setChainStart();
+ setCheckpointingEnabled(true);
+ setCheckpointMode(CheckpointingMode.EXACTLY_ONCE);
+ }
- /**
- * @return Masters states for the savepoint.
- */
- Collection<MasterState> getMasterStates();
+ public <IN> BoundedStreamConfig(TypeSerializer<?> keySerializer, KeySelector<IN, ?> keySelector) {
+ this();
- /**
- * @return Operator state for the given UID.
- *
- * @throws IOException If the savepoint does not contain operator state with the given uid.
- */
- OperatorState getOperatorState(String uid) throws IOException;
+ setStateKeySerializer(keySerializer);
+ setStatePartitioner(0, keySelector);
+ }
}
+
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
new file mode 100644
index 0000000..158fa35
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
@@ -0,0 +1,67 @@
+package org.apache.flink.state.api.runtime.metadata;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.runtime.checkpoint.MasterState;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.state.api.BootstrapTransformation;
+import org.apache.flink.state.api.runtime.OperatorIDGenerator;
+import org.apache.flink.types.Either;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Savepoint metadata that can be modified.
+ */
+@Internal
+public class ModifiableSavepointMetadata extends SavepointMetadata {
+
+ private transient Map<OperatorID, Either<OperatorState, BootstrapTransformation<?>>> operatorStateIndex;
+
+ public ModifiableSavepointMetadata(int maxParallelism, Collection<MasterState> masterStates, Collection<OperatorState> initialStates) {
+ super(maxParallelism, masterStates);
+
+ this.operatorStateIndex = new HashMap<>(initialStates.size());
+
+ for (OperatorState operatorState : initialStates) {
+ operatorStateIndex.put(operatorState.getOperatorID(), Either.Left(operatorState));
+ }
+ }
+
+ /**
+ * @return Operator state for the given UID.
+ *
+ * @throws IOException If the savepoint does not contain operator state with the given uid.
+ */
+ public OperatorState getOperatorState(String uid) throws IOException {
+ OperatorID operatorID = OperatorIDGenerator.fromUid(uid);
+
+ Either<OperatorState, BootstrapTransformation<?>> operatorState = operatorStateIndex.get(operatorID);
+ if (operatorState == null || operatorState.isRight()) {
+ throw new IOException("Savepoint does not contain state with operator uid " + uid);
+ }
+
+ return operatorState.left();
+ }
+
+ public void removeOperator(String uid) {
+ operatorStateIndex.remove(OperatorIDGenerator.fromUid(uid));
+ }
+
+ public void addOperator(String uid, BootstrapTransformation<?> transformation) {
+ OperatorID id = OperatorIDGenerator.fromUid(uid);
+
+ if (operatorStateIndex.containsKey(id)) {
+ throw new IllegalArgumentException("The savepoint already contains uid " + uid + ". All uid's must be unique");
+ }
+
+ operatorStateIndex.put(id, Either.Right(transformation));
+ }
+
+ public Map<OperatorID, Either<OperatorState, BootstrapTransformation<?>>> getOperatorStates() {
+ return operatorStateIndex;
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OnDiskSavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OnDiskSavepointMetadata.java
deleted file mode 100644
index c4010d6..0000000
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/OnDiskSavepointMetadata.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * 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.state.api.runtime.metadata;
-
-import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-import org.apache.flink.runtime.checkpoint.savepoint.Savepoint;
-import org.apache.flink.runtime.jobgraph.OperatorID;
-import org.apache.flink.state.api.runtime.OperatorIDGenerator;
-import org.apache.flink.state.api.runtime.SavepointLoader;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Returns metadata about an existing savepoint.
- */
-@Internal
-public class OnDiskSavepointMetadata implements SavepointMetadata {
-
- private static final long serialVersionUID = 3623389893479485802L;
-
- private final int maxParallelism;
-
- private final Collection<MasterState> masterStates;
-
- private final Map<OperatorID, OperatorState> operatorStateIndex;
-
- public OnDiskSavepointMetadata(String path) throws IOException {
- Savepoint savepoint = SavepointLoader.loadSavepoint(path);
-
- this.maxParallelism = savepoint
- .getOperatorStates()
- .stream()
- .map(OperatorState::getMaxParallelism)
- .max(Comparator.naturalOrder())
- .orElseThrow(() -> new RuntimeException("Savepoint's must contain at least one operator"));
-
- this.masterStates = savepoint.getMasterStates();
-
- Collection<OperatorState> operatorStates = savepoint.getOperatorStates();
- this.operatorStateIndex = new HashMap<>(operatorStates.size());
- operatorStates.forEach(state -> this.operatorStateIndex.put(state.getOperatorID(), state));
- }
-
- /**
- * @return The max parallelism for the savepoint.
- */
- @Override
- public int maxParallelism() {
- return maxParallelism;
- }
-
- /**
- * @return Masters states for the savepoint.
- */
- @Override
- public Collection<MasterState> getMasterStates() {
- return masterStates;
- }
-
- /**
- * @return Operator state for the given UID.
- */
- @Override
- public OperatorState getOperatorState(String uid) throws IOException {
- OperatorID operatorID = OperatorIDGenerator.fromUid(uid);
-
- OperatorState operatorState = operatorStateIndex.get(operatorID);
- if (operatorState == null) {
- throw new IOException("Savepoint does not contain state with operator uid " + uid);
- }
-
- return operatorState;
- }
-}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
index 3a2b7cb..40ce66a 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
@@ -20,9 +20,7 @@ package org.apache.flink.state.api.runtime.metadata;
import org.apache.flink.annotation.Internal;
import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
@@ -30,22 +28,30 @@ import java.util.Collection;
* Returns metadata about a savepoint.
*/
@Internal
-public interface SavepointMetadata extends Serializable {
+public class SavepointMetadata implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final int maxParallelism;
+
+ private final Collection<MasterState> masterStates;
+
+ public SavepointMetadata(int maxParallelism, Collection<MasterState> masterStates) {
+ this.maxParallelism = maxParallelism;
+ this.masterStates = masterStates;
+ }
/**
* @return The max parallelism for the savepoint.
*/
- int maxParallelism();
+ public int maxParallelism() {
+ return maxParallelism;
+ }
/**
* @return Masters states for the savepoint.
*/
- Collection<MasterState> getMasterStates();
-
- /**
- * @return Operator state for the given UID.
- *
- * @throws IOException If the savepoint does not contain operator state with the given uid.
- */
- OperatorState getOperatorState(String uid) throws IOException;
+ public Collection<MasterState> getMasterStates() {
+ return masterStates;
+ }
}
diff --git a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java
new file mode 100644
index 0000000..707ccde
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/BootstrapTransformationTest.java
@@ -0,0 +1,170 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.ExecutionEnvironment;
+import org.apache.flink.api.java.operators.DataSource;
+import org.apache.flink.api.java.operators.Operator;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.runtime.state.FunctionInitializationContext;
+import org.apache.flink.runtime.state.FunctionSnapshotContext;
+import org.apache.flink.runtime.state.memory.MemoryStateBackend;
+import org.apache.flink.state.api.functions.BroadcastStateBootstrapFunction;
+import org.apache.flink.state.api.functions.StateBootstrapFunction;
+import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
+import org.apache.flink.state.api.runtime.OperatorIDGenerator;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
+import org.apache.flink.test.util.AbstractTestBase;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Collections;
+
+/**
+ * Tests for bootstrap transformations.
+ */
+public class BootstrapTransformationTest extends AbstractTestBase {
+
+ @Test
+ public void testBroadcastStateTransformationParallelism() {
+ ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+ env.setParallelism(10);
+
+ DataSet<Integer> input = env.fromElements(0);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(input)
+ .transform(new ExampleBroadcastStateBootstrapFunction());
+
+ int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(4, Collections.emptyList()));
+ DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
+ OperatorIDGenerator.fromUid("uid"),
+ new MemoryStateBackend(),
+ new Path(),
+ maxParallelism
+ );
+
+ Assert.assertEquals("Broadcast transformations should always be run at parallelism 1",
+ 1,
+ getParallelism(result));
+ }
+
+ @Test
+ public void testDefaultParallelismRespectedWhenLessThanMaxParallelism() {
+ ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+ env.setParallelism(4);
+
+ DataSource<Integer> input = env.fromElements(0);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(input)
+ .transform(new ExampleStateBootstrapFunction());
+
+ int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(10, Collections.emptyList()));
+ DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
+ OperatorIDGenerator.fromUid("uid"),
+ new MemoryStateBackend(),
+ new Path(),
+ maxParallelism
+ );
+
+ Assert.assertEquals(
+ "The parallelism of a data set should not change when less than the max parallelism of the savepoint",
+ ExecutionConfig.PARALLELISM_DEFAULT,
+ getParallelism(result));
+ }
+
+ @Test
+ public void testMaxParallelismRespected() {
+ ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+ env.setParallelism(10);
+
+ DataSource<Integer> input = env.fromElements(0);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(input)
+ .transform(new ExampleStateBootstrapFunction());
+
+ int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(4, Collections.emptyList()));
+ DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
+ OperatorIDGenerator.fromUid("uid"),
+ new MemoryStateBackend(),
+ new Path(),
+ maxParallelism
+ );
+
+ Assert.assertEquals(
+ "The parallelism of a data set should be constrained my the savepoint max parallelism",
+ 4,
+ getParallelism(result));
+ }
+
+ @Test
+ public void testOperatorSpecificMaxParallelismRespected() {
+ ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+ env.setParallelism(4);
+
+ DataSource<Integer> input = env.fromElements(0);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(input)
+ .setMaxParallelism(1)
+ .transform(new ExampleStateBootstrapFunction());
+
+ int maxParallelism = transformation.getMaxParallelism(new SavepointMetadata(4, Collections.emptyList()));
+ DataSet<TaggedOperatorSubtaskState> result = transformation.writeOperatorSubtaskStates(
+ OperatorIDGenerator.fromUid("uid"),
+ new MemoryStateBackend(),
+ new Path(),
+ maxParallelism
+ );
+
+ Assert.assertEquals("The parallelism of a data set should be constrained my the savepoint max parallelism", 1, getParallelism(result));
+ }
+
+ private static <T> int getParallelism(DataSet<T> dataSet) {
+ //All concrete implementations of DataSet are operators so this should always be safe.
+ return ((Operator) dataSet).getParallelism();
+ }
+
+ private static class ExampleBroadcastStateBootstrapFunction extends BroadcastStateBootstrapFunction<Integer> {
+
+ @Override
+ public void processElement(Integer value, Context ctx) throws Exception {
+ }
+ }
+
+ private static class ExampleStateBootstrapFunction extends StateBootstrapFunction<Integer> {
+
+ @Override
+ public void processElement(Integer value, Context ctx) throws Exception {
+ }
+
+ @Override
+ public void snapshotState(FunctionSnapshotContext context) throws Exception {
+ }
+
+ @Override
+ public void initializeState(FunctionInitializationContext context) throws Exception {
+ }
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java
new file mode 100644
index 0000000..7d34244
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.api.java.ExecutionEnvironment;
+import org.apache.flink.api.java.operators.DataSource;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.state.FunctionInitializationContext;
+import org.apache.flink.runtime.state.FunctionSnapshotContext;
+import org.apache.flink.runtime.state.memory.MemoryStateBackend;
+import org.apache.flink.state.api.functions.StateBootstrapFunction;
+import org.apache.flink.state.api.runtime.OperatorIDGenerator;
+import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Tests the api of creating new savepoints.
+ */
+public class SavepointTest {
+
+ private static final String UID = "uid";
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNewSavepointEnforceUniqueUIDs() {
+ ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+ env.setParallelism(10);
+
+ DataSource<Integer> input = env.fromElements(0);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(input)
+ .transform(new ExampleStateBootstrapFunction());
+
+ ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(1, Collections.emptyList(), Collections.emptyList());
+
+ new NewSavepoint(metadata, new MemoryStateBackend())
+ .withOperator(UID, transformation)
+ .withOperator(UID, transformation);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testExistingSavepointEnforceUniqueUIDs() throws IOException {
+ ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+ env.setParallelism(10);
+
+ DataSource<Integer> input = env.fromElements(0);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(input)
+ .transform(new ExampleStateBootstrapFunction());
+
+ Collection<OperatorState> operatorStates = Collections.singletonList(new OperatorState(
+ OperatorIDGenerator.fromUid(UID), 1, 4));
+
+ ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(4, Collections.emptyList(), operatorStates);
+
+ new ExistingSavepoint(env, metadata, new MemoryStateBackend())
+ .withOperator(UID, transformation)
+ .withOperator(UID, transformation);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testExistingSavepointEnforceUniqueUIDsWithOldSavepoint() throws IOException {
+ ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+ env.setParallelism(10);
+
+ DataSource<Integer> input = env.fromElements(0);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(input)
+ .transform(new ExampleStateBootstrapFunction());
+
+ Collection<OperatorState> operatorStates = Collections.singletonList(new OperatorState(
+ OperatorIDGenerator.fromUid(UID), 1, 4));
+
+ ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(4, Collections.emptyList(), operatorStates);
+
+ new ExistingSavepoint(env, metadata, new MemoryStateBackend())
+ .withOperator(UID, transformation)
+ .write("");
+ }
+
+ private static class ExampleStateBootstrapFunction extends StateBootstrapFunction<Integer> {
+
+ @Override
+ public void processElement(Integer value, Context ctx) throws Exception {
+ }
+
+ @Override
+ public void snapshotState(FunctionSnapshotContext context) throws Exception {
+ }
+
+ @Override
+ public void initializeState(FunctionInitializationContext context) throws Exception {
+ }
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointWriterITCase.java b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointWriterITCase.java
new file mode 100644
index 0000000..5621fac
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointWriterITCase.java
@@ -0,0 +1,442 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.api.common.functions.RichFlatMapFunction;
+import org.apache.flink.api.common.functions.RichMapFunction;
+import org.apache.flink.api.common.state.ListState;
+import org.apache.flink.api.common.state.ListStateDescriptor;
+import org.apache.flink.api.common.state.MapStateDescriptor;
+import org.apache.flink.api.common.state.ValueState;
+import org.apache.flink.api.common.state.ValueStateDescriptor;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.ExecutionEnvironment;
+import org.apache.flink.client.program.ClusterClient;
+import org.apache.flink.client.program.ProgramInvocationException;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.contrib.streaming.state.RocksDBStateBackend;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings;
+import org.apache.flink.runtime.state.FunctionInitializationContext;
+import org.apache.flink.runtime.state.FunctionSnapshotContext;
+import org.apache.flink.runtime.state.StateBackend;
+import org.apache.flink.runtime.state.memory.MemoryStateBackend;
+import org.apache.flink.state.api.functions.BroadcastStateBootstrapFunction;
+import org.apache.flink.state.api.functions.KeyedStateBootstrapFunction;
+import org.apache.flink.state.api.functions.StateBootstrapFunction;
+import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
+import org.apache.flink.streaming.api.functions.sink.DiscardingSink;
+import org.apache.flink.streaming.api.functions.sink.SinkFunction;
+import org.apache.flink.test.util.AbstractTestBase;
+import org.apache.flink.util.AbstractID;
+import org.apache.flink.util.Collector;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentSkipListSet;
+
+/**
+ * IT test for writing savepoints.
+ */
+@RunWith(value = Parameterized.class)
+public class SavepointWriterITCase extends AbstractTestBase {
+ private static final String ACCOUNT_UID = "accounts";
+
+ private static final String CURRENCY_UID = "currency";
+
+ private static final String MODIFY_UID = "numbers";
+
+ private static final MapStateDescriptor<String, Double> descriptor = new MapStateDescriptor<>(
+ "currency-rate", Types.STRING, Types.DOUBLE);
+
+ private final StateBackend backend;
+
+ private static final Collection<Account> accounts = Arrays.asList(
+ new Account(1, 100.0),
+ new Account(2, 100.0),
+ new Account(3, 100.0));
+
+ private static final Collection<CurrencyRate> currencyRates = Arrays.asList(
+ new CurrencyRate("USD", 1.0),
+ new CurrencyRate("EUR", 1.3)
+ );
+
+ public SavepointWriterITCase(StateBackend backend) throws Exception {
+ this.backend = backend;
+
+ //reset the cluster so we can change the state backend
+ miniClusterResource.after();
+ miniClusterResource.before();
+ }
+
+ @Parameterized.Parameters(name = "Savepoint Writer: {0}")
+ public static Collection<StateBackend> data() {
+ return Arrays.asList(
+ new MemoryStateBackend(),
+ new RocksDBStateBackend((StateBackend) new MemoryStateBackend()));
+ }
+
+ @Test
+ public void testStateBootstrapAndModification() throws Exception {
+ final String savepointPath = getTempDirPath(new AbstractID().toHexString());
+
+ bootstrapState(savepointPath);
+
+ validateBootstrap(savepointPath);
+
+ final String modifyPath = getTempDirPath(new AbstractID().toHexString());
+
+ modifySavepoint(savepointPath, modifyPath);
+
+ validateModification(modifyPath);
+ }
+
+ private void bootstrapState(String savepointPath) throws Exception {
+ ExecutionEnvironment bEnv = ExecutionEnvironment.getExecutionEnvironment();
+
+ DataSet<Account> accountDataSet = bEnv.fromCollection(accounts);
+
+ BootstrapTransformation<Account> transformation = OperatorTransformation
+ .bootstrapWith(accountDataSet)
+ .keyBy(acc -> acc.id)
+ .transform(new AccountBootstrapper());
+
+ DataSet<CurrencyRate> currencyDataSet = bEnv.fromCollection(currencyRates);
+
+ BootstrapTransformation<CurrencyRate> broadcastTransformation = OperatorTransformation
+ .bootstrapWith(currencyDataSet)
+ .transform(new CurrencyBootstrapFunction());
+
+ Savepoint
+ .create(backend, 128)
+ .withOperator(ACCOUNT_UID, transformation)
+ .withOperator(CURRENCY_UID, broadcastTransformation)
+ .write(savepointPath);
+
+ bEnv.execute("Bootstrap");
+ }
+
+ private void validateBootstrap(String savepointPath) throws ProgramInvocationException {
+ StreamExecutionEnvironment sEnv = StreamExecutionEnvironment.getExecutionEnvironment();
+ sEnv.setStateBackend(backend);
+
+ CollectSink.accountList.clear();
+
+ sEnv.fromCollection(accounts)
+ .keyBy(acc -> acc.id)
+ .flatMap(new UpdateAndGetAccount())
+ .uid(ACCOUNT_UID)
+ .addSink(new CollectSink());
+
+ sEnv
+ .fromCollection(currencyRates)
+ .connect(sEnv.fromCollection(currencyRates).broadcast(descriptor))
+ .process(new CurrencyValidationFunction())
+ .uid(CURRENCY_UID)
+ .addSink(new DiscardingSink<>());
+
+ JobGraph jobGraph = sEnv.getStreamGraph().getJobGraph();
+ jobGraph.setSavepointRestoreSettings(SavepointRestoreSettings.forPath(savepointPath, false));
+
+ ClusterClient<?> client = miniClusterResource.getClusterClient();
+ client.submitJob(jobGraph, SavepointWriterITCase.class.getClassLoader());
+
+ Assert.assertEquals("Unexpected output", 3, CollectSink.accountList.size());
+ }
+
+ private void modifySavepoint(String savepointPath, String modifyPath) throws Exception {
+ ExecutionEnvironment bEnv = ExecutionEnvironment.getExecutionEnvironment();
+
+ DataSet<Integer> data = bEnv.fromElements(1, 2, 3);
+
+ BootstrapTransformation<Integer> transformation = OperatorTransformation
+ .bootstrapWith(data)
+ .transform(new ModifyProcessFunction());
+
+ Savepoint
+ .load(bEnv, savepointPath, backend)
+ .removeOperator(CURRENCY_UID)
+ .withOperator(MODIFY_UID, transformation)
+ .write(modifyPath);
+
+ bEnv.execute("Modifying");
+ }
+
+ private void validateModification(String savepointPath) throws ProgramInvocationException {
+ StreamExecutionEnvironment sEnv = StreamExecutionEnvironment.getExecutionEnvironment();
+ sEnv.setStateBackend(backend);
+
+ CollectSink.accountList.clear();
+
+ DataStream<Account> stream = sEnv.fromCollection(accounts)
+ .keyBy(acc -> acc.id)
+ .flatMap(new UpdateAndGetAccount())
+ .uid(ACCOUNT_UID);
+
+ stream.addSink(new CollectSink());
+
+ stream
+ .map(acc -> acc.id)
+ .map(new StatefulOperator())
+ .uid(MODIFY_UID)
+ .addSink(new DiscardingSink<>());
+
+ JobGraph jobGraph = sEnv.getStreamGraph().getJobGraph();
+ jobGraph.setSavepointRestoreSettings(SavepointRestoreSettings.forPath(savepointPath, false));
+
+ ClusterClient<?> client = miniClusterResource.getClusterClient();
+ client.submitJob(jobGraph, SavepointWriterITCase.class.getClassLoader());
+
+ Assert.assertEquals("Unexpected output", 3, CollectSink.accountList.size());
+ }
+
+ /**
+ * A simple pojo.
+ */
+ @SuppressWarnings("WeakerAccess")
+ public static class Account {
+ Account(int id, double amount) {
+ this.id = id;
+ this.amount = amount;
+ this.timestamp = 1000L;
+ }
+
+ public int id;
+
+ public double amount;
+
+ public long timestamp;
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof Account && ((Account) obj).id == id && ((Account) obj).amount == amount;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, amount);
+ }
+ }
+
+ /**
+ * A simple pojo.
+ */
+ @SuppressWarnings("WeakerAccess")
+ public static class CurrencyRate {
+ public String currency;
+
+ public Double rate;
+
+ CurrencyRate(String currency, double rate) {
+ this.currency = currency;
+ this.rate = rate;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof CurrencyRate
+ && ((CurrencyRate) obj).currency.equals(currency)
+ && ((CurrencyRate) obj).rate.equals(rate);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(currency, rate);
+ }
+ }
+
+ /**
+ * A savepoint writer function.
+ */
+ public static class AccountBootstrapper extends KeyedStateBootstrapFunction<Integer, Account> {
+ ValueState<Double> state;
+
+ @Override
+ public void open(Configuration parameters) {
+ ValueStateDescriptor<Double> descriptor = new ValueStateDescriptor<>("total", Types.DOUBLE);
+ state = getRuntimeContext().getState(descriptor);
+ }
+
+ @Override
+ public void processElement(Account value, Context ctx) throws Exception {
+ state.update(value.amount);
+ }
+ }
+
+ /**
+ * A streaming function bootstrapped off the state.
+ */
+ public static class UpdateAndGetAccount extends RichFlatMapFunction<Account, Account> {
+ ValueState<Double> state;
+
+ @Override
+ public void open(Configuration parameters) throws Exception {
+ super.open(parameters);
+
+ ValueStateDescriptor<Double> descriptor = new ValueStateDescriptor<>("total", Types.DOUBLE);
+ state = getRuntimeContext().getState(descriptor);
+ }
+
+ @Override
+ public void flatMap(Account value, Collector<Account> out) throws Exception {
+ Double current = state.value();
+ if (current != null) {
+ value.amount += current;
+ }
+
+ state.update(value.amount);
+ out.collect(value);
+ }
+ }
+
+ /**
+ * A bootstrap function.
+ */
+ public static class ModifyProcessFunction extends StateBootstrapFunction<Integer> {
+ List<Integer> numbers;
+
+ ListState<Integer> state;
+
+ @Override
+ public void open(Configuration parameters) {
+ numbers = new ArrayList<>();
+ }
+
+ @Override
+ public void processElement(Integer value, Context ctx) {
+ numbers.add(value);
+ }
+
+ @Override
+ public void snapshotState(FunctionSnapshotContext context) throws Exception {
+ state.clear();
+ state.addAll(numbers);
+ }
+
+ @Override
+ public void initializeState(FunctionInitializationContext context) throws Exception {
+ state = context.getOperatorStateStore().getUnionListState(
+ new ListStateDescriptor<>("numbers", Types.INT)
+ );
+ }
+ }
+
+ /**
+ * A streaming function bootstrapped off the state.
+ */
+ public static class StatefulOperator extends RichMapFunction<Integer, Integer> implements CheckpointedFunction {
+ List<Integer> numbers;
+
+ ListState<Integer> state;
+
+ @Override
+ public void open(Configuration parameters) {
+ numbers = new ArrayList<>();
+ }
+
+ @Override
+ public void snapshotState(FunctionSnapshotContext context) throws Exception {
+ state.clear();
+ state.addAll(numbers);
+ }
+
+ @Override
+ public void initializeState(FunctionInitializationContext context) throws Exception {
+ state = context.getOperatorStateStore().getUnionListState(
+ new ListStateDescriptor<>("numbers", Types.INT)
+ );
+
+ if (context.isRestored()) {
+ Set<Integer> expected = new HashSet<>();
+ expected.add(1);
+ expected.add(2);
+ expected.add(3);
+
+ for (Integer number : state.get()) {
+ Assert.assertTrue("Duplicate state", expected.contains(number));
+ expected.remove(number);
+ }
+
+ Assert.assertTrue("Failed to bootstrap all state elements: " + Arrays.toString(expected.toArray()), expected.isEmpty());
+ }
+ }
+
+ @Override
+ public Integer map(Integer value) {
+ return null;
+ }
+ }
+
+ /**
+ * A broadcast bootstrap function.
+ */
+ public static class CurrencyBootstrapFunction extends BroadcastStateBootstrapFunction<CurrencyRate> {
+
+ @Override
+ public void processElement(CurrencyRate value, Context ctx) throws Exception {
+ ctx.getBroadcastState(descriptor).put(value.currency, value.rate);
+ }
+ }
+
+ /**
+ * Checks the restored broadcast state.
+ */
+ public static class CurrencyValidationFunction extends BroadcastProcessFunction<CurrencyRate, CurrencyRate, Void> {
+
+ @Override
+ public void processElement(CurrencyRate value, ReadOnlyContext ctx, Collector<Void> out) throws Exception {
+ Assert.assertEquals(
+ "Incorrect currency rate",
+ value.rate,
+ ctx.getBroadcastState(descriptor).get(value.currency),
+ 0.0001);
+ }
+
+ @Override
+ public void processBroadcastElement(CurrencyRate value, Context ctx, Collector<Void> out) {
+ //ignore
+ }
+ }
+
+ /**
+ * A simple collections sink.
+ */
+ public static class CollectSink implements SinkFunction<Account> {
+ static Set<Integer> accountList = new ConcurrentSkipListSet<>();
+
+ @Override
+ public void invoke(Account value, Context context) {
+ accountList.add(value.id);
+ }
+ }
+}
diff --git a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SavepointOutputFormatTest.java b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SavepointOutputFormatTest.java
new file mode 100644
index 0000000..7cc4b2f
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SavepointOutputFormatTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.runtime.checkpoint.savepoint.Savepoint;
+import org.apache.flink.runtime.checkpoint.savepoint.SavepointV2;
+import org.apache.flink.state.api.runtime.OperatorIDGenerator;
+import org.apache.flink.state.api.runtime.SavepointLoader;
+import org.apache.flink.streaming.util.MockStreamingRuntimeContext;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.util.Collections;
+
+/**
+ * Test for writing output savepoint metadata.
+ */
+public class SavepointOutputFormatTest {
+
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+ @Test(expected = IllegalStateException.class)
+ public void testSavepointOutputFormatOnlyWorksWithParallelismOne() throws Exception {
+ Path path = new Path(temporaryFolder.newFolder().getAbsolutePath());
+ SavepointOutputFormat format = createSavepointOutputFormat(path);
+
+ format.open(0, 2);
+ }
+
+ @Test
+ public void testSavepointOutputFormat() throws Exception {
+ Path path = new Path(temporaryFolder.newFolder().getAbsolutePath());
+ SavepointOutputFormat format = createSavepointOutputFormat(path);
+
+ Savepoint savepoint = createSavepoint();
+
+ format.open(0, 1);
+ format.writeRecord(savepoint);
+ format.close();
+
+ Savepoint savepointOnDisk = SavepointLoader.loadSavepoint(path.getPath());
+
+ Assert.assertEquals(
+ "Incorrect checkpoint id",
+ savepoint.getCheckpointId(),
+ savepointOnDisk.getCheckpointId());
+
+ Assert.assertEquals(
+ "Incorrect number of operator states in savepoint",
+ savepoint.getOperatorStates().size(),
+ savepointOnDisk.getOperatorStates().size());
+
+ Assert.assertEquals(
+ "Incorrect operator state in savepoint",
+ savepoint.getOperatorStates().iterator().next(),
+ savepointOnDisk.getOperatorStates().iterator().next());
+ }
+
+ private SavepointV2 createSavepoint() {
+ OperatorState operatorState = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 128);
+
+ operatorState.putState(0, new OperatorSubtaskState());
+ return new SavepointV2(0, Collections.singleton(operatorState), Collections.emptyList());
+ }
+
+ private SavepointOutputFormat createSavepointOutputFormat(Path path) throws Exception {
+ RuntimeContext ctx = new MockStreamingRuntimeContext(false, 1, 0);
+
+ SavepointOutputFormat format = new SavepointOutputFormat(path);
+ format.setRuntimeContext(ctx);
+
+ return format;
+ }
+}
+
diff --git a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SnapshotUtilsTest.java b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SnapshotUtilsTest.java
new file mode 100644
index 0000000..01bcbbc
--- /dev/null
+++ b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/output/SnapshotUtilsTest.java
@@ -0,0 +1,155 @@
+/*
+ * 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.state.api.output;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.state.CheckpointStorageWorkerView;
+import org.apache.flink.runtime.state.CheckpointStreamFactory;
+import org.apache.flink.runtime.state.ttl.mock.MockStateBackend;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OperatorSnapshotFutures;
+import org.apache.flink.streaming.api.operators.StreamOperator;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Tests that snapshot utils can properly snapshot an operator.
+ */
+public class SnapshotUtilsTest {
+
+ private static final List<String> EXPECTED_CALL_OPERATOR_SNAPSHOT = Arrays.asList(
+ "prepareSnapshotPreBarrier",
+ "snapshotState",
+ "notifyCheckpointComplete");
+
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
+
+ private static final List<String> ACTUAL_ORDER_TRACKING =
+ Collections.synchronizedList(new ArrayList<>(EXPECTED_CALL_OPERATOR_SNAPSHOT.size()));
+
+ @Test
+ public void testSnapshotUtilsLifecycle() throws Exception {
+ StreamOperator<Void> operator = new LifecycleOperator();
+ CheckpointStorageWorkerView storage = new MockStateBackend().createCheckpointStorage(new JobID());
+
+ Path path = new Path(folder.newFolder().getAbsolutePath());
+
+ SnapshotUtils.snapshot(operator, 0, 0L, storage, path);
+
+ Assert.assertEquals(EXPECTED_CALL_OPERATOR_SNAPSHOT, ACTUAL_ORDER_TRACKING);
+ }
+
+ private static class LifecycleOperator implements StreamOperator<Void> {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void open() throws Exception {
+ ACTUAL_ORDER_TRACKING.add("open");
+ }
+
+ @Override
+ public void close() throws Exception {
+ ACTUAL_ORDER_TRACKING.add("close");
+ }
+
+ @Override
+ public void dispose() throws Exception {
+ ACTUAL_ORDER_TRACKING.add("dispose");
+ }
+
+ @Override
+ public void prepareSnapshotPreBarrier(long checkpointId) throws Exception {
+ ACTUAL_ORDER_TRACKING.add("prepareSnapshotPreBarrier");
+ }
+
+ @Override
+ public OperatorSnapshotFutures snapshotState(long checkpointId, long timestamp, CheckpointOptions checkpointOptions, CheckpointStreamFactory storageLocation) throws Exception {
+ ACTUAL_ORDER_TRACKING.add("snapshotState");
+ return new OperatorSnapshotFutures();
+ }
+
+ @Override
+ public void initializeState() throws Exception {
+ ACTUAL_ORDER_TRACKING.add("initializeState");
+ }
+
+ @Override
+ public void setKeyContextElement1(StreamRecord<?> record) throws Exception {
+ ACTUAL_ORDER_TRACKING.add("setKeyContextElement1");
+ }
+
+ @Override
+ public void setKeyContextElement2(StreamRecord<?> record) throws Exception {
+ ACTUAL_ORDER_TRACKING.add("setKeyContextElement2");
+ }
+
+ @Override
+ public ChainingStrategy getChainingStrategy() {
+ ACTUAL_ORDER_TRACKING.add("getChainingStrategy");
+ return null;
+ }
+
+ @Override
+ public void setChainingStrategy(ChainingStrategy strategy) {
+ ACTUAL_ORDER_TRACKING.add("setChainingStrategy");
+ }
+
+ @Override
+ public MetricGroup getMetricGroup() {
+ ACTUAL_ORDER_TRACKING.add("getMetricGroup");
+ return null;
+ }
+
+ @Override
+ public OperatorID getOperatorID() {
+ ACTUAL_ORDER_TRACKING.add("getOperatorID");
+ return null;
+ }
+
+ @Override
+ public void notifyCheckpointComplete(long checkpointId) throws Exception {
+ ACTUAL_ORDER_TRACKING.add("notifyCheckpointComplete");
+ }
+
+ @Override
+ public void setCurrentKey(Object key) {
+ ACTUAL_ORDER_TRACKING.add("setCurrentKey");
+ }
+
+ @Override
+ public Object getCurrentKey() {
+ ACTUAL_ORDER_TRACKING.add("getCurrentKey");
+ return null;
+ }
+ }
+}
[flink] 07/11: [FLINK-12963] [state-processor] Simplify
SavepointMetadata hierarchy
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit f16fed8ae5d581cb08e3121b0a68e01c4e2107b5
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Thu Jul 4 03:15:02 2019 +0800
[FLINK-12963] [state-processor] Simplify SavepointMetadata hierarchy
From the current state of its usage, the previous hierarchy
(ModifiableSavepointMetada -> SavepointMetadata) is redundant. This
commit simplifies this to just be a single SavepointMetadata class.
---
.../apache/flink/state/api/ExistingSavepoint.java | 6 +-
.../org/apache/flink/state/api/NewSavepoint.java | 4 +-
.../java/org/apache/flink/state/api/Savepoint.java | 6 +-
.../apache/flink/state/api/WritableSavepoint.java | 8 +-
.../metadata/ModifiableSavepointMetadata.java | 88 ---------------------
.../api/runtime/metadata/SavepointMetadata.java | 91 +++++++++++++++++++---
.../org/apache/flink/state/api/SavepointTest.java | 8 +-
7 files changed, 98 insertions(+), 113 deletions(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
index a169662..e82b579 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
@@ -37,7 +37,7 @@ import org.apache.flink.state.api.input.BroadcastStateInputFormat;
import org.apache.flink.state.api.input.KeyedStateInputFormat;
import org.apache.flink.state.api.input.ListStateInputFormat;
import org.apache.flink.state.api.input.UnionStateInputFormat;
-import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
import org.apache.flink.util.Preconditions;
import java.io.IOException;
@@ -50,11 +50,11 @@ import java.io.IOException;
public class ExistingSavepoint extends WritableSavepoint<ExistingSavepoint> {
private final ExecutionEnvironment env;
- private final ModifiableSavepointMetadata metadata;
+ private final SavepointMetadata metadata;
private final StateBackend stateBackend;
- ExistingSavepoint(ExecutionEnvironment env, ModifiableSavepointMetadata metadata, StateBackend stateBackend) throws IOException {
+ ExistingSavepoint(ExecutionEnvironment env, SavepointMetadata metadata, StateBackend stateBackend) throws IOException {
super(metadata, stateBackend);
Preconditions.checkNotNull(env, "The execution environment must not be null");
Preconditions.checkNotNull(metadata, "The savepoint metadata must not be null");
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
index f42e886..dc371eb 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
@@ -20,14 +20,14 @@ package org.apache.flink.state.api;
import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.runtime.state.StateBackend;
-import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
/**
* A new savepoint.
*/
@PublicEvolving
public class NewSavepoint extends WritableSavepoint<NewSavepoint> {
- NewSavepoint(ModifiableSavepointMetadata metadata, StateBackend stateBackend) {
+ NewSavepoint(SavepointMetadata metadata, StateBackend stateBackend) {
super(metadata, stateBackend);
}
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
index 9754c5b..a90d810 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
@@ -23,7 +23,7 @@ import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.runtime.checkpoint.OperatorState;
import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.state.api.runtime.SavepointLoader;
-import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
import org.apache.flink.util.Preconditions;
import java.io.IOException;
@@ -59,7 +59,7 @@ public final class Savepoint {
.max(Comparator.naturalOrder())
.orElseThrow(() -> new RuntimeException("Savepoint's must contain at least one operator"));
- ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(maxParallelism, savepoint.getMasterStates(), savepoint.getOperatorStates());
+ SavepointMetadata metadata = new SavepointMetadata(maxParallelism, savepoint.getMasterStates(), savepoint.getOperatorStates());
return new ExistingSavepoint(env, metadata, stateBackend);
}
@@ -76,7 +76,7 @@ public final class Savepoint {
"Maximum parallelism must be between 1 and " + UPPER_BOUND_MAX_PARALLELISM
+ ". Found: " + maxParallelism);
- ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(maxParallelism, Collections.emptyList(), Collections.emptyList());
+ SavepointMetadata metadata = new SavepointMetadata(maxParallelism, Collections.emptyList(), Collections.emptyList());
return new NewSavepoint(metadata, stateBackend);
}
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
index b4a29e4..fb60779 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
@@ -25,7 +25,7 @@ import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.state.api.output.MergeOperatorStates;
import org.apache.flink.state.api.output.SavepointOutputFormat;
import org.apache.flink.state.api.runtime.BootstrapTransformationWithID;
-import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
import org.apache.flink.util.Preconditions;
import java.util.List;
@@ -38,11 +38,11 @@ import java.util.List;
@SuppressWarnings("WeakerAccess")
public abstract class WritableSavepoint<F extends WritableSavepoint> {
- protected final ModifiableSavepointMetadata metadata;
+ protected final SavepointMetadata metadata;
protected final StateBackend stateBackend;
- WritableSavepoint(ModifiableSavepointMetadata metadata, StateBackend stateBackend) {
+ WritableSavepoint(SavepointMetadata metadata, StateBackend stateBackend) {
Preconditions.checkNotNull(metadata, "The savepoint metadata must not be null");
Preconditions.checkNotNull(stateBackend, "The state backend must not be null");
this.metadata = metadata;
@@ -114,7 +114,7 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
.stream()
.map(newOperatorState -> newOperatorState
.getBootstrapTransformation()
- .writeOperatorState(newOperatorState.getOperatorID(), stateBackend, metadata.maxParallelism(), savepointWritePath))
+ .writeOperatorState(newOperatorState.getOperatorID(), stateBackend, metadata.getMaxParallelism(), savepointWritePath))
.reduce(DataSet::union)
.orElseThrow(() -> new IllegalStateException("Savepoint's must contain at least one operator"));
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
deleted file mode 100644
index eada085..0000000
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package org.apache.flink.state.api.runtime.metadata;
-
-import org.apache.flink.annotation.Internal;
-import org.apache.flink.runtime.checkpoint.MasterState;
-import org.apache.flink.runtime.checkpoint.OperatorState;
-import org.apache.flink.runtime.jobgraph.OperatorID;
-import org.apache.flink.state.api.BootstrapTransformation;
-import org.apache.flink.state.api.runtime.BootstrapTransformationWithID;
-import org.apache.flink.state.api.runtime.OperatorIDGenerator;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * Savepoint metadata that can be modified.
- */
-@Internal
-public class ModifiableSavepointMetadata extends SavepointMetadata {
-
- private final Map<OperatorID, OperatorStateSpec> operatorStateIndex;
-
- public ModifiableSavepointMetadata(int maxParallelism, Collection<MasterState> masterStates, Collection<OperatorState> initialStates) {
- super(maxParallelism, masterStates);
-
- this.operatorStateIndex = new HashMap<>(initialStates.size());
- initialStates.forEach(existingState -> operatorStateIndex.put(
- existingState.getOperatorID(),
- OperatorStateSpec.existing(existingState)));
- }
-
- /**
- * @return Operator state for the given UID.
- *
- * @throws IOException If the savepoint does not contain operator state with the given uid.
- */
- public OperatorState getOperatorState(String uid) throws IOException {
- OperatorID operatorID = OperatorIDGenerator.fromUid(uid);
-
- OperatorStateSpec operatorState = operatorStateIndex.get(operatorID);
- if (operatorState == null || operatorState.isNewStateTransformation()) {
- throw new IOException("Savepoint does not contain state with operator uid " + uid);
- }
-
- return operatorState.asExistingState();
- }
-
- public void removeOperator(String uid) {
- operatorStateIndex.remove(OperatorIDGenerator.fromUid(uid));
- }
-
- public void addOperator(String uid, BootstrapTransformation<?> transformation) {
- OperatorID id = OperatorIDGenerator.fromUid(uid);
-
- if (operatorStateIndex.containsKey(id)) {
- throw new IllegalArgumentException("The savepoint already contains uid " + uid + ". All uid's must be unique");
- }
-
- operatorStateIndex.put(id, OperatorStateSpec.newWithTransformation(new BootstrapTransformationWithID<>(id, transformation)));
- }
-
- /**
- * @return List of {@link OperatorState} that already exists within the savepoint.
- */
- public List<OperatorState> getExistingOperators() {
- return operatorStateIndex
- .values()
- .stream()
- .filter(OperatorStateSpec::isExistingState)
- .map(OperatorStateSpec::asExistingState)
- .collect(Collectors.toList());
- }
-
- /**
- * @return List of new operator states for the savepoint, represented by their target {@link OperatorID} and {@link BootstrapTransformation}.
- */
- public List<BootstrapTransformationWithID<?>> getNewOperators() {
- return operatorStateIndex
- .values()
- .stream()
- .filter(OperatorStateSpec::isNewStateTransformation)
- .map(OperatorStateSpec::asNewStateTransformation)
- .collect(Collectors.toList());
- }
-}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
index 91efa04..b8590f2 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/SavepointMetadata.java
@@ -20,11 +20,24 @@ package org.apache.flink.state.api.runtime.metadata;
import org.apache.flink.annotation.Internal;
import org.apache.flink.runtime.checkpoint.MasterState;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.state.api.BootstrapTransformation;
+import org.apache.flink.state.api.runtime.BootstrapTransformationWithID;
+import org.apache.flink.state.api.runtime.OperatorIDGenerator;
+import org.apache.flink.util.Preconditions;
+import java.io.IOException;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.runtime.state.KeyGroupRangeAssignment.UPPER_BOUND_MAX_PARALLELISM;
/**
- * Returns metadata about a savepoint.
+ * Savepoint metadata that can be modified.
*/
@Internal
public class SavepointMetadata {
@@ -33,22 +46,82 @@ public class SavepointMetadata {
private final Collection<MasterState> masterStates;
- public SavepointMetadata(int maxParallelism, Collection<MasterState> masterStates) {
+ private final Map<OperatorID, OperatorStateSpec> operatorStateIndex;
+
+ public SavepointMetadata(int maxParallelism, Collection<MasterState> masterStates, Collection<OperatorState> initialStates) {
+ Preconditions.checkArgument(maxParallelism > 0
+ && maxParallelism <= UPPER_BOUND_MAX_PARALLELISM,
+ "Maximum parallelism must be between 1 and " + UPPER_BOUND_MAX_PARALLELISM
+ + ". Found: " + maxParallelism);
this.maxParallelism = maxParallelism;
- this.masterStates = masterStates;
+
+ this.masterStates = Preconditions.checkNotNull(masterStates);
+
+ this.operatorStateIndex = new HashMap<>(initialStates.size());
+ initialStates.forEach(existingState -> operatorStateIndex.put(
+ existingState.getOperatorID(),
+ OperatorStateSpec.existing(existingState)));
+ }
+
+ public int getMaxParallelism() {
+ return maxParallelism;
+ }
+
+ public Collection<MasterState> getMasterStates() {
+ return masterStates;
}
/**
- * @return The max parallelism for the savepoint.
+ * @return Operator state for the given UID.
+ *
+ * @throws IOException If the savepoint does not contain operator state with the given uid.
*/
- public int maxParallelism() {
- return maxParallelism;
+ public OperatorState getOperatorState(String uid) throws IOException {
+ OperatorID operatorID = OperatorIDGenerator.fromUid(uid);
+
+ OperatorStateSpec operatorState = operatorStateIndex.get(operatorID);
+ if (operatorState == null || operatorState.isNewStateTransformation()) {
+ throw new IOException("Savepoint does not contain state with operator uid " + uid);
+ }
+
+ return operatorState.asExistingState();
+ }
+
+ public void removeOperator(String uid) {
+ operatorStateIndex.remove(OperatorIDGenerator.fromUid(uid));
+ }
+
+ public void addOperator(String uid, BootstrapTransformation<?> transformation) {
+ OperatorID id = OperatorIDGenerator.fromUid(uid);
+
+ if (operatorStateIndex.containsKey(id)) {
+ throw new IllegalArgumentException("The savepoint already contains uid " + uid + ". All uid's must be unique");
+ }
+
+ operatorStateIndex.put(id, OperatorStateSpec.newWithTransformation(new BootstrapTransformationWithID<>(id, transformation)));
}
/**
- * @return Masters states for the savepoint.
+ * @return List of {@link OperatorState} that already exists within the savepoint.
*/
- public Collection<MasterState> getMasterStates() {
- return masterStates;
+ public List<OperatorState> getExistingOperators() {
+ return operatorStateIndex
+ .values()
+ .stream()
+ .filter(OperatorStateSpec::isExistingState)
+ .map(OperatorStateSpec::asExistingState)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * @return List of new operator states for the savepoint, represented by their target {@link OperatorID} and {@link BootstrapTransformation}.
+ */
+ public List<BootstrapTransformationWithID<?>> getNewOperators() {
+ return operatorStateIndex
+ .values()
+ .stream()
+ .filter(OperatorStateSpec::isNewStateTransformation)
+ .map(OperatorStateSpec::asNewStateTransformation)
+ .collect(Collectors.toList());
}
}
diff --git a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java
index 7d34244..3d34840 100644
--- a/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java
+++ b/flink-libraries/flink-state-processing-api/src/test/java/org/apache/flink/state/api/SavepointTest.java
@@ -26,7 +26,7 @@ import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.runtime.state.memory.MemoryStateBackend;
import org.apache.flink.state.api.functions.StateBootstrapFunction;
import org.apache.flink.state.api.runtime.OperatorIDGenerator;
-import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
+import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
import org.junit.Test;
@@ -52,7 +52,7 @@ public class SavepointTest {
.bootstrapWith(input)
.transform(new ExampleStateBootstrapFunction());
- ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(1, Collections.emptyList(), Collections.emptyList());
+ SavepointMetadata metadata = new SavepointMetadata(1, Collections.emptyList(), Collections.emptyList());
new NewSavepoint(metadata, new MemoryStateBackend())
.withOperator(UID, transformation)
@@ -73,7 +73,7 @@ public class SavepointTest {
Collection<OperatorState> operatorStates = Collections.singletonList(new OperatorState(
OperatorIDGenerator.fromUid(UID), 1, 4));
- ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(4, Collections.emptyList(), operatorStates);
+ SavepointMetadata metadata = new SavepointMetadata(4, Collections.emptyList(), operatorStates);
new ExistingSavepoint(env, metadata, new MemoryStateBackend())
.withOperator(UID, transformation)
@@ -94,7 +94,7 @@ public class SavepointTest {
Collection<OperatorState> operatorStates = Collections.singletonList(new OperatorState(
OperatorIDGenerator.fromUid(UID), 1, 4));
- ModifiableSavepointMetadata metadata = new ModifiableSavepointMetadata(4, Collections.emptyList(), operatorStates);
+ SavepointMetadata metadata = new SavepointMetadata(4, Collections.emptyList(), operatorStates);
new ExistingSavepoint(env, metadata, new MemoryStateBackend())
.withOperator(UID, transformation)
[flink] 09/11: [FLINK-12693] [state-processor] Improve Javadocs for
user-facing APIs
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit c9815a7fe6d8a9facbc062457fb1ce525d6df427
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Thu Jul 4 12:27:38 2019 +0800
[FLINK-12693] [state-processor] Improve Javadocs for user-facing APIs
This commit improves Javadocs for the State Processor API that are
either outdated, or not sufficiently informative.
---
.../flink/state/api/BootstrapTransformation.java | 17 +++++++++++--
.../apache/flink/state/api/ExistingSavepoint.java | 19 ++++++++++++++-
.../state/api/KeyedOperatorTransformation.java | 5 ++++
.../org/apache/flink/state/api/NewSavepoint.java | 5 +++-
.../state/api/OneInputOperatorTransformation.java | 4 +++-
.../flink/state/api/OperatorTransformation.java | 28 +++++++++++++++++++++-
.../java/org/apache/flink/state/api/Savepoint.java | 6 +++--
.../state/api/SavepointWriterOperatorFactory.java | 13 +++++++++-
.../apache/flink/state/api/WritableSavepoint.java | 8 ++++++-
9 files changed, 95 insertions(+), 10 deletions(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
index c5ee559..a5163f3 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/BootstrapTransformation.java
@@ -46,22 +46,35 @@ import javax.annotation.Nullable;
import java.util.OptionalInt;
/**
- * Bootstrapped data that can be written into a {@code Savepoint}.
+ * A {@code BootstrapTransformation} represents a procedure of writing new operator state into a {@code Savepoint}.
+ * It is defined by a {@code DataSet} containing the data to bootstrap with, a factory for a stream operator
+ * that consumes the elements of the {@code DataSet} and generates state to be snapshotted, as well as an optional
+ * key selector if the new operator state is partitioned.
+ *
+ * @see OperatorTransformation
+ * @see OneInputOperatorTransformation
+ *
* @param <T> The input type of the transformation.
*/
@PublicEvolving
@SuppressWarnings("WeakerAccess")
public class BootstrapTransformation<T> {
+
+ /** The data set containing the data to bootstrap the operator state with. */
private final DataSet<T> dataSet;
+ /** Factory for the {@link StreamOperator} to consume and snapshot the bootstrapping data set. */
private final SavepointWriterOperatorFactory factory;
+ /** Partitioner for the bootstrapping data set. Only relevant if this bootstraps partitioned state. */
@Nullable
private final HashSelector<T> keySelector;
+ /** Type information for the key of the bootstrapped state. Only relevant if this bootstraps partitioned state. */
@Nullable
private final TypeInformation<?> keyType;
+ /** Local max parallelism for the bootstrapped operator. */
private final OptionalInt operatorMaxParallelism;
BootstrapTransformation(
@@ -134,7 +147,7 @@ public class BootstrapTransformation<T> {
config = new BoundedStreamConfig(keySerializer, keySelector);
}
- StreamOperator<TaggedOperatorSubtaskState> operator = factory.getOperator(
+ StreamOperator<TaggedOperatorSubtaskState> operator = factory.createOperator(
System.currentTimeMillis(),
savepointPath);
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
index e82b579..de6205e 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/ExistingSavepoint.java
@@ -43,15 +43,32 @@ import org.apache.flink.util.Preconditions;
import java.io.IOException;
/**
- * An existing savepoint.
+ * An existing savepoint. This class provides the entry points for reading previous
+ * existing operator states in savepoints. Operator states can be removed
+ * from and added to the set of existing operator states, and eventually, written to
+ * distributed storage as a new savepoint.
+ *
+ * <p>New savepoints written using this class are based on the previous existing savepoint.
+ * This means that for existing operators that remain untouched, the new savepoint only contains
+ * a shallow copy of pointers to state data that resides in the previous existing savepoint paths.
+ * This means that both savepoints share state and one cannot be deleted without corrupting the other!
+ *
+ * @see WritableSavepoint
*/
@PublicEvolving
@SuppressWarnings("WeakerAccess")
public class ExistingSavepoint extends WritableSavepoint<ExistingSavepoint> {
+
+ /** The batch execution environment. Used for creating inputs for reading state. */
private final ExecutionEnvironment env;
+ /** The savepoint metadata, which maintains the current set of existing / newly added operator states. */
private final SavepointMetadata metadata;
+ /**
+ * The state backend that was previously used to write existing operator states in this savepoint.
+ * This is also the state backend that will be used when writing again this existing savepoint.
+ */
private final StateBackend stateBackend;
ExistingSavepoint(ExecutionEnvironment env, SavepointMetadata metadata, StateBackend stateBackend) throws IOException {
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
index 49825cf..d2cde56 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
@@ -37,12 +37,17 @@ import java.util.OptionalInt;
@PublicEvolving
@SuppressWarnings("WeakerAccess")
public class KeyedOperatorTransformation<K, T> {
+
+ /** The data set containing the data to bootstrap the operator state with. */
private final DataSet<T> dataSet;
+ /** Local max parallelism for the bootstrapped operator. */
private final OptionalInt operatorMaxParallelism;
+ /** Partitioner for the bootstrapping data set. */
private final KeySelector<T, K> keySelector;
+ /** Type information for the key of the bootstrapped operator. */
private final TypeInformation<K> keyType;
KeyedOperatorTransformation(
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
index dc371eb..a5f8d8e 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/NewSavepoint.java
@@ -23,7 +23,10 @@ import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.state.api.runtime.metadata.SavepointMetadata;
/**
- * A new savepoint.
+ * A new savepoint. Operator states can be removed from and added to the savepoint, and eventually, written to
+ * distributed storage as a new savepoint.
+ *
+ * @see WritableSavepoint
*/
@PublicEvolving
public class NewSavepoint extends WritableSavepoint<NewSavepoint> {
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java
index 687eced..62ddd77 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OneInputOperatorTransformation.java
@@ -44,15 +44,17 @@ import java.util.OptionalInt;
@PublicEvolving
@SuppressWarnings("WeakerAccess")
public class OneInputOperatorTransformation<T> {
+
+ /** The data set containing the data to bootstrap the operator state with. */
private final DataSet<T> dataSet;
+ /** Local max parallelism for the bootstrapped operator. */
private OptionalInt operatorMaxParallelism = OptionalInt.empty();
OneInputOperatorTransformation(DataSet<T> dataSet) {
this.dataSet = dataSet;
}
-
/**
* Sets the maximum parallelism of this operator.
*
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
index 7bba046..983f94e 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
@@ -22,7 +22,33 @@ import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.java.DataSet;
/**
- * An OperatorTransformation represents a single operator within a {@link Savepoint}.
+ * This class provides the entry point for building {@link BootstrapTransformation}s,
+ * which represents procedures to bootstrap new operator states with a given {@code DataSet}.
+ *
+ * <h2>Example usage</h2>
+ *
+ * <pre>{@code
+ * DataSet<StateData> stateData = ...;
+ *
+ * // to bootstrap non-keyed state:
+ * BootstrapTransformation<StateData> nonKeyedStateBootstrap = OperatorTransformation
+ * .bootstrapWith(stateData)
+ * .transform(new StateBootstrapFunction<StateData>() {...})
+ *
+ * // to bootstrap keyed state:
+ * BootstrapTransformation<StateData> keyedStateBootstrap = OperatorTransformation
+ * .bootstrapWith(stateData)
+ * .keyBy(new KeySelector<StateData, KeyType>() {...})
+ * .transform(new KeyedStateBootstrapFunction<KeyType, StateData>() {...})
+ * }</pre>
+ *
+ * <p>The code example above demonstrates how to create {@code BootstrapTransformation}s for non-keyed and keyed
+ * state. The built bootstrap transformations can then be registered with your {@link ExistingSavepoint} or {@link Savepoint}
+ * prior to writing it.
+ *
+ * @see OneInputOperatorTransformation
+ * @see KeyedOperatorTransformation
+ * @see BootstrapTransformation
*/
@PublicEvolving
@SuppressWarnings("WeakerAccess")
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
index a90d810..96e4005 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/Savepoint.java
@@ -33,8 +33,10 @@ import java.util.Comparator;
import static org.apache.flink.runtime.state.KeyGroupRangeAssignment.UPPER_BOUND_MAX_PARALLELISM;
/**
- * A {@link Savepoint} is a collection of operator states that can be used to supply initial state
- * when starting a {@link org.apache.flink.streaming.api.datastream.DataStream} job.
+ * This class provides entry points for loading an existing savepoint, or a new empty savepoint.
+ *
+ * @see ExistingSavepoint
+ * @see NewSavepoint
*/
@PublicEvolving
public final class Savepoint {
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/SavepointWriterOperatorFactory.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/SavepointWriterOperatorFactory.java
index a2b3a60..fc78168 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/SavepointWriterOperatorFactory.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/SavepointWriterOperatorFactory.java
@@ -18,6 +18,7 @@
package org.apache.flink.state.api;
+import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.core.fs.Path;
import org.apache.flink.state.api.output.TaggedOperatorSubtaskState;
import org.apache.flink.streaming.api.operators.StreamOperator;
@@ -25,7 +26,17 @@ import org.apache.flink.streaming.api.operators.StreamOperator;
/**
* Creates a savepoint writing operator from a savepoint path.
*/
+@PublicEvolving
@FunctionalInterface
public interface SavepointWriterOperatorFactory {
- StreamOperator<TaggedOperatorSubtaskState> getOperator(long timestamp, Path savepointPath);
+
+ /**
+ * Creates a {@link StreamOperator} to be used for generating and snapshotting state.
+ *
+ * @param savepointTimestamp the timestamp to associate with the generated savepoint.
+ * @param savepointPath the path to write the savepoint to.
+ *
+ * @return a stream operator for writing the savepoint.
+ */
+ StreamOperator<TaggedOperatorSubtaskState> createOperator(long savepointTimestamp, Path savepointPath);
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
index fb60779..cd24e63 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
@@ -31,15 +31,21 @@ import org.apache.flink.util.Preconditions;
import java.util.List;
/**
- * Any savepoint that can be written to from a batch context.
+ * A {@code WritableSavepoint} is any savepoint that can be written to from a batch context.
+ * Internally, a {@link SavepointMetadata} object is maintained that keeps track of the set
+ * of existing operator states in the savepoint, as well as newly added operator states defined by their
+ * {@link BootstrapTransformation}.
+ *
* @param <F> The implementation type.
*/
@PublicEvolving
@SuppressWarnings("WeakerAccess")
public abstract class WritableSavepoint<F extends WritableSavepoint> {
+ /** The savepoint metadata, which maintains the current set of existing / newly added operator states. */
protected final SavepointMetadata metadata;
+ /** The state backend to use when writing this savepoint. */
protected final StateBackend stateBackend;
WritableSavepoint(SavepointMetadata metadata, StateBackend stateBackend) {
[flink] 03/11: [FLINK-12963] [docs] Document savepoint writer for
bootstrapping new savepoints
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 03bb097acf3594f1b445bd8a59acbbae2d34ad15
Author: Seth Wiesman <sj...@gmail.com>
AuthorDate: Mon Jun 24 16:11:46 2019 -0500
[FLINK-12963] [docs] Document savepoint writer for bootstrapping new savepoints
---
docs/dev/libs/state_processor_api.md | 151 +++++++++++++++++++--
..._processor_api.md => state_processor_api.zh.md} | 151 +++++++++++++++++++--
2 files changed, 280 insertions(+), 22 deletions(-)
diff --git a/docs/dev/libs/state_processor_api.md b/docs/dev/libs/state_processor_api.md
index ae8d059..676ac49 100644
--- a/docs/dev/libs/state_processor_api.md
+++ b/docs/dev/libs/state_processor_api.md
@@ -23,8 +23,8 @@ specific language governing permissions and limitations
under the License.
-->
-Apache Flink State Processor API provides powerful functionality to reading savepoints and checkpoints using Flink’s batch DataSet api.
-This is useful for analyzing state for interesting patterns and troubleshooting or auditing jobs by checking for discrepancies.
+Apache Flink's State Processor API provides powerful functionality to reading, writing, and modifing savepoints and checkpoints using Flink’s batch DataSet api.
+This is useful for tasks such as analyzing state for interesting patterns, troubleshooting or auditing jobs by checking for discrepancies, and bootstrapping state for new applications.
* This will be replaced by the TOC
{:toc}
@@ -70,8 +70,8 @@ class Summarize extends RichFlatMapFunction<Transaction, Summary> {
transient ValueState<Integer> countState;
public void open(Configuration configuration) throws Exception {
- totalState = getRuntimeContext().getState(new ValueStateDescriptor("total", Types.DOUBLE));
- countState = getRuntimeContext().getState(new ValueStateDescriptor("count", Types.INT));
+ totalState = getRuntimeContext().getState(new ValueStateDescriptor<>("total", Types.DOUBLE));
+ countState = getRuntimeContext().getState(new ValueStateDescriptor<>("count", Types.INT));
}
public void flatMap(Transaction value, Collector<Summary> out) throws Exception {
@@ -108,10 +108,12 @@ transactions
This job contains multiple operators along with various kinds of state.
When analyzing that state we can first scope data by its operator, named by setting its uid.
-Within each operator we can look at the registered states. CurrencyConverter has a broadcast state, which is a type of non-partitioned operator state.
-In general, there is no relationship between any two elements in an operator state and so we can look at each value as being its own row. Contrast this with Summarize, which contains two keyed states.
+Within each operator we can look at the registered states.
+`CurrencyConverter` has a broadcast state, which is a type of non-partitioned operator state.
+In general, there is no relationship between any two elements in an operator state and so we can look at each value as being its own row.
+Contrast this with Summarize, which contains two keyed states.
Because both states are scoped under the same key we can safely assume there exists some relationship between the two values.
-Therefore, keyed state is best understood as a single table per operator containing one “key” column along with n value columns, one for each registered state.
+Therefore, keyed state is best understood as a single table per operator containing one _key_ column along with _n_ value columns, one for each registered state.
All of this means that the state for this job could be described using the following pseudo-sql commands.
{% highlight sql %}
@@ -144,7 +146,7 @@ In general, the savepoint ↔ database relationship can be summarized as:
## Reading State
Reading state begins by specifiying the path to a valid savepoint or checkpoint along with the `StateBackend` that should be used to restore the data.
-The compatability guaruntees for restoring state are identical to those when restoring a `DataStream` application.
+The compatability guarantees for restoring state are identical to those when restoring a `DataStream` application.
<div class="codetabs" markdown="1">
<div data-lang="java" markdown="1">
@@ -253,7 +255,7 @@ public class StatefulFunctionWithTime extends KeyedProcessFunction<Integer, Inte
{% highlight scala %}
public class StatefulFunctionWithTime extends KeyedProcessFunction[Integer, Integer, Void] {
- @transient var state: ValueState[Integer];
+ var state: ValueState[Integer];
@throws[Exception]
override def open(Configuration parameters) {
@@ -308,7 +310,7 @@ DataSet<KeyedState> keyedState = savepoint.readKeyedState("my-uid", new ReaderFu
case class KeyedState(key: Int, value: Int)
class ReaderFunction extends KeyedStateReaderFunction[Integer, KeyedState] {
- @transient var state ValueState[Integer];
+ var state ValueState[Integer];
override def open(Configuration parameters) {
state = getRuntimeContext().getState(stateDescriptor);
@@ -327,4 +329,131 @@ class ReaderFunction extends KeyedStateReaderFunction[Integer, KeyedState] {
val keyedState = savepoint.readKeyedState("my-uid", new ReaderFunction());
{% endhighlight %}
</div>
-</div>
\ No newline at end of file
+</div>
+
+{% panel **Note:** When using a `KeyedStateReaderFunction` all state descriptors must be registered eagerly inside of open. Any attempt to call `RuntimeContext#getState`, `RuntimeContext#getListState`, or `RuntimeContext#getMapState` will result in a `RuntimeException`. %}
+
+## Writing New Savepoints
+
+State writers are based around the abstraction of `Savepoint`, where one `Savepoint` may have many operators and the state for any particular operator is created using a `BootstrapTransformation`.
+
+A `BootstrapTransformation` starts with a `DataSet` containing the values that are to be written into state.
+The transformation may be optionally `keyed` depending on whether or not you are writing keyed or operator state.
+Finally a bootstrap function is applied depending to the transformation; Flink supplies `KeyedStateBootstrapFunction` for writing keyed state, `StateBootstrapFunction` for writing non keyed state, and `BroadcastStateBootstrapFunction` for writing broadcast state.
+
+<div class="codetabs" markdown="1">
+<div data-lang="java" markdown="1">
+{% highlight java %}
+public class Account {
+ public int id;
+
+ public double amount;
+
+ public long timestamp;
+}
+
+public class AccountBootstrapper extends KeyedStateBootstrapFunction<Integer, Account> {
+ ValueState<Double> state;
+
+ @Override
+ public void open(Configuration parameters) {
+ ValueStateDescriptor<Double> descriptor = new ValueStateDescriptor<>("total",Types.DOUBLE);
+ state = getRuntimeContext().getState(descriptor);
+ }
+
+ @Override
+ public void processElement(Account value, Context ctx) throws Exception {
+ state.update(value.amount);
+ }
+}
+
+ExecutionEnvironment bEnv = ExecutionEnvironment.getExecutionEnvironment();
+
+DataSet<Account> accountDataSet = bEnv.fromCollection(accounts);
+
+BootstrapTransformation<Account> transformation = OperatorTransformation
+ .bootstrapWith(accountDataSet)
+ .keyBy(acc -> acc.id)
+ .transform(new AccountBootstrapper());
+{% endhighlight %}
+</div>
+<div data-lang="scala" markdown="1">
+{% highlight scala %}
+case class Account(id: Int, amount: Double, timestamp: Long)
+
+class AccountBootstrapper extends KeyedStateBootstrapFunction[Integer, Account] {
+ var state: ValueState[Double]
+
+ override def open(parameters: Configuration): Unit = {
+ val descriptor = new ValueStateDescriptor[Double]("total",Types.DOUBLE)
+ state = getRuntimeContext().getState(descriptor)
+ }
+
+ @throws[Exception]
+ override def processElement(value: Account, ctx: Context): Unit = {
+ state.update(value.amount)
+ }
+}
+
+val bEnv = ExecutionEnvironment.getExecutionEnvironment()
+
+val accountDataSet = bEnv.fromCollection(accounts)
+
+val transformation = OperatorTransformation
+ .bootstrapWith(accountDataSet)
+ .keyBy(acc -> acc.id)
+ .transform(new AccountBootstrapper())
+{% endhighlight %}
+</div>
+</div>
+
+The 'KeyedStateBootstrapFunction` supports setting event time and processing time timers.
+The timers will not fire insde the bootstrap function and only become active once restored within a `DataStream` application.
+If a processing time timer is set but the state is not restored until after that time has passed, the timer will fire immediatly upon start.
+
+Once one or more transformations have been created they may be combined into a single `Savepoint`.
+`Savepoint`'s are created using a state backend and max parallelism, they may contain any number of operators.
+
+<div class="codetabs" markdown="1">
+<div data-lang="java" markdown="1">
+{% highlight java %}
+Savepoint
+ .create(backend, 128)
+ .withOperator("uid1", transformation1)
+ .withOperator("uid2", transformation2)
+ .write(savepointPath);
+{% endhighlight %}
+</div>
+<div data-lang="scala" markdown="1">
+{% highlight scala %}
+Savepoint
+ .create(backend, 128)
+ .withOperator("uid1", transformation1)
+ .withOperator("uid2", transformation2)
+ .write(savepointPath)
+{% endhighlight %}
+</div>
+</div>
+
+Besides creating a savepoint from scratch, you can base on off an existing savepoint such as when bootstrapping a single new operator for an existing job.
+
+<div class="codetabs" markdown="1">
+<div data-lang="java" markdown="1">
+{% highlight java %}
+Savepoint
+ .load(backend, oldPath)
+ .withOperator("uid", transformation)
+ .write(newPath);
+{% endhighlight %}
+</div>
+<div data-lang="scala" markdown="1">
+{% highlight scala %}
+Savepoint
+ .load(backend, oldPath)
+ .withOperator("uid", transformation)
+ .write(newPath)
+{% endhighlight %}
+</div>
+</div>
+
+{% panel **Note:** When basing a new savepoint on existing state, the state processor api makes a shallow copy of the pointers to the existing operators. This means that both savepoints share state and one cannot be deleted without corrupting the other! %}
diff --git a/docs/dev/libs/state_processor_api.md b/docs/dev/libs/state_processor_api.zh.md
similarity index 63%
copy from docs/dev/libs/state_processor_api.md
copy to docs/dev/libs/state_processor_api.zh.md
index ae8d059..676ac49 100644
--- a/docs/dev/libs/state_processor_api.md
+++ b/docs/dev/libs/state_processor_api.zh.md
@@ -23,8 +23,8 @@ specific language governing permissions and limitations
under the License.
-->
-Apache Flink State Processor API provides powerful functionality to reading savepoints and checkpoints using Flink’s batch DataSet api.
-This is useful for analyzing state for interesting patterns and troubleshooting or auditing jobs by checking for discrepancies.
+Apache Flink's State Processor API provides powerful functionality to reading, writing, and modifing savepoints and checkpoints using Flink’s batch DataSet api.
+This is useful for tasks such as analyzing state for interesting patterns, troubleshooting or auditing jobs by checking for discrepancies, and bootstrapping state for new applications.
* This will be replaced by the TOC
{:toc}
@@ -70,8 +70,8 @@ class Summarize extends RichFlatMapFunction<Transaction, Summary> {
transient ValueState<Integer> countState;
public void open(Configuration configuration) throws Exception {
- totalState = getRuntimeContext().getState(new ValueStateDescriptor("total", Types.DOUBLE));
- countState = getRuntimeContext().getState(new ValueStateDescriptor("count", Types.INT));
+ totalState = getRuntimeContext().getState(new ValueStateDescriptor<>("total", Types.DOUBLE));
+ countState = getRuntimeContext().getState(new ValueStateDescriptor<>("count", Types.INT));
}
public void flatMap(Transaction value, Collector<Summary> out) throws Exception {
@@ -108,10 +108,12 @@ transactions
This job contains multiple operators along with various kinds of state.
When analyzing that state we can first scope data by its operator, named by setting its uid.
-Within each operator we can look at the registered states. CurrencyConverter has a broadcast state, which is a type of non-partitioned operator state.
-In general, there is no relationship between any two elements in an operator state and so we can look at each value as being its own row. Contrast this with Summarize, which contains two keyed states.
+Within each operator we can look at the registered states.
+`CurrencyConverter` has a broadcast state, which is a type of non-partitioned operator state.
+In general, there is no relationship between any two elements in an operator state and so we can look at each value as being its own row.
+Contrast this with Summarize, which contains two keyed states.
Because both states are scoped under the same key we can safely assume there exists some relationship between the two values.
-Therefore, keyed state is best understood as a single table per operator containing one “key” column along with n value columns, one for each registered state.
+Therefore, keyed state is best understood as a single table per operator containing one _key_ column along with _n_ value columns, one for each registered state.
All of this means that the state for this job could be described using the following pseudo-sql commands.
{% highlight sql %}
@@ -144,7 +146,7 @@ In general, the savepoint ↔ database relationship can be summarized as:
## Reading State
Reading state begins by specifiying the path to a valid savepoint or checkpoint along with the `StateBackend` that should be used to restore the data.
-The compatability guaruntees for restoring state are identical to those when restoring a `DataStream` application.
+The compatability guarantees for restoring state are identical to those when restoring a `DataStream` application.
<div class="codetabs" markdown="1">
<div data-lang="java" markdown="1">
@@ -253,7 +255,7 @@ public class StatefulFunctionWithTime extends KeyedProcessFunction<Integer, Inte
{% highlight scala %}
public class StatefulFunctionWithTime extends KeyedProcessFunction[Integer, Integer, Void] {
- @transient var state: ValueState[Integer];
+ var state: ValueState[Integer];
@throws[Exception]
override def open(Configuration parameters) {
@@ -308,7 +310,7 @@ DataSet<KeyedState> keyedState = savepoint.readKeyedState("my-uid", new ReaderFu
case class KeyedState(key: Int, value: Int)
class ReaderFunction extends KeyedStateReaderFunction[Integer, KeyedState] {
- @transient var state ValueState[Integer];
+ var state ValueState[Integer];
override def open(Configuration parameters) {
state = getRuntimeContext().getState(stateDescriptor);
@@ -327,4 +329,131 @@ class ReaderFunction extends KeyedStateReaderFunction[Integer, KeyedState] {
val keyedState = savepoint.readKeyedState("my-uid", new ReaderFunction());
{% endhighlight %}
</div>
-</div>
\ No newline at end of file
+</div>
+
+{% panel **Note:** When using a `KeyedStateReaderFunction` all state descriptors must be registered eagerly inside of open. Any attempt to call `RuntimeContext#getState`, `RuntimeContext#getListState`, or `RuntimeContext#getMapState` will result in a `RuntimeException`. %}
+
+## Writing New Savepoints
+
+State writers are based around the abstraction of `Savepoint`, where one `Savepoint` may have many operators and the state for any particular operator is created using a `BootstrapTransformation`.
+
+A `BootstrapTransformation` starts with a `DataSet` containing the values that are to be written into state.
+The transformation may be optionally `keyed` depending on whether or not you are writing keyed or operator state.
+Finally a bootstrap function is applied depending to the transformation; Flink supplies `KeyedStateBootstrapFunction` for writing keyed state, `StateBootstrapFunction` for writing non keyed state, and `BroadcastStateBootstrapFunction` for writing broadcast state.
+
+<div class="codetabs" markdown="1">
+<div data-lang="java" markdown="1">
+{% highlight java %}
+public class Account {
+ public int id;
+
+ public double amount;
+
+ public long timestamp;
+}
+
+public class AccountBootstrapper extends KeyedStateBootstrapFunction<Integer, Account> {
+ ValueState<Double> state;
+
+ @Override
+ public void open(Configuration parameters) {
+ ValueStateDescriptor<Double> descriptor = new ValueStateDescriptor<>("total",Types.DOUBLE);
+ state = getRuntimeContext().getState(descriptor);
+ }
+
+ @Override
+ public void processElement(Account value, Context ctx) throws Exception {
+ state.update(value.amount);
+ }
+}
+
+ExecutionEnvironment bEnv = ExecutionEnvironment.getExecutionEnvironment();
+
+DataSet<Account> accountDataSet = bEnv.fromCollection(accounts);
+
+BootstrapTransformation<Account> transformation = OperatorTransformation
+ .bootstrapWith(accountDataSet)
+ .keyBy(acc -> acc.id)
+ .transform(new AccountBootstrapper());
+{% endhighlight %}
+</div>
+<div data-lang="scala" markdown="1">
+{% highlight scala %}
+case class Account(id: Int, amount: Double, timestamp: Long)
+
+class AccountBootstrapper extends KeyedStateBootstrapFunction[Integer, Account] {
+ var state: ValueState[Double]
+
+ override def open(parameters: Configuration): Unit = {
+ val descriptor = new ValueStateDescriptor[Double]("total",Types.DOUBLE)
+ state = getRuntimeContext().getState(descriptor)
+ }
+
+ @throws[Exception]
+ override def processElement(value: Account, ctx: Context): Unit = {
+ state.update(value.amount)
+ }
+}
+
+val bEnv = ExecutionEnvironment.getExecutionEnvironment()
+
+val accountDataSet = bEnv.fromCollection(accounts)
+
+val transformation = OperatorTransformation
+ .bootstrapWith(accountDataSet)
+ .keyBy(acc -> acc.id)
+ .transform(new AccountBootstrapper())
+{% endhighlight %}
+</div>
+</div>
+
+The 'KeyedStateBootstrapFunction` supports setting event time and processing time timers.
+The timers will not fire insde the bootstrap function and only become active once restored within a `DataStream` application.
+If a processing time timer is set but the state is not restored until after that time has passed, the timer will fire immediatly upon start.
+
+Once one or more transformations have been created they may be combined into a single `Savepoint`.
+`Savepoint`'s are created using a state backend and max parallelism, they may contain any number of operators.
+
+<div class="codetabs" markdown="1">
+<div data-lang="java" markdown="1">
+{% highlight java %}
+Savepoint
+ .create(backend, 128)
+ .withOperator("uid1", transformation1)
+ .withOperator("uid2", transformation2)
+ .write(savepointPath);
+{% endhighlight %}
+</div>
+<div data-lang="scala" markdown="1">
+{% highlight scala %}
+Savepoint
+ .create(backend, 128)
+ .withOperator("uid1", transformation1)
+ .withOperator("uid2", transformation2)
+ .write(savepointPath)
+{% endhighlight %}
+</div>
+</div>
+
+Besides creating a savepoint from scratch, you can base on off an existing savepoint such as when bootstrapping a single new operator for an existing job.
+
+<div class="codetabs" markdown="1">
+<div data-lang="java" markdown="1">
+{% highlight java %}
+Savepoint
+ .load(backend, oldPath)
+ .withOperator("uid", transformation)
+ .write(newPath);
+{% endhighlight %}
+</div>
+<div data-lang="scala" markdown="1">
+{% highlight scala %}
+Savepoint
+ .load(backend, oldPath)
+ .withOperator("uid", transformation)
+ .write(newPath)
+{% endhighlight %}
+</div>
+</div>
+
+{% panel **Note:** When basing a new savepoint on existing state, the state processor api makes a shallow copy of the pointers to the existing operators. This means that both savepoints share state and one cannot be deleted without corrupting the other! %}
[flink] 04/11: [FLINK-12963] [state-processor] Refactor operator
state access concerns into ModifiableSavepointMetadata
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 31e827e54bcf643dc0593227aa6e491061102ccd
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Wed Jul 3 13:05:37 2019 +0800
[FLINK-12963] [state-processor] Refactor operator state access concerns into ModifiableSavepointMetadata
---
.../apache/flink/state/api/WritableSavepoint.java | 39 +++++++++-------------
.../metadata/ModifiableSavepointMetadata.java | 29 ++++++++++++++--
2 files changed, 42 insertions(+), 26 deletions(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
index 300d1ae..0f68cce 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/WritableSavepoint.java
@@ -19,8 +19,10 @@ package org.apache.flink.state.api;
import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.jobgraph.OperatorID;
import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.state.api.output.MergeOperatorStates;
import org.apache.flink.state.api.output.SavepointOutputFormat;
@@ -28,7 +30,6 @@ import org.apache.flink.state.api.runtime.metadata.ModifiableSavepointMetadata;
import org.apache.flink.util.Preconditions;
import java.util.List;
-import java.util.stream.Collectors;
/**
* Any savepoint that can be written to from a batch context.
@@ -79,9 +80,10 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
public final void write(String path) {
final Path savepointPath = new Path(path);
- DataSet<OperatorState> newOperatorStates = getOperatorStates(savepointPath);
+ List<Tuple2<OperatorID, BootstrapTransformation<?>>> newOperatorTransformations = metadata.getNewOperatorTransformations();
+ DataSet<OperatorState> newOperatorStates = writeOperatorStates(newOperatorTransformations, savepointPath);
- List<OperatorState> existingOperators = getExistingOperatorStates();
+ List<OperatorState> existingOperators = metadata.getExistingOperators();
DataSet<OperatorState> finalOperatorStates = unionOperatorStates(newOperatorStates, existingOperators);
@@ -92,27 +94,6 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
.name(path);
}
- private List<OperatorState> getExistingOperatorStates() {
- return metadata
- .getOperatorStates()
- .entrySet()
- .stream()
- .filter(entry -> entry.getValue().isLeft())
- .map(entry -> entry.getValue().left())
- .collect(Collectors.toList());
- }
-
- private DataSet<OperatorState> getOperatorStates(Path savepointPath) {
- return metadata
- .getOperatorStates()
- .entrySet()
- .stream()
- .filter(entry -> entry.getValue().isRight())
- .map(entry -> entry.getValue().right().writeOperatorState(entry.getKey(), stateBackend, metadata, savepointPath))
- .reduce(DataSet::union)
- .orElseThrow(() -> new IllegalStateException("Savepoint's must contain at least one operator"));
- }
-
private DataSet<OperatorState> unionOperatorStates(DataSet<OperatorState> newOperatorStates, List<OperatorState> existingOperators) {
DataSet<OperatorState> finalOperatorStates;
if (existingOperators.isEmpty()) {
@@ -126,4 +107,14 @@ public abstract class WritableSavepoint<F extends WritableSavepoint> {
}
return finalOperatorStates;
}
+
+ private DataSet<OperatorState> writeOperatorStates(
+ List<Tuple2<OperatorID, BootstrapTransformation<?>>> newOperatorTransformations,
+ Path savepointWritePath) {
+ return newOperatorTransformations
+ .stream()
+ .map(transformation -> transformation.f1.writeOperatorState(transformation.f0, stateBackend, metadata, savepointWritePath))
+ .reduce(DataSet::union)
+ .orElseThrow(() -> new IllegalStateException("Savepoint's must contain at least one operator"));
+ }
}
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
index 158fa35..8df8723 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/metadata/ModifiableSavepointMetadata.java
@@ -1,6 +1,7 @@
package org.apache.flink.state.api.runtime.metadata;
import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.checkpoint.MasterState;
import org.apache.flink.runtime.checkpoint.OperatorState;
import org.apache.flink.runtime.jobgraph.OperatorID;
@@ -11,7 +12,10 @@ import org.apache.flink.types.Either;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
/**
* Savepoint metadata that can be modified.
@@ -61,7 +65,28 @@ public class ModifiableSavepointMetadata extends SavepointMetadata {
operatorStateIndex.put(id, Either.Right(transformation));
}
- public Map<OperatorID, Either<OperatorState, BootstrapTransformation<?>>> getOperatorStates() {
- return operatorStateIndex;
+ /**
+ * @return List of {@link OperatorState} that already exists within the savepoint.
+ */
+ public List<OperatorState> getExistingOperators() {
+ return operatorStateIndex
+ .values()
+ .stream()
+ .filter(Either::isLeft)
+ .map(Either::left)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * @return List of new operator states for the savepoint, represented by their target {@link OperatorID} and {@link BootstrapTransformation}.
+ */
+ public List<Tuple2<OperatorID, BootstrapTransformation<?>>> getNewOperatorTransformations() {
+ Stream<Tuple2<OperatorID, BootstrapTransformation<?>>> transformations = operatorStateIndex
+ .entrySet()
+ .stream()
+ .filter(entry -> entry.getValue().isRight())
+ .map(entry -> Tuple2.of(entry.getKey(), entry.getValue().right()));
+
+ return transformations.collect(Collectors.toList());
}
}
[flink] 10/11: [FLINK-12963] [state-processor] Allow providing user
defined SavepointWriterOperatorFactory when creating keyed
BootstrapTransformations
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 699c1f89d08dd09d10c98aa42a37ca2182025b36
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Thu Jul 4 12:29:17 2019 +0800
[FLINK-12963] [state-processor] Allow providing user defined SavepointWriterOperatorFactory when creating keyed BootstrapTransformations
This was exposed for non-partitioned BootstrapTransformations, but not
for keyed ones. This commit makes the functionality on-par for both
sides.
---
.../java/org/apache/flink/state/api/KeyedOperatorTransformation.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
index d2cde56..6e42f16 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/KeyedOperatorTransformation.java
@@ -84,7 +84,7 @@ public class KeyedOperatorTransformation<K, T> {
* @param factory A factory returning transformation logic type of the return stream
* @return An {@link BootstrapTransformation} that can be added to a {@link Savepoint}.
*/
- private BootstrapTransformation<T> transform(SavepointWriterOperatorFactory factory) {
+ public BootstrapTransformation<T> transform(SavepointWriterOperatorFactory factory) {
return new BootstrapTransformation<>(dataSet, operatorMaxParallelism, factory, keySelector, keyType);
}
}
[flink] 11/11: [FLINK-12963] [state-processor] Make
OperatorTransformation non-instantiable
Posted by tz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit a39ade1dcc3eda4b9f2362a2b440019d13c0e99b
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Thu Jul 4 12:33:04 2019 +0800
[FLINK-12963] [state-processor] Make OperatorTransformation non-instantiable
The OperatorTransformation class is a simple entry point for creating
BootstrapTransformations via the #bootstrapWith static factory method.
This commit solidifies this by making the class non-instantiable.
This closes #8861.
---
.../main/java/org/apache/flink/state/api/OperatorTransformation.java | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
index 983f94e..856103f 100644
--- a/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
+++ b/flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/OperatorTransformation.java
@@ -52,7 +52,9 @@ import org.apache.flink.api.java.DataSet;
*/
@PublicEvolving
@SuppressWarnings("WeakerAccess")
-public abstract class OperatorTransformation {
+public final class OperatorTransformation {
+
+ private OperatorTransformation() {}
/**
* Create a new {@link OperatorTransformation} from a {@link DataSet}.