Skip to content

Commit 7a8d343

Browse files
mattp-dwoz
authored andcommitted
salt.key: optimize check_minion_cache
the loops involved dont use sets which seems to have an oversized impact on lookup performance. using sets for containment checks makes this function go 15x faster on a 50k dataset (from 30+ sec to 2~ sec).
1 parent 78880f2 commit 7a8d343

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

salt/key.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -474,21 +474,28 @@ def check_minion_cache(self, preserve_minions=None):
474474
Optionally, pass in a list of minions which should have their caches
475475
preserved. To preserve all caches, set __opts__['preserve_minion_cache']
476476
"""
477+
if self.opts.get("preserve_minion_cache", False):
478+
return
479+
477480
if preserve_minions is None:
478481
preserve_minions = []
482+
preserve_minions = set(preserve_minions)
483+
479484
keys = self.list_keys()
480-
minions = []
481-
for key, val in keys.items():
482-
minions.extend(val)
483-
if not self.opts.get("preserve_minion_cache", False):
484-
# we use a new cache instance here as we dont want the key cache
485-
cache = salt.cache.factory(self.opts)
486-
for bank in ["grains", "pillar"]:
487-
clist = cache.list(bank)
488-
if clist:
489-
for minion in clist:
490-
if minion not in minions and minion not in preserve_minions:
491-
cache.flush(bank, minion)
485+
486+
for val in keys.values():
487+
preserve_minions.update(val)
488+
489+
# we use a new cache instance here as we dont want the key cache
490+
cache = salt.cache.factory(self.opts)
491+
492+
for bank in ["grains", "pillar"]:
493+
clist = set(cache.list(bank))
494+
for minion in clist - preserve_minions:
495+
# pillar optionally encodes pillarenv in the key as minion:$pillarenv
496+
if ":" in minion and minion.split(":")[0] in preserve_minions:
497+
continue
498+
cache.flush(bank, minion)
492499

493500
def check_master(self):
494501
"""

0 commit comments

Comments
 (0)