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

fix: always select fallback if available #422

Merged
merged 2 commits into from May 3, 2024

Conversation

giovanni-guidini
Copy link
Contributor

Because random.choices makes selections with replacement and we were making 3 selections only
there was a chance that an app would vanish from the choices (from not being selected).
This is bad because it hides this app as a possible fallbakc option.

Particularly if the choice is marked currently as "rate limited" we would fail with exception
instead of falling back in the other app. We want the fallback.

These changes go around this "choice with selection" by removing the selected app from the
population after each step. It's maybe slower (in practice the population will be small enough
that this doesn't really matter), but makes sure that we always select all available apps.
And still respectes the weighted selection.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

@giovanni-guidini giovanni-guidini changed the base branch from gio/extend-upload-flow to main April 30, 2024 11:57
@giovanni-guidini giovanni-guidini requested a review from a team April 30, 2024 11:57
@codecov-qa
Copy link

codecov-qa bot commented Apr 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.40%. Comparing base (5c7c892) to head (6065c59).

✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #422   +/-   ##
=======================================
  Coverage   97.40%   97.40%           
=======================================
  Files         398      398           
  Lines       33575    33582    +7     
=======================================
+ Hits        32703    32710    +7     
  Misses        872      872           
Flag Coverage Δ
integration 97.40% <100.00%> (+<0.01%) ⬆️
latest-uploader-overall 97.40% <100.00%> (+<0.01%) ⬆️
unit 97.40% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
NonTestCode 94.72% <100.00%> (+<0.01%) ⬆️
OutsideTasks 97.48% <100.00%> (+<0.01%) ⬆️
Files Coverage Δ
services/bots.py 98.43% <100.00%> (+0.06%) ⬆️
services/tests/test_bots.py 100.00% <100.00%> (ø)

Copy link

codecov-public-qa bot commented Apr 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.40%. Comparing base (5c7c892) to head (6065c59).

✅ All tests successful. No failed tests found ☺️

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #422   +/-   ##
=======================================
  Coverage   97.40%   97.40%           
=======================================
  Files         398      398           
  Lines       33575    33582    +7     
=======================================
+ Hits        32703    32710    +7     
  Misses        872      872           
Flag Coverage Δ
integration 97.40% <100.00%> (+<0.01%) ⬆️
latest-uploader-overall 97.40% <100.00%> (+<0.01%) ⬆️
unit 97.40% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
NonTestCode 94.72% <100.00%> (+<0.01%) ⬆️
OutsideTasks 97.48% <100.00%> (+<0.01%) ⬆️
Files Coverage Δ
services/bots.py 98.43% <100.00%> (+0.06%) ⬆️
services/tests/test_bots.py 100.00% <100.00%> (ø)

Copy link

codecov bot commented Apr 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.42%. Comparing base (5c7c892) to head (6065c59).

Changes have been made to critical files, which contain lines commonly executed in production. Learn more

✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #422   +/-   ##
=======================================
  Coverage   97.42%   97.42%           
=======================================
  Files         429      429           
  Lines       34266    34273    +7     
=======================================
+ Hits        33385    33392    +7     
  Misses        881      881           
Flag Coverage Δ
integration 97.40% <100.00%> (+<0.01%) ⬆️
latest-uploader-overall 97.40% <100.00%> (+<0.01%) ⬆️
unit 97.40% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
NonTestCode 94.74% <100.00%> (+<0.01%) ⬆️
OutsideTasks 97.48% <100.00%> (+<0.01%) ⬆️
Files Coverage Δ
services/bots.py Critical 98.43% <100.00%> (+0.06%) ⬆️
services/tests/test_bots.py 100.00% <100.00%> (ø)
Related Entrypoints
run/app.tasks.upload.Upload
run/app.tasks.status.SetError
run/app.tasks.notify.Notify
run/app.tasks.pulls.Sync
run/app.tasks.compute_comparison.ComputeComparison
run/app.tasks.upload.UploadFinisher
run/app.tasks.upload.UploadProcessor
run/app.tasks.commit_update.CommitUpdate
run/app.tasks.upload.PreProcessUpload
run/app.tasks.sync_repo_languages_gql.SyncLanguagesGQL
run/app.tasks.bundle_analysis.BundleAnalysisNotify
run/app.tasks.test_results.TestResultsFinisherTask
run/app.tasks.sync_repo_languages.SyncLanguages
run/app.tasks.sync_repos.SyncRepos
run/app.tasks.label_analysis.process_request

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.

