| 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 |
"""Test "cylc set" functionality. |
| 18 |
|
| 19 |
Note: see also functional tests |
| 20 |
""" |
| 21 |
|
| 22 |
from secrets import token_hex |
| 23 |
from typing import Callable |
| 24 |
|
| 25 |
import pytest |
| 26 |
|
| 27 |
from cylc.flow.commands import ( |
| 28 |
run_cmd, |
| 29 |
set_prereqs_and_outputs, |
| 30 |
) |
| 31 |
from cylc.flow.cycling.integer import IntegerPoint |
| 32 |
from cylc.flow.data_messages_pb2 import ( |
| 33 |
PbJob, |
| 34 |
PbTaskProxy, |
| 35 |
) |
| 36 |
from cylc.flow.data_store_mgr import ( |
| 37 |
JOBS, |
| 38 |
TASK_PROXIES, |
| 39 |
task_mean_elapsed_time, |
| 40 |
) |
| 41 |
from cylc.flow.id import TaskTokens |
| 42 |
from cylc.flow.scheduler import Scheduler |
| 43 |
from cylc.flow.task_outputs import ( |
| 44 |
TASK_OUTPUT_FAILED, |
| 45 |
TASK_OUTPUT_STARTED, |
| 46 |
TASK_OUTPUT_SUBMITTED, |
| 47 |
TASK_OUTPUT_SUCCEEDED, |
| 48 |
) |
| 49 |
from cylc.flow.task_proxy import TaskProxy |
| 50 |
from cylc.flow.task_state import ( |
| 51 |
TASK_STATUS_FAILED, |
| 52 |
TASK_STATUS_PREPARING, |
| 53 |
TASK_STATUS_SUCCEEDED, |
| 54 |
TASK_STATUS_WAITING, |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def outputs_section(*names: str) -> dict: |
| 59 |
"""Create outputs section with random messages for the given output names. |
| 60 |
""" |
| 61 |
return { |
| 62 |
'outputs': { |
| 63 |
name: token_hex() for name in names |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
async def test_set_parentless_spawning( |
| 69 |
flow, |
| 70 |
scheduler, |
| 71 |
run, |
| 72 |
complete, |
| 73 |
): |
| 74 |
"""Ensure that setting outputs does not interfere with parentless spawning. |
| 75 |
|
| 76 |
Setting outputs manually causes the logic to follow a different code |
| 77 |
pathway to "natural" output satisfaction. If we're not careful this could |
| 78 |
lead to "premature shutdown" (i.e. the scheduler thinks it's finished when |
| 79 |
it isn't), this test makes sure that's not the case. |
| 80 |
""" |
| 81 |
id_ = flow({ |
| 82 |
'scheduling': { |
| 83 |
'initial cycle point': '1', |
| 84 |
'cycling mode': 'integer', |
| 85 |
'runahead limit': 'P0', |
| 86 |
'graph': {'P1': 'a => z'}, |
| 87 |
}, |
| 88 |
}) |
| 89 |
schd: Scheduler = scheduler(id_, paused_start=False) |
| 90 |
async with run(schd): |
| 91 |
# mark cycle 1 as succeeded |
| 92 |
schd.pool.set_prereqs_and_outputs( |
| 93 |
{TaskTokens('1', 'a'), TaskTokens('1', 'z')}, |
| 94 |
['succeeded'], |
| 95 |
[], |
| 96 |
['1'], |
| 97 |
) |
| 98 |
|
| 99 |
# the parentless task "a" should be spawned out to the next cycle |
| 100 |
assert schd.pool.get_task_ids() == {'2/a'} |
| 101 |
|
| 102 |
# the workflow should run on to the next cycle |
| 103 |
await complete(schd, '2/a', timeout=5) |
| 104 |
assert schd.pool.get_task_ids() == {'2/z', '3/a'} |
| 105 |
|
| 106 |
|
| 107 |
async def test_rerun_incomplete( |
| 108 |
flow, |
| 109 |
scheduler, |
| 110 |
run, |
| 111 |
complete, |
| 112 |
reflog, |
| 113 |
): |
| 114 |
"""Incomplete tasks should be re-run.""" |
| 115 |
id_ = flow({ |
| 116 |
'scheduling': { |
| 117 |
'graph': {'R1': 'a => z'}, |
| 118 |
}, |
| 119 |
'runtime': { |
| 120 |
# register a custom output |
| 121 |
'a': {'outputs': {'x': 'xyz'}}, |
| 122 |
}, |
| 123 |
}) |
| 124 |
schd = scheduler(id_, paused_start=False) |
| 125 |
async with run(schd): |
| 126 |
# generate 1/a:x but do not complete 1/a |
| 127 |
schd.pool.set_prereqs_and_outputs( |
| 128 |
{TaskTokens('1', 'a')}, ['x'], [], ['1'] |
| 129 |
) |
| 130 |
triggers = reflog(schd) |
| 131 |
await complete(schd) |
| 132 |
|
| 133 |
assert triggers == { |
| 134 |
# the task 1/a should have been run despite the earlier |
| 135 |
# setting of the "x" output |
| 136 |
('1/a', None), |
| 137 |
('1/z', ('1/a',)), |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
async def test_data_store( |
| 142 |
flow, |
| 143 |
scheduler, |
| 144 |
start, |
| 145 |
): |
| 146 |
"""Test that manually set prereqs/outputs are applied to the data store.""" |
| 147 |
id_ = flow({ |
| 148 |
'scheduling': { |
| 149 |
'graph': {'R1': 'a => z'}, |
| 150 |
}, |
| 151 |
'runtime': { |
| 152 |
# register a custom output |
| 153 |
'a': {'outputs': {'x': 'xyz'}}, |
| 154 |
}, |
| 155 |
}) |
| 156 |
schd: Scheduler = scheduler(id_) |
| 157 |
async with start(schd): |
| 158 |
await schd.update_data_structure() |
| 159 |
data = schd.data_store_mgr.data[schd.tokens.id] |
| 160 |
# 1/a not the same data-store object post set and not in pool |
| 161 |
# post succeeded. |
| 162 |
task_a_id = schd.pool.get_task(IntegerPoint('1'), 'a').tokens.id |
| 163 |
|
| 164 |
# set the 1/a:succeeded prereq of 1/z |
| 165 |
schd.pool.set_prereqs_and_outputs( |
| 166 |
{TaskTokens('1', 'z')}, [], ['1/a:succeeded'], ['1']) |
| 167 |
task_z = data[TASK_PROXIES][ |
| 168 |
schd.pool.get_task(IntegerPoint('1'), 'z').tokens.id |
| 169 |
] |
| 170 |
await schd.update_data_structure() |
| 171 |
assert task_z.prerequisites[0].satisfied is True |
| 172 |
|
| 173 |
# set 1/a:x the task should be waiting with output x satisfied |
| 174 |
schd.pool.set_prereqs_and_outputs( |
| 175 |
{TaskTokens('1', 'a')}, ['x'], [], ['1'] |
| 176 |
) |
| 177 |
await schd.update_data_structure() |
| 178 |
task_a: PbTaskProxy = data[TASK_PROXIES][task_a_id] |
| 179 |
assert task_a.state == TASK_STATUS_WAITING |
| 180 |
assert task_a.outputs['x'].satisfied is True |
| 181 |
assert task_a.outputs['succeeded'].satisfied is False |
| 182 |
|
| 183 |
# set 1/a:succeeded the task should be succeeded with output x sat |
| 184 |
schd.pool.set_prereqs_and_outputs( |
| 185 |
{TaskTokens('1', 'a')}, ['succeeded'], [], ['1'] |
| 186 |
) |
| 187 |
await schd.update_data_structure() |
| 188 |
task_a: PbTaskProxy = data[TASK_PROXIES][task_a_id] |
| 189 |
assert task_a.state == TASK_STATUS_SUCCEEDED |
| 190 |
assert task_a.outputs['x'].satisfied is True |
| 191 |
assert task_a.outputs['succeeded'].satisfied is True |
| 192 |
|
| 193 |
|
| 194 |
async def test_incomplete_detection( |
| 195 |
one_conf, |
| 196 |
flow, |
| 197 |
scheduler, |
| 198 |
start, |
| 199 |
log_filter, |
| 200 |
): |
| 201 |
"""It should detect and log finished tasks left with incomplete outputs.""" |
| 202 |
schd = scheduler(flow(one_conf)) |
| 203 |
async with start(schd): |
| 204 |
schd.pool.set_prereqs_and_outputs( |
| 205 |
{TaskTokens('1', 'one')}, ['failed'], [], ['1'] |
| 206 |
) |
| 207 |
assert log_filter(contains='1/one did not complete') |
| 208 |
|
| 209 |
|
| 210 |
async def test_pre_all(flow, scheduler, run): |
| 211 |
"""Ensure that --pre=all is interpreted as a special case |
| 212 |
and _not_ tokenized. |
| 213 |
""" |
| 214 |
id_ = flow({'scheduling': {'graph': {'R1': 'a => z'}}}) |
| 215 |
schd = scheduler(id_, paused_start=False) |
| 216 |
async with run(schd) as log: |
| 217 |
schd.pool.set_prereqs_and_outputs( |
| 218 |
{TaskTokens('1', 'z')}, [], ['all'], [] |
| 219 |
) |
| 220 |
warn_or_higher = [i for i in log.records if i.levelno > 30] |
| 221 |
assert warn_or_higher == [] |
| 222 |
|
| 223 |
|
| 224 |
async def test_bad_prereq( |
| 225 |
flow: 'Callable', |
| 226 |
scheduler: 'Callable', |
| 227 |
run: 'Callable', |
| 228 |
complete: 'Callable', |
| 229 |
caplog: 'pytest.LogCaptureFixture' |
| 230 |
): |
| 231 |
"""Attempting to set an invalid prerequisite should not leave a trace in |
| 232 |
the DB that prevents the target task from spawning. later on. |
| 233 |
""" |
| 234 |
id_ = flow({ |
| 235 |
'scheduling': { |
| 236 |
'graph': {'R1': 'a => b => c'}, |
| 237 |
}, |
| 238 |
}) |
| 239 |
schd = scheduler(id_, paused_start=False) |
| 240 |
async with run(schd): |
| 241 |
schd.pool.set_prereqs_and_outputs( |
| 242 |
{TaskTokens('1', 'c')}, [], ['1/a'], [] |
| 243 |
) |
| 244 |
assert schd.pool.get_task_ids() == {'1/a'} |
| 245 |
assert '1/c does not depend on "1/a:succeeded"' in caplog.text |
| 246 |
|
| 247 |
schd.workflow_db_mgr.process_queued_ops() |
| 248 |
|
| 249 |
# This will fail if the previous set left 1/c in the DB: |
| 250 |
schd.pool.set_prereqs_and_outputs( |
| 251 |
{TaskTokens('1', 'c')}, [], ['1/b'], [] |
| 252 |
) |
| 253 |
assert schd.pool.get_task_ids() == {'1/a', '1/c'} |
| 254 |
await complete(schd, '1/c', timeout=5) |
| 255 |
|
| 256 |
|
| 257 |
async def test_no_outputs_given(flow, scheduler, start): |
| 258 |
"""Test `cylc set` without providing any outputs. |
| 259 |
|
| 260 |
It should set the "success pathway" outputs. |
| 261 |
""" |
| 262 |
schd: Scheduler = scheduler( |
| 263 |
flow({ |
| 264 |
'scheduling': { |
| 265 |
'graph': { |
| 266 |
'R1': r""" |
| 267 |
foo? => alpha |
| 268 |
foo:submitted? => bravo |
| 269 |
foo:started? => charlie |
| 270 |
foo:x => xray |
| 271 |
# Optional custom outputs not emitted: |
| 272 |
foo:y? => yankee |
| 273 |
# Non-success-pathway outputs not emitted: |
| 274 |
foo:submit-failed? => delta |
| 275 |
""", |
| 276 |
}, |
| 277 |
}, |
| 278 |
'runtime': { |
| 279 |
'foo': outputs_section('x', 'y'), |
| 280 |
}, |
| 281 |
}) |
| 282 |
) |
| 283 |
async with start(schd): |
| 284 |
foo = schd.pool.get_tasks()[0] |
| 285 |
await run_cmd( |
| 286 |
set_prereqs_and_outputs(schd, {foo.tokens}, []) |
| 287 |
) |
| 288 |
assert set(foo.state.outputs.get_completed_outputs()) == { |
| 289 |
TASK_OUTPUT_SUBMITTED, |
| 290 |
TASK_OUTPUT_STARTED, |
| 291 |
TASK_OUTPUT_SUCCEEDED, |
| 292 |
'x' |
| 293 |
} |
| 294 |
assert schd.pool.get_task_ids() == { |
| 295 |
'1/alpha', |
| 296 |
'1/bravo', |
| 297 |
'1/charlie', |
| 298 |
'1/xray', |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
async def test_completion_expr(flow, scheduler, start): |
| 303 |
"""Test `cylc set` without providing any outputs on a task that has a |
| 304 |
custom completion expression.""" |
| 305 |
conf = { |
| 306 |
'scheduling': { |
| 307 |
'graph': { |
| 308 |
'R1': 'foo? | foo:x? => bar' |
| 309 |
}, |
| 310 |
}, |
| 311 |
'runtime': { |
| 312 |
'foo': { |
| 313 |
**outputs_section('x'), |
| 314 |
'completion': '(succeeded or x) or failed' |
| 315 |
}, |
| 316 |
}, |
| 317 |
} |
| 318 |
schd: Scheduler = scheduler(flow(conf)) |
| 319 |
async with start(schd): |
| 320 |
foo = schd.pool.get_tasks()[0] |
| 321 |
await run_cmd( |
| 322 |
set_prereqs_and_outputs(schd, {foo.tokens}, []) |
| 323 |
) |
| 324 |
assert set(foo.state.outputs.get_completed_outputs()) == { |
| 325 |
TASK_OUTPUT_SUBMITTED, |
| 326 |
TASK_OUTPUT_STARTED, |
| 327 |
TASK_OUTPUT_SUCCEEDED, |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
async def test_set_submitted(flow, scheduler, start): |
| 332 |
"""`cylc set --out submitted` should spawn children that depend on the |
| 333 |
output, but not affect the task's state.""" |
| 334 |
schd: Scheduler = scheduler(flow('foo:submitted? => bar')) |
| 335 |
async with start(schd): |
| 336 |
foo = schd.pool.get_tasks()[0] |
| 337 |
await run_cmd( |
| 338 |
set_prereqs_and_outputs( |
| 339 |
schd, [foo.identity], [], ['submitted'] |
| 340 |
) |
| 341 |
) |
| 342 |
assert set(foo.state.outputs.get_completed_outputs()) == { |
| 343 |
TASK_OUTPUT_SUBMITTED, |
| 344 |
} |
| 345 |
assert schd.pool.get_task_ids() == {'1/foo', '1/bar'} |
| 346 |
assert foo.state(TASK_STATUS_WAITING) |
| 347 |
|
| 348 |
|
| 349 |
async def test_job_state(flow, scheduler, start, db_select): |
| 350 |
"""Test that setting outputs does not change the job state.""" |
| 351 |
schd: Scheduler = scheduler(flow('foo => bar')) |
| 352 |
fail_time = '1950-01-01T00:00:00Z' |
| 353 |
|
| 354 |
def db_task_states(itask: TaskProxy): |
| 355 |
return db_select( |
| 356 |
schd, True, 'task_states', 'status', name=itask.tdef.name |
| 357 |
) |
| 358 |
|
| 359 |
def assert_job_failed(itask: TaskProxy): |
| 360 |
"""...in the DB and data store.""" |
| 361 |
assert db_select( |
| 362 |
schd, |
| 363 |
True, |
| 364 |
'task_jobs', |
| 365 |
'run_status', |
| 366 |
'time_run_exit', |
| 367 |
name=itask.tdef.name, |
| 368 |
) == [(1, fail_time)] |
| 369 |
pb_job: PbJob = schd.data_store_mgr.data[schd.tokens.id][JOBS][ |
| 370 |
itask.job_tokens.id |
| 371 |
] |
| 372 |
assert pb_job.state == TASK_STATUS_FAILED |
| 373 |
assert pb_job.finished_time == fail_time |
| 374 |
|
| 375 |
async with start(schd): |
| 376 |
foo = schd.pool.get_tasks()[0] |
| 377 |
schd.submit_task_jobs([foo]) |
| 378 |
schd.task_events_mgr.process_message( |
| 379 |
foo, 'INFO', TASK_OUTPUT_FAILED, event_time=fail_time |
| 380 |
) |
| 381 |
await schd.update_data_structure() |
| 382 |
assert foo.state(TASK_STATUS_FAILED) |
| 383 |
assert db_task_states(foo) == [(TASK_STATUS_FAILED,)] |
| 384 |
assert_job_failed(foo) |
| 385 |
|
| 386 |
await run_cmd( |
| 387 |
set_prereqs_and_outputs(schd, [foo.identity], []) |
| 388 |
) |
| 389 |
await schd.update_data_structure() |
| 390 |
# Task is now succeeded: |
| 391 |
assert foo.state(TASK_STATUS_SUCCEEDED) |
| 392 |
assert db_task_states(foo) == [(TASK_STATUS_SUCCEEDED,)] |
| 393 |
# But job is still failed: |
| 394 |
assert_job_failed(foo) |
| 395 |
|
| 396 |
|
| 397 |
async def test_set_already_succeeded( |
| 398 |
flow, scheduler, run, complete, db_select |
| 399 |
): |
| 400 |
"""Doing `cylc set` on a task that has already succeeded should not |
| 401 |
change anything.""" |
| 402 |
schd: Scheduler = scheduler( |
| 403 |
flow('foo => bar'), |
| 404 |
paused_start=False, |
| 405 |
) |
| 406 |
|
| 407 |
def db_task_states(itask: TaskProxy): |
| 408 |
return db_select( |
| 409 |
schd, |
| 410 |
True, |
| 411 |
'task_states', |
| 412 |
'submit_num', |
| 413 |
'status', |
| 414 |
'time_updated', |
| 415 |
name=itask.tdef.name, |
| 416 |
) |
| 417 |
|
| 418 |
def data_store_task_state(itask: TaskProxy): |
| 419 |
return schd.data_store_mgr.data[schd.tokens.id][TASK_PROXIES][ |
| 420 |
itask.tokens.id |
| 421 |
].state |
| 422 |
|
| 423 |
async with run(schd): |
| 424 |
foo = schd.pool.get_tasks()[0] |
| 425 |
await complete(schd, foo.identity) |
| 426 |
time_updated = db_task_states(foo)[0][2] |
| 427 |
expected = [(1, TASK_STATUS_SUCCEEDED, time_updated)] |
| 428 |
assert db_task_states(foo) == expected |
| 429 |
assert data_store_task_state(foo) == TASK_STATUS_SUCCEEDED |
| 430 |
|
| 431 |
await run_cmd( |
| 432 |
set_prereqs_and_outputs(schd, [foo.identity], []) |
| 433 |
) |
| 434 |
assert foo.state(TASK_STATUS_SUCCEEDED) |
| 435 |
await schd.update_data_structure() |
| 436 |
assert db_task_states(foo) == expected |
| 437 |
assert data_store_task_state(foo) == TASK_STATUS_SUCCEEDED |
| 438 |
|
| 439 |
|
| 440 |
async def test_timings(one_conf, flow, scheduler, start, caplog): |
| 441 |
"""Test that setting outputs does not change the task timings.""" |
| 442 |
wid = flow({ |
| 443 |
**one_conf, |
| 444 |
'runtime': { |
| 445 |
'one': { |
| 446 |
'execution time limit': 'PT100S', |
| 447 |
}, |
| 448 |
}, |
| 449 |
}) |
| 450 |
schd: Scheduler = scheduler(wid) |
| 451 |
async with start(schd): |
| 452 |
itask = schd.pool.get_tasks()[0] |
| 453 |
|
| 454 |
def check_times(): |
| 455 |
assert not itask.tdef.elapsed_times |
| 456 |
assert task_mean_elapsed_time(itask.tdef) == 100 |
| 457 |
|
| 458 |
itask.state_reset(TASK_STATUS_PREPARING) |
| 459 |
schd.task_events_mgr.process_message( |
| 460 |
itask, 'INFO', TASK_OUTPUT_STARTED |
| 461 |
) |
| 462 |
check_times() |
| 463 |
|
| 464 |
await run_cmd(set_prereqs_and_outputs(schd, [itask.identity], [])) |
| 465 |
check_times() |
| 466 |
|
| 467 |
|
| 468 |
async def test_set_clears_modifiers(flow, scheduler, start): |
| 469 |
"""Manually setting a final output on a task should clear some states. |
| 470 |
|
| 471 |
The is_held and is_retry task states should be cleared if a task is |
| 472 |
manually set to a final state as these states become moot and confusing. |
| 473 |
|
| 474 |
See https://github.com/cylc/cylc-flow/issues/7065 |
| 475 |
""" |
| 476 |
id_ = flow( |
| 477 |
{ |
| 478 |
'scheduling': { |
| 479 |
'graph': {'R1': 'a => b'}, |
| 480 |
}, |
| 481 |
'runtime': { |
| 482 |
'a': { |
| 483 |
'outputs': { |
| 484 |
'x': 'xxx', |
| 485 |
}, |
| 486 |
}, |
| 487 |
}, |
| 488 |
}, |
| 489 |
) |
| 490 |
schd = scheduler(id_) |
| 491 |
|
| 492 |
async with start(schd): |
| 493 |
task_a = schd.pool.get_task(IntegerPoint('1'), 'a') |
| 494 |
assert task_a |
| 495 |
|
| 496 |
# line up both submission and execution retries |
| 497 |
schd.task_events_mgr._retry_task(task_a, 0, True) |
| 498 |
schd.task_events_mgr._retry_task(task_a, 0, False) |
| 499 |
|
| 500 |
# mark the task as held |
| 501 |
schd.pool.hold_tasks({task_a.tokens}) |
| 502 |
|
| 503 |
# update the data store |
| 504 |
await schd.update_data_structure() |
| 505 |
task_a_data_store = schd.data_store_mgr.data[schd.id]['task_proxies'][ |
| 506 |
task_a.tokens.id |
| 507 |
] |
| 508 |
|
| 509 |
# the task should have the states is_held and is_retry |
| 510 |
assert task_a.state.is_held is True |
| 511 |
assert task_a_data_store.is_retry is True |
| 512 |
assert task_a_data_store.is_retry is True |
| 513 |
|
| 514 |
# set a custom output |
| 515 |
await run_cmd( |
| 516 |
set_prereqs_and_outputs(schd, [task_a.identity], [], ['x']) |
| 517 |
) |
| 518 |
|
| 519 |
# the is_held and is_retry statuses should be unchanged |
| 520 |
assert task_a.state.is_held is True |
| 521 |
assert task_a_data_store.is_retry is True |
| 522 |
assert task_a_data_store.is_retry is True |
| 523 |
|
| 524 |
# manually complete the task |
| 525 |
await run_cmd( |
| 526 |
set_prereqs_and_outputs( |
| 527 |
schd, [task_a.identity], [], [TASK_OUTPUT_SUCCEEDED] |
| 528 |
) |
| 529 |
) |
| 530 |
await schd.update_data_structure() |
| 531 |
|
| 532 |
# the is_held and is_retry states should have been cleared |
| 533 |
assert task_a.state.is_held is False |
| 534 |
assert task_a_data_store.is_retry is False |
| 535 |
assert task_a_data_store.is_retry is False |