/
PipelineBuilderTest.scala
443 lines (345 loc) · 12.8 KB
/
PipelineBuilderTest.scala
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
433
434
435
436
437
438
439
440
441
442
443
/*
* 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.codefeedr.pipeline
import org.apache.flink.api.common.restartstrategy.RestartStrategies
import org.apache.flink.api.common.restartstrategy.RestartStrategies.RestartStrategyConfiguration
import org.apache.flink.streaming.api.functions.sink.SinkFunction
import org.apache.flink.streaming.api.scala.{
DataStream,
StreamExecutionEnvironment
}
import org.codefeedr.keymanager.StaticKeyManager
import org.codefeedr.buffer.{Buffer, BufferType}
import org.apache.flink.api.scala._
import org.apache.flink.runtime.state.filesystem.FsStateBackend
import org.apache.flink.runtime.state.memory.MemoryStateBackend
import org.apache.flink.streaming.api.{CheckpointingMode, TimeCharacteristic}
import org.codefeedr.buffer.serialization.Serializer
import org.codefeedr.stages.utilities.StringType
import org.codefeedr.stages.OutputStage
import org.codefeedr.testUtils.{
SimpleSinkStage,
SimpleSourceStage,
SimpleTransformStage,
SimpleTwoTransformStage
}
import org.scalatest.{BeforeAndAfter, FunSuite, Matchers}
class PipelineBuilderTest extends FunSuite with BeforeAndAfter with Matchers {
var builder: PipelineBuilder = _
before {
builder = new PipelineBuilder()
}
test("An Simple pipeline configuration throws") {
assertThrows[EmptyPipelineException] {
val pipeline = builder.build()
}
}
test("Set buffertype should also be retrievable") {
builder.setBufferType(BufferType.Kafka)
assert(builder.getBufferType == BufferType.Kafka)
}
test("Every pipeline object should appear in the pipeline (1)") {
val pipeline = builder
.append(new SimpleSourceStage())
.disablePipelineVerification()
.build()
assert(pipeline.graph.nodes.size == 1)
pipeline.graph.nodes.head shouldBe an[SimpleSourceStage]
}
test("Every pipeline object should appear in the pipeline (2)") {
val pipeline = builder
.append(new SimpleSourceStage())
.append(new SimpleTransformStage())
.build()
assert(pipeline.graph.nodes.size == 2)
pipeline.graph.nodes.head shouldBe an[SimpleSourceStage]
pipeline.graph.nodes.last shouldBe an[SimpleTransformStage]
}
test("Set properties should be available in stage properties") {
val stage = new SimpleSourceStage()
val stage2 = new SimpleTransformStage()
val pipeline = builder
.append(stage)
.disablePipelineVerification()
.setStageProperty(stage, "key", "value")
.setStageProperty(stage, "anotherKey", "true")
.build()
assert(pipeline.propertiesOf(stage).get("key").get == "value")
assert(pipeline.propertiesOf(stage2).get("key").isEmpty)
}
test(
"Set buffer properties should be available in pipeline buffer properties") {
val stage = new SimpleSourceStage()
val pipeline = builder
.append(stage)
.setBufferProperty("key", "value")
.build()
assert(pipeline.bufferProperties.get("key").get == "value")
}
test("A set keymanager should be forwarded to the pipeline") {
val km = new StaticKeyManager()
val pipeline = builder
.append(new SimpleSourceStage())
.setKeyManager(km)
.build()
assert(pipeline.keyManager == km)
}
test("A pipeline name should be set") {
val name = "Simple pipeline"
val pipeline = builder
.append(new SimpleSourceStage())
.setPipelineName(name)
.build()
assert(pipeline.name == name)
}
test("A DAG pipeline can't be appeneded to") {
builder.edge(new SimpleSourceStage(), new SimpleTransformStage())
assertThrows[IllegalStateException] {
builder.append(new SimpleSourceStage())
}
}
test("A sequential pipeline cannot switch to a DAG automatically") {
builder.append(new SimpleSourceStage())
assertThrows[IllegalStateException] {
builder.edge(new SimpleTransformStage(), new SimpleSinkStage())
}
}
test("A sequential pipeline can switch to a DAG manually") {
builder.append(new SimpleSourceStage())
builder.setPipelineType(PipelineType.DAG)
builder.edge(new SimpleTransformStage(), new SimpleSinkStage())
}
test("Default TimeCharacteristic is EventTime") {
val pipeline = builder.append(new SimpleSourceStage()).build()
assert(
pipeline.pipelineProperties.streamTimeCharacteristic == TimeCharacteristic.EventTime)
}
test("TimeCharacteristic can be overridden.") {
val pipeline = builder
.append(new SimpleSourceStage())
.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime)
.build()
assert(
pipeline.pipelineProperties.streamTimeCharacteristic == TimeCharacteristic.IngestionTime)
}
test("Default RestartStrategy is no restart.") {
val pipeline = builder.append(new SimpleSourceStage()).build()
assert(
pipeline.pipelineProperties.restartStrategy == RestartStrategies
.noRestart())
}
test("Default RestartStrategy can be overriden.") {
val pipeline = builder
.append(new SimpleSourceStage())
.setRestartStrategy(RestartStrategies.fallBackRestart())
.build()
assert(
pipeline.pipelineProperties.restartStrategy == RestartStrategies
.fallBackRestart())
}
test("Default StateBackend is memory") {
val pipeline = builder.append(new SimpleSourceStage()).build()
assert(
pipeline.pipelineProperties.stateBackend.isInstanceOf[MemoryStateBackend])
}
test("Default StateBackend can be overriden.") {
val pipeline = builder
.append(new SimpleSourceStage())
.setStateBackend(new FsStateBackend("file://test/test"))
.build()
assert(
pipeline.pipelineProperties.stateBackend.isInstanceOf[FsStateBackend])
}
test("Default checkpointing is disabled") {
val pipeline = builder.append(new SimpleSourceStage()).build()
assert(pipeline.pipelineProperties.checkpointing.isEmpty)
assert(
pipeline.pipelineProperties.checkpointingMode == CheckpointingMode.EXACTLY_ONCE)
}
test("Default checkpointing can be enabled.") {
val pipeline =
builder.append(new SimpleSourceStage()).enableCheckpointing(1000).build()
assert(pipeline.pipelineProperties.checkpointing.get == 1000)
}
test("Default checkpointing can be enabled and mode can be set.") {
val pipeline =
builder
.append(new SimpleSourceStage())
.enableCheckpointing(500, CheckpointingMode.AT_LEAST_ONCE)
.build()
assert(pipeline.pipelineProperties.checkpointing.get == 500)
assert(
pipeline.pipelineProperties.checkpointingMode == CheckpointingMode.AT_LEAST_ONCE)
}
test("Checkpointmode can be overriden.") {
val pipeline =
builder
.append(new SimpleSourceStage())
.setCheckpointingMode(CheckpointingMode.AT_LEAST_ONCE)
.build()
assert(
pipeline.pipelineProperties.checkpointingMode == CheckpointingMode.AT_LEAST_ONCE)
assert(pipeline.pipelineProperties.checkpointing.isEmpty)
}
test("A non-sequential pipeline cannot switch to a sequential pipeline") {
val a = new SimpleSourceStage()
val b = new SimpleTransformStage()
val c = new SimpleTransformStage()
builder.edge(a, b)
builder.edge(a, c)
assert(builder.getPipelineType == PipelineType.DAG)
assertThrows[IllegalStateException] {
builder.setPipelineType(PipelineType.Sequential)
}
}
test("Can't add edges to the DAG pipeline twice") {
val a = new SimpleSourceStage()
val b = new SimpleTransformStage()
builder.edge(a, b)
assertThrows[IllegalArgumentException] {
builder.edge(a, b)
}
}
test("Direct link from one stage to a list of others.") {
val a = new SimpleSourceStage()
val b = new SimpleTransformStage()
val c = new SimpleTransformStage()
builder.edge(a, List(b, c))
assert(builder.getPipelineType == PipelineType.DAG)
val pipeline = builder.build()
assert(pipeline.graph.getChildren(a).size == 2)
}
test("Direct link from list of stages to one stage.") {
val a = new SimpleSourceStage()
val b = new SimpleSourceStage()
val c = new SimpleTwoTransformStage()
builder.edge(List(a, b), c)
assert(builder.getPipelineType == PipelineType.DAG)
val pipeline = builder.build()
assert(pipeline.graph.getParents(c).size == 2)
}
test("Direct link from list of stages to other list of stages.") {
val a = new SimpleSourceStage()
val b = new SimpleSourceStage()
val c = new SimpleTwoTransformStage()
val d = new SimpleTwoTransformStage()
val e = new SimpleTwoTransformStage()
builder.edge(List(a, b), List(c, d, e))
assert(builder.getPipelineType == PipelineType.DAG)
val pipeline = builder.build()
assert(pipeline.graph.getParents(c).size == 2)
assert(pipeline.graph.getParents(d).size == 2)
assert(pipeline.graph.getParents(e).size == 2)
assert(pipeline.graph.getChildren(a).size == 3)
assert(pipeline.graph.getChildren(b).size == 3)
}
test("Appending after switching to seq") {
val a = new SimpleSourceStage()
val b = new SimpleTransformStage()
val c = new SimpleTransformStage()
builder.edge(a, b)
builder.setPipelineType(PipelineType.Sequential)
builder.append(c)
assert(builder.graph.isSequential)
}
test("Cannot append same object twice") {
val a = new SimpleSourceStage()
builder.append(a)
assertThrows[IllegalArgumentException] {
builder.append(a)
}
}
test("Append an anonymous input pipeline item") {
val pipeline = builder
.appendSource { x: StreamExecutionEnvironment =>
x.fromCollection(List(StringType("Test")))
}
.append { x: DataStream[StringType] =>
x.map(x => x)
}
.build()
assert(pipeline.graph.nodes.size == 2)
pipeline.graph.nodes.head shouldBe an[Stage[Nothing, StringType]]
pipeline.graph.nodes.last shouldBe an[Stage[StringType, StringType]]
}
test("Append an anonymous pipeline item") {
val pipeline = builder
.append(new SimpleSourceStage())
.append { x: DataStream[StringType] =>
x.map(x => x)
}
.build()
assert(pipeline.graph.nodes.size == 2)
pipeline.graph.nodes.head shouldBe an[SimpleSourceStage]
pipeline.graph.nodes.last shouldBe an[Stage[StringType, StringType]]
}
test("Append an anonymous pipeline job") {
val pipeline = builder
.append(new SimpleSourceStage())
.append { x: DataStream[StringType] =>
x.addSink(new SinkFunction[StringType] {})
}
.build()
assert(pipeline.graph.nodes.size == 2)
pipeline.graph.nodes.head shouldBe an[SimpleSourceStage]
pipeline.graph.nodes.last shouldBe an[OutputStage[StringType]]
}
test("Should add parents when using addParents") {
val a = new SimpleSourceStage()
val b = new SimpleTransformStage()
val c = new SimpleTransformStage()
val pipeline = builder
.disablePipelineVerification()
.addParents(c, a :+ b)
.build()
pipeline.graph.getParents(c)(0) shouldBe an[SimpleSourceStage]
pipeline.graph.getParents(c)(1) shouldBe an[SimpleTransformStage]
}
test("Using add parents when objects already exists only adds edges") {
val a = new SimpleSourceStage()
val b = new SimpleTransformStage()
val c = new SimpleTransformStage()
val pipeline = builder
.disablePipelineVerification()
.addParents(c, a :+ b)
.addParents(c, a)
.build()
pipeline.graph.getParents(c)(0) shouldBe an[SimpleSourceStage]
pipeline.graph.getParents(c)(1) shouldBe an[SimpleTransformStage]
assert(pipeline.graph.getParents(c).size == 2)
}
test("Set the serializer directly") {
val pipeline = builder
.append(new SimpleSourceStage())
.setSerializer(Serializer.JSON)
.build()
assert(pipeline.bufferProperties.get(Buffer.SERIALIZER).get == "JSON")
}
test("Next level") {
val a = new SimpleSourceStage(Some("testId"))
val b = new SimpleTransformStage()
val pipeline = builder
.append(a)
// .addStage(a)
// .usingId("AnId")
// .setProperty("foo", "bar")
// .setProperty("hello", "world")
.build()
assert(a.getContext.stageId == "testId")
}
}