Skip to content

Commit 090b916

Browse files
authored
Fix find_cached_script to use already found cache_list (#755)
1 parent f695b36 commit 090b916

File tree

3 files changed

+78
-76
lines changed

3 files changed

+78
-76
lines changed

automation/script/cache_utils.py

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,26 @@ def prepare_cache_tags(i):
155155

156156
def search_cache(i, explicit_cached_tags):
157157
'''
158-
Search for cached script outputs based on prepared cache tags
158+
Prune cache_lists based on prepared cache tags
159159
'''
160-
search_tags = '-tmp'
161-
if len(explicit_cached_tags) > 0:
162-
search_tags += ',' + ','.join(explicit_cached_tags)
163160

164161
i['logger'].debug(
165162
i['recursion_spaces'] +
166-
' - Searching for cached script outputs with the following tags: {}'.format(search_tags))
163+
' - Pruning cache list outputs with the following tags: {}'.format(explicit_cached_tags))
167164

168-
r = i['self'].cache_action.access({
169-
'action': 'search',
170-
'target_name': 'cache',
171-
'tags': search_tags
172-
})
173-
if r['return'] > 0:
174-
return r
165+
cache_list = i['cache_list']
166+
167+
pruned_cache_list = [
168+
item
169+
for item in cache_list
170+
if set(explicit_cached_tags) <= set(item.meta.get('tags', []))
171+
]
175172

176-
return search_tags, r['list']
173+
return pruned_cache_list
177174

178175

179-
def apply_remembered_cache_selection(i, search_tags, found_cached_scripts):
176+
def apply_remembered_cache_selection(
177+
i, explicit_search_tags, found_cached_scripts):
180178
'''
181179
Apply remembered cache selection if any
182180
'''
@@ -189,7 +187,7 @@ def apply_remembered_cache_selection(i, search_tags, found_cached_scripts):
189187

190188
for selection in i['remembered_selections']:
191189
if selection['type'] == 'cache' and set(
192-
selection['tags'].split(',')) == set(search_tags.split(',')):
190+
selection['tags'].split(',')) == set(explicit_search_tags):
193191
tmp_version_in_cached_script = selection['cached_script'].meta.get(
194192
'version', '')
195193
skip_cached_script = check_versions(
@@ -205,7 +203,7 @@ def apply_remembered_cache_selection(i, search_tags, found_cached_scripts):
205203
found_cached_scripts = [selection['cached_script']]
206204
i['logger'].debug(
207205
i['recursion_spaces'] +
208-
' - Found remembered selection with tags "{}"!'.format(search_tags))
206+
' - Found remembered selection with tags "{}"!'.format(explicit_search_tags))
209207
return [selection['cached_script']]
210208

211209
return found_cached_scripts
@@ -376,6 +374,58 @@ def run_validate_cache_if_present(i, cached_script):
376374

377375
return r.get('version')
378376

377+
##############################################################################
378+
379+
380+
def find_cached_script(i):
381+
"""
382+
Internal automation function: find cached script
383+
384+
Args:
385+
(MLC input dict):
386+
387+
deps (dict): deps dict
388+
update_deps (dict): key matches "names" in deps
389+
390+
Returns:
391+
(MLC return dict):
392+
* return (int): return code == 0 if no error and >0 if error
393+
* (error) (str): error string if return>0
394+
"""
395+
# 1. Prepare cache tags
396+
# 2. If new_cache_entry, return empty
397+
# 3. Search cache
398+
# 4. Apply remembered cache selection
399+
# 5. Validate cached scripts
400+
401+
i['logger'].debug(
402+
i['recursion_spaces'] +
403+
' - Checking if script execution is already cached ...')
404+
405+
cached_tags, explicit_cached_tags = prepare_cache_tags(i)
406+
407+
if i['new_cache_entry']:
408+
i['logger'].debug(
409+
i['recursion_spaces'] +
410+
f' - New cache entry requested, skipping cache search.'
411+
)
412+
return {'return': 0, 'cached_tags': cached_tags,
413+
'search_tags': '', 'found_cached_scripts': []}
414+
415+
found_cached_scripts = search_cache(i, explicit_cached_tags)
416+
417+
found_cached_scripts = apply_remembered_cache_selection(
418+
i, explicit_cached_tags, found_cached_scripts)
419+
found_cached_scripts = validate_cached_scripts(i, found_cached_scripts)
420+
421+
search_tags = '-tmp'
422+
if len(explicit_cached_tags) > 0:
423+
search_tags += ',' + ','.join(explicit_cached_tags)
424+
425+
return {'return': 0, 'cached_tags': cached_tags,
426+
'search_tags': search_tags, 'found_cached_scripts': found_cached_scripts}
427+
428+
379429
##########################################################################
380430

381431

automation/script/module.py

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,7 @@ def _run(self, i):
574574

575575
cache_list = r['cache_list']
576576

577-
# print(list_of_found_scripts)
578577
meta = script_item.meta
579-
# print(meta)
580578
path = script_item.path
581579

582580
# Check min MLC version requirement
@@ -711,7 +709,7 @@ def _run(self, i):
711709
# const)
712710

713711
variations = script_item.meta.get('variations', {})
714-
state['docker'] = meta.get('docker', {})
712+
run_state['docker'] = meta.get('docker', {})
715713

