Skip to content
Merged
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
24 changes: 22 additions & 2 deletions src/classy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ This MFA can contain calls to various @code{classy:on_...} functions.
, the_cluster/0
, node_sets/0
, prep_stop/0
, prep_stop/1
]).

-export([ on_node_init/2
, on_prep_stop/2
, on_create_cluster/2
, on_create_site/2
, on_peer_connection_change/2
Expand Down Expand Up @@ -286,6 +288,13 @@ node_of_site(Site, OnlyConnected) ->
{error, {unknown_node, Site}}
end.

-doc """
Equivalent to @code{prep_stop(shutdown)}.
""".
-spec prep_stop() -> ok.
prep_stop() ->
prep_stop(shutdown).

-doc """
This function can be called before the Erlang node shuts down.

Expand All @@ -294,8 +303,9 @@ without blocking the application controller.

This is helpful if changing the run level involves stopping or starting OTP applications.
""".
-spec prep_stop() -> ok.
prep_stop() ->
-spec prep_stop(term()) -> ok.
prep_stop(Reason) ->
classy_node:prep_stop(Reason),
classy_sup:prep_stop().

%%--------------------------------------------------------------------------------
Expand Down Expand Up @@ -501,6 +511,16 @@ and can be used to override the default cluster and site initialization logic.
on_node_init(Hook, Prio) ->
classy_hook:insert(?on_node_init, Hook, Prio).

-doc """
Register a hook that is executed before shutting down business applications.

It is called before classy application starts the shutdown procedure,
as well when the site leaves a cluster or heals from a network partition.
""".
Comment on lines +514 to +519
-spec on_prep_stop(fun((_Reason) -> _), classy_hook:prio()) -> classy_hook:hook().
on_prep_stop(Hook, Prio) ->
classy_hook:insert(?on_prep_stop, Hook, Prio).

-doc """
This callback is executed once per cluster by the site that originally creates the cluster.
""".
Expand Down
1 change: 1 addition & 0 deletions src/classy_hook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ safe_apply(HookPoint, Fun, Args) ->
#{ reason => Err
, hook => Fun
, hookpoint => HookPoint
, args => Args
}),
error
end.
1 change: 1 addition & 0 deletions src/classy_internal.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
-define(min_hook_prio, -?max_hook_prio).

-define(on_node_init, on_node_init).
-define(on_prep_stop, on_prep_stop).
-define(on_create_cluster, on_create_cluster).
-define(on_create_site, on_create_site).
-define(on_peer_connection_status_change, on_peer_connection_status_change).
Expand Down
9 changes: 7 additions & 2 deletions src/classy_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ safe_apply(Module, Function, Args) ->
-doc """
Apply a function in a separate process with a timeout.
""".
-spec safe_apply_with_timeout(callback(), timeout()) -> {ok, term()} | wrapped_exception() | {error, timeout}.
-spec safe_apply_with_timeout(callback(), timeout()) ->
{ok, term()} |
wrapped_exception() |
{error, {timeout, Info}} when
Info :: list() | undefined.
safe_apply_with_timeout(Callback, Timeout) ->
{Pid, MRef} = spawn_monitor(
fun() ->
Expand All @@ -117,9 +121,10 @@ safe_apply_with_timeout(Callback, Timeout) ->
{'DOWN', MRef, process, Pid, Reason} ->
Reason
after Timeout ->
Info = process_info(Pid, [current_stacktrace]),
demonitor(MRef, [flush]),
exit(Pid, kill),
{error, timeout}
{error, {timeout, Info}}
Comment on lines +124 to +127
end.

-doc """
Expand Down
16 changes: 11 additions & 5 deletions src/classy_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Management of the local site and node.
, peer_info/0
, node_of_site/2
, n_restarts/1
, prep_stop/1
]).

%% behavior callbacks:
Expand Down Expand Up @@ -290,7 +291,7 @@ terminate(Reason, _S) ->
}),
classy_table:flush(?globals),
classy_table:flush(?site_info),
sync_set_run_level(?stopped),
prep_stop(shutdown, infinity),
persistent_term:erase(?pt_node_sets),
persistent_term:erase(?pt_site_sets),
persistent_term:erase(?pt_site),
Expand Down Expand Up @@ -346,6 +347,10 @@ notify_mem_deltas(Cluster, Deltas) ->
, data = Deltas
}).

-doc false.
prep_stop(Reason) ->
classy_hook:foreach(?on_prep_stop, [Reason]).
Comment on lines +350 to +352

%%================================================================================
%% Internal functions
%%================================================================================
Expand Down Expand Up @@ -460,7 +465,7 @@ do_join_node(Node, Cluster, Remote, MemData, JoinIntent, S0) ->
end.

on_leave(S = #s{cluster = Cluster, site = Local}, Intent) ->
sync_set_run_level(?stopped),
prep_stop(leave, infinity),
%% Sync with the business apps:
classy_table:delete(?globals, ?the_cluster),
classy_hook:foreach(?on_post_kick, [Cluster, Local, Intent]),
Expand Down Expand Up @@ -570,8 +575,9 @@ start_old_clusters(Site) ->
end,
classy_membership:known_clusters(Site)).

sync_set_run_level(Level) ->
classy_rl_changer:set_sync(Level, infinity).
prep_stop(Reason, Timeout) ->
prep_stop(Reason),
classy_rl_changer:set_sync(?stopped, Timeout).

the_cluster() ->
case classy_table:lookup(?globals, ?the_cluster) of
Expand Down Expand Up @@ -620,7 +626,7 @@ apply_deltas_with_effects(Deltas, S0 = #s{cluster = Cluster, site = Local}) ->

-spec on_remote_restart(#s{}) -> {ok, #s{}}.
on_remote_restart(S) ->
classy_rl_changer:set_sync(?stopped, 120_000),
prep_stop(remote_restart, 120_000),
{ok, adjust_run_level(S)}.

-spec import_deltas(#{classy:site() => classy_membership:update()}, #s{}) ->
Expand Down
12 changes: 6 additions & 6 deletions test/classy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -405,18 +405,18 @@ t_061_run_level_timeouts(_) ->
?assertMatch(
[ %% Advance:
#{f := ?stopped, t := ?single}
, #{?snk_kind := ?classy_hook_failure, reason := {error, timeout}}
, #{?snk_kind := ?classy_hook_failure, reason := {error, {timeout, _}}}
, #{f := ?single, t := ?cluster}
, #{?snk_kind := ?classy_hook_failure, reason := {error, timeout}}
, #{?snk_kind := ?classy_hook_failure, reason := {error, {timeout, _}}}
, #{f := ?cluster, t := ?quorum}
, #{?snk_kind := ?classy_hook_failure, reason := {error, timeout}}
, #{?snk_kind := ?classy_hook_failure, reason := {error, {timeout, _}}}
%% Retard:
, #{f := ?quorum, t := ?cluster}
, #{?snk_kind := ?classy_hook_failure, reason := {error, timeout}}
, #{?snk_kind := ?classy_hook_failure, reason := {error, {timeout, _}}}
, #{f := ?cluster, t := ?single}
, #{?snk_kind := ?classy_hook_failure, reason := {error, timeout}}
, #{?snk_kind := ?classy_hook_failure, reason := {error, {timeout, _}}}
, #{f := ?single, t := ?stopped}
, #{?snk_kind := ?classy_hook_failure, reason := {error, timeout}}
, #{?snk_kind := ?classy_hook_failure, reason := {error, {timeout, _}}}
],
Events3)
end,
Expand Down
Loading