-
-
Notifications
You must be signed in to change notification settings - Fork 23
Dataset cache purge cronjob #1211
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
ponyisi
wants to merge
9
commits into
develop
Choose a base branch
from
dataset-cache-cleanup
base: develop
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cbf5b54
Dataset cache purge cronjob
ponyisi c7f438f
Merge branch 'develop' into dataset-cache-cleanup
ponyisi 4c7d8ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 946cca3
Bad variable name fix
ponyisi 628488c
Add tests, fix bug
ponyisi 97a7d55
Null check
ponyisi 4d609d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 25d022e
Use timezone-aware datetime for dataset lifecycle ops
Copilot 30ccd81
Use args
ponyisi 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
Some comments aren't visible on the classic Files Changed page.
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
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,24 @@ | ||
| apiVersion: batch/v1 | ||
ponyisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| kind: CronJob | ||
| metadata: | ||
| name: {{ .Release.Name }}-dataset-lifecycle-job | ||
| spec: | ||
| schedule: {{ .Values.datasetLifecycle.schedule | default "0 * * * *" | quote }} | ||
| concurrencyPolicy: "Forbid" | ||
| jobTemplate: | ||
| spec: | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: {{ .Release.Name }}-dataset-lifecycle-job | ||
| spec: | ||
| containers: | ||
| - name: {{ .Release.Name }}-dataset-lifecycle-job | ||
| image: {{ .Values.datasetLifecycle.image }}:{{ .Values.datasetLifecycle.tag }} | ||
| imagePullPolicy: {{ .Values.datasetLifecycle.pullPolicy }} | ||
| env: | ||
| - name: LIFETIME | ||
| value: {{ .Values.datasetLifecycle.cacheLifetime }} | ||
ponyisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| args: | ||
| - --request POST "http://{{ .Release.Name }}-servicex-app:8000/servicex/internal/dataset-lifecycle?age=$(LIFETIME)" | ||
| restartPolicy: OnFailure | ||
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
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
61 changes: 61 additions & 0 deletions
61
servicex_app/servicex_app/resources/internal/dataset_lifecycle_ops.py
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,61 @@ | ||
| # Copyright (c) 2025, IRIS-HEP | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # * Redistributions of source code must retain the above copyright notice, this | ||
| # list of conditions and the following disclaimer. | ||
| # | ||
| # * Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # | ||
| # * Neither the name of the copyright holder nor the names of its | ||
| # contributors may be used to endorse or promote products derived from | ||
| # this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| from flask import request, current_app | ||
|
|
||
| from servicex_app.resources.servicex_resource import ServiceXResource | ||
| from ..datasets.get_all import get_all_datasets | ||
| from ..datasets.delete_dataset import delete_dataset | ||
|
|
||
|
|
||
| class DatasetLifecycleOps(ServiceXResource): | ||
| def post(self): | ||
| """ | ||
| Obsolete cached datasets older than N hours | ||
| """ | ||
| now = datetime.now(timezone.utc) | ||
| try: | ||
| age = float(request.get_json().get("age", 24)) | ||
| except Exception: | ||
ponyisi marked this conversation as resolved.
Show resolved
Hide resolved
ponyisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return {"message": "Invalid age parameter"}, 422 | ||
| delta = timedelta(hours=age) | ||
| datasets = ( | ||
| get_all_datasets() | ||
| ) # by default this will only give non-stale datasets | ||
| todelete = [ | ||
| _.id for _ in datasets if _.last_updated and (now - _.last_updated) > delta | ||
| ] | ||
| current_app.logger.info( | ||
| f"Obsoletion called for datasets older than {delta}. " | ||
| f"Obsoleting {len(todelete)} datasets." | ||
| ) | ||
| for dataset_id in todelete: | ||
| delete_dataset(dataset_id) | ||
ponyisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return {"message": "Success"}, 200 | ||
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
87 changes: 87 additions & 0 deletions
87
servicex_app/servicex_app_test/resources/internal/test_dataset_lifecycle_ops.py
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,87 @@ | ||
| # Copyright (c) 2025, IRIS-HEP | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # * Redistributions of source code must retain the above copyright notice, this | ||
| # list of conditions and the following disclaimer. | ||
| # | ||
| # * Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # | ||
| # * Neither the name of the copyright holder nor the names of its | ||
| # contributors may be used to endorse or promote products derived from | ||
| # this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| from datetime import datetime, timezone | ||
| from unittest.mock import patch | ||
|
|
||
| from pytest import fixture | ||
|
|
||
| from servicex_app.models import Dataset | ||
|
|
||
| from servicex_app_test.resource_test_base import ResourceTestBase | ||
|
|
||
|
|
||
| class TestDatasetLifecycle(ResourceTestBase): | ||
| @fixture | ||
| def fake_dataset_list(self): | ||
| with patch( | ||
| "servicex_app.resources.internal.dataset_lifecycle_ops.get_all_datasets" | ||
| ) as dsfunc: | ||
| dsfunc.return_value = [ | ||
| Dataset( | ||
| last_used=datetime(2022, 1, 1, tzinfo=timezone.utc), | ||
| last_updated=datetime(2022, 1, 1, tzinfo=timezone.utc), | ||
| id=1, | ||
| name="not-orphaned", | ||
| events=100, | ||
| size=1000, | ||
| n_files=1, | ||
| lookup_status="complete", | ||
| did_finder="rucio", | ||
| ), | ||
| Dataset( | ||
| last_used=datetime.now(timezone.utc), | ||
| last_updated=datetime.now(timezone.utc), | ||
| id=2, | ||
| name="orphaned", | ||
| events=100, | ||
| size=1000, | ||
| n_files=1, | ||
| lookup_status="complete", | ||
| did_finder="rucio", | ||
| ), | ||
| ] | ||
| yield dsfunc | ||
|
|
||
| def test_fail_on_bad_param(self, client): | ||
| with client.application.app_context(): | ||
| response = client.post( | ||
| "/servicex/internal/dataset-lifecycle", json={"age": "string"} | ||
| ) | ||
| assert response.status_code == 422 | ||
|
|
||
| def test_deletion(self, fake_dataset_list, client): | ||
| with client.application.app_context(): | ||
| with patch( | ||
| "servicex_app.resources.internal.dataset_lifecycle_ops.delete_dataset" | ||
| ) as deletion_obj: | ||
| response = client.post( | ||
| "/servicex/internal/dataset-lifecycle", json={"age": 24} | ||
| ) | ||
| fake_dataset_list.assert_called_once() | ||
| deletion_obj.assert_called_once() | ||
| assert response.status_code == 200 |
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.
Uh oh!
There was an error while loading. Please reload this page.