forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SnapshotUtilsTest.java
156 lines (127 loc) · 5.43 KB
/
SnapshotUtilsTest.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* 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.MockCheckpointStorage;
import org.apache.flink.streaming.api.operators.OperatorSnapshotFutures;
import org.apache.flink.streaming.api.operators.StreamOperator;
import org.apache.flink.streaming.api.operators.StreamTaskStateInitializer;
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
import org.apache.flink.streaming.runtime.streamstatus.StreamStatus;
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 MockCheckpointStorage().createCheckpointStorage(new JobID());
Path path = new Path(folder.newFolder().getAbsolutePath());
SnapshotUtils.snapshot(operator, 0, 0L, true, false, 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(StreamTaskStateInitializer streamTaskStateManager)
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 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 notifyCheckpointAborted(long checkpointId) {}
@Override
public void setCurrentKey(Object key) {
ACTUAL_ORDER_TRACKING.add("setCurrentKey");
}
@Override
public Object getCurrentKey() {
ACTUAL_ORDER_TRACKING.add("getCurrentKey");
return null;
}
@Override
public void processStreamStatus(StreamStatus status) throws Exception {
ACTUAL_ORDER_TRACKING.add("processStreamStatus");
}
}
}