Skip to content

Commit 8f0ae84

Browse files
Merge pull request #7000 from rabbitmq/dialyzer-warnings-abbitmq_peer_discovery_consul
Fix all dialyzer warnings in peer discovery plugins
2 parents 9db9db4 + 183a260 commit 8f0ae84

18 files changed

+65
-74
lines changed

MODULE.bazel

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ erlang_package.git_package(
227227

228228
erlang_package.hex_package(
229229
name = "thoas",
230-
sha256 = "442296847aca11db8d25180693d7ca3073d6d7179f66952f07b16415306513b6",
231-
version = "0.4.0",
230+
sha256 = "4918d50026c073c4ab1388437132c77a6f6f7c8ac43c60c13758cc0adce2134e",
231+
version = "0.4.1",
232232
)
233233

234234
erlang_package.git_package(

deps/rabbit_common/src/rabbit_json.erl

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ encode(Term, Opts) ->
5555
end,
5656
thoas:encode(fixup_terms(Term, F), Opts).
5757

58-
-spec try_encode(thoas:json_term()) -> {ok, iodata()} |
58+
-spec try_encode(thoas:input_term()) -> {ok, iodata()} |
5959
{error, Reason :: term()}.
6060
try_encode(Term) ->
6161
try_encode(Term, ?DEFAULT_ENCODE_OPTIONS).
6262

63-
-spec try_encode(thoas:json_term(), thoas:decode_options()) ->
63+
-spec try_encode(thoas:input_term(), thoas:encode_options()) ->
6464
{ok, iodata()} | {error, Reason :: term()}.
6565
try_encode(Term, Opts) ->
6666
try

deps/rabbitmq_peer_discovery_common/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ plt(
4444
name = "base_plt",
4545
apps = EXTRA_APPS,
4646
plt = "//:base_plt",
47-
deps = DEPS,
47+
deps = DEPS + RUNTIME_DEPS,
4848
)
4949

5050
dialyze(

deps/rabbitmq_peer_discovery_common/src/rabbit_peer_discovery_httpc.erl

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ put(Scheme, Host, Port, Path, Args, Headers, Body) ->
251251
%% @doc Perform a HTTP PUT request
252252
%% @end
253253
%%
254-
-spec put(Scheme, Host, Port, Path, Args, Headers, HttpOpts, Body) -> {ok, string()} | {error, any()} when
254+
-spec put(Scheme, Host, Port, Path, Args, Headers, HttpOpts, Body) -> {ok, term()} | {error, any()} when
255255
Scheme :: atom() | string(),
256256
Host :: string() | binary(),
257257
Port :: integer(),
@@ -425,7 +425,7 @@ decode_body(?CONTENT_JSON, Body) ->
425425
%% @doc Decode the response body and return a list
426426
%% @end
427427
%%
428-
-spec parse_response({ok, integer(), string()} | {error, any()}) -> {ok, string()} | {error, any()}.
428+
-spec parse_response({ok, integer(), string()} | {error, any()}) -> {ok, term()} | {error, any()}.
429429

430430
parse_response({error, Reason}) ->
431431
?LOG_DEBUG("HTTP error ~tp", [Reason], #{domain => ?RMQLOG_DOMAIN_PEER_DIS}),

deps/rabbitmq_peer_discovery_common/src/rabbit_peer_discovery_util.erl

+6-6
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ nic_ipv4(Device) ->
163163
%% IPv4 address if found.
164164
%% @end
165165
%%--------------------------------------------------------------------
166-
-spec nic_ipv4(Device :: string(), Interfaces :: list())
167-
-> {ok, string()} | {error, not_found}.
166+
-spec nic_ipv4(Device :: string(), Interfaces :: [{string(), [ifopt()]}])
167+
-> {'ok', string()} | {'error', 'not_found'}.
168168
nic_ipv4(_, []) -> {error, not_found};
169169
nic_ipv4(Device, [{Interface, Opts}|_]) when Interface =:= Device ->
170170
{ok, nic_ipv4_address(Opts)};
@@ -178,7 +178,7 @@ nic_ipv4(Device, [_|T]) ->
178178
%% for the interface.
179179
%% @end
180180
%%--------------------------------------------------------------------
181-
-spec nic_ipv4_address([ifopt()]) -> {ok, string()} | {error, not_found}.
181+
-spec nic_ipv4_address([ifopt()]) -> string() | {'error', 'not_found'}.
182182
nic_ipv4_address([]) -> {error, not_found};
183183
nic_ipv4_address([{addr, {A,B,C,D}}|_]) ->
184184
inet_parse:ntoa({A,B,C,D});
@@ -373,9 +373,9 @@ stringify_error({error, Term}) ->
373373
{error, lists:flatten(io_lib:format("~tp", [Term]))}.
374374

375375
-spec maybe_backend_configured(BackendConfigKey :: atom(),
376-
ClusterFormationUndefinedFun :: fun(() -> {ok, term()} | ok),
377-
BackendUndefinedFun :: fun(() -> {ok, term()} | ok),
378-
ConfiguredFun :: fun((list()) -> {ok, term()})) -> {ok, term()}.
376+
ClusterFormationUndefinedFun :: fun(() -> term()),
377+
BackendUndefinedFun :: fun(() -> term()),
378+
ConfiguredFun :: fun((list()) -> term())) -> term().
379379
maybe_backend_configured(BackendConfigKey,
380380
ClusterFormationUndefinedFun,
381381
BackendUndefinedFun,

deps/rabbitmq_peer_discovery_consul/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ plt(
4343
dialyze(
4444
dialyzer_opts = RABBITMQ_DIALYZER_OPTS,
4545
plt = ":base_plt",
46-
warnings_as_errors = False,
4746
)
4847

4948
broker_for_integration_suites()

deps/rabbitmq_peer_discovery_consul/include/rabbit_peer_discovery_consul.hrl

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
},
8787
consul_svc_meta => #peer_discovery_config_entry_meta{
8888
type = list,
89+
env_variable = "CONSUL_SVC_META",
8990
default_value = []
9091
},
9192
consul_deregister_after => #peer_discovery_config_entry_meta{

deps/rabbitmq_peer_discovery_consul/src/rabbit_peer_discovery_consul.erl

+23-24
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ init() ->
4545
ok = application:ensure_started(inets),
4646
%% we cannot start this plugin yet since it depends on the rabbit app,
4747
%% which is in the process of being started by the time this function is called
48-
application:load(rabbitmq_peer_discovery_common),
48+
_ = application:load(rabbitmq_peer_discovery_common),
4949
rabbit_peer_discovery_httpc:maybe_configure_proxy(),
5050
rabbit_peer_discovery_httpc:maybe_configure_inet6().
5151

@@ -181,7 +181,7 @@ lock(Node) ->
181181
-spec unlock({SessionId :: string(), TRef :: timer:tref()}) -> ok.
182182

183183
unlock({SessionId, TRef}) ->
184-
timer:cancel(TRef),
184+
_ = timer:cancel(TRef),
185185
?LOG_DEBUG(
186186
"Stopped session renewal",
187187
#{domain => ?RMQLOG_DOMAIN_PEER_DIS}),
@@ -235,7 +235,7 @@ http_options(HttpOpts0, M) ->
235235
HttpOpts1 = [TLSOpts | HttpOpts0],
236236
HttpOpts1.
237237

238-
-spec filter_nodes(ConsulResult :: list(), AllowWarning :: atom()) -> list().
238+
-spec filter_nodes(ConsulResult :: [#{term() => term()}], AllowWarning :: boolean()) -> [#{term() => term()}].
239239
filter_nodes(Nodes, Warn) ->
240240
case Warn of
241241
true ->
@@ -251,10 +251,10 @@ filter_nodes(Nodes, Warn) ->
251251
false -> Nodes
252252
end.
253253

254-
-spec extract_nodes(ConsulResult :: list()) -> list().
254+
-spec extract_nodes(ConsulResult :: [#{binary() => term()}]) -> list().
255255
extract_nodes(Data) -> extract_nodes(Data, []).
256256

257-
-spec extract_nodes(ConsulResult :: list(), Nodes :: list())
257+
-spec extract_nodes(ConsulResult :: [#{binary() => term()}], Nodes :: list())
258258
-> list().
259259
extract_nodes([], Nodes) -> Nodes;
260260
extract_nodes([H | T], Nodes) ->
@@ -570,8 +570,6 @@ maybe_re_register({error, Reason}) ->
570570
#{domain => ?RMQLOG_DOMAIN_PEER_DIS});
571571
maybe_re_register({ok, {Members, _NodeType}}) ->
572572
maybe_re_register(Members);
573-
maybe_re_register({ok, Members}) ->
574-
maybe_re_register(Members);
575573
maybe_re_register(Members) ->
576574
case lists:member(node(), Members) of
577575
true ->
@@ -589,13 +587,14 @@ maybe_re_register(Members) ->
589587
wait_for_list_nodes() ->
590588
wait_for_list_nodes(60).
591589

590+
-spec wait_for_list_nodes(non_neg_integer()) -> {'ok', term()} | {'error', term()}.
591+
wait_for_list_nodes(0) ->
592+
list_nodes();
592593
wait_for_list_nodes(N) ->
593-
case {list_nodes(), N} of
594-
{Reply, 0} ->
595-
Reply;
596-
{{ok, _} = Reply, _} ->
594+
case list_nodes() of
595+
{ok, _} = Reply ->
597596
Reply;
598-
{{error, _}, _} ->
597+
_ ->
599598
timer:sleep(1000),
600599
wait_for_list_nodes(N - 1)
601600
end.
@@ -606,7 +605,7 @@ wait_for_list_nodes(N) ->
606605
%% Create a session to be acquired for a common key
607606
%% @end
608607
%%--------------------------------------------------------------------
609-
-spec create_session(string(), pos_integer()) -> {ok, string()} | {error, Reason::string()}.
608+
-spec create_session(atom(), pos_integer()) -> {ok, string()} | {error, Reason::string()}.
610609
create_session(Name, TTL) ->
611610
case consul_session_create([], maybe_add_acl([]),
612611
[{'Name', Name},
@@ -623,10 +622,10 @@ create_session(Name, TTL) ->
623622
%% Create session
624623
%% @end
625624
%%--------------------------------------------------------------------
626-
-spec consul_session_create(Query, Headers, Body) -> {ok, string()} | {error, any()} when
625+
-spec consul_session_create(Query, Headers, Body) -> {ok, term()} | {error, any()} when
627626
Query :: list(),
628627
Headers :: [{string(), string()}],
629-
Body :: term().
628+
Body :: thoas:input_term().
630629
consul_session_create(Query, Headers, Body) ->
631630
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
632631
case serialize_json_body(Body) of
@@ -652,7 +651,7 @@ consul_session_create(Query, Headers, Body) ->
652651
%% the JSON serialization library.
653652
%% @end
654653
%%--------------------------------------------------------------------
655-
-spec serialize_json_body(term()) -> {ok, Payload :: binary()} | {error, atom()}.
654+
-spec serialize_json_body(thoas:input_term()) -> {ok, Payload :: binary()} | {error, atom()}.
656655
serialize_json_body([]) -> {ok, []};
657656
serialize_json_body(Payload) ->
658657
case rabbit_json:try_encode(Payload) of
@@ -666,7 +665,7 @@ serialize_json_body(Payload) ->
666665
%% Extract session ID from Consul response
667666
%% @end
668667
%%--------------------------------------------------------------------
669-
-spec get_session_id(term()) -> string().
668+
-spec get_session_id(#{binary() => term()}) -> string().
670669
get_session_id(#{<<"ID">> := ID}) -> binary:bin_to_list(ID).
671670

672671
%%--------------------------------------------------------------------
@@ -675,7 +674,7 @@ get_session_id(#{<<"ID">> := ID}) -> binary:bin_to_list(ID).
675674
%% Start periodically renewing an existing session ttl
676675
%% @end
677676
%%--------------------------------------------------------------------
678-
-spec start_session_ttl_updater(string()) -> ok.
677+
-spec start_session_ttl_updater(string()) -> timer:tref().
679678
start_session_ttl_updater(SessionId) ->
680679
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
681680
Interval = get_config_key(consul_svc_ttl, M),
@@ -693,7 +692,7 @@ start_session_ttl_updater(SessionId) ->
693692
%% @end
694693
-spec lock(timer:tref(), string(), pos_integer(), pos_integer()) -> {ok, string()} | {error, string()}.
695694
lock(TRef, _, Now, EndTime) when EndTime < Now ->
696-
timer:cancel(TRef),
695+
_ = timer:cancel(TRef),
697696
{error, "Acquiring lock taking too long, bailing out"};
698697
lock(TRef, SessionId, _, EndTime) ->
699698
case acquire_lock(SessionId) of
@@ -707,15 +706,15 @@ lock(TRef, SessionId, _, EndTime) ->
707706
ok ->
708707
lock(TRef, SessionId, erlang:system_time(seconds), EndTime);
709708
{error, Reason} ->
710-
timer:cancel(TRef),
709+
_ = timer:cancel(TRef),
711710
{error, lists:flatten(io_lib:format("Error waiting for lock release, reason: ~ts",[Reason]))}
712711
end;
713712
{error, Reason} ->
714-
timer:cancel(TRef),
713+
_ = timer:cancel(TRef),
715714
{error, lists:flatten(io_lib:format("Error obtaining lock status, reason: ~ts", [Reason]))}
716715
end;
717716
{error, Reason} ->
718-
timer:cancel(TRef),
717+
_ = timer:cancel(TRef),
719718
{error, lists:flatten(io_lib:format("Error while acquiring lock, reason: ~ts", [Reason]))}
720719
end.
721720

@@ -747,7 +746,7 @@ release_lock(SessionId) ->
747746
%%--------------------------------------------------------------------
748747
-spec consul_kv_write(Path, Query, Headers, Body) -> {ok, any()} | {error, string()} when
749748
Path :: string(),
750-
Query :: [{string(), string()}],
749+
Query :: [{string() | atom(), string()}],
751750
Headers :: [{string(), string()}],
752751
Body :: term().
753752
consul_kv_write(Path, Query, Headers, Body) ->
@@ -839,7 +838,7 @@ base_path() ->
839838
wait_for_lock_release(false, _, _) -> ok;
840839
wait_for_lock_release(_, Index, Wait) ->
841840
case consul_kv_read(startup_lock_path(),
842-
[{index, Index}, {wait, service_ttl(Wait)}],
841+
[{"index", Index}, {"wait", service_ttl(Wait)}],
843842
maybe_add_acl([])) of
844843
{ok, _} -> ok;
845844
{error, _} = Err -> Err

deps/rabbitmq_peer_discovery_consul/src/rabbitmq_peer_discovery_consul_health_check_helper.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ terminate(_Arg, #state{timer_ref = undefined}) ->
5555
ok;
5656

5757
terminate(_Arg, #state{timer_ref = TRef}) ->
58-
timer:cancel(TRef),
58+
_ = timer:cancel(TRef),
5959
ok.
6060

6161
code_change(_OldVsn, State, _Extra) ->

deps/rabbitmq_peer_discovery_consul/test/rabbitmq_peer_discovery_consul_SUITE.erl

+2-2
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ wait_for_lock_release_with_session_without_token_test(_Config) ->
879879
?assertEqual("localhost", Host),
880880
?assertEqual(8500, Port),
881881
?assertEqual("v1/kv/rabbitmq/default/startup_lock", Path),
882-
?assertEqual([{index, 42}, {wait, "300s"}], Args),
882+
?assertEqual([{"index", 42}, {"wait", "300s"}], Args),
883883
?assertEqual([], Headers),
884884
?assertEqual([], HttpOpts),
885885
{ok, []}
@@ -894,7 +894,7 @@ wait_for_lock_release_with_session_with_token_test(_Config) ->
894894
?assertEqual("localhost", Host),
895895
?assertEqual(8500, Port),
896896
?assertEqual("v1/kv/rabbitmq/default/startup_lock", Path),
897-
?assertEqual([{index, 42}, {wait, "300s"}], Args),
897+
?assertEqual([{"index", 42}, {"wait", "300s"}], Args),
898898
?assertEqual([{"X-Consul-Token", "token-value"}], Headers),
899899
?assertEqual([], HttpOpts),
900900
{ok, []}

deps/rabbitmq_peer_discovery_etcd/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ xref()
3939

4040
plt(
4141
name = "base_plt",
42+
plt = "//:base_plt",
4243
deps = DEPS,
4344
)
4445

4546
dialyze(
4647
dialyzer_opts = RABBITMQ_DIALYZER_OPTS,
4748
plt = ":base_plt",
48-
warnings_as_errors = False,
4949
)
5050

5151
broker_for_integration_suites()

deps/rabbitmq_peer_discovery_etcd/src/rabbit_peer_discovery_etcd.erl

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
init() ->
2727
%% We cannot start this plugin yet since it depends on the rabbit app,
2828
%% which is in the process of being started by the time this function is called
29-
application:load(rabbitmq_peer_discovery_common),
30-
application:load(rabbitmq_peer_discovery_etcd),
29+
_ = application:load(rabbitmq_peer_discovery_common),
30+
_ = application:load(rabbitmq_peer_discovery_etcd),
3131

3232
%% Here we start the client very early on, before plugins have initialized.
3333
%% We need to do it conditionally, however.
3434
NoOp = fun() -> ok end,
3535
Run = fun(_) ->
3636
rabbit_log:debug("Peer discovery etcd: initialising..."),
37-
application:ensure_all_started(eetcd),
37+
_ = application:ensure_all_started(eetcd),
3838
Formation = application:get_env(rabbit, cluster_formation, []),
3939
Opts = maps:from_list(proplists:get_value(peer_discovery_etcd, Formation, [])),
4040
{ok, Pid} = rabbitmq_peer_discovery_etcd_v3_client:start_link(Opts),

deps/rabbitmq_peer_discovery_etcd/src/rabbitmq_peer_discovery_etcd.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ unregister() ->
4444
post_registration() ->
4545
?DELEGATE:post_registration().
4646

47-
-spec lock(Node :: atom()) -> not_supported.
47+
-spec lock(Node :: atom()) -> {'ok', term()} | {'error', string()}.
4848
lock(Node) ->
4949
?DELEGATE:lock(Node).
5050

deps/rabbitmq_peer_discovery_etcd/src/rabbitmq_peer_discovery_etcd_v3_client.erl

+14-20
Original file line numberDiff line numberDiff line change
@@ -231,23 +231,17 @@ connected({call, From}, list_keys, Data = #statem_data{connection_name = Conn})
231231
{ok, #{kvs := Result}} = eetcd_kv:get(C2),
232232
rabbit_log:debug("etcd peer discovery returned keys: ~tp", [Result]),
233233
Values = [maps:get(value, M) || M <- Result],
234-
case Values of
235-
Xs when is_list(Xs) ->
236-
rabbit_log:debug("etcd peer discovery: listing node keys returned ~b results", [length(Xs)]),
237-
ParsedNodes = lists:map(fun extract_node/1, Xs),
238-
{Successes, Failures} = lists:partition(fun filter_node/1, ParsedNodes),
239-
JoinedString = lists:join(",", [rabbit_data_coercion:to_list(Node) || Node <- lists:usort(Successes)]),
240-
rabbit_log:error("etcd peer discovery: successfully extracted nodes: ~ts", [JoinedString]),
241-
lists:foreach(fun(Val) ->
242-
rabbit_log:error("etcd peer discovery: failed to extract node name from etcd value ~tp", [Val])
243-
end, Failures),
244-
gen_statem:reply(From, lists:usort(Successes)),
245-
keep_state_and_data;
246-
Other ->
247-
rabbit_log:debug("etcd peer discovery: listing node keys returned ~tp", [Other]),
248-
gen_statem:reply(From, []),
249-
keep_state_and_data
250-
end.
234+
rabbit_log:debug("etcd peer discovery: listing node keys returned ~b results", [length(Values)]),
235+
ParsedNodes = lists:map(fun extract_node/1, Values),
236+
{Successes, Failures} = lists:partition(fun filter_node/1, ParsedNodes),
237+
JoinedString = lists:join(",", [rabbit_data_coercion:to_list(Node) || Node <- lists:usort(Successes)]),
238+
rabbit_log:error("etcd peer discovery: successfully extracted nodes: ~ts", [JoinedString]),
239+
lists:foreach(fun(Val) ->
240+
rabbit_log:error("etcd peer discovery: failed to extract node name from etcd value ~tp", [Val])
241+
end, Failures),
242+
gen_statem:reply(From, lists:usort(Successes)),
243+
keep_state_and_data.
244+
251245

252246
disconnected(enter, _PrevState, _Data) ->
253247
rabbit_log:info("etcd peer discovery: successfully disconnected from etcd"),
@@ -307,9 +301,9 @@ registration_value(#statem_data{node_key_lease_id = LeaseID, node_key_ttl_in_sec
307301
-spec extract_node(binary()) -> atom() | {error, any()}.
308302

309303
extract_node(Payload) ->
310-
case rabbit_json:decode(Payload) of
304+
case rabbit_json:try_decode(Payload) of
311305
{error, Error} -> {error, Error};
312-
Map ->
306+
{ok, Map} ->
313307
case maps:get(<<"node">>, Map, undefined) of
314308
undefined -> undefined;
315309
Node -> rabbit_data_coercion:to_atom(Node)
@@ -376,7 +370,7 @@ obfuscate(Password) ->
376370

377371
deobfuscate(undefined) -> undefined;
378372
deobfuscate(Password) ->
379-
credentials_obfuscation:decrypt(to_binary(Password)).
373+
credentials_obfuscation:decrypt({encrypted, to_binary(Password)}).
380374

381375
disconnect(ConnName, #statem_data{connection_monitor = Ref}) ->
382376
maybe_demonitor(Ref),

deps/rabbitmq_peer_discovery_k8s/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ plt(
4343
dialyze(
4444
dialyzer_opts = RABBITMQ_DIALYZER_OPTS,
4545
plt = ":base_plt",
46-
warnings_as_errors = False,
4746
)
4847

4948
broker_for_integration_suites()

0 commit comments

Comments
 (0)