Skip to content

Commit fbe83e4

Browse files
Merge branch 'main' into rabbitmq-server-7262
2 parents b0d2f94 + 2463933 commit fbe83e4

File tree

5 files changed

+149
-4
lines changed

5 files changed

+149
-4
lines changed

deps/rabbit/priv/schema/rabbit.schema

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,22 @@ end}.
743743
{datatype, integer}
744744
]}.
745745

746+
{mapping, "default_policies.operator.$id.classic_queues.ha_mode", "rabbit.default_policies.operator", [
747+
{datatype, string}
748+
]}.
749+
750+
{mapping, "default_policies.operator.$id.classic_queues.ha_params", "rabbit.default_policies.operator", [
751+
{datatype, [integer, {list, string}]}
752+
]}.
753+
746754
{translation, "rabbit.default_policies.operator", fun(Conf) ->
747-
Props = rabbit_cuttlefish:aggregate_props(Conf, ["default_policies", "operator"]),
755+
Props = rabbit_cuttlefish:aggregate_props(
756+
Conf,
757+
["default_policies", "operator"],
758+
fun({["default_policies","operator",ID,"classic_queues"|T], V}) ->
759+
{["default_policies","operator",ID|T],V};
760+
(E) -> E
761+
end),
748762
Props1 = lists:map(
749763
fun({K, Ss}) ->
750764
{K,

deps/rabbit/src/rabbit_cuttlefish.erl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@
88
-module(rabbit_cuttlefish).
99

1010
-export([
11-
aggregate_props/2
11+
aggregate_props/2,
12+
aggregate_props/3
1213
]).
1314

1415
-type keyed_props() :: [{binary(), [{binary(), any()}]}].
1516

1617
-spec aggregate_props([{string(), any()}], [string()]) ->
1718
keyed_props().
1819
aggregate_props(Conf, Prefix) ->
20+
aggregate_props(Conf, Prefix, fun(E) -> E end).
21+
22+
-spec aggregate_props([{string(), any()}], [string()], function()) ->
23+
keyed_props().
24+
aggregate_props(Conf, Prefix, KeyFun) ->
1925
Pattern = Prefix ++ ["$id", "$_"],
2026
PrefixLen = length(Prefix),
2127
FlatList = lists:filtermap(
22-
fun({K, V}) ->
28+
fun(E) ->
29+
{K, V} = KeyFun(E),
2330
case cuttlefish_variable:is_fuzzy_match(K, Pattern) of
2431
true -> {true, {lists:nthtail(PrefixLen, K), V}};
25-
_ -> false
32+
false -> false
2633
end
2734
end,
2835
Conf

deps/rabbit/src/rabbit_mirror_queue_misc.erl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
-module(rabbit_mirror_queue_misc).
99
-behaviour(rabbit_policy_validator).
10+
-behaviour(rabbit_policy_merge_strategy).
1011

1112
-include("amqqueue.hrl").
1213

@@ -15,6 +16,7 @@
1516
initial_queue_node/2, suggested_queue_nodes/1, actual_queue_nodes/1,
1617
is_mirrored/1, is_mirrored_ha_nodes/1,
1718
update_mirrors/2, update_mirrors/1, validate_policy/1,
19+
merge_policy_value/3,
1820
maybe_auto_sync/1, maybe_drop_master_after_sync/1,
1921
sync_batch_size/1, default_max_sync_throughput/0,
2022
log_info/3, log_warning/3]).
@@ -46,6 +48,14 @@
4648
[policy_validator, <<"ha-promote-on-shutdown">>, ?MODULE]}},
4749
{mfa, {rabbit_registry, register,
4850
[policy_validator, <<"ha-promote-on-failure">>, ?MODULE]}},
51+
{mfa, {rabbit_registry, register,
52+
[operator_policy_validator, <<"ha-mode">>, ?MODULE]}},
53+
{mfa, {rabbit_registry, register,
54+
[operator_policy_validator, <<"ha-params">>, ?MODULE]}},
55+
{mfa, {rabbit_registry, register,
56+
[policy_merge_strategy, <<"ha-mode">>, ?MODULE]}},
57+
{mfa, {rabbit_registry, register,
58+
[policy_merge_strategy, <<"ha-params">>, ?MODULE]}},
4959
{requires, rabbit_registry},
5060
{enables, recovery}]}).
5161

