2017-01-13 10:03:39 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% Created : 12 Jan 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2017-01-13 10:03:39 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(mod_bosh_mnesia).
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
-behaviour(mod_bosh).
|
|
|
|
|
|
|
|
%% mod_bosh API
|
2017-04-14 12:57:52 +02:00
|
|
|
-export([init/0, open_session/2, close_session/1, find_session/1,
|
|
|
|
use_cache/0]).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
2017-02-26 13:10:59 +01:00
|
|
|
terminate/2, code_change/3, start_link/0]).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
2019-09-10 15:02:51 +02:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-define(CALL_TIMEOUT, timer:minutes(10)).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-record(bosh, {sid = <<"">> :: binary(),
|
|
|
|
timestamp = erlang:timestamp() :: erlang:timestamp(),
|
|
|
|
pid = self() :: pid()}).
|
|
|
|
|
|
|
|
-record(state, {nodes = #{} :: #{node() => {pid(), reference()}}}).
|
|
|
|
-type state() :: #state{}.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
2017-02-26 13:10:59 +01:00
|
|
|
-spec init() -> ok | {error, any()}.
|
2017-01-13 10:03:39 +01:00
|
|
|
init() ->
|
2017-02-26 13:10:59 +01:00
|
|
|
Spec = {?MODULE, {?MODULE, start_link, []},
|
|
|
|
transient, 5000, worker, [?MODULE]},
|
|
|
|
case supervisor:start_child(ejabberd_backend_sup, Spec) of
|
|
|
|
{ok, _Pid} -> ok;
|
2019-09-10 15:02:51 +02:00
|
|
|
{error, {already_started, _}} -> ok;
|
2017-02-26 13:10:59 +01:00
|
|
|
Err -> Err
|
2017-01-13 10:03:39 +01:00
|
|
|
end.
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-spec start_link() -> {ok, pid()} | {error, any()}.
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
use_cache() ->
|
|
|
|
false.
|
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec open_session(binary(), pid()) -> ok.
|
2017-01-13 10:03:39 +01:00
|
|
|
open_session(SID, Pid) ->
|
2019-02-27 09:56:20 +01:00
|
|
|
Session = #bosh{sid = SID, timestamp = erlang:timestamp(), pid = Pid},
|
2019-09-10 15:02:51 +02:00
|
|
|
gen_server:call(?MODULE, {write, Session}, ?CALL_TIMEOUT).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec close_session(binary()) -> ok.
|
2017-01-13 10:03:39 +01:00
|
|
|
close_session(SID) ->
|
|
|
|
case mnesia:dirty_read(bosh, SID) of
|
|
|
|
[Session] ->
|
2019-09-10 15:02:51 +02:00
|
|
|
gen_server:call(?MODULE, {delete, Session}, ?CALL_TIMEOUT);
|
2017-01-13 10:03:39 +01:00
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec find_session(binary()) -> {ok, pid()} | {error, notfound}.
|
2017-01-13 10:03:39 +01:00
|
|
|
find_session(SID) ->
|
|
|
|
case mnesia:dirty_read(bosh, SID) of
|
|
|
|
[#bosh{pid = Pid}] ->
|
|
|
|
{ok, Pid};
|
|
|
|
[] ->
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, notfound}
|
2017-01-13 10:03:39 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec init([]) -> {ok, state()}.
|
2017-01-13 10:03:39 +01:00
|
|
|
init([]) ->
|
|
|
|
setup_database(),
|
2019-09-10 15:02:51 +02:00
|
|
|
multicast({join, node(), self()}),
|
|
|
|
mnesia:subscribe(system),
|
2017-01-13 10:03:39 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec handle_call(_, _, state()) -> {reply, ok, state()} | {noreply, state()}.
|
|
|
|
handle_call({write, Session} = Msg, _From, State) ->
|
|
|
|
write_session(Session),
|
|
|
|
multicast(Msg),
|
|
|
|
{reply, ok, State};
|
|
|
|
handle_call({delete, Session} = Msg, _From, State) ->
|
|
|
|
delete_session(Session),
|
|
|
|
multicast(Msg),
|
|
|
|
{reply, ok, State};
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec handle_cast(_, state()) -> {noreply, state()}.
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2017-01-13 10:03:39 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec handle_info(_, state()) -> {noreply, state()}.
|
2017-01-13 10:03:39 +01:00
|
|
|
handle_info({write, Session}, State) ->
|
|
|
|
write_session(Session),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({delete, Session}, State) ->
|
|
|
|
delete_session(Session),
|
|
|
|
{noreply, State};
|
2019-09-10 15:02:51 +02:00
|
|
|
handle_info({join, Node, Pid}, State) ->
|
|
|
|
ejabberd_cluster:send(Pid, {joined, node(), self()}),
|
|
|
|
case maps:find(Node, State#state.nodes) of
|
|
|
|
{ok, {Pid, _}} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ejabberd_cluster:send(Pid, {join, node(), self()})
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({joined, Node, Pid}, State) ->
|
|
|
|
case maps:find(Node, State#state.nodes) of
|
|
|
|
{ok, {Pid, _}} ->
|
|
|
|
{noreply, State};
|
|
|
|
Ret ->
|
|
|
|
MRef = erlang:monitor(process, {?MODULE, Node}),
|
|
|
|
Nodes = maps:put(Node, {Pid, MRef}, State#state.nodes),
|
|
|
|
case Ret of
|
|
|
|
error -> ejabberd_cluster:send(Pid, {first, self()});
|
|
|
|
_ -> ok
|
|
|
|
end,
|
|
|
|
{noreply, State#state{nodes = Nodes}}
|
|
|
|
end;
|
|
|
|
handle_info({first, From}, State) ->
|
|
|
|
ejabberd_cluster:send(From, {replica, node(), first_session()}),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({next, From, Key}, State) ->
|
|
|
|
ejabberd_cluster:send(From, {replica, node(), next_session(Key)}),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({replica, _From, '$end_of_table'}, State) ->
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({replica, From, Session}, State) ->
|
|
|
|
write_session(Session),
|
|
|
|
ejabberd_cluster:send(From, {next, self(), Session#bosh.sid}),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({'DOWN', _, process, {?MODULE, _}, _Info}, State) ->
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({mnesia_system_event, {mnesia_down, Node}}, State) ->
|
|
|
|
Sessions =
|
|
|
|
ets:select(
|
|
|
|
bosh,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#bosh{pid = Pid} = S) when node(Pid) == Node ->
|
|
|
|
S
|
|
|
|
end)),
|
|
|
|
lists:foreach(
|
|
|
|
fun(S) ->
|
|
|
|
mnesia:dirty_delete_object(S)
|
|
|
|
end, Sessions),
|
|
|
|
Nodes = maps:remove(Node, State#state.nodes),
|
|
|
|
{noreply, State#state{nodes = Nodes}};
|
|
|
|
handle_info({mnesia_system_event, _}, State) ->
|
|
|
|
{noreply, State};
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2017-01-13 10:03:39 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec write_session(#bosh{}) -> ok.
|
2017-01-13 10:03:39 +01:00
|
|
|
write_session(#bosh{pid = Pid1, sid = SID, timestamp = T1} = S1) ->
|
|
|
|
case mnesia:dirty_read(bosh, SID) of
|
|
|
|
[#bosh{pid = Pid2, timestamp = T2} = S2] ->
|
|
|
|
if Pid1 == Pid2 ->
|
|
|
|
mnesia:dirty_write(S1);
|
|
|
|
T1 < T2 ->
|
2019-09-10 15:02:51 +02:00
|
|
|
ejabberd_cluster:send(Pid2, replaced),
|
2017-01-13 10:03:39 +01:00
|
|
|
mnesia:dirty_write(S1);
|
|
|
|
true ->
|
2019-09-10 15:02:51 +02:00
|
|
|
ejabberd_cluster:send(Pid1, replaced),
|
2017-01-13 10:03:39 +01:00
|
|
|
mnesia:dirty_write(S2)
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
mnesia:dirty_write(S1)
|
|
|
|
end.
|
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec delete_session(#bosh{}) -> ok.
|
2017-01-13 10:03:39 +01:00
|
|
|
delete_session(#bosh{sid = SID, pid = Pid1}) ->
|
|
|
|
case mnesia:dirty_read(bosh, SID) of
|
|
|
|
[#bosh{pid = Pid2}] ->
|
|
|
|
if Pid1 == Pid2 ->
|
|
|
|
mnesia:dirty_delete(bosh, SID);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2019-09-10 15:02:51 +02:00
|
|
|
-spec multicast(_) -> ok.
|
|
|
|
multicast(Msg) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Node) when Node /= node() ->
|
|
|
|
ejabberd_cluster:send({?MODULE, Node}, Msg);
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, ejabberd_cluster:get_nodes()).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
setup_database() ->
|
|
|
|
case catch mnesia:table_info(bosh, attributes) of
|
|
|
|
[sid, pid] ->
|
|
|
|
mnesia:delete_table(bosh);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ejabberd_mnesia:create(?MODULE, bosh,
|
|
|
|
[{ram_copies, [node()]}, {local_content, true},
|
2017-04-21 11:27:15 +02:00
|
|
|
{attributes, record_info(fields, bosh)}]).
|
2019-09-10 15:02:51 +02:00
|
|
|
|
|
|
|
-spec first_session() -> #bosh{} | '$end_of_table'.
|
|
|
|
first_session() ->
|
|
|
|
case mnesia:dirty_first(bosh) of
|
|
|
|
'$end_of_table' ->
|
|
|
|
'$end_of_table';
|
|
|
|
First ->
|
|
|
|
read_session(First)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec next_session(binary()) -> #bosh{} | '$end_of_table'.
|
|
|
|
next_session(Prev) ->
|
|
|
|
case mnesia:dirty_next(bosh, Prev) of
|
|
|
|
'$end_of_table' ->
|
|
|
|
'$end_of_table';
|
|
|
|
Next ->
|
|
|
|
read_session(Next)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec read_session(binary()) -> #bosh{} | '$end_of_table'.
|
|
|
|
read_session(Key) ->
|
|
|
|
case mnesia:dirty_read(bosh, Key) of
|
|
|
|
[#bosh{pid = Pid} = Session] when node(Pid) == node() ->
|
|
|
|
Session;
|
|
|
|
_ ->
|
|
|
|
next_session(Key)
|
|
|
|
end.
|