| 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 |
|
| 18 |
import asyncio |
| 19 |
import logging |
| 20 |
from secrets import token_hex |
| 21 |
from unittest.mock import Mock |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
from cylc.flow.commands import ( |
| 26 |
kill_tasks, |
| 27 |
remove_tasks, |
| 28 |
run_cmd, |
| 29 |
set_prereqs_and_outputs, |
| 30 |
) |
| 31 |
from cylc.flow.cycling.integer import IntegerPoint |
| 32 |
from cylc.flow.scheduler import Scheduler |
| 33 |
from cylc.flow.task_proxy import TaskProxy |
| 34 |
from cylc.flow.task_remote_mgr import ( |
| 35 |
REMOTE_FILE_INSTALL_DONE, |
| 36 |
REMOTE_FILE_INSTALL_IN_PROGRESS, |
| 37 |
REMOTE_INIT_DONE, |
| 38 |
REMOTE_INIT_IN_PROGRESS, |
| 39 |
) |
| 40 |
from cylc.flow.task_state import ( |
| 41 |
TASK_STATUS_FAILED, |
| 42 |
TASK_STATUS_PREPARING, |
| 43 |
TASK_STATUS_RUNNING, |
| 44 |
TASK_STATUS_SUBMIT_FAILED, |
| 45 |
TASK_STATUS_WAITING, |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
LOCALHOST = 'localhost' |
| 50 |
|
| 51 |
|
| 52 |
async def task_state(itask: TaskProxy, state: str, timeout=4, **kwargs): |
| 53 |
"""Await task state.""" |
| 54 |
async with asyncio.timeout(timeout): |
| 55 |
while not itask.state(state, **kwargs): |
| 56 |
await asyncio.sleep(0.1) |
| 57 |
|
| 58 |
|
| 59 |
def patch_remote_init(schd: Scheduler, value: str): |
| 60 |
"""Set remote init state.""" |
| 61 |
schd.task_job_mgr.task_remote_mgr.remote_init_map[LOCALHOST] = value |
| 62 |
|
| 63 |
|
| 64 |
async def test_simulation(flow, scheduler, run): |
| 65 |
"""Test killing a running task in simulation mode.""" |
| 66 |
conf = { |
| 67 |
'scheduling': { |
| 68 |
'graph': { |
| 69 |
'R1': 'foo', |
| 70 |
}, |
| 71 |
}, |
| 72 |
'runtime': { |
| 73 |
'root': { |
| 74 |
'simulation': { |
| 75 |
'default run length': 'PT30S', |
| 76 |
}, |
| 77 |
}, |
| 78 |
}, |
| 79 |
} |
| 80 |
schd: Scheduler = scheduler(flow(conf), paused_start=False) |
| 81 |
async with run(schd): |
| 82 |
itask = schd.pool.get_tasks()[0] |
| 83 |
await task_state(itask, TASK_STATUS_RUNNING) |
| 84 |
|
| 85 |
await run_cmd(kill_tasks(schd, [itask.identity])) |
| 86 |
await task_state(itask, TASK_STATUS_FAILED, is_held=True) |
| 87 |
assert schd.check_workflow_stalled() |
| 88 |
|
| 89 |
|
| 90 |
async def test_kill_preparing( |
| 91 |
flow, scheduler, run, monkeypatch: pytest.MonkeyPatch, log_filter |
| 92 |
): |
| 93 |
"""Test killing a preparing task.""" |
| 94 |
schd: Scheduler = scheduler( |
| 95 |
flow('foo'), run_mode='live', paused_start=False |
| 96 |
) |
| 97 |
async with run(schd): |
| 98 |
# Make the task indefinitely preparing: |
| 99 |
monkeypatch.setattr( |
| 100 |
schd.task_job_mgr, '_prep_submit_task_job', Mock(return_value=None) |
| 101 |
) |
| 102 |
itask = schd.pool.get_tasks()[0] |
| 103 |
await task_state(itask, TASK_STATUS_PREPARING, is_held=False) |
| 104 |
|
| 105 |
await run_cmd(kill_tasks(schd, [itask.identity])) |
| 106 |
await task_state(itask, TASK_STATUS_SUBMIT_FAILED, is_held=True) |
| 107 |
assert log_filter(logging.ERROR, 'killed in job prep') |
| 108 |
|
| 109 |
|
| 110 |
async def test_kill_preparing_pipeline( |
| 111 |
flow, scheduler, start, monkeypatch: pytest.MonkeyPatch |
| 112 |
): |
| 113 |
"""Test killing a preparing task through various stages of the preparing |
| 114 |
pipeline that involve submitting subprocesses and waiting for them to |
| 115 |
complete.""" |
| 116 |
# Make localhost look like a remote target so we can test |
| 117 |
# remote init/file install stages: |
| 118 |
monkeypatch.setattr( |
| 119 |
'cylc.flow.task_job_mgr.get_localhost_install_target', |
| 120 |
Mock(return_value=token_hex()), |
| 121 |
) |
| 122 |
|
| 123 |
schd: Scheduler = scheduler( |
| 124 |
flow('one'), run_mode='live', paused_start=False |
| 125 |
) |
| 126 |
async with start(schd): |
| 127 |
remote_mgr = schd.task_job_mgr.task_remote_mgr |
| 128 |
mock_eval_platform = Mock(return_value=None) |
| 129 |
monkeypatch.setattr(remote_mgr, 'eval_platform', mock_eval_platform) |
| 130 |
mock_remote_init = Mock() |
| 131 |
monkeypatch.setattr(remote_mgr, 'remote_init', mock_remote_init) |
| 132 |
mock_file_install = Mock() |
| 133 |
monkeypatch.setattr(remote_mgr, 'file_install', mock_file_install) |
| 134 |
itask = schd.pool.get_tasks()[0] |
| 135 |
|
| 136 |
# Platform eval: |
| 137 |
schd.submit_task_jobs([itask]) |
| 138 |
assert itask.state(TASK_STATUS_PREPARING) |
| 139 |
assert schd.release_tasks_to_run() is False |
| 140 |
await run_cmd(kill_tasks(schd, [itask.identity])) |
| 141 |
assert itask.state(TASK_STATUS_SUBMIT_FAILED) |
| 142 |
assert schd.release_tasks_to_run() is False |
| 143 |
# Set to finished: |
| 144 |
mock_eval_platform.return_value = LOCALHOST |
| 145 |
# Should not submit after finish because it was killed: |
| 146 |
assert schd.release_tasks_to_run() is False |
| 147 |
|
| 148 |
# Remote init: |
| 149 |
patch_remote_init(schd, REMOTE_INIT_IN_PROGRESS) |
| 150 |
schd.submit_task_jobs([itask]) |
| 151 |
assert itask.state(TASK_STATUS_PREPARING) |
| 152 |
assert schd.release_tasks_to_run() is False |
| 153 |
await run_cmd(kill_tasks(schd, [itask.identity])) |
| 154 |
assert itask.state(TASK_STATUS_SUBMIT_FAILED) |
| 155 |
assert schd.release_tasks_to_run() is False |
| 156 |
# Set to finished: |
| 157 |
patch_remote_init(schd, REMOTE_INIT_DONE) |
| 158 |
# Should not submit after finish because it was killed: |
| 159 |
assert schd.release_tasks_to_run() is False |
| 160 |
assert not mock_remote_init.called |
| 161 |
|
| 162 |
# Remote file install: |
| 163 |
patch_remote_init(schd, REMOTE_FILE_INSTALL_IN_PROGRESS) |
| 164 |
schd.submit_task_jobs([itask]) |
| 165 |
assert itask.state(TASK_STATUS_PREPARING) |
| 166 |
assert schd.release_tasks_to_run() is False |
| 167 |
await run_cmd(kill_tasks(schd, [itask.identity])) |
| 168 |
assert itask.state(TASK_STATUS_SUBMIT_FAILED) |
| 169 |
assert schd.release_tasks_to_run() is False |
| 170 |
# Set to finished: |
| 171 |
patch_remote_init(schd, REMOTE_FILE_INSTALL_DONE) |
| 172 |
# Should not submit after finish because it was killed: |
| 173 |
assert schd.release_tasks_to_run() is False |
| 174 |
assert not mock_file_install.called |
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.parametrize('method', (remove_tasks, set_prereqs_and_outputs)) |
| 178 |
async def test_kill_hold_retry(method, flow, scheduler, start): |
| 179 |
"""Test the interaction between kill, hold and automated retry. |
| 180 |
|
| 181 |
When killed, tasks should be put into the held state to suppress automatic |
| 182 |
retries. |
| 183 |
|
| 184 |
Once the task has been completed or been removed, the held and retrying |
| 185 |
states no longer serve a purpose and should be cleared (see |
| 186 |
https://github.com/cylc/cylc-flow/pull/7100). |
| 187 |
""" |
| 188 |
id_ = flow({ |
| 189 |
'scheduling': { |
| 190 |
'graph': { |
| 191 |
'R1': 'execution & submission' |
| 192 |
}, |
| 193 |
}, |
| 194 |
'runtime': { |
| 195 |
'execution, submission': { |
| 196 |
'execution retry delays': 'PT0S', |
| 197 |
'submission retry delays': 'PT0S', |
| 198 |
} |
| 199 |
} |
| 200 |
}) |
| 201 |
schd: Scheduler = scheduler(id_) |
| 202 |
async with start(schd): |
| 203 |
# a task we will make execution-fail |
| 204 |
execution = schd.pool.get_task(IntegerPoint('1'), 'execution') |
| 205 |
# a task we will make submission-fail |
| 206 |
submission = schd.pool.get_task(IntegerPoint('1'), 'submission') |
| 207 |
assert execution and submission |
| 208 |
|
| 209 |
# the tasks should not be held (doesn't hurt to check!) |
| 210 |
assert execution.state(is_held=False) |
| 211 |
assert submission.state(is_held=False) |
| 212 |
|
| 213 |
# fake job submissions |
| 214 |
execution.state_reset(TASK_STATUS_RUNNING) |
| 215 |
submission.state_reset(TASK_STATUS_PREPARING) |
| 216 |
schd.task_job_mgr._set_retry_timers(execution, execution.tdef.rtconfig) |
| 217 |
schd.task_job_mgr._set_retry_timers( |
| 218 |
submission, submission.tdef.rtconfig |
| 219 |
) |
| 220 |
|
| 221 |
# kill the tasks |
| 222 |
await run_cmd( |
| 223 |
kill_tasks(schd, [execution.identity, submission.identity]) |
| 224 |
) |
| 225 |
|
| 226 |
# the tasks should be in the held state with a retry lined up |
| 227 |
assert execution.state(TASK_STATUS_WAITING, is_held=True) # held |
| 228 |
assert submission.state(TASK_STATUS_WAITING, is_held=True) |
| 229 |
assert list(execution.state.xtriggers.values()) == [False] # retry |
| 230 |
assert list(submission.state.xtriggers.values()) == [False] |
| 231 |
|
| 232 |
# either remove or complete the tasks |
| 233 |
await run_cmd( |
| 234 |
method(schd, [execution.identity, submission.identity], []) |
| 235 |
) |
| 236 |
|
| 237 |
# the held state should be cleared |
| 238 |
assert execution.state(is_held=False) |
| 239 |
assert submission.state(is_held=False) |
| 240 |
|
| 241 |
# the retry state should be cleared |
| 242 |
assert list(execution.state.xtriggers.values()) == [True] |
| 243 |
assert list(submission.state.xtriggers.values()) == [True] |