From 4778bc73654461c46cd3e8d7656bcc2ea0960e04 Mon Sep 17 00:00:00 2001 From: Scott Thrasher Date: Thu, 21 Jul 2016 14:31:41 -0700 Subject: [PATCH 1/3] Speed up /jobs for salt-api when run under cherrypy. Currently, if a jid is supplied, two runners are run (both jobs.lookup_jid and jobs.list_job). This is unnecessary because the return from jobs.list_job contains all of the information from jobs.lookup_jid as well. This change runs only jobs.list_job in this case and builds an appropriate return from the output, netting an ~ 40% decrease in response time for this endpoint when a jid is supplied. --- salt/netapi/rest_cherrypy/app.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/salt/netapi/rest_cherrypy/app.py b/salt/netapi/rest_cherrypy/app.py index 6346771153..05a55755f7 100644 --- a/salt/netapi/rest_cherrypy/app.py +++ b/salt/netapi/rest_cherrypy/app.py @@ -1319,29 +1319,21 @@ class Jobs(LowDataAdapter): ''' lowstate = [{ 'client': 'runner', - 'fun': 'jobs.lookup_jid' if jid else 'jobs.list_jobs', + 'fun': 'jobs.list_job' if jid else 'jobs.list_jobs', 'jid': jid, }] - if jid: - lowstate.append({ - 'client': 'runner', - 'fun': 'jobs.list_job', - 'jid': jid, - }) - cherrypy.request.lowstate = lowstate job_ret_info = list(self.exec_lowstate( token=cherrypy.session.get('token'))) ret = {} if jid: - job_ret, job_info = job_ret_info - ret['info'] = [job_info] + ret['info'] = job_ret_info[0] + ret['return'] = [dict((k, job_ret_info[0]['Result'][k]['return']) for k in job_ret_info[0]['Result'])] else: - job_ret = job_ret_info[0] + ret['return'] = job_ret_info[0] - ret['return'] = [job_ret] return ret From 78dd629f09fde8e9938458cd24512b6d12b89b72 Mon Sep 17 00:00:00 2001 From: Scott Thrasher Date: Wed, 31 Aug 2016 14:57:29 -0700 Subject: [PATCH 2/3] Fix compat issues with /jobs return values. * ensure that both the return and info keys are lists. * When the Result key doesn't match the expected shape, return an empty list of an empty dict (this matches existing behavior when looking for a non-existant JID). --- salt/netapi/rest_cherrypy/app.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/salt/netapi/rest_cherrypy/app.py b/salt/netapi/rest_cherrypy/app.py index 05a55755f7..699a7065bd 100644 --- a/salt/netapi/rest_cherrypy/app.py +++ b/salt/netapi/rest_cherrypy/app.py @@ -1329,10 +1329,13 @@ class Jobs(LowDataAdapter): ret = {} if jid: - ret['info'] = job_ret_info[0] - ret['return'] = [dict((k, job_ret_info[0]['Result'][k]['return']) for k in job_ret_info[0]['Result'])] + ret['info'] = [job_ret_info[0]] + try: + ret['return'] = [dict((k, job_ret_info[0]['Result'][k]['return']) for k in job_ret_info[0]['Result'])] + except (TypeError, KeyError): + ret['return'] = [{}] else: - ret['return'] = job_ret_info[0] + ret['return'] = [job_ret_info[0]] return ret From 0f897b24262b29ff55a121b7c0bd1d910b4f6219 Mon Sep 17 00:00:00 2001 From: Scott Thrasher Date: Fri, 2 Sep 2016 10:14:45 -0700 Subject: [PATCH 3/3] Switch from comprehension to logic used in jobs runner. This makes it easier to deal with potential unicode in returns. --- salt/netapi/rest_cherrypy/app.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/salt/netapi/rest_cherrypy/app.py b/salt/netapi/rest_cherrypy/app.py index 699a7065bd..3e89ff7882 100644 --- a/salt/netapi/rest_cherrypy/app.py +++ b/salt/netapi/rest_cherrypy/app.py @@ -1330,10 +1330,14 @@ class Jobs(LowDataAdapter): ret = {} if jid: ret['info'] = [job_ret_info[0]] - try: - ret['return'] = [dict((k, job_ret_info[0]['Result'][k]['return']) for k in job_ret_info[0]['Result'])] - except (TypeError, KeyError): - ret['return'] = [{}] + minion_ret = {} + returns = job_ret_info[0].get('Result') + for minion in returns.keys(): + if u'return' in returns[minion]: + minion_ret[minion] = returns[minion].get(u'return') + else: + minion_ret[minion] = returns[minion].get('return') + ret['return'] = [minion_ret] else: ret['return'] = [job_ret_info[0]]