Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`onRunTrackedJs`, `onGetTrackedObjects` and `onTrackedObjectDelete` hooks, which embedders may
override to customize what tracking means
- Added `string:to_integer/1`
- Added support for `rest_for_one` and `simple_one_for_one` supervisor restart strategies

### Changed
- `erlang:process_info/2` now accepts only pids of local processes, as Erlang/OTP does:
Expand Down
184 changes: 152 additions & 32 deletions libs/estdlib/src/supervisor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
%% Caveats:
%% <ul>
%% <li>Support only for locally named procs</li>
%% <li>No support for simple_one_for_one or one_for_rest strategies</li>
%% <li>No support for hibernate</li>
%% <li>No support for automatic shutdown</li>
%% </ul>
Expand Down Expand Up @@ -81,7 +80,7 @@
-type child_id() :: term().
-type child() :: undefined | pid().

-type strategy() :: one_for_all | one_for_one.
-type strategy() :: one_for_all | one_for_one | rest_for_one | simple_one_for_one.
-type sup_flags() ::
#{
strategy => strategy(),
Expand Down Expand Up @@ -138,7 +137,8 @@
period = ?DEFAULT_PERIOD :: pos_integer(),
restart_count = 0 :: non_neg_integer(),
restarts = [] :: [integer()],
children = [] :: [#child{}]
children = [] :: [#child{}],
template = undefined :: #child{} | undefined
}).

%% Used to trim stale restarts when the 'intensity' value is large.
Expand All @@ -156,23 +156,26 @@ start_link(Module, Args) ->
start_link(SupName, Module, Args) ->
gen_server:start_link(SupName, ?MODULE, {Module, Args}, []).

-spec start_child(Supervisor :: sup_ref(), ChildSpec :: child_spec()) -> startchild_ret().
-spec start_child(Supervisor :: sup_ref(), ChildSpec :: child_spec() | [term()]) ->
startchild_ret().
start_child(Supervisor, ChildSpec) ->
gen_server:call(Supervisor, {start_child, ChildSpec}, infinity).

-spec terminate_child(Supervisor :: sup_ref(), ChildId :: any()) -> ok | {error, not_found}.
%% ChildId is a pid() for simple_one_for_one, a child id otherwise.
-spec terminate_child(Supervisor :: sup_ref(), ChildId :: any()) ->
ok | {error, not_found | simple_one_for_one}.
terminate_child(Supervisor, ChildId) ->
gen_server:call(Supervisor, {terminate_child, ChildId}, infinity).

-spec restart_child(Supervisor :: sup_ref(), ChildId :: any()) ->
{ok, Child :: child()}
| {ok, Child :: child(), Info :: term()}
| {error, Reason :: running | restarting | not_found | term()}.
| {error, Reason :: running | restarting | not_found | simple_one_for_one | term()}.
restart_child(Supervisor, ChildId) ->
gen_server:call(Supervisor, {restart_child, ChildId}, infinity).

-spec delete_child(Supervisor :: sup_ref(), ChildId :: any()) ->
ok | {error, Reason :: running | restarting | not_found}.
ok | {error, Reason :: running | restarting | not_found | simple_one_for_one}.
delete_child(Supervisor, ChildId) ->
gen_server:call(Supervisor, {delete_child, ChildId}, infinity).

Expand Down Expand Up @@ -202,32 +205,42 @@ count_children(Supervisor) ->
-spec init({Mod :: module(), Args :: [any()]}) ->
{ok, State :: #state{}}
| {stop, {bad_return, {Mod :: module(), init, Reason :: term()}}}
| {stop, {bad_start_spec, StartSpec :: term()}}
| {stop, {shutdown, {failed_to_start_child, ChildId :: term(), Reason :: term()}}}.
init({Mod, Args}) ->
erlang:process_flag(trap_exit, true),
case Mod:init(Args) of
{ok, {{Strategy, Intensity, Period}, StartSpec}} ->
State = init_state(StartSpec, #state{
finalize_init(StartSpec, #state{
restart_strategy = Strategy,
intensity = Intensity,
period = Period
}),
init_start_children(State);
});
{ok, {#{} = SupSpec, StartSpec}} ->
Strategy = maps:get(strategy, SupSpec, one_for_one),
Intensity = maps:get(intensity, SupSpec, ?DEFAULT_INTENSITY),
Period = maps:get(period, SupSpec, ?DEFAULT_PERIOD),
State = init_state(StartSpec, #state{
finalize_init(StartSpec, #state{
restart_strategy = Strategy,
intensity = Intensity,
period = Period
}),
init_start_children(State);
});
Error ->
% TODO: log supervisor init failure
{stop, {bad_return, {Mod, init, Error}}}
end.

