|
19 | 19 | package org.apache.flink.state.api; |
20 | 20 |
|
21 | 21 | import org.apache.flink.api.common.JobID; |
| 22 | +import org.apache.flink.api.common.eventtime.WatermarkStrategy; |
22 | 23 | import org.apache.flink.api.common.functions.OpenContext; |
23 | 24 | import org.apache.flink.api.common.state.ListState; |
24 | 25 | import org.apache.flink.api.common.state.ListStateDescriptor; |
25 | 26 | import org.apache.flink.api.common.state.MapStateDescriptor; |
26 | 27 | import org.apache.flink.api.common.time.Deadline; |
| 28 | +import org.apache.flink.api.connector.source.ReaderOutput; |
| 29 | +import org.apache.flink.api.connector.source.SourceReaderContext; |
| 30 | +import org.apache.flink.api.connector.source.SplitEnumerator; |
| 31 | +import org.apache.flink.api.connector.source.SplitEnumeratorContext; |
27 | 32 | import org.apache.flink.api.java.tuple.Tuple2; |
28 | 33 | import org.apache.flink.client.program.ClusterClient; |
29 | 34 | import org.apache.flink.core.execution.SavepointFormatType; |
| 35 | +import org.apache.flink.core.io.InputStatus; |
30 | 36 | import org.apache.flink.runtime.jobgraph.JobGraph; |
31 | 37 | import org.apache.flink.runtime.state.FunctionInitializationContext; |
32 | 38 | import org.apache.flink.runtime.state.FunctionSnapshotContext; |
|
37 | 43 | import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
38 | 44 | import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction; |
39 | 45 | import org.apache.flink.streaming.api.functions.sink.v2.DiscardingSink; |
40 | | -import org.apache.flink.streaming.api.functions.source.legacy.SourceFunction; |
41 | 46 | import org.apache.flink.test.util.AbstractTestBaseJUnit4; |
| 47 | +import org.apache.flink.test.util.source.AbstractTestSource; |
| 48 | +import org.apache.flink.test.util.source.SingleSplitEnumerator; |
| 49 | +import org.apache.flink.test.util.source.TestSourceReader; |
| 50 | +import org.apache.flink.test.util.source.TestSplit; |
42 | 51 | import org.apache.flink.util.AbstractID; |
43 | 52 | import org.apache.flink.util.Collector; |
44 | 53 |
|
@@ -88,7 +97,12 @@ public void testOperatorStateInputFormat() throws Exception { |
88 | 97 | StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); |
89 | 98 | env.setParallelism(4); |
90 | 99 |
|
91 | | - DataStream<Integer> data = env.addSource(new SavepointSource()).rebalance(); |
| 100 | + DataStream<Integer> data = |
| 101 | + env.fromSource( |
| 102 | + new SavepointSource(), |
| 103 | + WatermarkStrategy.noWatermarks(), |
| 104 | + "SavepointSource") |
| 105 | + .rebalance(); |
92 | 106 |
|
93 | 107 | StatefulOperator statefulOperator = new StatefulOperator(list, union, broadcast); |
94 | 108 | data.connect(data.broadcast(broadcast)) |
@@ -209,35 +223,50 @@ private String takeSavepoint(JobGraph jobGraph) throws Exception { |
209 | 223 | } |
210 | 224 | } |
211 | 225 |
|
212 | | - private static class SavepointSource implements SourceFunction<Integer> { |
| 226 | + private static class SavepointSource extends AbstractTestSource<Integer> { |
213 | 227 | private static volatile boolean finished; |
214 | 228 |
|
215 | | - private volatile boolean running = true; |
216 | | - |
217 | 229 | private static final Integer[] elements = {1, 2, 3}; |
218 | 230 |
|
219 | 231 | @Override |
220 | | - public void run(SourceContext<Integer> ctx) { |
221 | | - synchronized (ctx.getCheckpointLock()) { |
222 | | - for (Integer element : elements) { |
223 | | - ctx.collect(element); |
224 | | - } |
| 232 | + public SplitEnumerator<TestSplit, Void> createEnumerator( |
| 233 | + SplitEnumeratorContext<TestSplit> enumContext) { |
| 234 | + return new SingleSplitEnumerator(enumContext); |
| 235 | + } |
225 | 236 |
|
226 | | - finished = true; |
227 | | - } |
| 237 | + @Override |
| 238 | + public TestSourceReader<Integer> createReader(SourceReaderContext readerContext) { |
| 239 | + return new TestSourceReader<>(readerContext) { |
| 240 | + private boolean receivedSplit = false; |
| 241 | + private CompletableFuture<Void> availability = new CompletableFuture<>(); |
| 242 | + |
| 243 | + @Override |
| 244 | + public void addSplits(List<TestSplit> splits) { |
| 245 | + if (!splits.isEmpty()) { |
| 246 | + receivedSplit = true; |
| 247 | + if (!availability.isDone()) { |
| 248 | + availability.complete(null); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
228 | 252 |
|
229 | | - while (running) { |
230 | | - try { |
231 | | - Thread.sleep(100); |
232 | | - } catch (InterruptedException e) { |
233 | | - // ignore |
| 253 | + @Override |
| 254 | + public InputStatus pollNext(ReaderOutput<Integer> output) { |
| 255 | + if (receivedSplit) { |
| 256 | + for (Integer element : elements) { |
| 257 | + output.collect(element); |
| 258 | + } |
| 259 | + finished = true; |
| 260 | + availability = new CompletableFuture<>(); |
| 261 | + } |
| 262 | + return InputStatus.NOTHING_AVAILABLE; |
234 | 263 | } |
235 | | - } |
236 | | - } |
237 | 264 |
|
238 | | - @Override |
239 | | - public void cancel() { |
240 | | - running = false; |
| 265 | + @Override |
| 266 | + public CompletableFuture<Void> isAvailable() { |
| 267 | + return availability; |
| 268 | + } |
| 269 | + }; |
241 | 270 | } |
242 | 271 |
|
243 | 272 | private static void initializeForTest() { |
|
0 commit comments