revise dumping event notifications

To prevent dumping duplicated event notifications to rabbitMQ, we will only
dump it at the beginning of processing the action:
https://git.openstack.org/cgit/openstack/senlin/tree/senlin/engine/actions/base.py#n471

and at the end of processing the action:
https://git.openstack.org/cgit/openstack/senlin/tree/senlin/engine/actions/base.py#n492

Change-Id: I3ac9115f29cca40d3b16132a5e0afa76f502ec1a
This commit is contained in:
RUIJIE YUAN 2017-02-19 14:22:25 +08:00
parent 969990f6b7
commit a17d0d351d
5 changed files with 37 additions and 33 deletions

View File

@ -368,9 +368,8 @@ class Action(object):
if self.data['status'] == policy_mod.CHECK_OK:
return True
reason = _("Failed policy '%(name)s': %(reason)s"
) % {'name': name, 'reason': reason}
EVENT.error(self, consts.PHASE_ERROR, reason)
self.data['reason'] = _("Failed policy '%(name)s': %(reason)s"
) % {'name': name, 'reason': reason}
return False
def policy_check(self, cluster_id, target):

View File

@ -24,7 +24,6 @@ from senlin.common import utils
from senlin.engine.actions import base
from senlin.engine import cluster as cluster_mod
from senlin.engine import dispatcher
from senlin.engine import event as EVENT
from senlin.engine import node as node_mod
from senlin.engine import scheduler
from senlin.engine import senlin_lock
@ -960,7 +959,6 @@ class ClusterAction(base.Action):
self.policy_check(self.entity.id, 'BEFORE')
if self.data['status'] != policy_mod.CHECK_OK:
reason = _('Policy check failure: %s') % self.data['reason']
EVENT.error(self, consts.PHASE_ERROR, reason)
return self.RES_ERROR, reason
result = self.RES_OK
@ -969,7 +967,6 @@ class ClusterAction(base.Action):
method = getattr(self, method_name, None)
if method is None:
reason = _('Unsupported action: %s.') % self.action
EVENT.error(self, consts.PHASE_ERROR, reason)
return self.RES_ERROR, reason
result, reason = method()
@ -979,7 +976,6 @@ class ClusterAction(base.Action):
self.policy_check(self.entity.id, 'AFTER')
if self.data['status'] != policy_mod.CHECK_OK:
reason = _('Policy check failure: %s') % self.data['reason']
EVENT.error(self, consts.PHASE_ERROR, reason)
return self.RES_ERROR, reason
return result, reason

View File

@ -125,6 +125,8 @@ class TestScalingPolicy(base.BaseSenlinFunctionalTest):
self.assertEqual('ACTIVE', cluster['status'])
self.assertEqual(1, cluster['desired_capacity'])
self.assertEqual(1, len(cluster['nodes']))
reason = _("Policy check failure: The target capacity (-1) is less "
"than the cluster's min_size (0).")
reason = _(
"Policy check failure: Failed policy '%s': The target "
"capacity (-1) is less than the cluster's "
"min_size (0).") % scalein_policy['name']
self.assertEqual(reason, res)

View File

