From fceab5299b61683b75de9eac8239158e931453fd Mon Sep 17 00:00:00 2001
From: licanwei
Date: Thu, 9 Feb 2017 18:06:51 +0800
Subject: [PATCH] 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
---
watcher/common/exception.py | 4 +---
watcher/decision_engine/audit/base.py | 21 ++++++++++---------
.../audit/test_audit_handlers.py | 15 ++++++-------
3 files changed, 18 insertions(+), 22 deletions(-)
diff --git a/watcher/common/exception.py b/watcher/common/exception.py
index 06e434144..cc2703793 100644
--- a/watcher/common/exception.py
+++ b/watcher/common/exception.py
@@ -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):
diff --git a/watcher/decision_engine/audit/base.py b/watcher/decision_engine/audit/base.py
index 0b298d504..092374b0a 100644
--- a/watcher/decision_engine/audit/base.py
+++ b/watcher/decision_engine/audit/base.py
@@ -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)
diff --git a/watcher/tests/decision_engine/audit/test_audit_handlers.py b/watcher/tests/decision_engine/audit/test_audit_handlers.py
index c5b334e4f..5d0f1d826 100644
--- a/watcher/tests/decision_engine/audit/test_audit_handlers.py
+++ b/watcher/tests/decision_engine/audit/test_audit_handlers.py
@@ -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')