mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-07-12 13:29:04 +00:00
The modelling_e2e Lambda runs on a single-connection pool (pool_size=1, max_overflow=0) so one invocation uses one Postgres connection. But re-hydrating a Property through PostgresUnitOfWork resolved its Landlord Overrides through a PropertyOverridesPostgresReader built from the unit's session *factory* — which opens a brand-new Session per call. While the unit's own read transaction was still open (PropertyPostgresRepository.get_many had checked out the connection), that second Session asked the pool for a second connection, found none, and timed out after 30s: QueuePool limit of size 1 overflow 0 reached, connection timed out, timeout 30.00 The baseline stage (PropertyBaselineOrchestrator.run -> uow.property.get_many -> landlord overrides) hit this on every invocation. Read the overrides on the unit's OWN session instead. property_overrides is committed reference data, so reading it inside the unit's transaction sees the same rows and keeps the invocation on one connection. Extract the query/mapping into a shared helper and add OpenSessionPropertyOverridesReader (reads on a caller-owned, already-open session without closing it) for the unit; the standalone PropertyOverridesPostgresReader still opens its own short session for use outside a unit. Regression test pins the invariant with a real pool_size=1/max_overflow=0 engine: without the fix it reproduces the exact QueuePool timeout. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
83 lines
3.4 KiB
Python
83 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from types import TracebackType
|
|
from typing import Optional
|
|
|
|
from sqlmodel import Session
|
|
|
|
from repositories.property_baseline.property_baseline_postgres_repository import (
|
|
PropertyBaselinePostgresRepository,
|
|
)
|
|
from repositories.epc.epc_postgres_repository import EpcPostgresRepository
|
|
from repositories.plan.plan_postgres_repository import PlanPostgresRepository
|
|
from repositories.product.composite_product_repository import (
|
|
catalogue_with_off_catalogue_overrides,
|
|
)
|
|
from repositories.property.property_overrides_postgres_reader import (
|
|
OpenSessionPropertyOverridesReader,
|
|
)
|
|
from repositories.property.property_postgres_repository import (
|
|
PropertyPostgresRepository,
|
|
)
|
|
from repositories.scenario.scenario_postgres_repository import (
|
|
ScenarioPostgresRepository,
|
|
)
|
|
from repositories.solar.solar_postgres_repository import SolarPostgresRepository
|
|
from repositories.spatial.spatial_postgres_repository import SpatialPostgresRepository
|
|
from repositories.unit_of_work import UnitOfWork
|
|
|
|
|
|
class PostgresUnitOfWork(UnitOfWork):
|
|
"""Postgres-backed Unit of Work: one ``Session``, all repos bound to it.
|
|
|
|
Built from a session factory (a module-scoped engine + sessionmaker in
|
|
production, ADR-0012) so the connection pool is reused across warm Lambda
|
|
invocations. The session is opened on ``__enter__`` and closed on
|
|
``__exit__``; a fresh instance is one single-use unit.
|
|
"""
|
|
|
|
def __init__(self, session_factory: Callable[[], Session]) -> None:
|
|
self._session_factory = session_factory
|
|
|
|
def __enter__(self) -> "PostgresUnitOfWork":
|
|
self._session = self._session_factory()
|
|
epc_repo = EpcPostgresRepository(self._session)
|
|
spatial_repo = SpatialPostgresRepository(self._session)
|
|
# Fold Landlord Overrides onto the Effective EPC on every re-hydration
|
|
# (ADR-0032), so what the Baseline orchestrator scores off ``uow.property``
|
|
# matches what the Plan was modelled from. ``property_overrides`` is
|
|
# committed reference data, but the reader must read on THIS uow's session
|
|
# — a second session opened concurrently checks out a second connection
|
|
# and deadlocks the single-connection pool while this uow's transaction is
|
|
# open. Reading committed data inside the uow's transaction is correct and
|
|
# keeps the invocation on one connection.
|
|
overrides_reader = OpenSessionPropertyOverridesReader(self._session)
|
|
self.property = PropertyPostgresRepository(
|
|
self._session, epc_repo, spatial_repo, overrides_reader
|
|
)
|
|
self.epc = epc_repo
|
|
self.solar = SolarPostgresRepository(self._session)
|
|
self.spatial = spatial_repo
|
|
self.property_baseline = PropertyBaselinePostgresRepository(self._session)
|
|
self.scenario = ScenarioPostgresRepository(self._session)
|
|
self.product = catalogue_with_off_catalogue_overrides(self._session)
|
|
self.plan = PlanPostgresRepository(self._session)
|
|
return self
|
|
|
|
def __exit__(
|
|
self,
|
|
exc_type: Optional[type[BaseException]],
|
|
exc: Optional[BaseException],
|
|
tb: Optional[TracebackType],
|
|
) -> None:
|
|
try:
|
|
self._session.rollback()
|
|
finally:
|
|
self._session.close()
|
|
|
|
def commit(self) -> None:
|
|
self._session.commit()
|
|
|
|
def rollback(self) -> None:
|
|
self._session.rollback()
|