Skip to content
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

Optimize Report merging #867

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions services/processing/merging.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def merge_reports(
commit_yaml: UserYaml,
master_report: Report,
intermediate_reports: list[IntermediateReport],
) -> MergeResult:
) -> tuple[Report, MergeResult]:
session_mapping: dict[int, int] = dict()
deleted_sessions: set[int] = set()

Expand All @@ -34,9 +34,14 @@ def merge_reports(

old_sessionid = next(iter(report.sessions))
new_sessionid = master_report.next_session_number()
change_sessionid(report, old_sessionid, new_sessionid)
session_mapping[intermediate_report.upload_id] = new_sessionid

if master_report.is_empty() and old_sessionid == new_sessionid:
# if the master report is empty, we can avoid a costly merge operation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens the very first time there's an upload for a commit right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes exactly, except when using carry-forwards

master_report = report
continue

change_sessionid(report, old_sessionid, new_sessionid)
session = report.sessions[new_sessionid]

_session_id, session = master_report.add_session(
Expand All @@ -51,7 +56,7 @@ def merge_reports(

master_report.merge(report)

return MergeResult(session_mapping, deleted_sessions)
return master_report, MergeResult(session_mapping, deleted_sessions)


@sentry_sdk.trace
Expand Down Expand Up @@ -158,6 +163,7 @@ def make_upload_totals(
)


@sentry_sdk.trace
def change_sessionid(report: EditableReport, old_id: int, new_id: int):
"""
Modifies the `EditableReport`, changing the session with `old_id` to have `new_id` instead.
Expand Down
12 changes: 11 additions & 1 deletion services/report/raw_upload_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ def process_raw_upload(
r.filename = current_filename
raise

if report_from_file:
if not report_from_file:
continue
if report.is_empty():
# if the initial report is empty, we can avoid a costly merge operation
report = report_from_file
else:
# merging the smaller report into the larger one is faster,
# so swap the two reports in that case.
if len(report_from_file._files) > len(report._files):
report_from_file, report = report, report_from_file

report.merge(report_from_file, joined=True)

if not report:
Expand Down
5 changes: 2 additions & 3 deletions tasks/compute_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ def run_impl(

# At this point we can calculate the patch coverage
# Because we have a HEAD report and a base commit to get the diff from
if comparison.patch_totals is None:
patch_totals = comparison_proxy.get_patch_totals()
comparison.patch_totals = minimal_totals(patch_totals)
patch_totals = comparison_proxy.get_patch_totals()
comparison.patch_totals = minimal_totals(patch_totals)

if not comparison_proxy.has_project_coverage_base_report():
comparison.error = CompareCommitError.missing_base_report.value
Expand Down
13 changes: 13 additions & 0 deletions tasks/tests/integration/test_upload_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ def setup_mocks(
"tasks.upload.fetch_commit_yaml_and_possibly_store",
return_value=UserYaml(user_yaml or {}),
)
mocker.patch(
"tasks.compute_comparison.get_current_yaml",
return_value=UserYaml(user_yaml or {}),
)
# disable all the tasks being emitted from `UploadFinisher`.
# ideally, we would really want to test their outcomes as well.
mocker.patch("tasks.notify.NotifyTask.run_impl")
mocker.patch("tasks.sync_pull.PullSyncTask.run_impl")
mocker.patch("tasks.save_commit_measurements.SaveCommitMeasurementsTask.run_impl")

# force `report_json` to be written out to storage
Expand Down Expand Up @@ -238,6 +243,14 @@ def test_full_upload(
f"UPDATE reports_upload SET id={upload_id} WHERE id={first_upload.id}"
)

with run_tasks():
upload_task.apply_async(
kwargs={
"repoid": repoid,
"commitid": commitid,
}
)

do_upload(
b"""
a.rs
Expand Down
4 changes: 3 additions & 1 deletion tasks/upload_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@ def perform_report_merging(
archive_service, commit.commitid, upload_ids, intermediate_reports_in_redis
)

merge_result = merge_reports(commit_yaml, master_report, intermediate_reports)
master_report, merge_result = merge_reports(
commit_yaml, master_report, intermediate_reports
)

# Update the `Upload` in the database with the final session_id
# (aka `order_number`) and other statuses
Expand Down
Loading