Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 3 additions & 2 deletions .env-devel
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ PAYMENTS_USERNAME=admin
PAYMENTS_TRACING={}

POSTGRES_DB=simcoredb
POSTGRES_ENDPOINT=postgres:5432
POSTGRES_HOST=postgres
POSTGRES_PASSWORD=adminadmin
POSTGRES_PORT=5432
POSTGRES_USER=scu
POSTGRES_MINSIZE=2 # see https://github.com/ITISFoundation/osparc-simcore/pull/8199
POSTGRES_MINSIZE=1
POSTGRES_MAXSIZE=50
POSTGRES_MAX_POOLSIZE=10
POSTGRES_MAX_OVERFLOW=20
POSTGRES_READONLY_PASSWORD=readonly
POSTGRES_READONLY_USER=postgres_readonly

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ def postgres_env_vars_dict(postgres_dsn: PostgresTestConfig) -> EnvVarsDict:
"POSTGRES_DB": postgres_dsn["database"],
"POSTGRES_HOST": postgres_dsn["host"],
"POSTGRES_PORT": f"{postgres_dsn['port']}",
"POSTGRES_ENDPOINT": f"{postgres_dsn['host']}:{postgres_dsn['port']}",
}


Expand Down
8 changes: 4 additions & 4 deletions packages/service-library/src/servicelib/db_asyncpg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ async def create_async_engine_and_database_ready(

engine = create_async_engine(
settings.dsn_with_async_sqlalchemy,
pool_size=settings.POSTGRES_MINSIZE,
max_overflow=settings.POSTGRES_MAXSIZE - settings.POSTGRES_MINSIZE,
pool_size=settings.POSTGRES_MAX_POOLSIZE,
max_overflow=settings.POSTGRES_MAX_OVERFLOW,
connect_args={"server_settings": server_settings},
pool_pre_ping=True, # https://docs.sqlalchemy.org/en/14/core/pooling.html#dealing-with-disconnects
future=True, # this uses sqlalchemy 2.0 API, shall be removed when sqlalchemy 2.0 is released
Expand Down Expand Up @@ -90,8 +90,8 @@ async def with_async_pg_engine(

engine = create_async_engine(
settings.dsn_with_async_sqlalchemy,
pool_size=settings.POSTGRES_MINSIZE,
max_overflow=settings.POSTGRES_MAXSIZE - settings.POSTGRES_MINSIZE,
pool_size=settings.POSTGRES_MAX_POOLSIZE,
max_overflow=settings.POSTGRES_MAX_OVERFLOW,
connect_args={"server_settings": server_settings},
pool_pre_ping=True, # https://docs.sqlalchemy.org/en/14/core/pooling.html#dealing-with-disconnects
future=True, # this uses sqlalchemy 2.0 API, shall be removed when sqlalchemy 2.0 is released
Expand Down
29 changes: 24 additions & 5 deletions packages/settings-library/src/settings_library/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic import (
AliasChoices,
Field,
NonNegativeInt,
PostgresDsn,
SecretStr,
model_validator,
Expand All @@ -30,11 +31,28 @@ class PostgresSettings(BaseCustomSettings):

# pool connection limits
POSTGRES_MINSIZE: Annotated[
int, Field(description="Minimum number of connections in the pool", ge=2)
] = 2 # see https://github.com/ITISFoundation/osparc-simcore/pull/8199
int,
Field(
description="Minimum number of connections in the pool that are always created and kept",
ge=1,
),
] = 1
POSTGRES_MAXSIZE: Annotated[
int, Field(description="Maximum number of connections in the pool", ge=2)
int,
Field(
description="Maximum number of connections in the pool that are kept",
ge=1,
),
] = 50
POSTGRES_MAX_POOLSIZE: Annotated[
int,
Field(
description="Maximal number of connection in asyncpg pool (without overflow), lazily created on demand"
),
] = 10
POSTGRES_MAX_OVERFLOW: Annotated[
NonNegativeInt, Field(description="Maximal overflow connections")
] = 20

POSTGRES_CLIENT_NAME: Annotated[
str | None,
Expand Down Expand Up @@ -125,8 +143,10 @@ def _update_json_schema_extra(schema: JsonDict) -> None:
"POSTGRES_USER": "usr",
"POSTGRES_PASSWORD": "secret",
"POSTGRES_DB": "db",
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": "my_app", # first-choice
"HOST": "should be ignored",
"HOST_NAME": "should be ignored",
Expand All @@ -136,4 +156,3 @@ def _update_json_schema_extra(schema: JsonDict) -> None:
)

model_config = SettingsConfigDict(json_schema_extra=_update_json_schema_extra)
model_config = SettingsConfigDict(json_schema_extra=_update_json_schema_extra)
2 changes: 1 addition & 1 deletion packages/settings-library/tests/data/.env-compact
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
APP_HOST=localhost
APP_PORT=80
APP_OPTIONAL_ADDON='{"MODULE_VALUE": 10, "MODULE_VALUE_DEFAULT": 42}'
APP_REQUIRED_PLUGIN='{"POSTGRES_HOST": "localhost", "POSTGRES_PORT": 5432, "POSTGRES_USER": "foo", "POSTGRES_PASSWORD": "**********", "POSTGRES_DB": "foodb", "POSTGRES_MINSIZE": 2, "POSTGRES_MAXSIZE": 50, "POSTGRES_CLIENT_NAME": "None"}'
APP_REQUIRED_PLUGIN='{"POSTGRES_HOST": "localhost", "POSTGRES_PORT": 5432, "POSTGRES_USER": "foo", "POSTGRES_PASSWORD": "**********", "POSTGRES_DB": "foodb", "POSTGRES_MINSIZE": 2, "POSTGRES_MAX_POOLSIZE": 10, "POSTGRES_MAX_OVERFLOW": 20, "POSTGRES_MAXSIZE": 50, "POSTGRES_CLIENT_NAME": "None"}'
6 changes: 3 additions & 3 deletions packages/settings-library/tests/data/.env-granular
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ POSTGRES_USER=foo
POSTGRES_PASSWORD=**********
# Database name
POSTGRES_DB=foodb
# Minimum number of connections in the pool
POSTGRES_MINSIZE=2
# Maximum number of connections in the pool
POSTGRES_MINSIZE=1
POSTGRES_MAX_POOLSIZE=10
POSTGRES_MAX_OVERFLOW=20
POSTGRES_MAXSIZE=50
# Name of the application connecting the postgres database, will default to use the host hostname (hostname on linux)
POSTGRES_CLIENT_NAME=None
6 changes: 3 additions & 3 deletions packages/settings-library/tests/data/.env-mixed
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ POSTGRES_USER=foo
POSTGRES_PASSWORD=**********
# Database name
POSTGRES_DB=foodb
# Minimum number of connections in the pool
POSTGRES_MINSIZE=2
# Maximum number of connections in the pool
POSTGRES_MINSIZE=1
POSTGRES_MAX_POOLSIZE=10
POSTGRES_MAX_OVERFLOW=20
POSTGRES_MAXSIZE=50
# Name of the application connecting the postgres database, will default to use the host hostname (hostname on linux)
POSTGRES_CLIENT_NAME=None
6 changes: 3 additions & 3 deletions packages/settings-library/tests/data/.env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ POSTGRES_USER=foo
POSTGRES_PASSWORD=secret
# Database name
POSTGRES_DB=foodb
# Maximum number of connections in the pool
POSTGRES_MINSIZE=2
# Minimum number of connections in the pool
POSTGRES_MINSIZE=1
POSTGRES_MAX_POOLSIZE=10
POSTGRES_MAX_OVERFLOW=20
POSTGRES_MAXSIZE=50

# --- APP_MODULE_FIELD ---
Expand Down
16 changes: 12 additions & 4 deletions packages/settings-library/tests/test_base_w_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ def test_parse_from_individual_envs(
"POSTGRES_PASSWORD": "shh",
"POSTGRES_DB": "db",
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": None,
}
}
Expand All @@ -215,7 +217,9 @@ def test_parse_from_individual_envs(
"POSTGRES_PASSWORD": "shh",
"POSTGRES_DB": "db",
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": None,
}
}
Expand Down Expand Up @@ -262,7 +266,9 @@ def test_parse_compact_env(
"POSTGRES_PASSWORD": "shh2",
"POSTGRES_DB": "db2",
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": None,
}
}
Expand Down Expand Up @@ -372,7 +378,9 @@ def test_parse_from_mixed_envs(
"POSTGRES_PASSWORD": "shh2",
"POSTGRES_DB": "db2",
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": None,
}
}
Expand Down
26 changes: 17 additions & 9 deletions packages/settings-library/tests/test_utils_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ def fake_granular_env_file_content() -> str:
POSTGRES_USER=foo
POSTGRES_PASSWORD=secret
POSTGRES_DB=foodb
POSTGRES_MINSIZE=2
POSTGRES_MINSIZE=1
POSTGRES_MAXSIZE=50
POSTGRES_MAX_POOLSIZE=10
POSTGRES_MAX_OVERFLOW=20
POSTGRES_CLIENT_NAME=None
MODULE_VALUE=10
"""
Expand Down Expand Up @@ -188,8 +190,10 @@ def test_cli_default_settings_envs(
"POSTGRES_USER": "foo",
"POSTGRES_PASSWORD": "secret",
"POSTGRES_DB": "foodb",
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": None,
},
}
Expand Down Expand Up @@ -219,8 +223,10 @@ def test_cli_compact_settings_envs(
"POSTGRES_USER": "foo",
"POSTGRES_PASSWORD": "secret",
"POSTGRES_DB": "foodb",
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": None,
},
}
Expand All @@ -244,7 +250,7 @@ def test_cli_compact_settings_envs(
"APP_HOST": "localhost",
"APP_PORT": "80",
"APP_OPTIONAL_ADDON": '{"MODULE_VALUE":10,"MODULE_VALUE_DEFAULT":42}',
"APP_REQUIRED_PLUGIN": '{"POSTGRES_HOST":"localhost","POSTGRES_PORT":5432,"POSTGRES_USER":"foo","POSTGRES_PASSWORD":"secret","POSTGRES_DB":"foodb","POSTGRES_MINSIZE":2,"POSTGRES_MAXSIZE":50,"POSTGRES_CLIENT_NAME":null}',
"APP_REQUIRED_PLUGIN": '{"POSTGRES_HOST":"localhost","POSTGRES_PORT":5432,"POSTGRES_USER":"foo","POSTGRES_PASSWORD":"secret","POSTGRES_DB":"foodb","POSTGRES_MINSIZE":1,"POSTGRES_MAXSIZE":50,"POSTGRES_MAX_POOLSIZE":10,"POSTGRES_MAX_OVERFLOW":20,"POSTGRES_CLIENT_NAME":null}',
}

settings_2 = fake_settings_class()
Expand All @@ -261,7 +267,7 @@ def test_compact_format(
APP_HOST=localhost
APP_PORT=80
APP_OPTIONAL_ADDON='{"MODULE_VALUE": 10, "MODULE_VALUE_DEFAULT": 42}'
APP_REQUIRED_PLUGIN='{"POSTGRES_HOST": "localhost", "POSTGRES_PORT": 5432, "POSTGRES_USER": "foo", "POSTGRES_PASSWORD": "secret", "POSTGRES_DB": "foodb", "POSTGRES_MINSIZE": 2, "POSTGRES_MAXSIZE": 50, "POSTGRES_CLIENT_NAME": "None"}'
APP_REQUIRED_PLUGIN='{"POSTGRES_HOST": "localhost", "POSTGRES_PORT": 5432, "POSTGRES_USER": "foo", "POSTGRES_PASSWORD": "secret", "POSTGRES_DB": "foodb", "POSTGRES_MINSIZE": 1, "POSTGRES_MAXSIZE": 50, "POSTGRES_MAX_POOLSIZE": 10, "POSTGRES_MAX_OVERFLOW": 20, "POSTGRES_CLIENT_NAME": "None"}'
""",
)