716714
r = self._update_state_from_variations(
717715
i,
@@ -829,8 +827,8 @@ def _run(self, i):
829827
return r
830828

831829
if is_true(env.get('MLC_RUN_STATE_DOCKER', False)):
832-
if state.get('docker'):
833-
if is_false(state['docker'].get('run', True)):
830+
if run_state.get('docker'):
831+
if is_false(run_state['docker'].get('run', True)):
834832
logger.info(
835833
self.recursion_spaces +
836834
' - Skipping script::{} run as we are inside docker'.format(found_script_item))
@@ -853,7 +851,7 @@ def _run(self, i):
853851
'deps': []}
854852
return rr
855853

856-
elif is_false(state['docker'].get('real_run', True)):
854+
elif is_false(run_state['docker'].get('real_run', True)):
857855
logger.info(
858856
self.recursion_spaces +
859857
' - Doing fake run for script::{} as we are inside docker'.format(found_script_item))
@@ -946,10 +944,10 @@ def _run(self, i):
946944
#######################################################################
947945
# Check if the output of a selected script should be cached
948946
if cache:
949-
# TBD - need to reuse and prune cache_list instead of a new CM
950-
# search inside find_cached_script
951-
947+
# Checking if any of the found entry in cache_list has
948+
# matching_tags and is a valid cache_entry
952949
r = find_cached_script({'self': self,
950+
'cache_list': cache_list,
953951
'extra_recursion_spaces': extra_recursion_spaces,
954952
'add_deps_recursive': add_deps_recursive,
955953
'script_tags': script_tags,
@@ -1768,7 +1766,7 @@ def _run(self, i):
17681766
return {
17691767
'return': 1, 'error': 'MLC artifact format is wrong "{}" - no comma found'.format(found_script_item)}
17701768

1771-
cached_meta['associated_script_item_uid'] = found_script_item[x + 1:]
1769+
cached_meta['associated_script_item_uid'] = found_script_item[x + 1:].strip()
17721770

17731771
# Check if the cached entry is dependent on any path
17741772
if dependent_cached_path != '':
@@ -4425,52 +4423,6 @@ def relaxed_subset(v, variation_tags):
44254423
return True
44264424

44274425

4428-
##############################################################################
4429-
4430-
def find_cached_script(i):
4431-
"""
4432-
Internal automation function: find cached script
4433-
4434-
Args:
4435-
(MLC input dict):
4436-
4437-
deps (dict): deps dict
4438-
update_deps (dict): key matches "names" in deps
4439-
4440-
Returns:
4441-
(MLC return dict):
4442-
* return (int): return code == 0 if no error and >0 if error
4443-
* (error) (str): error string if return>0
4444-
"""
4445-
# 1. Prepare cache tags
4446-
# 2. If new_cache_entry, return empty
4447-
# 3. Search cache
4448-
# 4. Apply remembered cache selection
4449-
# 5. Validate cached scripts
4450-
4451-
i['logger'].debug(
4452-
i['recursion_spaces'] +
4453-
' - Checking if script execution is already cached ...')
4454-
4455-
cached_tags, explicit_cached_tags = prepare_cache_tags(i)
4456-
4457-
if i['new_cache_entry']:
4458-
i['logger'].debug(
4459-
i['recursion_spaces'] +
4460-
f' - New cache entry requested, skipping cache search.'
4461-
)
4462-
return {'return': 0, 'cached_tags': cached_tags,
4463-
'search_tags': '', 'found_cached_scripts': []}
4464-
4465-
search_tags, found_cached_scripts = search_cache(i, explicit_cached_tags)
4466-
found_cached_scripts = apply_remembered_cache_selection(
4467-
i, search_tags, found_cached_scripts)
4468-
found_cached_scripts = validate_cached_scripts(i, found_cached_scripts)
4469-
4470-
return {'return': 0, 'cached_tags': cached_tags,
4471-
'search_tags': search_tags, 'found_cached_scripts': found_cached_scripts}
4472-
4473-
44744426
##############################################################################
44754427
def enable_or_skip_script(meta, env):
44764428
"""
@@ -5457,9 +5409,9 @@ def update_state_from_meta(meta, env, state, const, const_state, run_state, i):
54575409
utils.merge_dicts({'dict1': const_state, 'dict2': c_meta.get(
54585410
'const_state', {}), 'append_lists': True, 'append_unique': True})
54595411
if c_meta.get('docker', {}):
5460-
if not state.get('docker', {}):
5461-
state['docker'] = {}
5462-
utils.merge_dicts({'dict1': state['docker'],
5412+
if not run_state.get('docker', {}):
5413+
run_state['docker'] = {}
5414+
utils.merge_dicts({'dict1': run_state['docker'],
54635415
'dict2': c_meta['docker'],
54645416
'append_lists': True,
54655417
'append_unique': True})

automation/script/script_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def prune_cache_for_selected_script(cache_list, selected_script):
357357

358358
return [
359359
c for c in cache_list
360-
if c.meta.get("associated_script_item_uid") == selected_uid
360+
if c.meta.get("associated_script_item_uid", '').strip() == selected_uid
361361
]
362362

363363

0 commit comments

Comments
 (0)