Repository navigation
OpenConceptLab/ocl_issues#3003 | expansion health stored and kept current (PR 2 of 4, stacked on #1025) #1026
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
base: issues-3003-expansion-health-classifier
Are you sure you want to change the base?
Changes from all commits
619f921
30dade1
b427f19
a0dac37
adb3dee
f63ff30
3cacf5b
7737488
5b93c92
837bd4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import logging | ||
| import time | ||
| from contextlib import nullcontext | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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, \ | ||
|
|
@@ -47,6 +49,7 @@ | |
|
|
||
| TRUTHY = get_truthy_values() | ||
| FALSEY = get_falsy_values() | ||
| logger = logging.getLogger('oclapi') | ||
|
|
||
|
|
||
| class Collection(DirtyFieldsMixin, VersionCompareMixin, ConceptContainerModel): | ||
|
|
@@ -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: | ||
|
|
@@ -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: | ||
|
|
@@ -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 = [] | ||
|
|
@@ -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() | ||
|
|
||
| return expansion | ||
|
|
||
|
|
@@ -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)), | ||
|
|
@@ -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): | ||
|
|
@@ -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): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 | ||
|
|
||
There was a problem hiding this comment.
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.