diff --git a/salt/manager/tools/sbin/so-push-drainer b/salt/manager/tools/sbin/so-push-drainer index aad62b5c7..beee274d9 100644 --- a/salt/manager/tools/sbin/so-push-drainer +++ b/salt/manager/tools/sbin/so-push-drainer @@ -67,6 +67,8 @@ TEXT_LIMIT = 500 # Lead-in salt puts on the comment of an orchestration step that raised. STEP_RAISED = 'An exception occurred in this state:' +NO_RETURN = 'no return received' + # salt-run --async reports the jid only in a log line (stderr by default). JID_RE = re.compile(r'salt/run/(\d{20})') @@ -236,6 +238,10 @@ def _lookup_jid(jid, log): def _minion_failure(minion_ret): + # What salt.state records for a minion it stopped waiting on: no reply to a saltutil.find_job + # poll within gather_job_timeout, though the state run may still be queued or running. + if minion_ret == {}: + return NO_RETURN if isinstance(minion_ret, dict): return '; '.join( '{}: {}'.format(state.get('__id__', state_key), _trim(state.get('comment', ''))) @@ -298,11 +304,21 @@ def _failed_steps(ret): return steps +def _lost_track(step): + changes = step.get('changes') + minion_rets = changes.get('ret') if isinstance(changes, dict) else None + if not isinstance(minion_rets, dict): + return False + failed = [text for text in map(_minion_failure, minion_rets.values()) if text] + return bool(failed) and all(text == NO_RETURN for text in failed) + + def _result_unknown(ret): - # A step that raised (e.g. salt-master restarted while it waited on a queued state run) - # never collected the minion's return, so the state run may still have completed. + # A step that raised (e.g. salt-master restarted while it waited on a queued state run) or + # stopped waiting never collected the minion's return, so the state run may still have completed. steps = _failed_steps(ret) - return bool(steps) and all(str(step.get('comment', '')).startswith(STEP_RAISED) for step in steps) + return bool(steps) and all( + str(step.get('comment', '')).startswith(STEP_RAISED) or _lost_track(step) for step in steps) def _recheck_delay(age): diff --git a/salt/manager/tools/sbin/so-push-drainer_test.py b/salt/manager/tools/sbin/so-push-drainer_test.py index 3f26d1822..062358100 100644 --- a/salt/manager/tools/sbin/so-push-drainer_test.py +++ b/salt/manager/tools/sbin/so-push-drainer_test.py @@ -88,6 +88,22 @@ RAISED_RET = _orch_ret(dict(REFRESH_STEP, **{ }, }), success=False) +LOST_RET = _orch_ret(dict(REFRESH_STEP, **{ + 'salt_|-apply_soc_1_|-apply_soc_1_|-state': { + '__id__': 'apply_soc_1', 'result': False, + 'changes': {'out': 'highstate', 'ret': {'manager_standalone': {}}}, + 'comment': 'Run failed on minions: manager_standalone', + }, +}), success=False) + +LOST_AND_FAILED_RET = _orch_ret({ + 'salt_|-apply_soc_1_|-apply_soc_1_|-state': { + '__id__': 'apply_soc_1', 'result': False, + 'changes': {'out': 'highstate', 'ret': {'manager_standalone': {}, 'sensor1_sensor': [CONFLICT]}}, + 'comment': 'Run failed on minions: manager_standalone, sensor1_sensor', + }, +}, success=False) + SUCCESS_RET = _orch_ret(dict(REFRESH_STEP, **{ 'salt_|-apply_telegraf_1_|-apply_telegraf_1_|-state': { '__id__': 'apply_telegraf_1', 'result': True, @@ -276,6 +292,13 @@ class TestResults(DrainerTestCase): self.assertEqual(drainer._minion_failure('Rendering SLS failed'), 'Rendering SLS failed') self.assertEqual(drainer._minion_failure(True), '') self.assertEqual(drainer._minion_failure({'a': {'result': True}}), '') + self.assertEqual(drainer._minion_failure({}), drainer.NO_RETURN) + + def test_orch_failures_lost(self): + self.assertEqual(drainer._orch_failures(LOST_RET), [ + 'apply_soc_1: Run failed on minions: manager_standalone', + 'manager_standalone: no return received', + ]) def test_orch_failures_conflict(self): failures = drainer._orch_failures(CONFLICT_RET) @@ -319,7 +342,17 @@ class TestResults(DrainerTestCase): def test_result_unknown(self): self.assertTrue(drainer._result_unknown(RAISED_RET)) - for ret in (CONFLICT_RET, STATE_FAIL_RET, SUCCESS_RET, ['No minions matched'], {MASTER: 'odd'}, + self.assertTrue(drainer._result_unknown(LOST_RET)) + changed = json.loads(json.dumps(LOST_RET)) + changed[MASTER]['return']['data'][MASTER]['salt_|-apply_soc_1_|-apply_soc_1_|-state']['changes']['ret'][ + 'sensor1_sensor'] = {'file_|-a_|-a_|-managed': {'result': True, 'changes': {'diff': 'x'}}} + self.assertTrue(drainer._result_unknown(changed)) + odd = _orch_ret({'salt_|-apply_soc_1_|-apply_soc_1_|-state': { + '__id__': 'apply_soc_1', 'result': False, 'changes': 'Run failed', 'comment': 'Run failed on minions', + }}, success=False) + self.assertFalse(drainer._result_unknown(odd)) + for ret in (CONFLICT_RET, STATE_FAIL_RET, SUCCESS_RET, LOST_AND_FAILED_RET, ['No minions matched'], + {MASTER: 'odd'}, {MASTER: {'return': {'data': {MASTER: ['Rendering SLS failed']}}, 'success': False}}): self.assertFalse(drainer._result_unknown(ret), ret) mixed = _orch_ret(dict(RAISED_RET[MASTER]['return']['data'][MASTER], @@ -361,6 +394,21 @@ class TestResults(DrainerTestCase): self.assertIn('AuthenticationError: Authentication error occurred.', self.logged('warning')) self.assertNotIn('6_unknown', self.logged('error')) + def test_check_dispatched_lost(self): + now = time.time() + results = {'1_lost': LOST_RET, '2_lost_and_failed': LOST_AND_FAILED_RET} + paths = [self.record(jid, 60, now) for jid in results] + with patch.object(drainer, '_lookup_jid', side_effect=lambda jid, log: results[jid]): + drainer._check_dispatched(self.log, now) + + for path in paths: + self.assertFalse(os.path.exists(path), path) + self.assertIn('push result unknown jid=1_lost', self.logged('warning')) + self.assertIn('manager_standalone: no return received', self.logged('warning')) + self.assertNotIn('1_lost', self.logged('error')) + self.assertIn('push failed jid=2_lost_and_failed', self.logged('error')) + self.assertIn('is running as PID 372218', self.logged('error')) + def test_check_dispatched_survives_bad_result(self): now = time.time() bad = self.record('1_bad', 60, now)