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 update_commit_from_provider_info #855

Merged
merged 3 commits into from
Nov 7, 2024

Conversation

Swatinem
Copy link
Contributor

@Swatinem Swatinem commented Nov 6, 2024

This makes the possibly_ variant of this function sync, to avoid an unnecessary async_to_sync call.

The main function is changed in such a way that:

  • It early-returns when no commit was found, de-indenting the rest of the block
  • Most importantly, it moves some of the field writes around, and adds an explicit flush at the end. That way, it avoids splitting the UPDATE into two different queries, and rather does one update at the end.
  • A query indirection doing a COUNT before the actual query is removed by just doing the filtering on the Python side
  • It might avoid a second provider request for a bitbucket edgecase.

Part of codecov/engineering-team#2824

This makes the `possibly_` variant of this function sync, to avoid an unnecessary `async_to_sync` call.

The main function is changed in such a way that:
- It early-returns when no commit was found, de-indenting the rest of the block
- Most importantly, it moves some of the field writes around, and adds an explicit `flush` at the end. That way, it avoids splitting the `UPDATE` into two different queries, and rather does one update at the end.
- A query indirection doing a `COUNT` before the actual query is removed by just doing the filtering on the Python side
- It might avoid a second provider request for a bitbucket edgecase.
@Swatinem Swatinem requested a review from a team November 6, 2024 14:30
@Swatinem Swatinem self-assigned this Nov 6, 2024
Copy link

codecov bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 98.27586% with 1 line in your changes missing coverage. Please review.

Project coverage is 98.01%. Comparing base (1e2c27a) to head (4bfca43).
Report is 4 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
services/repository.py 98.18% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #855      +/-   ##
==========================================
- Coverage   98.02%   98.01%   -0.02%     
==========================================
  Files         444      445       +1     
  Lines       36057    36102      +45     
==========================================
+ Hits        35346    35386      +40     
- Misses        711      716       +5     
Flag Coverage Δ
integration 98.01% <98.27%> (-0.02%) ⬇️
unit 98.01% <98.27%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 96.00% <98.27%> (+0.02%) ⬆️
OutsideTasks 98.05% <98.18%> (-0.01%) ⬇️
Files with missing lines Coverage Δ
tasks/commit_update.py 100.00% <100.00%> (ø)
tasks/preprocess_upload.py 98.11% <100.00%> (-0.04%) ⬇️
tasks/tests/unit/test_upload_task.py 98.92% <ø> (ø)
tasks/upload.py 96.51% <100.00%> (ø)
services/repository.py 96.21% <98.18%> (-0.35%) ⬇️

... and 21 files with indirect coverage changes

@codecov-notifications
Copy link

codecov-notifications bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 98.27586% with 1 line in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
services/repository.py 98.18% 1 Missing ⚠️

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #855      +/-   ##
==========================================
- Coverage   98.02%   98.01%   -0.02%     
==========================================
  Files         444      445       +1     
  Lines       36057    36102      +45     
==========================================
+ Hits        35346    35386      +40     
- Misses        711      716       +5     
Flag Coverage Δ
integration 98.01% <98.27%> (-0.02%) ⬇️
unit 98.01% <98.27%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 96.00% <98.27%> (+0.02%) ⬆️
OutsideTasks 98.05% <98.18%> (-0.01%) ⬇️
Files with missing lines Coverage Δ
tasks/commit_update.py 100.00% <100.00%> (ø)
tasks/preprocess_upload.py 98.11% <100.00%> (-0.04%) ⬇️
tasks/tests/unit/test_upload_task.py 98.92% <ø> (ø)
tasks/upload.py 96.51% <100.00%> (ø)
services/repository.py 96.21% <98.18%> (-0.35%) ⬇️

... and 21 files with indirect coverage changes

@codecov-qa
Copy link

codecov-qa bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 98.27586% with 1 line in your changes missing coverage. Please review.

Project coverage is 98.01%. Comparing base (1e2c27a) to head (4bfca43).
Report is 4 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
services/repository.py 98.18% 1 Missing ⚠️

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #855      +/-   ##
==========================================
- Coverage   98.02%   98.01%   -0.02%     
==========================================
  Files         444      445       +1     
  Lines       36057    36102      +45     
==========================================
+ Hits        35346    35386      +40     
- Misses        711      716       +5     
Flag Coverage Δ
integration 98.01% <98.27%> (-0.02%) ⬇️
unit 98.01% <98.27%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 96.00% <98.27%> (+0.02%) ⬆️
OutsideTasks 98.05% <98.18%> (-0.01%) ⬇️
Files with missing lines Coverage Δ
tasks/commit_update.py 100.00% <100.00%> (ø)
tasks/preprocess_upload.py 98.11% <100.00%> (-0.04%) ⬇️
tasks/tests/unit/test_upload_task.py 98.92% <ø> (ø)
tasks/upload.py 96.51% <100.00%> (ø)
services/repository.py 96.21% <98.18%> (-0.35%) ⬇️

... and 21 files with indirect coverage changes

Copy link

codecov-public-qa bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 98.27586% with 1 line in your changes missing coverage. Please review.

Project coverage is 98.01%. Comparing base (1e2c27a) to head (4bfca43).
Report is 2 commits behind head on main.

✅ All tests successful. No failed tests found.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #855      +/-   ##
==========================================
- Coverage   98.02%   98.01%   -0.02%     
==========================================
  Files         444      445       +1     
  Lines       36057    36102      +45     
==========================================
+ Hits        35346    35386      +40     
- Misses        711      716       +5     
Flag Coverage Δ
integration 98.01% <98.27%> (-0.02%) ⬇️
unit 98.01% <98.27%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 96.00% <98.27%> (+0.02%) ⬆️
OutsideTasks 98.05% <98.18%> (-0.01%) ⬇️
Files Coverage Δ
tasks/commit_update.py 100.00% <100.00%> (ø)
tasks/preprocess_upload.py 98.11% <100.00%> (-0.04%) ⬇️
tasks/tests/unit/test_upload_task.py 98.92% <ø> (ø)
tasks/upload.py 96.51% <100.00%> (ø)
services/repository.py 96.21% <98.18%> (-0.35%) ⬇️

... and 21 files with indirect coverage changes

Copy link
Contributor

@michelletran-codecov michelletran-codecov left a comment

Choose a reason for hiding this comment

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

LGTM

pull_details = await repository_service.get_pull_request(pullid)
commit.branch = pull_details["base"]["branch"]

db_session.flush()
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for my owner understanding, I'm curious to how this flush helps with reducing the number of updates. I had assumed that we had to either flush (or commit, which does a flush first) for the update to "happen" in the transaction, so I might have the wrong mental model on how it works!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The number of updates are reduced by moving code around.
Previously, some fields on the current Commit were written, and those were apparently implicitly flushed by sqalchemy as another function was querying Commits.
Now the queries happen first, and all the field updates second, whereby the UPDATE should update all the fields at once.

The flush is just there to force the UPDATE to happen inside of this function, as previously, it would be delayed till later on nested in a completely different call chain.

Though to be quite honest, I haven’t verified this behavior locally. We will see this more clearly in tracing.

@Swatinem Swatinem added this pull request to the merge queue Nov 7, 2024
Merged via the queue into main with commit d369849 Nov 7, 2024
24 of 27 checks passed
@Swatinem Swatinem deleted the swatinem/async-update-commit branch November 7, 2024 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants