Skip to content

Commit

Permalink
bug: configure maximum db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jul 5, 2024
1 parent 785405c commit 0d8cc56
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 52 deletions.
10 changes: 3 additions & 7 deletions anvil-python/anvil/commands/haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,11 @@ def haproxy_install_steps(
client: Client,
manifest: Manifest,
jhelper: JujuHelper,
deployment: LocalDeployment,
model: str,
fqdn: str,
) -> List[BaseStep]:
return [
TerraformInitStep(manifest.get_tfhelper("haproxy-plan")),
DeployHAProxyApplicationStep(
client, manifest, jhelper, deployment.infrastructure_model
),
AddHAProxyUnitsStep(
client, fqdn, jhelper, deployment.infrastructure_model
),
DeployHAProxyApplicationStep(client, manifest, jhelper, model),
AddHAProxyUnitsStep(client, fqdn, jhelper, model),
]
10 changes: 3 additions & 7 deletions anvil-python/anvil/commands/maas_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,11 @@ def maas_agent_install_steps(
client: Client,
manifest: Manifest,
jhelper: JujuHelper,
deployment: LocalDeployment,
model: str,
fqdn: str,
) -> List[BaseStep]:
return [
TerraformInitStep(manifest.get_tfhelper("maas-agent-plan")),
DeployMAASAgentApplicationStep(
client, manifest, jhelper, deployment.infrastructure_model
),
AddMAASAgentUnitsStep(
client, fqdn, jhelper, deployment.infrastructure_model
),
DeployMAASAgentApplicationStep(client, manifest, jhelper, model),
AddMAASAgentUnitsStep(client, fqdn, jhelper, model),
]
10 changes: 3 additions & 7 deletions anvil-python/anvil/commands/maas_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,11 @@ def maas_region_install_steps(
client: Client,
manifest: Manifest,
jhelper: JujuHelper,
deployment: LocalDeployment,
model: str,
fqdn: str,
) -> List[BaseStep]:
return [
TerraformInitStep(manifest.get_tfhelper("maas-region-plan")),
DeployMAASRegionApplicationStep(
client, manifest, jhelper, deployment.infrastructure_model
),
AddMAASRegionUnitsStep(
client, fqdn, jhelper, deployment.infrastructure_model
),
DeployMAASRegionApplicationStep(client, manifest, jhelper, model),
AddMAASRegionUnitsStep(client, fqdn, jhelper, model),
]
161 changes: 141 additions & 20 deletions anvil-python/anvil/commands/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List
import logging
from typing import Any, List, Optional

from rich.status import Status
from sunbeam.clusterd.client import Client
from sunbeam.commands.terraform import TerraformInitStep
from sunbeam.jobs import questions
from sunbeam.jobs.common import BaseStep, Result, ResultType
from sunbeam.jobs.juju import JujuHelper
from sunbeam.jobs.manifest import BaseStep
from sunbeam.jobs.steps import (
AddMachineUnitsStep,
DeployMachineApplicationStep,
Expand All @@ -28,8 +31,10 @@
from anvil.jobs.manifest import Manifest
from anvil.provider.local.deployment import LocalDeployment

LOG = logging.getLogger(__name__)
APPLICATION = "postgresql"
CONFIG_KEY = "TerraformVarsPostgresqlPlan"
POSTGRESQL_CONFIG_KEY = "TerraformVarsPostgresql"
POSTGRESQL_APP_TIMEOUT = (
180 # 3 minutes, managing the application should be fast
)
Expand All @@ -38,15 +43,66 @@
)


def postgresql_install_steps(
client: Client,
manifest: Manifest,
jhelper: JujuHelper,
model: str,
fqdn: str,
accept_defaults: bool,
preseed: dict[Any, Any],
) -> List[BaseStep]:
return [
TerraformInitStep(manifest.get_tfhelper("postgresql-plan")),
DeployPostgreSQLApplicationStep(
client,
manifest,
jhelper,
model,
accept_defaults=accept_defaults,
deployment_preseed=preseed,
),
AddPostgreSQLUnitsStep(client, fqdn, jhelper, model),
]


def postgresql_questions() -> dict[str, questions.PromptQuestion]:
return {
"max_connections": questions.PromptQuestion(
"Maximum number of concurrent connections to allow to the database server",
default_value="default",
validation_function=validate_max_connections,
),
}


def validate_max_connections(value: str) -> str | ValueError:
if value in ["default", "dynamic"]:
return value
try:
if 100 <= int(value) <= 500:
return value
else:
raise ValueError
except ValueError:
raise ValueError(
"Please provide either a number between 1 and 500 or 'default' for system default or 'dynamic' for calculating max_connections relevant to maas regions"
)


class DeployPostgreSQLApplicationStep(DeployMachineApplicationStep):
"""Deploy PostgreSQL application using Terraform"""

_CONFIG = POSTGRESQL_CONFIG_KEY

