From 6ad93eb6091bda94ec850bc0972347e7f9a2a96a Mon Sep 17 00:00:00 2001 From: Christophe Romain Date: Wed, 13 Jan 2010 10:22:55 +0000 Subject: [PATCH] add send_loop robustness SVN Revision: 2899 --- src/mod_pubsub/mod_pubsub.erl | 40 +++++++++++---- src/mod_pubsub/mod_pubsub_odbc.erl | 40 +++++++++++---- src/mod_pubsub/pubsub_odbc.patch | 80 +++++++++++++++--------------- 3 files changed, 100 insertions(+), 60 deletions(-) diff --git a/src/mod_pubsub/mod_pubsub.erl b/src/mod_pubsub/mod_pubsub.erl index 82910b850..7bd1fae77 100644 --- a/src/mod_pubsub/mod_pubsub.erl +++ b/src/mod_pubsub/mod_pubsub.erl @@ -134,8 +134,7 @@ last_item_cache = false, max_items_node = ?MAXITEMS, nodetree = ?STDTREE, - plugins = [?STDNODE], - send_loop}). + plugins = [?STDNODE]}). %%==================================================================== %% API @@ -195,6 +194,7 @@ init([ServerHost, Opts]) -> ets:insert(gen_mod:get_module_proc(ServerHost, config), {last_item_cache, LastItemCache}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {max_items_node, MaxItemsNode}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {pep_mapping, PepMapping}), + ets:insert(gen_mod:get_module_proc(ServerHost, config), {ignore_pep_from_offline, PepOffline}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {host, Host}), ejabberd_hooks:add(disco_sm_identity, ServerHost, ?MODULE, disco_sm_identity, 75), ejabberd_hooks:add(disco_sm_features, ServerHost, ?MODULE, disco_sm_features, 75), @@ -229,9 +229,14 @@ init([ServerHost, Opts]) -> max_items_node = MaxItemsNode, nodetree = NodeTree, plugins = Plugins}, - SendLoop = spawn(?MODULE, send_loop, [State]), %% TODO put this in supervision - register(gen_mod:get_module_proc(ServerHost, ?LOOPNAME), SendLoop), - {ok, State#state{send_loop = SendLoop}}. + init_send_loop(ServerHost, State), + {ok, State}. + +init_send_loop(ServerHost, State) -> + Proc = gen_mod:get_module_proc(ServerHost, ?LOOPNAME), + SendLoop = spawn(?MODULE, send_loop, [State]), + register(Proc, SendLoop), + SendLoop. %% @spec (Host, ServerHost, Opts) -> Plugins %% Host = mod_pubsub:host() Opts = [{Key,Value}] @@ -720,8 +725,24 @@ presence_probe(#jid{luser = User, lserver = Server, lresource = Resource}, #jid{ presence(Host, {presence, User, Server, [Resource], JID}). presence(ServerHost, Presence) -> - Proc = gen_mod:get_module_proc(ServerHost, ?LOOPNAME), - Proc ! Presence. + SendLoop = case whereis(gen_mod:get_module_proc(ServerHost, ?LOOPNAME)) of + undefined -> + % in case send_loop process died, we rebuild a minimal State record and respawn it + Host = host(ServerHost), + Plugins = plugins(Host), + PepOffline = case catch ets:lookup(gen_mod:get_module_proc(ServerHost, config), ignore_pep_from_offline) of + [{ignore_pep_from_offline, PO}] -> PO; + _ -> true + end, + State = #state{host = Host, + server_host = ServerHost, + ignore_pep_from_offline = PepOffline, + plugins = Plugins}, + init_send_loop(ServerHost, State); + Pid -> + Pid + end, + SendLoop ! Presence. %% ------- %% subscription hooks handling functions @@ -853,8 +874,7 @@ handle_info(_Info, State) -> terminate(_Reason, #state{host = Host, server_host = ServerHost, nodetree = TreePlugin, - plugins = Plugins, - send_loop = SendLoop}) -> + plugins = Plugins}) -> ejabberd_router:unregister_route(Host), case lists:member(?PEPNODE, Plugins) of true -> @@ -877,7 +897,7 @@ terminate(_Reason, #state{host = Host, gen_iq_handler:remove_iq_handler(ejabberd_sm, ServerHost, ?NS_PUBSUB), gen_iq_handler:remove_iq_handler(ejabberd_sm, ServerHost, ?NS_PUBSUB_OWNER), mod_disco:unregister_feature(ServerHost, ?NS_PUBSUB), - SendLoop ! stop, + gen_mod:get_module_proc(ServerHost, ?LOOPNAME) ! stop, terminate_plugins(Host, ServerHost, Plugins, TreePlugin). %%-------------------------------------------------------------------- diff --git a/src/mod_pubsub/mod_pubsub_odbc.erl b/src/mod_pubsub/mod_pubsub_odbc.erl index a30009f4e..2eeb49c82 100644 --- a/src/mod_pubsub/mod_pubsub_odbc.erl +++ b/src/mod_pubsub/mod_pubsub_odbc.erl @@ -134,8 +134,7 @@ last_item_cache = false, max_items_node = ?MAXITEMS, nodetree = ?STDTREE, - plugins = [?STDNODE], - send_loop}). + plugins = [?STDNODE]}). %%==================================================================== %% API @@ -195,6 +194,7 @@ init([ServerHost, Opts]) -> ets:insert(gen_mod:get_module_proc(ServerHost, config), {last_item_cache, LastItemCache}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {max_items_node, MaxItemsNode}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {pep_mapping, PepMapping}), + ets:insert(gen_mod:get_module_proc(ServerHost, config), {ignore_pep_from_offline, PepOffline}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {host, Host}), ejabberd_hooks:add(disco_sm_identity, ServerHost, ?MODULE, disco_sm_identity, 75), ejabberd_hooks:add(disco_sm_features, ServerHost, ?MODULE, disco_sm_features, 75), @@ -227,9 +227,14 @@ init([ServerHost, Opts]) -> max_items_node = MaxItemsNode, nodetree = NodeTree, plugins = Plugins}, - SendLoop = spawn(?MODULE, send_loop, [State]), %% TODO put this in supervision - register(gen_mod:get_module_proc(ServerHost, ?LOOPNAME), SendLoop), - {ok, State#state{send_loop = SendLoop}}. + init_send_loop(ServerHost, State), + {ok, State}. + +init_send_loop(ServerHost, State) -> + Proc = gen_mod:get_module_proc(ServerHost, ?LOOPNAME), + SendLoop = spawn(?MODULE, send_loop, [State]), + register(Proc, SendLoop), + SendLoop. %% @spec (Host, ServerHost, Opts) -> Plugins %% Host = mod_pubsub:host() Opts = [{Key,Value}] @@ -523,8 +528,24 @@ presence_probe(#jid{luser = User, lserver = Server, lresource = Resource}, #jid{ presence(Host, {presence, User, Server, [Resource], JID}). presence(ServerHost, Presence) -> - Proc = gen_mod:get_module_proc(ServerHost, ?LOOPNAME), - Proc ! Presence. + SendLoop = case whereis(gen_mod:get_module_proc(ServerHost, ?LOOPNAME)) of + undefined -> + % in case send_loop process died, we rebuild a minimal State record and respawn it + Host = host(ServerHost), + Plugins = plugins(Host), + PepOffline = case catch ets:lookup(gen_mod:get_module_proc(ServerHost, config), ignore_pep_from_offline) of + [{ignore_pep_from_offline, PO}] -> PO; + _ -> true + end, + State = #state{host = Host, + server_host = ServerHost, + ignore_pep_from_offline = PepOffline, + plugins = Plugins}, + init_send_loop(ServerHost, State); + Pid -> + Pid + end, + SendLoop ! Presence. %% ------- %% subscription hooks handling functions @@ -656,8 +677,7 @@ handle_info(_Info, State) -> terminate(_Reason, #state{host = Host, server_host = ServerHost, nodetree = TreePlugin, - plugins = Plugins, - send_loop = SendLoop}) -> + plugins = Plugins}) -> ejabberd_router:unregister_route(Host), case lists:member(?PEPNODE, Plugins) of true -> @@ -680,7 +700,7 @@ terminate(_Reason, #state{host = Host, gen_iq_handler:remove_iq_handler(ejabberd_sm, ServerHost, ?NS_PUBSUB), gen_iq_handler:remove_iq_handler(ejabberd_sm, ServerHost, ?NS_PUBSUB_OWNER), mod_disco:unregister_feature(ServerHost, ?NS_PUBSUB), - SendLoop ! stop, + gen_mod:get_module_proc(ServerHost, ?LOOPNAME) ! stop, terminate_plugins(Host, ServerHost, Plugins, TreePlugin). %%-------------------------------------------------------------------- diff --git a/src/mod_pubsub/pubsub_odbc.patch b/src/mod_pubsub/pubsub_odbc.patch index 1c400d912..c2fa5ed23 100644 --- a/src/mod_pubsub/pubsub_odbc.patch +++ b/src/mod_pubsub/pubsub_odbc.patch @@ -1,5 +1,5 @@ ---- mod_pubsub.erl 2010-01-12 16:20:07.000000000 +0100 -+++ mod_pubsub_odbc.erl 2010-01-12 16:20:19.000000000 +0100 +--- mod_pubsub.erl 2010-01-13 10:58:17.000000000 +0100 ++++ mod_pubsub_odbc.erl 2010-01-13 10:59:59.000000000 +0100 @@ -42,7 +42,7 @@ %%% 6.2.3.1, 6.2.3.5, and 6.3. For information on subscription leases see %%% XEP-0060 section 12.18. @@ -49,7 +49,7 @@ init_nodes(Host, ServerHost, NodeTree, Plugins), State = #state{host = Host, server_host = ServerHost, -@@ -272,207 +270,14 @@ +@@ -277,207 +275,14 @@ init_nodes(Host, ServerHost, _NodeTree, Plugins) -> %% TODO, this call should be done plugin side @@ -260,7 +260,7 @@ send_loop(State) -> receive {presence, JID, Pid} -> -@@ -483,17 +288,15 @@ +@@ -488,17 +293,15 @@ %% for each node From is subscribed to %% and if the node is so configured, send the last published item to From lists:foreach(fun(PType) -> @@ -284,7 +284,7 @@ true -> % resource not concerned about that subscription ok -@@ -749,10 +552,10 @@ +@@ -770,10 +573,10 @@ lists:foreach(fun(PType) -> {result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Entity]), lists:foreach(fun @@ -297,7 +297,7 @@ true -> node_action(Host, PType, unsubscribe_node, [NodeId, Entity, JID, all]); false -> -@@ -919,7 +722,8 @@ +@@ -939,7 +742,8 @@ sub_el = SubEl} = IQ -> {xmlelement, _, QAttrs, _} = SubEl, Node = xml:get_attr_s("node", QAttrs), @@ -307,7 +307,7 @@ {result, IQRes} -> jlib:iq_to_xml( IQ#iq{type = result, -@@ -1036,7 +840,7 @@ +@@ -1056,7 +860,7 @@ [] -> ["leaf"]; %% No sub-nodes: it's a leaf node _ -> @@ -316,7 +316,7 @@ {result, []} -> ["collection"]; {result, _} -> ["leaf", "collection"]; _ -> [] -@@ -1052,8 +856,9 @@ +@@ -1072,8 +876,9 @@ []; true -> [{xmlelement, "feature", [{"var", ?NS_PUBSUB}], []} | @@ -328,7 +328,7 @@ end, features(Type))] end, %% TODO: add meta-data info (spec section 5.4) -@@ -1082,8 +887,9 @@ +@@ -1102,8 +907,9 @@ {xmlelement, "feature", [{"var", ?NS_PUBSUB}], []}, {xmlelement, "feature", [{"var", ?NS_COMMANDS}], []}, {xmlelement, "feature", [{"var", ?NS_VCARD}], []}] ++ @@ -340,7 +340,7 @@ end, features(Host, Node))}; <> -> command_disco_info(Host, Node, From); -@@ -1093,7 +899,7 @@ +@@ -1113,7 +919,7 @@ node_disco_info(Host, Node, From) end. @@ -349,7 +349,7 @@ case tree_action(Host, get_subnodes, [Host, <<>>, From]) of Nodes when is_list(Nodes) -> {result, lists:map( -@@ -1105,14 +911,14 @@ +@@ -1125,14 +931,14 @@ Other -> Other end; @@ -367,7 +367,7 @@ case string:tokens(Item, "!") of [_SNode, _ItemID] -> {result, []}; -@@ -1120,10 +926,10 @@ +@@ -1140,10 +946,10 @@ Node = string_to_node(SNode), Action = fun(#pubsub_node{type = Type, id = NodeId}) -> @@ -381,7 +381,7 @@ end, Nodes = lists:map( fun(#pubsub_node{nodeid = {_, SubNode}}) -> -@@ -1136,7 +942,7 @@ +@@ -1156,7 +962,7 @@ {result, Name} = node_call(Type, get_item_name, [Host, Node, RN]), {xmlelement, "item", [{"jid", Host}, {"name", Name}], []} end, NodeItems), @@ -390,7 +390,7 @@ end, case transaction(Host, Node, Action, sync_dirty) of {result, {_, Result}} -> {result, Result}; -@@ -1265,7 +1071,8 @@ +@@ -1285,7 +1091,8 @@ (_, Acc) -> Acc end, [], xml:remove_cdata(Els)), @@ -400,7 +400,7 @@ {get, "subscriptions"} -> get_subscriptions(Host, Node, From, Plugins); {get, "affiliations"} -> -@@ -1288,7 +1095,9 @@ +@@ -1308,7 +1115,9 @@ iq_pubsub_owner(Host, ServerHost, From, IQType, SubEl, Lang) -> {xmlelement, _, _, SubEls} = SubEl, @@ -411,7 +411,7 @@ case Action of [{xmlelement, Name, Attrs, Els}] -> Node = string_to_node(xml:get_attr_s("node", Attrs)), -@@ -1418,7 +1227,8 @@ +@@ -1438,7 +1247,8 @@ _ -> [] end end, @@ -421,7 +421,7 @@ sync_dirty) of {result, Res} -> Res; Err -> Err -@@ -1457,7 +1267,7 @@ +@@ -1477,7 +1287,7 @@ %%% authorization handling @@ -430,7 +430,7 @@ Lang = "en", %% TODO fix Stanza = {xmlelement, "message", [], -@@ -1486,7 +1296,7 @@ +@@ -1506,7 +1316,7 @@ [{xmlelement, "value", [], [{xmlcdata, "false"}]}]}]}]}, lists:foreach(fun(Owner) -> ejabberd_router:route(service_jid(Host), jlib:make_jid(Owner), Stanza) @@ -439,7 +439,7 @@ find_authorization_response(Packet) -> {xmlelement, _Name, _Attrs, Els} = Packet, -@@ -1550,8 +1360,8 @@ +@@ -1570,8 +1380,8 @@ "true" -> true; _ -> false end, @@ -450,7 +450,7 @@ {result, Subscriptions} = node_call(Type, get_subscriptions, [NodeId, Subscriber]), if not IsApprover -> -@@ -1742,7 +1552,7 @@ +@@ -1762,7 +1572,7 @@ Reply = [{xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}], [{xmlelement, "create", nodeAttr(Node), []}]}], @@ -459,7 +459,7 @@ {result, {Result, broadcast}} -> %%Lang = "en", %% TODO: fix %%OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)), -@@ -1850,7 +1660,7 @@ +@@ -1870,7 +1680,7 @@ %%
  • The node does not exist.
  • %% subscribe_node(Host, Node, From, JID, Configuration) -> @@ -468,7 +468,7 @@ {result, GoodSubOpts} -> GoodSubOpts; _ -> invalid end, -@@ -1858,7 +1668,7 @@ +@@ -1878,7 +1688,7 @@ error -> {"", "", ""}; J -> jlib:jid_tolower(J) end, @@ -477,7 +477,7 @@ Features = features(Type), SubscribeFeature = lists:member("subscribe", Features), OptionsFeature = lists:member("subscription-options", Features), -@@ -1877,9 +1687,13 @@ +@@ -1897,9 +1707,13 @@ {"", "", ""} -> {false, false}; _ -> @@ -494,7 +494,7 @@ end end, if -@@ -2210,7 +2024,7 @@ +@@ -2230,7 +2044,7 @@ %%

    The permission are not checked in this function.

    %% @todo We probably need to check that the user doing the query has the right %% to read the items. @@ -503,7 +503,7 @@ MaxItems = if SMaxItems == "" -> get_max_items_node(Host); -@@ -2249,11 +2063,11 @@ +@@ -2269,11 +2083,11 @@ node_call(Type, get_items, [NodeId, From, AccessModel, PresenceSubscription, RosterGroup, @@ -517,7 +517,7 @@ SendItems = case ItemIDs of [] -> Items; -@@ -2266,7 +2080,8 @@ +@@ -2286,7 +2100,8 @@ %% number of items sent to MaxItems: {result, [{xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}], [{xmlelement, "items", nodeAttr(Node), @@ -527,7 +527,7 @@ Error -> Error end -@@ -2298,16 +2113,27 @@ +@@ -2318,16 +2133,27 @@ %% @doc

    Resend the items of a node to the user.

    %% @todo use cache-last-item feature send_items(Host, Node, NodeId, Type, LJID, last) -> @@ -561,7 +561,7 @@ send_items(Host, Node, NodeId, Type, LJID, Number) -> ToSend = case node_action(Host, Type, get_items, [NodeId, LJID]) of {result, []} -> -@@ -2433,29 +2259,12 @@ +@@ -2453,29 +2279,12 @@ error -> {error, ?ERR_BAD_REQUEST}; _ -> @@ -594,7 +594,7 @@ end, Entities), {result, []}; _ -> -@@ -2508,11 +2317,11 @@ +@@ -2528,11 +2337,11 @@ end. read_sub(Subscriber, Node, NodeID, SubID, Lang) -> @@ -608,7 +608,7 @@ OptionsEl = {xmlelement, "options", [{"jid", jlib:jid_to_string(Subscriber)}, {"subid", SubID}|nodeAttr(Node)], [XdataEl]}, -@@ -2538,7 +2347,7 @@ +@@ -2558,7 +2367,7 @@ end. set_options_helper(Configuration, JID, NodeID, SubID, Type) -> @@ -617,7 +617,7 @@ {result, GoodSubOpts} -> GoodSubOpts; _ -> invalid end, -@@ -2567,7 +2376,7 @@ +@@ -2587,7 +2396,7 @@ write_sub(_Subscriber, _NodeID, _SubID, invalid) -> {error, extended_error(?ERR_BAD_REQUEST, "invalid-options")}; write_sub(Subscriber, NodeID, SubID, Options) -> @@ -626,7 +626,7 @@ {error, notfound} -> {error, extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")}; {result, _} -> -@@ -2735,8 +2544,8 @@ +@@ -2755,8 +2564,8 @@ {"subscription", subscription_to_string(Sub)} | nodeAttr(Node)], []}]}]}, ejabberd_router:route(service_jid(Host), jlib:make_jid(JID), Stanza) end, @@ -637,7 +637,7 @@ true -> Result = lists:foldl(fun({JID, Subscription, SubId}, Acc) -> -@@ -3020,7 +2829,7 @@ +@@ -3040,7 +2849,7 @@ {Depth, [{N, get_node_subs(N)} || N <- Nodes]} end, tree_call(Host, get_parentnodes_tree, [Host, Node, service_jid(Host)]))} end, @@ -646,7 +646,7 @@ {result, CollSubs} -> CollSubs; _ -> [] end. -@@ -3034,9 +2843,9 @@ +@@ -3054,9 +2863,9 @@ get_options_for_subs(NodeID, Subs) -> lists:foldl(fun({JID, subscribed, SubID}, Acc) -> @@ -658,7 +658,7 @@ _ -> Acc end; (_, Acc) -> -@@ -3246,6 +3055,30 @@ +@@ -3266,6 +3075,30 @@ Result end. @@ -689,7 +689,7 @@ %% @spec (Host, Options) -> MaxItems %% Host = host() %% Options = [Option] -@@ -3638,7 +3471,13 @@ +@@ -3658,7 +3491,13 @@ tree_action(Host, Function, Args) -> ?DEBUG("tree_action ~p ~p ~p",[Host,Function,Args]), Fun = fun() -> tree_call(Host, Function, Args) end, @@ -704,7 +704,7 @@ %% @doc

    node plugin call.

    node_call(Type, Function, Args) -> -@@ -3658,13 +3497,13 @@ +@@ -3678,13 +3517,13 @@ node_action(Host, Type, Function, Args) -> ?DEBUG("node_action ~p ~p ~p ~p",[Host,Type,Function,Args]), @@ -720,7 +720,7 @@ case tree_call(Host, get_node, [Host, Node]) of N when is_record(N, pubsub_node) -> case Action(N) of -@@ -3677,8 +3516,14 @@ +@@ -3697,8 +3536,14 @@ end end, Trans). @@ -737,7 +737,7 @@ {result, Result} -> {result, Result}; {error, Error} -> {error, Error}; {atomic, {result, Result}} -> {result, Result}; -@@ -3686,6 +3531,15 @@ +@@ -3706,6 +3551,15 @@ {aborted, Reason} -> ?ERROR_MSG("transaction return internal error: ~p~n", [{aborted, Reason}]), {error, ?ERR_INTERNAL_SERVER_ERROR}; @@ -753,7 +753,7 @@ {'EXIT', Reason} -> ?ERROR_MSG("transaction return internal error: ~p~n", [{'EXIT', Reason}]), {error, ?ERR_INTERNAL_SERVER_ERROR}; -@@ -3694,6 +3548,17 @@ +@@ -3714,6 +3568,17 @@ {error, ?ERR_INTERNAL_SERVER_ERROR} end.