forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Replace aiopg in catalog service (ITISFoundation#2869)
* catalog uses sqlalchemy in async mode * use async sqlalchemy in tests too * added caching of director-v0 services
- Loading branch information
Showing
28 changed files
with
571 additions
and
332 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
packages/postgres-database/src/simcore_postgres_database/utils_aiosqlalchemy.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,32 @@ | ||
from typing import Any, Dict | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.ext.asyncio import AsyncEngine | ||
|
||
from .utils_migration import get_current_head | ||
|
||
|
||
async def get_pg_engine_stateinfo(engine: AsyncEngine) -> Dict[str, Any]: | ||
return { | ||
"current pool connections": f"{engine.pool.checkedin()=},{engine.pool.checkedout()=}", | ||
} | ||
|
||
|
||
class DBMigrationError(RuntimeError): | ||
pass | ||
|
||
|
||
async def raise_if_migration_not_ready(engine: AsyncEngine): | ||
"""Ensures db migration is complete | ||
:raises DBMigrationError | ||
""" | ||
async with engine.connect() as conn: | ||
version_num = await conn.scalar( | ||
sa.DDL('SELECT "version_num" FROM "alembic_version"') | ||
) | ||
head_version_num = get_current_head() | ||
if version_num != head_version_num: | ||
raise DBMigrationError( | ||
f"Migration is incomplete, expected {head_version_num} but got {version_num}" | ||
) |
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
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
22 changes: 8 additions & 14 deletions
22
services/catalog/src/simcore_service_catalog/api/dependencies/database.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 |
---|---|---|
@@ -1,38 +1,32 @@ | ||
import logging | ||
from typing import AsyncGenerator, Callable, Type | ||
|
||
from aiopg.sa import Engine | ||
from fastapi import Depends | ||
from fastapi.requests import Request | ||
from sqlalchemy.ext.asyncio import AsyncEngine | ||
|
||
from ...db.repositories import BaseRepository | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _get_db_engine(request: Request) -> Engine: | ||
def _get_db_engine(request: Request) -> AsyncEngine: | ||
return request.app.state.engine | ||
|
||
|
||
def get_repository(repo_type: Type[BaseRepository]) -> Callable: | ||
async def _get_repo( | ||
engine: Engine = Depends(_get_db_engine), | ||
engine: AsyncEngine = Depends(_get_db_engine), | ||
) -> AsyncGenerator[BaseRepository, None]: | ||
# NOTE: 2 different ideas were tried here with not so good | ||
# 1st one was acquiring a connection per repository which lead to the following issue https://github.com/ITISFoundation/osparc-simcore/pull/1966 | ||
# 2nd one was acquiring a connection per request which works but blocks the director-v2 responsiveness once | ||
# the max amount of connections is reached | ||
# now the current solution is to acquire connection when needed. | ||
available_engines = engine.maxsize - (engine.size - engine.freesize) | ||
if available_engines <= 1: | ||
logger.warning( | ||
"Low pg connections available in pool: pool size=%d, acquired=%d, free=%d, reserved=[%d, %d]", | ||
engine.size, | ||
engine.size - engine.freesize, | ||
engine.freesize, | ||
engine.minsize, | ||
engine.maxsize, | ||
) | ||
# now the current solution is to connect connection when needed. | ||
logger.info( | ||
"%s", | ||
f"current pool connections {engine.pool.checkedin()=},{engine.pool.checkedout()=}", | ||
) | ||
yield repo_type(db_engine=engine) | ||
|
||
return _get_repo |
Oops, something went wrong.