/
StreamExecutionEnvironmentTest.java
432 lines (354 loc) · 16.6 KB
/
StreamExecutionEnvironmentTest.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* 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.streaming.api;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.typeutils.GenericTypeInfo;
import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.DiscardingSink;
import org.apache.flink.streaming.api.functions.source.FromElementsFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.source.StatefulSequenceSource;
import org.apache.flink.streaming.api.graph.StreamGraph;
import org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator;
import org.apache.flink.streaming.api.operators.StreamOperator;
import org.apache.flink.types.Row;
import org.apache.flink.util.Collector;
import org.apache.flink.util.SplittableIterator;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** Tests for {@link StreamExecutionEnvironment}. */
public class StreamExecutionEnvironmentTest {
@Test
public void fromElementsWithBaseTypeTest1() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.fromElements(ParentClass.class, new SubClass(1, "Java"), new ParentClass(1, "hello"));
}
@Test(expected = IllegalArgumentException.class)
public void fromElementsWithBaseTypeTest2() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.fromElements(SubClass.class, new SubClass(1, "Java"), new ParentClass(1, "hello"));
}
@Test
public void testFromElementsDeducedType() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<String> source = env.fromElements("a", "b");
FromElementsFunction<String> elementsFunction =
(FromElementsFunction<String>) getFunctionFromDataSource(source);
assertEquals(
BasicTypeInfo.STRING_TYPE_INFO.createSerializer(env.getConfig()),
elementsFunction.getSerializer());
}
@Test
public void testFromElementsPostConstructionType() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<String> source = env.fromElements("a", "b");
TypeInformation<String> customType = new GenericTypeInfo<>(String.class);
source.returns(customType);
FromElementsFunction<String> elementsFunction =
(FromElementsFunction<String>) getFunctionFromDataSource(source);
assertNotEquals(
BasicTypeInfo.STRING_TYPE_INFO.createSerializer(env.getConfig()),
elementsFunction.getSerializer());
assertEquals(
customType.createSerializer(env.getConfig()), elementsFunction.getSerializer());
}
@Test
@SuppressWarnings("unchecked")
public void testFromCollectionParallelism() {
try {
TypeInformation<Integer> typeInfo = BasicTypeInfo.INT_TYPE_INFO;
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<Integer> dataStream1 =
env.fromCollection(new DummySplittableIterator<Integer>(), typeInfo);
try {
dataStream1.setParallelism(4);
fail("should throw an exception");
} catch (IllegalArgumentException e) {
// expected
}
dataStream1.addSink(new DiscardingSink<Integer>());
DataStreamSource<Integer> dataStream2 =
env.fromParallelCollection(new DummySplittableIterator<Integer>(), typeInfo)
.setParallelism(4);
dataStream2.addSink(new DiscardingSink<Integer>());
final StreamGraph streamGraph = env.getStreamGraph();
streamGraph.getStreamingPlanAsJSON();
assertEquals(
"Parallelism of collection source must be 1.",
1,
streamGraph.getStreamNode(dataStream1.getId()).getParallelism());
assertEquals(
"Parallelism of parallel collection source must be 4.",
4,
streamGraph.getStreamNode(dataStream2.getId()).getParallelism());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
@Test
public void testSources() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
SourceFunction<Integer> srcFun =
new SourceFunction<Integer>() {
private static final long serialVersionUID = 1L;
@Override
public void run(SourceContext<Integer> ctx) throws Exception {}
@Override
public void cancel() {}
};
DataStreamSource<Integer> src1 = env.addSource(srcFun);
src1.addSink(new DiscardingSink<Integer>());
assertEquals(srcFun, getFunctionFromDataSource(src1));
List<Long> list = Arrays.asList(0L, 1L, 2L);
DataStreamSource<Long> src2 = env.generateSequence(0, 2);
assertTrue(getFunctionFromDataSource(src2) instanceof StatefulSequenceSource);
DataStreamSource<Long> src3 = env.fromElements(0L, 1L, 2L);
assertTrue(getFunctionFromDataSource(src3) instanceof FromElementsFunction);
DataStreamSource<Long> src4 = env.fromCollection(list);
assertTrue(getFunctionFromDataSource(src4) instanceof FromElementsFunction);
}
/** Verifies that the API method doesn't throw and creates a source of the expected type. */
@Test
public void testFromSequence() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<Long> src = env.fromSequence(0, 2);
assertEquals(BasicTypeInfo.LONG_TYPE_INFO, src.getType());
}
@Test
public void testParallelismBounds() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
SourceFunction<Integer> srcFun =
new SourceFunction<Integer>() {
private static final long serialVersionUID = 1L;
@Override
public void run(SourceContext<Integer> ctx) throws Exception {}
@Override
public void cancel() {}
};
SingleOutputStreamOperator<Object> operator =
env.addSource(srcFun)
.flatMap(
new FlatMapFunction<Integer, Object>() {
private static final long serialVersionUID = 1L;
@Override
public void flatMap(Integer value, Collector<Object> out)
throws Exception {}
});
// default value for max parallelism
Assert.assertEquals(-1, operator.getTransformation().getMaxParallelism());
// bounds for parallelism 1
try {
operator.setParallelism(0);
Assert.fail();
} catch (IllegalArgumentException expected) {
}
// bounds for parallelism 2
operator.setParallelism(1);
Assert.assertEquals(1, operator.getParallelism());
// bounds for parallelism 3
operator.setParallelism(1 << 15);
Assert.assertEquals(1 << 15, operator.getParallelism());
// default value after generating
env.getStreamGraph(StreamExecutionEnvironment.DEFAULT_JOB_NAME, false).getJobGraph();
Assert.assertEquals(-1, operator.getTransformation().getMaxParallelism());
// configured value after generating
env.setMaxParallelism(42);
env.getStreamGraph(StreamExecutionEnvironment.DEFAULT_JOB_NAME, false).getJobGraph();
Assert.assertEquals(42, operator.getTransformation().getMaxParallelism());
// bounds configured parallelism 1
try {
env.setMaxParallelism(0);
Assert.fail();
} catch (IllegalArgumentException expected) {
}
// bounds configured parallelism 2
try {
env.setMaxParallelism(1 + (1 << 15));
Assert.fail();
} catch (IllegalArgumentException expected) {
}
// bounds for max parallelism 1
try {
operator.setMaxParallelism(0);
Assert.fail();
} catch (IllegalArgumentException expected) {
}
// bounds for max parallelism 2
try {
operator.setMaxParallelism(1 + (1 << 15));
Assert.fail();
} catch (IllegalArgumentException expected) {
}
// bounds for max parallelism 3
operator.setMaxParallelism(1);
Assert.assertEquals(1, operator.getTransformation().getMaxParallelism());
// bounds for max parallelism 4
operator.setMaxParallelism(1 << 15);
Assert.assertEquals(1 << 15, operator.getTransformation().getMaxParallelism());
// override config
env.getStreamGraph(StreamExecutionEnvironment.DEFAULT_JOB_NAME, false).getJobGraph();
Assert.assertEquals(1 << 15, operator.getTransformation().getMaxParallelism());
}
@Test
public void testGetStreamGraph() {
try {
TypeInformation<Integer> typeInfo = BasicTypeInfo.INT_TYPE_INFO;
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<Integer> dataStream1 =
env.fromCollection(new DummySplittableIterator<Integer>(), typeInfo);
dataStream1.addSink(new DiscardingSink<Integer>());
assertEquals(2, env.getStreamGraph().getStreamNodes().size());
DataStreamSource<Integer> dataStream2 =
env.fromCollection(new DummySplittableIterator<Integer>(), typeInfo);
dataStream2.addSink(new DiscardingSink<Integer>());
assertEquals(2, env.getStreamGraph().getStreamNodes().size());
DataStreamSource<Integer> dataStream3 =
env.fromCollection(new DummySplittableIterator<Integer>(), typeInfo);
dataStream3.addSink(new DiscardingSink<Integer>());
// Does not clear the transformations.
env.getExecutionPlan();
DataStreamSource<Integer> dataStream4 =
env.fromCollection(new DummySplittableIterator<Integer>(), typeInfo);
dataStream4.addSink(new DiscardingSink<Integer>());
assertEquals(4, env.getStreamGraph("TestJob").getStreamNodes().size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
@Test
public void testDefaultJobName() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
testJobName(StreamExecutionEnvironment.DEFAULT_JOB_NAME, env);
}
@Test
public void testUserDefinedJobName() {
String jobName = "MyTestJob";
Configuration config = new Configuration();
config.set(PipelineOptions.NAME, jobName);
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(config);
testJobName(jobName, env);
}
@Test
public void testUserDefinedJobNameWithConfigure() {
String jobName = "MyTestJob";
Configuration config = new Configuration();
config.set(PipelineOptions.NAME, jobName);
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.configure(config, this.getClass().getClassLoader());
testJobName(jobName, env);
}
private void testJobName(String expectedJobName, StreamExecutionEnvironment env) {
env.fromElements(1, 2, 3).print();
StreamGraph streamGraph = env.getStreamGraph();
assertEquals(expectedJobName, streamGraph.getJobName());
}
@Test
public void testAddSourceWithUserDefinedTypeInfo() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<Row> source1 =
env.addSource(new RowSourceFunction(), Types.ROW(Types.STRING));
// the source type information should be the user defined type
assertEquals(Types.ROW(Types.STRING), source1.getType());
DataStreamSource<Row> source2 = env.addSource(new RowSourceFunction());
// the source type information should be derived from RowSourceFunction#getProducedType
assertEquals(new GenericTypeInfo<>(Row.class), source2.getType());
}
/////////////////////////////////////////////////////////////
// Utilities
/////////////////////////////////////////////////////////////
private static StreamOperator<?> getOperatorFromDataStream(DataStream<?> dataStream) {
StreamExecutionEnvironment env = dataStream.getExecutionEnvironment();
StreamGraph streamGraph = env.getStreamGraph();
return streamGraph.getStreamNode(dataStream.getId()).getOperator();
}
@SuppressWarnings("unchecked")
private static <T> SourceFunction<T> getFunctionFromDataSource(
DataStreamSource<T> dataStreamSource) {
dataStreamSource.addSink(new DiscardingSink<T>());
AbstractUdfStreamOperator<?, ?> operator =
(AbstractUdfStreamOperator<?, ?>) getOperatorFromDataStream(dataStreamSource);
return (SourceFunction<T>) operator.getUserFunction();
}
private static class DummySplittableIterator<T> extends SplittableIterator<T> {
private static final long serialVersionUID = 1312752876092210499L;
@SuppressWarnings("unchecked")
@Override
public Iterator<T>[] split(int numPartitions) {
return (Iterator<T>[]) new Iterator<?>[0];
}
@Override
public int getMaximumNumberOfSplits() {
return 0;
}
@Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
private static class ParentClass {
int num;
String string;
public ParentClass(int num, String string) {
this.num = num;
this.string = string;
}
}
private static class SubClass extends ParentClass {
public SubClass(int num, String string) {
super(num, string);
}
}
private static class RowSourceFunction
implements SourceFunction<Row>, ResultTypeQueryable<Row> {
private static final long serialVersionUID = 5216362688122691404L;
@Override
public TypeInformation<Row> getProducedType() {
return TypeInformation.of(Row.class);
}
@Override
public void run(SourceContext<Row> ctx) throws Exception {}
@Override
public void cancel() {}
}
}