Skip to content

Commit cafbb5c

Browse files
authored
DPE-4699 Set instance offline mode on restore (#478)
* 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
1 parent 90bb1fb commit cafbb5c

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

lib/charms/data_platform_libs/v0/upgrade.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def restart(self, event) -> None:
285285

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

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

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

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

lib/charms/mysql/v0/backups.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def is_unit_blocked(self) -> bool:
9999

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

104104

105105
if typing.TYPE_CHECKING:
@@ -557,9 +557,12 @@ def _pre_restore(self) -> Tuple[bool, str]:
557557
try:
558558
logger.info("Stopping mysqld before restoring the backup")
559559
self.charm._mysql.kill_client_sessions()
560+
self.charm._mysql.set_instance_offline_mode(True)
560561
self.charm._mysql.stop_mysqld()
561562
except MySQLKillSessionError:
562563
return False, "Failed to kill client sessions"
564+
except MySQLSetInstanceOfflineModeError:
565+
return False, "Failed to set instance as offline before restoring the backup"
563566
except MySQLStopMySQLDError:
564567
return False, "Failed to stop mysqld"
565568

tests/unit/test_backups.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,13 @@ def test_on_restore_failure(
747747
event.fail.assert_not_called()
748748

749749
@patch_network_get(private_address="1.1.1.1")
750+
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
750751
@patch("mysql_vm_helpers.MySQL.is_mysqld_running", return_value=True)
751752
@patch("mysql_vm_helpers.MySQL.kill_client_sessions")
752753
@patch("mysql_vm_helpers.MySQL.stop_mysqld")
753-
def test_pre_restore(self, _stop_mysqld, _kill_client_sessions, _mysqld_running):
754+
def test_pre_restore(
755+
self, _stop_mysqld, _kill_client_sessions, _mysqld_running, _set_instance_offline_mode
756+
):
754757
"""Test _pre_restore()."""
755758
success, error = self.mysql_backups._pre_restore()
756759

@@ -759,12 +762,16 @@ def test_pre_restore(self, _stop_mysqld, _kill_client_sessions, _mysqld_running)
759762
_mysqld_running.assert_called_once()
760763
_stop_mysqld.assert_called_once()
761764
_kill_client_sessions.assert_called_once()
765+
_set_instance_offline_mode.assert_called_once()
762766

763767
@patch_network_get(private_address="1.1.1.1")
768+
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
764769
@patch("mysql_vm_helpers.MySQL.is_mysqld_running", return_value=True)
765770
@patch("mysql_vm_helpers.MySQL.kill_client_sessions")
766771
@patch("mysql_vm_helpers.MySQL.stop_mysqld")
767-
def test_pre_restore_failure(self, _stop_mysqld, _kill_client_sessions, _mysqld_running):
772+
def test_pre_restore_failure(
773+
self, _stop_mysqld, _kill_client_sessions, _mysqld_running, _set_instance_offline_mode
774+
):
768775
"""Test failure of _pre_restore()."""
769776
_stop_mysqld.side_effect = MySQLStopMySQLDError()
770777

0 commit comments

Comments
 (0)