Skip to content

Commit 783ffbd

Browse files
[MISC] Define charm utility properties (#749)
1 parent df4f23c commit 783ffbd

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

src/backups.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def _is_primary_pgbackrest_service_running(self) -> bool:
745745

746746
def _on_s3_credential_changed(self, event: CredentialsChangedEvent):
747747
"""Call the stanza initialization when the credentials or the connection info change."""
748-
if "cluster_initialised" not in self.charm.app_peer_data:
748+
if not self.charm.is_cluster_initialised:
749749
logger.debug("Cannot set pgBackRest configurations, PostgreSQL has not yet started.")
750750
event.defer()
751751
return
@@ -757,10 +757,7 @@ def _on_s3_credential_changed(self, event: CredentialsChangedEvent):
757757
return
758758

759759
# Prevents S3 change in the middle of restoring backup and patroni / pgbackrest errors caused by that.
760-
if (
761-
"restoring-backup" in self.charm.app_peer_data
762-
or "restore-to-time" in self.charm.app_peer_data
763-
):
760+
if self.charm.is_cluster_restoring_backup or self.charm.is_cluster_restoring_to_time:
764761
logger.info("Cannot change S3 configuration during restore")
765762
event.defer()
766763
return

src/charm.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,26 @@ def is_cluster_initialised(self) -> bool:
345345
"""Returns whether the cluster is already initialised."""
346346
return "cluster_initialised" in self.app_peer_data
347347

348+
@property
349+
def is_cluster_restoring_backup(self) -> bool:
350+
"""Returns whether the cluster is restoring a backup."""
351+
return "restoring-backup" in self.app_peer_data
352+
353+
@property
354+
def is_cluster_restoring_to_time(self) -> bool:
355+
"""Returns whether the cluster is restoring a backup to a specific time."""
356+
return "restore-to-time" in self.app_peer_data
357+
358+
@property
359+
def is_unit_departing(self) -> bool:
360+
"""Returns whether the unit is departing."""
361+
return "departing" in self.unit_peer_data
362+
363+
@property
364+
def is_unit_stopped(self) -> bool:
365+
"""Returns whether the unit is stopped."""
366+
return "stopped" in self.unit_peer_data
367+
348368
@property
349369
def postgresql(self) -> PostgreSQL:
350370
"""Returns an instance of the object used to interact with the database."""
@@ -452,9 +472,9 @@ def _on_peer_relation_departed(self, event: RelationDepartedEvent) -> None:
452472
if not self.unit.is_leader():
453473
return
454474

455-
if "cluster_initialised" not in self._peers.data[
456-
self.app
457-
] or not self._updated_synchronous_node_count(len(self._units_ips)):
475+
if not self.is_cluster_initialised or not self._updated_synchronous_node_count(
476+
len(self._units_ips)
477+
):
458478
logger.debug("Deferring on_peer_relation_departed: cluster not initialized")
459479
event.defer()
460480
return
@@ -662,7 +682,7 @@ def has_raft_keys(self):
662682
def _peer_relation_changed_checks(self, event: HookEvent) -> bool:
663683
"""Split of to reduce complexity."""
664684
# Prevents the cluster to be reconfigured before it's bootstrapped in the leader.
665-
if "cluster_initialised" not in self._peers.data[self.app]:
685+
if not self.is_cluster_initialised:
666686
logger.debug("Deferring on_peer_relation_changed: cluster not initialized")
667687
event.defer()
668688
return False
@@ -710,7 +730,7 @@ def _on_peer_relation_changed(self, event: HookEvent):
710730
return
711731

712732
if (
713-
"restoring-backup" in self.app_peer_data or "restore-to-time" in self.app_peer_data
733+
self.is_cluster_restoring_backup or self.is_cluster_restoring_to_time
714734
) and not self._was_restore_successful():
715735
logger.debug("on_peer_relation_changed early exit: Backup restore check failed")
716736
return
@@ -1127,7 +1147,7 @@ def _on_leader_elected(self, event: LeaderElectedEvent) -> None:
11271147

11281148
# Don't update connection endpoints in the first time this event run for
11291149
# this application because there are no primary and replicas yet.
1130-
if "cluster_initialised" not in self._peers.data[self.app]:
1150+
if not self.is_cluster_initialised:
11311151
logger.debug("Early exit on_leader_elected: Cluster not initialized")
11321152
return
11331153

@@ -1403,7 +1423,7 @@ def _start_primary(self, event: StartEvent) -> None:
14031423

14041424
def _start_replica(self, event) -> None:
14051425
"""Configure the replica if the cluster was already initialised."""
1406-
if "cluster_initialised" not in self._peers.data[self.app]:
1426+
if not self.is_cluster_initialised:
14071427
logger.debug("Deferring on_start: awaiting for cluster to start")
14081428
self.unit.status = WaitingStatus("awaiting for cluster to start")
14091429
event.defer()
@@ -1541,7 +1561,7 @@ def _on_update_status(self, _) -> None:
15411561
return
15421562

15431563
if (
1544-
"restoring-backup" in self.app_peer_data or "restore-to-time" in self.app_peer_data
1564+
self.is_cluster_restoring_backup or self.is_cluster_restoring_to_time
15451565
) and not self._was_restore_successful():
15461566
return
15471567

@@ -1566,7 +1586,7 @@ def _on_update_status(self, _) -> None:
15661586
self._observer.start_observer()
15671587

15681588
def _was_restore_successful(self) -> bool:
1569-
if "restore-to-time" in self.app_peer_data and all(self.is_pitr_failed()):
1589+
if self.is_cluster_restoring_to_time and all(self.is_pitr_failed()):
15701590
logger.error(
15711591
"Restore failed: database service failed to reach point-in-time-recovery target. "
15721592
"You can launch another restore with different parameters"
@@ -1623,7 +1643,7 @@ def _was_restore_successful(self) -> bool:
16231643
return True
16241644

16251645
def _can_run_on_update_status(self) -> bool:
1626-
if "cluster_initialised" not in self._peers.data[self.app]:
1646+
if not self.is_cluster_initialised:
16271647
return False
16281648

16291649
if self.has_raft_keys():

src/relations/async_replication.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def is_primary_cluster(self) -> bool:
479479
return self.charm.app == self._get_primary_cluster()
480480

481481
def _on_async_relation_broken(self, _) -> None:
482-
if not self.charm._peers or "departing" in self.charm._peers.data[self.charm.unit]:
482+
if not self.charm._peers or self.charm.is_unit_departing:
483483
logger.debug("Early exit on_async_relation_broken: Skipping departing unit.")
484484
return
485485

@@ -521,11 +521,11 @@ def _on_async_relation_changed(self, event: RelationChangedEvent) -> None:
521521
if not self._stop_database(event):
522522
return
523523

524-
if not all(
524+
if not (self.charm.is_unit_stopped or self._is_following_promoted_cluster()) or not all(
525525
"stopped" in self.charm._peers.data[unit]
526526
or self.charm._peers.data[unit].get("unit-promoted-cluster-counter")
527527
== self._get_highest_promoted_cluster_counter_value()
528-
for unit in {*self.charm._peers.units, self.charm.unit}
528+
for unit in self.charm._peers.units
529529
):
530530
self.charm.unit.status = WaitingStatus(
531531
"Waiting for the database to be stopped in all units"
@@ -691,10 +691,7 @@ def _set_app_status(self) -> None:
691691

692692
def _stop_database(self, event: RelationChangedEvent) -> bool:
693693
"""Stop the database."""
694-
if (
695-
"stopped" not in self.charm._peers.data[self.charm.unit]
696-
and not self._is_following_promoted_cluster()
697-
):
694+
if not self.charm.is_unit_stopped and not self._is_following_promoted_cluster():
698695
if not self.charm.unit.is_leader() and not os.path.exists(POSTGRESQL_DATA_PATH):
699696
logger.debug("Early exit on_async_relation_changed: following promoted cluster.")
700697
return False

src/relations/db.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _on_relation_changed(self, event: RelationChangedEvent) -> None:
117117
return
118118

119119
if (
120-
"cluster_initialised" not in self.charm._peers.data[self.charm.app]
120+
not self.charm.is_cluster_initialised
121121
or not self.charm._patroni.member_started
122122
or not self.charm.primary_endpoint
123123
):
@@ -240,7 +240,7 @@ def _on_relation_departed(self, event: RelationDepartedEvent) -> None:
240240
return
241241

242242
if (
243-
"cluster_initialised" not in self.charm._peers.data[self.charm.app]
243+
not self.charm.is_cluster_initialised
244244
or not self.charm._patroni.member_started
245245
or not self.charm.primary_endpoint
246246
):
@@ -266,7 +266,7 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
266266
# Check for some conditions before trying to access the PostgreSQL instance.
267267
if (
268268
not self.charm.unit.is_leader()
269-
or "cluster_initialised" not in self.charm._peers.data[self.charm.app]
269+
or not self.charm.is_cluster_initialised
270270
or not self.charm._patroni.member_started
271271
or not self.charm.primary_endpoint
272272
):
@@ -281,7 +281,7 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
281281
# https://bugs.launchpad.net/juju/+bug/1979811.
282282
# Neither peer relation data nor stored state
283283
# are good solutions, just a temporary solution.
284-
if "departing" in self.charm._peers.data[self.charm.unit]:
284+
if self.charm.is_unit_departing:
285285
logger.debug("Early exit on_relation_broken: Skipping departing unit")
286286
return
287287

src/relations/postgresql_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
7272
return
7373

7474
if (
75-
"cluster_initialised" not in self.charm._peers.data[self.charm.app]
75+
not self.charm.is_cluster_initialised
7676
or not self.charm._patroni.member_started
7777
or not self.charm.primary_endpoint
7878
):

0 commit comments

Comments
 (0)