refactor: call emqx_alarm:ensure_deactivated everywhere

This commit is contained in:
Zaiming (Stone) Shi 2022-05-13 13:04:56 +02:00
parent 1ea0639321
commit c355c40ea8
9 changed files with 40 additions and 24 deletions

View File

@ -35,6 +35,8 @@
deactivate/1, deactivate/1,
deactivate/2, deactivate/2,
deactivate/3, deactivate/3,
ensure_deactivated/1,
ensure_deactivated/2,
ensure_deactivated/3, ensure_deactivated/3,
delete_all_deactivated_alarms/0, delete_all_deactivated_alarms/0,
get_alarms/0, get_alarms/0,
@ -114,6 +116,28 @@ activate(Name, Details) ->
activate(Name, Details, Message) -> activate(Name, Details, Message) ->
gen_server:call(?MODULE, {activate_alarm, Name, Details, Message}). gen_server:call(?MODULE, {activate_alarm, Name, Details, Message}).
-spec ensure_deactivated(binary() | atom()) -> ok.
ensure_deactivated(Name) ->
ensure_deactivated(Name, no_details).
-spec ensure_deactivated(binary() | atom(), atom() | map()) -> ok.
ensure_deactivated(Name, Data) ->
ensure_deactivated(Name, Data, <<>>).
-spec ensure_deactivated(binary() | atom(), atom() | map(), iodata()) -> ok.
ensure_deactivated(Name, Data, Message) ->
%% this duplicates the dirty read in handle_call,
%% intention is to avoid making gen_server calls when there is no alarm
case mnesia:dirty_read(?ACTIVATED_ALARM, Name) of
[] ->
ok;
_ ->
case deactivate(Name, Data, Message) of
{error, not_found} -> ok;
Other -> Other
end
end.
-spec deactivate(binary() | atom()) -> ok | {error, not_found}. -spec deactivate(binary() | atom()) -> ok | {error, not_found}.
deactivate(Name) -> deactivate(Name) ->
deactivate(Name, no_details, <<"">>). deactivate(Name, no_details, <<"">>).
@ -121,17 +145,6 @@ deactivate(Name) ->
deactivate(Name, Details) -> deactivate(Name, Details) ->
deactivate(Name, Details, <<"">>). deactivate(Name, Details, <<"">>).
ensure_deactivated(Name, Details, Message) ->
case mnesia:dirty_read(?ACTIVATED_ALARM, Name) of
[] ->
ok;
_ ->
case deactivate(Name, Details, Message) of
{error, not_found} -> ok;
Other -> Other
end
end.
deactivate(Name, Details, Message) -> deactivate(Name, Details, Message) ->
gen_server:call(?MODULE, {deactivate_alarm, Name, Details, Message}). gen_server:call(?MODULE, {deactivate_alarm, Name, Details, Message}).

View File

@ -69,7 +69,7 @@ handle_event({set_alarm, {process_memory_high_watermark, Pid}}, State) ->
), ),
{ok, State}; {ok, State};
handle_event({clear_alarm, process_memory_high_watermark}, State) -> handle_event({clear_alarm, process_memory_high_watermark}, State) ->
_ = emqx_alarm:deactivate(high_process_memory_usage), emqx_alarm:ensure_deactivated(high_process_memory_usage),
{ok, State}; {ok, State};
handle_event({set_alarm, {?LC_ALARM_ID_RUNQ, Info}}, State) -> handle_event({set_alarm, {?LC_ALARM_ID_RUNQ, Info}}, State) ->
#{node := Node, runq_length := Len} = Info, #{node := Node, runq_length := Len} = Info,
@ -77,7 +77,7 @@ handle_event({set_alarm, {?LC_ALARM_ID_RUNQ, Info}}, State) ->
emqx_alarm:activate(runq_overload, Info, Message), emqx_alarm:activate(runq_overload, Info, Message),
{ok, State}; {ok, State};
handle_event({clear_alarm, ?LC_ALARM_ID_RUNQ}, State) -> handle_event({clear_alarm, ?LC_ALARM_ID_RUNQ}, State) ->
_ = emqx_alarm:deactivate(runq_overload), emqx_alarm:ensure_deactivated(runq_overload),
{ok, State}; {ok, State};
handle_event(_, State) -> handle_event(_, State) ->
{ok, State}. {ok, State}.

View File

@ -115,7 +115,7 @@ do_cancel_alarm_congestion(Socket, Transport, Channel, Reason) ->
ok = remove_alarm_sent_at(Reason), ok = remove_alarm_sent_at(Reason),
AlarmDetails = tcp_congestion_alarm_details(Socket, Transport, Channel), AlarmDetails = tcp_congestion_alarm_details(Socket, Transport, Channel),
Message = io_lib:format("connection congested: ~0p", [AlarmDetails]), Message = io_lib:format("connection congested: ~0p", [AlarmDetails]),
emqx_alarm:deactivate(?ALARM_CONN_CONGEST(Channel, Reason), AlarmDetails, Message), emqx_alarm:ensure_deactivated(?ALARM_CONN_CONGEST(Channel, Reason), AlarmDetails, Message),
ok. ok.
is_tcp_congested(Socket, Transport) -> is_tcp_congested(Socket, Transport) ->

