Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
'task': 'core.common.tasks.vacuum_and_analyze_db',
'schedule': crontab(0, 1), # Run at 1 am
},
'recheck-expansion-health': {
'task': 'core.common.tasks.recheck_expansion_health',
'schedule': crontab(0, 2), # Run at 2 am
},
'expire-celery-tasks': {
'task': 'core.common.tasks.expire_old_celery_tasks',
'schedule': crontab(0, 1), # Run at 1 am
Expand Down
5 changes: 5 additions & 0 deletions core/collections/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
UNRESOLVED_RESOURCE_VERSION_TYPE = 'reference.resource_version'
INTENSIONAL_REFERENCE_RESOURCE_VERSIONS_TOGGLE = 'INTENSIONAL_REFERENCE_RESOURCE_VERSIONS_TOGGLE'
EXPANSION_LOCKED_REPO_VERSION_TOGGLE = 'EXPANSION_LOCKED_REPO_VERSION_TOGGLE'
EXPANSION_HEALTH_TOGGLE = 'EXPANSION_HEALTH_TOGGLE'
EXPANSION_HEALTH_DEBOUNCE_SECONDS = 60
# a repo version's fields that expansion health reads
HEALTH_RELEVANT_VERSION_FIELDS = {'released', 'retired', 'is_active', 'public_access', 'expansion_uri'}
EXPANSION_HEALTH_RECHECK_LIMIT = 1000
VERSION_EXPANSION_LOCK = 'lock'
VERSION_EXPANSION_LATEST = 'latest'
VERSION_EXPANSION_NONE = 'none'
Expand Down
26 changes: 26 additions & 0 deletions core/collections/migrations/0074_expansion_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('collections', '0073_collection_verification'),
]

operations = [
migrations.AddField(
model_name='expansion',
name='health',
field=models.JSONField(blank=True, null=True),
),
migrations.AddField(
model_name='expansion',
name='health_is_current',
field=models.BooleanField(blank=True, null=True),
),
migrations.AddField(
model_name='expansion',
name='health_revision',
field=models.IntegerField(default=0),
),
]
182 changes: 175 additions & 7 deletions core/collections/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import time
from contextlib import nullcontext

Expand All @@ -19,7 +20,8 @@
TRANSFORM_TO_RESOURCE_VERSIONS, COLLECTION_REFERENCE_TYPE, TRANSFORM_TO_EXTENSIONAL,
RESOURCE_VERSION_REPO_RESOLUTION_TOGGLE, UNRESOLVED_RESOURCE_VERSION_TYPE,
INTENSIONAL_REFERENCE_RESOURCE_VERSIONS_TOGGLE, EXPANSION_LOCKED_REPO_VERSION_TOGGLE, VERSION_EXPANSION_LOCK,
VERSION_EXPANSION_LATEST, REFERENCE_FOUND_NOTHING, REFERENCE_VERSION_NOT_HELD, REFERENCE_EXCLUDED_BY_PARAMETERS)
VERSION_EXPANSION_LATEST, REFERENCE_FOUND_NOTHING, REFERENCE_VERSION_NOT_HELD, REFERENCE_EXCLUDED_BY_PARAMETERS,
EXPANSION_HEALTH_TOGGLE, EXPANSION_HEALTH_DEBOUNCE_SECONDS, HEALTH_RELEVANT_VERSION_FIELDS)
from core.collections.parsers import CollectionReferenceParser
from core.collections.translators import CollectionReferenceTranslator
from core.collections.utils import is_concept, is_mapping
Expand All @@ -32,7 +34,7 @@
from core.common.models import ConceptContainerModel, BaseResourceModel
from core.common.search import CustomESSearch
from core.common.tasks import seed_children_to_expansion, batch_index_resources, index_expansion_concepts, \
index_expansion_mappings, readd_references_to_expansion_on_references_removal
index_expansion_mappings, readd_references_to_expansion_on_references_removal, compute_expansion_health
from core.common.utils import drop_version, to_owner_uri, generate_temp_version, es_id_in, \
get_resource_class_from_resource_name, to_snake_case, \
es_to_pks, id_batches, split_list_by_condition, decode_string, is_canonical_uri, encode_string, \
Expand All @@ -47,6 +49,7 @@

TRUTHY = get_truthy_values()
FALSEY = get_falsy_values()
logger = logging.getLogger('oclapi')


