File: test_remove.py

package info (click to toggle)
cylc-flow 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,368 kB
  • sloc: python: 87,751; sh: 17,109; sql: 233; xml: 171; javascript: 78; lisp: 55; makefile: 11
file content (549 lines) | stat: -rw-r--r-- 18,283 bytes parent folder | download
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
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
import pytest

from cylc.flow import CYLC_LOG
from cylc.flow.commands import (
    force_trigger_tasks,
    reload_workflow,
    remove_tasks,
    run_cmd,
)
from cylc.flow.cycling.integer import IntegerPoint
from cylc.flow.id import TaskTokens
from cylc.flow.scheduler import Scheduler
from cylc.flow.task_outputs import TASK_OUTPUT_SUCCEEDED
from cylc.flow.task_proxy import TaskProxy
from cylc.flow.task_state import TASK_STATUS_FAILED


@pytest.fixture
async def cylc_show_prereqs(cylc_show):
    """Fixture that returns the prereq info from `cylc show` in an
    easy-to-use format."""
    async def inner(schd: Scheduler, task: str):
        prerequisites = (await cylc_show(schd, task))[task]['prerequisites']
        return [
            (
                p['satisfied'],
                {c['taskId']: c['satisfied'] for c in p['conditions']},
            )
            for p in prerequisites
        ]

    return inner


@pytest.fixture
def example_workflow(flow):
    return flow({
        'scheduling': {
            'graph': {
                # Note: test both `&` and separate arrows for combining
                # dependencies
                'R1': '''
                    a1 & a2 => b
                    a3 => b
                ''',
            },
        },
    })


def get_data_store_flow_nums(schd: Scheduler, itask: TaskProxy):
    _, ds_tproxy = schd.data_store_mgr.store_node_fetcher(itask.tokens)
    if ds_tproxy:
        return ds_tproxy.flow_nums


async def test_basic(
    example_workflow, scheduler, start, db_select
):
    """Test removing a task from all flows."""
    schd: Scheduler = scheduler(example_workflow)
    async with start(schd):
        a1 = schd.pool._get_task_by_id('1/a1')
        a3 = schd.pool._get_task_by_id('1/a3')
        schd.pool.spawn_on_output(a1, TASK_OUTPUT_SUCCEEDED)
        schd.pool.spawn_on_output(a3, TASK_OUTPUT_SUCCEEDED)
        await schd.update_data_structure()

        assert a1 in schd.pool.get_tasks()
        for table in ('task_states', 'task_outputs'):
            assert db_select(schd, True, table, 'flow_nums', name='a1') == [
                ('[1]',),
            ]
        assert db_select(
            schd, True, 'task_prerequisites', 'satisfied', prereq_name='a1'
        ) == [
            ('satisfied naturally',),
        ]
        assert get_data_store_flow_nums(schd, a1) == '[1]'

        await run_cmd(remove_tasks(schd, ['1/a1'], []))
        await schd.update_data_structure()

        assert a1 not in schd.pool.get_tasks()  # removed from pool
        for table in ('task_states', 'task_outputs'):
            assert db_select(schd, True, table, 'flow_nums', name='a1') == [
                ('[]',),  # removed from all flows
            ]
        assert db_select(
            schd, True, 'task_prerequisites', 'satisfied', prereq_name='a1'
        ) == [
            ('0',),  # prereq is now unsatisfied
        ]
        assert get_data_store_flow_nums(schd, a1) == '[]'


async def test_specific_flow(
    example_workflow, scheduler, start, db_select
):
    """Test removing a task from a specific flow."""
    schd: Scheduler = scheduler(example_workflow)

    def select_prereqs():
        return db_select(
            schd,
            True,
            'task_prerequisites',
            'flow_nums',
            'satisfied',
            prereq_name='a1',
        )

    async with start(schd):
        a1 = schd.pool._get_task_by_id('1/a1')
        await run_cmd(force_trigger_tasks(schd, ['1/a1'], ['1', '2']))
        schd.pool.spawn_on_output(a1, TASK_OUTPUT_SUCCEEDED)
        await schd.update_data_structure()

        assert a1 in schd.pool.get_tasks()
        assert a1.flow_nums == {1, 2}
        for table in ('task_states', 'task_outputs'):
            assert sorted(
                db_select(schd, True, table, 'flow_nums', name='a1')
            ) == [
                ('[1, 2]',),  # triggered task
                ('[1]',),  # original spawned task
            ]
        assert select_prereqs() == [
            ('[1, 2]', 'satisfied naturally'),
        ]
        assert get_data_store_flow_nums(schd, a1) == '[1, 2]'

        await run_cmd(remove_tasks(schd, ['1/a1'], ['1']))
        await schd.update_data_structure()

        assert a1 in schd.pool.get_tasks()  # still in pool
        assert a1.flow_nums == {2}
        for table in ('task_states', 'task_outputs'):
            assert sorted(
                db_select(schd, True, table, 'flow_nums', name='a1')
            ) == [
                ('[2]',),
                ('[]',),
            ]
        assert select_prereqs() == [
            ('[1, 2]', '0'),
        ]
        assert get_data_store_flow_nums(schd, a1) == '[2]'


