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-2617 autotune production profile #231

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 additions & 2 deletions lib/charms/postgresql_k8s/v0/postgresql.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should increment the lib version. I've created canonical/postgresql-k8s-operator#264 for k8s

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ufff..... we need an automated protection here on CI/CD. Good ideas are welcome!

Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,21 @@ def build_postgresql_parameters(

Args:
profile: the profile to use.
available_memory: available memory to use in calculation
available_memory: available memory to use in calculation in bytes.

Returns:
Dictionary with the PostgreSQL parameters.
"""
logger.debug(f"Building PostgreSQL parameters for {profile=} and {available_memory=}")
if profile == "production":
return {"shared_buffers": "987MB", "effective_cache_size": "2958MB"}
# Use 25% of the available memory for shared_buffers.
# and the remaind as cache memory.
shared_buffers = int(available_memory * 0.25)
effective_cache_size = int(available_memory - shared_buffers)

parameters = {
"shared_buffers": f"{int(shared_buffers/10**6)}MB",
"effective_cache_size": f"{int(effective_cache_size/10**6)}MB",
}

return parameters
16 changes: 15 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,11 @@ def update_config(self, is_creating_backup: bool = False) -> bool:
"""Updates Patroni config file based on the existence of the TLS files."""
enable_tls = all(self.tls.get_tls_files())

# Build PostgreSQL parameters.
pg_parameters = self.postgresql.build_postgresql_parameters(
self.config["profile"], self.get_available_memory()
)

# Update and reload configuration based on TLS files availability.
self._patroni.render_patroni_yml_file(
connectivity=self.unit_peer_data.get("connectivity", "on") == "on",
Expand All @@ -1370,7 +1375,7 @@ def update_config(self, is_creating_backup: bool = False) -> bool:
backup_id=self.app_peer_data.get("restoring-backup"),
stanza=self.app_peer_data.get("stanza"),
restore_stanza=self.app_peer_data.get("restore-stanza"),
parameters=self.postgresql.build_postgresql_parameters(self.config["profile"], 0),
parameters=pg_parameters,
)
if not self._is_workload_running:
# If Patroni/PostgreSQL has not started yet and TLS relations was initialised,
Expand Down Expand Up @@ -1417,6 +1422,15 @@ def _update_relation_endpoints(self) -> None:
self.legacy_db_relation.update_endpoints()
self.legacy_db_admin_relation.update_endpoints()

def get_available_memory(self) -> int:
"""Returns the system available memory in bytes."""
with open("/proc/meminfo") as meminfo:
for line in meminfo:
if "MemTotal" in line:
return int(line.split()[1]) * 1024

return 0


if __name__ == "__main__":
main(PostgresqlOperatorCharm)
19 changes: 18 additions & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See LICENSE file for licensing details.
import subprocess
import unittest
from unittest.mock import MagicMock, Mock, PropertyMock, patch
from unittest.mock import MagicMock, Mock, PropertyMock, mock_open, patch

from charms.operator_libs_linux.v2 import snap
from charms.postgresql_k8s.v0.postgresql import (
Expand Down Expand Up @@ -1391,3 +1391,20 @@ def test_is_workload_running(self, _snap_cache):

pg_snap.present = True
self.assertTrue(self.charm._is_workload_running)

def test_get_available_memory(self):
meminfo = (
"MemTotal: 16089488 kB"
"MemFree: 799284 kB"
"MemAvailable: 3926924 kB"
"Buffers: 187232 kB"
"Cached: 4445936 kB"
"SwapCached: 156012 kB"
"Active: 11890336 kB"
)

with patch("builtins.open", mock_open(read_data=meminfo)):
self.assertEqual(self.charm.get_available_memory(), 16475635712)

with patch("builtins.open", mock_open(read_data="")):
self.assertEqual(self.charm.get_available_memory(), 0)