|
@@ -14,7 +14,9 @@
|
|
|
%% limitations under the License.
|
|
%% limitations under the License.
|
|
|
%%--------------------------------------------------------------------
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-%% @doc MQTT Message Router
|
|
|
|
|
|
|
+%% @doc
|
|
|
|
|
+%% The Message Router on Local Node.
|
|
|
|
|
+%% @end
|
|
|
-module(emqttd_router).
|
|
-module(emqttd_router).
|
|
|
|
|
|
|
|
-behaviour(gen_server2).
|
|
-behaviour(gen_server2).
|
|
@@ -43,22 +45,26 @@
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
terminate/2, code_change/3]).
|
|
terminate/2, code_change/3]).
|
|
|
|
|
|
|
|
|
|
+-ifdef(TEST).
|
|
|
|
|
+-compile(export_all).
|
|
|
|
|
+-endif.
|
|
|
|
|
+
|
|
|
-record(aging, {topics, time, tref}).
|
|
-record(aging, {topics, time, tref}).
|
|
|
|
|
|
|
|
--record(state, {pool, id, statsfun, aging :: #aging{}}).
|
|
|
|
|
|
|
+-record(state, {pool, id, aging :: #aging{}, statsfun}).
|
|
|
|
|
|
|
|
-%% @doc Start a local router.
|
|
|
|
|
|
|
+%% @doc Start a router.
|
|
|
-spec start_link(atom(), pos_integer(), fun((atom()) -> ok), list()) -> {ok, pid()} | {error, any()}.
|
|
-spec start_link(atom(), pos_integer(), fun((atom()) -> ok), list()) -> {ok, pid()} | {error, any()}.
|
|
|
start_link(Pool, Id, StatsFun, Env) ->
|
|
start_link(Pool, Id, StatsFun, Env) ->
|
|
|
gen_server2:start_link({local, ?PROC_NAME(?MODULE, Id)},
|
|
gen_server2:start_link({local, ?PROC_NAME(?MODULE, Id)},
|
|
|
?MODULE, [Pool, Id, StatsFun, Env], []).
|
|
?MODULE, [Pool, Id, StatsFun, Env], []).
|
|
|
|
|
|
|
|
-%% @doc Route Message on the local node.
|
|
|
|
|
|
|
+%% @doc Route Message on this node.
|
|
|
-spec route(emqttd_topic:topic(), mqtt_message()) -> any().
|
|
-spec route(emqttd_topic:topic(), mqtt_message()) -> any().
|
|
|
route(Queue = <<"$Q/", _Q>>, Msg) ->
|
|
route(Queue = <<"$Q/", _Q>>, Msg) ->
|
|
|
case lookup_routes(Queue) of
|
|
case lookup_routes(Queue) of
|
|
|
[] ->
|
|
[] ->
|
|
|
- emqttd_metrics:inc('messages/dropped');
|
|
|
|
|
|
|
+ dropped(Queue);
|
|
|
[SubPid] ->
|
|
[SubPid] ->
|
|
|
SubPid ! {dispatch, Queue, Msg};
|
|
SubPid ! {dispatch, Queue, Msg};
|
|
|
Routes ->
|
|
Routes ->
|
|
@@ -70,8 +76,8 @@ route(Queue = <<"$Q/", _Q>>, Msg) ->
|
|
|
route(Topic, Msg) ->
|
|
route(Topic, Msg) ->
|
|
|
case lookup_routes(Topic) of
|
|
case lookup_routes(Topic) of
|
|
|
[] ->
|
|
[] ->
|
|
|
- emqttd_metrics:inc('messages/dropped');
|
|
|
|
|
- [SubPid] -> %% optimize
|
|
|
|
|
|
|
+ dropped(Topic);
|
|
|
|
|
+ [SubPid] ->
|
|
|
SubPid ! {dispatch, Topic, Msg};
|
|
SubPid ! {dispatch, Topic, Msg};
|
|
|
Routes ->
|
|
Routes ->
|
|
|
lists:foreach(fun(SubPid) ->
|
|
lists:foreach(fun(SubPid) ->
|
|
@@ -79,9 +85,16 @@ route(Topic, Msg) ->
|
|
|
end, Routes)
|
|
end, Routes)
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
|
|
+%% @private
|
|
|
|
|
+%% @doc Ingore $SYS Messages.
|
|
|
|
|
+dropped(<<"$SYS/", _/binary>>) ->
|
|
|
|
|
+ ok;
|
|
|
|
|
+dropped(_Topic) ->
|
|
|
|
|
+ emqttd_metrics:inc('messages/dropped').
|
|
|
|
|
+
|
|
|
%% @doc Has Route?
|
|
%% @doc Has Route?
|
|
|
-spec has_route(emqttd_topic:topic()) -> boolean().
|
|
-spec has_route(emqttd_topic:topic()) -> boolean().
|
|
|
-has_route(Topic) ->
|
|
|
|
|
|
|
+has_route(Topic) when is_binary(Topic) ->
|
|
|
ets:member(route, Topic).
|
|
ets:member(route, Topic).
|
|
|
|
|
|
|
|
%% @doc Lookup Routes
|
|
%% @doc Lookup Routes
|
|
@@ -94,12 +107,12 @@ lookup_routes(Topic) when is_binary(Topic) ->
|
|
|
[]
|
|
[]
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
-%% @doc Add Route.
|
|
|
|
|
|
|
+%% @doc Add Route
|
|
|
-spec add_route(emqttd_topic:topic(), pid()) -> ok.
|
|
-spec add_route(emqttd_topic:topic(), pid()) -> ok.
|
|
|
add_route(Topic, Pid) when is_pid(Pid) ->
|
|
add_route(Topic, Pid) when is_pid(Pid) ->
|
|
|
call(pick(Topic), {add_route, Topic, Pid}).
|
|
call(pick(Topic), {add_route, Topic, Pid}).
|
|
|
|
|
|
|
|
-%% @doc Add Routes.
|
|
|
|
|
|
|
+%% @doc Add Routes
|
|
|
-spec add_routes(list(emqttd_topic:topic()), pid()) -> ok.
|
|
-spec add_routes(list(emqttd_topic:topic()), pid()) -> ok.
|
|
|
add_routes([], _Pid) ->
|
|
add_routes([], _Pid) ->
|
|
|
ok;
|
|
ok;
|
|
@@ -111,12 +124,12 @@ add_routes(Topics, Pid) ->
|
|
|
call(Router, {add_routes, Slice, Pid})
|
|
call(Router, {add_routes, Slice, Pid})
|
|
|
end, slice(Topics)).
|
|
end, slice(Topics)).
|
|
|
|
|
|
|
|
-%% @doc Delete Route.
|
|
|
|
|
|
|
+%% @doc Delete Route
|
|
|
-spec delete_route(emqttd_topic:topic(), pid()) -> ok.
|
|
-spec delete_route(emqttd_topic:topic(), pid()) -> ok.
|
|
|
delete_route(Topic, Pid) ->
|
|
delete_route(Topic, Pid) ->
|
|
|
cast(pick(Topic), {delete_route, Topic, Pid}).
|
|
cast(pick(Topic), {delete_route, Topic, Pid}).
|
|
|
|
|
|
|
|
-%% @doc Delete Routes.
|
|
|
|
|
|
|
+%% @doc Delete Routes
|
|
|
-spec delete_routes(list(emqttd_topic:topic()), pid()) -> ok.
|
|
-spec delete_routes(list(emqttd_topic:topic()), pid()) -> ok.
|
|
|
delete_routes([Topic], Pid) ->
|
|
delete_routes([Topic], Pid) ->
|
|
|
delete_route(Topic, Pid);
|
|
delete_route(Topic, Pid);
|
|
@@ -136,8 +149,11 @@ slice(Topics) ->
|
|
|
pick(Topic) ->
|
|
pick(Topic) ->
|
|
|
gproc_pool:pick_worker(router, Topic).
|
|
gproc_pool:pick_worker(router, Topic).
|
|
|
|
|
|
|
|
|
|
+%% @doc For unit test.
|
|
|
stop(Id) when is_integer(Id) ->
|
|
stop(Id) when is_integer(Id) ->
|
|
|
- gen_server2:call(?PROC_NAME(?MODULE, Id), stop).
|
|
|
|
|
|
|
+ gen_server2:call(?PROC_NAME(?MODULE, Id), stop);
|
|
|
|
|
+stop(Pid) when is_pid(Pid) ->
|
|
|
|
|
+ gen_server2:call(Pid, stop).
|
|
|
|
|
|
|
|
call(Router, Request) ->
|
|
call(Router, Request) ->
|
|
|
gen_server2:call(Router, Request, infinity).
|
|
gen_server2:call(Router, Request, infinity).
|
|
@@ -147,21 +163,22 @@ cast(Router, Msg) ->
|
|
|
|
|
|
|
|
init([Pool, Id, StatsFun, Opts]) ->
|
|
init([Pool, Id, StatsFun, Opts]) ->
|
|
|
|
|
|
|
|
- emqttd_time:seed(),
|
|
|
|
|
-
|
|
|
|
|
%% Calls from pubsub should be scheduled first?
|
|
%% Calls from pubsub should be scheduled first?
|
|
|
process_flag(priority, high),
|
|
process_flag(priority, high),
|
|
|
|
|
|
|
|
- ?GPROC_POOL(join, Pool, Id),
|
|
|
|
|
|
|
+ emqttd_time:seed(),
|
|
|
|
|
|
|
|
- AgingSecs = proplists:get_value(route_aging, Opts, 5),
|
|
|
|
|
|
|
+ ?GPROC_POOL(join, Pool, Id),
|
|
|
|
|
|
|
|
- %% Aging Timer
|
|
|
|
|
- {ok, AgingTref} = start_tick(AgingSecs + random:uniform(AgingSecs)),
|
|
|
|
|
|
|
+ Aging = init_aging(Opts),
|
|
|
|
|
|
|
|
- Aging = #aging{topics = dict:new(), time = AgingSecs, tref = AgingTref},
|
|
|
|
|
|
|
+ {ok, #state{pool = Pool, id = Id, aging = Aging, statsfun = StatsFun}}.
|
|
|
|
|
|
|
|
- {ok, #state{pool = Pool, id = Id, statsfun = StatsFun, aging = Aging}}.
|
|
|
|
|
|
|
+%% Init Aging
|
|
|
|
|
+init_aging(Opts) ->
|
|
|
|
|
+ AgingSecs = proplists:get_value(route_aging, Opts, 5),
|
|
|
|
|
+ {ok, AgingTref} = start_tick(AgingSecs + random:uniform(AgingSecs)),
|
|
|
|
|
+ #aging{topics = dict:new(), time = AgingSecs, tref = AgingTref}.
|
|
|
|
|
|
|
|
start_tick(Secs) ->
|
|
start_tick(Secs) ->
|
|
|
timer:send_interval(timer:seconds(Secs), {clean, aged}).
|
|
timer:send_interval(timer:seconds(Secs), {clean, aged}).
|
|
@@ -181,24 +198,15 @@ handle_call(Req, _From, State) ->
|
|
|
?UNEXPECTED_REQ(Req, State).
|
|
?UNEXPECTED_REQ(Req, State).
|
|
|
|
|
|
|
|
handle_cast({delete_route, Topic, Pid}, State = #state{aging = Aging}) ->
|
|
handle_cast({delete_route, Topic, Pid}, State = #state{aging = Aging}) ->
|
|
|
- ets:delete_object(route, {Topic, Pid}),
|
|
|
|
|
- NewState =
|
|
|
|
|
- case has_route(Topic) of
|
|
|
|
|
- false -> State#state{aging = store_aged(Topic, Aging)};
|
|
|
|
|
- true -> State
|
|
|
|
|
- end,
|
|
|
|
|
- {noreply, setstats(NewState)};
|
|
|
|
|
|
|
+ Aging1 = delete_route_(Topic, Pid, Aging),
|
|
|
|
|
+ {noreply, setstats(State#state{aging = Aging1})};
|
|
|
|
|
|
|
|
handle_cast({delete_routes, Topics, Pid}, State) ->
|
|
handle_cast({delete_routes, Topics, Pid}, State) ->
|
|
|
- NewAging =
|
|
|
|
|
|
|
+ Aging1 =
|
|
|
lists:foldl(fun(Topic, Aging) ->
|
|
lists:foldl(fun(Topic, Aging) ->
|
|
|
- ets:delete_object(route, {Topic, Pid}),
|
|
|
|
|
- case has_route(Topic) of
|
|
|
|
|
- false -> store_aged(Topic, Aging);
|
|
|
|
|
- true -> Aging
|
|
|
|
|
- end
|
|
|
|
|
|
|
+ delete_route_(Topic, Pid, Aging)
|
|
|
end, State#state.aging, Topics),
|
|
end, State#state.aging, Topics),
|
|
|
- {noreply, setstats(State#state{aging = NewAging})};
|
|
|
|
|
|
|
+ {noreply, setstats(State#state{aging = Aging1})};
|
|
|
|
|
|
|
|
handle_cast(Msg, State) ->
|
|
handle_cast(Msg, State) ->
|
|
|
?UNEXPECTED_MSG(Msg, State).
|
|
?UNEXPECTED_MSG(Msg, State).
|
|
@@ -211,9 +219,9 @@ handle_info({clean, aged}, State = #state{aging = Aging}) ->
|
|
|
|
|
|
|
|
Dict1 = try_clean(ByTime, dict:to_list(Dict)),
|
|
Dict1 = try_clean(ByTime, dict:to_list(Dict)),
|
|
|
|
|
|
|
|
- NewAging = Aging#aging{topics = dict:from_list(Dict1)},
|
|
|
|
|
|
|
+ Aging1 = Aging#aging{topics = dict:from_list(Dict1)},
|
|
|
|
|
|
|
|
- {noreply, State#state{aging = NewAging}, hibernate};
|
|
|
|
|
|
|
+ {noreply, State#state{aging = Aging1}, hibernate};
|
|
|
|
|
|
|
|
handle_info(Info, State) ->
|
|
handle_info(Info, State) ->
|
|
|
?UNEXPECTED_INFO(Info, State).
|
|
?UNEXPECTED_INFO(Info, State).
|
|
@@ -225,6 +233,13 @@ terminate(_Reason, #state{pool = Pool, id = Id, aging = #aging{tref = TRef}}) ->
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
{ok, State}.
|
|
{ok, State}.
|
|
|
|
|
|
|
|
|
|
+delete_route_(Topic, Pid, Aging) ->
|
|
|
|
|
+ ets:delete_object(route, {Topic, Pid}),
|
|
|
|
|
+ case has_route(Topic) of
|
|
|
|
|
+ false -> store_aged(Topic, Aging);
|
|
|
|
|
+ true -> Aging
|
|
|
|
|
+ end.
|
|
|
|
|
+
|
|
|
try_clean(ByTime, List) ->
|
|
try_clean(ByTime, List) ->
|
|
|
try_clean(ByTime, List, []).
|
|
try_clean(ByTime, List, []).
|
|
|
|
|
|
|
@@ -253,15 +268,12 @@ try_clean2(ByTime, {Topic, _TS}, Left, Acc) ->
|
|
|
try_remove_topic(TopicR = #mqtt_topic{topic = Topic}) ->
|
|
try_remove_topic(TopicR = #mqtt_topic{topic = Topic}) ->
|
|
|
%% Lock topic first
|
|
%% Lock topic first
|
|
|
case mnesia:wread({topic, Topic}) of
|
|
case mnesia:wread({topic, Topic}) of
|
|
|
- [] ->
|
|
|
|
|
- ok; %% mnesia:abort(not_found);
|
|
|
|
|
- [TopicR] ->
|
|
|
|
|
- %% Remove topic and trie
|
|
|
|
|
- delete_topic(TopicR),
|
|
|
|
|
- emqttd_trie:delete(Topic);
|
|
|
|
|
- _More ->
|
|
|
|
|
- %% Remove topic only
|
|
|
|
|
- delete_topic(TopicR)
|
|
|
|
|
|
|
+ [] -> ok;
|
|
|
|
|
+ [TopicR] -> %% Remove topic and trie
|
|
|
|
|
+ delete_topic(TopicR),
|
|
|
|
|
+ emqttd_trie:delete(Topic);
|
|
|
|
|
+ _More -> %% Remove topic only
|
|
|
|
|
+ delete_topic(TopicR)
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
delete_topic(TopicR) ->
|
|
delete_topic(TopicR) ->
|