2016.3.4 executes grain sync on startup (triggered through reactors) before executing custom grains
Please see #35069 which seems to have stalled, but contains extensive log data which might point to a race condition.
Since 2016.3.1 my reactor synchronizing grains is not executed anymore. This leads to grains not being available on the first run of state.highstate on a new minion.
I am trying to get this ticket reevaluated since as a side-effect of #37511 all my production setups are now completely broken. When I bring up new nodes, services get assigned the wrong Consul cluster role due to missing grains and they can't be reconfigured on the second highstate run because service.dead can't be used to shut the wrongly assigned service down, since the state will fail unless the node had been misconfigured before. So now I'm stuck between a rock and a hard place where I can select which group of nodes I want to fail with my salt config: the wrongly configured ones or the correctly configured ones.
The reactors are set up like this:
I have the following reactor setup in /srv/reactor/sync_grains.sls:
sync_grains:
local.saltutil.sync_grains:
- tgt: {{ data['id'] }}
and the according setup in /etc/salt/master.d/saltshaker.conf:
reactor:
- 'salt/minion/*/start': # this was 'minion_start' before
- /srv/reactor/sync_grains.sls
With these settings, I am seeing the start tag from the minion come through, and then the return of the job running sync_grains also coming through on the master event bus.
minion_start {
"_stamp": "2016-11-08T14:05:36.641694",
"cmd": "_minion_event",
"data": "Minion salt.c.gtmanfred-1263.internal started at Tue Nov 8 14:05:36 2016",
"id": "salt.c.gtmanfred-1263.internal",
"pretag": null,
"tag": "minion_start"
}
salt/minion/salt.c.gtmanfred-1263.internal/start { <-- start here
"_stamp": "2016-11-08T14:05:36.652753",
"cmd": "_minion_event",
"data": "Minion salt.c.gtmanfred-1263.internal started at Tue Nov 8 14:05:36 2016",
"id": "salt.c.gtmanfred-1263.internal",
"pretag": null,
"tag": "salt/minion/salt.c.gtmanfred-1263.internal/start"
}
20161108140536671722 {
"_stamp": "2016-11-08T14:05:36.672998",
"minions": [
"salt.c.gtmanfred-1263.internal"
]
}
salt/job/20161108140536671722/new { <-- publish of the job to the zmq bus
"_stamp": "2016-11-08T14:05:36.673467",
"arg": [],
"fun": "saltutil.sync_grains",
"jid": "20161108140536671722",
"minions": [
"salt.c.gtmanfred-1263.internal"
],
"tgt": "salt.c.gtmanfred-1263.internal",
"tgt_type": "glob",
"user": "root"
}
salt/job/20161108140536671722/ret/salt.c.gtmanfred-1263.internal { <-- return of the sync_grains by the minion
"_stamp": "2016-11-08T14:05:36.844349",
"cmd": "_return",
"fun": "saltutil.sync_grains",
"fun_args": [],
"id": "salt.c.gtmanfred-1263.internal",
"jid": "20161108140536671722",
"retcode": 0,
"return": [],
"success": true
}
Can you give me any more information about this?
Thanks Daniel
With these settings, I am seeing the start tag from the minion come through
Note to self: That's why you don't do copy'n'paste reporting :). This ticket's title is way off. Sorry.
So, yes you do. But as noted in #35069 the grains are synced before custom grains are executed. I put an extensive analysis of the underlying issue in this comment on #35069. But I was unable to get anyone to look at that ticket again for months.
Ok, can you force a module_refresh with a sync_all instead of specifically sync_grains?
If you do a sync_all, it will do a full sync of grains, modules, states, etc, and then ends with running a module_refresh which should run all of the custom grains and check /etc/salt/grains.
sync_all_things:
local.saltutil.sync_all:
- tgt: {{ data['id'] }}
- kwarg:
refresh: True
as you can see here, it will do the sync_* of all the things, and if you pass refresh: True, it will refresh modules.
https://github.com/saltstack/salt/blob/develop/salt/modules/saltutil.py#L637
refresh_modules also refreshes grains and should execute all of your custom grains.
Ok, can you force a module_refresh with a sync_all instead of specifically sync_grains?
Cool, will do as soon as I'm back in front of a computer, in 2 hours or so, since right now I am only on my phone.
Still that behavior must have changed between the 2015.11 and the 2016.x releases, since before that, this setup worked for a long time on my end. :-/
If the work-around you pointed to works out, we should at least figure out where to document this behavior.
@gtmanfred as you can see from the commits above , I tested your suggestion. There is no change. The custom grains only become available on the second state.highstate on every minion. The reactor does not execute to sync the grains before executing the states.
I have not yet had time to sync up and check all of the logs like I did previously in #35069, but the logs back then showed that the grains get synced after the master rendered the pillars and states, which would explain the behavior I'm seeing where the grains appear to be there on the second highstate, right? I would assume that that's the culprit.
Can you verify that my current setup in the master config is correct?
# make sure that all minions run the roledir grain before their initial highstate
# http://docs.saltstack.com/en/latest/topics/reactor/#syncing-custom-types-on-minion-start
reactor:
- 'salt/minion/*/start':
- /srv/reactor/sync_all.sls
yup, that should be correct.
Let me know what happens in the logs
Update time.
Using the Vagrant Salt provisioner, I ran 27 installations using the bootstrap-salt.sh script and installing master and minion at the same time got the following results:
27 runs
fail (no minion response): 4
134 states changed (roles did not propagate from minion to master): 22
204 states changed (roles did propagate from minion to master): 1
In the 134 states changed case, the logs look like I previously described in #35069 where the custom grains don't get synchronized before the highstate starts running.
If I split the bootstrap-salt.sh invocation so it installs the master first and then the minion in a second script run, I can reliably get 204 states changed.
Imho, that and that I got one "successful" run above speaks to a race condition, but I couldn't reproduce it a second time with a single bootstrap-salt.sh invocation.
I am now working on making my Salt repository usable for other people by creating a script that will automatically create a valid SSL configuration for it. This way I hope that this ticket will become reproducible for y'all.
Interestingly, the work-around mentioned above of running salt twice in my Vagrantfile also fails regularly to sync states on minion_start. This is frustrating to debug to say the least.
So... as far as I can tell now, a minimal configuration with the reactor and custom grain as I built here: https://github.com/jdelic/salt37545 works as expected. But basically the same configuration with 150 states does not process the custom grain before executing the state.highstate.
Still digging...
@gtmanfred I am currently verifying that 2016.3.4 has solved this issue, because it seems to have disappeared. Currently, I would bet that #36720, which was backported for 2016.3.4, was the fix.
Awesome, thanks for reporting back!
Unfortunately, the issue seems to have been mitigated, but it still exists. Installing salt-master and salt-minion in a single step in Vagrant still leads to the race condition occurring: states are rendered by the master and executed on the minion before the master processed the return of the minion_start event (which has the evaluated custom grains). However, all of my development boxes behave correctly and evaluate the custom grains now if salt-master and salt-minion are installed separately from each other. That behavior changed/improved from 2016.3.3.
@skizunov I apologize for blatantly mention-spamming you on this issue, but you authored #36720 which I think might be connected to what I'm seeing. Basically, my Salt setup has a custom grain that is used to assign states to minions in top.sls. This custom grain seems to be evaluated only on the second call to state.highstate. I can see from the logs that the minion evaluates the custom grain but the master renders states before the custom grains have been processed by the master, therefor the minion evaluates only a subset of states. This does not happen all of the time though. This sounds like it could be directly connected to what you were working on in #36720. 2016.3.4 improved the situation somewhat, but didn't completely fix it. This is both hard to reproduce and difficult to debug for me, unfortunately :(. So I'm hoping you might have some insights to share?
@jdelic I can't think of anything at the top of my head. Your use case is quite different than mine, though. I have never used Vagrant. One thing I would like to point out is that the minion_start event is processed in parallel to whatever is going on in your minion after it connects (since the reactor architecture runs in its own process). If you want to guarantee sequential ordering, you can have the minion_start event fire into your own custom runner and have the runner create a job (eg state.apply some_state) after it runs a sync_all (or you can create a multi-function job that does both, one after the other). This is similar to what I do for my use case, the minion_start event would invoke a runner that would create a multi-func job with 4 functions, one of them being a state.apply.
If you don't want to explore this avenue, I would suggest you work with the SaltStack team to help them reproduce this issue. It is extremely difficult to solve something if they can't reproduce it. If I happen to run into a similar issue in the future myself, I will be sure to try to fix it.
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.