Skip to content

Commit

Permalink
Add limit to unassociate [RHELDST-20725]
Browse files Browse the repository at this point in the history
We want to perform garbage collection in batches to reduce the amount of
resources it consumes and avoid out of memory exceptions. Unit
association criteria has a limit field which we could use for
batching requests.
  • Loading branch information
amcmahon-rh committed Oct 17, 2023
1 parent 6f26dd3 commit 2e59ec7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pubtools/pulplib/_impl/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ def _do_associate(self, src_repo_id, dest_repo_id, criteria=None, raw_options=No
self._do_request, method="POST", url=url, json=body
)

def _do_unassociate(self, repo_id, criteria=None):
def _do_unassociate(self, repo_id, criteria=None, limit=None):
url = os.path.join(
self._url, "pulp/api/v2/repositories/%s/actions/unassociate/" % repo_id
)
Expand All @@ -800,6 +800,9 @@ def _do_unassociate(self, repo_id, criteria=None):
else:
body["criteria"]["filters"] = {"unit": pulp_search.filters}

if limit:
body["criteria"]["limit"] = limit

LOG.debug("Submitting %s unassociate: %s", url, body)

return self._task_executor.submit(
Expand Down
8 changes: 6 additions & 2 deletions pubtools/pulplib/_impl/fake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def do_next_upload(checksum, size):

return out

def _do_unassociate(self, repo_id, criteria=None):
def _do_unassociate(self, repo_id, criteria=None, limit=None):
repo_f = self.get_repository(repo_id)
if repo_f.exception():
return repo_f
Expand All @@ -431,7 +431,11 @@ def _do_unassociate(self, repo_id, criteria=None):

for unit_with_key in units_with_key:
unit = unit_with_key["unit"]
if match_object(criteria, unit):
if (
match_object(criteria, unit)
and limit
and len(removed_units) <= limit
):
removed_units.add(unit)
else:
kept_keys.add(unit_with_key["key"])
Expand Down
8 changes: 7 additions & 1 deletion pubtools/pulplib/_impl/model/repository/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,13 @@ def remove_content(self, criteria=None, **kwargs):
Matcher.in_(type_ids), # Criteria.with_field_in is deprecated
)

return f_proxy(self._client._do_unassociate(self.id, criteria=criteria))
return f_proxy(
self._client._do_unassociate(
self.id,
criteria=criteria,
limit=kwargs.get("limit"),
)
)

@classmethod
def from_data(cls, data):
Expand Down

0 comments on commit 2e59ec7

Please sign in to comment.