Skip to content

Commit 8e7bd20

Browse files
Add new local random exchange type.
This exchange type will only bind classic queues and will only return routes for queues that are local to the publishing connection. If more than one queue is bound it will make a random choice of the locally bound queues. This exchange type is suitable as a component in systems that run highly available low-latency RPC workloads. Co-authored-by: Marcial Rosales <mrosales@pivotal.io>
1 parent b4efe04 commit 8e7bd20

File tree

5 files changed

+321
-1
lines changed

5 files changed

+321
-1
lines changed

deps/rabbit/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,14 @@ rabbitmq_integration_suite(
10621062
],
10631063
)
10641064

1065+
rabbitmq_integration_suite(
1066+
name = "rabbit_local_random_exchange_SUITE",
1067+
size = "small",
1068+
additional_beam = [
1069+
":test_queue_utils_beam",
1070+
],
1071+
)
1072+
10651073
rabbitmq_integration_suite(
10661074
name = "rabbit_direct_reply_to_prop_SUITE",
10671075
size = "medium",

deps/rabbit/app.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def all_beam_files(name = "all_beam_files"):
131131
"src/rabbit_exchange_type_fanout.erl",
132132
"src/rabbit_exchange_type_headers.erl",
133133
"src/rabbit_exchange_type_invalid.erl",
134+
"src/rabbit_exchange_type_local_random.erl",
134135
"src/rabbit_exchange_type_topic.erl",
135136
"src/rabbit_feature_flags.erl",
136137
"src/rabbit_ff_controller.erl",
@@ -391,6 +392,7 @@ def all_test_beam_files(name = "all_test_beam_files"):
391392
"src/rabbit_exchange_type_fanout.erl",
392393
"src/rabbit_exchange_type_headers.erl",
393394
"src/rabbit_exchange_type_invalid.erl",
395+
"src/rabbit_exchange_type_local_random.erl",
394396
"src/rabbit_exchange_type_topic.erl",
395397
"src/rabbit_feature_flags.erl",
396398
"src/rabbit_ff_controller.erl",
@@ -670,6 +672,7 @@ def all_srcs(name = "all_srcs"):
670672
"src/rabbit_exchange_type_fanout.erl",
671673
"src/rabbit_exchange_type_headers.erl",
672674
"src/rabbit_exchange_type_invalid.erl",
675+
"src/rabbit_exchange_type_local_random.erl",
673676
"src/rabbit_exchange_type_topic.erl",
674677
"src/rabbit_feature_flags.erl",
675678
"src/rabbit_ff_controller.erl",
@@ -2048,7 +2051,6 @@ def test_suite_beam_files(name = "test_suite_beam_files"):
20482051
erlc_opts = "//:test_erlc_opts",
20492052
deps = ["//deps/amqp_client:erlang_app"],
20502053
)
2051-
20522054
erlang_bytecode(
20532055
name = "test_event_recorder_beam",
20542056
testonly = True,
@@ -2129,3 +2131,12 @@ def test_suite_beam_files(name = "test_suite_beam_files"):
21292131
erlc_opts = "//:test_erlc_opts",
21302132
deps = ["//deps/amqp_client:erlang_app"],
21312133
)
2134+
erlang_bytecode(
2135+
name = "rabbit_local_random_exchange_SUITE_beam_files",
2136+
testonly = True,
2137+
srcs = ["test/rabbit_local_random_exchange_SUITE.erl"],
2138+
outs = ["test/rabbit_local_random_exchange_SUITE.beam"],
2139+
app_name = "rabbit",
2140+
erlc_opts = "//:test_erlc_opts",
2141+
deps = ["//deps/amqp_client:erlang_app"],
2142+
)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
%%
7+
8+
-module(rabbit_exchange_type_local_random).
9+
-behaviour(rabbit_exchange_type).
10+
-include_lib("rabbit_common/include/rabbit.hrl").
11+
12+
-rabbit_boot_step({?MODULE,
13+
[{description, "exchange type local random"},
14+
{mfa, {rabbit_registry, register,
15+
[exchange, <<"x-local-random">>, ?MODULE]}},
16+
{requires, rabbit_registry},
17+
{enables, kernel_ready}
18+
]}).
19+
20+
-export([add_binding/3,
21+
assert_args_equivalence/2,
22+
create/2,
23+
delete/2,
24+
policy_changed/2,
25+
description/0,
26+
recover/2,
27+
remove_bindings/3,
28+
validate_binding/2,
29+
route/3,
30+
serialise_events/0,
31+
validate/1,
32+
info/1,
33+
info/2
34+
]).
35+
36+
description() ->
37+
[{name, <<"x-local-random">>},
38+
{description, <<"Picks one random local binding (queue) to route via (to).">>}].
39+
40+
route(#exchange{name = Name}, _Msg, _Opts) ->
41+
Matches = rabbit_router:match_routing_key(Name, [<<>>]),
42+
case lists:filter(fun filter_local_queue/1, Matches) of
43+
[] ->
44+
[];
45+
[_] = One ->
46+
One;
47+
LocalMatches ->
48+
Rand = rand:uniform(length(LocalMatches)),
49+
[lists:nth(Rand, LocalMatches)]
50+
end.
51+
52+
info(_X) -> [].
53+
info(_X, _) -> [].
54+
serialise_events() -> false.
55+
validate(_X) -> ok.
56+
create(_Serial, _X) -> ok.
57+
recover(_X, _Bs) -> ok.
58+
delete(_Serial, _X) -> ok.
59+
policy_changed(_X1, _X2) -> ok.
60+
add_binding(_Serial, _X, _B) -> ok.
61+
remove_bindings(_Serial, _X, _Bs) -> ok.
62+
63+
validate_binding(_X, #binding{destination = Dest,
64+
key = <<>>}) ->
65+
case rabbit_amqqueue:lookup(Dest) of
66+
{ok, Q} ->
67+
case amqqueue:get_type(Q) of
68+
rabbit_classic_queue ->
69+
ok;
70+
Type ->
71+
{error, {binding_invalid,
72+
"Queue type ~ts not valid for this exchange type",
73+
[Type]}}
74+
end;
75+
_ ->
76+
{error, {binding_invalid,
77+
"Destination not found",
78+
[]}}
79+
end;
80+
validate_binding(_X, #binding{key = BKey}) ->
81+
{error, {binding_invalid,
82+
"Non empty binding '~s' key not permitted",
83+
[BKey]}}.
84+
85+
assert_args_equivalence(X, Args) ->
86+
rabbit_exchange:assert_args_equivalence(X, Args).
87+
88+
filter_local_queue(QName) ->
89+
%% TODO: introduce lookup function that _only_ gets the pid
90+
case rabbit_amqqueue:lookup(QName) of
91+
{ok, Q} ->
92+
case amqqueue:get_pid(Q) of
93+
Pid when is_pid(Pid) andalso
94+
node(Pid) =:= node() ->
95+
is_process_alive(Pid);
96+
_ ->
97+
false
98+
end;
99+
_ ->
100+
false
101+
end.
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
%%
7+
-module(rabbit_local_random_exchange_SUITE).
8+
9+
-compile(nowarn_export_all).
10+
-compile(export_all).
11+
12+
-include_lib("amqp_client/include/amqp_client.hrl").
13+
-include_lib("eunit/include/eunit.hrl").
14+
15+
all() ->
16+
[
17+
{group, non_parallel_tests}
18+
].
19+
20+
groups() ->
21+
[
22+
{non_parallel_tests, [], [
23+
routed_to_one_local_queue_test,
24+
no_route
25+
]}
26+
].
27+
28+
%% -------------------------------------------------------------------
29+
%% Test suite setup/teardown
30+
%% -------------------------------------------------------------------
31+
32+
init_per_suite(Config) ->
33+
rabbit_ct_helpers:log_environment(),
34+
Config1 = rabbit_ct_helpers:set_config(Config,
35+
[
36+
{rmq_nodename_suffix, ?MODULE},
37+
{rmq_nodes_count, 3},
38+
{tcp_ports_base}
39+
]),
40+
rabbit_ct_helpers:run_setup_steps(Config1,
41+
rabbit_ct_broker_helpers:setup_steps() ++
42+
rabbit_ct_client_helpers:setup_steps()).
43+
44+
end_per_suite(Config) ->
45+
rabbit_ct_helpers:run_teardown_steps(
46+
Config,
47+
rabbit_ct_client_helpers:teardown_steps() ++
48+
rabbit_ct_broker_helpers:teardown_steps()).
49+
50+
init_per_group(_, Config) ->
51+
Config.
52+
53+
end_per_group(_, Config) ->
54+
Config.
55+
56+
init_per_testcase(Testcase, Config) ->
57+
TestCaseName = rabbit_ct_helpers:config_to_testcase_name(Config, Testcase),
58+
Config1 = rabbit_ct_helpers:set_config(Config, {test_resource_name,
59+
re:replace(TestCaseName, "/", "-",
60+
[global, {return, list}])}),
61+
rabbit_ct_helpers:testcase_started(Config1, Testcase).
62+
63+
end_per_testcase(Testcase, Config) ->
64+
rabbit_ct_helpers:testcase_finished(Config, Testcase).
65+
66+
%% -------------------------------------------------------------------
67+
%% Test cases
68+
%% -------------------------------------------------------------------
69+
70+
routed_to_one_local_queue_test(Config) ->
71+
E = make_exchange_name(Config, "0"),
72+
declare_exchange(Config, E),
73+
%% declare queue on the first two nodes: 0, 1
74+
QueueNames = declare_and_bind_queues(Config, 2, E),
75+
%% publish message on node 1
76+
publish(Config, E, 0),
77+
publish(Config, E, 1),
78+
%% message should arrive to queue on node 1
79+
run_on_node(Config, 0,
80+
fun(Chan) ->
81+
assert_queue_size(Config, Chan, 1, lists:nth(1, QueueNames))
82+
end),
83+
run_on_node(Config, 0,
84+
fun(Chan) ->
85+
assert_queue_size(Config, Chan, 1, lists:nth(2, QueueNames))
86+
end),
87+
delete_exchange_and_queues(Config, E, QueueNames),
88+
ok.
89+
90+
no_route(Config) ->
91+
92+
E = make_exchange_name(Config, "0"),
93+
declare_exchange(Config, E),
94+
%% declare queue on nodes 0, 1
95+
QueueNames = declare_and_bind_queues(Config, 2, E),
96+
%% publish message on node 2
97+
publish_expect_return(Config, E, 2),
98+
%% message should arrive to any of the other nodes. Total size among all queues is 1
99+
delete_exchange_and_queues(Config, E, QueueNames),
100+
ok.
101+
102+
delete_exchange_and_queues(Config, E, QueueNames) ->
103+
run_on_node(Config, 0,
104+
fun(Chan) ->
105+
amqp_channel:call(Chan, #'exchange.delete'{exchange = E }),
106+
[amqp_channel:call(Chan, #'queue.delete'{queue = Q })
107+
|| Q <- QueueNames]
108+
end).
109+
publish(Config, E, Node) ->
110+
run_on_node(Config, Node,
111+
fun(Chan) ->
112+
amqp_channel:call(Chan, #'confirm.select'{}),
113+
amqp_channel:call(Chan,
114+
#'basic.publish'{exchange = E, routing_key = rnd()},
115+
#amqp_msg{props = #'P_basic'{}, payload = <<>>}),
116+
amqp_channel:wait_for_confirms_or_die(Chan)
117+
end).
118+
119+
publish_expect_return(Config, E, Node) ->
120+
run_on_node(Config, Node,
121+
fun(Chan) ->
122+
amqp_channel:register_return_handler(Chan, self()),
123+
amqp_channel:call(Chan,
124+
#'basic.publish'{exchange = E,
125+
mandatory = true,
126+
routing_key = rnd()},
127+
#amqp_msg{props = #'P_basic'{},
128+
payload = <<>>}),
129+
receive
130+
{#'basic.return'{}, _} ->
131+
ok
132+
after 5000 ->
133+
flush(100),
134+
ct:fail("no return received")
135+
end
136+
end).
137+
138+
run_on_node(Config, Node, RunMethod) ->
139+
{Conn, Chan} = rabbit_ct_client_helpers:open_connection_and_channel(Config, Node),
140+
Return = RunMethod(Chan),
141+
rabbit_ct_client_helpers:close_connection_and_channel(Conn, Chan),
142+
Return.
143+
144+
declare_exchange(Config, ExchangeName) ->
145+
run_on_node(Config, 0,
146+
fun(Chan) ->
147+
#'exchange.declare_ok'{} =
148+
amqp_channel:call(Chan,
149+
#'exchange.declare'{exchange = ExchangeName,
150+
type = <<"x-local-random">>,
151+
auto_delete = false})
152+
end).
153+
154+
declare_and_bind_queues(Config, NodeCount, E) ->
155+
QueueNames = [make_queue_name(Config, Node) || Node <- lists:seq(0, NodeCount -1)],
156+
[run_on_node(Config, Node,
157+
fun(Chan) ->
158+
declare_and_bind_queue(Chan, E, make_queue_name(Config, Node))
159+
end) || Node <- lists:seq(0, NodeCount -1)],
160+
QueueNames.
161+
162+
declare_and_bind_queue(Ch, E, Q) ->
163+
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = Q}),
164+
#'queue.bind_ok'{} = amqp_channel:call(Ch, #'queue.bind'{queue = Q,
165+
exchange = E,
166+
routing_key = <<"">>}),
167+
ok.
168+
169+
assert_total_queue_size(_Config, Chan, ExpectedSize, ExpectedQueues) ->
170+
Counts = [begin
171+
#'queue.declare_ok'{message_count = M} =
172+
amqp_channel:call(Chan, #'queue.declare'{queue = Q}),
173+
M
174+
end || Q <- ExpectedQueues],
175+
?assertEqual(ExpectedSize, lists:sum(Counts)).
176+
177+
assert_queue_size(_Config, Chan, ExpectedSize, ExpectedQueue) ->
178+
ct:log("assert_queue_size ~p ~p", [ExpectedSize, ExpectedQueue]),
179+
#'queue.declare_ok'{message_count = M} =
180+
amqp_channel:call(Chan, #'queue.declare'{queue = ExpectedQueue}),
181+
?assertEqual(ExpectedSize, M).
182+
183+
rnd() ->
184+
list_to_binary(integer_to_list(rand:uniform(1000000))).
185+
186+
make_exchange_name(Config, Suffix) ->
187+
B = rabbit_ct_helpers:get_config(Config, test_resource_name),
188+
erlang:list_to_binary("x-" ++ B ++ "-" ++ Suffix).
189+
make_queue_name(Config, Node) ->
190+
B = rabbit_ct_helpers:get_config(Config, test_resource_name),
191+
erlang:list_to_binary("q-" ++ B ++ "-" ++ integer_to_list(Node)).
192+
193+
flush(T) ->
194+
receive X ->
195+
ct:pal("flushed ~p", [X]),
196+
flush(T)
197+
after T ->
198+
ok
199+
end.

moduleindex.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ rabbit:
647647
- rabbit_exchange_type_fanout
648648
- rabbit_exchange_type_headers
649649
- rabbit_exchange_type_invalid
650+
- rabbit_exchange_type_local_random
650651
- rabbit_exchange_type_topic
651652
- rabbit_feature_flags
652653
- rabbit_ff_controller

0 commit comments

Comments
 (0)