Skip to content

Commit

Permalink
fix: resolve environment N+1 caused by feature versioning v2 (#3040)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Nov 27, 2023
1 parent 34c6d07 commit 5392480
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/environments/identities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def get_all_feature_states(
full_query &= additional_filters

select_related_args = [
"environment",
"feature",
"feature_state_value",
"feature_segment",
Expand Down
30 changes: 30 additions & 0 deletions api/environments/identities/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from core.constants import FLAGSMITH_UPDATED_AT_HEADER
from django.test import override_settings
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APIClient, APITestCase

Expand Down Expand Up @@ -849,6 +850,35 @@ def test_get_identities_request_includes_updated_at_header(self):
self.environment.updated_at.timestamp()
)

def test_get_identities_nplus1(self) -> None:
"""
Specific test to reproduce N+1 issue found after deployment of
v2 feature versioning.
TODO: move this (and other tests) to /api/tests/...
"""
url = "%s?identifier=%s" % (
reverse("api-v1:sdk-identities"),
self.identity.identifier,
)

# let's get a baseline with only a single version of a flag
with self.assertNumQueries(6):
self.client.get(url)

# now let's create some new versions of the same flag
# and verify that the query count doesn't increase
v1_flag = FeatureState.objects.get(
environment=self.environment, feature=self.feature_1
)
now = timezone.now()
for i in range(2, 13):
v1_flag.clone(env=self.environment, version=i, live_from=now)

# in fact it is lower because the
with self.assertNumQueries(5):
self.client.get(url)


def test_get_identities_with_hide_sensitive_data_with_feature_name(
environment, feature, identity, api_client
Expand Down

3 comments on commit 5392480

@vercel
Copy link

@vercel vercel bot commented on 5392480 Nov 27, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 5392480 Nov 27, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

docs – ./docs

docs.bullet-train.io
docs.flagsmith.com
docs-flagsmith.vercel.app
docs-git-main-flagsmith.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 5392480 Nov 27, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.