| 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 |
from contextlib import suppress |
| 18 |
|
| 19 |
from cylc.flow.cycling.integer import IntegerPoint |
| 20 |
from cylc.flow.data_store_mgr import ( |
| 21 |
TASK_PROXIES, |
| 22 |
) |
| 23 |
from cylc.flow.id import Tokens |
| 24 |
|
| 25 |
|
| 26 |
def increment_graph_window(schd, task): |
| 27 |
"""Increment the graph window about the active task.""" |
| 28 |
tokens = schd.tokens.duplicate(cycle='1', task=task) |
| 29 |
schd.data_store_mgr.increment_graph_window( |
| 30 |
tokens, |
| 31 |
IntegerPoint('1'), |
| 32 |
is_manual_submit=False, |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
def get_deltas(schd): |
| 37 |
"""Return the ids and graph-window values in the delta store. |
| 38 |
|
| 39 |
Note, call before get_n_window as this clears the delta store. |
| 40 |
|
| 41 |
Returns: |
| 42 |
(added, updated, pruned) |
| 43 |
|
| 44 |
""" |
| 45 |
# populate added deltas |
| 46 |
schd.data_store_mgr.gather_delta_elements( |
| 47 |
schd.data_store_mgr.added, |
| 48 |
'added', |
| 49 |
) |
| 50 |
# populate pruned deltas |
| 51 |
schd.data_store_mgr.prune_data_store() |
| 52 |
# Run depth finder |
| 53 |
schd.data_store_mgr.window_depth_finder() |
| 54 |
# populate updated deltas |
| 55 |
schd.data_store_mgr.gather_delta_elements( |
| 56 |
schd.data_store_mgr.updated, |
| 57 |
'updated', |
| 58 |
) |
| 59 |
return ( |
| 60 |
{ |
| 61 |
# added |
| 62 |
Tokens(tb_task_proxy.id)['task']: tb_task_proxy.graph_depth |
| 63 |
for tb_task_proxy in schd.data_store_mgr.deltas[TASK_PROXIES].added |
| 64 |
}, |
| 65 |
{ |
| 66 |
# updated |
| 67 |
Tokens(tb_task_proxy.id)['task']: tb_task_proxy.graph_depth |
| 68 |
for tb_task_proxy in schd.data_store_mgr.deltas[ |
| 69 |
TASK_PROXIES |
| 70 |
].updated |
| 71 |
# only include those updated nodes whose depths have been set |
| 72 |
if 'graph_depth' |
| 73 |
in {sub_field.name for sub_field, _ in tb_task_proxy.ListFields()} |
| 74 |
}, |
| 75 |
{ |
| 76 |
# pruned |
| 77 |
Tokens(id_)['task'] |
| 78 |
for id_ in schd.data_store_mgr.deltas[TASK_PROXIES].pruned |
| 79 |
}, |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
async def get_n_window(schd): |
| 84 |
"""Read out the graph window of the workflow.""" |
| 85 |
await schd.update_data_structure() |
| 86 |
data = schd.data_store_mgr.data[schd.data_store_mgr.workflow_id] |
| 87 |
return { |
| 88 |
t.name: t.graph_depth |
| 89 |
for t in data[TASK_PROXIES].values() |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
async def complete_task(schd, task): |
| 94 |
"""Mark a task as completed.""" |
| 95 |
schd.data_store_mgr.remove_pool_node(task, IntegerPoint('1')) |
| 96 |
|
| 97 |
|
| 98 |
def add_task(schd, task): |
| 99 |
"""Add a waiting task to the pool.""" |
| 100 |
schd.data_store_mgr.add_pool_node(task, IntegerPoint('1')) |
| 101 |
|
| 102 |
|
| 103 |
def get_graph_walk_cache(schd): |
| 104 |
"""Return the head task names of cached graph walks.""" |
| 105 |
# prune graph walk cache |
| 106 |
schd.data_store_mgr.prune_data_store() |
| 107 |
# fetch the cached walks |
| 108 |
n_window_node_walks = sorted( |
| 109 |
Tokens(task_id)['task'] |
| 110 |
for task_id in schd.data_store_mgr.n_window_node_walks |
| 111 |
) |
| 112 |
n_window_completed_walks = sorted( |
| 113 |
Tokens(task_id)['task'] |
| 114 |
for task_id in schd.data_store_mgr.n_window_completed_walks |
| 115 |
) |
| 116 |
# the IDs in set and keys of dict are only the same at n<2 window. |
| 117 |
assert n_window_node_walks == n_window_completed_walks |
| 118 |
return n_window_completed_walks |
| 119 |
|
| 120 |
|
| 121 |
async def test_increment_graph_window_blink(flow, scheduler, start): |
| 122 |
"""Test with a task which drifts in and out of the n-window. |
| 123 |
|
| 124 |
This workflow presents a fiendish challenge for the graph window algorithm. |
| 125 |
|
| 126 |
The test runs in the n=3 window and simulates running each task in the |
| 127 |
chain a - s one by one. The task "blink" is dependent on multiple tasks |
| 128 |
in the chain awkwardly spaced so that the "blink" task routinely |
| 129 |
disappears from the n-window, only to re-appear again later. |
| 130 |
|
| 131 |
The expansion of the window around the "blink" task is difficult to get |
| 132 |
right as it can be influenced by caches from previous graph walks. |
| 133 |
""" |
| 134 |
id_ = flow({ |
| 135 |
'scheduler': { |
| 136 |
'allow implicit tasks': 'True', |
| 137 |
}, |
| 138 |
'scheduling': { |
| 139 |
'cycling mode': 'integer', |
| 140 |
'initial cycle point': '1', |
| 141 |
'graph': { |
| 142 |
'R1': ''' |
| 143 |
# the "abdef" chain of tasks which run one after another |
| 144 |
a => b => c => d => e => f => g => h => i => j => k => l => |
| 145 |
m => n => o => p => q => r => s |
| 146 |
|
| 147 |
# these dependencies cause "blink" to disappear and |
| 148 |
# reappear at set intervals |
| 149 |
a => blink |
| 150 |
g => blink |
| 151 |
m => blink |
| 152 |
s => blink |
| 153 |
''', |
| 154 |
} |
| 155 |
} |
| 156 |
}) |
| 157 |
schd = scheduler(id_) |
| 158 |
|
| 159 |
# the tasks traversed via the "blink" task when... |
| 160 |
blink = { |
| 161 |
1: { |
| 162 |
# the blink task is n=1 |
| 163 |
'blink': 1, |
| 164 |
'a': 2, |
| 165 |
'g': 2, |
| 166 |
'm': 2, |
| 167 |
's': 2, |
| 168 |
'b': 3, |
| 169 |
'f': 3, |
| 170 |
'h': 3, |
| 171 |
'l': 3, |
| 172 |
'n': 3, |
| 173 |
'r': 3, |
| 174 |
}, |
| 175 |
2: { |
| 176 |
# the blink task is n=2 |
| 177 |
'blink': 2, |
| 178 |
'a': 3, |
| 179 |
'g': 3, |
| 180 |
'm': 3, |
| 181 |
's': 3, |
| 182 |
}, |
| 183 |
3: { |
| 184 |
# the blink task is n=3 |
| 185 |
'blink': 3, |
| 186 |
}, |
| 187 |
4: { |
| 188 |
# the blink task is n=4 |
| 189 |
}, |
| 190 |
} |
| 191 |
|
| 192 |
def advance(): |
| 193 |
"""Advance to the next task in the workflow. |
| 194 |
|
| 195 |
This works its way down the chain of tasks between "a" and "s" |
| 196 |
inclusive, yielding what the n-window should look like for this |
| 197 |
workflow at each step. |
| 198 |
|
| 199 |
Yields: |
| 200 |
tuple - (previous_task, active_task, n_window) |
| 201 |
|
| 202 |
previous_task: |
| 203 |
The task which has just "succeeded". |
| 204 |
active_task: |
| 205 |
The task which is about to run. |
| 206 |
n_window: |
| 207 |
Dictionary of {task_name: graph_depth} for the n=3 window. |
| 208 |
|
| 209 |
""" |
| 210 |
# the initial window on startup (minus the nodes traversed via "blink") |
| 211 |
window = { |
| 212 |
'a': 0, |
| 213 |
'b': 1, |
| 214 |
'c': 2, |
| 215 |
'd': 3, |
| 216 |
} |
| 217 |
# the tasks we will run in order |
| 218 |
letters = 'abcdefghijklmnopqrs' |
| 219 |
# the graph-depth of the "blink" task at each stage of the workflow |
| 220 |
blink_distances = [1] + [*range(2, 5), *range(3, 0, -1)] * 3 |
| 221 |
|
| 222 |
for ind, blink_distance in zip(range(len(letters)), blink_distances): |
| 223 |
previous_task = letters[ind - 1] if ind > 0 else None |
| 224 |
active_task = letters[ind] |
| 225 |
yield ( |
| 226 |
previous_task, |
| 227 |
active_task, |
| 228 |
{ |
| 229 |
# the tasks traversed via the "blink" task |
| 230 |
**blink[blink_distance], |
| 231 |
# the tasks in the main "abcdefg" chain |
| 232 |
**{key: abs(value) for key, value in window.items()}, |
| 233 |
} |
| 234 |
) |
| 235 |
|
| 236 |
# move each task in the "abcdef" chain down one |
| 237 |
window = {key: value - 1 for key, value in window.items()} |
| 238 |
# add the n=3 task in the "abcdef" chain into the window |
| 239 |
with suppress(IndexError): |
| 240 |
window[letters[ind + 4]] = 3 |
| 241 |
# pull out anything which is not supposed to be in the n=3 window |
| 242 |
window = { |
| 243 |
key: value |
| 244 |
for key, value in window.items() |
| 245 |
if abs(value) < 4 |
| 246 |
} |
| 247 |
|
| 248 |
async with start(schd): |
| 249 |
schd.data_store_mgr.set_graph_window_extent(3) |
| 250 |
await schd.update_data_structure() |
| 251 |
|
| 252 |
previous_n_window = {} |
| 253 |
for previous_task, active_task, expected_n_window in advance(): |
| 254 |
# mark the previous task as completed |
| 255 |
await complete_task(schd, previous_task) |
| 256 |
# add the next task to the pool |
| 257 |
add_task(schd, active_task) |
| 258 |
# run the graph window algorithm |
| 259 |
increment_graph_window(schd, active_task) |
| 260 |
# get the deltas which increment_graph_window created |
| 261 |
added, updated, pruned = get_deltas(schd) |
| 262 |
|
| 263 |
# compare the n-window in the store to what we were expecting |
| 264 |
n_window = await get_n_window(schd) |
| 265 |
assert n_window == expected_n_window |
| 266 |
|
| 267 |
# compare the deltas to what we were expecting |
| 268 |
if active_task != 'a': |
| 269 |
# skip the first task as this is complicated by startup logic |
| 270 |
assert added == { |
| 271 |
key: value |
| 272 |
for key, value in expected_n_window.items() |
| 273 |
if key not in previous_n_window |
| 274 |
} |
| 275 |
# Skip added as depth isn't updated |
| 276 |
# (the manager only updates those that need it) |
| 277 |
assert updated == { |
| 278 |
key: value |
| 279 |
for key, value in expected_n_window.items() |
| 280 |
if key not in added |
| 281 |
} |
| 282 |
assert pruned == { |
| 283 |
key |
| 284 |
for key in previous_n_window |
| 285 |
if key not in expected_n_window |
| 286 |
} |
| 287 |
|
| 288 |
previous_n_window = n_window |
| 289 |
|
| 290 |
|
| 291 |
async def test_window_resize_rewalk(flow, scheduler, start): |
| 292 |
"""The window resize method should wipe and rebuild the n-window.""" |
| 293 |
id_ = flow({ |
| 294 |
'scheduler': { |
| 295 |
'allow implicit tasks': 'true', |
| 296 |
}, |
| 297 |
'scheduling': { |
| 298 |
'graph': { |
| 299 |
'R1': 'a => b => c => d => e => f => g' |
| 300 |
} |
| 301 |
}, |
| 302 |
}) |
| 303 |
schd = scheduler(id_) |
| 304 |
async with start(schd): |
| 305 |
# start with an empty pool |
| 306 |
schd.pool.remove(schd.pool.get_tasks()[0]) |
| 307 |
|
| 308 |
# the n-window should be empty |
| 309 |
assert await get_n_window(schd) == {} |
| 310 |
|
| 311 |
# expand the window around 1/d |
| 312 |
add_task(schd, 'd') |
| 313 |
increment_graph_window(schd, 'd') |
| 314 |
|
| 315 |
# set the graph window to n=3 |
| 316 |
schd.data_store_mgr.set_graph_window_extent(3) |
| 317 |
assert set(await get_n_window(schd)) == { |
| 318 |
'a', 'b', 'c', 'd', 'e', 'f', 'g' |
| 319 |
} |
| 320 |
|
| 321 |
# set the graph window to n=1 |
| 322 |
schd.data_store_mgr.set_graph_window_extent(1) |
| 323 |
schd.data_store_mgr.window_resize_rewalk() |
| 324 |
assert set(await get_n_window(schd)) == { |
| 325 |
'c', 'd', 'e' |
| 326 |
} |
| 327 |
|
| 328 |
# set the graph window to n=2 |
| 329 |
schd.data_store_mgr.set_graph_window_extent(2) |
| 330 |
schd.data_store_mgr.window_resize_rewalk() |
| 331 |
assert set(await get_n_window(schd)) == { |
| 332 |
'b', 'c', 'd', 'e', 'f' |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
async def test_cache_pruning(flow, scheduler, start): |
| 337 |
"""It should remove graph walks from the cache when no longer needed. |
| 338 |
|
| 339 |
The algorithm caches graph walks for efficiency. This test is designed to |
| 340 |
ensure we don't introduce a memory leak by failing to clear cached walks |
| 341 |
at the correct point. |
| 342 |
""" |
| 343 |
id_ = flow({ |
| 344 |
'scheduler': { |
| 345 |
'allow implicit tasks': 'True', |
| 346 |
}, |
| 347 |
'scheduling': { |
| 348 |
'graph': { |
| 349 |
'R1': ''' |
| 350 |
# a chain of tasks |
| 351 |
a => b1 & b2 => c => d1 & d2 => e => f |
| 352 |
# force "a" to drift into an out of the window |
| 353 |
a => c |
| 354 |
a => e |
| 355 |
''' |
| 356 |
} |
| 357 |
}, |
| 358 |
}) |
| 359 |
schd = scheduler(id_) |
| 360 |
async with start(schd): |
| 361 |
schd.data_store_mgr.set_graph_window_extent(1) |
| 362 |
|
| 363 |
# work through this workflow, step by step checking the cached items... |
| 364 |
|
| 365 |
# active: a |
| 366 |
add_task(schd, 'a') |
| 367 |
increment_graph_window(schd, 'a') |
| 368 |
assert get_graph_walk_cache(schd) == ['a'] |
| 369 |
|
| 370 |
# active: b1, b2 |
| 371 |
await complete_task(schd, 'a') |
| 372 |
add_task(schd, 'b1') |
| 373 |
add_task(schd, 'b2') |
| 374 |
increment_graph_window(schd, 'b1') |
| 375 |
increment_graph_window(schd, 'b2') |
| 376 |
assert get_graph_walk_cache(schd) == ['a', 'b1', 'b2'] |
| 377 |
|
| 378 |
# active: c |
| 379 |
await complete_task(schd, 'b1') |
| 380 |
await complete_task(schd, 'b2') |
| 381 |
add_task(schd, 'c') |
| 382 |
increment_graph_window(schd, 'c') |
| 383 |
assert get_graph_walk_cache(schd) == ['a', 'b1', 'b2', 'c'] |
| 384 |
|
| 385 |
# active: d1, d2 |
| 386 |
await complete_task(schd, 'c') |
| 387 |
add_task(schd, 'd1') |
| 388 |
add_task(schd, 'd2') |
| 389 |
increment_graph_window(schd, 'd1') |
| 390 |
increment_graph_window(schd, 'd2') |
| 391 |
assert get_graph_walk_cache(schd) == ['c', 'd1', 'd2'] |
| 392 |
|
| 393 |
# active: e |
| 394 |
await complete_task(schd, 'd1') |
| 395 |
await complete_task(schd, 'd2') |
| 396 |
add_task(schd, 'e') |
| 397 |
increment_graph_window(schd, 'e') |
| 398 |
assert get_graph_walk_cache(schd) == ['d1', 'd2', 'e'] |
| 399 |
|
| 400 |
# active: f |
| 401 |
await complete_task(schd, 'e') |
| 402 |
add_task(schd, 'f') |
| 403 |
increment_graph_window(schd, 'f') |
| 404 |
assert get_graph_walk_cache(schd) == ['e', 'f'] |
| 405 |
|
| 406 |
# active: None |
| 407 |
await complete_task(schd, 'f') |
| 408 |
increment_graph_window(schd, 'f') |
| 409 |
assert get_graph_walk_cache(schd) == [] |