Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 5676738

Browse files
fix(ta): handle random task retry better (#917)
1 parent 1d68d39 commit 5676738

1 file changed

Lines changed: 80 additions & 87 deletions

File tree

tasks/test_results_processor.py

Lines changed: 80 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import zlib
55
from dataclasses import dataclass
66
from datetime import date, datetime
7-
from typing import List
87

98
import sentry_sdk
109
from shared.celery_config import test_results_processor_task_name
@@ -79,36 +78,38 @@ def run_impl(
7978
repoid = int(repoid)
8079

8180
results = []
82-
upload_list = []
8381

8482
repo_flakes = (
8583
db_session.query(Flake.testid)
8684
.filter(Flake.repoid == repoid, Flake.end_date.is_(None))
8785
.all()
8886
)
8987
flaky_test_set = {flake.testid for flake in repo_flakes}
88+
repository = (
89+
db_session.query(Repository)
90+
.filter(Repository.repoid == int(repoid))
91+
.first()
92+
)
93+
94+
should_delete_archive = self.should_delete_archive(commit_yaml)
95+
archive_service = ArchiveService(repository)
9096

9197
# process each report session's test information
9298
for arguments in arguments_list:
9399
upload = (
94100
db_session.query(Upload).filter_by(id_=arguments["upload_id"]).first()
95101
)
96102
result = self.process_individual_upload(
97-
db_session, repoid, commitid, upload, flaky_test_set
103+
db_session,
104+
archive_service,
105+
repository,
106+
commitid,
107+
upload,
108+
flaky_test_set,
109+
should_delete_archive,
98110
)
99111

100112
results.append(result)
101-
upload_list.append(upload)
102-
103-
if self.should_delete_archive(commit_yaml):
104-
repository = (
105-
db_session.query(Repository)
106-
.filter(Repository.repoid == int(repoid))
107-
.first()
108-
)
109-
self.delete_archive(
110-
commitid, repository, commit_yaml, uploads_to_delete=upload_list
111-
)
112113

113114
return results
114115

@@ -329,60 +330,21 @@ def create_daily_total():
329330
log.info("Inserted test instances to database", extra=dict(upload_id=upload_id))
330331

331332
def process_individual_upload(
332-
self, db_session, repoid, commitid, upload_obj: Upload, flaky_test_set: set[str]
333+
self,
334+
db_session,
335+
archive_service: ArchiveService,
336+
repository: Repository,
337+
commitid,
338+
upload: Upload,
339+
flaky_test_set: set[str],
340+
should_delete_archive: bool,
333341
):
334-
upload_id = upload_obj.id
335-
log.info("Processing individual upload", extra=dict(upload_id=upload_id))
336-
parsing_results = self.process_individual_arg(
337-
db_session, upload_obj, upload_obj.report.commit.repository
338-
)
339-
340-
if all(len(result.testruns) == 0 for result in parsing_results):
341-
log.error(
342-
"No test result files were successfully parsed for this upload",
343-
extra=dict(upload_id=upload_id),
344-
)
345-
return {"successful": False}
346-
347-
self._bulk_write_tests_to_db(
348-
db_session,
349-
repoid,
350-
commitid,
351-
upload_id,
352-
upload_obj.report.commit.branch,
353-
parsing_results,
354-
flaky_test_set,
355-
upload_obj.flag_names,
356-
)
357-
log.info(
358-
"Finished processing individual upload", extra=dict(upload_id=upload_id)
359-
)
360-
361-
return {"successful": True}
362-
363-
def rewrite_readable(
364-
self, network: list[str] | None, report_contents: list[ReadableFile]
365-
) -> bytes:
366-
buffer = b""
367-
if network is not None:
368-
for file in network:
369-
buffer += f"{file}\n".encode("utf-8")
370-
buffer += b"<<<<<< network\n\n"
371-
for report_content in report_contents:
372-
buffer += f"# path={report_content.path}\n".encode("utf-8")
373-
buffer += report_content.contents
374-
buffer += b"\n<<<<<< EOF\n\n"
375-
return buffer
342+
upload_id = upload.id
376343

377-
@sentry_sdk.trace
378-
def process_individual_arg(
379-
self, db_session: Session, upload: Upload, repository: Repository
380-
) -> list[ParsingInfo]:
344+
log.info("Processing individual upload", extra=dict(upload_id=upload_id))
381345
if upload.state == "processed" or upload.state == "has_failed":
382346
return []
383347

384-
archive_service = ArchiveService(repository)
385-
386348
payload_bytes = archive_service.read_file(upload.storage_path)
387349
try:
388350
data = json.loads(payload_bytes)
@@ -423,16 +385,53 @@ def process_individual_arg(
423385
if upload.state != "has_failed":
424386
upload.state = "processed"
425387

426-
db_session.flush()
388+
if all(len(result.testruns) == 0 for result in parsing_results):
389+
successful = False
390+
log.error(
391+
"No test result files were successfully parsed for this upload",
392+
extra=dict(upload_id=upload_id),
393+
)
394+
else:
395+
successful = True
396+
397+
self._bulk_write_tests_to_db(
398+
db_session,
399+
repository.repoid,
400+
commitid,
401+
upload_id,
402+
upload.report.commit.branch,
403+
parsing_results,
404+
flaky_test_set,
405+
upload.flag_names,
406+
)
407+
427408
db_session.commit()
428-
log.info("Marked upload as processed", extra=dict(upload_id=upload.id))
429409

430-
# FIXME: we are unconditionally rewriting as readable, even if we delete it later
431-
readable_report = self.rewrite_readable(network, report_contents)
432-
archive_service.write_file(upload.storage_path, readable_report)
433-
log.info("Wrote readable report to archive", extra=dict(upload_id=upload.id))
410+
log.info(
411+
"Finished processing individual upload", extra=dict(upload_id=upload_id)
412+
)
434413

435-
return parsing_results
414+
if should_delete_archive:
415+
self.delete_archive(archive_service, upload)
416+
else:
417+
readable_report = self.rewrite_readable(network, report_contents)
418+
archive_service.write_file(upload.storage_path, readable_report)
419+
420+
return {"successful": successful}
421+
422+
def rewrite_readable(
423+
self, network: list[str] | None, report_contents: list[ReadableFile]
424+
) -> bytes:
425+
buffer = b""
426+
if network is not None:
427+
for file in network:
428+
buffer += f"{file}\n".encode("utf-8")
429+
buffer += b"<<<<<< network\n\n"
430+
for report_content in report_contents:
431+
buffer += f"# path={report_content.path}\n".encode("utf-8")
432+
buffer += report_content.contents
433+
buffer += b"\n<<<<<< EOF\n\n"
434+
return buffer
436435

437436
def should_delete_archive(self, commit_yaml):
438437
if get_config("services", "minio", "expire_raw_after_n_days"):
@@ -441,23 +440,17 @@ def should_delete_archive(self, commit_yaml):
441440
commit_yaml, ("codecov", "archive", "uploads"), _else=True
442441
)
443442

444-
def delete_archive(
445-
self, commitid, repository, commit_yaml, uploads_to_delete: List[Upload]
446-
):
447-
archive_service = ArchiveService(repository)
448-
for upload in uploads_to_delete:
449-
archive_url = upload.storage_path
450-
if archive_url and not archive_url.startswith("http"):
451-
log.info(
452-
"Deleting uploaded file as requested",
453-
extra=dict(
454-
archive_url=archive_url,
455-
commit=commitid,
456-
upload=upload.external_id,
457-
parent_task=self.request.parent_id,
458-
),
459-
)
460-
archive_service.delete_file(archive_url)
443+
def delete_archive(self, archive_service: ArchiveService, upload: Upload):
444+
archive_url = upload.storage_path
445+
if archive_url and not archive_url.startswith("http"):
446+
log.info(
447+
"Deleting uploaded file as requested",
448+
extra=dict(
449+
archive_url=archive_url,
450+
upload=upload.external_id,
451+
),
452+
)
453+
archive_service.delete_file(archive_url)
461454

462455

463456
RegisteredTestResultsProcessorTask = celery_app.register_task(

0 commit comments

Comments
 (0)