Expand Down Expand Up @@ -292,10 +298,10 @@ def test_granular_format(
POSTGRES_PASSWORD=secret
# Database name
POSTGRES_DB=foodb
# Minimum number of connections in the pool
POSTGRES_MINSIZE=2
# Maximum number of connections in the pool
POSTGRES_MINSIZE=1
POSTGRES_MAXSIZE=50
POSTGRES_MAX_POOLSIZE=10
POSTGRES_MAX_OVERFLOW=20
# Name of the application connecting the postgres database, will default to use the host hostname (hostname on linux)
POSTGRES_CLIENT_NAME=None
""",
Expand All @@ -313,8 +319,10 @@ def test_granular_format(
"POSTGRES_USER": "foo",
"POSTGRES_PASSWORD": "secret",
"POSTGRES_DB": "foodb",
"POSTGRES_MINSIZE": 2,
"POSTGRES_MINSIZE": 1,
"POSTGRES_MAXSIZE": 50,
"POSTGRES_MAX_POOLSIZE": 10,
"POSTGRES_MAX_OVERFLOW": 20,
"POSTGRES_CLIENT_NAME": None,
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ async def db_engine(
state: AppState,
) -> AsyncGenerator[AsyncEngine, Any]:
async with contextlib.AsyncExitStack() as stack:
assert state.environment["POSTGRES_ENDPOINT"] # nosec
db_endpoint = state.environment["POSTGRES_ENDPOINT"]
assert state.environment["POSTGRES_HOST"] # nosec
assert state.environment["POSTGRES_PORT"] # nosec
db_endpoint = (
f"{state.environment['POSTGRES_HOST']}:{state.environment['POSTGRES_PORT']}"
)
if state.main_bastion_host:
assert state.ssh_key_path # nosec
db_host, db_port = db_endpoint.split(":")
Expand Down
4 changes: 3 additions & 1 deletion services/api-server/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ def default_app_env_vars(
env_vars["API_SERVER_DEV_FEATURES_ENABLED"] = "1"
env_vars["API_SERVER_LOG_FORMAT_LOCAL_DEV_ENABLED"] = "1"
env_vars["API_SERVER_PROMETHEUS_INSTRUMENTATION_ENABLED"] = "0"
env_vars["POSTGRES_MINSIZE"] = "2"
env_vars["POSTGRES_MINSIZE"] = "1"
env_vars["POSTGRES_MAXSIZE"] = "10"
env_vars["POSTGRES_MAX_POOLSIZE"] = "10"
env_vars["POSTGRES_MAX_OVERFLOW"] = "20"
env_vars["API_SERVER_CELERY"] = "null"
env_vars["API_SERVER_RABBITMQ"] = "null"

Expand Down
1 change: 0 additions & 1 deletion services/director-v2/.env-devel
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ DIRECTOR_V2_GENERIC_RESOURCE_PLACEMENT_CONSTRAINTS_SUBSTITUTIONS='{}'

LOG_LEVEL=DEBUG

POSTGRES_ENDPOINT=postgres:5432
POSTGRES_USER=test
POSTGRES_PASSWORD=test
POSTGRES_DB=test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def _get_environment_variables(
"DYNAMIC_SIDECAR_LOG_LEVEL": app_settings.DYNAMIC_SERVICES.DYNAMIC_SIDECAR.DYNAMIC_SIDECAR_LOG_LEVEL,
"DY_SIDECAR_LOG_FORMAT_LOCAL_DEV_ENABLED": f"{app_settings.DIRECTOR_V2_LOG_FORMAT_LOCAL_DEV_ENABLED}",
"POSTGRES_DB": f"{app_settings.POSTGRES.POSTGRES_DB}",
"POSTGRES_ENDPOINT": f"{app_settings.POSTGRES.POSTGRES_HOST}:{app_settings.POSTGRES.POSTGRES_PORT}",
"POSTGRES_HOST": f"{app_settings.POSTGRES.POSTGRES_HOST}",
"POSTGRES_PASSWORD": f"{app_settings.POSTGRES.POSTGRES_PASSWORD.get_secret_value()}",
"POSTGRES_PORT": f"{app_settings.POSTGRES.POSTGRES_PORT}",
Expand Down Expand Up @@ -458,9 +457,9 @@ async def get_dynamic_sidecar_spec( # pylint:disable=too-many-arguments# noqa:
dynamic_sidecar_settings=dynamic_sidecar_settings, app_settings=app_settings
)

assert scheduler_data.product_name is not None, (
"ONLY for legacy. This function should not be called with product_name==None"
) # nosec
assert (
scheduler_data.product_name is not None
), "ONLY for legacy. This function should not be called with product_name==None" # nosec

standard_simcore_docker_labels: dict[DockerLabelKey, str] = SimcoreContainerLabels(
user_id=scheduler_data.user_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"DYNAMIC_SIDECAR_TRACING",
"NODE_PORTS_400_REQUEST_TIMEOUT_ATTEMPTS",
"POSTGRES_DB",
"POSTGRES_ENDPOINT",
"POSTGRES_HOST",
"POSTGRES_PASSWORD",
"POSTGRES_PORT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def mock_env(
"DYNAMIC_SIDECAR_IMAGE": "local/dynamic-sidecar:MOCK",
"LOG_LEVEL": "DEBUG",
"POSTGRES_DB": "test",
"POSTGRES_ENDPOINT": "localhost:5432",
"POSTGRES_HOST": "localhost",
"POSTGRES_PASSWORD": "test",
"POSTGRES_PORT": "5432",
Expand Down Expand Up @@ -275,7 +274,6 @@ def expected_dynamic_sidecar_spec(
"POSTGRES_PORT": "5432",
"POSTGRES_USER": "test",
"POSTGRES_PASSWORD": "test",
"POSTGRES_ENDPOINT": "localhost:5432",
"RABBIT_HOST": "rabbit",
"RABBIT_PASSWORD": "adminadmin",
"RABBIT_PORT": "5672",
Expand Down
Loading
Loading