/
test_sensor_failure_recovery.py
306 lines (263 loc) · 11.5 KB
/
test_sensor_failure_recovery.py
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
import multiprocessing
import pendulum
import pytest
from dagster.core.definitions.run_request import InstigatorType
from dagster.core.instance import DagsterInstance
from dagster.core.scheduler.instigation import InstigatorState, InstigatorStatus, TickStatus
from dagster.core.storage.pipeline_run import PipelineRunStatus
from dagster.core.storage.tags import RUN_KEY_TAG, SENSOR_NAME_TAG
from dagster.core.test_utils import (
SingleThreadPoolExecutor,
cleanup_test_instance,
create_test_daemon_workspace,
get_crash_signals,
get_logger_output_from_capfd,
wait_for_futures,
)
from dagster.daemon import get_default_daemon_logger
from dagster.daemon.sensor import execute_sensor_iteration
from dagster.seven import IS_WINDOWS
from dagster.seven.compat.pendulum import create_pendulum_time, to_timezone
from .test_sensor_run import (
instance_with_sensors,
wait_for_all_runs_to_start,
workspace_load_target,
)
spawn_ctx = multiprocessing.get_context("spawn")
def _test_launch_sensor_runs_in_subprocess(instance_ref, execution_datetime, debug_crash_flags):
with DagsterInstance.from_ref(instance_ref) as instance:
try:
with pendulum.test(execution_datetime), create_test_daemon_workspace(
workspace_load_target=workspace_load_target(),
instance=instance,
) as workspace:
logger = get_default_daemon_logger("SensorDaemon")
futures = {}
list(
execute_sensor_iteration(
instance,
logger,
workspace,
threadpool_executor=SingleThreadPoolExecutor(),
debug_crash_flags=debug_crash_flags,
debug_futures=futures,
)
)
wait_for_futures(futures)
finally:
cleanup_test_instance(instance)
@pytest.mark.skipif(
IS_WINDOWS, reason="Windows keeps resources open after termination in a flaky way"
)
@pytest.mark.parametrize("crash_location", ["TICK_CREATED", "TICK_HELD"])
@pytest.mark.parametrize("crash_signal", get_crash_signals())
def test_failure_before_run_created(crash_location, crash_signal, capfd):
frozen_datetime = to_timezone(
create_pendulum_time(year=2019, month=2, day=28, hour=0, minute=0, second=1, tz="UTC"),
"US/Central",
)
with instance_with_sensors() as (
instance,
_grpc_server_registry,
external_repo,
):
with pendulum.test(frozen_datetime):
external_sensor = external_repo.get_external_sensor("simple_sensor")
instance.add_instigator_state(
InstigatorState(
external_sensor.get_external_origin(),
InstigatorType.SENSOR,
InstigatorStatus.RUNNING,
)
)
# create a tick
launch_process = spawn_ctx.Process(
target=_test_launch_sensor_runs_in_subprocess,
args=[instance.get_ref(), frozen_datetime, None],
)
launch_process.start()
launch_process.join(timeout=60)
ticks = instance.get_ticks(
external_sensor.get_external_origin_id(), external_sensor.selector_id
)
assert len(ticks) == 1
assert ticks[0].status == TickStatus.SKIPPED
capfd.readouterr()
# create a starting tick, but crash
debug_crash_flags = {external_sensor.name: {crash_location: crash_signal}}
launch_process = spawn_ctx.Process(
target=_test_launch_sensor_runs_in_subprocess,
args=[instance.get_ref(), frozen_datetime.add(seconds=31), debug_crash_flags],
)
launch_process.start()
launch_process.join(timeout=60)
assert launch_process.exitcode != 0
capfd.readouterr()
ticks = instance.get_ticks(
external_sensor.get_external_origin_id(), external_sensor.selector_id
)
assert len(ticks) == 2
assert ticks[0].status == TickStatus.STARTED
assert not int(ticks[0].timestamp) % 2 # skip condition for simple_sensor
assert instance.get_runs_count() == 0
# create another tick, but ensure that the last evaluation time used is from the first,
# successful tick rather than the failed tick
launch_process = spawn_ctx.Process(
target=_test_launch_sensor_runs_in_subprocess,
args=[instance.get_ref(), frozen_datetime.add(seconds=62), None],
)
launch_process.start()
launch_process.join(timeout=60)
assert launch_process.exitcode == 0
wait_for_all_runs_to_start(instance)
assert instance.get_runs_count() == 1
run = instance.get_runs()[0]
assert (
get_logger_output_from_capfd(capfd, "dagster.daemon.SensorDaemon")
== f"""2019-02-27 18:01:03 -0600 - dagster.daemon.SensorDaemon - INFO - Checking for new runs for sensor: simple_sensor
2019-02-27 18:01:03 -0600 - dagster.daemon.SensorDaemon - INFO - Launching run for simple_sensor
2019-02-27 18:01:03 -0600 - dagster.daemon.SensorDaemon - INFO - Completed launch of run {run.run_id} for simple_sensor"""
)
ticks = instance.get_ticks(
external_sensor.get_external_origin_id(), external_sensor.selector_id
)
assert len(ticks) == 3
assert ticks[0].status == TickStatus.SUCCESS
@pytest.mark.skipif(
IS_WINDOWS, reason="Windows keeps resources open after termination in a flaky way"
)
@pytest.mark.parametrize("crash_location", ["RUN_CREATED"])
@pytest.mark.parametrize("crash_signal", get_crash_signals())
def test_failure_after_run_created_before_run_launched(crash_location, crash_signal, capfd):
frozen_datetime = to_timezone(
create_pendulum_time(year=2019, month=2, day=28, hour=0, minute=0, second=0, tz="UTC"),
"US/Central",
)
with instance_with_sensors() as (
instance,
_grpc_server_registry,
external_repo,
):
with pendulum.test(frozen_datetime):
external_sensor = external_repo.get_external_sensor("run_key_sensor")
instance.add_instigator_state(
InstigatorState(
external_sensor.get_external_origin(),
InstigatorType.SENSOR,
InstigatorStatus.RUNNING,
)
)
# create a starting tick, but crash
debug_crash_flags = {external_sensor.name: {crash_location: crash_signal}}
launch_process = spawn_ctx.Process(
target=_test_launch_sensor_runs_in_subprocess,
args=[instance.get_ref(), frozen_datetime, debug_crash_flags],
)
launch_process.start()
launch_process.join(timeout=60)
assert launch_process.exitcode != 0
ticks = instance.get_ticks(
external_sensor.get_external_origin_id(), external_sensor.selector_id
)
assert len(ticks) == 1
assert ticks[0].status == TickStatus.STARTED
assert instance.get_runs_count() == 1
run = instance.get_runs()[0]
# Run was created, but hasn't launched yet
assert run.status == PipelineRunStatus.NOT_STARTED
assert run.tags.get(SENSOR_NAME_TAG) == "run_key_sensor"
assert run.tags.get(RUN_KEY_TAG) == "only_once"
# clear output
capfd.readouterr()
launch_process = spawn_ctx.Process(
target=_test_launch_sensor_runs_in_subprocess,
args=[instance.get_ref(), frozen_datetime.add(seconds=1), None],
)
launch_process.start()
launch_process.join(timeout=60)
assert launch_process.exitcode == 0
wait_for_all_runs_to_start(instance)
assert instance.get_runs_count() == 1
run = instance.get_runs()[0]
captured = capfd.readouterr()
assert (
f"Run {run.run_id} already created with the run key `only_once` for run_key_sensor"
in captured.out
)
ticks = instance.get_ticks(
external_sensor.get_external_origin_id(), external_sensor.selector_id
)
assert len(ticks) == 2
assert ticks[0].status == TickStatus.SUCCESS
@pytest.mark.skipif(
IS_WINDOWS, reason="Windows keeps resources open after termination in a flaky way"
)
@pytest.mark.parametrize("crash_location", ["RUN_LAUNCHED"])
@pytest.mark.parametrize("crash_signal", get_crash_signals())
def test_failure_after_run_launched(crash_location, crash_signal, capfd):
frozen_datetime = to_timezone(
create_pendulum_time(
year=2019,
month=2,
day=28,
hour=0,
minute=0,
second=0,
tz="UTC",
),
"US/Central",
)
with instance_with_sensors() as (
instance,
_grpc_server_registry,
external_repo,
):
with pendulum.test(frozen_datetime):
external_sensor = external_repo.get_external_sensor("run_key_sensor")
instance.add_instigator_state(
InstigatorState(
external_sensor.get_external_origin(),
InstigatorType.SENSOR,
InstigatorStatus.RUNNING,
)
)
# create a run, launch but crash
debug_crash_flags = {external_sensor.name: {crash_location: crash_signal}}
launch_process = spawn_ctx.Process(
target=_test_launch_sensor_runs_in_subprocess,
args=[instance.get_ref(), frozen_datetime, debug_crash_flags],
)
launch_process.start()
launch_process.join(timeout=60)
assert launch_process.exitcode != 0
ticks = instance.get_ticks(
external_sensor.get_external_origin_id(), external_sensor.selector_id
)
assert len(ticks) == 1
assert ticks[0].status == TickStatus.STARTED
assert instance.get_runs_count() == 1
run = instance.get_runs()[0]
wait_for_all_runs_to_start(instance)
assert run.tags.get(SENSOR_NAME_TAG) == "run_key_sensor"
assert run.tags.get(RUN_KEY_TAG) == "only_once"
capfd.readouterr()
launch_process = spawn_ctx.Process(
target=_test_launch_sensor_runs_in_subprocess,
args=[instance.get_ref(), frozen_datetime.add(seconds=1), None],
)
launch_process.start()
launch_process.join(timeout=60)
assert launch_process.exitcode == 0
wait_for_all_runs_to_start(instance)
assert instance.get_runs_count() == 1
run = instance.get_runs()[0]
captured = capfd.readouterr()
assert (
'Skipping 1 run for sensor run_key_sensor already completed with run keys: ["only_once"]'
in captured.out
)
ticks = instance.get_ticks(
external_sensor.get_external_origin_id(), external_sensor.selector_id
)
assert len(ticks) == 2
assert ticks[0].status == TickStatus.SKIPPED