-
Notifications
You must be signed in to change notification settings - Fork 95
/
ra_server_sup_sup.erl
253 lines (234 loc) · 9.12 KB
/
ra_server_sup_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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2017-2022 VMware, Inc. or its affiliates. All rights reserved.
%%
%% @hidden
-module(ra_server_sup_sup).
-behaviour(supervisor).
-define(MUTABLE_CONFIG_KEYS,
[cluster_name,
metrics_key,
broadcast_time,
tick_timeout,
install_snap_rpc_timeout,
await_condition_timeout,
max_pipeline_count,
ra_event_formatter]).
%% API functions
-export([start_server/2,
restart_server/3,
stop_server/2,
delete_server/2,
remove_all/1,
start_link/1,
recover_config/2,
% for rpcs only
start_server_rpc/3,
restart_server_rpc/3,
delete_server_rpc/2,
prepare_server_stop_rpc/2]).
%% Supervisor callbacks
-export([init/1]).
-include("ra.hrl").
-spec start_server(System :: atom(), ra_server:ra_server_config()) ->
supervisor:startchild_ret() | {error, not_new | system_not_started} | {badrpc, term()}.
start_server(System, #{id := NodeId,
uid := UId} = Config)
when is_atom(System) ->
Node = ra_lib:ra_server_id_node(NodeId),
rpc:call(Node, ?MODULE, start_server_rpc, [System, UId, Config]).
-spec restart_server(atom(), ra_server_id(), ra_server:mutable_config()) ->
supervisor:startchild_ret() | {error, system_not_started} | {badrpc, term()}.
restart_server(System, {RaName, Node}, AddConfig) ->
rpc:call(Node, ?MODULE, restart_server_rpc,
[System, {RaName, Node}, AddConfig]).
start_server_rpc(System, UId, Config0) ->
case ra_system:fetch(System) of
undefined ->
{error, system_not_started};
SysCfg ->
Config = Config0#{system_config => SysCfg},
%% check that the server isn't already registered
case ra_directory:name_of(System, UId) of
undefined ->
case ra_system:lookup_name(System, server_sup) of
{ok, Name} ->
start_child(Name, Config);
Err ->
Err
end;
Name ->
case whereis(Name) of
undefined ->
{error, not_new};
Pid ->
{error, {already_started, Pid}}
end
end
end.
restart_server_rpc(System, {RaName, _Node}, AddConfig)
when is_atom(System) ->
case ra_system:fetch(System) of
undefined ->
{error, system_not_started};
SysCfg ->
case recover_config(System, RaName) of
{ok, Config0} ->
MutConfig = maps:with(?MUTABLE_CONFIG_KEYS, AddConfig),
{ok, Name} = ra_system:lookup_name(System, server_sup),
Config = case maps:merge(Config0, MutConfig) of
Config0 ->
%% the config has not changed
Config0#{system_config => SysCfg,
has_changed => false};
Config1 ->
Config1#{system_config => SysCfg,
has_changed => true}
end,
start_child(Name, Config);
Err ->
Err
end
end.
-spec stop_server(System :: atom(), RaNodeId :: ra_server_id()) ->
ok | {error, term()}.
stop_server(System, ServerId) when is_atom(System) ->
Node = ra_lib:ra_server_id_node(ServerId),
RaName = ra_lib:ra_server_id_to_local_name(ServerId),
Res = rpc:call(Node, ?MODULE,
prepare_server_stop_rpc, [System, RaName]),
case Res of
{error, _} = Err ->
Err;
{ok, Pid, SrvSup} when is_pid(Pid) ->
supervisor:terminate_child({SrvSup, Node}, Pid);
{ok, undefined, _} ->
%% no parent - no need to stop
ok;
Err ->
{error, Err}
end.
prepare_server_stop_rpc(System, RaName) ->
case ra_system:fetch(System) of
undefined ->
{error, system_not_started};
#{names := #{server_sup := SrvSup} = Names} ->
Parent = ra_directory:where_is_parent(Names, RaName),
{ok, Parent, SrvSup}
end.
-spec delete_server(atom(), NodeId :: ra_server_id()) ->
ok | {error, term()} | {badrpc, term()}.
delete_server(System, NodeId) when is_atom(System) ->
Node = ra_lib:ra_server_id_node(NodeId),
Name = ra_lib:ra_server_id_to_local_name(NodeId),
case stop_server(System, NodeId) of
ok ->
rpc:call(Node, ?MODULE, delete_server_rpc, [System, Name]);
{error, _} = Err -> Err
end.
delete_server_rpc(System, RaName) ->
case ra_system:fetch(System) of
undefined ->
{error, system_not_started};
#{data_dir := _SysDir,
names := #{log_meta := Meta,
server_sup := SrvSup} = Names} ->
?INFO("Deleting server ~w and its data directory.~n",
[RaName]),
%% TODO: better handle and report errors
UId = ra_directory:uid_of(Names, RaName),
Pid = ra_directory:where_is(Names, RaName),
ra_log_meta:delete(Meta, UId),
Dir = ra_env:server_data_dir(System, UId),
_ = supervisor:terminate_child(SrvSup, UId),
_ = delete_data_directory(Dir),
_ = ra_directory:unregister_name(Names, UId),
%% forcefully clean up ETS tables
catch ets:delete(ra_log_metrics, UId),
catch ets:delete(ra_log_snapshot_state, UId),
catch ets:delete(ra_metrics, RaName),
catch ets:delete(ra_state, RaName),
catch ets:delete(ra_open_file_metrics, Pid),
catch ra_counters:delete({RaName, node()}),
ok
end.
delete_data_directory(Directory) ->
DeleteFunction = fun() ->
try ra_lib:recursive_delete(Directory) of
ok ->
% moving on
ok
catch
_:_ = Err ->
?WARN("ra: delete_server/1 failed to delete directory ~ts~n"
"Error: ~p", [Directory, Err]),
error
end
end,
case DeleteFunction() of
ok ->
ok;
_ ->
spawn(fun() ->
ra_lib:retry(DeleteFunction, 2)
end)
end.
remove_all(System) when is_atom(System) ->
#{names := #{server_sup := Sup}} = ra_system:fetch(System),
_ = [begin
?DEBUG("ra: terminating child ~w in system ~ts~n", [Pid, System]),
supervisor:terminate_child(Sup, Pid)
end
|| {_, Pid, _, _} <- supervisor:which_children(Sup)],
ok.
recover_config(System, RaName) ->
case ra_directory:uid_of(System, RaName) of
undefined ->
{error, name_not_registered};
UId ->
Dir = ra_env:server_data_dir(System, UId),
case ra_directory:where_is(System, UId) of
Pid when is_pid(Pid) ->
case is_process_alive(Pid) of
true ->
{error, {already_started, Pid}};
false ->
ra_log:read_config(Dir)
end;
_ ->
% can it be made generic without already knowing the config state?
ra_log:read_config(Dir)
end
end.
-spec start_link(ra_system:config()) ->
{ok, pid()} | ignore | {error, term()}.
start_link(#{names := #{server_sup := Name}}) ->
supervisor:start_link({local, Name}, ?MODULE, []).
init([]) ->
SupFlags = #{strategy => simple_one_for_one},
ChildSpec = #{id => undefined,
type => supervisor,
restart => temporary,
start => {ra_server_sup, start_link, []}},
{ok, {SupFlags, [ChildSpec]}}.
start_child(Name, Config) ->
Ref = make_ref(),
case supervisor:start_child(Name, [Config#{reply_to => {Ref, self()}}]) of
{ok, Pid} ->
%% we have started the process now and have to wait for reply
%% that is sent after init but before state machine recovery
MRef = erlang:monitor(process, Pid),
receive
{Ref, ok} ->
_ = erlang:demonitor(MRef),
{ok, Pid};
{'DOWN', MRef, _, _, Reason} ->
?ERROR("Ra: failed to start ra server ~ts, err ~s",
[Name, Reason]),
{error, Reason}
end;
Err ->
Err
end.