class Collection(DirtyFieldsMixin, VersionCompareMixin, ConceptContainerModel):
Expand Down Expand Up @@ -167,13 +170,16 @@ def should_auto_expand(self):
def save(
self, *args, force_insert=False, force_update=False, using=None, update_fields=None
):
is_new = not self.id
dirty_fields = self.get_dirty_fields()

super().save(*args, force_insert=force_insert, force_update=force_update, using=using,
update_fields=update_fields)

if self.id and 'canonical_url' in dirty_fields:
self.resolve_url_registry_entries_later()
if self.id and not is_new and HEALTH_RELEVANT_VERSION_FIELDS & set(dirty_fields):
Expansion.mark_health_not_current_for_repository(self)

def post_create_actions(self):
if self.should_auto_expand:
Expand Down Expand Up @@ -297,7 +303,12 @@ def add_references(self, references, user=None, _async=False):
added_references.append(reference)

if self.expansion_uri:
self.expansion.add_references(added_references)
expansion = self.expansion
expansion.add_references(added_references)
if added_references:
expansion.mark_health_out_of_date()
if added_references:
Expansion.mark_health_not_current_for_value_set(self)
self.__dict__.pop('locked_source_versions', None)
self.__dict__.pop('locked_collection_versions', None)
if user and user.is_authenticated:
Expand Down Expand Up @@ -328,19 +339,27 @@ def delete_references(self, expressions):
if expressions == ALL: # Deprecated: Old way
# one transaction, so no one sees the expansion emptied while the references remain
with CollectionReference.atomic_when_locked():
if self.expansion_uri:
self.expansion.delete_expressions(expressions)
expansion = self.expansion if self.expansion_uri else None
if expansion:
expansion.delete_expressions(expressions)
self.references.all().delete()
if expansion:
expansion.mark_health_out_of_date()
Expansion.mark_health_not_current_for_value_set(self)
return

self.remove_references(self.references.filter(expression__in=expressions))

def remove_references(self, references):
"""Members and references go in one transaction, so no one sees one gone without the other"""
with CollectionReference.atomic_when_locked():
if self.expansion_uri:
self.expansion.delete_references(references)
expansion = self.expansion if self.expansion_uri else None
if expansion:
expansion.delete_references(references)
references.delete()
if expansion:
expansion.mark_health_out_of_date()
Expansion.mark_health_not_current_for_value_set(self)

