Skip to content

Commit

Permalink
PsqlDos: Add migration to remove hashes for all CalcJobNodes
Browse files Browse the repository at this point in the history
The algorithm for computing the hashes of `CalcJobNodes` contained a bug
which was fixed in 685e0f8. This means
however that all existing hashes for `CalcJobNodes` are inconsistent
with the new algorithm. This would mean that valid cache hits would be
missed, and worse, different calculatons could end up with the same hash
and mistakingly be cached from one another.

The migration drops the `_aiida_hash` extra which is used to store the
node's hash. A warning is emitted to notify the user, suggesting to run
`verdi node rehash` to recompute the hash of all `CalcJobNodes`.
  • Loading branch information
sphuber committed May 15, 2023
1 parent f84fe5b commit 7ad9168
Show file tree
Hide file tree
Showing 3 changed files with 641 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
# pylint: disable=invalid-name,no-member
"""Drop the hashes for all ``CalcJobNode`` instances.
The computed hash erroneously included the hash of the file repository. This was present as of v2.0 and so all nodes
created with versions since then will have incorrect hashes.
Revision ID: main_0002
Revises: main_0001
Create Date: 2023-05-05
"""
from alembic import op

from aiida.storage.psql_dos.migrations.utils.integrity import drop_hashes

revision = 'main_0002'
down_revision = 'main_0001'
branch_labels = None
depends_on = None


def upgrade():
"""Migrations for the upgrade."""
drop_hashes(
op.get_bind(), hash_extra_key='_aiida_hash', entry_point_string='aiida.node:process.calculation.calcjob'
)


def downgrade():
"""Migrations for the downgrade."""
drop_hashes(
op.get_bind(), hash_extra_key='_aiida_hash', entry_point_string='aiida.node:process.calculation.calcjob'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Test ``main_0002_recompute_hash_calc_job_node.py``."""
from aiida.common import timezone
from aiida.common.utils import get_new_uuid
from aiida.storage.psql_dos.migrator import PsqlDosMigrator


def test_migration(perform_migrations: PsqlDosMigrator):
"""Test the migration removes the ``_aiida_hash`` extra of all ``CalcJobNodes`` and those nodes only."""
perform_migrations.migrate_up('main@main_0001')

user_model = perform_migrations.get_current_table('db_dbuser')
node_model = perform_migrations.get_current_table('db_dbnode')

with perform_migrations.session() as session:
user = user_model(email='test', first_name='test', last_name='test', institution='test')
session.add(user)
session.commit()

calcjob = node_model(
uuid=get_new_uuid(),
user_id=user.id,
ctime=timezone.now(),
mtime=timezone.now(),
label='test',
description='',
node_type='process.calculation.calcjob.CalcJobNode.',
process_type='aiida.calculations:core.arithmetic.add',
attributes={'parser_name': 'core.arithmetic.add'},
repository_metadata={},
extras={
'_aiida_hash': 'hash',
'other_extra': 'value'
}
)
workflow = node_model(
uuid=get_new_uuid(),
user_id=user.id,
ctime=timezone.now(),
mtime=timezone.now(),
label='test',
description='',
node_type='process.workflow.workchain.WorkChainNode.',
process_type='aiida.workflows:core.arithmetic.add_multiply',
repository_metadata={},
extras={
'_aiida_hash': 'hash',
'other_extra': 'value'
}
)
session.add_all((calcjob, workflow))
session.commit()

calcjob_id = calcjob.id
workflow_id = workflow.id

# Perform the migration that is being tested.
perform_migrations.migrate_up('main@main_0002')

node_model = perform_migrations.get_current_table('db_dbnode')

# Check that the ``_aiida_hash`` extra was removed, and only that extra, and only for the calcjob, not the workflow.
with perform_migrations.session() as session:
calcjob = session.query(node_model).filter(node_model.id == calcjob_id).one()
assert calcjob.extras == {'other_extra': 'value'}

workflow = session.query(node_model).filter(node_model.id == workflow_id).one()
assert workflow.extras == {'_aiida_hash': 'hash', 'other_extra': 'value'}
Loading

0 comments on commit 7ad9168

Please sign in to comment.