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

Process: refactor out input handling specific to CalcJob #5539

Merged
merged 2 commits into from
May 25, 2022

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented May 23, 2022

Fixes #5537

The CalcJob subclass of Process adds inputs that are specific to its
implementation, namely code, metadata.computer and metadata.options.
Nevertheless, these were being handled in the Process class making
assumptions about the exact type of the code input.

The code is refactored to move the CalcJob specific logic to its
implementation. The signature of _setup_metadata had to be changed to
pass in a copy of the metadata inputs as the subclass needs to pop its
custom keys such that Process won't raise on keys it doesn't recognize.

The _setup_inputs implementation on CalcJob is simplified as it now
just needs to set the Computer of the Code if one hasn't already
been set in the _setup_metadata which is called before it.

The `CalcJob` subclass of `Process` adds inputs that are specific to its
implementation, namely `code`, `metadata.computer` and `metadata.options.
Nevertheless, these were being handled in the `Process` class making
assumptions about the exact type of the `code` input.

The code is refactored to move the `CalcJob` specific logic to its
implementation. The signature of `_setup_metadata` had to be changed to
pass in a copy of the metadata inputs as the subclass needs to pop its
custom keys such that `Process` won't raise on keys it doesn't recognize.

The `_setup_inputs` implementation on `CalcJob` is simplified as it now
just needs to set the `Computer` of the `Code` if one hasn't already
been set in the `_setup_metadata` which is called before it.
@sphuber sphuber requested a review from unkcpz May 23, 2022 20:37
Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

@sphuber thanks! Looks all good. Only have two questions if you can elaborate a bit.

"""Initialize the profile."""
# pylint: disable=attribute-defined-outside-init
self.computer = aiida_localhost
self.inputs = {
'code': aiida_local_code_factory('core.arithmetic.add', '/bin/bash', computer=aiida_localhost),
Copy link
Member

Choose a reason for hiding this comment

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

Just out of curiosity. I think the code is mandatory input for calcjob. This is not in the original code, but why did that not fail the test?

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 in fact not required in the port. For normal executions it is sort of required because the validator will make sure it is defined. However, there is an exception for imported jobs. See these lines

remote_folder = inputs.get('remote_folder', None)
if remote_folder is not None:
# The `remote_folder` input has been specified and so this concerns an import run, which means that neither
# a `Code` nor a `Computer` are required. However, they are allowed to be specified but will not be explicitly
# checked for consistency.
return None

If a remote_folder is defined, it is an import job and so the code is not required.

@@ -684,27 +685,22 @@ def _setup_db_record(self) -> None:
elif isinstance(self.node, orm.WorkflowNode):
self.node.base.links.add_incoming(parent_calc, LinkType.CALL_WORK, self.metadata.call_link_label)

self._setup_metadata()
self._setup_metadata(copy.copy(dict(self.inputs.metadata)))
Copy link
Member

Choose a reason for hiding this comment

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

I do not quite get the idea of using copy here. Could you give me an example of a subclass that has a custom key, what issue will happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The CalcJob, for example, defines the computer and options keys. It needs to be able to pop those, otherwise this line

raise RuntimeError(f'unsupported metadata key: {name}')

will raise in the Process._setup_metadata step.

# in the ``metadata`` inputs, set the computer associated with the ``code`` input. Note that not all ``code``s
# will have an associated computer, but in that case the ``computer`` property should return ``None`` and
# nothing would change anyway.
if not self.node.computer:
Copy link
Member

Choose a reason for hiding this comment

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

I am thinking is there any possibility that _setup_inputs is called before _setup_metadata? then the computer is not set yet and will cause a problem?

Copy link
Member

Choose a reason for hiding this comment

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

Okay, nevermind. It is sure about the order in _setup_db_record

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right though, if we were to change that in Process._setup_db_record it would fail.

@sphuber sphuber merged commit 408efa7 into aiidateam:main May 25, 2022
@sphuber sphuber deleted the fix/5537/calcjob-process-separation branch May 25, 2022 06:28
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.

Remove setting of CalcJob specific inputs from Process base class
2 participants