async def test_unset_prereq(example_workflow, scheduler, start):
    """Test removing a task unsets any prerequisites it satisfied."""
    schd: Scheduler = scheduler(example_workflow)
    async with start(schd):
        for task in ('a1', 'a2', 'a3'):
            schd.pool.spawn_on_output(
                schd.pool.get_task(IntegerPoint('1'), task),
                TASK_OUTPUT_SUCCEEDED,
            )
        b = schd.pool.get_task(IntegerPoint('1'), 'b')
        assert b.prereqs_are_satisfied()

        await run_cmd(remove_tasks(schd, ['1/a1'], []))

        assert not b.prereqs_are_satisfied()


async def test_not_unset_prereq(
    example_workflow, scheduler, start, db_select
):
    """Test removing a task does not unset a force-satisfied prerequisite
    (one that was satisfied by `cylc set --pre`)."""
    schd: Scheduler = scheduler(example_workflow)
    async with start(schd):
        # This set prereq should not be unset by removing a1:
        schd.pool.set_prereqs_and_outputs(
            {TaskTokens('1', 'b')}, outputs=[], prereqs=['1/a1'], flow=[]
        )
        # Whereas the prereq satisfied by this set output *should* be unset
        # by removing a2:
        schd.pool.set_prereqs_and_outputs(
            {TaskTokens('1', 'a2')}, outputs=['succeeded'], prereqs=[], flow=[]
        )
        await schd.update_data_structure()

        assert sorted(
            db_select(
                schd, True, 'task_prerequisites', 'prereq_name', 'satisfied'
            )
        ) == [
            ('a1', 'force satisfied'),
            ('a2', 'satisfied naturally'),
            ('a3', '0'),
        ]

        await run_cmd(remove_tasks(schd, ['1/a1', '1/a2'], []))
        await schd.update_data_structure()

        assert sorted(
            db_select(
                schd, True, 'task_prerequisites', 'prereq_name', 'satisfied'
            )
        ) == [
            ('a1', 'force satisfied'),
            ('a2', '0'),
            ('a3', '0'),
        ]


async def test_nothing_to_do(
    example_workflow, scheduler, start, log_filter
):
    """Test removing an invalid task."""
    schd: Scheduler = scheduler(example_workflow)
    async with start(schd):
        await run_cmd(remove_tasks(schd, ['1/doh'], []))
    assert log_filter(logging.WARNING, 'No tasks match "1/doh"')


async def test_logging(
    flow, scheduler, start, log_filter, caplog: pytest.LogCaptureFixture
):
    """Test logging of a mixture of valid and invalid task removals."""
    schd: Scheduler = scheduler(
        flow({
            'scheduler': {
                'cycle point format': 'CCYY',
            },
            'scheduling': {
                'initial cycle point': '2000',
                'graph': {
                    'R3//P1Y': 'b[-P1Y] => a & b',
                },
            },
        })
    )
    tasks_to_remove = [
        # Active, removable tasks:
        '2000/*',
        # Future, non-removable tasks:
        '2001/a', '2001/b',
        # Glob that doesn't match any active tasks:
        '2002/*',
        # Invalid tasks:
        '2005/a', '2000/doh',
    ]
    async with start(schd) as log:
        log.set_level(logging.DEBUG, CYLC_LOG)
        await run_cmd(remove_tasks(schd, tasks_to_remove, []))

    assert log_filter(
        logging.INFO, "Removed tasks: 2000/a (flows=1), 2000/b (flows=1)"
    )

    assert log_filter(logging.DEBUG, "Task(s) not removable: 2001/a, 2001/b")
    assert log_filter(logging.WARNING, "Invalid cycle point for task: a, 2005")
    assert log_filter(
        logging.WARNING, "No tasks match the IDs:\n* 2000/doh\n* 2005/a"
    )
    # No tasks were submitted/running so none should have been killed:
    assert "job killed" not in caplog.text


