-
Notifications
You must be signed in to change notification settings - Fork 58
/
partisan_sup.erl
137 lines (107 loc) · 4.04 KB
/
partisan_sup.erl
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
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2015 Helium Systems, Inc. All Rights Reserved.
%% Copyright (c) 2016 Christopher Meiklejohn. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (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.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(partisan_sup).
-behaviour(supervisor).
-include("partisan_logger.hrl").
-include("partisan.hrl").
-export([start_link/0]).
-export([init/1]).
-ifdef(TEST).
-define(CHILDREN, [?WORKER(partisan_test_server, [], permanent, 5000)]).
-else.
-define(CHILDREN, []).
-endif.
-define(SUPERVISOR(Id, Args, Restart, Timeout), #{
id => Id,
start => {Id, start_link, Args},
restart => Restart,
shutdown => Timeout,
type => supervisor,
modules => [Id]
}).
-define(WORKER(Id, Args, Restart, Timeout), #{
id => Id,
start => {Id, start_link, Args},
restart => Restart,
shutdown => Timeout,
type => worker,
modules => [Id]
}).
-define(EVENT_MANAGER(Id, Restart, Timeout), #{
id => Id,
start => {gen_event, start_link, [{local, Id}]},
restart => Restart,
shutdown => Timeout,
type => worker,
modules => [dynamic]
}).
%% =============================================================================
%% API
%% =============================================================================
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% =============================================================================
%% SUPERVISO CALLBACKS
%% =============================================================================
init([]) ->
%% Make sure we always call this function first.
partisan_config:init(),
Children = lists:flatten([
?WORKER(partisan_inet, [], permanent, 5000),
?WORKER(partisan_rpc_backend, [], permanent, 5000),
?WORKER(partisan_acknowledgement_backend, [], permanent, 5000),
?WORKER(partisan_orchestration_backend, [], permanent, 5000),
?SUPERVISOR(partisan_peer_service_sup, [], permanent, infinity),
%% THe peer service needs started before Plumtree servers
?WORKER(partisan_plumtree_backend, [], permanent, 5000),
?WORKER(partisan_plumtree_broadcast, [], permanent, 5000)
| ?CHILDREN
]),
%% Run a single backend for each label.
CausalLabels = partisan_config:get(causal_labels, []),
CausalBackendFun = fun(Label) ->
{partisan_causality_backend,
{partisan_causality_backend, start_link, [Label]},
permanent, 5000, worker, [partisan_causality_backend]}
end,
CausalBackends = lists:map(CausalBackendFun, CausalLabels),
%% Initialize the plumtree outstanding messages table
%% supervised by the supervisor.
%% The table is used by partisan_plumtree_broadcast and maps a nodename()
%% to set of outstanding messages. It uses a duplicate_bag to quickly
%% delete all messages for a nodename.
?PLUMTREE_OUTSTANDING = ets:new(
?PLUMTREE_OUTSTANDING,
[public, named_table, duplicate_bag, {read_concurrency, true}]
),
%% Open connection pool.
%% This MUST be the last child to be started
PoolSup = {
partisan_acceptor_socket_pool_sup,
{partisan_acceptor_socket_pool_sup, start_link, []},
permanent,
20000,
supervisor,
[partisan_acceptor_socket_pool_sup]
},
RestartStrategy = {one_for_one, 10, 10},
AllChildren = Children ++ CausalBackends ++ [PoolSup],
{ok, {RestartStrategy, AllChildren}}.