diff --git a/src/backups.py b/src/backups.py index 930b5681fe..573d184497 100644 --- a/src/backups.py +++ b/src/backups.py @@ -31,6 +31,7 @@ BACKUP_TYPE_OVERRIDES, BACKUP_USER, PGBACKREST_LOGROTATE_FILE, + POSTGRESQL_ARCHIVE_PATH, WORKLOAD_OS_GROUP, WORKLOAD_OS_USER, ) @@ -1184,6 +1185,7 @@ def _render_pgbackrest_conf_file(self) -> bool: # Open the template pgbackrest.conf file. with open("templates/pgbackrest.conf.j2") as file: template = Template(file.read()) + cpu_count, _ = self.charm.get_available_resources() # Render the template file with the correct values. rendered = template.render( enable_tls=self.charm.is_tls_enabled and len(self.charm.peer_members_endpoints) > 0, @@ -1200,7 +1202,8 @@ def _render_pgbackrest_conf_file(self) -> bool: storage_path=self.charm._storage_path, user=BACKUP_USER, retention_full=s3_parameters["delete-older-than-days"], - process_max=max(os.cpu_count() - 2, 1), + process_max=max(cpu_count - 2, 1), + archive_path=POSTGRESQL_ARCHIVE_PATH, ) # Delete the original file and render the one with the right info. filename = "/etc/pgbackrest.conf" diff --git a/src/constants.py b/src/constants.py index 030699032c..5f17accc78 100644 --- a/src/constants.py +++ b/src/constants.py @@ -19,6 +19,7 @@ METRICS_PORT = "9187" POSTGRESQL_DATA_PATH = "/var/lib/postgresql/data/pgdata" POSTGRESQL_LOGS_PATH = "/var/log/postgresql" +POSTGRESQL_ARCHIVE_PATH = "/var/lib/postgresql/archive" POSTGRESQL_LOGS_PATTERN = "postgresql*.log" POSTGRES_LOG_FILES = [ "/var/log/pgbackrest/*", diff --git a/templates/pgbackrest.conf.j2 b/templates/pgbackrest.conf.j2 index 1606ca52bb..ce88fe9900 100644 --- a/templates/pgbackrest.conf.j2 +++ b/templates/pgbackrest.conf.j2 @@ -1,6 +1,7 @@ [global] backup-standby=y compress-type=zst +spool-path={{ archive_path }} repo1-retention-full-type=time repo1-retention-full={{ retention_full }} repo1-retention-history=365 diff --git a/tests/unit/test_backups.py b/tests/unit/test_backups.py index a67cdc102f..db08b709ab 100644 --- a/tests/unit/test_backups.py +++ b/tests/unit/test_backups.py @@ -1,7 +1,6 @@ # Copyright 2023 Canonical Ltd. # See LICENSE file for licensing details. import datetime -from os import cpu_count from unittest.mock import MagicMock, PropertyMock, call, mock_open, patch import pytest @@ -14,7 +13,7 @@ from tenacity import RetryError, wait_fixed from charm import PostgresqlOperatorCharm -from constants import PEER +from constants import PEER, POSTGRESQL_ARCHIVE_PATH from tests.unit.helpers import _FakeApiError ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE = "the S3 repository has backups from another cluster" @@ -1762,6 +1761,7 @@ def test_render_pgbackrest_conf_file(harness, tls_ca_chain_filename): new_callable=PropertyMock(return_value=tls_ca_chain_filename), ) as _tls_ca_chain_filename, patch("charm.PostgreSQLBackups._retrieve_s3_parameters") as _retrieve_s3_parameters, + patch("charm.PostgresqlOperatorCharm.get_available_resources", return_value=(4, 1024)), ): # Set up a mock for the `open` method, set returned data to postgresql.conf template. with open("templates/pgbackrest.conf.j2") as f: @@ -1813,7 +1813,8 @@ def test_render_pgbackrest_conf_file(harness, tls_ca_chain_filename): storage_path=harness.charm._storage_path, user="backup", retention_full=30, - process_max=max(cpu_count() - 2, 1), + process_max=2, + archive_path=POSTGRESQL_ARCHIVE_PATH, ) # Patch the `open` method with our mock.