-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(flags): hypercache flag provider #50737
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
Open
matheus-vb
wants to merge
15
commits into
master
Choose a base branch
from
matheus-vb/hypercache-flag-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
db2be90
add ff package with hypercache flag provider
matheus-vb 702909d
configure posthoganalytics.flag_definition_cache_provider with hyperc…
matheus-vb e912c69
address comments
matheus-vb 5b269b7
bump pyproject version
matheus-vb 9a0f447
update lockfile
matheus-vb d496866
Merge branch 'master' into matheus-vb/hypercache-flag-provider
matheus-vb fc9434f
avoid circular import
matheus-vb 62bab3e
tmp test
matheus-vb b89aba0
Merge branch 'master' into matheus-vb/hypercache-flag-provider
matheus-vb 5af0226
rm tmp code
matheus-vb 712f9fe
Merge branch 'master' into matheus-vb/hypercache-flag-provider
matheus-vb 92137e3
fix type issues
matheus-vb c2c6686
rm wrong fix
matheus-vb 2e7970a
try to make mypy happy
matheus-vb 1fcd811
try to make mypy happy again
matheus-vb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Optional | ||
|
|
||
| import structlog | ||
| from posthoganalytics.flag_definition_cache import FlagDefinitionCacheData | ||
|
|
||
| if TYPE_CHECKING: | ||
| from posthog.storage.hypercache import HyperCache | ||
|
|
||
| logger = structlog.get_logger(__name__) | ||
|
|
||
|
|
||
| class HyperCacheFlagProvider: | ||
| """ | ||
| Read-only FlagDefinitionCacheProvider that reads flag definitions from the | ||
| existing HyperCache infrastructure instead of polling the API. | ||
| The HyperCache is kept fresh via Django signals (when flags/cohorts change) | ||
| and periodic refresh tasks. This provider eliminates per-process API polling | ||
| by reading directly from the same Redis cache. | ||
| """ | ||
|
|
||
| def __init__(self, team_id: int): | ||
| self._team_id = team_id | ||
| self._hypercache: Optional[HyperCache] = None | ||
|
|
||
| def _get_hypercache(self): | ||
| """Lazily resolve the hypercache reference. | ||
| The import is deferred because local_evaluation.py triggers a deep | ||
| import chain (cohort.util → hogql → api → ... → cohort.util) that | ||
| causes a circular ImportError when called during AppConfig.ready(). | ||
| By caching the reference after the first successful import, subsequent | ||
| calls skip the import entirely. | ||
| """ | ||
| if self._hypercache is None: | ||
| from posthog.models.feature_flag.local_evaluation import flag_definitions_hypercache | ||
|
|
||
| self._hypercache = flag_definitions_hypercache | ||
| return self._hypercache | ||
|
|
||
| def get_flag_definitions(self) -> Optional[FlagDefinitionCacheData]: | ||
| try: | ||
| data = self._get_hypercache().get_from_cache(self._team_id) | ||
| if data is not None: | ||
| # Defensive: ensure a valid FlagDefinitionCacheData even if | ||
| # the HyperCache shape drifts in the future. | ||
| return { | ||
| "flags": data.get("flags", []), | ||
| "group_type_mapping": data.get("group_type_mapping", {}), | ||
| "cohorts": data.get("cohorts", {}), | ||
| } | ||
| return None | ||
| except ImportError: | ||
| # Expected during Django startup — local_evaluation.py has a | ||
| # circular import chain through cohort.util that resolves once | ||
| # all modules finish loading. The SDK's next poll cycle will retry. | ||
| logger.debug("hypercache_flag_provider_import_pending", team_id=self._team_id) | ||
| return None | ||
| except Exception: | ||
| logger.exception("hypercache_flag_provider_read_error", team_id=self._team_id) | ||
| return None | ||
|
|
||
| def should_fetch_flag_definitions(self) -> bool: | ||
| # Never poll the API — HyperCache handles all writes via Django signals | ||
| # and periodic refresh tasks | ||
| return False | ||
|
|
||
| def on_flag_definitions_received(self, data: FlagDefinitionCacheData) -> None: | ||
| pass # No-op — should_fetch always returns False, so this is never called | ||
|
|
||
| def shutdown(self) -> None: | ||
| pass # No-op — no locks or resources to release | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from django.test import SimpleTestCase | ||
|
|
||
| from parameterized import parameterized | ||
| from posthoganalytics.client import Client | ||
|
|
||
| from posthog.feature_flags.sdk_cache_provider import HyperCacheFlagProvider | ||
|
|
||
|
|
||
| class TestHyperCacheFlagProvider(SimpleTestCase): | ||
| def setUp(self): | ||
| self.provider = HyperCacheFlagProvider(team_id=2) | ||
|
|
||
| def test_should_fetch_flag_definitions_always_returns_false(self): | ||
| assert self.provider.should_fetch_flag_definitions() is False | ||
|
|
||
| def test_on_flag_definitions_received_is_noop(self): | ||
| self.provider.on_flag_definitions_received({"flags": [], "group_type_mapping": {}, "cohorts": {}}) | ||
|
|
||
| def test_shutdown_is_noop(self): | ||
| self.provider.shutdown() | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ( | ||
| "cache_hit", | ||
| { | ||
| "flags": [{"key": "test-flag", "active": True}], | ||
| "group_type_mapping": {"0": "company"}, | ||
| "cohorts": {"1": {"properties": []}}, | ||
| }, | ||
| None, | ||
| { | ||
| "flags": [{"key": "test-flag", "active": True}], | ||
| "group_type_mapping": {"0": "company"}, | ||
| "cohorts": {"1": {"properties": []}}, | ||
| }, | ||
| ), | ||
| ("cache_miss", None, None, None), | ||
| ( | ||
| "missing_keys_defaults", | ||
| {"flags": [{"key": "flag-1"}]}, | ||
| None, | ||
| {"flags": [{"key": "flag-1"}], "group_type_mapping": {}, "cohorts": {}}, | ||
| ), | ||
| ("exception", None, Exception("Redis connection failed"), None), | ||
| ] | ||
| ) | ||
| @patch("posthog.models.feature_flag.local_evaluation.flag_definitions_hypercache") | ||
| def test_get_flag_definitions(self, _name, cache_return, side_effect, expected, mock_hypercache): | ||
| # Reset cached reference so the mock is picked up | ||
| self.provider._hypercache = None | ||
|
|
||
| if side_effect: | ||
| mock_hypercache.get_from_cache.side_effect = side_effect | ||
| else: | ||
| mock_hypercache.get_from_cache.return_value = cache_return | ||
|
|
||
| result = self.provider.get_flag_definitions() | ||
|
|
||
| if expected is None: | ||
| assert result is None | ||
| else: | ||
| assert result == expected | ||
|
|
||
| @patch( | ||
| "posthog.feature_flags.sdk_cache_provider.HyperCacheFlagProvider._get_hypercache", | ||
| side_effect=ImportError("circular import"), | ||
| ) | ||
| def test_get_flag_definitions_returns_none_on_circular_import(self, _mock): | ||
| assert self.provider.get_flag_definitions() is None | ||
|
|
||
| @patch("posthog.models.feature_flag.local_evaluation.flag_definitions_hypercache") | ||
| def test_caches_hypercache_reference(self, mock_hypercache): | ||
| self.provider._hypercache = None | ||
| mock_hypercache.get_from_cache.return_value = None | ||
|
|
||
| self.provider.get_flag_definitions() | ||
| self.provider.get_flag_definitions() | ||
|
|
||
| assert self.provider._hypercache is not None | ||
|
|
||
| def test_implements_protocol(self): | ||
| from posthoganalytics.flag_definition_cache import FlagDefinitionCacheProvider | ||
|
|
||
| assert isinstance(self.provider, FlagDefinitionCacheProvider) | ||
|
|
||
|
|
||
| SAMPLE_FLAGS = { | ||
| "flags": [ | ||
| {"id": 1, "key": "beta-feature", "active": True, "filters": {"groups": [{"rollout_percentage": 100}]}}, | ||
| {"id": 2, "key": "disabled-flag", "active": False, "filters": {}}, | ||
| ], | ||
| "group_type_mapping": {"0": "company", "1": "project"}, | ||
| "cohorts": {"10": {"properties": [{"key": "plan", "value": "enterprise"}]}}, | ||
| } | ||
|
|
||
|
|
||
| class TestSDKClientIntegration(SimpleTestCase): | ||
| """Test HyperCacheFlagProvider with a real posthoganalytics.Client.""" | ||
|
|
||
| def _make_client(self, provider: HyperCacheFlagProvider) -> Client: | ||
| return Client( | ||
| project_api_key="test-key", | ||
| personal_api_key="test-personal-key", | ||
| host="http://localhost:8000", | ||
| flag_definition_cache_provider=provider, | ||
| poll_interval=99999, # prevent background polling | ||
| send=False, | ||
| enable_exception_autocapture=False, | ||
| ) | ||
|
|
||
| def test_sdk_loads_flags_from_provider_instead_of_api(self): | ||
| mock_hypercache = MagicMock() | ||
| mock_hypercache.get_from_cache.return_value = SAMPLE_FLAGS | ||
| provider = HyperCacheFlagProvider(team_id=2) | ||
| provider._hypercache = mock_hypercache | ||
|
|
||
| client = self._make_client(provider) | ||
|
|
||
| with patch.object(client, "_fetch_feature_flags_from_api") as mock_api: | ||
| client._load_feature_flags() | ||
|
|
||
| mock_api.assert_not_called() | ||
|
|
||
| assert len(client.feature_flags) == 2 | ||
| flags_by_key: dict = client.feature_flags_by_key or {} | ||
| assert flags_by_key["beta-feature"]["active"] is True | ||
| assert client.group_type_mapping == {"0": "company", "1": "project"} | ||
| assert client.cohorts == {"10": {"properties": [{"key": "plan", "value": "enterprise"}]}} | ||
|
|
||
| def test_sdk_falls_back_to_api_when_cache_is_empty_and_no_flags_loaded(self): | ||
| mock_hypercache = MagicMock() | ||
| mock_hypercache.get_from_cache.return_value = None | ||
| provider = HyperCacheFlagProvider(team_id=2) | ||
| provider._hypercache = mock_hypercache | ||
|
|
||
| client = self._make_client(provider) | ||
|
|
||
| with patch.object(client, "_fetch_feature_flags_from_api") as mock_api: | ||
| client._load_feature_flags() | ||
|
|
||
| mock_api.assert_called_once() | ||
|
|
||
| def test_sdk_skips_api_when_cache_empty_but_flags_already_loaded(self): | ||
| mock_hypercache = MagicMock() | ||
| provider = HyperCacheFlagProvider(team_id=2) | ||
| provider._hypercache = mock_hypercache | ||
|
|
||
| client = self._make_client(provider) | ||
|
|
||
| # First call: cache has data → loads flags | ||
| mock_hypercache.get_from_cache.return_value = SAMPLE_FLAGS | ||
| client._load_feature_flags() | ||
| assert len(client.feature_flags) == 2 | ||
|
|
||
| # Second call: cache is empty → keeps existing flags, no API call | ||
| mock_hypercache.get_from_cache.return_value = None | ||
|
|
||
| with patch.object(client, "_fetch_feature_flags_from_api") as mock_api: | ||
| client._load_feature_flags() | ||
|
|
||
| mock_api.assert_not_called() | ||
|
|
||
| # Flags from the first load are still there | ||
| assert len(client.feature_flags) == 2 | ||
|
|
||
| def test_sdk_picks_up_flag_changes_on_next_poll(self): | ||
| mock_hypercache = MagicMock() | ||
| provider = HyperCacheFlagProvider(team_id=2) | ||
| provider._hypercache = mock_hypercache | ||
|
|
||
| client = self._make_client(provider) | ||
|
|
||
| # Initial load | ||
| mock_hypercache.get_from_cache.return_value = SAMPLE_FLAGS | ||
| client._load_feature_flags() | ||
| flags_by_key: dict = client.feature_flags_by_key or {} | ||
| assert flags_by_key["beta-feature"]["active"] is True | ||
|
|
||
| # Flag changed in HyperCache (e.g., toggled off via Django admin) | ||
| updated_flags = { | ||
| "flags": [ | ||
| {"id": 1, "key": "beta-feature", "active": False, "filters": {}}, | ||
| ], | ||
| "group_type_mapping": {}, | ||
| "cohorts": {}, | ||
| } | ||
| mock_hypercache.get_from_cache.return_value = updated_flags | ||
| client._load_feature_flags() | ||
|
|
||
| flags_by_key: dict = client.feature_flags_by_key or {} | ||
| assert flags_by_key["beta-feature"]["active"] is False | ||
| assert len(client.feature_flags) == 1 | ||
|
|
||
| def test_sdk_falls_back_to_api_when_provider_raises(self): | ||
| mock_hypercache = MagicMock() | ||
| mock_hypercache.get_from_cache.side_effect = Exception("Redis down") | ||
| provider = HyperCacheFlagProvider(team_id=2) | ||
| provider._hypercache = mock_hypercache | ||
|
|
||
| client = self._make_client(provider) | ||
|
|
||
| with patch.object(client, "_fetch_feature_flags_from_api") as mock_api: | ||
| client._load_feature_flags() | ||
|
|
||
| mock_api.assert_called_once() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.