Skip to content

Commit

Permalink
Reduce fileclient.get_file latency by merging _file_find and _file_hash
Browse files Browse the repository at this point in the history
This commit uses the newly created _file_hash_and_stat master function.
This is especially useful if your master and minion servers are in another datacenter (in my case the ping latency between them is 10ms, which somehow makes it around 40ms to access full file metadata)
This time is now cut in half. Yay!
  • Loading branch information
ninja- committed Feb 13, 2017
1 parent 0f43fe6 commit 94c6238
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions salt/fileclient.py
Expand Up @@ -1275,10 +1275,10 @@ def __hash_and_stat_file(self, path, saltenv='base'):
hash_type = self.opts.get('hash_type', 'md5')
ret['hsum'] = salt.utils.get_hash(path, form=hash_type)
ret['hash_type'] = hash_type
return ret
return ret, list(os.stat(path))
load = {'path': path,
'saltenv': saltenv,
'cmd': '_file_hash'}
'cmd': '_file_hash_and_stat'}
return self.channel.send(load)

def hash_file(self, path, saltenv='base'):
Expand All @@ -1287,33 +1287,14 @@ def hash_file(self, path, saltenv='base'):
master file server prepend the path with salt://<file on server>
otherwise, prepend the file with / for a local file.
'''
return self.__hash_and_stat_file(path, saltenv)
return self.__hash_and_stat_file(path, saltenv)[0]

def hash_and_stat_file(self, path, saltenv='base'):
'''
The same as hash_file, but also return the file's mode, or None if no
mode data is present.
'''
hash_result = self.hash_file(path, saltenv)
try:
path = self._check_proto(path)
except MinionError as err:
if not os.path.isfile(path):
return hash_result, None
else:
try:
return hash_result, list(os.stat(path))
except Exception:
return hash_result, None
load = {'path': path,
'saltenv': saltenv,
'cmd': '_file_find'}
fnd = self.channel.send(load)
try:
stat_result = fnd.get('stat')
except AttributeError:
stat_result = None
return hash_result, stat_result
return self.__hash_and_stat_file(path, saltenv)

def list_env(self, saltenv='base'):
'''
Expand Down

0 comments on commit 94c6238

Please sign in to comment.