Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPE-3684] reinit action #684

Merged
merged 4 commits into from
Dec 24, 2024
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
7 changes: 6 additions & 1 deletion actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ list-backups:
pre-upgrade-check:
description: Run necessary pre-upgrade checks and preparations before executing a charm refresh.
promote-to-primary:
description: Promotes the cluster of choice to a primary cluster. Must be ran against the leader unit.
description: Promotes the cluster of choice to a primary cluster. Must be ran against the leader unit when promoting a cluster
or against the unit to be promoted within the cluster.
params:
scope:
type: string
default: cluster
description: Whether to promote a unit or a cluster. Must be set to either unit or cluster.
force:
type: boolean
description: Force the promotion of a cluster when there is already a primary cluster.
Expand Down
23 changes: 23 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def __init__(self, *args):
self.framework.observe(self.on.start, self._on_start)
self.framework.observe(self.on.get_password_action, self._on_get_password)
self.framework.observe(self.on.set_password_action, self._on_set_password)
self.framework.observe(self.on.promote_to_primary_action, self._on_promote_to_primary)
self.framework.observe(self.on.update_status, self._on_update_status)
self.cluster_name = self.app.name
self._member_name = self.unit.name.replace("/", "-")
Expand Down Expand Up @@ -626,6 +627,7 @@ def _raft_reinitialisation(self) -> None:
and "raft_primary" not in self.unit_peer_data
and "raft_followers_stopped" in self.app_peer_data
):
self.unit.status = MaintenanceStatus("Reinitialising raft")
logger.info(f"Reinitialising {self.unit.name} as primary")
self._patroni.reinitialise_raft_data()
self.unit_peer_data["raft_primary"] = "True"
Expand Down Expand Up @@ -1502,6 +1504,27 @@ def _on_set_password(self, event: ActionEvent) -> None:

event.set_results({"password": password})

def _on_promote_to_primary(self, event: ActionEvent) -> None:
if event.params.get("scope") == "cluster":
return self.async_replication.promote_to_primary(event)
elif event.params.get("scope") == "unit":
return self.promote_primary_unit(event)
else:
event.fail("Scope should be either cluster or unit")

def promote_primary_unit(self, event: ActionEvent) -> None:
"""Handles promote to primary for unit scope."""
if event.params.get("force"):
if self.has_raft_keys():
self.unit_peer_data.update({"raft_candidate": "True"})
if self.unit.is_leader():
self._raft_reinitialisation()
return
event.fail("Raft is not stuck.")
else:
# TODO Regular promotion
pass

def _on_update_status(self, _) -> None:
"""Update the unit status message and users list in the database."""
if not self._can_run_on_update_status():
Expand Down
10 changes: 1 addition & 9 deletions src/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,17 +901,9 @@ def remove_raft_member(self, member_ip: str) -> None:
if not raft_status["has_quorum"] and (
not raft_status["leader"] or raft_status["leader"].host == member_ip
):
self.charm.unit.status = MaintenanceStatus("Reinitialising raft")
self.charm.unit.status = MaintenanceStatus("Raft majority loss")
logger.warning("Remove raft member: Stuck raft cluster detected")
data_flags = {"raft_stuck": "True"}
try:
candidate = self._get_role() in ["leader", "sync_standby"]
except Exception:
logger.warning("Remove raft member: Unable to get cluster status")
candidate = False
if candidate:
logger.info(f"{self.charm.unit.name} is raft candidate")
data_flags["raft_candidate"] = "True"
self.charm.unit_peer_data.update(data_flags)

# Leader doesn't always trigger when changing it's own peer data.
Expand Down
5 changes: 1 addition & 4 deletions src/relations/async_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ def __init__(self, charm):
self.framework.observe(
self.charm.on.create_replication_action, self._on_create_replication
)
self.framework.observe(
self.charm.on.promote_to_primary_action, self._on_promote_to_primary
)

self.framework.observe(self.charm.on.secret_changed, self._on_secret_changed)

Expand Down Expand Up @@ -583,7 +580,7 @@ def _on_create_replication(self, event: ActionEvent) -> None:
# Set the status.
self.charm.unit.status = MaintenanceStatus("Creating replication...")

def _on_promote_to_primary(self, event: ActionEvent) -> None:
def promote_to_primary(self, event: ActionEvent) -> None:
"""Promote this cluster to the primary cluster."""
if (
self.charm.app.status.message != READ_ONLY_MODE_BLOCKING_MESSAGE
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ def test_remove_raft_member_no_quorum(patroni, harness):

patroni.remove_raft_member("1.2.3.4")

assert harness.charm.unit_peer_data == {"raft_candidate": "True", "raft_stuck": "True"}
assert harness.charm.unit_peer_data == {"raft_stuck": "True"}


def test_remove_raft_data(patroni):
Expand Down
Loading