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: resolve environment N+1 caused by feature versioning v2 #3040

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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