Skip to content

Commit

Permalink
DPE-4933 Avoid early calls to unit_initialized (#481)
Browse files Browse the repository at this point in the history
* ensure unit_initialized can be called

* restarts must be waited more
  • Loading branch information
paulomach authored Aug 13, 2024
1 parent 0934aba commit e657758
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions src/log_rotate_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/relations/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/relations/mysql_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e657758

Please sign in to comment.