Skip to content

Commit

Permalink
Add lock on object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasG0 committed Dec 11, 2024
1 parent 65cc50b commit 349f075
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion backend/infrahub/graphql/mutations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from infrahub.log import get_log_data, get_logger
from infrahub.worker import WORKER_IDENTITY

from ...lock import InfrahubMultiLock
from .node_getter.by_default_filter import MutationNodeGetterByDefaultFilter
from .node_getter.by_hfid import MutationNodeGetterByHfid
from .node_getter.by_id import MutationNodeGetterById
Expand Down Expand Up @@ -144,7 +145,33 @@ async def mutate_create(
) -> tuple[Node, Self]:
context: GraphqlContext = info.context
db = database or context.db
obj = await cls.mutate_create_object(data=data, db=db, branch=branch)

# TODO clarify whether we want to lock on any ancestor kind having uniqueness_constraint,
# or if object / first ancestor kind having uniqueness_constraint is enough
kinds_to_lock = []
if cls._meta.schema.uniqueness_constraints:
# kinds_to_lock = [cls._meta.schema.kind]
kinds_to_lock.append(cls._meta.schema.kind)
else:
try:
ancestors_kinds = cls._meta.schema.inherit_from
except AttributeError:
# kinds_to_lock = []
pass
else:
ancestors_kinds_with_uc = [
kind for kind in ancestors_kinds if registry.schema.get(name=kind).uniqueness_constraints
]
# kinds_to_lock = ancestors_kinds_with_uc[0] if len(ancestors_kinds_with_uc) > 0 else []
kinds_to_lock.extend(ancestors_kinds_with_uc)

if len(kinds_to_lock) > 0:
lock_names = [f"global.object.{kind}" for kind in kinds_to_lock]
async with InfrahubMultiLock(_registry=registry, locks=lock_names):
obj = await cls.mutate_create_object(data=data, db=db, branch=branch)
else:
obj = await cls.mutate_create_object(data=data, db=db, branch=branch)

result = await cls.mutate_create_to_graphql(info=info, db=db, obj=obj)
return obj, result

Expand Down

0 comments on commit 349f075

Please sign in to comment.