Skip to content

Commit 2f67d19

Browse files
committed
rabbit_feature_flags: Lock registry once and enable many feature flags
[Why] Before this change, the controller was looping on all feature flags to enable, then for each: 1. it checked if it was supported 2. it acquired the registry lock 3. it enabled the feature flag 4. it released the registry lock It was done this way to not acquire the log if the feature flag was unsupported in the first place. However, this put more load on the lock mechanism. [How] This commit changes the order. The controller acquires the registry lock once, then loops on feature flags to enable. The support check is now under the registry lock.
1 parent bc1e0ad commit 2f67d19

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

deps/rabbit/src/rabbit_ff_controller.erl

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -823,12 +823,29 @@ refresh_after_app_load_task() ->
823823
Ret :: ok | {error, Reason},
824824
Reason :: term().
825825

826-
enable_many(#{states_per_node := _} = Inventory, [FeatureName | Rest]) ->
826+
enable_many(#{states_per_node := _} = Inventory, FeatureNames) ->
827+
%% We acquire a lock before making any change to the registry. This is not
828+
%% used by the controller (because it is already using a globally
829+
%% registered name to prevent concurrent runs). But this is used in
830+
%% `rabbit_feature_flags:is_enabled()' to block while the state is
831+
%% `state_changing'.
832+
rabbit_ff_registry_factory:acquire_state_change_lock(),
833+
Ret = enable_many_locked(Inventory, FeatureNames),
834+
rabbit_ff_registry_factory:release_state_change_lock(),
835+
Ret.
836+
837+
-spec enable_many_locked(Inventory, FeatureNames) -> Ret when
838+
Inventory :: rabbit_feature_flags:cluster_inventory(),
839+
FeatureNames :: [rabbit_feature_flags:feature_name()],
840+
Ret :: ok | {error, Reason},
841+
Reason :: term().
842+
843+
enable_many_locked(#{states_per_node := _} = Inventory, [FeatureName | Rest]) ->
827844
case enable_if_supported(Inventory, FeatureName) of
828-
{ok, Inventory1} -> enable_many(Inventory1, Rest);
845+
{ok, Inventory1} -> enable_many_locked(Inventory1, Rest);
829846
Error -> Error
830847
end;
831-
enable_many(_Inventory, []) ->
848+
enable_many_locked(_Inventory, []) ->
832849
ok.
833850

834851
-spec enable_if_supported(Inventory, FeatureName) -> Ret when
@@ -845,7 +862,7 @@ enable_if_supported(#{states_per_node := _} = Inventory, FeatureName) ->
845862
"Feature flags: `~ts`: supported; continuing",
846863
[FeatureName],
847864
#{domain => ?RMQLOG_DOMAIN_FEAT_FLAGS}),
848-
lock_registry_and_enable(Inventory, FeatureName);
865+
enable_with_registry_locked(Inventory, FeatureName);
849866
false ->
850867
?LOG_DEBUG(
851868
"Feature flags: `~ts`: unsupported; aborting",
@@ -854,23 +871,6 @@ enable_if_supported(#{states_per_node := _} = Inventory, FeatureName) ->
854871
{error, unsupported}
855872
end.
856873

857-
-spec lock_registry_and_enable(Inventory, FeatureName) -> Ret when
858-
Inventory :: rabbit_feature_flags:cluster_inventory(),
859-
FeatureName :: rabbit_feature_flags:feature_name(),
860-
Ret :: {ok, Inventory} | {error, Reason},
861-
Reason :: term().
862-
863-
lock_registry_and_enable(#{states_per_node := _} = Inventory, FeatureName) ->
864-
%% We acquire a lock before making any change to the registry. This is not
865-
%% used by the controller (because it is already using a globally
866-
%% registered name to prevent concurrent runs). But this is used in
867-
%% `rabbit_feature_flags:is_enabled()' to block while the state is
868-
%% `state_changing'.
869-
rabbit_ff_registry_factory:acquire_state_change_lock(),
870-
Ret = enable_with_registry_locked(Inventory, FeatureName),
871-
rabbit_ff_registry_factory:release_state_change_lock(),
872-
Ret.
873-
874874
-spec enable_with_registry_locked(Inventory, FeatureName) -> Ret when
875875
Inventory :: rabbit_feature_flags:cluster_inventory(),
876876
FeatureName :: rabbit_feature_flags:feature_name(),

0 commit comments

Comments
 (0)