-
Notifications
You must be signed in to change notification settings - Fork 84
Support repairing metadata files #1114
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
Open
jobselko
wants to merge
1
commit into
pulp:main
Choose a base branch
from
jobselko:fix_1099
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added support for recreating and fixing metadata files to `repair_metadata` endpoint. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,11 +8,12 @@ | |||||||||||||||||||||||||||||||||||||||||||
| from django.db.models.query import QuerySet | ||||||||||||||||||||||||||||||||||||||||||||
| from pulp_python.app.models import PythonPackageContent, PythonRepository | ||||||||||||||||||||||||||||||||||||||||||||
| from pulp_python.app.utils import ( | ||||||||||||||||||||||||||||||||||||||||||||
| artifact_to_metadata_artifact, | ||||||||||||||||||||||||||||||||||||||||||||
| artifact_to_python_content_data, | ||||||||||||||||||||||||||||||||||||||||||||
| fetch_json_release_metadata, | ||||||||||||||||||||||||||||||||||||||||||||
| parse_metadata, | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| from pulpcore.plugin.models import ContentArtifact, ProgressReport | ||||||||||||||||||||||||||||||||||||||||||||
| from pulpcore.plugin.models import Artifact, ContentArtifact, Domain, ProgressReport | ||||||||||||||||||||||||||||||||||||||||||||
| from pulpcore.plugin.util import get_domain | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| log = logging.getLogger(__name__) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -41,16 +42,25 @@ def repair(repository_pk: UUID) -> None: | |||||||||||||||||||||||||||||||||||||||||||
| content_set = repository.latest_version().content.values_list("pk", flat=True) | ||||||||||||||||||||||||||||||||||||||||||||
| content = PythonPackageContent.objects.filter(pk__in=content_set) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| num_repaired, pkgs_not_repaired = repair_metadata(content) | ||||||||||||||||||||||||||||||||||||||||||||
| num_repaired, pkgs_not_repaired, num_metadata_repaired, pkgs_metadata_not_repaired = ( | ||||||||||||||||||||||||||||||||||||||||||||
| repair_metadata(content) | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| # Convert set() to 0 | ||||||||||||||||||||||||||||||||||||||||||||
| if not pkgs_not_repaired: | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_not_repaired = 0 | ||||||||||||||||||||||||||||||||||||||||||||
| if not pkgs_metadata_not_repaired: | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_metadata_not_repaired = 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| log.info( | ||||||||||||||||||||||||||||||||||||||||||||
| _( | ||||||||||||||||||||||||||||||||||||||||||||
| "{} packages' metadata repaired. Not repaired packages due to either " | ||||||||||||||||||||||||||||||||||||||||||||
| "inaccessible URL or mismatched sha256: {}." | ||||||||||||||||||||||||||||||||||||||||||||
| ).format(num_repaired, pkgs_not_repaired) | ||||||||||||||||||||||||||||||||||||||||||||
| "inaccessible URL or mismatched sha256: {}. " | ||||||||||||||||||||||||||||||||||||||||||||
| "{} metadata files repaired. Packages whose metadata files could not be repaired: {}." | ||||||||||||||||||||||||||||||||||||||||||||
| ).format(num_repaired, pkgs_not_repaired, num_metadata_repaired, pkgs_metadata_not_repaired) | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def repair_metadata(content: QuerySet[PythonPackageContent]) -> tuple[int, set[str]]: | ||||||||||||||||||||||||||||||||||||||||||||
| def repair_metadata(content: QuerySet[PythonPackageContent]) -> tuple[int, set[str], int, set[str]]: | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| Repairs metadata for a queryset of PythonPackageContent objects | ||||||||||||||||||||||||||||||||||||||||||||
| and updates the progress report. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -59,9 +69,11 @@ def repair_metadata(content: QuerySet[PythonPackageContent]) -> tuple[int, set[s | |||||||||||||||||||||||||||||||||||||||||||
| content (QuerySet[PythonPackageContent]): The queryset of items to repair. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||
| tuple[int, set[str]]: A tuple containing: | ||||||||||||||||||||||||||||||||||||||||||||
| tuple[int, set[str], int, set[str]]: A tuple containing: | ||||||||||||||||||||||||||||||||||||||||||||
| - The number of packages that were repaired. | ||||||||||||||||||||||||||||||||||||||||||||
| - A set of packages' PKs that were not repaired. | ||||||||||||||||||||||||||||||||||||||||||||
| - The number of metadata files that were repaired. | ||||||||||||||||||||||||||||||||||||||||||||
| - A set of packages' PKs without repaired metadata artifacts. | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| immediate_content = ( | ||||||||||||||||||||||||||||||||||||||||||||
| content.filter(contentartifact__artifact__isnull=False) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -87,6 +99,11 @@ def repair_metadata(content: QuerySet[PythonPackageContent]) -> tuple[int, set[s | |||||||||||||||||||||||||||||||||||||||||||
| # Keep track of on-demand packages that were not repaired | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_not_repaired = set() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # Metadata artifacts and content artifacts | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch = [] | ||||||||||||||||||||||||||||||||||||||||||||
| total_metadata_repaired = 0 | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_metadata_not_repaired = set() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| progress_report = ProgressReport( | ||||||||||||||||||||||||||||||||||||||||||||
| message="Repairing packages' metadata", | ||||||||||||||||||||||||||||||||||||||||||||
| code="repair.metadata", | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -102,6 +119,14 @@ def repair_metadata(content: QuerySet[PythonPackageContent]) -> tuple[int, set[s | |||||||||||||||||||||||||||||||||||||||||||
| .artifact | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| new_data = artifact_to_python_content_data(package.filename, main_artifact, domain) | ||||||||||||||||||||||||||||||||||||||||||||
| total_metadata_repaired += update_metadata_artifact_if_needed( | ||||||||||||||||||||||||||||||||||||||||||||
| package, | ||||||||||||||||||||||||||||||||||||||||||||
| new_data.get("metadata_sha256"), | ||||||||||||||||||||||||||||||||||||||||||||
| main_artifact, | ||||||||||||||||||||||||||||||||||||||||||||
| domain, | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch, | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_metadata_not_repaired, | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| total_repaired += update_package_if_needed( | ||||||||||||||||||||||||||||||||||||||||||||
| package, new_data, batch, set_of_update_fields | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -163,7 +188,12 @@ def repair_metadata(content: QuerySet[PythonPackageContent]) -> tuple[int, set[s | |||||||||||||||||||||||||||||||||||||||||||
| total_repaired += len(batch) | ||||||||||||||||||||||||||||||||||||||||||||
| PythonPackageContent.objects.bulk_update(batch, set_of_update_fields) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return total_repaired, pkgs_not_repaired | ||||||||||||||||||||||||||||||||||||||||||||
| if metadata_batch: | ||||||||||||||||||||||||||||||||||||||||||||
| not_repaired = _process_metadata_batch(metadata_batch) | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_metadata_not_repaired.update(not_repaired) | ||||||||||||||||||||||||||||||||||||||||||||
| total_metadata_repaired += len(metadata_batch) - len(not_repaired) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return total_repaired, pkgs_not_repaired, total_metadata_repaired, pkgs_metadata_not_repaired | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def update_package_if_needed( | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -202,3 +232,95 @@ def update_package_if_needed( | |||||||||||||||||||||||||||||||||||||||||||
| set_of_update_fields.clear() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return total_repaired | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def update_metadata_artifact_if_needed( | ||||||||||||||||||||||||||||||||||||||||||||
| package: PythonPackageContent, | ||||||||||||||||||||||||||||||||||||||||||||
| new_metadata_sha256: str | None, | ||||||||||||||||||||||||||||||||||||||||||||
| main_artifact: Artifact, | ||||||||||||||||||||||||||||||||||||||||||||
| domain: Domain, | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch: list[tuple], | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_metadata_not_repaired: set[str], | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> int: | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| Repairs metadata artifacts for wheel packages by creating missing metadata artifacts | ||||||||||||||||||||||||||||||||||||||||||||
| or updating existing ones when the metadata_sha256 differs. Only processes wheel files | ||||||||||||||||||||||||||||||||||||||||||||
| that have a valid new_metadata_sha256. Queues operations for batch processing. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||
| package: Package to check for metadata changes. | ||||||||||||||||||||||||||||||||||||||||||||
| new_metadata_sha256: The correct metadata_sha256 extracted from the main artifact, or None. | ||||||||||||||||||||||||||||||||||||||||||||
| main_artifact: The main package artifact used to generate metadata. | ||||||||||||||||||||||||||||||||||||||||||||
| domain: The domain in which the metadata artifact will be created. | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch: List of tuples for batch processing (updated in-place). | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_metadata_not_repaired: Set of package PKs that failed repair (updated in-place). | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||
| Number of repaired metadata artifacts (only when batch is flushed at BULK_SIZE). | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| total_metadata_repaired = 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if not package.filename.endswith(".whl") or not new_metadata_sha256: | ||||||||||||||||||||||||||||||||||||||||||||
| return total_metadata_repaired | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| original_metadata_sha256 = package.metadata_sha256 | ||||||||||||||||||||||||||||||||||||||||||||
| cas = package.contentartifact_set.filter(relative_path__endswith=".metadata") | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # Create missing | ||||||||||||||||||||||||||||||||||||||||||||
| if not cas: | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch.append(("create", package, main_artifact, None, domain)) | ||||||||||||||||||||||||||||||||||||||||||||
| # Fix existing | ||||||||||||||||||||||||||||||||||||||||||||
| elif new_metadata_sha256 != original_metadata_sha256: | ||||||||||||||||||||||||||||||||||||||||||||
| ca = cas.first() | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_artifact = ca.artifact | ||||||||||||||||||||||||||||||||||||||||||||
| if metadata_artifact is None or (metadata_artifact.sha256 != new_metadata_sha256): | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch.append(("update", package, main_artifact, ca, domain)) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if len(metadata_batch) == BULK_SIZE: | ||||||||||||||||||||||||||||||||||||||||||||
| not_repaired = _process_metadata_batch(metadata_batch) | ||||||||||||||||||||||||||||||||||||||||||||
| pkgs_metadata_not_repaired.update(not_repaired) | ||||||||||||||||||||||||||||||||||||||||||||
| total_metadata_repaired += BULK_SIZE - len(not_repaired) | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch.clear() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return total_metadata_repaired | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def _process_metadata_batch(metadata_batch: list[tuple]) -> set[str]: | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| Processes a batch of metadata repair operations by creating metadata artifacts | ||||||||||||||||||||||||||||||||||||||||||||
| and their corresponding ContentArtifacts. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_batch: List of (action, package, main_artifact, content_artifact, domain) tuples. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||
| Set of package PKs for which metadata artifacts could not be created. | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| not_repaired = set() | ||||||||||||||||||||||||||||||||||||||||||||
| content_artifacts_to_create = [] | ||||||||||||||||||||||||||||||||||||||||||||
| content_artifacts_to_update = [] | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| for action, package, main_artifact, content_artifact, domain in metadata_batch: | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_artifact = artifact_to_metadata_artifact(package.filename, main_artifact) | ||||||||||||||||||||||||||||||||||||||||||||
| if metadata_artifact: | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_artifact.pulp_domain = domain | ||||||||||||||||||||||||||||||||||||||||||||
| metadata_artifact.save() | ||||||||||||||||||||||||||||||||||||||||||||
| if action == "create": | ||||||||||||||||||||||||||||||||||||||||||||
| ca = ContentArtifact( | ||||||||||||||||||||||||||||||||||||||||||||
| artifact=metadata_artifact, | ||||||||||||||||||||||||||||||||||||||||||||
| content=package, | ||||||||||||||||||||||||||||||||||||||||||||
| relative_path=f"{package.filename}.metadata", | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| content_artifacts_to_create.append(ca) | ||||||||||||||||||||||||||||||||||||||||||||
| elif action == "update": | ||||||||||||||||||||||||||||||||||||||||||||
| content_artifact.artifact = metadata_artifact | ||||||||||||||||||||||||||||||||||||||||||||
| content_artifacts_to_update.append(content_artifact) | ||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||
| not_repaired.add(package.pk) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if content_artifacts_to_create: | ||||||||||||||||||||||||||||||||||||||||||||
| ContentArtifact.objects.bulk_create(content_artifacts_to_create) | ||||||||||||||||||||||||||||||||||||||||||||
| if content_artifacts_to_update: | ||||||||||||||||||||||||||||||||||||||||||||
| ContentArtifact.objects.bulk_update(content_artifacts_to_update, ["artifact"]) | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+308
to
+324
Contributor
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. Not a bad strategy, but I think we can be more clever with some DJango ORM magic.
Suggested change
The tuple added to the metadata_batch would just need to be |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return not_repaired | ||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thinking about it some more,
artifact_to_python_content_datatook a domain arg so that I could avoid doing an extra database query when I pulled thepulp_domainoff theartifact. But I think that method is only ever called in tasks and serliazers, aka places where the domain is always set, soget_domainwould be the more appropriate thing to use.So as for
artifact_to_metadata_artifactit currently is using theget_domainwhen it saves the artifact so this save call does nothing. And since all tasks run in one domain we can remove domain from the metadata batch.