Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: packets_connack_sent is not incremented if the ack_flag is non-zero #11520

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/emqx/src/emqx_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,7 @@ disconnect_and_shutdown(Reason, Reply, Channel) ->
NChannel = ensure_disconnected(Reason, Channel),
shutdown(Reason, Reply, NChannel).

-compile({inline, [sp/1, flag/1]}).
sp(true) -> 1;
sp(false) -> 0.

Expand Down
10 changes: 0 additions & 10 deletions apps/emqx/src/emqx_cm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
insert_channel_info/3
]).

-export([connection_closed/1]).

-export([
get_chan_info/1,
get_chan_info/2,
Expand Down Expand Up @@ -192,14 +190,6 @@ do_unregister_channel({_ClientId, ChanPid} = Chan) ->
ok = emqx_hooks:run('channel.unregistered', [ChanPid]),
true.

-spec connection_closed(emqx_types:clientid()) -> true.
connection_closed(ClientId) ->
connection_closed(ClientId, self()).

-spec connection_closed(emqx_types:clientid(), chan_pid()) -> true.
connection_closed(ClientId, ChanPid) ->
ets:delete_object(?CHAN_CONN_TAB, {ClientId, ChanPid}).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember at least the function was left unchanged previously by @savonarola
but I cannot recall the discussion about why delete_object is still here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a clarification in 4.4
https://github.com/emqx/emqx/blob/main-v4.4/src/emqx_cm.erl#L173

%% We can't clean CHAN_CONN_TAB because records for dead connections
%% are required for `get_chann_conn_mod/1` function, and `get_chann_conn_mod/1`
%% is used for takeover.

I left the fun empty to avoid attempts to "fix" the things while we have the current mechanism of keeping offline sessions.


%% @doc Get info of a channel.
-spec get_chan_info(emqx_types:clientid()) -> maybe(emqx_types:infos()).
get_chan_info(ClientId) ->
Expand Down
1 change: 0 additions & 1 deletion apps/emqx/src/emqx_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ handle_msg(
handle_msg({event, disconnected}, State = #state{channel = Channel}) ->
ClientId = emqx_channel:info(clientid, Channel),
emqx_cm:set_chan_info(ClientId, info(State)),
emqx_cm:connection_closed(ClientId),
{ok, State};
handle_msg({event, _Other}, State = #state{channel = Channel}) ->
ClientId = emqx_channel:info(clientid, Channel),
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx/src/emqx_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ inc_sent(Packet) ->
inc('packets.sent'),
do_inc_sent(Packet).

do_inc_sent(?CONNACK_PACKET(ReasonCode)) ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-define(CONNACK_PACKET(ReasonCode), #mqtt_packet{
    header = #mqtt_packet_header{type = ?CONNACK},
    variable = #mqtt_packet_connack{
        ack_flags = 0,
        reason_code = ReasonCode
    }
}).

?CONNACK_PACKET/1 lock the ack_flags to 0.

do_inc_sent(?CONNACK_PACKET(ReasonCode, _SessPresent)) ->
(ReasonCode == ?RC_SUCCESS) orelse inc('packets.connack.error'),
((ReasonCode == ?RC_NOT_AUTHORIZED) orelse
(ReasonCode == ?CONNACK_AUTH)) andalso
Expand Down
1 change: 0 additions & 1 deletion apps/emqx/src/emqx_ws_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ handle_info({event, connected}, State = #state{channel = Channel}) ->
handle_info({event, disconnected}, State = #state{channel = Channel}) ->
ClientId = emqx_channel:info(clientid, Channel),
emqx_cm:set_chan_info(ClientId, info(State)),
emqx_cm:connection_closed(ClientId),
return(State);
handle_info({event, _Other}, State = #state{channel = Channel}) ->
ClientId = emqx_channel:info(clientid, Channel),
Expand Down
1 change: 0 additions & 1 deletion apps/emqx/test/emqx_connection_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ t_handle_msg_event(_) ->
ok = meck:expect(emqx_cm, register_channel, fun(_, _, _) -> ok end),
ok = meck:expect(emqx_cm, insert_channel_info, fun(_, _, _) -> ok end),
ok = meck:expect(emqx_cm, set_chan_info, fun(_, _) -> ok end),
ok = meck:expect(emqx_cm, connection_closed, fun(_) -> ok end),
?assertEqual(ok, handle_msg({event, connected}, st())),
?assertMatch({ok, _St}, handle_msg({event, disconnected}, st())),
?assertMatch({ok, _St}, handle_msg({event, undefined}, st())).
Expand Down
15 changes: 13 additions & 2 deletions apps/emqx/test/emqx_metrics_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ t_inc_sent(_) ->
with_metrics_server(
fun() ->
ok = emqx_metrics:inc_sent(?CONNACK_PACKET(0)),
ok = emqx_metrics:inc_sent(?CONNACK_PACKET(0, 1)),
ok = emqx_metrics:inc_sent(
?CONNACK_PACKET(0, 1, #{
'Maximum-Packet-Size' => 1048576,
'Retain-Available' => 1,
'Shared-Subscription-Available' => 1,
'Subscription-Identifier-Available' => 1,
'Topic-Alias-Maximum' => 65535,
'Wildcard-Subscription-Available' => 1
})
),
ok = emqx_metrics:inc_sent(?PUBLISH_PACKET(0, 0)),
ok = emqx_metrics:inc_sent(?PUBLISH_PACKET(1, 0)),
ok = emqx_metrics:inc_sent(?PUBLISH_PACKET(2, 0)),
Expand All @@ -134,8 +145,8 @@ t_inc_sent(_) ->
ok = emqx_metrics:inc_sent(?PACKET(?PINGRESP)),
ok = emqx_metrics:inc_sent(?PACKET(?DISCONNECT)),
ok = emqx_metrics:inc_sent(?PACKET(?AUTH)),
?assertEqual(13, emqx_metrics:val('packets.sent')),
?assertEqual(1, emqx_metrics:val('packets.connack.sent')),
?assertEqual(15, emqx_metrics:val('packets.sent')),
?assertEqual(3, emqx_metrics:val('packets.connack.sent')),
?assertEqual(3, emqx_metrics:val('messages.sent')),
?assertEqual(1, emqx_metrics:val('messages.qos0.sent')),
?assertEqual(1, emqx_metrics:val('messages.qos1.sent')),
Expand Down
1 change: 0 additions & 1 deletion apps/emqx/test/emqx_ws_connection_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ t_handle_info_close(_) ->
t_handle_info_event(_) ->
ok = meck:expect(emqx_cm, register_channel, fun(_, _, _) -> ok end),
ok = meck:expect(emqx_cm, insert_channel_info, fun(_, _, _) -> ok end),
ok = meck:expect(emqx_cm, connection_closed, fun(_) -> true end),
{ok, _} = ?ws_conn:handle_info({event, connected}, st()),
{ok, _} = ?ws_conn:handle_info({event, disconnected}, st()),
{ok, _} = ?ws_conn:handle_info({event, updated}, st()).
Expand Down
1 change: 1 addition & 0 deletions changes/ce/fix-11520.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed issue where packets_connack_sent metric was not incremented on CONNACK packets sent with non-zero ack_flag
Loading