Skip to content

Commit

Permalink
DPE-4699 Set instance offline mode on restore (#478)
Browse files Browse the repository at this point in the history
* ensure username uniqueness

* libs bump

* temporary branch for router

* test against temp branch revision of router

* support for legacy username format

* model uuid as suffix

* put instance in offline mode

* bump lib

* leftover
  • Loading branch information
paulomach authored Jul 5, 2024
1 parent 90bb1fb commit cafbb5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/charms/data_platform_libs/v0/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def restart(self, event) -> None:

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 17
LIBPATCH = 18

PYDEPS = ["pydantic>=1.10,<2", "poetry-core"]

Expand Down Expand Up @@ -921,7 +921,7 @@ def _on_upgrade_charm(self, event: UpgradeCharmEvent) -> None:
self.charm.unit.status = WaitingStatus("other units upgrading first...")
self.peer_relation.data[self.charm.unit].update({"state": "ready"})

if self.charm.app.planned_units() == 1:
if len(self.app_units) == 1:
# single unit upgrade, emit upgrade_granted event right away
getattr(self.on, "upgrade_granted").emit()

Expand Down
5 changes: 4 additions & 1 deletion lib/charms/mysql/v0/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def is_unit_blocked(self) -> bool:

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 9
LIBPATCH = 10


if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -557,9 +557,12 @@ def _pre_restore(self) -> Tuple[bool, str]:
try:
logger.info("Stopping mysqld before restoring the backup")
self.charm._mysql.kill_client_sessions()
self.charm._mysql.set_instance_offline_mode(True)
self.charm._mysql.stop_mysqld()
except MySQLKillSessionError:
return False, "Failed to kill client sessions"
except MySQLSetInstanceOfflineModeError:
return False, "Failed to set instance as offline before restoring the backup"
except MySQLStopMySQLDError:
return False, "Failed to stop mysqld"

Expand Down
11 changes: 9 additions & 2 deletions tests/unit/test_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,13 @@ def test_on_restore_failure(
event.fail.assert_not_called()

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
@patch("mysql_vm_helpers.MySQL.is_mysqld_running", return_value=True)
@patch("mysql_vm_helpers.MySQL.kill_client_sessions")
@patch("mysql_vm_helpers.MySQL.stop_mysqld")
def test_pre_restore(self, _stop_mysqld, _kill_client_sessions, _mysqld_running):
def test_pre_restore(
self, _stop_mysqld, _kill_client_sessions, _mysqld_running, _set_instance_offline_mode
):
"""Test _pre_restore()."""
success, error = self.mysql_backups._pre_restore()

Expand All @@ -759,12 +762,16 @@ def test_pre_restore(self, _stop_mysqld, _kill_client_sessions, _mysqld_running)
_mysqld_running.assert_called_once()
_stop_mysqld.assert_called_once()
_kill_client_sessions.assert_called_once()
_set_instance_offline_mode.assert_called_once()

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
@patch("mysql_vm_helpers.MySQL.is_mysqld_running", return_value=True)
@patch("mysql_vm_helpers.MySQL.kill_client_sessions")
@patch("mysql_vm_helpers.MySQL.stop_mysqld")
def test_pre_restore_failure(self, _stop_mysqld, _kill_client_sessions, _mysqld_running):
def test_pre_restore_failure(
self, _stop_mysqld, _kill_client_sessions, _mysqld_running, _set_instance_offline_mode
):
"""Test failure of _pre_restore()."""
_stop_mysqld.side_effect = MySQLStopMySQLDError()

Expand Down

0 comments on commit cafbb5c

Please sign in to comment.