This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: implement flake processing using timeseries models #1140
Merged
Merged
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
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,121 @@ | ||
| import logging | ||
| from datetime import datetime | ||
|
|
||
| from django.db import transaction | ||
| from django.db.models import Q, QuerySet | ||
| from redis.exceptions import LockError | ||
| from shared.django_apps.reports.models import CommitReport, ReportSession | ||
| from shared.django_apps.ta_timeseries.models import Testrun | ||
| from shared.django_apps.test_analytics.models import Flake | ||
|
|
||
| from services.redis import get_redis_connection | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| FAIL_FILTER = Q(outcome="failure") | Q(outcome="flaky_failure") | Q(outcome="error") | ||
|
|
||
| LOCK_NAME = "ta_flake_lock:{}" | ||
| KEY_NAME = "ta_flake_key:{}" | ||
|
|
||
|
|
||
| def get_relevant_uploads(repo_id: int, commit_id: str) -> QuerySet[ReportSession]: | ||
| return ReportSession.objects.filter( | ||
| report__report_type=CommitReport.ReportType.TEST_RESULTS.value, | ||
| report__commit__repository__repoid=repo_id, | ||
| report__commit__commitid=commit_id, | ||
| state__in=["processed"], | ||
| ) | ||
|
|
||
|
|
||
| def fetch_current_flakes(repo_id: int) -> dict[bytes, Flake]: | ||
| return { | ||
| bytes(flake.test_id): flake for flake in Flake.objects.filter(repoid=repo_id) | ||
| } | ||
|
|
||
|
|
||
| def get_testruns( | ||
| upload: ReportSession, curr_flakes: dict[bytes, Flake] | ||
| ) -> QuerySet[Testrun]: | ||
| upload_filter = Q(upload_id=upload.id) | ||
| flaky_pass_filter = Q(outcome="pass") & Q(test_id__in=curr_flakes.keys()) | ||
| return Testrun.objects.filter(upload_filter & (FAIL_FILTER | flaky_pass_filter)) | ||
|
|
||
|
|
||
| def handle_pass(curr_flakes: dict[bytes, Flake], test_id: bytes): | ||
| # possible that we expire it and stop caring about it | ||
| if test_id not in curr_flakes: | ||
| return | ||
|
|
||
| curr_flakes[test_id].recent_passes_count += 1 | ||
| curr_flakes[test_id].count += 1 | ||
| if curr_flakes[test_id].recent_passes_count == 30: | ||
|
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. moving this magic constant to the top level would make sense. and an explanation of "after X passes in a row, the test is not marked as flaky anymore" |
||
| curr_flakes[test_id].end_date = datetime.now() | ||
| curr_flakes[test_id].save() | ||
| del curr_flakes[test_id] | ||
|
|
||
|
|
||
| def handle_failure( | ||
| curr_flakes: dict[bytes, Flake], test_id: bytes, testrun: Testrun, repo_id: int | ||
| ): | ||
| existing_flake = curr_flakes.get(test_id) | ||
| if existing_flake: | ||
| existing_flake.fail_count += 1 | ||
| existing_flake.count += 1 | ||
| existing_flake.recent_passes_count = 0 | ||
| else: | ||
| if testrun.outcome != "flaky_failure": | ||
| testrun.outcome = "flaky_failure" | ||
| new_flake = Flake( | ||
| repoid=repo_id, | ||
| test_id=test_id, | ||
| count=1, | ||
| fail_count=1, | ||
| recent_passes_count=0, | ||
| start_date=datetime.now(), | ||
| ) | ||
| curr_flakes[test_id] = new_flake | ||
|
|
||
|
|
||
| def process_flakes_for_commit(repo_id: int, commit_id: str): | ||
| uploads = get_relevant_uploads(repo_id, commit_id) | ||
|
|
||
| curr_flakes = fetch_current_flakes(repo_id) | ||
|
|
||
| for upload in uploads: | ||
| testruns = get_testruns(upload, curr_flakes) | ||
|
|
||
| for testrun in testruns: | ||
| test_id = bytes(testrun.test_id) | ||
| match testrun.outcome: | ||
| case "pass": | ||
| handle_pass(curr_flakes, test_id) | ||
| case "failure" | "flaky_failure" | "error": | ||
| handle_failure(curr_flakes, test_id, testrun, repo_id) | ||
| case _: | ||
| continue | ||
|
|
||
| Testrun.objects.bulk_update(testruns, ["outcome"]) | ||
|
|
||
| Flake.objects.bulk_create( | ||
| curr_flakes.values(), | ||
| update_conflicts=True, | ||
| unique_fields=["id"], | ||
| update_fields=["end_date", "count", "recent_passes_count", "fail_count"], | ||
| ) | ||
|
|
||
| transaction.commit() | ||
|
|
||
|
|
||
| def process_flakes_for_repo(repo_id: int): | ||
| redis_client = get_redis_connection() | ||
| lock_name = LOCK_NAME.format(repo_id) | ||
| key_name = KEY_NAME.format(repo_id) | ||
| try: | ||
| with redis_client.lock(lock_name, timeout=300, blocking_timeout=3): | ||
| while commit_ids := redis_client.lpop(key_name, 10): | ||
| for commit_id in commit_ids: | ||
| process_flakes_for_commit(repo_id, commit_id.decode()) | ||
| return True | ||
| except LockError: | ||
| log.warning("Failed to acquire lock for repo %s", repo_id) | ||
| return False | ||
22 changes: 22 additions & 0 deletions
22
services/test_analytics/tests/snapshots/ta_process_flakes__testrun_filters__0.json
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,22 @@ | ||
| { | ||
| "test1": { | ||
| "count": 6, | ||
| "fail_count": 2, | ||
| "recent_passes_count": 1 | ||
| }, | ||
| "test3": { | ||
| "count": 1, | ||
| "fail_count": 1, | ||
| "recent_passes_count": 0 | ||
| }, | ||
| "test4": { | ||
| "count": 1, | ||
| "fail_count": 1, | ||
| "recent_passes_count": 0 | ||
| }, | ||
| "test5": { | ||
| "count": 1, | ||
| "fail_count": 1, | ||
| "recent_passes_count": 0 | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.