diff --git a/.github/workflows/build_slim_packages.yaml b/.github/workflows/build_slim_packages.yaml index f369525e7..640f1298d 100644 --- a/.github/workflows/build_slim_packages.yaml +++ b/.github/workflows/build_slim_packages.yaml @@ -11,7 +11,7 @@ on: jobs: build: - runs-on: ubuntu-20.04 + runs-on: ${{ matrix.runs-on }} strategy: fail-fast: false @@ -21,6 +21,16 @@ jobs: os: - ubuntu20.04 - el8 + runs-on: + - aws-amd64 + - ubuntu-20.04 + use-self-hosted: + - ${{ github.repository_owner == 'emqx' }} + exclude: + - runs-on: ubuntu-20.04 + use-self-hosted: true + - runs-on: aws-amd64 + use-self-hosted: false container: ghcr.io/emqx/emqx-builder/4.4-19:${{ matrix.erl_otp }}-${{ matrix.os }} @@ -38,8 +48,20 @@ jobs: fi - name: fix-git-unsafe-repository run: git config --global --add safe.directory /__w/emqx/emqx + - uses: actions/cache@v2 + with: + # dialyzer PLTs + path: ~/.cache/rebar3/ + key: dialyer-${{ matrix.erl_otp }} - name: make xref run: make xref + - name: make dialyzer + run: | + # TODO: once dialyzer warnings are fixed in 4.4+, we may + # enforce this. + if [[ "$(./pkg-vsn.sh)" =~ 4.3.* ]]; then + make dialyzer + fi - name: build zip packages run: make ${EMQX_NAME}-zip - name: build deb/rpm packages diff --git a/.github/workflows/run_test_cases.yaml b/.github/workflows/run_test_cases.yaml index 138b07993..4e9a5fc37 100644 --- a/.github/workflows/run_test_cases.yaml +++ b/.github/workflows/run_test_cases.yaml @@ -24,8 +24,19 @@ jobs: run: make proper run_common_test: - runs-on: ubuntu-20.04 - + runs-on: ${{ matrix.runs-on }} + strategy: + matrix: + runs-on: + - aws-amd64 + - ubuntu-20.04 + use-self-hosted: + - ${{ github.repository_owner == 'emqx' }} + exclude: + - runs-on: ubuntu-20.04 + use-self-hosted: true + - runs-on: aws-amd64 + use-self-hosted: false steps: - uses: actions/checkout@v2 - name: set edition diff --git a/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl b/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl index f8569ef8a..1c1798918 100644 --- a/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl +++ b/apps/emqx_auth_mnesia/include/emqx_auth_mnesia.hrl @@ -13,6 +13,12 @@ -type(created_at():: integer()). -record(emqx_user, { + login, + password, + created_at + }). + +-type(emqx_user() :: #emqx_user{ login :: login(), password :: binary(), created_at :: created_at() diff --git a/apps/emqx_exproto/src/emqx_exproto_channel.erl b/apps/emqx_exproto/src/emqx_exproto_channel.erl index cf6229be3..4b354183e 100644 --- a/apps/emqx_exproto/src/emqx_exproto_channel.erl +++ b/apps/emqx_exproto/src/emqx_exproto_channel.erl @@ -159,7 +159,7 @@ init(ConnInfo = #{socktype := Socktype, Channel = #channel{gcli = #{channel => GRpcChann}, conninfo = NConnInfo1, clientinfo = ClientInfo, - conn_state = accepted, + conn_state = idle, timers = #{} }, case emqx_hooks:run_fold('client.connect', [NConnInfo], #{}) of diff --git a/apps/emqx_lua_hook/test/emqx_lua_hook_SUITE.erl b/apps/emqx_lua_hook/test/emqx_lua_hook_SUITE.erl index bb9247464..5b2ed75e7 100644 --- a/apps/emqx_lua_hook/test/emqx_lua_hook_SUITE.erl +++ b/apps/emqx_lua_hook/test/emqx_lua_hook_SUITE.erl @@ -75,7 +75,7 @@ case01(_Config) -> "\nend", ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), - Msg = #message{from = <<"myclient">>, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{username => <<"tester">>}}, + Msg = #message{id = emqx_guid:gen(), from = <<"myclient">>, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{username => <<"tester">>}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{payload = <<"hello">>}, Ret). @@ -90,7 +90,7 @@ case02(_Config) -> "\nend", ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), - Msg = #message{from = <<"myclient">>, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{username => <<"tester">>}}, + Msg = #message{id = emqx_guid:gen(), from = <<"myclient">>, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{username => <<"tester">>}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{headers = #{username => <<"tester">>, allow_publish => false}}, Ret). @@ -124,7 +124,7 @@ case04(_Config) -> "\nend", ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), - Msg = #message{from = <<"broker">>, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{username => <<"tester">>}}, + Msg = #message{id = emqx_guid:gen(), from = <<"broker">>, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{username => <<"tester">>}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{payload = <<"hello broker">>}, Ret). @@ -155,7 +155,7 @@ case12(_Config) -> "\nend", ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.delivered', [#{clientid => <<"myclient">>, username => <<"myuser">>}], Msg), ?assertEqual(Msg#message{payload = <<"hello broker">>}, Ret). @@ -470,8 +470,9 @@ case101(_Config) -> "\nend", ok = file:write_file(ScriptName2, Code2), ok = emqx_lua_hook:load_scripts(), - Ret = emqx_hooks:run_fold('message.publish',[], #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}), - ?assertEqual(#message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"hello">>, headers = #{}}, Ret), + ID = emqx_guid:gen(), + Ret = emqx_hooks:run_fold('message.publish',[], #message{id = ID, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}), + ?assertEqual(#message{id = ID, qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"hello">>, headers = #{}}, Ret), TopicTable = [{<<"a/b/c">>, [qos, 1]}, {<<"d/+/e">>, [{qos, 2}]}], Ret2 = emqx_hooks:run_fold('client.subscribe',[#{clientid => <<"myclient">>, username => <<"myuser">>}, #{}], TopicTable), @@ -488,7 +489,7 @@ case110(_Config) -> "\nend", ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{topic = <<"changed/topic">>, payload = <<"hello">>}, Ret). @@ -504,7 +505,7 @@ case111(_Config) -> ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), emqx_ctl:run_command(["luahook", "unload", ScriptName]), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg, Ret). @@ -523,7 +524,7 @@ case112(_Config) -> timer:sleep(100), emqx_ctl:run_command(["luahook", "load", "abc.lua"]), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{topic = <<"changed/topic">>, payload = <<"hello">>}, Ret). @@ -541,7 +542,7 @@ case113(_Config) -> file:delete(ScriptDisabled), emqx_ctl:run_command(["luahook", "disable", "abc.lua"]), % this command will rename "abc.lua" to "abc.lua.x" - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg, Ret), true = filelib:is_file(ScriptDisabled). @@ -558,7 +559,7 @@ case114(_Config) -> ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), emqx_ctl:run_command(["luahook", "enable", "abc.lua"]), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{topic = <<"changed/topic">>, payload = <<"hello">>}, Ret). @@ -578,7 +579,7 @@ case115(_Config) -> ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), emqx_ctl:run_command(["luahook", "reload", "abc.lua"]), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{topic = <<"changed/topic">>, payload = <<"hello">>}, Ret), @@ -636,7 +637,7 @@ case204(_Config) -> "\nend", ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg#message{payload = <<"123_Z">>}, Ret). @@ -651,7 +652,7 @@ case205(_Config) -> "\nend", ok = file:write_file(ScriptName, Code), ok = emqx_lua_hook:load_scripts(), - Msg = #message{qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, + Msg = #message{id = emqx_guid:gen(), qos = 2, flags = #{retain => true}, topic = <<"a/b/c">>, payload = <<"123">>, headers = #{}}, Ret = emqx_hooks:run_fold('message.publish',[], Msg), ?assertEqual(Msg, Ret). diff --git a/apps/emqx_management/src/emqx_mgmt_api_clients.erl b/apps/emqx_management/src/emqx_mgmt_api_clients.erl index e0e5af39d..de6e96e73 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_clients.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_clients.erl @@ -406,7 +406,7 @@ run_fuzzy_match(E = {_, _, Stats}, [{Key, '=<', Int}|Fuzzy]) -> %%-------------------------------------------------------------------- %% QueryString to Match Spec --spec qs2ms(list()) -> ets:match_spec(). +-spec qs2ms(list()) -> {ets:match_spec(), [{_Key, _Symbol, _Val}]}. qs2ms(Qs) -> {MatchHead, Conds, FuzzyStats} = qs2ms(Qs, 2, #{}, [], []), {[{{'$1', MatchHead, '_'}, Conds, ['$_']}], FuzzyStats}. diff --git a/apps/emqx_rule_engine/include/rule_engine.hrl b/apps/emqx_rule_engine/include/rule_engine.hrl index 858d9b9b6..04cfeb06f 100644 --- a/apps/emqx_rule_engine/include/rule_engine.hrl +++ b/apps/emqx_rule_engine/include/rule_engine.hrl @@ -61,28 +61,49 @@ }). -record(action_instance, - { id :: action_instance_id() - , name :: action_name() - , fallbacks :: list(#action_instance{}) - , args :: #{binary() => term()} %% the args got from API for initializing action_instance + { id + , name + , fallbacks + , args %% the args got from API for initializing action_instance }). +-type(action_instance() + :: #action_instance{ id :: action_instance_id() + , name :: action_name() + , fallbacks :: list(#action_instance{}) + , args :: #{binary() => term()} %% the args got from API for initializing action_instance + }). -record(rule, - { id :: rule_id() - , for :: list(topic()) - , rawsql :: binary() - , is_foreach :: boolean() - , fields :: list() - , doeach :: term() - , incase :: list() - , conditions :: tuple() - , on_action_failed :: continue | stop - , actions :: list(#action_instance{}) - , enabled :: boolean() - , created_at :: integer() %% epoch in millisecond precision - , description :: binary() - , state = normal :: atom() + { id + , for + , rawsql + , is_foreach + , fields + , doeach + , incase + , conditions + , on_action_failed + , actions + , enabled + , created_at %% epoch in millisecond precision + , description + , state = normal }). +-type(rule() :: #rule{ id :: rule_id() + , for :: list(topic()) + , rawsql :: binary() + , is_foreach :: boolean() + , fields :: list() + , doeach :: term() + , incase :: list() + , conditions :: tuple() + , on_action_failed :: continue | stop + , actions :: list(#action_instance{}) + , enabled :: boolean() + , created_at :: integer() %% epoch in millisecond precision + , description :: binary() + , state :: normal | atom() + }). -record(resource, { id :: resource_id() diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine.erl b/apps/emqx_rule_engine/src/emqx_rule_engine.erl index 872e314cb..d5c3ba92c 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_engine.erl @@ -64,7 +64,6 @@ -export([alarm_name_of_resource_down/2]). -endif. --type(rule() :: #rule{}). -type(action() :: #action{}). -type(resource() :: #resource{}). -type(resource_type() :: #resource_type{}). @@ -172,7 +171,6 @@ module_attributes(Module) -> %% APIs for rules and resources %%------------------------------------------------------------------------------ --dialyzer([{nowarn_function, [create_rule/1, rule_id/0]}]). -spec create_rule(map()) -> {ok, rule()} | {error, term()}. create_rule(Params = #{rawsql := Sql, actions := ActArgs}) -> case emqx_rule_sqlparser:parse_select(Sql) of @@ -307,7 +305,7 @@ do_check_and_update_resource(#{id := Id, type := Type, description := NewDescrip Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec), case test_resource(#{type => Type, config => NewConfig}) of ok -> - delete_resource(Id), + _ = delete_resource(Id), _ = ?CLUSTER_CALL(init_resource, [Module, Create, Id, Config]), emqx_rule_registry:add_resource(#resource{ id = Id, diff --git a/apps/emqx_rule_engine/src/emqx_rule_runtime.erl b/apps/emqx_rule_engine/src/emqx_rule_runtime.erl index 8b6d6f915..794c70dfd 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_runtime.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_runtime.erl @@ -52,7 +52,7 @@ apply_rules([], _Input) -> apply_rules([#rule{enabled = false}|More], Input) -> apply_rules(More, Input); apply_rules([Rule|More], Input) -> - apply_rule(Rule, Input), + _ = apply_rule(Rule, Input), apply_rules(More, Input). apply_rule(Rule = #rule{id = RuleId}, Input) -> diff --git a/apps/emqx_rule_engine/src/emqx_rule_sqlparser.erl b/apps/emqx_rule_engine/src/emqx_rule_sqlparser.erl index 82d3cfb64..5fbd9337c 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_sqlparser.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_sqlparser.erl @@ -48,10 +48,6 @@ -export_type([select/0]). -%% Dialyzer gives up on the generated code. -%% probably due to stack depth, or inlines. --dialyzer({nowarn_function, [parse_select/1]}). - %% Parse one select statement. -spec(parse_select(string() | binary()) -> {ok, select()} | {parse_error, term()} | {lex_error, term()}). @@ -105,4 +101,3 @@ select_from(#select{from = From}) -> -spec(select_where(select()) -> tuple()). select_where(#select{where = Where}) -> Where. - diff --git a/src/emqx.erl b/src/emqx.erl index 29c5d7020..469a4672b 100644 --- a/src/emqx.erl +++ b/src/emqx.erl @@ -236,7 +236,7 @@ shutdown(Reason) -> reboot() -> case is_application_running(emqx_dashboard) of true -> - application:stop(emqx_dashboard), %% dashboard must be started after mnesia + _ = application:stop(emqx_dashboard), %% dashboard must be started after mnesia lists:foreach(fun application:start/1 , default_started_applications()), application:start(emqx_dashboard); diff --git a/src/emqx_calendar.erl b/src/emqx_calendar.erl index 0c094df81..71fff3182 100644 --- a/src/emqx_calendar.erl +++ b/src/emqx_calendar.erl @@ -317,7 +317,6 @@ trans_x_second(FromUnit, ToUnit, Time) -> end, padding(XSecond, Len). -do_trans_x_second(second, second, Time) -> Time div 60; do_trans_x_second(second, _, _Time) -> 0; do_trans_x_second(millisecond, millisecond, Time) -> Time rem 1000; @@ -433,4 +432,3 @@ dm(9) -> 243; dm(10) -> 273; dm(11) -> 304; dm(12) -> 334. - diff --git a/src/emqx_channel.erl b/src/emqx_channel.erl index 49f515236..f3fb6194b 100644 --- a/src/emqx_channel.erl +++ b/src/emqx_channel.erl @@ -1742,8 +1742,7 @@ disconnect_reason(?RC_SUCCESS) -> normal; disconnect_reason(ReasonCode) -> emqx_reason_codes:name(ReasonCode). reason_code(takeovered) -> ?RC_SESSION_TAKEN_OVER; -reason_code(discarded) -> ?RC_SESSION_TAKEN_OVER; -reason_code(_) -> ?RC_NORMAL_DISCONNECTION. +reason_code(discarded) -> ?RC_SESSION_TAKEN_OVER. %%-------------------------------------------------------------------- %% Helper functions diff --git a/src/emqx_message.erl b/src/emqx_message.erl index 5a0ab3bbd..40779c86b 100644 --- a/src/emqx_message.erl +++ b/src/emqx_message.erl @@ -343,8 +343,6 @@ format(#message{id = Id, [printable_msg_id(Id), QoS, Topic, From, format(flags, Flags), format(headers, Headers)]). -printable_msg_id(undefined) -> - <<>>; printable_msg_id(Id) -> emqx_guid:to_hexstr(Id). @@ -352,4 +350,3 @@ format(flags, Flags) -> io_lib:format("~p", [[Flag || {Flag, true} <- maps:to_list(Flags)]]); format(headers, Headers) -> io_lib:format("~p", [Headers]). - diff --git a/src/emqx_shared_sub.erl b/src/emqx_shared_sub.erl index 3f67c225d..337d63d15 100644 --- a/src/emqx_shared_sub.erl +++ b/src/emqx_shared_sub.erl @@ -251,7 +251,7 @@ maybe_nack_dropped(Msg) -> %% For retry Ref we can't reject a message if inflight is full, so we mark it as %% acknowledged and put it into mqueue - {_Sender, {retry, _Group, _Ref}} -> maybe_ack(Msg), store; + {_Sender, {retry, _Group, _Ref}} -> _ = maybe_ack(Msg), store; %% This clause is for backward compatibility Ack ->