-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathtest_bootstrapping.py
More file actions
560 lines (448 loc) · 16.1 KB
/
test_bootstrapping.py
File metadata and controls
560 lines (448 loc) · 16.1 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
from feldera.enums import BootstrapPolicy, PipelineStatus
from feldera.pipeline_builder import PipelineBuilder
from feldera.runtime_config import RuntimeConfig
from tests import TEST_CLIENT, enterprise_only
from .helper import (
gen_pipeline_name,
)
from feldera.testutils import (
FELDERA_TEST_NUM_WORKERS,
FELDERA_TEST_NUM_HOSTS,
single_host_only,
)
@enterprise_only
@gen_pipeline_name
def test_bootstrap_enterprise(pipeline_name):
"""
Enterprise: test backfill avoidance and bootstrapping.
"""
print("Creating baseline pipeline")
sql = """CREATE TABLE t1(x int) WITH ('materialized'='true');
CREATE MATERIALIZED VIEW v1 AS SELECT COUNT(*) AS c FROM t1;
"""
pipeline = PipelineBuilder(
TEST_CLIENT,
pipeline_name,
sql=sql,
runtime_config=RuntimeConfig(
workers=FELDERA_TEST_NUM_WORKERS,
hosts=FELDERA_TEST_NUM_HOSTS,
fault_tolerance_model=None, # We will make manual checkpoints in this test.
),
).create_or_replace()
pipeline.start()
pipeline.execute("INSERT INTO t1 VALUES (1), (2), (3);")
pipeline.checkpoint(True)
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 3}]
pipeline.stop(force=True)
print("Adding new view v2")
sql += """CREATE MATERIALIZED VIEW v2 AS SELECT COUNT(*) AS c FROM t1;
"""
pipeline.modify(sql=sql)
try:
pipeline.start(bootstrap_policy=BootstrapPolicy.REJECT)
# If we reach here, the pipeline started successfully when it should have failed
assert False, (
"Expected pipeline.start() to raise an exception with bootstrap_policy='reject', but it succeeded"
)
except Exception as e:
print(f"Expected exception caught: {e}")
# Reject triggers async stopping.
# This only guarantees deployment_status is Stopped
pipeline.wait_for_status(PipelineStatus.STOPPED, timeout=30)
pass
print(
"Starting pipeline with bootstrap_policy='allow' and dismissing deployment error from previous reject start"
)
pipeline.start(bootstrap_policy=BootstrapPolicy.ALLOW, dismiss_error=True)
assert pipeline.status() == PipelineStatus.RUNNING
pipeline.execute("INSERT INTO t1 VALUES (4), (5), (6);")
pipeline.wait_for_idle()
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 6}]
result = list(pipeline.query("SELECT * FROM v2;"))
assert result == [{"c": 6}]
pipeline.stop(force=True)
pipeline.wait_for_status(PipelineStatus.STOPPED, timeout=30)
# Since we didn't make a checkpoint during the previous run, the pipeline should be in the AWAITINGAPPROVAL state.
print("Starting pipeline with bootstrap_policy='await_approval'")
pipeline.start(bootstrap_policy=BootstrapPolicy.AWAIT_APPROVAL)
assert pipeline.status() == PipelineStatus.AWAITINGAPPROVAL
diff = pipeline.deployment_runtime_status_details()
print(f"Pipeline diff: {diff}")
assert diff["program_diff"] == {
"added_tables": [],
"added_views": ["v2"],
"modified_tables": [],
"modified_views": [],
"removed_tables": [],
"removed_views": [],
}
pipeline.approve()
# Wait for the pipeline to reach RUNNING status (up to 300 seconds)
pipeline.wait_for_status(PipelineStatus.RUNNING, timeout=300)
pipeline.execute("INSERT INTO t1 VALUES (4), (5), (6);")
pipeline.wait_for_idle()
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 6}]
result = list(pipeline.query("SELECT * FROM v2;"))
assert result == [{"c": 6}]
pipeline.checkpoint(True)
pipeline.stop(force=True)
print("Adding new table t2 and view v3")
original_sql = sql
sql_with_new_table = (
original_sql
+ """CREATE TABLE t2(y int) WITH ('materialized'='true');
CREATE MATERIALIZED VIEW v3 AS SELECT MAX(y) AS m FROM t2;
"""
)
pipeline.modify(sql=sql_with_new_table)
pipeline.start(bootstrap_policy=BootstrapPolicy.AWAIT_APPROVAL)
assert pipeline.status() == PipelineStatus.AWAITINGAPPROVAL
diff = pipeline.deployment_runtime_status_details()
print(f"Pipeline diff: {diff}")
assert diff["program_diff"] == {
"added_tables": ["t2"],
"added_views": ["v3"],
"modified_tables": [],
"modified_views": [],
"removed_tables": [],
"removed_views": [],
}
pipeline.approve()
# Wait for the pipeline to reach RUNNING status (up to 300 seconds)
pipeline.wait_for_status(PipelineStatus.RUNNING, timeout=300)
pipeline.execute("INSERT INTO t2 VALUES (10), (20), (30);")
pipeline.wait_for_idle()
result = list(pipeline.query("SELECT * FROM v3;"))
assert result == [{"m": 30}]
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 6}]
pipeline.checkpoint(True)
pipeline.stop(force=True)
print("Modify table t2 and view v3")
original_sql = sql
sql_with_new_table = (
original_sql
+ """CREATE TABLE t2(y int, s string) WITH ('materialized'='true');
CREATE MATERIALIZED VIEW v3 AS SELECT MAX(y) AS m FROM t2;
"""
)
pipeline.modify(sql=sql_with_new_table)
pipeline.start(bootstrap_policy=BootstrapPolicy.AWAIT_APPROVAL)
assert pipeline.status() == PipelineStatus.AWAITINGAPPROVAL
diff = pipeline.deployment_runtime_status_details()
print(f"Pipeline diff: {diff}")
assert diff["program_diff"] == {
"added_tables": [],
"added_views": [],
"modified_tables": ["t2"],
"modified_views": ["v3"],
"removed_tables": [],
"removed_views": [],
}
pipeline.approve()
# Wait for the pipeline to reach RUNNING status (up to 300 seconds)
pipeline.wait_for_status(PipelineStatus.RUNNING, timeout=300)
result = list(pipeline.query("SELECT * FROM v3;"))
assert result == [{"m": None}]
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 6}]
pipeline.execute("INSERT INTO t2 (y) VALUES (40), (50), (60);")
pipeline.wait_for_idle()
result = list(pipeline.query("SELECT * FROM v3;"))
assert result == [{"m": 60}]
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 6}]
pipeline.checkpoint(True)
pipeline.stop(force=True)
print("Delete view v3")
original_sql = sql
sql_with_new_table = (
original_sql
+ """CREATE TABLE t2(y int, s string) WITH ('materialized'='true');
"""
)
pipeline.modify(sql=sql_with_new_table)
pipeline.start(bootstrap_policy=BootstrapPolicy.AWAIT_APPROVAL)
assert pipeline.status() == PipelineStatus.AWAITINGAPPROVAL
diff = pipeline.deployment_runtime_status_details()
print(f"Pipeline diff: {diff}")
assert diff["program_diff"] == {
"added_tables": [],
"added_views": [],
"modified_tables": [],
"modified_views": [],
"removed_tables": [],
"removed_views": ["v3"],
}
pipeline.approve()
# Wait for the pipeline to reach RUNNING status (up to 300 seconds)
pipeline.wait_for_status(PipelineStatus.RUNNING, timeout=300)
pipeline.execute("INSERT INTO t2 (y) VALUES (70), (80), (90);")
pipeline.wait_for_idle()
# The table hasn't changed, so the previous 3 rows should still be there.
result = list(pipeline.query("SELECT count(*) as c FROM t2;"))
assert result == [{"c": 6}]
pipeline.checkpoint(True)
pipeline.stop(force=True)
print("Delete table t2")
original_sql = sql
sql_with_new_table = original_sql
pipeline.modify(sql=sql_with_new_table)
pipeline.start(bootstrap_policy=BootstrapPolicy.AWAIT_APPROVAL)
assert pipeline.status() == PipelineStatus.AWAITINGAPPROVAL
diff = pipeline.deployment_runtime_status_details()
print(f"Pipeline diff: {diff}")
assert diff["program_diff"] == {
"added_tables": [],
"added_views": [],
"modified_tables": [],
"modified_views": [],
"removed_views": [],
"removed_tables": ["t2"],
}
pipeline.approve()
# Wait for the pipeline to reach RUNNING status (up to 300 seconds)
pipeline.wait_for_status(PipelineStatus.RUNNING, timeout=300)
result = list(pipeline.query("SELECT count(*) as c FROM t1;"))
assert result == [{"c": 6}]
pipeline.checkpoint(True)
pipeline.stop(force=True)
@enterprise_only
@gen_pipeline_name
def test_bootstrap_non_materialized_table_enterprise(pipeline_name):
"""
Enterprise: bootstrapping non-materialized table that hasn't changed since the last
checkpoint, but there is a view that depends on it that has changed, should fail.
"""
sql = """CREATE TABLE t1(x int);
CREATE MATERIALIZED VIEW v1 AS SELECT COUNT(*) AS c FROM t1;
"""
pipeline = PipelineBuilder(
TEST_CLIENT,
pipeline_name,
sql=sql,
runtime_config=RuntimeConfig(
workers=FELDERA_TEST_NUM_WORKERS,
hosts=FELDERA_TEST_NUM_HOSTS,
fault_tolerance_model=None, # We will make manual checkpoints in this test.
),
).create_or_replace()
pipeline.start()
pipeline.execute("INSERT INTO t1 VALUES (1), (2), (3);")
pipeline.checkpoint(True)
pipeline.stop(force=True)
sql += """CREATE MATERIALIZED VIEW v2 AS SELECT MAX(x) AS m FROM t1;
"""
pipeline.modify(sql=sql)
try:
pipeline.start(bootstrap_policy=BootstrapPolicy.ALLOW)
assert False, (
"Expected pipeline.start() to raise an exception with bootstrap_policy='reject', but it succeeded"
)
except Exception as e:
print(f"Expected exception caught: {e}")
assert "not materialized" in str(e)
pass
@enterprise_only
@gen_pipeline_name
def test_bootstrap_table_lateness_enterprise(pipeline_name):
"""
Enterprise: bootstrapping a table with lateness must fail.
"""
sql = """CREATE TABLE t1(x int lateness 0) with ('materialized'='true');
CREATE MATERIALIZED VIEW v1 AS SELECT COUNT(*) AS c FROM t1;
"""
pipeline = PipelineBuilder(
TEST_CLIENT,
pipeline_name,
sql=sql,
runtime_config=RuntimeConfig(
workers=FELDERA_TEST_NUM_WORKERS,
hosts=FELDERA_TEST_NUM_HOSTS,
fault_tolerance_model=None, # We will make manual checkpoints in this test.
),
).create_or_replace()
pipeline.start()
pipeline.execute("INSERT INTO t1 VALUES (1), (2), (3);")
pipeline.checkpoint(True)
pipeline.stop(force=True)
sql += """CREATE MATERIALIZED VIEW v2 AS SELECT MAX(x) AS m FROM t1;
"""
pipeline.modify(sql=sql)
try:
pipeline.start(bootstrap_policy=BootstrapPolicy.ALLOW)
assert False, (
"Expected pipeline.start() to raise an exception for a program with lateness, but it succeeded"
)
except Exception as e:
print(f"Expected exception caught: {e}")
assert "lateness" in str(e)
pass
@enterprise_only
@gen_pipeline_name
def test_bootstrap_view_lateness_enterprise(pipeline_name):
"""
Enterprise: bootstrapping a view with lateness must fail.
"""
sql = """CREATE TABLE t1(x int) with ('materialized'='true');
CREATE MATERIALIZED VIEW v1 AS SELECT COUNT(*) AS c FROM t1;
LATENESS v1.c 0;
"""
pipeline = PipelineBuilder(
TEST_CLIENT,
pipeline_name,
sql=sql,
runtime_config=RuntimeConfig(
workers=FELDERA_TEST_NUM_WORKERS,
hosts=FELDERA_TEST_NUM_HOSTS,
fault_tolerance_model=None, # We will make manual checkpoints in this test.
),
).create_or_replace()
pipeline.start()
pipeline.execute("INSERT INTO t1 VALUES (1), (2), (3);")
pipeline.checkpoint(True)
pipeline.stop(force=True)
sql += """CREATE MATERIALIZED VIEW v2 AS SELECT MAX(x) AS m FROM t1;
"""
pipeline.modify(sql=sql)
try:
pipeline.start(bootstrap_policy=BootstrapPolicy.ALLOW)
assert False, (
"Expected pipeline.start() to raise an exception for a program with lateness, but it succeeded"
)
except Exception as e:
print(f"Expected exception caught: {e}")
assert "lateness" in str(e)
pass
# Add/remove connectors.
@enterprise_only
@single_host_only
@gen_pipeline_name
def test_bootstrap_connectors(pipeline_name):
"""
Enterprise: add/remove connectors should require user approval.
"""
# Start pipeline with no connectors, feed 3 rows, and checkpoint its state.
def gen_sql(connectors):
return f"""CREATE TABLE t1(x int)
with (
'materialized' = 'true',
'connectors' = '[{connectors}]'
);
CREATE MATERIALIZED VIEW v1 AS SELECT COUNT(*) AS c FROM t1;
"""
pipeline = PipelineBuilder(
TEST_CLIENT,
pipeline_name,
sql=gen_sql(""),
runtime_config=RuntimeConfig(
workers=FELDERA_TEST_NUM_WORKERS,
hosts=FELDERA_TEST_NUM_HOSTS,
fault_tolerance_model=None, # We will make manual checkpoints in this test.
),
).create_or_replace()
pipeline.start()
assert pipeline.status() == PipelineStatus.RUNNING
pipeline.execute("INSERT INTO t1 VALUES (1), (2), (3);")
pipeline.wait_for_idle()
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 3}]
pipeline.checkpoint(True)
pipeline.stop(force=True)
# Add two input connector. This should require user approval.
# The new connectors should start running from scratch and feed 10 rows each to the table.
pipeline.modify(
sql=gen_sql("""{
"transport": {
"name": "datagen",
"config": {
"plan": [{
"limit": 10
}]
}
}
},{
"transport": {
"name": "datagen",
"config": {
"plan": [{
"limit": 10
}]
}
}
}""")
)
pipeline.start(bootstrap_policy=BootstrapPolicy.AWAIT_APPROVAL)
assert pipeline.status() == PipelineStatus.AWAITINGAPPROVAL
diff = pipeline.deployment_runtime_status_details()
print(f"Pipeline diff: {diff}")
assert diff == {
"added_input_connectors": ["t1.unnamed-0", "t1.unnamed-1"],
"added_output_connectors": [],
"modified_input_connectors": [],
"modified_output_connectors": [],
"program_diff": {
"added_tables": [],
"added_views": [],
"modified_tables": [],
"modified_views": [],
"removed_tables": [],
"removed_views": [],
},
"program_diff_error": None,
"removed_input_connectors": [],
"removed_output_connectors": [],
}
pipeline.approve()
pipeline.wait_for_status(PipelineStatus.RUNNING, timeout=300)
pipeline.wait_for_completion(timeout_s=300)
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 23}]
pipeline.checkpoint(True)
pipeline.stop(force=True)
# Delete one connector.
pipeline.modify(
sql=gen_sql("""
{
"transport": {
"name": "datagen",
"config": {
"plan": [{
"limit": 10
}]
}
}
}""")
)
pipeline.start(bootstrap_policy=BootstrapPolicy.AWAIT_APPROVAL)
assert pipeline.status() == PipelineStatus.AWAITINGAPPROVAL
diff = pipeline.deployment_runtime_status_details()
print(f"Pipeline diff: {diff}")
assert diff == {
"added_input_connectors": [],
"added_output_connectors": [],
"modified_input_connectors": [],
"modified_output_connectors": [],
"program_diff": {
"added_tables": [],
"added_views": [],
"modified_tables": [],
"modified_views": [],
"removed_tables": [],
"removed_views": [],
},
"program_diff_error": None,
"removed_input_connectors": ["t1.unnamed-1"],
"removed_output_connectors": [],
}
pipeline.approve()
pipeline.wait_for_status(PipelineStatus.RUNNING, timeout=300)
pipeline.wait_for_completion(timeout_s=300)
result = list(pipeline.query("SELECT * FROM v1;"))
assert result == [{"c": 23}]
pipeline.checkpoint(True)
pipeline.stop(force=True)