From e657758954cbe2a1983e684430cb46b3b4913b4b Mon Sep 17 00:00:00 2001 From: Paulo Machado Date: Tue, 13 Aug 2024 18:07:47 -0300 Subject: [PATCH] DPE-4933 Avoid early calls to unit_initialized (#481) * ensure unit_initialized can be called * restarts must be waited more --- src/charm.py | 3 ++- src/log_rotate_manager.py | 4 ++++ src/relations/mysql.py | 8 ++++---- src/relations/mysql_root.py | 7 ++++++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/charm.py b/src/charm.py index 60868ba5a..d695fa56a 100755 --- a/src/charm.py +++ b/src/charm.py @@ -445,7 +445,8 @@ def _restart(self, event: EventBase) -> None: self.unit.status = MaintenanceStatus("restarting MySQL") container = self.unit.get_container(CONTAINER_NAME) if container.can_connect(): - container.restart(MYSQLD_SAFE_SERVICE) + logger.debug("Restarting mysqld") + container.pebble.restart_services([MYSQLD_SAFE_SERVICE], timeout=3600) sleep(10) self._on_update_status(None) diff --git a/src/log_rotate_manager.py b/src/log_rotate_manager.py index 3ab976026..df82ca1dc 100644 --- a/src/log_rotate_manager.py +++ b/src/log_rotate_manager.py @@ -12,6 +12,8 @@ from ops.framework import Object from ops.model import ActiveStatus +from constants import CONTAINER_NAME + if typing.TYPE_CHECKING: from charm import MySQLOperatorCharm @@ -31,9 +33,11 @@ def __init__(self, charm: "MySQLOperatorCharm"): def start_log_rotate_manager(self): """Forks off a process that periodically dispatch a custom event to rotate logs.""" + container = self.charm.unit.get_container(CONTAINER_NAME) if ( not isinstance(self.charm.unit.status, ActiveStatus) or self.charm.peers is None + or not container.can_connect() or not self.charm.unit_initialized ): return diff --git a/src/relations/mysql.py b/src/relations/mysql.py index 0f7ebd8b2..8d811d31e 100644 --- a/src/relations/mysql.py +++ b/src/relations/mysql.py @@ -136,14 +136,14 @@ def _update_status(self, _) -> None: if not (relation_data := self.charm.app_peer_data.get(MYSQL_RELATION_DATA_KEY)): return - if not self.charm.unit_initialized: - # Skip update status for uninitialized unit - return - container = self.charm.unit.get_container(CONTAINER_NAME) if not container.can_connect(): return + if not self.charm.unit_initialized: + # Skip update status for uninitialized unit + return + if not self.charm.unit.is_leader(): return diff --git a/src/relations/mysql_root.py b/src/relations/mysql_root.py index d91b98d2f..493115630 100644 --- a/src/relations/mysql_root.py +++ b/src/relations/mysql_root.py @@ -12,7 +12,7 @@ from ops.framework import Object from ops.model import ActiveStatus, BlockedStatus -from constants import LEGACY_MYSQL_ROOT, PASSWORD_LENGTH, ROOT_PASSWORD_KEY +from constants import CONTAINER_NAME, LEGACY_MYSQL_ROOT, PASSWORD_LENGTH, ROOT_PASSWORD_KEY from mysql_k8s_helpers import ( MySQLCreateDatabaseError, MySQLCreateUserError, @@ -152,6 +152,11 @@ def _on_mysql_root_relation_created(self, event: RelationCreatedEvent) -> None: if not self.charm.unit.is_leader(): return + container = self.charm.unit.get_container(CONTAINER_NAME) + if not container.can_connect(): + event.defer() + return + # Wait until on-config-changed event is executed # (wait for root password to have been set) or wait until the unit is initialized if not (self.charm._is_peer_data_set and self.charm.unit_initialized):