finalize_init(StartSpec, #state{restart_strategy = simple_one_for_one} = State) ->
case StartSpec of
[Spec] ->
Template = child_spec_to_record(Spec),
{ok, State#state{template = Template, children = []}};
_ ->
{stop, {bad_start_spec, StartSpec}}
end;
finalize_init(StartSpec, State) ->
init_start_children(init_state(StartSpec, State)).

init_start_children(State) ->
case start_children(State#state.children, []) of
{ok, NewChildren} ->
Expand Down Expand Up @@ -300,6 +313,58 @@ start_children([], StartedC) ->
{ok, StartedC}.

% @hidden
handle_call(
{start_child, ExtraArgs},
_From,
#state{restart_strategy = simple_one_for_one, template = Template, children = Children} = State
) ->
#child{start = {M, F, A}} = Template,
NewChild = Template#child{id = make_ref(), start = {M, F, A ++ ExtraArgs}},
case try_start(NewChild) of
{ok, undefined, Result} ->
{reply, Result, State};
{ok, Pid, Result} ->
{reply, Result, State#state{children = [NewChild#child{pid = Pid} | Children]}};
{error, _Reason} = ErrorT ->
{reply, ErrorT, State}
end;
handle_call(
{terminate_child, Pid}, From, #state{restart_strategy = simple_one_for_one} = State
) when
is_pid(Pid)
->
case lists:keyfind(Pid, #child.pid, State#state.children) of
#child{} = Child ->
do_terminate(Child),
%% Mark as terminating so handle_child_exit replies on the 'EXIT'.
NewChild = Child#child{restart = {terminating, temporary, From}},
NewChildren = lists:keyreplace(Pid, #child.pid, State#state.children, NewChild),
{noreply, State#state{children = NewChildren}};
false ->
%% A failed restart is parked as {restarting, Pid}; cancel the retry.
case lists:keyfind({restarting, Pid}, #child.pid, State#state.children) of
#child{} ->
NewChildren = lists:keydelete(
{restarting, Pid}, #child.pid, State#state.children
),
{reply, ok, State#state{children = NewChildren}};
false ->
{reply, {error, not_found}, State}
end
end;
handle_call({terminate_child, _Id}, _From, #state{restart_strategy = simple_one_for_one} = State) ->
{reply, {error, simple_one_for_one}, State};
handle_call({restart_child, _Id}, _From, #state{restart_strategy = simple_one_for_one} = State) ->
{reply, {error, simple_one_for_one}, State};
handle_call({delete_child, _Id}, _From, #state{restart_strategy = simple_one_for_one} = State) ->
{reply, {error, simple_one_for_one}, State};
handle_call(
which_children,
_From,
#state{restart_strategy = simple_one_for_one, children = Children} = State
) ->
ChildrenInfo = [setelement(1, child_to_info(C), undefined) || C <- Children],
{reply, ChildrenInfo, State};
handle_call({start_child, ChildSpec}, _From, #state{children = Children} = State) ->
Child = child_spec_to_record(ChildSpec),
#child{id = ID} = Child,
Expand Down Expand Up @@ -369,8 +434,13 @@ handle_call(which_children, _From, #state{children = Children} = State) ->
ChildrenInfo = lists:map(fun child_to_info/1, Children),
{reply, ChildrenInfo, State};
handle_call(count_children, _From, #state{children = Children} = State) ->
{Specs, Active, Supers, Workers} =
{Specs0, Active, Supers, Workers} =
lists:foldl(fun count_child/2, {0, 0, 0, 0}, Children),
Specs =
case State#state.restart_strategy of
simple_one_for_one -> 1;
_ -> Specs0
end,
Reply = [{specs, Specs}, {active, Active}, {supervisors, Supers}, {workers, Workers}],
{reply, Reply, State}.

Expand Down Expand Up @@ -414,6 +484,13 @@ handle_info({try_again_restart, Id}, State) ->
case add_restart(State) of
{ok, State1} ->
case try_start(Child) of
{ok, undefined, _Result} when
State1#state.restart_strategy =:= simple_one_for_one
->
UpdatedChildren = lists:keydelete(
Id, #child.id, State1#state.children
),
{noreply, State1#state{children = UpdatedChildren}};
{ok, NewPid, _Result} ->
UpdatedChildren = lists:keyreplace(
Id, #child.id, State1#state.children, Child#child{pid = NewPid}
Expand Down Expand Up @@ -478,23 +555,10 @@ handle_child_exit(Pid, Reason, State) ->
end
end.

handle_restart_strategy(
#child{id = Id} = Child, #state{restart_strategy = one_for_one} = State
) ->
case try_start(Child) of
{ok, NewPid, _Result} ->
NewChild = Child#child{pid = NewPid},
Children = lists:keyreplace(
Id, #child.id, State#state.children, NewChild
),
{noreply, State#state{children = Children}};
{error, _} ->
NewChild = Child#child{pid = {restarting, Child#child.pid}},
Children = lists:keyreplace(
Id, #child.id, State#state.children, NewChild
),
{noreply, State#state{children = Children}, {timeout, 0, {try_again_restart, Id}}}
end;
handle_restart_strategy(#child{} = Child, #state{restart_strategy = one_for_one} = State) ->
restart_one(Child, State);
handle_restart_strategy(#child{} = Child, #state{restart_strategy = simple_one_for_one} = State) ->
restart_simple_one(Child, State);
handle_restart_strategy(
#child{pid = Pid} = Child, #state{restart_strategy = one_for_all} = State
) ->
Expand All @@ -511,7 +575,63 @@ handle_restart_strategy(
{ok, NewChildren} = get_restart_children(Children),
%% NewChildren is startup order (first at head) and needs to be reversed to keep Children in correct order in #state{}
{noreply, State#state{children = lists:reverse(NewChildren)},
{timeout, 0, {restart_many_children, NewChildren}}}.
{timeout, 0, {restart_many_children, NewChildren}}};
handle_restart_strategy(
#child{pid = Pid} = Child, #state{restart_strategy = rest_for_one} = State
) ->
{Affected0, Rest} = split_affected_rest(Pid, State#state.children),
Affected1 =
case Pid of
{restarting, _} ->
Affected0;
Pid when is_pid(Pid) ->
lists:keyreplace(Pid, #child.pid, Affected0, Child#child{
pid = {restarting, Pid}
})
end,
ok = terminate_one_for_all(Affected1),
{ok, NewAffected} = get_restart_children(Affected1),
{noreply, State#state{children = lists:reverse(NewAffected) ++ Rest},
{timeout, 0, {restart_many_children, NewAffected}}}.

split_affected_rest(Pid, Children) ->
split_affected_rest(Pid, Children, []).

split_affected_rest(Pid, [#child{pid = Pid} = Child | Rest], Acc) ->
{lists:reverse([Child | Acc]), Rest};
split_affected_rest(Pid, [Child | Tail], Acc) ->
split_affected_rest(Pid, Tail, [Child | Acc]);
split_affected_rest(_Pid, [], Acc) ->
{lists:reverse(Acc), []}.

restart_one(#child{id = Id} = Child, State) ->
case try_start(Child) of
{ok, NewPid, _Result} ->
NewChild = Child#child{pid = NewPid},
Children = lists:keyreplace(Id, #child.id, State#state.children, NewChild),
{noreply, State#state{children = Children}};
{error, _} ->
NewChild = Child#child{pid = {restarting, Child#child.pid}},
Children = lists:keyreplace(Id, #child.id, State#state.children, NewChild),
{noreply, State#state{children = Children}, {timeout, 0, {try_again_restart, Id}}}
end.

%% Unlike a static child, a dynamic one that returns ignore is dropped rather
%% than kept with pid = undefined.
restart_simple_one(#child{id = Id} = Child, State) ->
case try_start(Child) of
{ok, undefined, _Result} ->
Children = lists:keydelete(Id, #child.id, State#state.children),
{noreply, State#state{children = Children}};
{ok, NewPid, _Result} ->
NewChild = Child#child{pid = NewPid},
Children = lists:keyreplace(Id, #child.id, State#state.children, NewChild),
{noreply, State#state{children = Children}};
{error, _} ->
NewChild = Child#child{pid = {restarting, Child#child.pid}},
Children = lists:keyreplace(Id, #child.id, State#state.children, NewChild),
{noreply, State#state{children = Children}, {timeout, 0, {try_again_restart, Id}}}
end.

should_restart(_Reason, permanent) ->
true;
Expand Down
Loading
Loading