Skip to content

Commit

Permalink
Fix parameters validation
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloneppel committed Oct 16, 2023
1 parent d5f71c6 commit ce4873e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
18 changes: 1 addition & 17 deletions lib/charms/postgresql_k8s/v0/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,29 +305,13 @@ def enable_disable_extension(self, extension: str, enable: bool, database: str =
if connection is not None:
connection.close()

def get_applied_postgresql_parameters(self, parameters=List[str]) -> Dict:
"""Returns the applied PostgreSQL configurations and their values.
Args:
parameters: list of parameters to retrieve the values.
Returns:
Dict containing PostgreSQL configurations and their values.
"""
with self._connect_to_database() as connection, connection.cursor() as cursor:
cursor.execute(
"SELECT name, setting FROM pg_settings WHERE name IN %s;", (parameters,)
)
results = cursor.fetchall()
return {configuration[0]: configuration[1] for configuration in results}

def get_invalid_postgresql_parameters(self) -> List:
"""Returns the PostgreSQL configurations that have invalid values.
Returns:
Dict containing PostgreSQL configurations and their values.
"""
with self._connect_to_database() as connection, connection.cursor() as cursor:
with self._connect_to_database(connect_to_current_host=True) as connection, connection.cursor() as cursor:
cursor.execute("SELECT name FROM pg_file_settings WHERE error IS NOT NULL;")
results = cursor.fetchall()
return [parameter[0] for parameter in results]
Expand Down
14 changes: 8 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import os
import subprocess
import time
from typing import Dict, List, Optional, Set

from charms.data_platform_libs.v0.data_models import TypedCharmBase
Expand Down Expand Up @@ -900,8 +901,6 @@ def _on_config_changed(self, _) -> None:
if not self.unit.is_leader():
return

self.update_config()

# Enable and/or disable the extensions.
self.enable_disable_extensions()

Expand Down Expand Up @@ -1432,7 +1431,7 @@ def _validate_and_render_configurations(
# Retrieve PostgreSQL parameters.
limit_memory = None
if self.config.profile_limit_memory:
limit_memory = self.config.profile_limit_memory * 10 ** 6
limit_memory = self.config.profile_limit_memory * 10**6
postgresql_parameters = self.postgresql.build_postgresql_parameters(
self.config["profile"], self.get_available_memory(), limit_memory
)
Expand Down Expand Up @@ -1475,15 +1474,18 @@ def _validate_and_render_configurations(
return True

self._patroni.reload_patroni_configuration()
time.sleep(10)

invalid_configurations = self.postgresql.get_invalid_postgresql_parameters()
if len(invalid_configurations) > 0:
logger.error(
f"invalid values for the following parameters: {''.join(invalid_configurations)}"
)
applied_parameters = self.postgresql.get_applied_postgresql_parameters(
list(postgresql_parameters.keys())
)
applied_parameters = {
parameter: postgresql_parameters[parameter]
for parameter in postgresql_parameters.keys()
if parameter not in invalid_configurations
}
self._patroni.render_patroni_yml_file(
connectivity=self.unit_peer_data.get("connectivity", "on") == "on",
is_creating_backup=is_creating_backup,
Expand Down
30 changes: 30 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
class CharmConfig(BaseConfigModel):
"""Manager for the structured configuration."""

connection_ssl: Optional[bool]
durability_synchronous_commit: Optional[str]
instance_default_text_search_config: Optional[str]
instance_password_encryption: Optional[str]
logging_log_connections: Optional[bool]
logging_log_disconnections: Optional[bool]
logging_log_lock_waits: Optional[bool]
logging_log_min_duration_statement: Optional[int]
memory_maintenance_work_mem: Optional[int]
memory_max_prepared_transactions: Optional[int]
memory_shared_buffers: Optional[int]
memory_temp_buffers: Optional[int]
memory_work_mem: Optional[int]
optimizer_constraint_exclusion: Optional[str]
optimizer_default_statistics_target: Optional[int]
optimizer_from_collapse_limit: Optional[int]
optimizer_join_collapse_limit: Optional[int]
profile: str
profile_limit_memory: Optional[int]
plugin_citext_enable: bool
Expand All @@ -23,6 +40,19 @@ class CharmConfig(BaseConfigModel):
plugin_pg_trgm_enable: bool
plugin_plpython3u_enable: bool
plugin_unaccent_enable: bool
request_date_style: Optional[str]
request_standard_conforming_strings: Optional[bool]
request_time_zone: Optional[str]
response_bytea_output: Optional[str]
response_lc_monetary: Optional[str]
response_lc_numeric: Optional[str]
response_lc_time: Optional[str]
vacuum_autovacuum_analyze_scale_factor: Optional[float]
vacuum_autovacuum_analyze_threshold: Optional[int]
vacuum_autovacuum_freeze_max_age: Optional[int]
vacuum_autovacuum_vacuum_cost_delay: Optional[float]
vacuum_autovacuum_vacuum_scale_factor: Optional[float]
vacuum_vacuum_freeze_table_age: Optional[int]

@classmethod
def keys(cls) -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# Snap constants.
PGBACKREST_EXECUTABLE = "charmed-postgresql.pgbackrest"
POSTGRESQL_SNAP_NAME = "charmed-postgresql"
SNAP_PACKAGES = [(POSTGRESQL_SNAP_NAME, {"revision": "85"})]
SNAP_PACKAGES = [(POSTGRESQL_SNAP_NAME, {"revision": "86"})]

SNAP_COMMON_PATH = "/var/snap/charmed-postgresql/common"
SNAP_CURRENT_PATH = "/var/snap/charmed-postgresql/current"
Expand Down

0 comments on commit ce4873e

Please sign in to comment.