async def test_logging_flow_nums(
    example_workflow, scheduler, start, log_filter
):
    """Test logging of task removals involving flow numbers."""
    schd: Scheduler = scheduler(example_workflow)
    async with start(schd) as log:
        log.set_level(logging.DEBUG, CYLC_LOG)
        await run_cmd(force_trigger_tasks(schd, ['1/a1'], ['1', '2']))
        # Removing from flow that doesn't exist doesn't work:
        await run_cmd(remove_tasks(schd, ['1/a1'], ['3']))
        assert log_filter(
            logging.DEBUG, "Task(s) not removable: 1/a1 (flows=3)"
        )

        # But if a valid flow is included, it will be removed from that flow:
        await run_cmd(remove_tasks(schd, ['1/a1'], ['2', '3']))
        assert log_filter(logging.INFO, "Removed tasks: 1/a1 (flows=2)")
        assert schd.pool._get_task_by_id('1/a1').flow_nums == {1}


async def test_retrigger(flow, scheduler, run, reflog, complete):
    """Test prereqs & re-run behaviour when removing tasks."""
    schd: Scheduler = scheduler(
        flow('a => b => c'),
        paused_start=False,
    )
    async with run(schd):
        reflog_triggers: set = reflog(schd)
        await complete(schd, '1/b')

        await run_cmd(remove_tasks(schd, ['1/a', '1/b'], []))
        schd.process_workflow_db_queue()
        # Removing 1/b should un-queue 1/c:
        assert len(schd.pool.task_queue_mgr.queues['default'].deque) == 0

        assert reflog_triggers == {
            ('1/a', None),
            ('1/b', ('1/a',)),
        }
        reflog_triggers.clear()

        await run_cmd(force_trigger_tasks(schd, ['1/a'], []))
        await complete(schd)

    assert reflog_triggers == {
        ('1/a', None),
        # 1/b should have run again after 1/a on the re-trigger in flow 1:
        ('1/b', ('1/a',)),
        ('1/c', ('1/b',)),
    }


async def test_prereqs(
    flow, scheduler, run, complete, cylc_show_prereqs, log_filter
):
    """Test prereqs & stall behaviour when removing tasks."""
    schd: Scheduler = scheduler(
        flow('(a1 | a2) & b => x'),
        paused_start=False,
    )
    async with run(schd):
        await complete(schd, '1/a1', '1/a2', '1/b')

        await run_cmd(remove_tasks(schd, ['1/a1'], []))
        assert not schd.pool.is_stalled()
        assert len(schd.pool.task_queue_mgr.queues['default'].deque)
        # `cylc show` should reflect the now-unsatisfied condition:
        assert await cylc_show_prereqs(schd, '1/x') == [
            (True, {'1/a1': False, '1/a2': True, '1/b': True})
        ]

        await run_cmd(remove_tasks(schd, ['1/b'], []))
        # Should cause stall now because 1/c prereq is unsatisfied:
        assert len(schd.pool.task_queue_mgr.queues['default'].deque) == 0
        assert schd.pool.is_stalled()
        assert log_filter(
            logging.WARNING,
            "1/x is waiting on ['1/a1:succeeded', '1/b:succeeded']",
        )
        assert await cylc_show_prereqs(schd, '1/x') == [
            (False, {'1/a1': False, '1/a2': True, '1/b': False})
        ]

        assert schd.pool._get_task_by_id('1/x')
        await run_cmd(remove_tasks(schd, ['1/a2'], []))
        # Should cause 1/x to be removed from the pool as it no longer has
        # any satisfied prerequisite tasks:
        assert not schd.pool._get_task_by_id('1/x')
        assert log_filter(
            logging.INFO,
            regex=r"1/x.* removed .* prerequisite task\(s\) removed",
        )


async def test_downstream_preparing(flow, scheduler, start):
    """Downstream dependents should not be removed if they are already
    preparing."""
    schd: Scheduler = scheduler(
        flow('''
            a => x
            a => y
        '''),
    )
    async with start(schd):
        a = schd.pool._get_task_by_id('1/a')
        schd.pool.spawn_on_output(a, TASK_OUTPUT_SUCCEEDED)
        assert schd.pool.get_task_ids() == {'1/a', '1/x', '1/y'}

        schd.pool._get_task_by_id('1/y').state_reset('preparing')
        await run_cmd(remove_tasks(schd, ['1/a'], []))
        assert schd.pool.get_task_ids() == {'1/y'}


