Skip to content

Commit e657758

Browse files
authored
DPE-4933 Avoid early calls to unit_initialized (#481)
* ensure unit_initialized can be called * restarts must be waited more
1 parent 0934aba commit e657758

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

src/charm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ def _restart(self, event: EventBase) -> None:
445445
self.unit.status = MaintenanceStatus("restarting MySQL")
446446
container = self.unit.get_container(CONTAINER_NAME)
447447
if container.can_connect():
448-
container.restart(MYSQLD_SAFE_SERVICE)
448+
logger.debug("Restarting mysqld")
449+
container.pebble.restart_services([MYSQLD_SAFE_SERVICE], timeout=3600)
449450
sleep(10)
450451
self._on_update_status(None)
451452

src/log_rotate_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from ops.framework import Object
1313
from ops.model import ActiveStatus
1414

15+
from constants import CONTAINER_NAME
16+
1517
if typing.TYPE_CHECKING:
1618
from charm import MySQLOperatorCharm
1719

@@ -31,9 +33,11 @@ def __init__(self, charm: "MySQLOperatorCharm"):
3133

3234
def start_log_rotate_manager(self):
3335
"""Forks off a process that periodically dispatch a custom event to rotate logs."""
36+
container = self.charm.unit.get_container(CONTAINER_NAME)
3437
if (
3538
not isinstance(self.charm.unit.status, ActiveStatus)
3639
or self.charm.peers is None
40+
or not container.can_connect()
3741
or not self.charm.unit_initialized
3842
):
3943
return

src/relations/mysql.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ def _update_status(self, _) -> None:
136136
if not (relation_data := self.charm.app_peer_data.get(MYSQL_RELATION_DATA_KEY)):
137137
return
138138

139-
if not self.charm.unit_initialized:
140-
# Skip update status for uninitialized unit
141-
return
142-
143139
container = self.charm.unit.get_container(CONTAINER_NAME)
144140
if not container.can_connect():
145141
return
146142

143+
if not self.charm.unit_initialized:
144+
# Skip update status for uninitialized unit
145+
return
146+
147147
if not self.charm.unit.is_leader():
148148
return
149149

src/relations/mysql_root.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ops.framework import Object
1313
from ops.model import ActiveStatus, BlockedStatus
1414

15-
from constants import LEGACY_MYSQL_ROOT, PASSWORD_LENGTH, ROOT_PASSWORD_KEY
15+
from constants import CONTAINER_NAME, LEGACY_MYSQL_ROOT, PASSWORD_LENGTH, ROOT_PASSWORD_KEY
1616
from mysql_k8s_helpers import (
1717
MySQLCreateDatabaseError,
1818
MySQLCreateUserError,
@@ -152,6 +152,11 @@ def _on_mysql_root_relation_created(self, event: RelationCreatedEvent) -> None:
152152
if not self.charm.unit.is_leader():
153153
return
154154

155+
container = self.charm.unit.get_container(CONTAINER_NAME)
156+
if not container.can_connect():
157+
event.defer()
158+
return
159+
155160
# Wait until on-config-changed event is executed
156161
# (wait for root password to have been set) or wait until the unit is initialized
157162
if not (self.charm._is_peer_data_set and self.charm.unit_initialized):

0 commit comments

Comments
 (0)