Skip to content

Commit 3ecaf4c

Browse files
authored
[MISC] Move pg wal (#988)
* Move pg_wal dir instead of deleting * Check there's at least one dir * Move pg_wal once * PGB lib fix
1 parent f2628fb commit 3ecaf4c

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

lib/charms/postgresql_k8s/v1/postgresql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def create_predefined_instance_roles(self) -> None:
386386
connection.close()
387387

388388
def grant_database_privileges_to_user(
389-
self, user: str, database: str, privileges: list[str]
389+
self, user: str, database: str, privileges: List[str]
390390
) -> None:
391391
"""Grant the specified privileges on the provided database for the user."""
392392
try:

src/charm.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import subprocess
1616
import sys
1717
import time
18-
from datetime import datetime
18+
from datetime import UTC, datetime
1919
from pathlib import Path
2020
from typing import Literal, Optional, get_args
2121
from urllib.parse import urlparse
@@ -2038,8 +2038,11 @@ def _handle_processes_failures(self) -> bool:
20382038
if self._unit_ip in self.members_ips and self._patroni.member_inactive:
20392039
data_directory_contents = os.listdir(POSTGRESQL_DATA_PATH)
20402040
if len(data_directory_contents) == 1 and data_directory_contents[0] == "pg_wal":
2041-
os.remove(os.path.join(POSTGRESQL_DATA_PATH, "pg_wal"))
2042-
logger.info("PostgreSQL data directory was not empty. Removed pg_wal")
2041+
os.rename(
2042+
os.path.join(POSTGRESQL_DATA_PATH, "pg_wal"),
2043+
os.path.join(POSTGRESQL_DATA_PATH, f"pg_wal-{datetime.now(UTC).isoformat()}"),
2044+
)
2045+
logger.info("PostgreSQL data directory was not empty. Moved pg_wal")
20432046
return True
20442047
try:
20452048
self._patroni.restart_patroni()

tests/unit/test_charm.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import itertools
44
import json
55
import logging
6+
import os
67
import pathlib
78
import platform
89
import subprocess
10+
from datetime import UTC, datetime
911
from unittest.mock import MagicMock, Mock, PropertyMock, call, mock_open, patch, sentinel
1012

1113
import charm_refresh
@@ -43,7 +45,7 @@
4345
SwitchoverFailedError,
4446
SwitchoverNotSyncError,
4547
)
46-
from constants import PEER, SECRET_INTERNAL_LABEL, UPDATE_CERTS_BIN_PATH
48+
from constants import PEER, POSTGRESQL_DATA_PATH, SECRET_INTERNAL_LABEL, UPDATE_CERTS_BIN_PATH
4749

4850
CREATE_CLUSTER_CONF_PATH = "/etc/postgresql-common/createcluster.d/pgcharm.conf"
4951

@@ -2642,3 +2644,55 @@ def test_get_ldap_parameters(harness):
26422644
harness.charm.get_ldap_parameters()
26432645
_get_relation_data.assert_called_once()
26442646
_get_relation_data.reset_mock()
2647+
2648+
2649+
def test_handle_processes_failures(harness):
2650+
_now = datetime.now(UTC)
2651+
with (
2652+
patch(
2653+
"charm.Patroni.member_inactive",
2654+
new_callable=PropertyMock,
2655+
return_value=False,
2656+
) as _member_inactive,
2657+
patch(
2658+
"charm.Patroni.restart_patroni",
2659+
) as _restart_patroni,
2660+
patch("charm.os.listdir", return_value=["other_dirs", "pg_wal"]) as _listdir,
2661+
patch("charm.os.rename") as _rename,
2662+
patch("charm.datetime") as _datetime,
2663+
):
2664+
_datetime.now.return_value = _now
2665+
rel_id = harness.model.get_relation(PEER).id
2666+
with harness.hooks_disabled():
2667+
harness.update_relation_data(
2668+
rel_id,
2669+
harness.charm.app.name,
2670+
{"cluster_initialised": "True", "members_ips": '["192.0.2.0"]'},
2671+
)
2672+
2673+
# Does nothing if member is inactive
2674+
assert not harness.charm._handle_processes_failures()
2675+
assert not _restart_patroni.called
2676+
2677+
# Will not remove pg_wal if dir look right
2678+
_member_inactive.return_value = True
2679+
assert harness.charm._handle_processes_failures()
2680+
_restart_patroni.assert_called_once_with()
2681+
_restart_patroni.reset_mock()
2682+
2683+
# Will move pg_wal if there's only a pg_wal dir
2684+
_listdir.return_value = ["pg_wal"]
2685+
assert harness.charm._handle_processes_failures()
2686+
assert not _restart_patroni.called
2687+
_rename.assert_called_once_with(
2688+
os.path.join(POSTGRESQL_DATA_PATH, "pg_wal"),
2689+
os.path.join(POSTGRESQL_DATA_PATH, f"pg_wal-{_now.isoformat()}"),
2690+
)
2691+
_rename.reset_mock()
2692+
2693+
# Will not move pg_wal if there's only a pg_wal dir or other moved dirs
2694+
_listdir.return_value = ["pg_wal", f"pg_wal-{_now.isoformat()}"]
2695+
assert harness.charm._handle_processes_failures()
2696+
_restart_patroni.assert_called_once_with()
2697+
assert not _rename.called
2698+
_restart_patroni.reset_mock()

0 commit comments

Comments
 (0)