Skip to content
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

👌 IMPROVE: Check nodes are from same backend in validate_link #5462

Merged
merged 2 commits into from
Jul 11, 2023

Conversation

chrisjsewell
Copy link
Member

Since here:

link = DbLink(input_id=source.id, output_id=self.id, label=link_label, type=link_type.value)

Only the node ID's are used to create the DbLink row, it would be possible currently to accidentally pass it two nodes associated with different backends, and so create an incorrect link.

We could possibly use the actual DbNodes, to instantiate the DbLink, and have sqlalchemy check that they are associated with the same Session,
but adding the check in validate_link should catch any issue earlier on, and make it backend agnostic.

@chrisjsewell
Copy link
Member Author

FYI, this is how you would use source.bare_model/self.bare_model, instead of .id, and what would happen if the backends were different:

engine = sa.create_engine('sqlite:///:memory:', future=True, echo=False)
Base.metadata.create_all(engine)

session1 = Session(bind=engine, future=True)
session2 = Session(bind=engine, future=True)

node1 = DbNode(node_type='data.dict.dict', label='test1')
session1.add(node1)

node2 = DbNode(node_type='data.dict.dict', label='test2')
session2.add(node2)

link = DbLink(input=node1, output=node2, label='test', type='test')

session1.add(link)
InvalidRequestError: Object '<DbNode at 0x7fc6f3754700>' is already attached to session '9' (this is '8')

Perhaps could add this also 🤷

aiida/orm/utils/links.py Outdated Show resolved Hide resolved
@sphuber
Copy link
Contributor

sphuber commented Jul 11, 2023

Ran a quick benchmark to get a sense of the impact on performance and it seems negligible. Both get roughly 3.00 ms per loop when calling validate_link with %timeit in a loop.

@chrisjsewell
Copy link
Member Author

Cool cheers, well if you're happy then I'm happy lol

@sphuber
Copy link
Contributor

sphuber commented Jul 11, 2023

main

In [1]: from aiida.orm.utils.links import validate_link
   ...: from aiida.common.links import LinkType
   ...: a = CalcJobNode()
   ...: b = Int(2)

In [2]: %timeit validate_link(a, b, LinkType.CREATE, 'create')
2.9 ms ± 281 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [3]: %timeit validate_link(a, b, LinkType.CREATE, 'create')
2.89 ms ± 255 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [4]: %timeit validate_link(a, b, LinkType.CREATE, 'create')
2.7 ms ± 87.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

branch

In [1]: from aiida.orm.utils.links import validate_link
   ...: from aiida.common.links import LinkType
   ...: a = CalcJobNode()
   ...: b = Int(2)

In [2]: 

In [2]: %timeit validate_link(a, b, LinkType.CREATE, 'create')
2.73 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [3]: %timeit validate_link(a, b, LinkType.CREATE, 'create')
2.87 ms ± 240 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [4]: %timeit validate_link(a, b, LinkType.CREATE, 'create')
2.62 ms ± 31.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@sphuber sphuber self-requested a review July 11, 2023 13:54
Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

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

Thanks @chrisjsewell

@sphuber sphuber merged commit 7bd546e into aiidateam:main Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants