salt icon indicating copy to clipboard operation
salt copied to clipboard

Salt minion sometime return empty ret dict and it fails if failhard set to True

Open Oloremo opened this issue 6 years ago • 15 comments

Description of Issue

So Salt has this logic for batch execution: https://github.com/saltstack/salt/blob/master/salt/cli/batch.py#L246-L259

I have an orchestration, with batch and failhard set to True and sometimes get this exception:

2020-02-28 14:59:44,383 [salt.state] [ERROR]  An exception occurred in this state: Traceback (most recent call last):
  File "/opt/behavox/salt/venv/lib/python3.6/site-packages/salt/state.py", line 1933, in call
    **cdata['kwargs'])
  File "/opt/behavox/salt/venv/lib/python3.6/site-packages/salt/loader.py", line 1951, in wrapper
    return f(*args, **kwargs)
  File "/opt/behavox/salt/venv/lib/python3.6/site-packages/salt/states/saltmod.py", line 331, in state
    cmd_ret = __salt__['saltutil.cmd'](tgt, fun, **cmd_kw)
  File "/opt/behavox/salt/venv/lib/python3.6/site-packages/salt/modules/saltutil.py", line 1503, in cmd
    client, tgt, fun, arg, timeout, tgt_type, ret, kwarg, **kwargs)
  File "/opt/behavox/salt/venv/lib/python3.6/site-packages/salt/modules/saltutil.py", line 1467, in _exec
    for ret_comp in _cmd(**cmd_kwargs):
  File "/opt/behavox/salt/venv/lib/python3.6/site-packages/salt/client/__init__.py", line 574, in cmd_batch
    for ret in batch.run():
  File "/opt/behavox/salt/venv/lib/python3.6/site-packages/salt/cli/batch.py", line 258, in run
    if self.opts.get('failhard') and data['retcode'] > 0:
KeyError: 'retcode'

So I dig in and printed the minion and data variables from this logic during error and the problem was that for one minion data is {'ret': {}}.

I checked this minions logs and see that it has finished its logic correctly and sent the results back to master:

2020-02-28 14:56:08,445 [salt.minion] [INFO] [JID: 20200228145436184480] Returning information for job: 20200228145436184480

Steps to Reproduce Issue

It's flapping and looks like only happening during the load.

Versions Report

Same for master and minion.

Salt Version:
           Salt: 2019.2.2

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.10.3
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.6.2
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 3.6.8 (default, Aug  7 2019, 17:28:10)
   python-gnupg: 0.4.5
         PyYAML: 3.11
          PyZMQ: 18.0.2
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.1

System Versions:
           dist: centos 7.5.1804 Core
         locale: UTF-8
        machine: x86_64
        release: 3.10.0-862.3.2.el7.x86_64
         system: Linux
        version: CentOS Linux 7.5.1804 Core

Oloremo avatar Feb 28 '20 18:02 Oloremo

After some discussion in Slack there was an idea that it could be a race condition in https://github.com/saltstack/salt/blob/master/salt/client/init.py#L1100

Oloremo avatar Feb 28 '20 19:02 Oloremo

So I guess this particular error could be fixed by something like:

if self.opts.get('failhard') and data.get('ret', {}) and data.get('retcode', 255) > 0:

Oloremo avatar Feb 28 '20 19:02 Oloremo

@Oloremo Thanks for the report. Have you been able to test the 3000 release to see if this particular bug is happening there as well? The fact that it is only happening sporadically will make it difficult to reproduce.

garethgreenaway avatar Mar 05 '20 01:03 garethgreenaway

Yes, it's hard to reproduce.

First of all, there are 2 different problems:

  1. Some kind of race in Salt minion return gather logic, we kinda assume it didn't fetch the last batch of returns sometimes(race). Can't confirm it yet.
  2. While 1) happens this logic in batch.py can't handle it properly and raise that exception.

I'm trying to find a reproducible scenario for this issue, right now I can catch it like 1 in 50 runs on a specific cluster.

Oloremo avatar Mar 05 '20 01:03 Oloremo

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Apr 04 '20 03:04 stale[bot]

Not stale