@ -382,6 +382,30 @@ class ActionBaseTest(base.SenlinTestCase):
self.assertEqual('BUSY', action.status_reason)
mock_abandon.assert_called_once_with(action.context, 'FAKE_ID')
@mock.patch.object(EVENT, 'info')
@mock.patch.object(EVENT, 'error')
@mock.patch.object(EVENT, 'warning')
@mock.patch.object(ao.Action, 'mark_succeeded')
@mock.patch.object(ao.Action, 'mark_failed')
@mock.patch.object(ao.Action, 'abandon')
def test_set_status_dump_event(self, mock_abandon, mark_fail,
mark_succeed, mock_warning, mock_error,
mock_info):
action = ab.Action(OBJID, 'OBJECT_ACTION', self.ctx, id='FAKE_ID')
action.entity = mock.Mock()
action.set_status(action.RES_OK, 'FAKE_SUCCEEDED')
mock_info.assert_called_once_with(action, consts.PHASE_END,
'FAKE_SUCCEEDED')
action.set_status(action.RES_ERROR, 'FAKE_ERROR')
mock_error.assert_called_once_with(action, consts.PHASE_ERROR,
'FAKE_ERROR')
action.set_status(action.RES_RETRY, 'FAKE_RETRY')
mock_warning.assert_called_once_with(action, consts.PHASE_ERROR,
'FAKE_RETRY')
@mock.patch.object(EVENT, 'info')
@mock.patch.object(EVENT, 'error')
@mock.patch.object(EVENT, 'warning')
@ -637,8 +661,7 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
self.assertEqual(0, mock_pre_op.call_count)
self.assertEqual(0, mock_post_op.call_count)
@mock.patch.object(EVENT, 'debug')
def test__check_result_true(self, mock_event):
def test__check_result_true(self):
cluster_id = CLUSTER_ID
action = ab.Action(cluster_id, 'OBJECT_ACTION', self.ctx)
action.data['status'] = policy_mod.CHECK_OK
@ -648,8 +671,7 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
self.assertTrue(res)
@mock.patch.object(EVENT, 'error')
def test__check_result_false(self, mock_event):
def test__check_result_false(self):
cluster_id = CLUSTER_ID
action = ab.Action(cluster_id, 'OBJECT_ACTION', self.ctx)
action.data['status'] = policy_mod.CHECK_ERROR
@ -661,12 +683,10 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
reason = ("Failed policy '%(name)s': %(reason)s"
) % {'name': 'FAKE_POLICY_NAME', 'reason': reason}
self.assertFalse(res)
mock_event.assert_called_once_with(action, 'error', reason)
@mock.patch.object(EVENT, 'debug')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
@mock.patch.object(policy_mod.Policy, 'load')
def test_policy_check_pre_op(self, mock_load, mock_load_all, mock_event):
def test_policy_check_pre_op(self, mock_load, mock_load_all):
cluster_id = CLUSTER_ID
# Note: policy is mocked
spec = {
@ -697,10 +717,9 @@ class ActionPolicyCheckTest(base.SenlinTestCase):
# last_op was not updated
self.assertIsNone(pb.last_op)
@mock.patch.object(EVENT, 'debug')
@mock.patch.object(cpo.ClusterPolicy, 'get_all')
@mock.patch.object(policy_mod.Policy, 'load')
def test_policy_check_post_op(self, mock_load, mock_load_all, mock_event):
def test_policy_check_post_op(self, mock_load, mock_load_all):
cluster_id = CLUSTER_ID
# Note: policy is mocked
policy = mock.Mock(id=uuidutils.generate_uuid(), cooldown=0,

View File

@ -15,7 +15,6 @@ import mock
from senlin.engine.actions import base as ab
from senlin.engine.actions import cluster_action as ca
from senlin.engine import cluster as cm
from senlin.engine import event as EVENT
from senlin.engine import senlin_lock
from senlin.policies import base as pb
from senlin.tests.unit.common import base
@ -49,10 +48,8 @@ class ClusterActionTest(base.SenlinTestCase):
mock.call('FAKE_CLUSTER', 'BEFORE'),
mock.call('FAKE_CLUSTER', 'AFTER')])
@mock.patch.object(EVENT, 'error')
@mock.patch.object(ab.Action, 'policy_check')
def test_execute_failed_policy_check(self, mock_check, mock_error,
mock_load):
def test_execute_failed_policy_check(self, mock_check, mock_load):
cluster = mock.Mock()
cluster.id = 'FAKE_CLUSTER'
mock_load.return_value = cluster
@ -68,13 +65,9 @@ class ClusterActionTest(base.SenlinTestCase):
self.assertEqual(action.RES_ERROR, res_code)
self.assertEqual('Policy check failure: Something is wrong.', res_msg)
mock_check.assert_called_once_with('FAKE_CLUSTER', 'BEFORE')
mock_error.assert_called_once_with(
action, 'error', 'Policy check failure: Something is wrong.')
@mock.patch.object(EVENT, 'error')
@mock.patch.object(ab.Action, 'policy_check')
def test_execute_unsupported_action(self, mock_check, mock_error,
mock_load):
def test_execute_unsupported_action(self, mock_check, mock_load):
cluster = mock.Mock()
cluster.id = 'FAKE_CLUSTER'
mock_load.return_value = cluster
@ -89,11 +82,8 @@ class ClusterActionTest(base.SenlinTestCase):
self.assertEqual(action.RES_ERROR, res_code)
self.assertEqual('Unsupported action: CLUSTER_DANCE.', res_msg)
mock_check.assert_called_once_with('FAKE_CLUSTER', 'BEFORE')
mock_error.assert_called_once_with(
action, 'error', 'Unsupported action: CLUSTER_DANCE.')
@mock.patch.object(EVENT, 'error')
def test_execute_post_check_failed(self, mock_error, mock_load):
def test_execute_post_check_failed(self, mock_load):
def fake_check(cluster_id, target):
if target == 'BEFORE':
action.data = {
@ -122,8 +112,6 @@ class ClusterActionTest(base.SenlinTestCase):
mock_check.assert_has_calls([
mock.call('FAKE_CLUSTER', 'BEFORE'),
mock.call('FAKE_CLUSTER', 'AFTER')])
mock_error.assert_called_once_with(
action, 'error', 'Policy check failure: Policy checking failed.')
@mock.patch.object(senlin_lock, 'cluster_lock_acquire')
@mock.patch.object(senlin_lock, 'cluster_lock_release')