Browse Source

AMBARI-16199. Improve output of command execution retry logic on agents. (Daniel Gergely via stoader)

Daniel Gergely 9 years ago
parent
commit
d32f61c9e0

+ 6 - 1
ambari-agent/src/main/python/ambari_agent/ActionQueue.py

@@ -171,7 +171,7 @@ class ActionQueue(threading.Thread):
                 t.start()
                 t.start()
               else:
               else:
                 self.process_command(command)
                 self.process_command(command)
-                break;
+                break
               pass
               pass
             pass
             pass
         except (Queue.Empty):
         except (Queue.Empty):
@@ -315,12 +315,17 @@ class ActionQueue(threading.Thread):
         if delay > retryDuration:
         if delay > retryDuration:
           delay = retryDuration
           delay = retryDuration
         retryDuration -= delay  # allow one last attempt
         retryDuration -= delay  # allow one last attempt
+        commandresult['stderr'] += "\n\nCommand failed. Retrying command execution ...\n\n"
         logger.info("Retrying command id {cid} after a wait of {delay}".format(cid=taskId, delay=delay))
         logger.info("Retrying command id {cid} after a wait of {delay}".format(cid=taskId, delay=delay))
         time.sleep(delay)
         time.sleep(delay)
         continue
         continue
       else:
       else:
         break
         break
 
 
+    # final result to stdout
+    commandresult['stdout'] += '\n\nCommand completed successfully!\n' if status == self.COMPLETED_STATUS else '\n\nCommand failed after ' + str(numAttempts) + ' tries\n'
+    logger.info('Command {cid} completed successfully!'.format(cid=taskId) if status == self.COMPLETED_STATUS else 'Command {cid} failed after {attempts} tries'.format(cid=taskId, attempts=numAttempts))
+
     roleResult = self.commandStatuses.generate_report_template(command)
     roleResult = self.commandStatuses.generate_report_template(command)
     roleResult.update({
     roleResult.update({
       'stdout': commandresult['stdout'],
       'stdout': commandresult['stdout'],

+ 7 - 7
ambari-agent/src/test/python/ambari_agent/TestActionQueue.py

@@ -366,7 +366,7 @@ class TestActionQueue(TestCase):
     expected = {'status': 'COMPLETED',
     expected = {'status': 'COMPLETED',
                 'configurationTags': {'global': {'tag': 'v123'}},
                 'configurationTags': {'global': {'tag': 'v123'}},
                 'stderr': 'stderr',
                 'stderr': 'stderr',
-                'stdout': 'out',
+                'stdout': 'out\n\nCommand completed successfully!\n',
                 'clusterName': u'cc',
                 'clusterName': u'cc',
                 'structuredOut': '""',
                 'structuredOut': '""',
                 'roleCommand': u'CUSTOM_COMMAND',
                 'roleCommand': u'CUSTOM_COMMAND',
@@ -539,7 +539,7 @@ class TestActionQueue(TestCase):
     configname = os.path.join(tempdir, 'config.json')
     configname = os.path.join(tempdir, 'config.json')
     expected = {'status': 'COMPLETED',
     expected = {'status': 'COMPLETED',
                 'stderr': 'stderr',
                 'stderr': 'stderr',
-                'stdout': 'out',
+                'stdout': 'out\n\nCommand completed successfully!\n',
                 'clusterName': u'cc',
                 'clusterName': u'cc',
                 'structuredOut': '""',
                 'structuredOut': '""',
                 'roleCommand': u'INSTALL',
                 'roleCommand': u'INSTALL',
@@ -578,7 +578,7 @@ class TestActionQueue(TestCase):
       # check report
       # check report
     expected = {'status': 'FAILED',
     expected = {'status': 'FAILED',
                 'stderr': 'stderr',
                 'stderr': 'stderr',
-                'stdout': 'out',
+                'stdout': 'out\n\nCommand completed successfully!\n\n\nCommand failed after 1 tries\n',
                 'clusterName': u'cc',
                 'clusterName': u'cc',
                 'structuredOut': '""',
                 'structuredOut': '""',
                 'roleCommand': u'INSTALL',
                 'roleCommand': u'INSTALL',
@@ -610,7 +610,7 @@ class TestActionQueue(TestCase):
     # check report
     # check report
     expected = {'status': 'COMPLETED',
     expected = {'status': 'COMPLETED',
                 'stderr': 'stderr',
                 'stderr': 'stderr',
-                'stdout': 'out',
+                'stdout': 'out\n\nCommand completed successfully!\n\n\nCommand failed after 1 tries\n\n\nCommand completed successfully!\n',
                 'clusterName': 'clusterName',
                 'clusterName': 'clusterName',
                 'structuredOut': '""',
                 'structuredOut': '""',
                 'roleCommand': 'UPGRADE',
                 'roleCommand': 'UPGRADE',
@@ -654,7 +654,7 @@ class TestActionQueue(TestCase):
     expected = {'status': 'COMPLETED',
     expected = {'status': 'COMPLETED',
                 'configurationTags': {'global': {'tag': 'v123'}},
                 'configurationTags': {'global': {'tag': 'v123'}},
                 'stderr': 'stderr',
                 'stderr': 'stderr',
-                'stdout': 'out',
+                'stdout': 'out\n\nCommand completed successfully!\n',
                 'clusterName': u'cc',
                 'clusterName': u'cc',
                 'structuredOut': '""',
                 'structuredOut': '""',
                 'roleCommand': u'CUSTOM_COMMAND',
                 'roleCommand': u'CUSTOM_COMMAND',
@@ -696,7 +696,7 @@ class TestActionQueue(TestCase):
     expected = {'status': 'COMPLETED',
     expected = {'status': 'COMPLETED',
                 'configurationTags': {'global': {'tag': 'v123'}},
                 'configurationTags': {'global': {'tag': 'v123'}},
                 'stderr': 'stderr',
                 'stderr': 'stderr',
-                'stdout': 'out',
+                'stdout': 'out\n\nCommand completed successfully!\n',
                 'clusterName': u'cc',
                 'clusterName': u'cc',
                 'structuredOut': '""',
                 'structuredOut': '""',
                 'roleCommand': u'CUSTOM_COMMAND',
                 'roleCommand': u'CUSTOM_COMMAND',
@@ -986,7 +986,7 @@ class TestActionQueue(TestCase):
       'status': 'FAILED'
       'status': 'FAILED'
     }
     }
 
 
-    times_arr = [8, 10, 14, 18, 22]
+    times_arr = [8, 10, 14, 18, 22, 26]
     if self.logger.isEnabledFor(logging.INFO):
     if self.logger.isEnabledFor(logging.INFO):
       times_arr.insert(0, 4)
       times_arr.insert(0, 4)
     time_mock.side_effect = times_arr
     time_mock.side_effect = times_arr