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

ORM: Cache the logger adapter for ProcessNode #6492

Merged

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Jun 28, 2024

The logger adapter was recreated each time the logger property of the ProcessNode was invoked. It is now cached on the node instance as its contents should not change for the lifetime of the node.

@@ -162,6 +162,7 @@ class ProcessNode(Sealable, Node):
METADATA_INPUTS_KEY: str = 'metadata_inputs'

_unstorable_message = 'only Data, WorkflowNode, CalculationNode or their subclasses can be stored'
_logger_adapter = None
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a class variable or an instance variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a class variable, but you are right it should be an instance variable. I have updated the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@khsrali Ok, it turned out it was a bit more difficult. We cannot initialize the _logger_adapter in the constructor, because that path is not followded when loading an existing node from the database. So we have initialize it in the logger property and initialize it as None as a class variable. Since logger sets the adapter on the instance, it won't pollute the class variable though, so it seems fine as a solution.

@sphuber sphuber force-pushed the feature/cache-process-node-logger-adapter branch 4 times, most recently from 80eb6bc to 194abbc Compare June 28, 2024 20:59
Copy link

codecov bot commented Jun 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.79%. Comparing base (ef60b66) to head (732d48a).
Report is 115 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6492      +/-   ##
==========================================
+ Coverage   77.51%   77.79%   +0.29%     
==========================================
  Files         560      561       +1     
  Lines       41444    41809     +365     
==========================================
+ Hits        32120    32521     +401     
+ Misses       9324     9288      -36     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sphuber sphuber force-pushed the feature/cache-process-node-logger-adapter branch from 194abbc to ea88d34 Compare June 28, 2024 21:52
@sphuber sphuber requested a review from khsrali June 28, 2024 22:14
@@ -251,7 +259,16 @@ def logger(self):
"""
from aiida.orm.utils.log import create_logger_adapter

return create_logger_adapter(self._logger, self)
# If the node is not yet stored, there is no point in creating the logger adapter yet as the ``DbLogHandler``
# it configures only is triggered for stored nodes otherwise it cannot link the log message to the node.
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe consider rephrasing # it configures only is triggered

src/aiida/orm/nodes/process/process.py Outdated Show resolved Hide resolved
The logger adapter was recreated each time the `logger` property of the
`ProcessNode` was invoked. It is now created once in the `logger`
property. The created logger adapter is assigned to the `_logger_adapter`
attribute such that it can simply be returned at the next invocation.

The initialization of the adapter cannot be done in the constructor as
that route is not taken if an existing node is loaded from the database.

Finally, the `logger` property only creates and returns the adapter when
the node is stored. Otherwise it simply returns the base logger instance.
This is because the logger adapter only works for stored nodes and if it
were instantiated at the point when the node is unstored, it would not
be regenerated once the node is stored, and so the `DbLogHandler` will
never be able to persist log messages to the database.
@sphuber sphuber force-pushed the feature/cache-process-node-logger-adapter branch from ea88d34 to 1e81fde Compare June 29, 2024 11:10
@sphuber sphuber force-pushed the feature/cache-process-node-logger-adapter branch from 1e81fde to 7bb9e62 Compare June 29, 2024 11:49
@sphuber sphuber requested a review from khsrali June 29, 2024 11:55
Copy link
Contributor

@khsrali khsrali left a comment

Choose a reason for hiding this comment

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

thanks a lot @sphuber 👍

src/aiida/orm/nodes/process/process.py Outdated Show resolved Hide resolved
sphuber and others added 2 commits June 29, 2024 20:16
Co-authored-by: Ali Khosravi <khsrali@gmail.com>
@sphuber sphuber merged commit 1d104d0 into aiidateam:main Jun 29, 2024
11 checks passed
@sphuber sphuber deleted the feature/cache-process-node-logger-adapter branch June 29, 2024 20:00
mikibonacci pushed a commit to mikibonacci/aiida-core that referenced this pull request Sep 3, 2024
The logger adapter was recreated each time the `logger` property of the
`ProcessNode` was invoked. It is now created once in the `logger`
property. The created logger adapter is assigned to the `_logger_adapter`
attribute such that it can simply be returned at the next invocation.
The initialization of the adapter cannot be done in the constructor as
that route is not taken if an existing node is loaded from the database.

Finally, the `logger` property only creates and returns the adapter when
the node is stored. Otherwise it simply returns the base logger instance.
This is because the logger adapter only works for stored nodes and if it
were instantiated at the point when the node is unstored, it would not
be regenerated once the node is stored, and so the `DbLogHandler` will
never be able to persist log messages to the database.
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