Fix for multiple status-set - related to bug 1588462

This change fixes the obvious race for a status_set() between
check_optional_interfaces() and assess_status() as the later calls the former
which calls status_set(), returns the status, which is then potentially set
again by the assess_status() function.  This cleans up the code so that only a
single status_set() is performed when calling assess_status().

Change-Id: Idb11019cec20061622b5f36911e49adfb9bac14e
Related-Bug:#1588462
This commit is contained in:
Alex Kavanagh 2016-06-15 11:27:31 +00:00
parent 485868fa54
commit 36ee1afc8d
2 changed files with 41 additions and 16 deletions

View File

@ -24,7 +24,6 @@ from charmhelpers.core.hookenv import (
INFO,
relation_get,
relation_ids,
status_get,
)
from charmhelpers.contrib.network.ip import (
format_ipv6_addr,
@ -38,7 +37,6 @@ from charmhelpers.contrib.openstack.utils import (
make_assess_status_func,
pause_unit,
resume_unit,
set_os_workload_status,
)
from charmhelpers.contrib.hahelpers.cluster import get_hacluster_config
from charmhelpers.core.host import (
@ -184,24 +182,40 @@ def enable_pocket(pocket):
sources.write(line)
def check_optional_relations(configs):
required_interfaces = {}
def get_optional_interfaces():
"""Return the optional interfaces that should be checked if the relavent
relations have appeared.
:returns: {general_interface: [specific_int1, specific_int2, ...], ...}
"""
optional_interfaces = {}
if relation_ids('ha'):
optional_interfaces['ha'] = ['cluster']
if (cmp_pkgrevno('radosgw', '0.55') >= 0 and
relation_ids('identity-service')):
optional_interfaces['identity'] = ['identity-service']
return optional_interfaces
def check_optional_relations(configs):
"""Check that if we have a relation_id for high availability that we can
get the hacluster config. If we can't then we are blocked. This function
is called from assess_status/set_os_workload_status as the charm_func and
needs to return either 'unknown', '' if there is no problem or the status,
message if there is a problem.
:param configs: an OSConfigRender() instance.
:return 2-tuple: (string, string) = (status, message)
"""
if relation_ids('ha'):
required_interfaces['ha'] = ['cluster']
try:
get_hacluster_config()
except:
return ('blocked',
'hacluster missing configuration: '
'vip, vip_iface, vip_cidr')
if cmp_pkgrevno('radosgw', '0.55') >= 0 and \
relation_ids('identity-service'):
required_interfaces['identity'] = ['identity-service']
if required_interfaces:
set_os_workload_status(configs, required_interfaces)
return status_get()
else:
return 'unknown', 'No optional relations'
# return 'unknown' as the lowest priority to not clobber an existing
# status.
return 'unknown', ''
def setup_ipv6():
@ -245,14 +259,20 @@ def assess_status_func(configs):
Used directly by assess_status() and also for pausing and resuming
the unit.
NOTE: REQUIRED_INTERFACES is augmented with the optional interfaces
depending on the current config before being passed to the
make_assess_status_func() function.
NOTE(ajkavanagh) ports are not checked due to race hazards with services
that don't behave sychronously w.r.t their service scripts. e.g.
apache2.
@param configs: a templating.OSConfigRenderer() object
@return f() -> None : a function that assesses the unit's workload status
"""
required_interfaces = REQUIRED_INTERFACES.copy()
required_interfaces.update(get_optional_interfaces())
return make_assess_status_func(
configs, REQUIRED_INTERFACES,
configs, required_interfaces,
charm_func=check_optional_relations,
services=services(), ports=None)

View File

@ -27,6 +27,7 @@ class CephRadosGWUtilTests(CharmTestCase):
asf.assert_called_once_with('test-config')
callee.assert_called_once_with()
@patch.object(utils, 'get_optional_interfaces')
@patch.object(utils, 'check_optional_relations')
@patch.object(utils, 'REQUIRED_INTERFACES')
@patch.object(utils, 'services')
@ -35,12 +36,16 @@ class CephRadosGWUtilTests(CharmTestCase):
make_assess_status_func,
services,
REQUIRED_INTERFACES,
check_optional_relations):
check_optional_relations,
get_optional_interfaces):
services.return_value = 's1'
REQUIRED_INTERFACES.copy.return_value = {'int': ['test 1']}
get_optional_interfaces.return_value = {'opt': ['test 2']}
utils.assess_status_func('test-config')
# ports=None whilst port checks are disabled.
make_assess_status_func.assert_called_once_with(
'test-config', REQUIRED_INTERFACES,
'test-config',
{'int': ['test 1'], 'opt': ['test 2']},
charm_func=check_optional_relations,
services='s1', ports=None)