@@ -788,3 +798,36 @@ validate_pof(PromoteOnShutdown) ->
788798
Mode -> {error, "ha-promote-on-failure must be "
789799
"\"always\" or \"when-synced\", got ~tp", [Mode]}
790800
end.
801+
802+
merge_policy_value(<<"ha-mode">>, Val, Val) ->
803+
Val;
804+
merge_policy_value(<<"ha-mode">>, <<"all">> = Val, _OpVal) ->
805+
Val;
806+
merge_policy_value(<<"ha-mode">>, _Val, <<"all">> = OpVal) ->
807+
OpVal;
808+
merge_policy_value(<<"ha-mode">>, <<"exactly">> = Val, _OpVal) ->
809+
Val;
810+
merge_policy_value(<<"ha-mode">>, _Val, <<"exactly">> = OpVal) ->
811+
OpVal;
812+
%% Both values are integers, both are ha-mode 'exactly'
813+
merge_policy_value(<<"ha-params">>, Val, OpVal) when is_integer(Val)
814+
andalso
815+
is_integer(OpVal)->
816+
if Val > OpVal ->
817+
Val;
818+
true ->
819+
OpVal
820+
end;
821+
%% The integer values is of ha-mode 'exactly', the other is a list and of
822+
%% ha-mode 'nodes'. 'exactly' takes precedence
823+
merge_policy_value(<<"ha-params">>, Val, _OpVal) when is_integer(Val) ->
824+
Val;
825+
merge_policy_value(<<"ha-params">>, _Val, OpVal) when is_integer(OpVal) ->
826+
OpVal;
827+
%% Both values are lists, of ha-mode 'nodes', max length takes precedence.
828+
merge_policy_value(<<"ha-params">>, Val, OpVal) ->
829+
if length(Val) > length(OpVal) ->
830+
Val;
831+
true ->
832+
OpVal
833+
end.

deps/rabbit/test/config_schema_SUITE_data/rabbit.snippets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,13 @@ ssl_options.fail_if_no_peer_cert = true",
123123
default_policies.operator.a.expires = 1h
124124
default_policies.operator.a.queue_pattern = apple
125125
default_policies.operator.a.vhost_pattern = banana
126+
default_policies.operator.a.classic_queues.ha_mode = exactly
127+
default_policies.operator.a.classic_queues.ha_params = 2
126128
",
127129
[{rabbit, [{default_policies, [{operator, [
128130
{<<"a">>, [{<<"expires">>, 3600000},
131+
{<<"ha-mode">>, "exactly"},
132+
{<<"ha-params">>, 2},
129133
{<<"queue-pattern">>, "apple"},
130134
{<<"vhost-pattern">>, "banana"}]}]}]}]}],
131135
[]},

deps/rabbit/test/policy_SUITE.erl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
-include_lib("common_test/include/ct.hrl").
1111
-include_lib("amqp_client/include/amqp_client.hrl").
1212

13+
1314
-compile(export_all).
1415

