-
Notifications
You must be signed in to change notification settings - Fork 10
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
add FlareCleanupTask #947
Open
nora-codecov
wants to merge
2
commits into
main
Choose a base branch
from
nora/1029
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.
+234
−4
Open
add FlareCleanupTask #947
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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,98 @@ | ||
import logging | ||
|
||
from shared.api_archive.archive import ArchiveService | ||
from shared.celery_config import flare_cleanup_task_name | ||
from shared.django_apps.core.models import Pull, PullStates | ||
|
||
from app import celery_app | ||
from tasks.crontasks import CodecovCronTask | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class FlareCleanupTask(CodecovCronTask, name=flare_cleanup_task_name): | ||
""" | ||
Flare is a field on a Pull object. | ||
Flare is used to draw static graphs (see GraphHandler view in api) and can be large. | ||
The majority of flare graphs are used in pr comments, so we keep the (maybe large) flare "available" | ||
in either the db or Archive storage while the pull is OPEN. | ||
If the pull is not OPEN, we dump the flare to save space. | ||
If we need to generate a flare graph for a non-OPEN pull, we build_report_from_commit | ||
and generate fresh flare from that report (see GraphHandler view in api). | ||
""" | ||
|
||
@classmethod | ||
def get_min_seconds_interval_between_executions(cls): | ||
return 72000 # 20h | ||
|
||
def run_cron_task(self, db_session, batch_size=1000, limit=10000, *args, **kwargs): | ||
# for any Pull that is not OPEN, clear the flare field(s), targeting older data | ||
non_open_pulls = Pull.objects.exclude(state=PullStates.OPEN.value).order_by( | ||
"updatestamp" | ||
) | ||
|
||
log.info("Starting FlareCleanupTask") | ||
|
||
# clear in db | ||
non_open_pulls_with_flare_in_db = non_open_pulls.filter( | ||
_flare__isnull=False | ||
).exclude(_flare={}) | ||
|
||
# Process in batches using an offset | ||
total_updated = 0 | ||
offset = 0 | ||
while offset < limit: | ||
batch = non_open_pulls_with_flare_in_db.values_list("id", flat=True)[ | ||
offset : offset + batch_size | ||
] | ||
if not batch: | ||
break | ||
n_updated = non_open_pulls_with_flare_in_db.filter(id__in=batch).update( | ||
_flare=None | ||
) | ||
total_updated += n_updated | ||
offset += batch_size | ||
|
||
log.info(f"FlareCleanupTask cleared {total_updated} database flares") | ||
|
||
# clear in Archive | ||
non_open_pulls_with_flare_in_archive = non_open_pulls.filter( | ||
_flare_storage_path__isnull=False | ||
) | ||
|
||
# Process archive deletions in batches using an offset | ||
total_updated = 0 | ||
offset = 0 | ||
while offset < limit: | ||
batch = non_open_pulls_with_flare_in_archive.values_list("id", flat=True)[ | ||
offset : offset + batch_size | ||
] | ||
if not batch: | ||
break | ||
flare_paths_from_batch = Pull.objects.filter(id__in=batch).values_list( | ||
"_flare_storage_path", flat=True | ||
) | ||
try: | ||
archive_service = ArchiveService(repository=None) | ||
archive_service.delete_files(flare_paths_from_batch) | ||
except Exception as e: | ||
# if something fails with deleting from archive, leave the _flare_storage_path on the pull object. | ||
# only delete _flare_storage_path if the deletion from archive was successful. | ||
log.error(f"FlareCleanupTask failed to delete archive files: {e}") | ||
continue | ||
|
||
# Update the _flare_storage_path field for successfully processed pulls | ||
n_updated = Pull.objects.filter(id__in=batch).update( | ||
_flare_storage_path=None | ||
) | ||
total_updated += n_updated | ||
offset += batch_size | ||
|
||
log.info(f"FlareCleanupTask cleared {total_updated} Archive flares") | ||
|
||
def manual_run(self, db_session=None, limit=1000, *args, **kwargs): | ||
self.run_cron_task(db_session, limit=limit, *args, **kwargs) | ||
|
||
|
||
RegisteredFlareCleanupTask = celery_app.register_task(FlareCleanupTask()) | ||
flare_cleanup_task = celery_app.tasks[RegisteredFlareCleanupTask.name] |
This file contains 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,125 @@ | ||
import json | ||
from unittest.mock import call | ||
|
||
from shared.django_apps.core.models import Pull, PullStates | ||
from shared.django_apps.core.tests.factories import PullFactory, RepositoryFactory | ||
|
||
from tasks.flare_cleanup import FlareCleanupTask | ||
|
||
|
||
class TestFlareCleanupTask(object): | ||
def test_get_min_seconds_interval_between_executions(self): | ||
assert isinstance( | ||
FlareCleanupTask.get_min_seconds_interval_between_executions(), | ||
int, | ||
) | ||
assert FlareCleanupTask.get_min_seconds_interval_between_executions() > 17000 | ||
|
||
def test_successful_run(self, transactional_db, mocker): | ||
mock_logs = mocker.patch("logging.Logger.info") | ||
mock_archive_service = mocker.patch( | ||
"shared.django_apps.utils.model_utils.ArchiveService" | ||
) | ||
archive_value_for_flare = {"some": "data"} | ||
mock_archive_service.return_value.read_file.return_value = json.dumps( | ||
archive_value_for_flare | ||
) | ||
mock_path = "path/to/written/object" | ||
mock_archive_service.return_value.write_json_data_to_storage.return_value = ( | ||
mock_path | ||
) | ||
mock_archive_service_in_task = mocker.patch( | ||
"tasks.flare_cleanup.ArchiveService" | ||
) | ||
mock_archive_service_in_task.return_value.delete_file.return_value = None | ||
|
||
local_value_for_flare = {"test": "test"} | ||
open_pull_with_local_flare = PullFactory( | ||
state=PullStates.OPEN.value, | ||
_flare=local_value_for_flare, | ||
repository=RepositoryFactory(), | ||
) | ||
assert open_pull_with_local_flare.flare == local_value_for_flare | ||
assert open_pull_with_local_flare._flare == local_value_for_flare | ||
assert open_pull_with_local_flare._flare_storage_path is None | ||
|
||
closed_pull_with_local_flare = PullFactory( | ||
state=PullStates.CLOSED.value, | ||
_flare=local_value_for_flare, | ||
repository=RepositoryFactory(), | ||
) | ||
assert closed_pull_with_local_flare.flare == local_value_for_flare | ||
assert closed_pull_with_local_flare._flare == local_value_for_flare | ||
assert closed_pull_with_local_flare._flare_storage_path is None | ||
|
||
open_pull_with_archive_flare = PullFactory( | ||
state=PullStates.OPEN.value, | ||
_flare=None, | ||
_flare_storage_path=mock_path, | ||
repository=RepositoryFactory(), | ||
) | ||
assert open_pull_with_archive_flare.flare == archive_value_for_flare | ||
assert open_pull_with_archive_flare._flare is None | ||
assert open_pull_with_archive_flare._flare_storage_path == mock_path | ||
|
||
merged_pull_with_archive_flare = PullFactory( | ||
state=PullStates.MERGED.value, | ||
_flare=None, | ||
_flare_storage_path=mock_path, | ||
repository=RepositoryFactory(), | ||
) | ||
assert merged_pull_with_archive_flare.flare == archive_value_for_flare | ||
assert merged_pull_with_archive_flare._flare is None | ||
assert merged_pull_with_archive_flare._flare_storage_path == mock_path | ||
|
||
task = FlareCleanupTask() | ||
task.run_cron_task(transactional_db) | ||
|
||
mock_logs.assert_has_calls( | ||
[ | ||
call("Starting FlareCleanupTask"), | ||
call("FlareCleanupTask cleared 1 database flares"), | ||
call("FlareCleanupTask cleared 1 Archive flares"), | ||
] | ||
) | ||
|
||
# there is a cache for flare on the object (all ArchiveFields have this), | ||
# so get a fresh copy of each object without the cached value | ||
open_pull_with_local_flare = Pull.objects.get(id=open_pull_with_local_flare.id) | ||
assert open_pull_with_local_flare.flare == local_value_for_flare | ||
assert open_pull_with_local_flare._flare == local_value_for_flare | ||
assert open_pull_with_local_flare._flare_storage_path is None | ||
|
||
closed_pull_with_local_flare = Pull.objects.get( | ||
id=closed_pull_with_local_flare.id | ||
) | ||
assert closed_pull_with_local_flare.flare == {} | ||
assert closed_pull_with_local_flare._flare is None | ||
assert closed_pull_with_local_flare._flare_storage_path is None | ||
|
||
open_pull_with_archive_flare = Pull.objects.get( | ||
id=open_pull_with_archive_flare.id | ||
) | ||
assert open_pull_with_archive_flare.flare == archive_value_for_flare | ||
assert open_pull_with_archive_flare._flare is None | ||
assert open_pull_with_archive_flare._flare_storage_path == mock_path | ||
|
||
merged_pull_with_archive_flare = Pull.objects.get( | ||
id=merged_pull_with_archive_flare.id | ||
) | ||
assert merged_pull_with_archive_flare.flare == {} | ||
assert merged_pull_with_archive_flare._flare is None | ||
assert merged_pull_with_archive_flare._flare_storage_path is None | ||
|
||
mock_logs.reset_mock() | ||
# check that once these pulls are corrected they are not corrected again | ||
task = FlareCleanupTask() | ||
task.run_cron_task(transactional_db) | ||
|
||
mock_logs.assert_has_calls( | ||
[ | ||
call("Starting FlareCleanupTask"), | ||
call("FlareCleanupTask cleared 0 database flares"), | ||
call("FlareCleanupTask cleared 0 Archive flares"), | ||
] | ||
) |
Oops, something went wrong.
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.
Instead of mocking the
ArchiveService
, you could use themock_storage
fixture (I believe that should be hooked up to the shared archive service).That way, you can actually store files (in memory), and assert that those files are truely being deleted from the storage.