View File

@ -174,9 +174,9 @@ start_cpu_check_timer() ->
Interval = emqx:get_config([sysmon, os, cpu_check_interval]), Interval = emqx:get_config([sysmon, os, cpu_check_interval]),
case erlang:system_info(system_architecture) of case erlang:system_info(system_architecture) of
"x86_64-pc-linux-musl" -> ok; "x86_64-pc-linux-musl" -> ok;
_ -> _ = emqx_misc:start_timer(Interval, cpu_check) _ -> start_timer(Interval, cpu_check)
end, end.
ok.
start_mem_check_timer() -> start_mem_check_timer() ->
Interval = emqx:get_config([sysmon, os, mem_check_interval]), Interval = emqx:get_config([sysmon, os, mem_check_interval]),
IsSupported = IsSupported =
@ -189,10 +189,13 @@ start_mem_check_timer() ->
end, end,
case is_integer(Interval) andalso IsSupported of case is_integer(Interval) andalso IsSupported of
true -> true ->
_ = emqx_misc:start_timer(Interval, mem_check); start_timer(Interval, mem_check);
false -> false ->
ok ok
end, end.
start_timer(Interval, Msg) ->
_ = emqx_misc:start_timer(Interval, Msg),
ok. ok.
update_mem_alarm_stauts(HWM) when HWM > 1.0 orelse HWM < 0.0 -> update_mem_alarm_stauts(HWM) when HWM > 1.0 orelse HWM < 0.0 ->

View File

@ -195,7 +195,7 @@ handle_partition_event({partition, {occurred, Node}}) ->
emqx_alarm:activate(partition, #{occurred => Node}, Message); emqx_alarm:activate(partition, #{occurred => Node}, Message);
handle_partition_event({partition, {healed, Node}}) -> handle_partition_event({partition, {healed, Node}}) ->
Message = io_lib:format("Partition healed at node ~ts", [Node]), Message = io_lib:format("Partition healed at node ~ts", [Node]),
emqx_alarm:deactivate(partition, no_details, Message). emqx_alarm:ensure_deactivated(partition, no_details, Message).
suppress(Key, SuccFun, State = #{events := Events}) -> suppress(Key, SuccFun, State = #{events := Events}) ->
case lists:member(Key, Events) of case lists:member(Key, Events) of

View File

@ -77,7 +77,7 @@ handle_info({timeout, _Timer, check}, State) ->
Percent when Percent < ProcLowWatermark -> Percent when Percent < ProcLowWatermark ->
Usage = io_lib:format("~p%", [Percent * 100]), Usage = io_lib:format("~p%", [Percent * 100]),
Message = [Usage, " process usage"], Message = [Usage, " process usage"],
emqx_alarm:deactivate( emqx_alarm:ensure_deactivated(
too_many_processes, too_many_processes,
#{ #{
usage => Usage, usage => Usage,

View File

@ -77,7 +77,7 @@ health_check(Name) ->
{Pid, begin_health_check} -> {Pid, begin_health_check} ->
case emqx_resource:health_check(Name) of case emqx_resource:health_check(Name) of
ok -> ok ->
emqx_alarm:deactivate(Name); emqx_alarm:ensure_deactivated(Name);
{error, _} -> {error, _} ->
emqx_alarm:activate( emqx_alarm:activate(
Name, Name,

View File

@ -197,7 +197,7 @@ expiry_early_alarm(License) ->
Date = iolist_to_binary(io_lib:format("~B~2..0B~2..0B", [Y, M, D])), Date = iolist_to_binary(io_lib:format("~B~2..0B~2..0B", [Y, M, D])),
?OK(emqx_alarm:activate(license_expiry, #{expiry_at => Date})); ?OK(emqx_alarm:activate(license_expiry, #{expiry_at => Date}));
false -> false ->
?OK(emqx_alarm:deactivate(license_expiry)) ?OK(emqx_alarm:ensure_deactivated(license_expiry))
end. end.
print_warnings(Warnings) -> print_warnings(Warnings) ->

View File

@ -103,7 +103,7 @@ connection_quota_early_alarm({ok, #{max_connections := Max}}) when is_integer(Ma
]), ]),
?OK(emqx_alarm:activate(license_quota, #{high_watermark => HighPercent}, Message)) ?OK(emqx_alarm:activate(license_quota, #{high_watermark => HighPercent}, Message))
end, end,
Count < Max * Low andalso ?OK(emqx_alarm:deactivate(license_quota)); Count < Max * Low andalso ?OK(emqx_alarm:ensure_deactivated(license_quota));
connection_quota_early_alarm(_Limits) -> connection_quota_early_alarm(_Limits) ->
ok. ok.