Just a few comments. I think we should add some documentation to this function as it's doing some functionality that's a bit more complex than just get_apps_from_weighted_selection.

@@ -38,14 +38,17 @@ def _get_installation_weight(installation: GithubAppInstallation) -> int:
return MAX_GITHUB_APP_SELECTION_WEIGHT
seconds_in_hour = 3600
age_hours = (age.seconds // seconds_in_hour) + age.days * 24
return age_hours + 2**age.days
# Prevent clock differences from making the weight negative
return max(1, age_hours + 2**age.days)


def _get_apps_from_weighted_selection(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be useful to add some function documentation around what it's returning because it looks like we're significantly changing the functionality. If I understand the code correctly, we're trying to get an ordered list of apps that's random based on the weighting (so that we don't always use the highest weighted one). The return is no longer just a "random subset of the apps" but an ordered list of apps (with some randomness).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is true... good callout here.

services/bots.py Outdated
# Obviously apps with a higher weight have a higher change of being selected as the main app (1st selection)
# But it's important that others are also selected so we can use them as fallbacks
apps_to_consider = []
if keys:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this if check here (i.e. the while loop condition should fail if the list is empty)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll refactor this

Because `random.choices` makes selections with replacement and we were making 3 selections only
there was a chance that an app would vanish from the choices (from not being selected).
This is bad because it hides this app as a possible fallbakc option.

Particularly if the choice is marked currently as "rate limited" we would fail with exception
instead of falling back in the other app. We want the fallback.

These changes go around this "choice with selection" by removing the selected app from the
population after each step. It's maybe slower (in practice the population will be small enough
that this doesn't really matter), but makes sure that we always select all available apps.
And still respectes the weighted selection.
Add docstrings to `_get_apps_from_weighted_selection` and refactor it
to make the code easier to understand. And reduce code complexity.
@codecov-notifications
Copy link

codecov-notifications bot commented May 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #422   +/-   ##
=======================================
  Coverage   97.40%   97.40%           
=======================================
  Files         398      398           
  Lines       33575    33582    +7     
=======================================
+ Hits        32703    32710    +7     
  Misses        872      872           
Flag Coverage Δ
integration 97.40% <100.00%> (+<0.01%) ⬆️
latest-uploader-overall 97.40% <100.00%> (+<0.01%) ⬆️
unit 97.40% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
NonTestCode 94.72% <100.00%> (+<0.01%) ⬆️
OutsideTasks 97.48% <100.00%> (+<0.01%) ⬆️
Files Coverage Δ
services/bots.py 98.43% <100.00%> (+0.06%) ⬆️
services/tests/test_bots.py 100.00% <100.00%> (ø)

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.

Generally LGTM! I just have a non-blocking question.

or (not repository)
)
),
lambda obj: _can_use_this_app(obj, installation_name, repository),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is much more readable now! Thanks!

if installation_name != GITHUB_APP_INSTALLATION_DEFAULT_NAME:
# Add the default app as the last fallback if the owner is using a different app for the task
default_apps = filter(
lambda obj: obj.name == GITHUB_APP_INSTALLATION_DEFAULT_NAME,
lambda obj: _can_use_this_app(
Copy link
Contributor

Choose a reason for hiding this comment

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

Just double checking that this is OK. _can_use_this_app has extra logic that's not in the original functionality of this code, but those extra checks are required to be able to use the app anyways(?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually noticed when refactoring that the repo verification was missing from this filter. I think it is necessary to have that too, because if the repo is not covered we can't use the app.

Of course, considering the default installation, we expect all repos (that Codecov has access to) to be covered, but it's better for the logic to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

About the is_configured check is a similar story. We generaly expect the default app to be configured (otherwise it wouldn't have that name actually), but if it's not configured we shouldn't use it.

@giovanni-guidini giovanni-guidini added this pull request to the merge queue May 3, 2024
Merged via the queue into main with commit 50cd0a4 May 3, 2024
28 checks passed
@giovanni-guidini giovanni-guidini deleted the gio/fix-app-selection branch May 3, 2024 14:59
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.

None yet

2 participants