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
1 change: 1 addition & 0 deletions CHANGES/+collection_delete_memory.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduced memory usage when deleting collections with many versions by using targeted QuerySet field selection with `.only()` to avoid loading unnecessary JSON fields.
13 changes: 7 additions & 6 deletions pulp_ansible/app/galaxy/v3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
AnsibleCollectionDeprecated,
AnsibleDistribution,
AnsibleNamespaceMetadata,
AnsibleRepository,
Collection,
CollectionDownloadCount,
CollectionVersion,
Expand Down Expand Up @@ -447,9 +448,9 @@ def destroy(self, request: Request, *args, **kwargs) -> Response:
)

repositories = set()
for version in collection.versions.all():
for repo in version.repositories.all():
repositories.add(repo)
for version in collection.versions.only("pk"):
repositories.update(version.repositories.only("pk"))
repositories = AnsibleRepository.objects.filter(pk__in=repositories)

async_result = dispatch(
delete_collection,
Expand All @@ -464,9 +465,9 @@ def get_collection_dependents(parent):
"""Given a parent collection, return a list of collection versions that depend on it."""
key = f"{parent.namespace}.{parent.name}"
return list(
CollectionVersion.objects.exclude(collection=parent).filter(
dependencies__has_key=key, pulp_domain=get_domain()
)
CollectionVersion.objects.exclude(collection=parent)
.filter(dependencies__has_key=key, pulp_domain=get_domain())
.only("namespace", "name", "version")
)


Expand Down
4 changes: 2 additions & 2 deletions pulp_ansible/app/tasks/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _remove_collection_version_from_repos(collection_versions):
"""Remove CollectionVersions from latest RepositoryVersion of each repo."""
repos_with_collections_to_delete = defaultdict(list)
for collection_version in collection_versions:
for repo in collection_version.repositories.all():
for repo in collection_version.repositories.only("pk"):
repos_with_collections_to_delete[repo].append(collection_version.pk)
for repo, collections in repos_with_collections_to_delete.items():
add_and_remove(repo.pk, add_content_units=[], remove_content_units=collections)
Expand Down Expand Up @@ -72,7 +72,7 @@ def delete_collection(collection_pk):
3. Delete Collection
"""
collection = Collection.objects.get(pk=collection_pk)
versions = collection.versions.all()
versions = collection.versions.only("pk")
_remove_collection_version_from_repos(versions)
version_pks = versions.values_list("pk", flat=True)

Expand Down
Loading