-
Notifications
You must be signed in to change notification settings - Fork 138
[PULP-1118] Add better error handling for repover duplicate content #7280
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
pedro-psb
wants to merge
1
commit into
pulp:main
Choose a base branch
from
pedro-psb:fix/7184-report-conflicting-packages
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,2 @@ | ||
| Added better error handling for errors when creating RepositoryVersions. | ||
| Now the duplicate content pks are shown in the logs. |
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from __future__ import annotations | ||
| import http.client | ||
| from gettext import gettext as _ | ||
|
|
||
|
|
||
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
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 |
|---|---|---|
|
|
@@ -6,6 +6,11 @@ | |
| from pulpcore.app.files import validate_file_paths | ||
| from pulpcore.app.models import Content, ContentArtifact | ||
| from pulpcore.app.util import batch_qs | ||
| from pulpcore.exceptions import DuplicateContentInRepositoryError | ||
| from collections import defaultdict | ||
| from django_guid import get_guid | ||
| from typing import NamedTuple | ||
| from uuid import UUID | ||
|
|
||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
@@ -78,35 +83,59 @@ def validate_duplicate_content(version): | |
| Uses repo_key_fields to determine if content is duplicated. | ||
|
|
||
| Raises: | ||
| ValueError: If repo version has duplicate content. | ||
| RepositoryVersionCreateError: If repo version has duplicate content. | ||
| """ | ||
| error_messages = [] | ||
|
|
||
| dup_count = 0 | ||
| correlation_id = get_guid() | ||
| for type_obj in version.repository.CONTENT_TYPES: | ||
| if type_obj.repo_key_fields == (): | ||
| continue | ||
|
|
||
| pulp_type = type_obj.get_pulp_type() | ||
| repo_key_fields = type_obj.repo_key_fields | ||
| new_content_total = type_obj.objects.filter( | ||
| pk__in=version.content.filter(pulp_type=pulp_type) | ||
| ).count() | ||
| unique_new_content_total = ( | ||
| type_obj.objects.filter(pk__in=version.content.filter(pulp_type=pulp_type)) | ||
| .distinct(*repo_key_fields) | ||
| .count() | ||
| ) | ||
|
|
||
| if unique_new_content_total < new_content_total: | ||
| error_messages.append( | ||
| _( | ||
| "More than one {pulp_type} content with the duplicate values for {fields}." | ||
| ).format(pulp_type=pulp_type, fields=", ".join(repo_key_fields)) | ||
| ) | ||
| if error_messages: | ||
| raise ValueError( | ||
| _("Cannot create repository version. {msg}").format(msg=", ".join(error_messages)) | ||
| ) | ||
| unique_keys = type_obj.repo_key_fields | ||
| content_qs = type_obj.objects.filter(pk__in=version.content.filter(pulp_type=pulp_type)) | ||
| dup_count = count_duplicates(content_qs, unique_keys) | ||
| if dup_count > 0: | ||
| # At this point the task already failed, so we'll pay extra queries | ||
| # to collect duplicates and provide more useful logs | ||
| for duplicate in collect_duplicates(content_qs, unique_keys): | ||
| keyset_value = duplicate.keyset_value | ||
| duplicate_pks = duplicate.duplicate_pks | ||
| _logger.info(f"Duplicates found: {pulp_type=}; {keyset_value=}; {duplicate_pks=}") | ||
| if dup_count > 0: | ||
| raise DuplicateContentInRepositoryError(dup_count, correlation_id) | ||
|
|
||
|
|
||
| class DuplicateEntry(NamedTuple): | ||
| keyset_value: tuple[str, ...] | ||
| duplicate_pks: list[UUID] | ||
|
|
||
|
|
||
| def count_duplicates(content_qs, unique_keys: tuple[str]) -> int: | ||
| new_content_total = content_qs.count() | ||
| unique_new_content_total = content_qs.distinct(*unique_keys).count() | ||
| return new_content_total - unique_new_content_total | ||
|
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. same
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. This case is at least more sensible than the other one though, since there's more than one line that's actually doing something |
||
|
|
||
|
|
||
| def collect_duplicates(content_qs, unique_keys: tuple[str]) -> list[DuplicateEntry]: | ||
| last_keyset = None | ||
| last_pk = None | ||
| keyset_to_contents = defaultdict(list) | ||
| content_qs = content_qs.values_list(*unique_keys, "pk") | ||
| for values in content_qs.order_by(*unique_keys).iterator(): | ||
| keyset_value = values[:-1] | ||
| pk = str(values[-1]) | ||
| if keyset_value == last_keyset: | ||
| dup_pk_list = keyset_to_contents[keyset_value] | ||
| # the previous duplicate didn't know it was a duplicate | ||
| if len(dup_pk_list) == 0: | ||
| dup_pk_list.append(last_pk) | ||
| dup_pk_list.append(pk) | ||
| last_keyset = keyset_value | ||
| last_pk = pk | ||
| duplicate_entries = [] | ||
| for keyset_value, pk_list in keyset_to_contents.items(): | ||
| duplicate_entries.append(DuplicateEntry(duplicate_pks=pk_list, keyset_value=keyset_value)) | ||
| return duplicate_entries | ||
|
|
||
|
|
||
| def validate_version_paths(version): | ||
|
|
||
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.
I think the logic is messed up a bit.
If you have two types in the loop and the first one has duplicates the second is fine, this will not raise.