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/2,
deactivate/3,
ensure_deactivated/1,
ensure_deactivated/2,
ensure_deactivated/3,
delete_all_deactivated_alarms/0,
get_alarms/0,
@ -114,6 +116,28 @@ activate(Name, Details) ->
activate(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}.
deactivate(Name) ->
deactivate(Name, no_details, <<"">>).
@ -121,17 +145,6 @@ deactivate(Name) ->
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) ->
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};
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};
handle_event({set_alarm, {?LC_ALARM_ID_RUNQ, Info}}, State) ->
#{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),
{ok, State};
handle_event({clear_alarm, ?LC_ALARM_ID_RUNQ}, State) ->
_ = emqx_alarm:deactivate(runq_overload),
emqx_alarm:ensure_deactivated(runq_overload),
{ok, State};
handle_event(_, State) ->
{ok, State}.

View File

@ -115,7 +115,7 @@ do_cancel_alarm_congestion(Socket, Transport, Channel, Reason) ->
ok = remove_alarm_sent_at(Reason),
AlarmDetails = tcp_congestion_alarm_details(Socket, Transport, Channel),
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.
is_tcp_congested(Socket, Transport) ->

View File

@ -174,9 +174,9 @@ start_cpu_check_timer() ->
Interval = emqx:get_config([sysmon, os, cpu_check_interval]),
case erlang:system_info(system_architecture) of
"x86_64-pc-linux-musl" -> ok;
_ -> _ = emqx_misc:start_timer(Interval, cpu_check)
end,
ok.
_ -> start_timer(Interval, cpu_check)
end.
start_mem_check_timer() ->
Interval = emqx:get_config([sysmon, os, mem_check_interval]),
IsSupported =
@ -189,10 +189,13 @@ start_mem_check_timer() ->
end,
case is_integer(Interval) andalso IsSupported of
true ->
_ = emqx_misc:start_timer(Interval, mem_check);
start_timer(Interval, mem_check);
false ->
ok
end,
end.
start_timer(Interval, Msg) ->
_ = emqx_misc:start_timer(Interval, Msg),
ok.
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);
handle_partition_event({partition, {healed, 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}) ->
case lists:member(Key, Events) of

View File

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

View File

@ -77,7 +77,7 @@ health_check(Name) ->
{Pid, begin_health_check} ->
case emqx_resource:health_check(Name) of
ok ->
emqx_alarm:deactivate(Name);
emqx_alarm:ensure_deactivated(Name);
{error, _} ->
emqx_alarm:activate(
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])),
?OK(emqx_alarm:activate(license_expiry, #{expiry_at => Date}));
false ->
?OK(emqx_alarm:deactivate(license_expiry))
?OK(emqx_alarm:ensure_deactivated(license_expiry))
end.
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))
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) ->
ok.