def __init__(
self,
client: Client,
manifest: Manifest,
jhelper: JujuHelper,
model: str,
deployment_preseed: dict[Any, Any] | None = None,
accept_defaults: bool = False,
refresh: bool = False,
):
super().__init__(
Expand All @@ -62,9 +118,92 @@ def __init__(
refresh,
)

self.preseed = deployment_preseed or {}
self.accept_defaults = accept_defaults

def get_application_timeout(self) -> int:
return POSTGRESQL_APP_TIMEOUT

def prompt(self, console: questions.Console | None = None) -> None:
variables = questions.load_answers(self.client, self._CONFIG)
variables.setdefault("max_connections", "default")

# Set defaults
self.preseed.setdefault("max_connections", "default")

postgresql_config_bank = questions.QuestionBank(
questions=postgresql_questions(),
console=console,
preseed=self.preseed,
previous_answers=variables,
accept_defaults=self.accept_defaults,
)
max_connections = postgresql_config_bank.max_connections.ask()
variables["max_connections"] = max_connections

LOG.debug(variables)
questions.write_answers(self.client, self._CONFIG, self.variables)

def extra_tfvars(self) -> dict[str, Any]:
variables: dict[str, Any] = questions.load_answers(
self.client, self._CONFIG
)
variables["maas_region_nodes"] = (
self.client.cluster.list_nodes_by_role("region")
)
return variables

def has_prompts(self) -> bool:
return True


class ReapplyPostgreSQLTerraformPlanStep(DeployMachineApplicationStep):
"""Reapply PostgreSQL Terraform plan"""

_CONFIG = POSTGRESQL_CONFIG_KEY

def __init__(
self,
client: Client,
manifest: Manifest,
jhelper: JujuHelper,
model: str,
):
super().__init__(
client,
manifest,
jhelper,
CONFIG_KEY,
APPLICATION,
model,
"postgresql-plan",
"Reapply PostgreSQL Terraform plan",
"Reapplying PostgreSQL Terraform plan",
True,
)

def get_application_timeout(self) -> int:
return POSTGRESQL_APP_TIMEOUT

def extra_tfvars(self) -> dict[str, Any]:
variables: dict[str, Any] = questions.load_answers(
self.client, self._CONFIG
)
variables["maas_region_nodes"] = (
self.client.cluster.list_nodes_by_role("region")
)
return variables

def is_skip(self, status: Status | None = None) -> Result:
variables: dict[str, Any] = questions.load_answers(
self.client, self._CONFIG
)
variables.setdefault("max_connections", "default")
if variables["max_connections"] != "dynamic":
return Result(ResultType.SKIPPED)
else:
return super().is_skip(status)


class AddPostgreSQLUnitsStep(AddMachineUnitsStep):
"""Add PostgreSQL Unit."""
Expand Down Expand Up @@ -110,21 +249,3 @@ def __init__(

def get_unit_timeout(self) -> int:
return POSTGRESQL_UNIT_TIMEOUT


def postgresql_install_steps(
client: Client,
manifest: Manifest,
jhelper: JujuHelper,
deployment: LocalDeployment,
fqdn: str,
) -> List[BaseStep]:
return [
TerraformInitStep(manifest.get_tfhelper("postgresql-plan")),
DeployPostgreSQLApplicationStep(
client, manifest, jhelper, deployment.infrastructure_model
),
AddPostgreSQLUnitsStep(
client, fqdn, jhelper, deployment.infrastructure_model
),
]
60 changes: 52 additions & 8 deletions anvil-python/anvil/provider/local/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
maas_region_install_steps,
)
from anvil.commands.postgresql import (
ReapplyPostgreSQLTerraformPlanStep,
RemovePostgreSQLUnitStep,
postgresql_install_steps,
)
Expand Down Expand Up @@ -264,24 +265,42 @@ def bootstrap(
jhelper = JujuHelper(deployment.get_connected_controller())

plan4 = postgresql_install_steps(
client, manifest_obj, jhelper, deployment, fqdn
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
fqdn,
accept_defaults,
preseed,
)
if is_haproxy_node:
plan4.extend(
haproxy_install_steps(
client, manifest_obj, jhelper, deployment, fqdn
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
fqdn,
)
)
if is_region_node:
plan4.extend(
maas_region_install_steps(
client, manifest_obj, jhelper, deployment, fqdn
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
fqdn,
)
)
if is_agent_node:
plan4.extend(
maas_agent_install_steps(
client, manifest_obj, jhelper, deployment, fqdn
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
fqdn,
)
)
run_plan(plan4, console)
Expand Down Expand Up @@ -365,6 +384,7 @@ def join(
ctx: click.Context,
token: str,
roles: List[Role],
accept_defaults: bool = False,
) -> None:
"""Join node to the cluster.
Expand Down Expand Up @@ -413,6 +433,7 @@ def join(
manifest_obj = Manifest.load_latest_from_clusterdb(
deployment, include_defaults=True
)
preseed = manifest_obj.deployment_config

machine_id = -1
machine_id_result = get_step_message(plan1_results, AddJujuMachineStep)
Expand All @@ -425,25 +446,48 @@ def join(
if is_database_node:
plan2.extend(
postgresql_install_steps(
client, manifest_obj, jhelper, deployment, name
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
name,
accept_defaults,
preseed,
)
)
if is_haproxy_node:
plan2.extend(
haproxy_install_steps(
client, manifest_obj, jhelper, deployment, name
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
name,
)
)
if is_region_node:
plan2.extend(
maas_region_install_steps(
client, manifest_obj, jhelper, deployment, name
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
name,
)
)
plan2.append(
ReapplyPostgreSQLTerraformPlanStep(
client, manifest_obj, jhelper, deployment.infrastructure_model
)
)
if is_agent_node:
plan2.extend(
maas_agent_install_steps(
client, manifest_obj, jhelper, deployment, name
client,
manifest_obj,
jhelper,
deployment.infrastructure_model,
name,
)
)

Expand Down
Loading

0 comments on commit 0d8cc56

Please sign in to comment.