Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@
DatabaseEndpointsChangedEvent,
DatabaseRequires,
)
from ops import ActionEvent, ActiveStatus, CharmBase, Relation, StartEvent, main
from ops import (
ActionEvent,
ActiveStatus,
BlockedStatus,
CharmBase,
Relation,
RelationBrokenEvent,
StartEvent,
main,
)
from tenacity import RetryError, Retrying, stop_after_delay, wait_fixed

logger = logging.getLogger(__name__)

BLOCKED_NO_INTEGRATION_MSG = "No database integration available"
PEER = "postgresql-test-peers"
# Expected tmp access
LAST_WRITTEN_FILE = "/tmp/last_written_value" # noqa: S108
Expand Down Expand Up @@ -168,22 +178,23 @@ def are_writes_running(self) -> bool:
return False

def _on_start(self, event: StartEvent) -> None:
"""Only sets an Active status."""
self.unit.status = ActiveStatus()
"""Sets initial Waiting status and checks if writes should be restarted."""
self.unit.status = BlockedStatus(BLOCKED_NO_INTEGRATION_MSG)
if (
self.model.unit.is_leader()
and PROC_PID_KEY in self.app_peer_data
and not self.are_writes_running()
):
try:
writes = self._get_db_writes()
writes = self._get_db_writes(reraise=True)
except Exception:
logger.debug("Connection to db not yet available")
event.defer()
return
if writes > 0:
logger.info("Restarting continuous writes from db")
self._start_continuous_writes(writes + 1)
self.unit.status = ActiveStatus("received database credentials of the first database")

# First database events observers.
def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
Expand All @@ -205,9 +216,15 @@ def _on_database_endpoints_changed(self, event: DatabaseEndpointsChangedEvent) -
fd.write(self._connection_string)
os.fsync(fd)

def _on_relation_broken(self, _) -> None:
def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
"""Event triggered when a database relation is left."""
self.unit.status = ActiveStatus("")
if not any([
*self.model.relations.get("database"),
*self.model.relations.get("second-database"),
*self.model.relations.get("multiple-database-clusters"),
*self.model.relations.get("aliased-multiple-database-clusters"),
]):
self.unit.status = BlockedStatus(BLOCKED_NO_INTEGRATION_MSG)

# Second database events observers.
def _on_second_database_created(self, event: DatabaseCreatedEvent) -> None:
Expand Down Expand Up @@ -362,7 +379,7 @@ def _on_start_continuous_writes_action(self, event: ActionEvent) -> None:
self._start_continuous_writes(1)
event.set_results({"result": "True"})

def _get_db_writes(self) -> int:
def _get_db_writes(self, reraise: bool = False) -> int:
connection = None
try:
with (
Expand All @@ -372,9 +389,11 @@ def _get_db_writes(self) -> int:
connection.autocommit = True
cursor.execute("SELECT COUNT(*) FROM continuous_writes;")
writes = cursor.fetchone()[0]
except Exception:
except Exception as e:
writes = -1
logger.exception("Unable to count writes")
if reraise:
raise e
finally:
if connection:
connection.close()
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import logging
import time

import pytest
from juju.relation import Relation
from lightkube.core.client import Client
from lightkube.resources.core_v1 import Pod
Expand Down Expand Up @@ -143,7 +142,6 @@ async def test_restart(ops_test: OpsTest) -> None:
client = Client(namespace=ops_test.model.info.name)
client.delete(Pod, name=f"{TEST_APP_NAME}-0")
else:
pytest.skip("Unstable LXC restart test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please mention this problem in PR description at least.
BTW. what is unstable in LXD restart?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the unstable marker for LXD added in #368 (comment). The test passes now, so it's no longer to be skipped.

logger.info("Restarting lxc")
await restart_machine(ops_test, ops_test.model.applications[TEST_APP_NAME].units[0].name)

Expand Down