feat(session): store iterator ids in session record
This commit is contained in:
parent
95a51658e1
commit
9c6dd30f44
|
@ -49,7 +49,11 @@
|
||||||
%% Awaiting PUBREL Timeout (Unit: millisecond)
|
%% Awaiting PUBREL Timeout (Unit: millisecond)
|
||||||
await_rel_timeout :: timeout(),
|
await_rel_timeout :: timeout(),
|
||||||
%% Created at
|
%% Created at
|
||||||
created_at :: pos_integer()
|
created_at :: pos_integer(),
|
||||||
|
%% Topic filter to iterator ID mapping.
|
||||||
|
%% Note: we shouldn't serialize this when persisting sessions, as this information
|
||||||
|
%% also exists in the `?ITERATOR_REF_TAB' table.
|
||||||
|
iterators = #{} :: #{emqx_topic:topic() => emqx_ds:iterator_id()}
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
|
@ -97,6 +97,12 @@ get_all_iterator_ids(Node) ->
|
||||||
emqx_ds_storage_layer:foldl_iterator_prefix(?DS_SHARD, <<>>, Fn, [])
|
emqx_ds_storage_layer:foldl_iterator_prefix(?DS_SHARD, <<>>, Fn, [])
|
||||||
end).
|
end).
|
||||||
|
|
||||||
|
get_session_iterators(Node, ClientId) ->
|
||||||
|
erpc:call(Node, fun() ->
|
||||||
|
[ConnPid] = emqx_cm:lookup_channels(ClientId),
|
||||||
|
emqx_connection:info({channel, {session, iterators}}, sys:get_state(ConnPid))
|
||||||
|
end).
|
||||||
|
|
||||||
wait_nodeup(Node) ->
|
wait_nodeup(Node) ->
|
||||||
?retry(
|
?retry(
|
||||||
_Sleep0 = 500,
|
_Sleep0 = 500,
|
||||||
|
@ -191,14 +197,18 @@ t_session_subscription_idempotency(Config) ->
|
||||||
{ok, _} = emqtt:connect(Client1),
|
{ok, _} = emqtt:connect(Client1),
|
||||||
ct:pal("subscribing 2"),
|
ct:pal("subscribing 2"),
|
||||||
{ok, _, [2]} = emqtt:subscribe(Client1, SubTopicFilter, qos2),
|
{ok, _, [2]} = emqtt:subscribe(Client1, SubTopicFilter, qos2),
|
||||||
|
SessionIterators = get_session_iterators(Node1, ClientId),
|
||||||
|
|
||||||
ok = emqtt:stop(Client1),
|
ok = emqtt:stop(Client1),
|
||||||
|
|
||||||
ok
|
#{session_iterators => SessionIterators}
|
||||||
end,
|
end,
|
||||||
fun(Trace) ->
|
fun(Res, Trace) ->
|
||||||
ct:pal("trace:\n ~p", [Trace]),
|
ct:pal("trace:\n ~p", [Trace]),
|
||||||
|
#{session_iterators := SessionIterators} = Res,
|
||||||
%% Exactly one iterator should have been opened.
|
%% Exactly one iterator should have been opened.
|
||||||
|
?assertEqual(1, map_size(SessionIterators), #{iterators => SessionIterators}),
|
||||||
|
?assertMatch(#{SubTopicFilter := _}, SessionIterators),
|
||||||
?assertMatch({ok, [_]}, get_all_iterator_ids(Node1)),
|
?assertMatch({ok, [_]}, get_all_iterator_ids(Node1)),
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
{_IsNew = false, ClientId},
|
{_IsNew = false, ClientId},
|
||||||
|
|
|
@ -269,7 +269,9 @@ info(awaiting_rel_max, #session{max_awaiting_rel = Max}) ->
|
||||||
info(await_rel_timeout, #session{await_rel_timeout = Timeout}) ->
|
info(await_rel_timeout, #session{await_rel_timeout = Timeout}) ->
|
||||||
Timeout;
|
Timeout;
|
||||||
info(created_at, #session{created_at = CreatedAt}) ->
|
info(created_at, #session{created_at = CreatedAt}) ->
|
||||||
CreatedAt.
|
CreatedAt;
|
||||||
|
info(iterators, #session{iterators = Iterators}) ->
|
||||||
|
Iterators.
|
||||||
|
|
||||||
%% @doc Get stats of the session.
|
%% @doc Get stats of the session.
|
||||||
-spec stats(session()) -> emqx_types:stats().
|
-spec stats(session()) -> emqx_types:stats().
|
||||||
|
@ -318,8 +320,13 @@ is_subscriptions_full(#session{
|
||||||
-spec add_persistent_subscription(emqx_types:topic(), emqx_types:clientid(), session()) ->
|
-spec add_persistent_subscription(emqx_types:topic(), emqx_types:clientid(), session()) ->
|
||||||
session().
|
session().
|
||||||
add_persistent_subscription(TopicFilterBin, ClientId, Session) ->
|
add_persistent_subscription(TopicFilterBin, ClientId, Session) ->
|
||||||
_ = emqx_persistent_session_ds:add_subscription(TopicFilterBin, ClientId),
|
case emqx_persistent_session_ds:add_subscription(TopicFilterBin, ClientId) of
|
||||||
Session.
|
{ok, IteratorId, _IsNew} ->
|
||||||
|
Iterators = Session#session.iterators,
|
||||||
|
Session#session{iterators = Iterators#{TopicFilterBin => IteratorId}};
|
||||||
|
_ ->
|
||||||
|
Session
|
||||||
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Client -> Broker: UNSUBSCRIBE
|
%% Client -> Broker: UNSUBSCRIBE
|
||||||
|
|
Loading…
Reference in New Issue