1516
all() ->
@@ -20,6 +21,7 @@ all() ->
2021
groups() ->
2122
[
2223
{cluster_size_2, [], [
24+
target_count_policy,
2325
policy_ttl,
2426
operator_policy_ttl,
2527
operator_retroactive_policy_ttl,
@@ -149,6 +151,59 @@ operator_retroactive_policy_publish_ttl(Config) ->
149151
rabbit_ct_client_helpers:close_connection(Conn),
150152
passed.
151153

154+
target_count_policy(Config) ->
155+
[Server | _] = Nodes = rabbit_ct_broker_helpers:get_node_configs(Config, nodename),
156+
{Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
157+
QName = <<"policy_ha">>,
158+
declare(Ch, QName),
159+
BNodes = [atom_to_binary(N) || N <- Nodes],
160+
161+
AllPolicy = [{<<"ha-mode">>, <<"all">>}],
162+
ExactlyPolicyOne = [{<<"ha-mode">>, <<"exactly">>},
163+
{<<"ha-params">>, 1}],
164+
ExactlyPolicyTwo = [{<<"ha-mode">>, <<"exactly">>},
165+
{<<"ha-params">>, 2}],
166+
NodesPolicyAll = [{<<"ha-mode">>, <<"nodes">>},
167+
{<<"ha-params">>, BNodes}],
168+
NodesPolicyOne = [{<<"ha-mode">>, <<"nodes">>},
169+
{<<"ha-params">>, [hd(BNodes)]}],
170+
171+
%% ALL has precedence
172+
Opts = #{config => Config,
173+
server => Server,
174+
qname => QName},
175+
176+
verify_policies(AllPolicy, ExactlyPolicyTwo, [{<<"ha-mode">>, <<"all">>}], Opts),
177+
178+
verify_policies(ExactlyPolicyTwo, AllPolicy, [{<<"ha-mode">>, <<"all">>}], Opts),
179+
180+
verify_policies(AllPolicy, NodesPolicyAll, [{<<"ha-mode">>, <<"all">>}], Opts),
181+
182+
verify_policies(NodesPolicyAll, AllPolicy, [{<<"ha-mode">>, <<"all">>}], Opts),
183+
184+
%% exactly has precedence over nodes
185+
verify_policies(ExactlyPolicyTwo, NodesPolicyAll,[{<<"ha-mode">>, <<"exactly">>}, {<<"ha-params">>, 2}], Opts),
186+
187+
verify_policies(NodesPolicyAll, ExactlyPolicyTwo, [{<<"ha-mode">>, <<"exactly">>}, {<<"ha-params">>, 2}], Opts),
188+
189+
%% Highest exactly value has precedence
190+
verify_policies(ExactlyPolicyTwo, ExactlyPolicyOne, [{<<"ha-mode">>, <<"exactly">>}, {<<"ha-params">>, 2}], Opts),
191+
192+
verify_policies(ExactlyPolicyOne, ExactlyPolicyTwo, [{<<"ha-mode">>, <<"exactly">>}, {<<"ha-params">>, 2}], Opts),
193+
194+
%% Longest node count has precedence
195+
SortedNodes = lists:sort(BNodes),
196+
verify_policies(NodesPolicyAll, NodesPolicyOne, [{<<"ha-mode">>, <<"nodes">>}, {<<"ha-params">>, SortedNodes}], Opts),
197+
verify_policies(NodesPolicyOne, NodesPolicyAll, [{<<"ha-mode">>, <<"nodes">>}, {<<"ha-params">>, SortedNodes}], Opts),
198+
199+
delete(Ch, QName),
200+
rabbit_ct_broker_helpers:clear_policy(Config, 0, <<"policy">>),
201+
rabbit_ct_broker_helpers:clear_operator_policy(Config, 0, <<"op_policy">>),
202+
rabbit_ct_client_helpers:close_channel(Ch),
203+
rabbit_ct_client_helpers:close_connection(Conn),
204+
passed.
205+
206+
152207
%%----------------------------------------------------------------------------
153208

154209

@@ -201,4 +256,26 @@ get_messages(Number, Ch, Q) ->
201256
exit(failed)
202257
end.
203258

259+
check_policy_value(Server, QName, Value) ->
260+
{ok, Q} = rpc:call(Server, rabbit_amqqueue, lookup, [rabbit_misc:r(<<"/">>, queue, QName)]),
261+
proplists:get_value(Value, rpc:call(Server, rabbit_policy, effective_definition, [Q])).
262+
263+
verify_policies(Policy, OperPolicy, VerifyFuns, #{config := Config,
264+
server := Server,
265+
qname := QName}) ->
266+
rabbit_ct_broker_helpers:set_policy(Config, 0, <<"policy">>,
267+
<<"policy_ha">>, <<"queues">>,
268+
Policy),
269+
rabbit_ct_broker_helpers:set_operator_policy(Config, 0, <<"op_policy">>,
270+
<<"policy_ha">>, <<"queues">>,
271+
OperPolicy),
272+
verify_policy(VerifyFuns, Server, QName).
273+
274+
verify_policy([], _, _) ->
275+
ok;
276+
verify_policy([{HA, Expect} | Tail], Server, QName) ->
277+
Expect = check_policy_value(Server, QName, HA),
278+
verify_policy(Tail, Server, QName).
279+
280+
204281
%%----------------------------------------------------------------------------

0 commit comments

Comments
 (0)