def get_cascaded_mapping_uris_from_concept_expressions(self, expressions):
mapping_uris = []
Expand Down Expand Up @@ -380,6 +399,11 @@ def cascade_children_to_expansion(self, expansion_data=None, index=True, sync=Fa
elif should_auto_expand and not self.expansion_uri:
self.expansion_uri = expansion.uri
self.save()
# a seed that ended before this made it the default stored no health
if Expansion.objects.filter(id=expansion.id, is_processing=False, health_is_current__isnull=True).exists():
# the seed saved what it found (unresolved repositories among it) on its own copy
expansion.refresh_from_db()
expansion.update_health()
Comment on lines +402 to +406

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The seed saved what it found, unresolved repositories included, on its own copy, so this one is stale.

Suggested change
# a seed that ended before this made it the default stored no health
if Expansion.objects.filter(id=expansion.id, is_processing=False, health_is_current__isnull=True).exists():
expansion.update_health()
# a seed that ended before this made it the default stored no health
if Expansion.objects.filter(id=expansion.id, is_processing=False, health_is_current__isnull=True).exists():
# the seed saved what it found (unresolved repositories among it) on its own copy
expansion.refresh_from_db()
expansion.update_health()


return expansion

Expand Down Expand Up @@ -424,6 +448,7 @@ def seed_from_head_expansion(self, lock=True, index=True):
self.refresh_from_db()
self.__dict__.pop('locked_source_versions', None)
self.__dict__.pop('locked_collection_versions', None)
expansion.update_health()
if index:
expansion.index_resources(
list(expansion.concepts.values_list('id', flat=True)),
Expand Down Expand Up @@ -1617,6 +1642,12 @@ class Meta:
built_with = models.CharField(
max_length=10, null=True, blank=True,
choices=[(choice, choice) for choice in (VERSION_EXPANSION_LOCK, VERSION_EXPANSION_LATEST)])
# the summary core.collections.health computes (null: never computed), and whether it matches the members now
health = models.JSONField(null=True, blank=True)
health_is_current = models.BooleanField(null=True, blank=True)
# bumped by every mark, so a computation that started before a change can't store its result as current
health_revision = models.IntegerField(default=0)
HEALTH_FIELDS = ('health', 'health_is_current', 'health_revision')

@property
def is_head(self):
Expand Down Expand Up @@ -2521,6 +2552,143 @@ def _get_resources_index_collection_fields(self):
'collection_owner_url': [self.owner_url],
}

def save(self, *args, force_insert=False, force_update=False, using=None, update_fields=None):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nit (optional): this override applies whatever the toggle says. Every save of a loaded expansion now goes through update_fields, so saving one whose row has been deleted raises Save with update_fields did not affect any rows instead of silently inserting it again. For example, a long seed task finishes after its collection version was deleted.

Raising is arguably better, since re-inserting a deleted expansion was itself wrong. But it's a behaviour change outside the toggle, so say so in the PR description.

if update_fields is None and not force_insert and not self._state.adding:
# health is written only by its own updates, so a save from an older copy can't undo them
update_fields = [
field.name for field in self._meta.concrete_fields
if not field.primary_key and field.name not in self.HEALTH_FIELDS]
super().save(*args, force_insert=force_insert, force_update=force_update, using=using,
update_fields=update_fields)

@staticmethod
def is_health_on():
return bool(Toggle.get(EXPANSION_HEALTH_TOGGLE))

def update_health(self):
"""Computes and saves the health summary of a version's default expansion, the only one health is kept for
for now. Stored as current only if nothing marked it while it computed. A failure is logged and leaves it
out of date; it never fails the build that called it"""
if not self.is_health_on():
return None
from core.collections.health import ExpansionHealth
expansions = Expansion.objects.filter(id=self.id)
if not self.is_default:
expansions.update(health=None, health_is_current=None)
self.health = None
return None
revision = expansions.values_list('health_revision', flat=True).first()
if revision is None:
return None
try:
health = ExpansionHealth(self).compute()
except Exception: # pylint: disable=broad-except
logger.exception('Expansion health failed for %s', self.uri)
# None, not False: nothing is queued, so the next change queues a recompute
expansions.filter(health_revision=revision).update(health_is_current=None)
return None
# the store bumps it too, so an older run that ends after a newer one (a rebuild's) can't overwrite it
if not expansions.filter(health_revision=revision).update(
health=health, health_is_current=True, health_revision=F('health_revision') + 1):
# marked, or stored by a newer run, while it computed: a fresh run reads the change
self.queue_health_recompute(self.id)
return None
self.health = health
self.health_is_current = True
return health

@staticmethod
def queue_health_recompute(expansion_id):
"""One debounced recompute once the caller's transaction commits. A failure to queue leaves it not current
with nothing queued (None), so the next change queues one"""
if get(settings, 'TEST_MODE', False):
compute_expansion_health(expansion_id)
return

def queue_recompute():
try:
compute_expansion_health.apply_async(
(expansion_id,), queue='default', countdown=EXPANSION_HEALTH_DEBOUNCE_SECONDS)
except Exception: # pylint: disable=broad-except
logger.exception('Could not queue expansion health for %s', expansion_id)
Expansion.objects.filter(id=expansion_id, health_is_current=False).update(health_is_current=None)

transaction.on_commit(queue_recompute)

@classmethod
def mark_health_not_current(cls, expansion_ids):
"""Not current, with nothing queued: the next change, the nightly recheck or the backfill computes them. One
already out of date (False) keeps its queued run. Marked whatever the toggle, as it costs one update"""
if expansion_ids:
cls.objects.filter(id__in=expansion_ids).update(
health_revision=F('health_revision') + 1, health_is_current=models.Case(
models.When(health_is_current=False, then=models.Value(False)), default=models.Value(None),
output_field=models.BooleanField(null=True)))

@classmethod
def mark_health_not_current_for_repository(cls, repo_version):
"""Expansions that recorded a version of repo_version's repository: their classes read its versions, so a
release, retire, visibility change or delete leaves them not current. Returns their ids"""
kind = 'collection' if isinstance(repo_version, Collection) else 'source'
version_ids = repo_version.versions.values('id')
expansion_ids = list(cls.objects.filter(
models.Q(**{f'explicit_{kind}_versions__in': version_ids}) |
models.Q(**{f'evaluated_{kind}_versions__in': version_ids})
).values_list('id', flat=True).distinct())
cls.mark_health_not_current(expansion_ids)
return expansion_ids

@classmethod
def mark_health_not_current_for_value_set(cls, collection_version):
"""Expansions whose value-set references were judged against this collection version's expansion"""
cls.mark_health_not_current(list(cls.objects.filter(
models.Q(explicit_collection_versions=collection_version) |
models.Q(evaluated_collection_versions=collection_version)
).values_list('id', flat=True).distinct()))

@classmethod
def mark_health_not_current_for_members(cls, resource_type, member_ids):
"""Expansions holding these concepts or mappings, before a hard delete takes the membership rows"""
cls.mark_health_not_current(list(cls.objects.filter(
**{f'{resource_type}__id__in': member_ids}).values_list('id', flat=True).distinct()))

@classmethod
def mark_health_out_of_date_for_members(cls, resource_type, member_ids):
"""A member leaving or re-entering HEAD changes its class. The default expansions holding it are marked out
of date, and each that wasn't already is recomputed, as a frozen release would otherwise never be"""
if not member_ids:
return
expansion_ids = list(cls.objects.filter(
collection_version__expansion_uri=F('uri'), **{f'{resource_type}__id__in': member_ids}
).values_list('id', flat=True).distinct())
if not expansion_ids:
return
if not cls.is_health_on():
cls.mark_health_not_current(expansion_ids)
return
expansions = cls.objects.filter(id__in=expansion_ids)
# bumped first, as mark_health_out_of_date does, then the ones not already out of date are flipped and queued
expansions.update(health_revision=F('health_revision') + 1)
queued_ids = list(expansions.exclude(health_is_current=False).values_list('id', flat=True))
cls.objects.filter(id__in=queued_ids).update(health_is_current=False)
for expansion_id in queued_ids:
cls.queue_health_recompute(expansion_id)

def mark_health_out_of_date(self):
"""Marks the health out of date and queues one recompute, however many changes follow before it runs. With the
toggle off it is only marked not current, so the backfill finds it once health is on again"""
expansions = Expansion.objects.filter(id=self.id)
bump = F('health_revision') + 1
if not self.is_health_on():
expansions.update(health_is_current=None, health_revision=bump)
self.health_is_current = None
return
self.health_is_current = False
# bumped on its own first, so a run that read the old revision can't then store its result as current
expansions.update(health_revision=bump)
if expansions.exclude(health_is_current=False).update(health_is_current=False):
self.queue_health_recompute(self.id)

def clear_processing(self, full_save=False):
if self.is_processing:
self.is_processing = False
Expand Down
4 changes: 4 additions & 0 deletions core/collections/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,7 @@ def test_seed_children_task_evaluates_references_while_head_expansion_is_process
self.assertEqual(list(copy.concepts.all()), [latest])
self.assertEqual(list(collection_v1.references.first().concepts.all()), [latest])

@patch('core.collections.models.Expansion.is_health_on', Mock(return_value=False))
@patch('core.collections.models.batch_index_resources')
@patch('core.collections.models.readd_references_to_expansion_on_references_removal')
@patch('core.common.tasks.export_collection')
Expand Down Expand Up @@ -2790,6 +2791,7 @@ def test_manual_expansion_resolves_valuesets_under_its_own_lock(self):
self.assertEqual(list(manual.concepts.all()), expected)
self.assertIsNone(references[0]._valueset_locks) # pylint: disable=protected-access

@patch('core.collections.models.Expansion.is_health_on', Mock(return_value=False))
@override_settings(TEST_MODE=False)
@patch('core.collections.models.batch_index_resources')
@patch('core.collections.models.readd_references_to_expansion_on_references_removal')
Expand Down Expand Up @@ -2828,6 +2830,7 @@ def collection_with_referenced_member():
reference.concepts.add(concept)
return collection, expansion, reference

@patch('core.collections.models.Expansion.is_health_on', Mock(return_value=False))
@override_settings(TEST_MODE=False)
@patch('core.collections.models.batch_index_resources')
@patch('core.collections.models.readd_references_to_expansion_on_references_removal')
Expand All @@ -2849,6 +2852,7 @@ def test_remove_references_with_toggle_off_queues_at_once_before_deleting(self,
self.assertFalse(
Task.objects.filter(name__iendswith='readd_references_to_expansion_on_references_removal').exists())

@patch('core.collections.models.Expansion.is_health_on', Mock(return_value=False))
@override_settings(TEST_MODE=False)
@patch('core.collections.models.batch_index_resources')
@patch('core.collections.models.readd_references_to_expansion_on_references_removal')
Expand Down
Loading
Loading