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

Add tracemalloc utility view to API and improve k6 scripts for memory leak debugging #3046

Closed

Conversation

sarayourfriend
Copy link
Collaborator

Fixes

Related to #3028

Description

Two things in this PR help to reproduce and debug the memory leak:

  1. Add a new /_trace/ view to the API to make it easy to see tracemalloc changes over time.
  2. Update the k6 script to use random tags from actual results. This ensures we don't make requests that do not have any results, any tag returned will definitely have at least one result.

With the k6 script I'm able to consistently reproduce the leak locally. The API's memory usage climbs seemingly indefinitely now. To do so:

  1. Run just down -v && just api/init. Deleting the volumes ensures you do not have any dead links cached in redis so that dead link filtering is exercised to the fullest extent.
  2. cd into utilities/load_testing and run just k6-local
  3. In another terminal, run docker stats and observe the memory usage of openverse_web_1

You can run multiple just k6-local scripts in different terminals to increase the load but I haven't noticed a big difference when doing this, probably because it's a single worker locally. Speaking of which, you can increase the number of workers to 4 by removing -w 1 in docker-compose.yml for the web service's command. I've been able to observe jumps as big as 20MiB within 2 seconds when I do this locally.

If you enable the trace view (set ENABLE_TRACE_VIEW=1 in you api/.env), you can watch docker stats for when you see a jump in memory usage and then make a request to /_trace/ on your local API. With multiple workers this isn't reliable, so reduce the workers back to 1 if you increased them before (or tweak the implementation if you have ideas for how to make it work well).

Testing Instructions

See above.

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.
  • [N/A] I ran the DAG documentation generator (if 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.

@sarayourfriend sarayourfriend added 🟥 priority: critical Must be addressed ASAP 🤖 aspect: dx Concerns developers' experience with the codebase 🧰 goal: internal improvement Improvement that benefits maintainers, not users 🧱 stack: api Related to the Django API labels Sep 20, 2023
@sarayourfriend sarayourfriend requested review from a team as code owners September 20, 2023 01:24
@sarayourfriend
Copy link
Collaborator Author

sarayourfriend commented Sep 20, 2023

It does seem to kind of stabilise around 982MiB on the first run, but running the script a second time gets it creeping up again. I've got it up to 1GB now. There are further improvements to the script to make it more similar to production behaviour. It'd be good to add things like searching for heaps of sources the way the Gutenberg integration does, for example.

Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

The trace view seems valuable enough to me to make permanent (take out of the if), move to its own file (instead of the URL config) and keep even after this project (enabled conditionally with the env var).

@@ -9,6 +9,15 @@

import os


if os.getenv("ENABLE_TRACE_VIEW", "0") == "1":
Copy link
Member

Choose a reason for hiding this comment

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

Using decouple.config would allow direct cast to bool.

from decouple import config

TRACING = config('ENABLE_TRACE_VIEW', default=False, cast=bool)

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.

LGTM and I've confirmed the trace route doesn't work when the setting is enabled, but does work as expected when enabled! I'm not quite sure how to read the tracemalloc output, maybe we could implement something like the pretty top example from the docs?

Comment on lines +17 to +18
k6-local:
@API_TOKEN="" just k6 http://localhost:50280/v1/ --net=host
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is excellent, thanks for adding it!

@@ -1,6 +1,6 @@
import { group } from "k6"
import { searchBy } from "./search.js"
import { getProvider, getRandomWord } from "./utils.js"
import { getProvider } from "./utils.js"
Copy link
Contributor

Choose a reason for hiding this comment

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

Lines 40-41:

    provider_image_page_20: createScenario("images", "20", "searchByProvider"),
    provider_image_page_500: createScenario("images", "500", "searchByProvider"),

@sarayourfriend sarayourfriend marked this pull request as draft September 20, 2023 07:24
@sarayourfriend sarayourfriend added 🟩 priority: low Low priority and doesn't need to be rushed and removed 🟥 priority: critical Must be addressed ASAP labels Sep 20, 2023
@sarayourfriend
Copy link
Collaborator Author

Drafting and set priority to low. It'd be nice to get the load testing script working reliably, but this quick job I've done does only a very small amount of what would actually make these scripts more useful today. Because we think we have a good handle on what caused the memory leak (#3047), this isn't as high priority anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 aspect: dx Concerns developers' experience with the codebase 🧰 goal: internal improvement Improvement that benefits maintainers, not users 🟩 priority: low Low priority and doesn't need to be rushed 🧱 stack: api Related to the Django API
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants