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 1 commit
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 @@
"""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 @@
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 @@
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

Check warning on line 1432 in src/charm.py

View check run for this annotation

Codecov / codecov/patch

src/charm.py#L1432

Added line #L1432 was not covered by tests


if __name__ == "__main__":
main(PostgresqlOperatorCharm)
Loading