Oloremo avatar Apr 04 '20 10:04 Oloremo

Thank you for updating this issue. It is no longer marked as stale.

stale[bot] avatar Apr 04 '20 10:04 stale[bot]

Have you been able to test the 3000 release

Reproduced from time to time in 3000.3.

We heavily use orchestrate runner with batch. Batch is set with a percentage of 34%. It's ofter reproduces on bigger clusters, we don't see almost at all on ~10 nodes cluster. See it sometimes on 30 nodes cluster and actually reproduced more or less stable(like in 30% of times) on a 100 nodes cluster.

Oloremo avatar Jul 12 '20 13:07 Oloremo

We are running 3000.2 and is having same issue here. One of the application we manage has 50+ nodes and this issue has been witnessed multiple times. In the master log, we found the exeption:

KeyError: u'retcode'
Traceback (most recent call last):
  File "/bin/salt", line 9, in <module>
    load_entry_point('salt==3000.2', 'console_scripts', 'salt')()
  File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 530, in salt_main
    client.run()
  File "/usr/lib/python2.7/dist-packages/salt/cli/salt.py", line 66, in run
    self._run_batch()
  File "/usr/lib/python2.7/dist-packages/salt/cli/salt.py", line 284, in _run_batch
    for res in batch.run():
  File "/usr/lib/python2.7/dist-packages/salt/cli/batch.py", line 258, in run
    if self.opts.get('failhard') and data['retcode'] > 0

When this issue was witnessed, test.ping to minions also had multiple minion not returned. I don't see logs for that in master log. The list of minions that didn't return upon test.ping changed when re-run test.ping so it was likely not minion issue.

Our experience is that a restart of salt-master always fix this. Seems like something was broken with the master process at that moment and the master process can't fix itself.

seanzhang422 avatar Jul 24 '20 18:07 seanzhang422

Since the code in https://github.com/saltstack/salt/pull/59474 is not meant as a fix but instead a workaround that adds delays. this really needs to be looked at with more scrutiny to fix the race condition and not just work around it. can this be reassigned to someone for an actual fix?

whytewolf avatar May 10 '21 16:05 whytewolf

@whytewolf Not sure if that is the question to me, we tried the best we could to at least highlight the issue but we're unable to find a root cause and hoping for some Salt folks help and\or guidance.

Oloremo avatar May 10 '21 18:05 Oloremo

@whytewolf Not sure if that is the question to me, we tried the best we could to at least highlight the issue but we're unable to find a root cause and hoping for some Salt folks help and\or guidance.

No it wasn't to you, was more to @sagetherage as part of the triage process.

whytewolf avatar May 10 '21 18:05 whytewolf

This bug could potentially happen for any batch commands run. salt.cli.Batch.run is creating multiple generators (via salt.client.LocalClient.cmd_iter_no_block) which call salt.utils.event.SaltEvent.get_event on the same SaltEvent object. Each of these get_event calls has a different match clause. We might be iterating over a generator of one batch job when a return from another batch job arrives. In this situation SaltEvent will quietly filter out the event and return None. The crux of this issue is that we are using the same underlying SaltEvent object so all of the get_event calls are sharing the same underlying connection to the event publisher.

I see two potential paths forward. 1) refactor salt.cli.Batch.run to rely on a single SaltEvent.get_event call and a broader match string. 2) refactor LocalClient to subscribe to events and still use a single SaltEvent object for get_returns_no_block 3) Use a different SaltEvent for get_event calls. This can be done by creating a new event object in LocalClient.get_returns_no_block.

Option 3 was an easy enough change and test for me to try and it resolves the problem. I still need to look into options 1 and 2.

dwoz avatar May 22 '21 12:05 dwoz

Turns out we are subscribed to the events and the single event object is caching returns for the jobs we are looking for but we're also calling close on the event object which will discard any cached events. This behavior was introduced by https://github.com/saltstack/salt/commit/c59a5adb0d0480bbe2ed42c361d95fac2af2718b.

dwoz avatar May 23 '21 20:05 dwoz

Fix released in v3003.1 and ported to the master branch

sagetherage avatar Jun 23 '21 19:06 sagetherage