Skip to content

Commit a0391a1

Browse files
Reuse class PostgreSQL on each self.postgresql.* call (#1030)
* Reuse class PostgreSQL on each postgresql call * Visualise Patroni /cluster status calls to catch 'None' issue * Re-init class PostgreSQL if we got primary_host=None It should be explored further and isolate primary_host=None.
1 parent 137c60b commit a0391a1

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/charm.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ class PostgresqlOperatorCharm(TypedCharmBase[CharmConfig]):
251251

252252
config_type = CharmConfig
253253
on = ClusterTopologyChangeCharmEvents()
254+
_postgresql: PostgreSQL | None = None
254255

255256
def __init__(self, *args):
256257
super().__init__(*args)
@@ -605,14 +606,20 @@ def is_unit_stopped(self) -> bool:
605606
@property
606607
def postgresql(self) -> PostgreSQL:
607608
"""Returns an instance of the object used to interact with the database."""
608-
return PostgreSQL(
609-
primary_host=self.primary_endpoint,
610-
current_host=self._unit_ip,
611-
user=USER,
612-
password=self.get_secret(APP_SCOPE, f"{USER}-password"),
613-
database=DATABASE_DEFAULT_NAME,
614-
system_users=SYSTEM_USERS,
615-
)
609+
password = str(self.get_secret(APP_SCOPE, f"{USER}-password"))
610+
if self._postgresql is None or self._postgresql.primary_host is None:
611+
logger.debug("Init class PostgreSQL")
612+
self._postgresql = PostgreSQL(
613+
primary_host=self.primary_endpoint,
614+
current_host=self._unit_ip,
615+
user=USER,
616+
password=password,
617+
database=DATABASE_DEFAULT_NAME,
618+
system_users=SYSTEM_USERS,
619+
)
620+
else:
621+
self._postgresql.password = password
622+
return self._postgresql
616623

617624
@property
618625
def primary_endpoint(self) -> str | None:

src/cluster.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ def cluster_status(self, alternative_endpoints: list | None = None) -> list[Clus
260260
if response := self.parallel_patroni_get_request(
261261
f"/{PATRONI_CLUSTER_STATUS_ENDPOINT}", alternative_endpoints
262262
):
263+
logger.debug("Patroni cluster members: %s", response["members"])
263264
return response["members"]
264265
raise RetryError(last_attempt=Exception("Unable to reach any units"))
265266

0 commit comments

Comments
 (0)