/
Elixir.RabbitMQ.CLI.Ctl.Commands.AddSuperStreamCommand.erl
304 lines (281 loc) · 11.6 KB
/
Elixir.RabbitMQ.CLI.Ctl.Commands.AddSuperStreamCommand.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
%% The contents of this file are subject to the Mozilla Public License
%% Version 2.0 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License
%% at https://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
-module('Elixir.RabbitMQ.CLI.Ctl.Commands.AddSuperStreamCommand').
-include_lib("rabbitmq_stream_common/include/rabbit_stream.hrl").
-behaviour('Elixir.RabbitMQ.CLI.CommandBehaviour').
-export([scopes/0,
usage/0,
usage_additional/0,
usage_doc_guides/0,
switches/0,
banner/2,
validate/2,
merge_defaults/2,
run/2,
output/2,
description/0,
help_section/0]).
scopes() ->
[streams].
description() ->
<<"Add a super stream">>.
switches() ->
[{partitions, integer},
{binding_keys, string},
{routing_keys, string},
{max_length_bytes, string},
{max_age, string},
{stream_max_segment_size_bytes, string},
{leader_locator, string},
{initial_cluster_size, integer}].
help_section() ->
{plugin, stream}.
validate([], _Opts) ->
{validation_failure, not_enough_args};
validate([_Name], #{routing_keys := _, binding_keys := _}) ->
{validation_failure,
"Specify --binding-keys only."};
validate([_Name], #{partitions := _, binding_keys := _}) ->
{validation_failure,
"Specify --partitions or --binding-keys, not both."};
validate([_Name], #{partitions := _, routing_keys := _}) ->
{validation_failure,
"Specify --partitions or --binding-keys, not both."};
validate([_Name], #{partitions := Partitions}) when Partitions < 1 ->
{validation_failure, "The partition number must be greater than 0"};
validate([_Name], Opts) ->
validate_stream_arguments(Opts);
validate(_, _Opts) ->
{validation_failure, too_many_args}.
validate_stream_arguments(#{max_length_bytes := Value} = Opts) ->
case parse_information_unit(Value) of
error ->
{validation_failure,
"Invalid value for --max-length-bytes, valid example "
"values: 100gb, 50mb"};
_ ->
validate_stream_arguments(maps:remove(max_length_bytes, Opts))
end;
validate_stream_arguments(#{max_age := Value} = Opts) ->
case rabbit_date_time:parse_duration(Value) of
{ok, _} ->
validate_stream_arguments(maps:remove(max_age, Opts));
error ->
{validation_failure,
"Invalid value for --max-age, the value must a "
"ISO 8601 duration, e.g. e.g. PT10M30S for 10 "
"minutes 30 seconds, P5DT8H for 5 days 8 hours."}
end;
validate_stream_arguments(#{stream_max_segment_size_bytes := Value} =
Opts) ->
case parse_information_unit(Value) of
error ->
{validation_failure,
"Invalid value for --stream-max-segment-size-bytes, "
"valid example values: 100gb, 50mb"};
_ ->
validate_stream_arguments(maps:remove(stream_max_segment_size_bytes,
Opts))
end;
validate_stream_arguments(#{leader_locator := <<"client-local">>} =
Opts) ->
validate_stream_arguments(maps:remove(leader_locator, Opts));
validate_stream_arguments(#{leader_locator := <<"balanced">>} =
Opts) ->
validate_stream_arguments(maps:remove(leader_locator, Opts));
%% 'random' and 'least-leaders' are deprecated and get mapped to 'balanced'
validate_stream_arguments(#{leader_locator := <<"random">>} = Opts) ->
validate_stream_arguments(maps:remove(leader_locator, Opts));
validate_stream_arguments(#{leader_locator := <<"least-leaders">>} =
Opts) ->
validate_stream_arguments(maps:remove(leader_locator, Opts));
validate_stream_arguments(#{leader_locator := _}) ->
{validation_failure,
"Invalid value for --leader-locator, valid values "
"are client-local, balanced."};
validate_stream_arguments(#{initial_cluster_size := Value} = Opts) ->
try
case rabbit_data_coercion:to_integer(Value) of
S when S > 0 ->
validate_stream_arguments(maps:remove(initial_cluster_size,
Opts));
_ ->
{validation_failure,
"Invalid value for --initial-cluster-size, the "
"value must be positive."}
end
catch
error:_ ->
{validation_failure,
"Invalid value for --initial-cluster-size, the "
"value must be a positive integer."}
end;
validate_stream_arguments(_) ->
ok.
merge_defaults(_Args, #{binding_keys := _V} = Opts) ->
{_Args, maps:merge(#{vhost => <<"/">>}, Opts)};
merge_defaults(_Args, #{routing_keys := RKs} = Opts) ->
{_Args, maps:merge(#{vhost => <<"/">>, binding_keys => RKs},
maps:remove(routing_keys, Opts))};
merge_defaults(_Args, Opts) ->
{_Args, maps:merge(#{partitions => 3, vhost => <<"/">>}, Opts)}.
usage() ->
<<"add_super_stream <name> [--vhost <vhost>] [--partition"
"s <partitions>] [--binding-keys <binding-keys>]">>.
usage_additional() ->
[[<<"<name>">>,
<<"The name of the super stream.">>],
[<<"--vhost <vhost>">>,
<<"The virtual host the super stream is added to.">>],
[<<"--partitions <partitions>">>,
<<"The number of partitions, default is 3. Mutually exclusive with --routing-keys.">>],
[<<"--binding-keys <binding-keys>">>,
<<"Comma-separated list of binding keys. Mutually exclusive with --partitions.">>],
[<<"--max-length-bytes <max-length-bytes>">>,
<<"The maximum size of partition streams, example values: 20gb, 500mb.">>],
[<<"--max-age <max-age>">>,
<<"The maximum age of partition stream segments, using the ISO 8601 duration format, e.g. PT10M30S for 10 minutes 30 seconds, P5DT8H for 5 days 8 hours.">>],
[<<"--stream-max-segment-size-bytes <stream-max-segment-size-bytes>">>,
<<"The maximum size of partition stream segments, example values: 500mb, 1gb.">>],
[<<"--leader-locator <leader-locator>">>,
<<"Leader locator strategy for partition streams, possible values are client-local, balanced.">>],
[<<"--initial-cluster-size <initial-cluster-size>">>,
<<"The initial cluster size of partition streams.">>]].
usage_doc_guides() ->
[?STREAMS_GUIDE_URL].
run([SuperStream],
#{node := NodeName,
vhost := VHost,
timeout := Timeout,
partitions := Partitions} =
Opts) ->
Streams =
[list_to_binary(binary_to_list(SuperStream)
++ "-"
++ integer_to_list(K))
|| K <- lists:seq(0, Partitions - 1)],
RoutingKeys =
[integer_to_binary(K) || K <- lists:seq(0, Partitions - 1)],
create_super_stream(NodeName,
Timeout,
VHost,
SuperStream,
Streams,
stream_arguments(Opts),
RoutingKeys);
run([SuperStream],
#{node := NodeName,
vhost := VHost,
timeout := Timeout,
binding_keys := BindingKeysStr} =
Opts) ->
BindingKeys =
[rabbit_data_coercion:to_binary(
string:strip(K))
|| K
<- string:tokens(
rabbit_data_coercion:to_list(BindingKeysStr), ",")],
Streams =
[list_to_binary(binary_to_list(SuperStream)
++ "-"
++ binary_to_list(K))
|| K <- BindingKeys],
create_super_stream(NodeName,
Timeout,
VHost,
SuperStream,
Streams,
stream_arguments(Opts),
BindingKeys).
stream_arguments(Opts) ->
stream_arguments(#{}, Opts).
%% Something strange, dialyzer infers that map_size/1 returns positive_integer()
-dialyzer({no_match, stream_arguments/2}).
stream_arguments(Acc, Arguments) when map_size(Arguments) =:= 0 ->
Acc;
stream_arguments(Acc, #{max_length_bytes := Value} = Arguments) ->
stream_arguments(maps:put(<<"max-length-bytes">>,
parse_information_unit(Value), Acc),
maps:remove(max_length_bytes, Arguments));
stream_arguments(Acc, #{max_age := Value} = Arguments) ->
{ok, Duration} = rabbit_date_time:parse_duration(Value),
DurationInSeconds = duration_to_seconds(Duration),
stream_arguments(maps:put(<<"max-age">>,
list_to_binary(integer_to_list(DurationInSeconds)
++ "s"),
Acc),
maps:remove(max_age, Arguments));
stream_arguments(Acc,
#{stream_max_segment_size_bytes := Value} = Arguments) ->
stream_arguments(maps:put(<<"stream-max-segment-size-bytes">>,
parse_information_unit(Value), Acc),
maps:remove(stream_max_segment_size_bytes, Arguments));
stream_arguments(Acc, #{initial_cluster_size := Value} = Arguments) ->
stream_arguments(maps:put(<<"initial-cluster-size">>,
rabbit_data_coercion:to_binary(Value), Acc),
maps:remove(initial_cluster_size, Arguments));
stream_arguments(Acc, #{leader_locator := Value} = Arguments) ->
stream_arguments(maps:put(<<"queue-leader-locator">>, Value, Acc),
maps:remove(leader_locator, Arguments));
stream_arguments(ArgumentsAcc, _Arguments) ->
ArgumentsAcc.
duration_to_seconds([{sign, _},
{years, Y},
{months, M},
{days, D},
{hours, H},
{minutes, Mn},
{seconds, S}]) ->
Y * 365 * 86400 + M * 30 * 86400 + D * 86400 + H * 3600 + Mn * 60 + S.
create_super_stream(NodeName,
Timeout,
VHost,
SuperStream,
Streams,
Arguments,
RoutingKeys) ->
case rabbit_misc:rpc_call(NodeName,
rabbit_stream_manager,
create_super_stream,
[VHost,
SuperStream,
Streams,
Arguments,
RoutingKeys,
cli_acting_user()],
Timeout)
of
ok ->
{ok,
rabbit_misc:format("Super stream ~ts has been created",
[SuperStream])};
Error ->
Error
end.
banner(_, _) ->
<<"Adding a super stream...">>.
output({error, Msg}, _Opts) ->
{error, 'Elixir.RabbitMQ.CLI.Core.ExitCodes':exit_software(), Msg};
output({ok, Msg}, _Opts) ->
{ok, Msg}.
cli_acting_user() ->
'Elixir.RabbitMQ.CLI.Core.Helpers':cli_acting_user().
parse_information_unit(Value) ->
case rabbit_resource_monitor_misc:parse_information_unit(Value) of
{ok, R} ->
integer_to_binary(R);
{error, _} ->
error
end.