Optimize audit process

In the current audit process, after executing the strategy,
will check whether there are currently running actionplan,
and if so, will set the new actionplan SUPERSEDED.
We can optimize the process to perform this check in pre_execute(),
and if any actionplan is running, no further processing is performed.

Change-Id: I7377b53a2374b1dc177d256a0f800a86b1a2a16b
Closes-Bug: #1663150
This commit is contained in:
licanwei
2017-02-09 18:06:51 +08:00
parent 8ceb710b59
commit fceab5299b
3 changed files with 18 additions and 22 deletions

View File

@@ -267,9 +267,7 @@ class ActionPlanReferenced(Invalid):
class ActionPlanIsOngoing(Conflict):
msg_fmt = _("Action Plan %(action_plan)s is currently running. "
"New Action Plan %(new_action_plan)s will be set as "
"SUPERSEDED")
msg_fmt = _("Action Plan %(action_plan)s is currently running.")
class ActionNotFound(ResourceNotFound):

View File

@@ -102,23 +102,24 @@ class AuditHandler(BaseAuditHandler):
audit.state = state
audit.save()
@staticmethod
def check_ongoing_action_plans(request_context):
a_plan_filters = {'state': objects.action_plan.State.ONGOING}
ongoing_action_plans = objects.ActionPlan.list(
request_context, filters=a_plan_filters)
if ongoing_action_plans:
raise exception.ActionPlanIsOngoing(
action_plan=ongoing_action_plans[0].uuid)
def pre_execute(self, audit, request_context):
LOG.debug("Trigger audit %s", audit.uuid)
self.check_ongoing_action_plans(request_context)
# change state of the audit to ONGOING
self.update_audit_state(audit, objects.audit.State.ONGOING)
def post_execute(self, audit, solution, request_context):
action_plan = self.do_schedule(request_context, audit, solution)
a_plan_filters = {'state': objects.action_plan.State.ONGOING}
ongoing_action_plans = objects.ActionPlan.list(
request_context, filters=a_plan_filters)
if ongoing_action_plans:
action_plan.state = objects.action_plan.State.SUPERSEDED
action_plan.save()
raise exception.ActionPlanIsOngoing(
action_plan=ongoing_action_plans[0].uuid,
new_action_plan=action_plan.uuid)
elif audit.auto_trigger:
if audit.auto_trigger:
applier_client = rpcapi.ApplierAPI()
applier_client.launch_action_plan(request_context,
action_plan.uuid)

View File

@@ -19,7 +19,6 @@ import mock
from oslo_utils import uuidutils
from watcher.applier import rpcapi
from watcher.common import exception
from watcher.decision_engine.audit import continuous
from watcher.decision_engine.audit import oneshot
from watcher.decision_engine.model.collector import manager
@@ -190,16 +189,14 @@ class TestAutoTriggerActionPlan(base.DbTestCase):
strategy=self.strategy,
)
@mock.patch.object(oneshot.OneShotAuditHandler, 'do_execute')
@mock.patch.object(objects.action_plan.ActionPlan, 'list')
@mock.patch.object(objects.audit.Audit, 'get_by_id')
def test_trigger_action_plan_with_ongoing(self, mock_get_by_id, mock_list):
mock_get_by_id.return_value = self.audit
def test_trigger_audit_with_actionplan_ongoing(self, mock_list,
mock_do_execute):
mock_list.return_value = [self.ongoing_action_plan]
auto_trigger_handler = oneshot.OneShotAuditHandler(mock.MagicMock())
with mock.patch.object(auto_trigger_handler, 'do_schedule'):
self.assertRaises(exception.ActionPlanIsOngoing,
auto_trigger_handler.post_execute,
self.audit, mock.MagicMock(), self.context)
audit_handler = oneshot.OneShotAuditHandler(mock.MagicMock())
audit_handler.execute(self.audit, self.context)
self.assertFalse(mock_do_execute.called)
@mock.patch.object(rpcapi.ApplierAPI, 'launch_action_plan')
@mock.patch.object(objects.action_plan.ActionPlan, 'list')