Skip to content

Commit

Permalink
auto tune based on memory
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomach committed Sep 21, 2023
1 parent ecd609c commit 05d9564
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 12 additions & 2 deletions lib/charms/postgresql_k8s/v0/postgresql.py
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)

0 comments on commit 05d9564

Please sign in to comment.