-
Notifications
You must be signed in to change notification settings - Fork 549
feat(experimentation): default to control when rollout segment is created #7905
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
gagantrivedi
wants to merge
9
commits into
main
Choose a base branch
from
feat/experiment-rollout-default-control-allocation
base: main
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.
+347
−14
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf19595
feat(experimentation): default to control when rollout segment is cre…
gagantrivedi 7923793
test(experimentation): cover default allocation zeroing under v2 vers…
gagantrivedi d5d7fe5
docs: regenerate events catalogue
gagantrivedi 68d71f9
fix(experimentation): drop unreachable no-options guard in allocation…
gagantrivedi 501a042
test(experimentation): assert rollout update applies alongside surviv…
gagantrivedi a5d3aa6
fix(experimentation): lock experiment row to prevent duplicate rollou…
gagantrivedi f14a64e
fix(experimentation): read rollout state from the locked experiment row
gagantrivedi 166b3a1
docs: regenerate events catalogue
gagantrivedi 6417f9d
fix(experimentation): serialise null default value as empty string
gagantrivedi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| ) | ||
| from experimentation.models import ( | ||
| VALID_STATUS_TRANSITIONS, | ||
| Experiment, | ||
| ExperimentStatus, | ||
| MetricAggregation, | ||
| MetricDirection, | ||
|
|
@@ -57,22 +58,28 @@ | |
| ) | ||
| from features.models import FeatureState | ||
| from features.value_types import BOOLEAN, INTEGER, STRING | ||
| from features.versioning.dataclasses import FlagChangeSet | ||
| from features.versioning.dataclasses import FlagChangeSet, MultivariateValueChangeSet | ||
| from features.versioning.versioning_service import ( | ||
| update_flag, | ||
| update_multivariate_values, | ||
| ) | ||
| from integrations.flagsmith.client import get_openfeature_client | ||
| from segments.models import Condition, Segment, SegmentRule | ||
|
|
||
| _ROLLOUT_VALUE_TYPE = {INTEGER: "integer", STRING: "string", BOOLEAN: "boolean"} | ||
| _ROLLOUT_VALUE_TYPE: dict[str, "FeatureValueType"] = { | ||
| INTEGER: "integer", | ||
| STRING: "string", | ||
| BOOLEAN: "boolean", | ||
| } | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from collections.abc import Sequence | ||
| from datetime import datetime | ||
|
|
||
| from experimentation.models import Experiment, Metric, WarehouseConnection | ||
| from experimentation.models import Metric, WarehouseConnection | ||
| from experimentation.types import ExposureGranularity | ||
| from features.feature_states.models import FeatureValueType | ||
| from features.models import FeatureStateValue | ||
| from organisations.models import Organisation | ||
| from users.models import FFAdminUser | ||
|
|
||
|
|
@@ -627,6 +634,44 @@ def _update_rollout_in_place(experiment: Experiment, change_set: FlagChangeSet) | |
| update_flag(experiment.environment, experiment.feature, change_set) | ||
|
|
||
|
|
||
| def _reset_default_allocations_to_control( | ||
| experiment: Experiment, author: AuthorData | ||
| ) -> None: | ||
| """Zero every variant's allocation on the feature's environment-default | ||
| feature state, leaving control (the unallocated remainder) at 100%. | ||
|
|
||
| Run once, when the rollout segment is first created: identities outside the | ||
| rollout cohort should all receive control while the experiment runs. | ||
| """ | ||
| default_state = FeatureState.objects.get_live_feature_states( | ||
| environment=experiment.environment, | ||
| additional_filters=Q(feature_segment__isnull=True, identity__isnull=True), | ||
| feature_id=experiment.feature_id, | ||
| ).latest("id") | ||
| str_value, value_type = _serialize_feature_state_value( | ||
| default_state.feature_state_value | ||
| ) | ||
| update_flag( | ||
| experiment.environment, | ||
| experiment.feature, | ||
| FlagChangeSet( | ||
| author=author, | ||
| enabled=default_state.enabled, | ||
| feature_state_value=str_value, | ||
| type_=value_type, | ||
| multivariate_values=[ | ||
| MultivariateValueChangeSet( | ||
| multivariate_feature_option_id=option_id, | ||
| percentage_allocation=0, | ||
| ) | ||
| for option_id in experiment.feature.multivariate_options.values_list( | ||
| "id", flat=True | ||
| ) | ||
| ], | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def apply_experiment_rollout(experiment: Experiment, spec: RolloutSpec) -> None: | ||
| if experiment.status == ExperimentStatus.COMPLETED: | ||
| raise ValidationError( | ||
|
|
@@ -635,7 +680,11 @@ def apply_experiment_rollout(experiment: Experiment, spec: RolloutSpec) -> None: | |
| validate_rollout_spec(experiment, spec) | ||
| environment_id = experiment.environment_id | ||
| with transaction.atomic(): | ||
| experiment.refresh_from_db(from_queryset=Experiment.objects.select_for_update()) | ||
| is_first_rollout = experiment.rollout_segment_id is None | ||
| segment = _sync_rollout_segment(experiment, spec.rollout_percentage) | ||
| if is_first_rollout: | ||
| _reset_default_allocations_to_control(experiment, spec.author) | ||
| _update_rollout_in_place( | ||
| experiment, | ||
| FlagChangeSet( | ||
|
|
@@ -655,6 +704,19 @@ def apply_experiment_rollout(experiment: Experiment, spec: RolloutSpec) -> None: | |
| ) | ||
|
|
||
|
|
||
| def _serialize_feature_state_value( | ||
| value: FeatureStateValue, | ||
| ) -> tuple[str, FeatureValueType]: | ||
| """Render a stored feature state value as the (string, API type) pair that | ||
| a `FlagChangeSet` expects.""" | ||
| if value.value is None: | ||
| return "", "string" | ||
| return ( | ||
| str(value.value).lower() if value.type == BOOLEAN else str(value.value), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be return a |
||
| _ROLLOUT_VALUE_TYPE.get(value.type or STRING, "string"), | ||
| ) | ||
|
|
||
|
|
||
| def get_experiment_rollout(experiment: Experiment) -> dict[str, typing.Any] | None: | ||
| segment_id = experiment.rollout_segment_id | ||
| if segment_id is None: | ||
|
|
@@ -671,16 +733,13 @@ def get_experiment_rollout(experiment: Experiment) -> dict[str, typing.Any] | No | |
| condition = Condition.objects.get( | ||
| rule__segment_id=segment_id, operator=PERCENTAGE_SPLIT | ||
| ) | ||
| value = feature_state.feature_state_value | ||
| str_value, value_type = _serialize_feature_state_value( | ||
| feature_state.feature_state_value | ||
| ) | ||
| return { | ||
| "enabled": feature_state.enabled, | ||
| "rollout_percentage": float(condition.value or 0), | ||
| "feature_state_value": { | ||
| "type": _ROLLOUT_VALUE_TYPE.get(value.type or STRING, "string"), | ||
| "value": ( | ||
| str(value.value).lower() if value.type == BOOLEAN else str(value.value) | ||
| ), | ||
| }, | ||
| "feature_state_value": {"type": value_type, "value": str_value}, | ||
| "multivariate_feature_state_values": [ | ||
| { | ||
| "multivariate_feature_option": mv.multivariate_feature_option_id, | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmh, it's a bit edgy so let's NIT it (and already existing).
The
latest("id") would drift if ever there are unordered scheduled changes:==> We would get CR B instead of A
I honestly don't think it would happen in real life so if the fix is too complicated we can leave a comment and not block the PR