forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WritableSavepoint.java
104 lines (90 loc) · 3.63 KB
/
WritableSavepoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* 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.connectors.savepoint.api;
import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.connectors.savepoint.output.OperatorStateReducer;
import org.apache.flink.connectors.savepoint.output.OperatorSubtaskStateReducer;
import org.apache.flink.connectors.savepoint.output.SavepointOutputFormat;
import org.apache.flink.connectors.savepoint.output.metadata.SavepointMetadataProvider;
import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.checkpoint.OperatorState;
import org.apache.flink.runtime.state.StateBackend;
import javax.annotation.Nullable;
import java.util.Map;
/**
* 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> {
/**
* Drop an existing operator from the savepoint.
* @param uid The uid of the operator.
* @return A modified savepoint.
*/
public abstract F removeOperator(String uid);
/**
* 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.
*/
public abstract <T> F withOperator(String uid, BootstrapTransformation<T> transformation);
/**
* Write out a new or updated savepoint.
* @param path The path to where the savepoint should be written.
*/
public abstract void write(String path);
protected void write(
Path savepointPath,
Map<String, BootstrapTransformation> transformations,
StateBackend stateBackend,
SavepointMetadataProvider provider,
@Nullable DataSet<OperatorState> existingOperators) {
DataSet<OperatorState> newOperatorStates = transformations
.entrySet()
.stream()
.map(entry -> getOperatorStates(savepointPath, entry.getKey(), stateBackend, entry.getValue(), provider))
.reduce(DataSet::union)
.orElseThrow(() -> new IllegalStateException("Savepoint's must contain at least one operator"));
DataSet<OperatorState> finalOperatorStates;
if (existingOperators == null) {
finalOperatorStates = newOperatorStates;
} else {
finalOperatorStates = newOperatorStates.union(existingOperators);
}
finalOperatorStates
.reduceGroup(new OperatorStateReducer(provider))
.name("reduce(OperatorState)")
.output(new SavepointOutputFormat(savepointPath))
.name(savepointPath.toString());
}
@SuppressWarnings("unchecked")
private DataSet<OperatorState> getOperatorStates(
Path savepointPath,
String uid,
StateBackend stateBackend,
BootstrapTransformation operator,
SavepointMetadataProvider provider) {
return operator
.getOperatorSubtaskStates(uid, stateBackend, provider, savepointPath)
.reduceGroup(new OperatorSubtaskStateReducer(uid, provider))
.name("reduce(OperatorSubtaskState)");
}
}