summaryrefslogtreecommitdiff
path: root/src/delegate.erl
blob: 5ed7035160c2bcf17a83e22c280cd73e59ca551f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
%%   The contents of this file are subject to the Mozilla Public License
%%   Version 1.1 (the "License"); you may not use this file except in
%%   compliance with the License. You may obtain a copy of the License at
%%   http://www.mozilla.org/MPL/
%%
%%   Software distributed under the License is distributed on an "AS IS"
%%   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%%   License for the specific language governing rights and limitations
%%   under the License.
%%
%%   The Original Code is RabbitMQ.
%%
%%   The Initial Developers of the Original Code are LShift Ltd,
%%   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
%%
%%   Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
%%   Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
%%   are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
%%   Technologies LLC, and Rabbit Technologies Ltd.
%%
%%   Portions created by LShift Ltd are Copyright (C) 2007-2010 LShift
%%   Ltd. Portions created by Cohesive Financial Technologies LLC are
%%   Copyright (C) 2007-2010 Cohesive Financial Technologies
%%   LLC. Portions created by Rabbit Technologies Ltd are Copyright
%%   (C) 2007-2010 Rabbit Technologies Ltd.
%%
%%   All Rights Reserved.
%%
%%   Contributor(s): ______________________________________.
%%

-module(delegate).
-define(DELEGATE_PROCESS_COUNT_MULTIPLIER, 2).

-behaviour(gen_server2).

-export([start_link/1, invoke_async/2, invoke/2,
         server/1, process_count/0]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

%%----------------------------------------------------------------------------

-ifdef(use_specs).

-spec(start_link/1 :: (non_neg_integer()) -> {'ok', pid()}).
-spec(invoke_async/2 :: (pid() | [pid()], fun((pid()) -> any())) -> 'ok').
-spec(invoke/2 :: (pid() | [pid()], fun((pid()) -> A)) -> A).

-spec(server/1 :: (node() | non_neg_integer()) -> atom()).
-spec(process_count/0 :: () -> non_neg_integer()).

-endif.

%%----------------------------------------------------------------------------

start_link(Hash) ->
    gen_server2:start_link({local, server(Hash)},
                           ?MODULE, [], []).

invoke(Pid, FPid) when is_pid(Pid) ->
    [{Status, Res, _}] = invoke_per_node([{node(Pid), [Pid]}], FPid),
    {Status, Res};

invoke(Pids, FPid) when is_list(Pids) ->
    invoke_per_node(split_delegate_per_node(Pids), FPid).

invoke_async(Pid, FPid) when is_pid(Pid) ->
    invoke_async_per_node([{node(Pid), [Pid]}], FPid),
    ok;

invoke_async(Pids, FPid) when is_list(Pids) ->
    invoke_async_per_node(split_delegate_per_node(Pids),  FPid),
    ok.

%%----------------------------------------------------------------------------

internal_call(Node, Thunk) when is_atom(Node) ->
    gen_server2:call({server(Node), Node}, {thunk, Thunk}, infinity).

internal_cast(Node, Thunk) when is_atom(Node) ->
    gen_server2:cast({server(Node), Node}, {thunk, Thunk}).

split_delegate_per_node(Pids) ->
    orddict:to_list(
      lists:foldl(
        fun (Pid, D) ->
                orddict:update(node(Pid),
                               fun (Pids1) -> [Pid | Pids1] end,
                               [Pid], D)
        end,
        orddict:new(), Pids)).

invoke_per_node([{Node, Pids}], FPid) when Node == node() ->
    local_delegate(Pids, FPid);
invoke_per_node(NodePids, FPid) ->
    lists:append(delegate_per_node(NodePids, FPid, fun internal_call/2)).

invoke_async_per_node([{Node, Pids}], FPid) when Node == node() ->
    local_delegate(Pids, FPid);
invoke_async_per_node(NodePids, FPid) ->
    delegate_per_node(NodePids, FPid, fun internal_cast/2),
    ok.

local_delegate(Pids, FPid) ->
    [safe_invoke(FPid, Pid) || Pid <- Pids].

delegate_per_node(NodePids, FPid, DelegateFun) ->
    Self = self(),
    [spawn(fun() ->
        Self ! {result, DelegateFun(Node,
                                    fun() -> local_delegate(Pids, FPid) end)}
    end) || {Node, Pids} <- NodePids],
    gather_results([], length(NodePids)).

gather_results(ResultsAcc, 0) ->
    ResultsAcc;

gather_results(ResultsAcc, ToGo) ->
    receive {result, Result} ->
        gather_results([Result | ResultsAcc], ToGo - 1)
    end.

server(Node) when is_atom(Node) ->
    case get({delegate_server_name, Node}) of
        undefined ->
            case rpc:call(Node, delegate, process_count, []) of
                {badrpc, _} ->
                    delegate_process_1; % Have to return something, if we're
                                        % just casting then we don't want to
                                        % blow up
                Count ->
                    Name = server(erlang:phash2(self(), Count)),
                    put({delegate_server_name, Node}, Name),
                    Name
            end;
        Name -> Name
    end;

server(Hash) ->
    list_to_atom("delegate_process_" ++ integer_to_list(Hash)).

safe_invoke(FPid, Pid) ->
    % We need the catch here for the local case. In the remote case there will
    % already have been a catch in handle_ca{ll,st} below, but that's OK, catch
    % is idempotent.
    case catch FPid(Pid) of
        {'EXIT', Reason} ->
            {error, {'EXIT', Reason}, Pid};
        Result ->
            {ok, Result, Pid}
    end.

process_count() ->
    ?DELEGATE_PROCESS_COUNT_MULTIPLIER * erlang:system_info(schedulers).

%%--------------------------------------------------------------------

init([]) ->
    {ok, no_state}.

handle_call({thunk, Thunk}, _From, State) ->
   {reply, catch Thunk(), State}.

handle_cast({thunk, Thunk}, State) ->
    catch Thunk(),
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%--------------------------------------------------------------------