25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01:00
xmpp.chapril.org-ejabberd/src/ejabberd_sm_redis.erl

263 lines
7.7 KiB
Erlang
Raw Normal View History

2015-03-11 12:46:57 +01:00
%%%-------------------------------------------------------------------
2016-12-27 10:44:07 +01:00
%%% File : ejabberd_sm_redis.erl
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
2015-03-11 12:46:57 +01:00
%%% Created : 11 Mar 2015 by Evgeny Khramtsov <ekhramtsov@process-one.net>
2016-12-27 10:44:07 +01:00
%%%
%%%
2019-01-08 22:53:27 +01:00
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
2016-12-27 10:44:07 +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.
%%%
%%%----------------------------------------------------------------------
2015-03-11 12:46:57 +01:00
-module(ejabberd_sm_redis).
-ifndef(GEN_SERVER).
-define(GEN_SERVER, p1_server).
-endif.
-behaviour(?GEN_SERVER).
2015-03-11 12:46:57 +01:00
-behaviour(ejabberd_sm).
-export([init/0, set_session/1, delete_session/1,
2015-06-01 14:38:27 +02:00
get_sessions/0, get_sessions/1, get_sessions/2,
cache_nodes/1, clean_table/1, clean_table/0]).
%% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
terminate/2, code_change/3, start_link/0]).
2015-03-11 12:46:57 +01:00
-include("ejabberd_sm.hrl").
-include("logger.hrl").
-define(SM_KEY, <<"ejabberd:sm">>).
-define(MIN_REDIS_VERSION, <<"3.2.0">>).
-record(state, {}).
2015-03-11 12:46:57 +01:00
%%%===================================================================
%%% API
%%%===================================================================
-spec init() -> ok | {error, any()}.
init() ->
Spec = {?MODULE, {?MODULE, start_link, []},
transient, 5000, worker, [?MODULE]},
case supervisor:start_child(ejabberd_backend_sup, Spec) of
{ok, _Pid} -> ok;
Err -> Err
end.
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() ->
?GEN_SERVER:start_link({local, ?MODULE}, ?MODULE, [], []).
2015-03-11 12:46:57 +01:00
-spec cache_nodes(binary()) -> [node()].
cache_nodes(_LServer) ->
[node()].
-spec set_session(#session{}) -> ok | {error, ejabberd_redis:error_reason()}.
2015-03-11 12:46:57 +01:00
set_session(Session) ->
T = term_to_binary(Session),
USKey = us_to_key(Session#session.us),
SIDKey = sid_to_key(Session#session.sid),
ServKey = server_to_key(element(2, Session#session.us)),
USSIDKey = us_sid_to_key(Session#session.us, Session#session.sid),
NodeHostKey = node_host_to_key(node(), element(2, Session#session.us)),
case ejabberd_redis:multi(
fun() ->
ejabberd_redis:hset(USKey, SIDKey, T),
ejabberd_redis:hset(ServKey, USSIDKey, T),
2018-07-14 16:53:00 +02:00
ejabberd_redis:hset(NodeHostKey,
<<USKey/binary, "||", SIDKey/binary>>,
USSIDKey),
ejabberd_redis:publish(
?SM_KEY, term_to_binary({delete, Session#session.us}))
end) of
{ok, _} ->
ok;
Err ->
Err
end.
2015-03-11 12:46:57 +01:00
-spec delete_session(#session{}) -> ok | {error, ejabberd_redis:error_reason()}.
delete_session(#session{sid = SID} = Session) ->
USKey = us_to_key(Session#session.us),
SIDKey = sid_to_key(SID),
ServKey = server_to_key(element(2, Session#session.us)),
USSIDKey = us_sid_to_key(Session#session.us, SID),
NodeHostKey = node_host_to_key(node(), element(2, Session#session.us)),
case ejabberd_redis:multi(
fun() ->
ejabberd_redis:hdel(USKey, [SIDKey]),
ejabberd_redis:hdel(ServKey, [USSIDKey]),
2018-07-14 16:53:00 +02:00
ejabberd_redis:hdel(NodeHostKey,
[<<USKey/binary, "||", SIDKey/binary>>]),
ejabberd_redis:publish(
?SM_KEY,
term_to_binary({delete, Session#session.us}))
end) of
{ok, _} ->
ok;
Err ->
Err
2015-03-11 12:46:57 +01:00
end.
-spec get_sessions() -> [#session{}].
get_sessions() ->
lists:flatmap(
fun(LServer) ->
get_sessions(LServer)
end, ejabberd_sm:get_vh_by_backend(?MODULE)).
2015-03-11 12:46:57 +01:00
-spec get_sessions(binary()) -> [#session{}].
get_sessions(LServer) ->
ServKey = server_to_key(LServer),
2017-04-02 10:56:09 +02:00
case ejabberd_redis:hgetall(ServKey) of
2015-03-11 12:46:57 +01:00
{ok, Vals} ->
decode_session_list(Vals);
2017-04-07 08:10:33 +02:00
{error, _} ->
2015-03-11 12:46:57 +01:00
[]
end.
-spec get_sessions(binary(), binary()) -> {ok, [#session{}]} |
{error, ejabberd_redis:error_reason()}.
2015-03-11 12:46:57 +01:00
get_sessions(LUser, LServer) ->
USKey = us_to_key({LUser, LServer}),
2017-04-02 10:56:09 +02:00
case ejabberd_redis:hgetall(USKey) of
{ok, Vals} ->
{ok, decode_session_list(Vals)};
Err ->
Err
2015-03-11 12:46:57 +01:00
end.
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([]) ->
ejabberd_redis:subscribe([?SM_KEY]),
case clean_table() of
ok -> {ok, #state{}};
{error, Why} -> {stop, Why}
end.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({redis_message, ?SM_KEY, Data}, State) ->
case binary_to_term(Data) of
{delete, Key} ->
ets_cache:delete(?SM_CACHE, Key);
Msg ->
?WARNING_MSG("unexpected redis message: ~p", [Msg])
end,
{noreply, State};
handle_info(Info, State) ->
2018-07-14 16:53:00 +02:00
?ERROR_MSG("unexpected info: ~p", [Info]),
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
2015-03-11 12:46:57 +01:00
%%%===================================================================
%%% Internal functions
%%%===================================================================
us_to_key({LUser, LServer}) ->
2018-07-14 16:53:00 +02:00
<<(?SM_KEY)/binary, ":", LUser/binary, "@", LServer/binary>>.
2015-03-11 12:46:57 +01:00
server_to_key(LServer) ->
2018-07-14 16:53:00 +02:00
<<(?SM_KEY)/binary, ":", LServer/binary>>.
2015-03-11 12:46:57 +01:00
us_sid_to_key(US, SID) ->
term_to_binary({US, SID}).
sid_to_key(SID) ->
term_to_binary(SID).
2018-07-14 16:53:00 +02:00
node_session_deletion_cursor(Node, Host) ->
NodeName = node_host_to_key(Node, Host),
<<NodeName/binary, ":deletioncursor">>.
2018-07-14 16:53:00 +02:00
node_host_to_key(Node, Host) when is_atom(Node) ->
NodeBin = atom_to_binary(node(), utf8),
node_host_to_key(NodeBin, Host);
2018-07-14 16:53:00 +02:00
node_host_to_key(NodeBin, Host) ->
HostKey = server_to_key(Host),
2018-07-14 16:53:00 +02:00
<<HostKey/binary, ":node:", NodeBin/binary>>.
2017-04-02 10:56:09 +02:00
decode_session_list(Vals) ->
[binary_to_term(Val) || {_, Val} <- Vals].
2015-03-11 12:46:57 +01:00
clean_table() ->
clean_table(node()).
clean_table(Node) when is_atom(Node) ->
clean_table(atom_to_binary(Node, utf8));
2018-07-14 16:53:00 +02:00
clean_table(Node) ->
?DEBUG("Cleaning Redis SM table... ", []),
2017-04-02 10:56:09 +02:00
try
2018-07-14 16:53:00 +02:00
lists:foreach(
fun(Host) ->
ok = clean_node_sessions(Node, Host)
end, ejabberd_sm:get_vh_by_backend(?MODULE))
catch _:{badmatch, {error, _} = Err} ->
?ERROR_MSG("Failed to clean Redis SM table", []),
Err
end.
clean_node_sessions(Node, Host) ->
case load_script() of
2018-07-14 16:53:00 +02:00
{ok, SHA} ->
clean_node_sessions(Node, Host, SHA);
2018-07-14 16:53:00 +02:00
Err ->
Err
2017-04-02 10:56:09 +02:00
end.
clean_node_sessions(Node, Host, SHA) ->
2018-07-14 16:53:00 +02:00
Keys = [node_host_to_key(Node, Host),
server_to_key(Host),
node_session_deletion_cursor(Node, Host)],
case ejabberd_redis:evalsha(SHA, Keys, [1000]) of
{ok, <<"0">>} ->
ok;
{ok, _Cursor} ->
clean_node_sessions(Node, Host, SHA);
{error, _} = Err ->
Err
end.
load_script() ->
2018-07-14 16:53:00 +02:00
case misc:read_lua("redis_sm.lua") of
{ok, Data} ->
case ejabberd_redis:info(server) of
{ok, Info} ->
case proplists:get_value(redis_version, Info) of
V when V >= ?MIN_REDIS_VERSION ->
ejabberd_redis:script_load(Data);
V ->
?CRITICAL_MSG("Unsupported Redis version: ~s. "
"The version must be ~s or above",
[V, ?MIN_REDIS_VERSION]),
{error, unsupported_redis_version}
end;
{error, _} = Err ->
Err
end;
2018-07-14 16:53:00 +02:00
{error, _} = Err ->
Err
end.