-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (⚠️ devops) 🗃️ Is922 resource tracking/1. version of regular scrapi…
…ng (#4380)
- Loading branch information
1 parent
b537b67
commit c20bf3e
Showing
41 changed files
with
1,578 additions
and
277 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...tgres_database/migration/versions/6e91067932f2_adding_resource_tracker_container_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""adding resource tracker container table | ||
Revision ID: 6e91067932f2 | ||
Revises: 52cf00912ad9 | ||
Create Date: 2023-06-21 14:12:40.292816+00:00 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6e91067932f2" | ||
down_revision = "52cf00912ad9" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"resource_tracker_container", | ||
sa.Column("container_id", sa.String(), nullable=False), | ||
sa.Column("image", sa.String(), nullable=False), | ||
sa.Column("user_id", sa.BigInteger(), nullable=False), | ||
sa.Column("product_name", sa.String(), nullable=False), | ||
sa.Column( | ||
"service_settings_reservation_nano_cpus", sa.BigInteger(), nullable=True | ||
), | ||
sa.Column( | ||
"service_settings_reservation_memory_bytes", sa.BigInteger(), nullable=True | ||
), | ||
sa.Column( | ||
"service_settings_reservation_additional_info", | ||
postgresql.JSONB(astext_type=sa.Text()), | ||
nullable=False, | ||
), | ||
sa.Column("container_cpu_usage_seconds_total", sa.Float(), nullable=False), | ||
sa.Column("prometheus_created", sa.DateTime(timezone=True), nullable=False), | ||
sa.Column( | ||
"prometheus_last_scraped", sa.DateTime(timezone=True), nullable=False | ||
), | ||
sa.Column( | ||
"modified", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("now()"), | ||
nullable=False, | ||
), | ||
sa.PrimaryKeyConstraint("container_id", name="resource_tracker_container_pkey"), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("resource_tracker_container") | ||
# ### end Alembic commands ### |
74 changes: 74 additions & 0 deletions
74
packages/postgres-database/src/simcore_postgres_database/models/resource_tracker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" resource_tracker_container table | ||
- Table where we store the resource usage of each container that | ||
we scrape via resource-usage-tracker service | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.dialects.postgresql import JSONB | ||
|
||
from ._common import column_modified_datetime | ||
from .base import metadata | ||
|
||
resource_tracker_container = sa.Table( | ||
"resource_tracker_container", | ||
metadata, | ||
sa.Column( | ||
"container_id", | ||
sa.String, | ||
nullable=False, | ||
doc="Refers to container id scraped via Prometheus", | ||
), | ||
sa.Column( | ||
"image", | ||
sa.String, | ||
nullable=False, | ||
doc="image label scraped via Prometheus (taken from container labels), ex. registry.osparc.io/simcore/services/dynamic/jupyter-smash:3.0.9", | ||
), | ||
sa.Column( | ||
"user_id", | ||
sa.BigInteger, | ||
nullable=False, | ||
doc="user_id label scraped via Prometheus (taken from container labels)", | ||
), | ||
sa.Column( | ||
"product_name", | ||
sa.String, | ||
nullable=False, | ||
doc="product_name label scraped via Prometheus (taken from container labels)", | ||
), | ||
sa.Column( | ||
"service_settings_reservation_nano_cpus", | ||
sa.BigInteger, | ||
nullable=True, | ||
doc="CPU resource allocated to a container, ex.500000000 means that the container is allocated 0.5 CPU shares", | ||
), | ||
sa.Column( | ||
"service_settings_reservation_memory_bytes", | ||
sa.BigInteger, | ||
nullable=True, | ||
doc="memory limit in bytes scraped via Prometheus", | ||
), | ||
sa.Column( | ||
"service_settings_reservation_additional_info", | ||
JSONB, | ||
nullable=False, | ||
doc="storing additional information about the reservation settings", | ||
), | ||
sa.Column("container_cpu_usage_seconds_total", sa.Float, nullable=False), | ||
sa.Column( | ||
"prometheus_created", | ||
sa.DateTime(timezone=True), | ||
nullable=False, | ||
doc="First container creation timestamp (UTC timestamp)", | ||
), | ||
sa.Column( | ||
"prometheus_last_scraped", | ||
sa.DateTime(timezone=True), | ||
nullable=False, | ||
doc="Last prometheus scraped timestamp (UTC timestamp)", | ||
), | ||
column_modified_datetime(timezone=True), | ||
# --------------------------- | ||
sa.PrimaryKeyConstraint("container_id", name="resource_tracker_container_pkey"), | ||
) |
58 changes: 58 additions & 0 deletions
58
packages/service-library/src/servicelib/db_async_engine.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
from settings_library.postgres import PostgresSettings | ||
from simcore_postgres_database.utils_aiosqlalchemy import ( | ||
get_pg_engine_stateinfo, | ||
raise_if_migration_not_ready, | ||
) | ||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine | ||
from tenacity import retry | ||
|
||
from .retry_policies import PostgresRetryPolicyUponInitialization | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@retry(**PostgresRetryPolicyUponInitialization(logger).kwargs) | ||
async def connect_to_db(app: FastAPI, cfg: PostgresSettings) -> None: | ||
logger.debug("Connecting db ...") | ||
|
||
engine: AsyncEngine = create_async_engine( | ||
cfg.dsn_with_async_sqlalchemy, | ||
pool_size=cfg.POSTGRES_MINSIZE, | ||
max_overflow=cfg.POSTGRES_MAXSIZE - cfg.POSTGRES_MINSIZE, | ||
connect_args={ | ||
"server_settings": {"application_name": cfg.POSTGRES_CLIENT_NAME} | ||
}, | ||
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 | ||
) | ||
|
||
logger.debug("Connected to %s", engine.url) # pylint: disable=no-member | ||
|
||
logger.debug("Checking db migration...") | ||
try: | ||
await raise_if_migration_not_ready(engine) | ||
except Exception: | ||
# NOTE: engine must be closed because retry will create a new engine | ||
await engine.dispose() | ||
raise | ||
|
||
logger.debug("Migration up-to-date") | ||
|
||
app.state.engine = engine | ||
|
||
logger.debug( | ||
"Setup engine: %s", | ||
await get_pg_engine_stateinfo(engine), | ||
) | ||
|
||
|
||
async def close_db_connection(app: FastAPI) -> None: | ||
logger.debug("Disconnecting db ...") | ||
|
||
if engine := app.state.engine: | ||
await engine.dispose() | ||
|
||
logger.debug("Disconnected from %s", engine.url) # pylint: disable=no-member |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,12 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
from servicelib.retry_policies import PostgresRetryPolicyUponInitialization | ||
from settings_library.postgres import PostgresSettings | ||
from simcore_postgres_database.utils_aiosqlalchemy import ( | ||
get_pg_engine_stateinfo, | ||
raise_if_migration_not_ready, | ||
) | ||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine | ||
from tenacity import retry | ||
|
||
from .repositories.products import ProductsRepository | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@retry(**PostgresRetryPolicyUponInitialization(logger).kwargs) | ||
async def connect_to_db(app: FastAPI) -> None: | ||
logger.debug("Connecting db ...") | ||
cfg: PostgresSettings = app.state.settings.CATALOG_POSTGRES | ||
|
||
engine: AsyncEngine = create_async_engine( | ||
cfg.dsn_with_async_sqlalchemy, | ||
pool_size=cfg.POSTGRES_MINSIZE, | ||
max_overflow=cfg.POSTGRES_MAXSIZE - cfg.POSTGRES_MINSIZE, | ||
connect_args={ | ||
"server_settings": {"application_name": cfg.POSTGRES_CLIENT_NAME} | ||
}, | ||
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 | ||
) | ||
|
||
logger.debug("Connected to %s", engine.url) # pylint: disable=no-member | ||
|
||
logger.debug("Checking db migration...") | ||
try: | ||
await raise_if_migration_not_ready(engine) | ||
except Exception: | ||
# NOTE: engine must be closed because retry will create a new engine | ||
await engine.dispose() | ||
raise | ||
|
||
logger.debug("Migration up-to-date") | ||
|
||
app.state.engine = engine | ||
|
||
logger.debug( | ||
"Setup engine: %s", | ||
await get_pg_engine_stateinfo(engine), | ||
) | ||
|
||
|
||
async def close_db_connection(app: FastAPI) -> None: | ||
logger.debug("Disconnecting db ...") | ||
|
||
if engine := app.state.engine: | ||
await engine.dispose() | ||
|
||
logger.debug("Disconnected from %s", engine.url) # pylint: disable=no-member | ||
|
||
|
||
async def setup_default_product(app: FastAPI): | ||
repo = ProductsRepository(db_engine=app.state.engine) | ||
app.state.default_product_name = await repo.get_default_product_name() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
RESOURCE_USAGE_TRACKER_DEV_FEATURES_ENABLED=1 | ||
|
||
LOG_LEVEL=DEBUG | ||
|
||
POSTGRES_USER=test | ||
POSTGRES_PASSWORD=test | ||
POSTGRES_DB=test | ||
POSTGRES_HOST=localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.