| 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 Broadcast Manager.""" |
| 18 |
|
| 19 |
import asyncio |
| 20 |
|
| 21 |
import pytest |
| 22 |
|
| 23 |
from cylc.flow import commands |
| 24 |
from cylc.flow.cycling.integer import IntegerInterval, IntegerPoint |
| 25 |
from cylc.flow.cycling.iso8601 import ISO8601Interval, ISO8601Point |
| 26 |
from cylc.flow.task_state import TASK_STATUS_FAILED |
| 27 |
|
| 28 |
|
| 29 |
async def test_reject_valid_broadcast_is_remote_clash_with_config( |
| 30 |
one_conf, flow, start, scheduler, log_filter |
| 31 |
): |
| 32 |
"""`put_broadcast` gracefully rejects invalid broadcast: |
| 33 |
|
| 34 |
Existing config = [task][remote]host = foo |
| 35 |
Broadcast = [task]platform = bar |
| 36 |
|
| 37 |
https://github.com/cylc/cylc-flow/issues/6693 |
| 38 |
""" |
| 39 |
one_conf.update({'runtime': {'root': {'platform': 'foo'}}}) |
| 40 |
wid = flow(one_conf) |
| 41 |
schd = scheduler(wid) |
| 42 |
async with start(schd): |
| 43 |
bc_mgr = schd.broadcast_mgr |
| 44 |
good, bad = bc_mgr.put_broadcast( |
| 45 |
point_strings=['1'], |
| 46 |
namespaces=['one'], |
| 47 |
settings=[{'remote': {'host': 'bar'}}] |
| 48 |
) |
| 49 |
|
| 50 |
# the error should be reported in the workflow log |
| 51 |
assert log_filter(contains='Cannot apply broadcast') |
| 52 |
assert bc_mgr.broadcasts == {'1': {}} |
| 53 |
|
| 54 |
# the bad setting should be reported |
| 55 |
assert good == [] |
| 56 |
assert bad == { |
| 57 |
'settings': [ |
| 58 |
{'remote': {'host': 'bar'}}, |
| 59 |
] |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
async def test_reject_valid_broadcast_is_remote_clash_with_broadcast( |
| 64 |
one_conf, flow, start, scheduler, log_filter |
| 65 |
): |
| 66 |
"""`put_broadcast` gracefully rejects invalid broadcast: |
| 67 |
|
| 68 |
Existing Broadcast = [task][remote]host = foo |
| 69 |
New Broadcast = [task]platform = bar |
| 70 |
|
| 71 |
https://github.com/cylc/cylc-flow/pull/6711/files#r2033457964 |
| 72 |
""" |
| 73 |
schd = scheduler(flow(one_conf)) |
| 74 |
async with start(schd): |
| 75 |
bc_mgr = schd.broadcast_mgr |
| 76 |
_, bad = bc_mgr.put_broadcast( |
| 77 |
point_strings=['1'], |
| 78 |
namespaces=['one'], |
| 79 |
settings=[{'remote': {'host': 'bar'}}] |
| 80 |
) |
| 81 |
assert bad == {} # broadcast should be successful |
| 82 |
|
| 83 |
# this should not be allowed, if it is the scheduler will crash |
| 84 |
# when unpaused: |
| 85 |
good, bad = bc_mgr.put_broadcast( |
| 86 |
point_strings=['1'], |
| 87 |
namespaces=['one'], |
| 88 |
settings=[{'platform': 'foo'}] |
| 89 |
) |
| 90 |
|
| 91 |
# the error should be reported in the workflow log |
| 92 |
assert log_filter(contains='Cannot apply broadcast') |
| 93 |
assert bc_mgr.broadcasts == {'1': {'one': {'remote': {'host': 'bar'}}}} |
| 94 |
|
| 95 |
# the bad setting should be reported |
| 96 |
assert good == [] |
| 97 |
assert bad == { |
| 98 |
'settings': [ |
| 99 |
{'platform': 'foo'}, |
| 100 |
] |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.parametrize('cycling_mode', ('integer', 'gregorian', '360_day')) |
| 105 |
async def test_broadcast_expire_limit( |
| 106 |
cycling_mode, |
| 107 |
flow, |
| 108 |
scheduler, |
| 109 |
run, |
| 110 |
complete, |
| 111 |
capcall, |
| 112 |
): |
| 113 |
"""Test automatic broadcast expiry. |
| 114 |
|
| 115 |
To prevent broadcasts from piling up and causing a memory leak, we expire |
| 116 |
(aka clear) them. |
| 117 |
|
| 118 |
The broadcast expiry limit is the oldest active cycle MINUS the longest |
| 119 |
cycling sequence. |
| 120 |
|
| 121 |
See https://github.com/cylc/cylc-flow/pull/6964 |
| 122 |
""" |
| 123 |
# capture broadcast expiry calls |
| 124 |
_expires = capcall('cylc.flow.broadcast_mgr.BroadcastMgr.expire_broadcast') |
| 125 |
|
| 126 |
def expires(): |
| 127 |
"""Return a list of the cycle limit expired since the last call.""" |
| 128 |
ret = [x[0][1] for x in _expires] |
| 129 |
_expires.clear() |
| 130 |
return ret |
| 131 |
|
| 132 |
def cycle(number): |
| 133 |
"""Return a cycle point object in the relevant format.""" |
| 134 |
if cycling_mode == 'integer': |
| 135 |
return IntegerPoint(str(number)) |
| 136 |
else: |
| 137 |
return ISO8601Point(f'000{number}') |
| 138 |
|
| 139 |
def interval(number): |
| 140 |
"""Return an integer object in the relevant format.""" |
| 141 |
if cycling_mode == 'integer': |
| 142 |
return IntegerInterval(sequence(number)) |
| 143 |
else: |
| 144 |
return ISO8601Interval(sequence(number)) |
| 145 |
|
| 146 |
def sequence(number): |
| 147 |
"""Return a sequence string in the relevant format.""" |
| 148 |
if cycling_mode == 'integer': |
| 149 |
return f'P{number}' |
| 150 |
else: |
| 151 |
return f'P{number}Y' |
| 152 |
|
| 153 |
# a workflow with a sequential task |
| 154 |
id_ = flow({ |
| 155 |
'scheduler': { |
| 156 |
'cycle point format': 'CCYY' |
| 157 |
} if cycling_mode != 'integer' else {}, |
| 158 |
|
| 159 |
'scheduling': { |
| 160 |
'cycling mode': cycling_mode, |
| 161 |
'initial cycle point': cycle(1), |
| 162 |
'graph': { |
| 163 |
# the sequence with the sequential task |
| 164 |
sequence(1): f'a[-{sequence(1)}] => a', |
| 165 |
# a longer sequence to make the offset more interesting |
| 166 |
sequence(3): 'a', |
| 167 |
} |
| 168 |
} |
| 169 |
}) |
| 170 |
schd = scheduler(id_, paused_start=False) |
| 171 |
|
| 172 |
async with run(schd): |
| 173 |
# the longest cycling sequence has a step of "3" |
| 174 |
assert schd.config.interval_of_longest_sequence == interval(3) |
| 175 |
|
| 176 |
# no broadcast expires should happen on startup |
| 177 |
assert expires() == [] |
| 178 |
|
| 179 |
# when a cycle closes, auto broadcast expiry should happen |
| 180 |
# NOTE: datetimes cannot be negative, so this expiry will be skipped |
| 181 |
# for datetimetime cycling workflows |
| 182 |
await complete(schd, f'{cycle(1)}/a') |
| 183 |
assert expires() in ([], [cycle(-1)]) |
| 184 |
|
| 185 |
await complete(schd, f'{cycle(2)}/a') |
| 186 |
assert expires() == [cycle(0)] |
| 187 |
|
| 188 |
await complete(schd, f'{cycle(3)}/a') |
| 189 |
assert expires() == [cycle(1)] |
| 190 |
|
| 191 |
|
| 192 |
async def test_broadcast_expiry_async( |
| 193 |
one_conf, flow, scheduler, run, complete, capcall |
| 194 |
): |
| 195 |
"""Test auto broadcast expiry with async workflows. |
| 196 |
|
| 197 |
Auto broadcast expiry should not happen in async workflows as there is only |
| 198 |
one cycle so it doesn't make sense. |
| 199 |
|
| 200 |
See https://github.com/cylc/cylc-flow/pull/6964 |
| 201 |
""" |
| 202 |
# capture broadcast expiry calls |
| 203 |
expires = capcall('cylc.flow.broadcast_mgr.BroadcastMgr.expire_broadcast') |
| 204 |
|
| 205 |
id_ = flow(one_conf) |
| 206 |
schd = scheduler(id_, paused_start=False) |
| 207 |
|
| 208 |
async with run(schd): |
| 209 |
# this is an async workflow so the longest cycling interval is a |
| 210 |
# null interval |
| 211 |
assert ( |
| 212 |
schd.config.interval_of_longest_sequence |
| 213 |
== IntegerInterval.get_null() |
| 214 |
) |
| 215 |
await complete(schd) |
| 216 |
|
| 217 |
# no auto-expiry should take place |
| 218 |
assert expires == [] |
| 219 |
|
| 220 |
|
| 221 |
async def test_broadcast_old_cycle(flow, scheduler, run, complete): |
| 222 |
"""It should not expire broadcasts whilst the scheduler is paused. |
| 223 |
|
| 224 |
This tests the use case of broadcasting to a historical cycle (whilst the |
| 225 |
workflow is paused) before triggering it to run to ensure that the |
| 226 |
broadcast is not expired before the operator is able to run the trigger |
| 227 |
command. |
| 228 |
|
| 229 |
For context, see https://github.com/cylc/cylc-flow/pull/6499 and |
| 230 |
https://github.com/cylc/cylc-flow/pull/6192#issuecomment-2486785465 |
| 231 |
""" |
| 232 |
id_ = flow({ |
| 233 |
'scheduling': { |
| 234 |
'initial cycle point': '1', |
| 235 |
'cycling mode': 'integer', |
| 236 |
'graph': { |
| 237 |
'P1': 'a[-P1] => a', |
| 238 |
}, |
| 239 |
}, |
| 240 |
}) |
| 241 |
schd = scheduler(id_, paused_start=False) |
| 242 |
async with run(schd): |
| 243 |
# issue a broadcast into the first cycle |
| 244 |
schd.broadcast_mgr.put_broadcast( |
| 245 |
point_strings=['1'], |
| 246 |
namespaces=['a'], |
| 247 |
settings=[{'environment': {'ANSWER': '42'}}] |
| 248 |
) |
| 249 |
assert list(schd.broadcast_mgr.broadcasts) == ['1'] |
| 250 |
|
| 251 |
# the broadcast should expire after the workflow passes cycle "3" |
| 252 |
await complete(schd, '3/a') |
| 253 |
assert list(schd.broadcast_mgr.broadcasts) == [] |
| 254 |
|
| 255 |
# pause the workflow |
| 256 |
await commands.run_cmd(commands.pause(schd)) |
| 257 |
|
| 258 |
# issue a broadcast into the first cycle (now behind the broadcast |
| 259 |
# expire point) |
| 260 |
schd.broadcast_mgr.put_broadcast( |
| 261 |
point_strings=['1'], |
| 262 |
namespaces=['a'], |
| 263 |
settings=[{'simulation': {'fail cycle points': '1'}}] |
| 264 |
) |
| 265 |
|
| 266 |
# this should not be expired whilst the scheduler is paused |
| 267 |
await schd._main_loop() # run one iteration of the main loop |
| 268 |
assert list(schd.broadcast_mgr.broadcasts) == ['1'] |
| 269 |
|
| 270 |
# trigger the first cycle and resume the workflow |
| 271 |
await commands.run_cmd(commands.force_trigger_tasks(schd, ['1'], [])) |
| 272 |
await commands.run_cmd(commands.resume(schd)) |
| 273 |
|
| 274 |
# the broadcast should still be there |
| 275 |
await schd._main_loop() # run one iteration of the main loop |
| 276 |
assert list(schd.broadcast_mgr.broadcasts) == ['1'] |
| 277 |
|
| 278 |
# and should take effect |
| 279 |
a_1 = schd.pool._get_task_by_id('1/a') |
| 280 |
async with asyncio.timeout(5): |
| 281 |
while True: |
| 282 |
await asyncio.sleep(0.1) |
| 283 |
if a_1.state(TASK_STATUS_FAILED): |
| 284 |
break |