Skip to content

fix(backend): various database session fixes #6309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: stable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
44 changes: 31 additions & 13 deletions backend/infrahub/core/constraint/node/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,37 @@ def __init__(
self.uniqueness_constraint = uniqueness_constraint
self.relationship_manager_constraints = relationship_manager_constraints

async def _check(
self,
db: InfrahubDatabase,
node: Node,
field_filters: list[str] | None = None,
skip_uniqueness_check: bool = False,
) -> None:
await node.resolve_relationships(db=db)

if not skip_uniqueness_check:
self.uniqueness_constraint.db = db
await self.uniqueness_constraint.check(node, filters=field_filters)

for relationship_name in node.get_schema().relationship_names:
if field_filters and relationship_name not in field_filters:
continue
relationship_manager: RelationshipManager = getattr(node, relationship_name)
await relationship_manager.fetch_relationship_ids(db=db, force_refresh=True)
for relationship_constraint in self.relationship_manager_constraints:
relationship_constraint.db = db
await relationship_constraint.check(relm=relationship_manager, node_schema=node.get_schema())

async def check(
self, node: Node, field_filters: list[str] | None = None, skip_uniqueness_check: bool = False
) -> None:
async with self.db.start_session() as db:
await node.resolve_relationships(db=db)

if not skip_uniqueness_check:
await self.uniqueness_constraint.check(node, filters=field_filters)

for relationship_name in node.get_schema().relationship_names:
if field_filters and relationship_name not in field_filters:
continue
relationship_manager: RelationshipManager = getattr(node, relationship_name)
await relationship_manager.fetch_relationship_ids(db=db, force_refresh=True)
for relationship_constraint in self.relationship_manager_constraints:
await relationship_constraint.check(relm=relationship_manager, node_schema=node.get_schema())
if not self.db.is_transaction:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be careful here, as far as I remember one of the reason we are starting a new session is that otherwise we can't use the parallel runtime to check the uniqueness constraint and it will impact performance
I could be wrong here, I need to look deeper in the code

async with self.db.start_session(read_only=False) as db:
await self._check(
db=db, node=node, field_filters=field_filters, skip_uniqueness_check=skip_uniqueness_check
)
else:
await self._check(
db=self.db, node=node, field_filters=field_filters, skip_uniqueness_check=skip_uniqueness_check
)
1 change: 0 additions & 1 deletion backend/infrahub/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ def start_transaction(self, schemas: list[SchemaBranch] | None = None) -> Infrah
schemas=schemas or self._schemas.values(),
db_manager=self.manager,
driver=self._driver,
session=self._session,
session_mode=self._session_mode,
queries_names_to_config=self.queries_names_to_config,
**context,
Expand Down
Loading