Debian Codegraph @

tests/integration/test_reload.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """Tests for reload behaviour in the scheduler."""
18
19
20 from cylc.flow import (
21 commands,
22 flags,
23 )
24 from cylc.flow.cfgspec.glbl_cfg import glbl_cfg
25 from cylc.flow.data_store_mgr import TASK_PROXIES
26 from cylc.flow.platforms import get_platform
27 from cylc.flow.scheduler import Scheduler
28 from cylc.flow.task_state import (
29 TASK_STATUS_PREPARING,
30 TASK_STATUS_SUBMITTED,
31 TASK_STATUS_WAITING,
32 )
33
34
35 async def test_reload_waits_for_pending_tasks(
36 flow,
37 scheduler,
38 start,
39 monkeypatch,
40 log_scan,
41 ):
42 """Reload should flush out preparing tasks and pause the workflow.
43
44 Reloading a workflow with preparing tasks may be unsafe and is at least
45 confusing. For safety we should pause the workflow and flush out any
46 preparing tasks before attempting reload.
47
48 See https://github.com/cylc/cylc-flow/issues/5107
49 """
50 # speed up the test:
51 monkeypatch.setattr('cylc.flow.scheduler.sleep', lambda *_: None)
52 # a simple workflow with a single task
53 id_ = flow('foo')
54 schd: Scheduler = scheduler(id_, paused_start=False)
55
56 # we will artificially push the task through these states
57 state_seq = [
58 # repeat the preparing state a few times to simulate a task
59 # taking multiple main-loop cycles to submit
60 TASK_STATUS_PREPARING,
61 TASK_STATUS_PREPARING,
62 TASK_STATUS_PREPARING,
63 TASK_STATUS_SUBMITTED,
64 ]
65
66 # start the scheduler
67 async with start(schd) as log:
68 foo = schd.pool.get_tasks()[0]
69
70 # set the task to go through some state changes
71 def submit_task_jobs(*a, **k):
72 try:
73 foo.state_reset(state_seq.pop(0))
74 except IndexError:
75 foo.waiting_on_job_prep = False
76 return [foo]
77
78 monkeypatch.setattr(
79 schd.task_job_mgr, 'submit_task_jobs', submit_task_jobs
80 )
81
82 # the task should start as waiting
83 assert foo.state(TASK_STATUS_WAITING)
84
85 # put the task into the preparing state
86 schd.release_tasks_to_run()
87 assert foo.state(TASK_STATUS_PREPARING)
88
89 # reload the workflow
90 await commands.run_cmd(commands.reload_workflow(schd))
91
92 # the task should end in the submitted state
93 assert foo.state(TASK_STATUS_SUBMITTED)
94
95 # ensure the order of events was correct
96 log_scan(
97 log,
98 [
99 # the task should have entered the preparing state before the
100 # reload was requested
101 '[1/foo:waiting] => preparing',
102 # the reload should have put the workflow into the paused state
103 'Pausing the workflow: Reloading workflow',
104 # reload should have waited for the task to submit
105 '[1/foo/00:preparing] => submitted',
106 # before then reloading the workflow config
107 'Reloading the workflow definition.',
108 # post-reload the workflow should have been resumed
109 'RESUMING the workflow now',
110 ],
111 )
112
113
114 async def test_reload_waits_for_preparing_tasks_in_remote_init(
115 flow,
116 scheduler,
117 start,
118 monkeypatch,
119 ):
120 """Reload should wait for preparing tasks stuck in remote-init.
121
122 Test works by simulating a task taking longer than one reload flush
123 loop to complete remote-init, catching the situation where
124 release_tasks_to_run() returns False (because no tasks are ready
125 for submission) but the task is still waiting_on_job_prep or in the
126 preparing state. The reload flush loop should continue to iterate
127 until the task has actually submitted.
128
129 https://github.com/cylc/cylc-flow/issues/7296
130 """
131 # speed up the test
132 monkeypatch.setattr('cylc.flow.scheduler.sleep', lambda *_: None)
133
134 id_ = flow('foo')
135 schd: Scheduler = scheduler(id_, paused_start=False)
136
137 async with start(schd):
138 foo = schd.pool.get_tasks()[0]
139
140 # Track how many times submit_task_jobs is called to simulate
141 # a task going through remote-init (returns empty list) before
142 # eventually submitting.
143 call_count = 0
144
145 def submit_task_jobs(itasks, *a, **k):
146 nonlocal call_count
147 call_count += 1
148 if call_count <= 3:
149 # Simulate tasks stuck in remote-init: submit_task_jobs
150 # returns an empty list because remote-init hasn't completed
151 # yet, but the task is still waiting_on_job_prep.
152 # On the first call, set state to PREPARING (as the real
153 # prep_submit_task_jobs would do).
154 if not foo.state(TASK_STATUS_PREPARING):
155 foo.submit_num += 1
156 foo.state_reset(TASK_STATUS_PREPARING)
157 return []
158 else:
159 # Remote-init done, task actually submits now.
160 foo.waiting_on_job_prep = False
161 foo.state_reset(TASK_STATUS_SUBMITTED)
162 return [foo]
163
164 monkeypatch.setattr(
165 schd.task_job_mgr, 'submit_task_jobs', submit_task_jobs
166 )
167
168 # Put the task into the preparing state
169 schd.release_tasks_to_run()
170 assert foo.state(TASK_STATUS_PREPARING)
171 assert foo.waiting_on_job_prep
172
173 # Reload the workflow - this should wait for the task to submit
174 await commands.run_cmd(commands.reload_workflow(schd))
175
176 # The task should have been flushed through to submitted
177 assert foo.state(TASK_STATUS_SUBMITTED), (
178 "Reload proceeded before preparing task finished submitting"
179 )
180 assert not foo.waiting_on_job_prep
181 # submit_task_jobs should have been called multiple times
182 # (not just once before the loop exited)
183 assert call_count == 4
184
185
186 async def test_reload_failure(
187 flow,
188 one_conf,
189 scheduler,
190 start,
191 log_filter,
192 ):
193 """Reload should not crash the workflow on config errors.
194
195 A warning should be logged along with the error.
196 """
197 id_ = flow(one_conf)
198 schd = scheduler(id_)
199 async with start(schd):
200 # corrupt the config by removing the scheduling section
201 two_conf = {**one_conf, 'scheduling': {}}
202 flow(two_conf, workflow_id=id_)
203
204 # reload the workflow
205 await commands.run_cmd(commands.reload_workflow(schd))
206
207 # the reload should have failed but the workflow should still be
208 # running
209 assert log_filter(
210 contains=(
211 'Reload failed - WorkflowConfigError:'
212 ' missing [scheduling][[graph]] section'
213 )
214 )
215
216 # the config should be unchanged
217 assert schd.config.cfg['scheduling']['graph']['R1'] == 'one'
218
219
220 async def test_reload_global_platform(
221 flow,
222 one_conf,
223 scheduler,
224 start,
225 log_filter,
226 tmp_path,
227 monkeypatch,
228 ):
229 global_config_path = tmp_path / 'global.cylc'
230 monkeypatch.setenv("CYLC_CONF_PATH", str(global_config_path.parent))
231
232 # Original global config file
233 global_config_path.write_text("""
234 [platforms]
235 [[localhost]]
236 [[[meta]]]
237 x = 1
238 """)
239 glbl_cfg(reload=True)
240 assert glbl_cfg().get(['platforms', 'localhost', 'meta', 'x']) == '1'
241
242 id_ = flow(one_conf)
243 schd = scheduler(id_)
244 async with start(schd):
245 # Task platforms reflect the original config
246 rtconf = schd.broadcast_mgr.get_updated_rtconfig(
247 schd.pool.get_tasks()[0]
248 )
249 platform = get_platform(rtconf)
250 assert platform['meta']['x'] == '1'
251
252 # Modify the global config file
253 global_config_path.write_text("""
254 [platforms]
255 [[localhost]]
256 [[[meta]]]
257 x = 2
258 """)
259
260 # reload the workflow and global config
261 await commands.run_cmd(
262 commands.reload_workflow(schd, reload_global=True)
263 )
264
265 # Global config should have been reloaded
266 assert log_filter(contains=('Reloading the global configuration.'))
267
268 # Task platforms reflect the new config
269 rtconf = schd.broadcast_mgr.get_updated_rtconfig(
270 schd.pool.get_tasks()[0]
271 )
272 platform = get_platform(rtconf)
273 assert platform['meta']['x'] == '2'
274
275 # Modify the global config file with an error
276 global_config_path.write_text("""
277 [ERROR]
278 [platforms]
279 [[localhost]]
280 [[[meta]]]
281 x = 3
282 """)
283
284 # reload the workflow and global config
285 await commands.run_cmd(
286 commands.reload_workflow(schd, reload_global=True)
287 )
288
289 # Error is noted in the log
290 assert log_filter(
291 contains=(
292 'This is probably due to an issue with the new configuration.'
293 )
294 )
295
296 # Task platforms should be the last valid value
297 rtconf = schd.broadcast_mgr.get_updated_rtconfig(
298 schd.pool.get_tasks()[0]
299 )
300 platform = get_platform(rtconf)
301 assert platform['meta']['x'] == '2'
302
303 # reload the workflow in verbose mode
304 flags.verbosity = 2
305 await commands.run_cmd(
306 commands.reload_workflow(schd, reload_global=True)
307 )
308
309 # Traceback is shown in the log
310 # (match for ERROR, trace is not captured)
311 assert log_filter(exact_match='ERROR')
312
313
314 async def test_reload_global_platform_group(
315 flow,
316 scheduler,
317 start,
318 log_filter,
319 tmp_path,
320 monkeypatch,
321 ):
322 global_config_path = tmp_path / 'global.cylc'
323 monkeypatch.setenv("CYLC_CONF_PATH", str(global_config_path.parent))
324
325 # Original global config file
326 global_config_path.write_text("""
327 [platforms]
328 [[foo]]
329 [[[meta]]]
330 x = 1
331 [platform groups]
332 [[pg]]
333 platforms = foo
334 """)
335 glbl_cfg(reload=True)
336
337 # Task using the platform group
338 conf = {
339 'scheduling': {'graph': {'R1': 'one'}},
340 'runtime': {
341 'one': {
342 'platform': 'pg',
343 }
344 },
345 }
346
347 id_ = flow(conf)
348 schd = scheduler(id_)
349 async with start(schd):
350 # Task platforms reflect the original config
351 rtconf = schd.broadcast_mgr.get_updated_rtconfig(
352 schd.pool.get_tasks()[0]
353 )
354 platform = get_platform(rtconf)
355 assert platform['meta']['x'] == '1'
356
357 # Modify the global config file
358 global_config_path.write_text("""
359 [platforms]
360 [[bar]]
361 [[[meta]]]
362 x = 2
363 [platform groups]
364 [[pg]]
365 platforms = bar
366 """)
367
368 # reload the workflow and global config
369 await commands.run_cmd(
370 commands.reload_workflow(schd, reload_global=True)
371 )
372
373 # Global config should have been reloaded
374 assert log_filter(contains=('Reloading the global configuration.'))
375
376 # Task platforms reflect the new config
377 rtconf = schd.broadcast_mgr.get_updated_rtconfig(
378 schd.pool.get_tasks()[0]
379 )
380 platform = get_platform(rtconf)
381 assert platform['meta']['x'] == '2'
382
383
384 async def test_orphan_reload(
385 flow,
386 scheduler,
387 start,
388 log_filter,
389 ):
390 """Reload should not fail because of orphaned tasks.
391
392 The following aspects of reload-with-orphans are tested:
393 - Broadcast deltas generated after reload.
394 https://github.com/cylc/cylc-flow/issues/6814
395 - Removal of both xtrigger and associated active/incomplete task.
396 https://github.com/cylc/cylc-flow/issues/6815
397
398 (Orphans being active/incomplete tasks removed from reloaded workflow cfg.)
399 """
400 before = {
401 'scheduling': {
402 'initial cycle point': '20010101T0000Z',
403 'graph': {
404 'R1': '''
405 foo => bar
406 @wall_clock => bar
407 '''
408 }
409 }
410 }
411 after = {
412 'scheduling': {
413 'initial cycle point': '20010101T0000Z',
414 'graph': {
415 'R1': 'foo'
416 }
417 }
418 }
419 id_ = flow(before)
420 schd = scheduler(id_)
421 async with start(schd):
422 # spawn in bar
423 foo = schd.pool._get_task_by_id('20010101T0000Z/foo')
424 schd.pool.task_events_mgr.process_message(foo, 'INFO', 'succeeded')
425 bar = schd.pool._get_task_by_id('20010101T0000Z/bar')
426 # set bar to failed
427 schd.pool.task_events_mgr.process_message(bar, 'INFO', 'failed')
428
429 # Save our progress
430 schd.workflow_db_mgr.put_task_pool(schd.pool)
431
432 # Change workflow to one without bar and xtrigger
433 flow(after, workflow_id=id_)
434
435 # reload the workflow
436 await commands.run_cmd(commands.reload_workflow(schd))
437
438 # test broadcast delta over orphaned task
439 schd.data_store_mgr.delta_broadcast()
440
441 # the reload should have completed successfully
442 assert log_filter(
443 contains=('Reload completed')
444 )
445
446
447 async def test_data_store_tproxy(flow, scheduler, start):
448 """Check N>0 task proxy in data store has correct info on reload.
449
450 https://github.com/cylc/cylc-flow/issues/6973
451 """
452 schd: Scheduler = scheduler(flow('foo => bar'))
453
454 def get_ds_tproxy(task):
455 return schd.data_store_mgr.data[schd.id][TASK_PROXIES][
456 f'{schd.id}//1/{task}'
457 ]
458
459 async with start(schd):
460 await schd.update_data_structure()
461 assert str(get_ds_tproxy('bar').runtime)
462
463 await commands.run_cmd(commands.reload_workflow(schd))
464 assert str(get_ds_tproxy('bar').runtime)