async def test_downstream_other_flows(flow, scheduler, run, complete):
    """Downstream dependents should not be removed if they exist in other
    flows."""
    schd: Scheduler = scheduler(
        flow('''
            a => b => c => x
            a => x
        '''),
        paused_start=False,
    )
    async with run(schd):
        await complete(schd, '1/a')
        await run_cmd(force_trigger_tasks(schd, ['1/c'], ['2']))
        c = schd.pool._get_task_by_id('1/c')
        schd.pool.spawn_on_output(c, TASK_OUTPUT_SUCCEEDED)
        assert schd.pool._get_task_by_id('1/x').flow_nums == {1, 2}

        await run_cmd(remove_tasks(schd, ['1/c'], ['2']))
        assert schd.pool.get_task_ids() == {'1/b', '1/x'}
        # Note: in future we might want to remove 1/x from flow 2 as well, to
        # maintain flow continuity. However it is tricky at the moment because
        # other prerequisite tasks could exist in flow 2 (we don't know as
        # prereqs do not hold flow info other than in the DB).
        assert schd.pool._get_task_by_id('1/x').flow_nums == {1, 2}


async def test_suicide(flow, scheduler, run, reflog, complete):
    """Test that suicide prereqs are unset by `cylc remove`."""
    schd: Scheduler = scheduler(
        flow('''
            a => b => c => d => x
            a & c => !x
        '''),
        paused_start=False,
    )
    async with run(schd):
        reflog_triggers: set = reflog(schd)
        await complete(schd, '1/b')
        await run_cmd(remove_tasks(schd, ['1/a'], []))
        await complete(schd)

    assert reflog_triggers == {
        ('1/a', None),
        ('1/b', ('1/a',)),
        ('1/c', ('1/b',)),
        ('1/d', ('1/c',)),
        # 1/x not suicided as 1/a was removed:
        ('1/x', ('1/d',)),
    }


async def test_kill_running(flow, scheduler, run, complete, reflog):
    """Test removing a running task should kill it.

    Note this only tests simulation mode and a separate test for live mode
    exists in tests/functional/cylc-remove.
    """
    schd: Scheduler = scheduler(
        flow({
            'scheduling': {
                'graph': {
                    'R1': '''
                        a:started => b => c
                        a:failed => q
                    '''
                },
            },
            'runtime': {
                'a': {
                    'simulation': {
                        'default run length': 'PT30S'
                    },
                },
            },
        }),
        paused_start=False,
    )
    async with run(schd):
        reflog_triggers = reflog(schd)
        await complete(schd, '1/b')
        a = schd.pool._get_task_by_id('1/a')
        await run_cmd(remove_tasks(schd, ['1/a'], []))
        assert a.state(TASK_STATUS_FAILED, is_held=True)
        await complete(schd)

    assert reflog_triggers == {
        ('1/a', None),
        ('1/b', ('1/a',)),
        ('1/c', ('1/b',)),
        # The a:failed output should not cause 1/q to run
    }


async def test_reload_changed_config(flow, scheduler, run, complete):
    """Test that a task is removed from the pool if its configuration changes
    to make it no longer match the graph."""
    wid = flow({
        'scheduling': {
            'graph': {
                'R1': '''
                    a => b
                    a:started => s & b
                ''',
            },
        },
        'runtime': {
            'a': {
                'simulation': {
                    # Ensure 1/a still in pool during reload
                    'fail cycle points': 'all',
                },
            },
        },
    })
    schd: Scheduler = scheduler(wid, paused_start=False)
    async with run(schd):
        await complete(schd, '1/s')
        # Change graph then reload
        flow('b', workflow_id=wid)
        await run_cmd(reload_workflow(schd))
        assert schd.config.cfg['scheduling']['graph']['R1'] == 'b'
        assert schd.pool.get_task_ids() == {'1/a', '1/b'}

        await run_cmd(remove_tasks(schd, ['1/a'], []))
        await complete(schd, '1/b')


async def test_remove_triggered(flow, scheduler, start):
    """It should remove tasks from pool and from to_trigger set."""
    conf = {
        'scheduling': {
            'graph': {
                'R1': 'a & b'
            },
        },
    }
    schd: Scheduler = scheduler(flow(conf))
    async with start(schd):
        foo, bar = schd.pool.get_tasks()
        # trigger foo (now)
        await run_cmd(
            force_trigger_tasks(schd, [foo.identity], [])
        )
        assert foo in schd.pool.tasks_to_trigger_now

        # trigger bar
        await run_cmd(
            force_trigger_tasks(schd, [bar.identity], [])
        )
        assert bar in schd.pool.tasks_to_trigger_now

        await run_cmd(
            remove_tasks(schd, [foo.identity, bar.identity], [])
        )
        assert not schd.pool.get_tasks()
        assert not schd.pool.tasks_to_trigger_now