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

Pull all addons in one DB request #5289

Merged
merged 5 commits into from
Dec 20, 2024
Merged

Pull all addons in one DB request #5289

merged 5 commits into from
Dec 20, 2024

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Dec 18, 2024

Fixes

Fixes #5274

Description

This PR prevents audio list requests with ?peaks=true from making N+1 queries by fetching all AudioAddon models from the DB in a single call.

Testing Instructions

With DEBUG logging enabled, make API calls to the /v1/audio/ endpoint with ?peaks=true. You should see only 2 total calls to the DB, where the second call is for audio addons:

SELECT
  "api_audioaddon"."created_on",
  "api_audioaddon"."updated_on",
  "api_audioaddon"."audio_identifier",
  "api_audioaddon"."waveform_peaks"
FROM "api_audioaddon"
WHERE "api_audioaddon"."audio_identifier" IN (
  '134d47ed3cd448f1b34e2671a428b463'::uuid,
  -- more UUIDs
  '2708c8f651254cbd992373e55359f1a8'::uuid
);

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (ov just catalog/generate-docs for catalog
    PRs) or the media properties generator (ov just catalog/generate-docs media-props
    for the catalog or ov just api/generate-docs for the API) where applicable.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb requested a review from a team as a code owner December 18, 2024 11:43
@dhruvkb dhruvkb requested review from krysal and obulat and removed request for a team December 18, 2024 11:43
@openverse-bot openverse-bot added 🧱 stack: api Related to the Django API 🟧 priority: high Stalls work on the project or its dependents 🛠 goal: fix Bug fix 💻 aspect: code Concerns the software code in the repository labels Dec 18, 2024
@dhruvkb
Copy link
Member Author

dhruvkb commented Dec 18, 2024

Drafting so that I can update the tests.

@dhruvkb dhruvkb marked this pull request as draft December 18, 2024 12:15
@obulat
Copy link
Contributor

obulat commented Dec 18, 2024

@dhruvkb, how do we enable debug mode? I tried setting DJANGO_LOG_LEVEL to "DEBUG", but can't see the queries.

@dhruvkb
Copy link
Member Author

dhruvkb commented Dec 18, 2024

@obulat can you try again with DJANGO_DATABASE_LOGGING configured to true?

DJANGO_DATABASE_LOGGING = config("DJANGO_DATABASE_LOGGING", cast=bool, default=False)

@dhruvkb dhruvkb marked this pull request as ready for review December 18, 2024 17:46
Copy link
Collaborator

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

I'm not able to see the DB queries, even after adding DJANGO_DATABASE_LOGGING=true to the api/.env file 🤔 is there a different way I should be doing that? It's under ov just logs web, right?


@pytest.mark.django_db
@mock.patch("api.models.audio.AudioAddOn.objects.get")
def test_audio_waveform_sent_when_present(get_mock, audio_fixture):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should there be new tests for the modified functionality of get_db_results with the include_addons parameter?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call, I'll add some tests for that asap.

@dhruvkb
Copy link
Member Author

dhruvkb commented Dec 19, 2024

@AetherUnbound can you try this and see if you can see the DB logs?

Screenshot 2024-12-19 at 10 59 12 AM

Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

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

I can see the DB queries in logs when these variables are set this way:

DJANGO_LOG_LEVEL=DEBUG
DJANGO_DATABASE_LOGGING=True

And indeed, there are no more N+1 queries when peaks are requested 😄 I support @AetherUnbound comment tho. It would be nice to have some tests on this functionality.

@dhruvkb
Copy link
Member Author

dhruvkb commented Dec 19, 2024

@AetherUnbound and @krysal, do you think the test cases introduced in 51e1c3d are sufficient to cover that the addons are fetched and that at most 1 extra query is introduced for peaks?

Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

The added tests fail on main 👍

@@ -147,6 +159,20 @@ def _validate_source(self, source):
detail=f"Invalid source '{source}'. Valid sources are: {valid_string}.",
)

def include_addons(self, serializer):
Copy link
Contributor

Choose a reason for hiding this comment

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

Are addons described anywhere in the codebase docs? Do we only have waveforms as addons now, or is there something else, too?
I would like for this comment to include something like "addons, like waveforms for audio". A description of why something is an addon and not in the main model (if it doesn't exist anywhere now) , would be very useful. Not necessarily in this PR, though :)

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of the addon model is specific to the audio media type and that too only for the waveforms. The AudioAddOn model does not have any docstrings, so the conversation around the PR that added this model WordPress/openverse-api#510 is our only reference.

The PR describes that the reason the waveforms are in an AudioAddOn model is because the data in any columns that are not in the upstream DB would be gone after a data refresh.

@dhruvkb dhruvkb merged commit 4a85e14 into main Dec 20, 2024
52 checks passed
@dhruvkb dhruvkb deleted the fix_n_plus_one branch December 20, 2024 09:55
@AetherUnbound
Copy link
Collaborator

I can see the DB queries in logs when these variables are set this way:

DJANGO_LOG_LEVEL=DEBUG
DJANGO_DATABASE_LOGGING=True

And indeed, there are no more N+1 queries when peaks are requested 😄 I support @AetherUnbound comment tho. It would be nice to have some tests on this functionality.

Ahh, I was probably missing the DJANGO_LOG_LEVEL=DEBUG too! My bad!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟧 priority: high Stalls work on the project or its dependents 🧱 stack: api Related to the Django API
Projects
Status: 🤝 Merged
Development

Successfully merging this pull request may close these issues.

Causing N+1 queries when requesting related audio tracks with ?peaks=true
5 participants