Merge "Reuse database connections."

This commit is contained in:
Zuul 2020-07-14 15:01:34 +00:00 committed by Gerrit Code Review
commit 6d979f9ab3
2 changed files with 79 additions and 33 deletions

View File

@ -1028,7 +1028,8 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
["mode"])
# TODO: Generalize and move to mysql charmhelpers
def get_allowed_units(self, database, username, relation_id):
def get_allowed_units(self, database, username, relation_id,
db_helper=None):
"""Get Allowed Units.
Call MySQL8Helper.get_allowed_units and return space delimited list of
@ -1045,7 +1046,8 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
:returns: Space delimited list of unit names
:rtype: str
"""
db_helper = self.get_db_helper()
if not db_helper:
db_helper = self.get_db_helper()
allowed_units = db_helper.get_allowed_units(
database, username, relation_id=relation_id)
allowed_units = sorted(
@ -1068,13 +1070,13 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
:rtype: Bool
"""
completed = []
db_host = ch_net_ip.get_relation_ip(interface.endpoint_name)
db_helper = self.get_db_helper()
rw_helper = self.get_cluster_rw_db_helper()
for unit in interface.all_joined_units:
db_data = mysql.get_db_data(
dict(unit.received),
unprefixed=self._unprefixed)
db_host = ch_net_ip.get_relation_ip(interface.endpoint_name)
mysqlrouterset = {'username', 'hostname'}
singleset = {'database', 'username', 'hostname'}
@ -1085,12 +1087,14 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
username = db_data[prefix]['username']
password = self.configure_db_for_hosts(
hostname, database, username)
hostname, database, username,
rw_helper=rw_helper)
completed.append(password)
allowed_units = self.get_allowed_units(
database, username,
unit.relation.relation_id)
unit.relation.relation_id,
db_helper=db_helper)
if prefix in self._unprefixed:
prefix = None
@ -1099,7 +1103,10 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
hostname = db_data[prefix]['hostname']
username = db_data[prefix]['username']
password = self.configure_db_router(hostname, username)
password = self.configure_db_router(
hostname,
username,
rw_helper=rw_helper)
completed.append(password)
allowed_units = " ".join(
[x.unit_name for x in unit.relation.joined_units])
@ -1120,7 +1127,8 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
return False
# TODO: Generalize and move to mysql charmhelpers
def configure_db_for_hosts(self, hosts, database, username):
def configure_db_for_hosts(self, hosts, database, username,
rw_helper=None):
"""Configure database for user at host(s).
Create and configure database and user with full access permissions
@ -1135,6 +1143,8 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
:type database: str
:param username: Username
:type username: str
:param rw_helper: Instance of MySQL8Helper
:type rw_helper: charmhelpers.contrib.database.mysql.MySQL8Helper
:side effect: Calls MySQL8Helper.configure_db
:returns: Password for the DB user
:rtype: str
@ -1151,8 +1161,8 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
"Single hostname provided by relation: {}".format(hosts),
level="DEBUG")
hosts = [hosts]
rw_helper = self.get_cluster_rw_db_helper()
if not rw_helper:
rw_helper = self.get_cluster_rw_db_helper()
if not rw_helper:
ch_core.hookenv.log(
"No connection to the cluster primary RW node "
@ -1165,7 +1175,7 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
return password
def configure_db_router(self, hosts, username):
def configure_db_router(self, hosts, username, rw_helper=None):
"""Configure database for MySQL Router user at host(s).
Create and configure MySQL Router user with mysql router specific
@ -1178,6 +1188,8 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
:type hosts: Union[str, Json list]
:param username: Username
:type username: str
:param rw_helper: Instance of MySQL8Helper
:type rw_helper: charmhelpers.contrib.database.mysql.MySQL8Helper
:side effect: Calls MySQL8Helper.configure_router
:returns: Password for the DB user
:rtype: str
@ -1195,7 +1207,8 @@ class MySQLInnoDBClusterCharm(charms_openstack.charm.OpenStackCharm):
level="DEBUG")
hosts = [hosts]
rw_helper = self.get_cluster_rw_db_helper()
if not rw_helper:
rw_helper = self.get_cluster_rw_db_helper()
if not rw_helper:
ch_core.hookenv.log(
"No connection to the cluster primary RW node "

View File

@ -612,6 +612,13 @@ class TestMySQLInnoDBClusterCharm(test_utils.PatchHelper):
midbc = mysql_innodb_cluster.MySQLInnoDBClusterCharm()
midbc.get_allowed_units = mock.MagicMock()
midbc.get_allowed_units.side_effect = self._fake_get_allowed_units
_db_helper = mock.MagicMock()
midbc.get_db_helper = mock.MagicMock()
midbc.get_db_helper.return_value = _db_helper
_rw_db_helper = mock.MagicMock()
midbc.get_cluster_rw_db_helper = mock.MagicMock()
midbc.get_cluster_rw_db_helper.return_value = _rw_db_helper
_wait_timeout = 60
midbc.options.wait_timeout = _wait_timeout
@ -633,14 +640,22 @@ class TestMySQLInnoDBClusterCharm(test_utils.PatchHelper):
midbc.configure_db_router.assert_not_called()
_configure_db_calls = [
mock.call(self.keystone_unit5_ip, "keystone", "keystone"),
mock.call(self.keystone_unit7_ip, "keystone", "keystone"),
mock.call(self.nova_unit5_ip, "nova", "nova"),
mock.call(self.nova_unit5_ip, "nova_api", "nova"),
mock.call(self.nova_unit5_ip, "nova_cell0", "nova"),
mock.call(self.nova_unit7_ip, "nova", "nova"),
mock.call(self.nova_unit7_ip, "nova_api", "nova"),
mock.call(self.nova_unit7_ip, "nova_cell0", "nova")]
mock.call(self.keystone_unit5_ip, "keystone", "keystone",
rw_helper=_rw_db_helper),
mock.call(self.keystone_unit7_ip, "keystone", "keystone",
rw_helper=_rw_db_helper),
mock.call(self.nova_unit5_ip, "nova", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nova_unit5_ip, "nova_api", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nova_unit5_ip, "nova_cell0", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nova_unit7_ip, "nova", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nova_unit7_ip, "nova_api", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nova_unit7_ip, "nova_cell0", "nova",
rw_helper=_rw_db_helper)]
midbc.configure_db_for_hosts.assert_has_calls(
_configure_db_calls, any_order=True)
@ -698,6 +713,12 @@ class TestMySQLInnoDBClusterCharm(test_utils.PatchHelper):
self.certificates.root_ca_chain = "Intermediate Chain Certificate"
midbc = mysql_innodb_cluster.MySQLInnoDBClusterCharm()
_db_helper = mock.MagicMock()
midbc.get_db_helper = mock.MagicMock()
midbc.get_db_helper.return_value = _db_helper
_rw_db_helper = mock.MagicMock()
midbc.get_cluster_rw_db_helper = mock.MagicMock()
midbc.get_cluster_rw_db_helper.return_value = _rw_db_helper
midbc.get_allowed_units = mock.MagicMock()
midbc.get_allowed_units.side_effect = self._fake_get_allowed_units
midbc.configure_db_for_hosts = mock.MagicMock()
@ -719,22 +740,34 @@ class TestMySQLInnoDBClusterCharm(test_utils.PatchHelper):
# Validate
_conigure_db_router_calls = [
mock.call(self.kmr_unit5_ip, "mysqlrouteruser"),
mock.call(self.kmr_unit7_ip, "mysqlrouteruser"),
mock.call(self.nmr_unit5_ip, "mysqlrouteruser"),
mock.call(self.nmr_unit7_ip, "mysqlrouteruser")]
mock.call(self.kmr_unit5_ip, "mysqlrouteruser",
rw_helper=_rw_db_helper),
mock.call(self.kmr_unit7_ip, "mysqlrouteruser",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit5_ip, "mysqlrouteruser",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit7_ip, "mysqlrouteruser",
rw_helper=_rw_db_helper)]
midbc.configure_db_router.assert_has_calls(
_conigure_db_router_calls, any_order=True)
_configure_db_calls = [
mock.call(self.kmr_unit5_ip, "keystone", "keystone"),
mock.call(self.kmr_unit7_ip, "keystone", "keystone"),
mock.call(self.nmr_unit5_ip, "nova", "nova"),
mock.call(self.nmr_unit5_ip, "nova_api", "nova"),
mock.call(self.nmr_unit5_ip, "nova_cell0", "nova"),
mock.call(self.nmr_unit7_ip, "nova", "nova"),
mock.call(self.nmr_unit7_ip, "nova_api", "nova"),
mock.call(self.nmr_unit7_ip, "nova_cell0", "nova")]
mock.call(self.kmr_unit5_ip, "keystone", "keystone",
rw_helper=_rw_db_helper),
mock.call(self.kmr_unit7_ip, "keystone", "keystone",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit5_ip, "nova", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit5_ip, "nova_api", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit5_ip, "nova_cell0", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit7_ip, "nova", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit7_ip, "nova_api", "nova",
rw_helper=_rw_db_helper),
mock.call(self.nmr_unit7_ip, "nova_cell0", "nova",
rw_helper=_rw_db_helper)]
midbc.configure_db_for_hosts.assert_has_calls(
_configure_db_calls, any_order=True)