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

Improve N+1 detection for simple hydration methods #45848

Merged
merged 2 commits into from
Jul 24, 2024

Conversation

johnswanson
Copy link
Contributor

We have N+1 detection for simple hydration methods that checks to see whether the hydration method made any DB queries at all. This makes sense, because a simple hydration method is called on each instance individually - if we make database calls each time, we're in trouble.

Where this runs into issues is with cached data, where the simple hydration method is populating the cache - e.g. :can_read or :can_write dereferencing api/*current-user-permissions-set*. Currently we specifically exclude those hydration methods from the check.

But after running into this again for a different simple hydration method, I think there's a more robust approach:

  • first, run the hydration method without counting DB queries

  • then, run it again and make sure we don't make any additional DB queries.

There is a potential downside here: if someone wrote a simple hydration method with side effects, they'd potentially be very confused (in development - this doesn't run in prod!) when those side effects happened twice for a single hydration. But I think that's fairly unlikely.

We have N+1 detection for simple hydration methods that checks to see
whether the hydration method made any DB queries at all. This makes
sense, because a simple hydration method is called on each instance
individually - if we make database calls *each time*, we're in trouble.

Where this runs into issues is with cached data, where the simple
hydration method is populating the cache - e.g. `:can_read` or
`:can_write` dereferencing `api/*current-user-permissions-set*`.
Currently we specifically exclude those hydration methods from the
check.

But after running into this again for a different simple hydration
method, I think there's a more robust approach:

- first, run the hydration method without counting DB queries

- then, run it again and make sure we don't make any additional DB
queries.

There is a potential downside here: if someone wrote a simple hydration
method with side effects, they'd potentially be very confused (in
development - this doesn't run in prod!) when those side effects
happened twice for a single hydration. But I *think* that's fairly
unlikely.
@metabase-bot metabase-bot bot added the .Team/AdminWebapp Admin and Webapp team label Jul 19, 2024
@johnswanson johnswanson added the no-backport Do not backport this PR to any branch label Jul 19, 2024
johnswanson added a commit that referenced this pull request Jul 19, 2024
I forgot to commit this before.

Tests seem to be failing due to [this](#45848)
johnswanson added a commit that referenced this pull request Jul 19, 2024
I forgot to commit this before.

Tests seem to be failing due to [this](#45848)
johnswanson added a commit that referenced this pull request Jul 19, 2024
I forgot to commit this before.

Tests seem to be failing due to [this](#45848)
@johnswanson johnswanson requested a review from a team July 19, 2024 17:34
Copy link
Contributor

@escherize escherize left a comment

Choose a reason for hiding this comment

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

Looks good. Is this getting tested by some existing test? Might be worth adding one, though I can see where it would be tricky.

[toucan2.tools.hydrate :as t2.hydrate]))

(methodical/defmethod t2.hydrate/simple-hydrate
[:default ::some-silly-key-that-should-never-actually-be-hydrated!!!]
Copy link
Contributor

Choose a reason for hiding this comment

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

😂

@johnswanson johnswanson merged commit 50d0c35 into master Jul 24, 2024
117 checks passed
@johnswanson johnswanson deleted the jds/improved-n+1-detection-for-simple-hydration branch July 24, 2024 13:33
johnswanson added a commit that referenced this pull request Jul 25, 2024
I forgot to commit this before.

Tests seem to be failing due to [this](#45848)
johnswanson added a commit that referenced this pull request Jul 26, 2024
I forgot to commit this before.

Tests seem to be failing due to [this](#45848)
johnswanson added a commit that referenced this pull request Jul 29, 2024
I forgot to commit this before.

Tests seem to be failing due to [this](#45848)
johnswanson added a commit that referenced this pull request Jul 29, 2024
I forgot to commit this before.

Tests seem to be failing due to [this](#45848)
@github-actions github-actions bot added this to the 0.51 milestone Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-backport Do not backport this PR to any branch .Team/AdminWebapp Admin and Webapp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants