Master failed to save job cache file: "Could not write job invocation cache file: [Errno 2] No such file or directory"
Hello!
Here is Salt version info:
[root@qa212 salt]# salt --versions
Salt: 2015.5.3
Python: 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
Jinja2: 2.2.1
M2Crypto: 0.20.2
msgpack-python: 0.4.6
msgpack-pure: Not Installed
pycrypto: 2.0.1
libnacl: Not Installed
PyYAML: 3.09
ioflo: Not Installed
PyZMQ: 14.3.1
RAET: Not Installed
ZMQ: 3.2.5
Mako: Not Installed
Tornado: Not Installed
In our code we heavily rely on LocalClient().get_cache_returns() when we check if particular Job was finished on minions or not. So today it happened that we didn't get any returns from cache even after all jobs were completed on all minions.
After some investigation it was discovered that master failed to create cache file and dropped the following message in its log:
2015-10-29 09:57:51,479 [salt.loaded.int.returner.local_cache][WARNING][83620] Could not write job invocation cache file: [Errno 2] No such file or directory: '/var/cache/salt/master/jobs/da/f078df7329ccb5496667601d450d22/.load.p'
Unfortunately this issue is hard to reproduce and we saw it only once so we didn't prepare our environment and I have only minion debug logs for investigation.
Here what I've found.
At some point we sent a very simple job to all minions:
2015-10-29 09:57:47,546 [salt.minion][INFO][109772] User root Executing command state.sls with jid 20151029095746578949
2015-10-29 09:57:47,547 [salt.minion][DEBUG][109772] Command details {'tgt_type': 'list', 'jid': '20151029095746578949', 'tgt': ['qa212', 'qa213', 'qa211', 'qa214', 'qa215'], 'ret': '', 'user': 'root', 'arg': ['some_service.some_state'], 'fun': 'state.sls'}
2015-10-29 09:57:47,558 [salt.minion][INFO][109911] Starting a new job with PID 109911
After some time we see that all minions returned results to master:
[root@qa212 ~]# salt \* cmd.run 'grep -r "Returning information for job: 20151029095746578949" /var/log/salt/minion'
qa211:
2015-10-29 09:57:51,064 [salt.minion][INFO][91825] Returning information for job: 20151029095746578949
qa214:
2015-10-29 09:57:50,040 [salt.minion][INFO][115221] Returning information for job: 20151029095746578949
qa212:
2015-10-29 09:57:51,360 [salt.minion][INFO][85405] Returning information for job: 20151029095746578949
qa213:
2015-10-29 09:57:51,031 [salt.minion][INFO][109911] Returning information for job: 20151029095746578949
qa215:
2015-10-29 09:57:50,036 [salt.minion][INFO][7755] Returning information for job: 20151029095746578949
Also there are no exceptions and failures on minions side at this time so they are fine. So you can see that 120 msec after the last minion returned to master - he tried to save job results into cache but failed to do so.
Warning message leads us to salt/returners/local_cache.py:
def _jid_dir(jid):
'''
Return the jid_dir for the given job id
'''
jid = str(jid)
jhash = getattr(hashlib, __opts__['hash_type'])(jid).hexdigest()
return os.path.join(_job_dir(),
jhash[:2],
jhash[2:])
...
def save_load(jid, clear_load):
'''
Save the load to the specified jid
'''
jid_dir = _jid_dir(jid)
...
# Save the invocation information
try:
if not os.path.exists(jid_dir):
os.makedirs(jid_dir)
serial.dump(
clear_load,
salt.utils.fopen(os.path.join(jid_dir, LOAD_P), 'w+b')
)
except IOError as exc:
log.warning('Could not write job invocation cache file: {0}'.format(exc))
Here we see that first of all save_load called _jid_dir which returned the full path to cache of this job. Lets recreate what it did for the given jid 20151029095746578949:
>>> import hashlib
>>> hashlib.md5("20151029095746578949").hexdigest()
'daf078df7329ccb5496667601d450d22'
So as result it will return path /var/cache/salt/master/jobs/da/f078df7329ccb5496667601d450d22 .
A bit later we try to create this dir if it is absent:
if not os.path.exists(jid_dir):
os.makedirs(jid_dir)
For some reason we didn't create dir here because after all half of its path is absent:
[root@qa212 salt]# ls -l /var/cache/salt/master/jobs/da
total 0
Path jobs/da is here but jobs/da/f078df7329ccb5496667601d450d22 is missing.
After that section we try to actually put file .load.p and fail with OSError exception:
[Errno 2] No such file or directory: '/var/cache/salt/master/jobs/da/f078df7329ccb5496667601d450d22/.load.p'
I looked into develop branch and found that nothing was changed here accept this https://github.com/saltstack/salt/pull/26081 . So the issue is most likely to be present even for the latest code.
For me it looks like another one race condition when master processes fight for directory creation or something like that. What do you think about it? How it can be fixed?
Thank you in advance! =)
@vakulich, thanks for the report. I think this issue has been fixed and the fix should be available in 2015.8.1. Is it possible for you to confirm this?
Hello @jfindlay, I think this is still an issue in 2015.8.1.
I am seeing the ioerror periodically on a 2015.8.1 master when executing the salt runner 'jobs.print_job" either via the salt CLI tool or via the Python API.
What I would remark as strange about this issue is that I see it appear when querying for job ID 'X' and then all of a sudden see the error for a random job ID 'Y'. There seems to be a few changes to how the job is handled between 2015.5 and 2015.8 and while the ioerror is caught the details are not.
To see what the issue is I've added some of the output from the exception and this is what I see when the problem occurs:
Could not write out jid file for job 20151104030352429001 - [Errno 2] No such file or directory: '/var/cache/salt/master/jobs/f8/e5677a38a22446d64fbcededdbee75/jid'. Retrying.
In my case the salt master is running as a non-root user and has all the appropriate permissions in place for not running as root. When the above error occurs via "salt-run jobs.print_job <job_id>" it will loop spitting out the error, but it seems to eventually resolve itself in the sense that it breaks the loop on it's own.
Cheers!
Edit: Disregard the above. It was unrelated and an internal configuration issue that didn't play nice with some new changes in 2015.8 (not a Salt bug, more a side effect)
@jfindlay sorry but I cannot check it on 2015.8.1 right now.. In order to check it we need to upgrade the whole our infrastructure to new salt and run extensive testing on it, but currently we have no time for that.. =(
@vakulich, @trevor-h, thanks for the extra work on this issue. I have seen a similar problem, which was fixed by #28545, but this issue is more strange as in that case the directory was somehow concurrently created by another process between the check and the creation, whereas here it is not created at all. Perhaps it is concurrently removed by another process, but I am not familiar with the returner system to know whether this is plausible.
The solution here is to implement a retry timer in save_load similar to the one already present in prep_jid. If somebody wants to do this real quick, it should be easy to do. Otherwise I will try to get to it soon. :]
Hi @cachedout
Please review this PR #30699.
Thanks.