Skip to content

Commit 19633f3

Browse files
authoredMay 6, 2024··
avoid restart being triggered after upgrade (#451)
1 parent 03012d3 commit 19633f3

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed
 

‎src/charm.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,8 @@ def _on_config_changed(self, event: EventBase) -> None:
240240
# the upgrade already restart the daemon
241241
return
242242

243-
if not self._mysql.is_mysqld_running():
244-
# defer config-changed event until MySQL is running
245-
logger.debug("Deferring config-changed event until MySQL is running")
246-
event.defer()
247-
return
243+
# restart not required if mysqld is not running
244+
restart = self._mysql.is_mysqld_running()
248245

249246
previous_config = self.mysql_config.custom_config
250247
if not previous_config:
@@ -263,14 +260,15 @@ def _on_config_changed(self, event: EventBase) -> None:
263260

264261
if self.mysql_config.keys_requires_restart(changed_config):
265262
# there are static configurations in changed keys
266-
logger.info("Configuration change requires restart")
267-
263+
logger.info("Persisting configuration changes to file")
268264
# persist config to file
269265
self._mysql.write_content_to_file(
270266
path=MYSQLD_CUSTOM_CONFIG_FILE, content=new_config_content
271267
)
272-
self.on[f"{self.restart.name}"].acquire_lock.emit()
273-
return
268+
if restart:
269+
logger.info("Configuration change requires restart")
270+
self.on[f"{self.restart.name}"].acquire_lock.emit()
271+
return
274272

275273
if dynamic_config := self.mysql_config.filter_static_keys(changed_config):
276274
# if only dynamic config changed, apply it
@@ -796,6 +794,11 @@ def join_unit_to_cluster(self) -> None:
796794

797795
def _restart(self, event: EventBase) -> None:
798796
"""Restart the MySQL service."""
797+
if self.peers.units != self.restart_peers.units:
798+
# defer restart until all units are in the relation
799+
logger.debug("Deferring restart until all units are in the relation")
800+
event.defer()
801+
return
799802
if self._mysql.is_unit_primary(self.unit_label):
800803
restart_states = {
801804
self.restart_peers.data[unit].get("state", "unset") for unit in self.peers.units

‎src/upgrade.py

+5
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ def _on_upgrade_granted(self, event: UpgradeGrantedEvent) -> None: # noqa: C901
183183
return
184184
self.charm.unit.status = MaintenanceStatus("check if upgrade is possible")
185185
self._check_server_upgradeability()
186+
# override config, avoid restart
187+
self.charm._mysql.write_mysqld_config(
188+
profile=self.charm.config.profile,
189+
memory_limit=self.charm.config.profile_limit_memory,
190+
)
186191
self.charm.unit.status = MaintenanceStatus("starting services...")
187192
self.charm._mysql.start_mysqld()
188193
self.charm._mysql.setup_logrotate_and_cron()

‎tests/unit/test_upgrade.py

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def test_pre_upgrade_prepare(
131131
mock_get_primary_label.assert_called_once()
132132
assert mock_set_dynamic_variable.call_count == 2
133133

134+
@patch("mysql_vm_helpers.MySQL.write_mysqld_config")
134135
@patch("upgrade.MySQLVMUpgrade._check_server_unsupported_downgrade")
135136
@patch("upgrade.MySQLVMUpgrade._reset_on_unsupported_downgrade")
136137
@patch("mysql_vm_helpers.MySQL.hold_if_recovering")
@@ -158,6 +159,7 @@ def test_upgrade_granted(
158159
mock_hold_if_recovering,
159160
mock_reset_on_unsupported_downgrade,
160161
mock_check_server_unsupported_downgrade,
162+
mock_write_mysqld_config,
161163
):
162164
"""Test upgrade-granted hook."""
163165
self.charm.on.config_changed.emit()
@@ -176,6 +178,7 @@ def test_upgrade_granted(
176178
mock_install_workload.assert_called_once()
177179
mock_get_mysql_version.assert_called_once()
178180
mock_setup_logrotate_and_cron.assert_called_once()
181+
mock_write_mysqld_config.assert_called_once()
179182

180183
self.harness.update_relation_data(
181184
self.upgrade_relation_id, "mysql/0", {"state": "upgrading"}

0 commit comments

Comments
 (0)
Please sign in to comment.