Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cets_discovery:wait_for_get_nodes/2 #42

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 26 additions & 6 deletions src/cets_discovery.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
get_tables/1,
info/1,
system_info/1,
wait_for_ready/2
wait_for_ready/2,
wait_for_get_nodes/2
]).
-export([
init/1,
Expand All @@ -55,6 +56,7 @@
info/1,
system_info/1,
wait_for_ready/2,
wait_for_get_nodes/2,
behaviour_info/1
]).

Expand Down Expand Up @@ -91,6 +93,7 @@
should_retry_join := boolean(),
timer_ref := reference() | undefined,
pending_wait_for_ready := [gen_server:from()],
pending_wait_for_get_nodes := [gen_server:from()],
nodeup_timestamps := #{node() => milliseconds()},
nodedown_timestamps := #{node() => milliseconds()},
node_start_timestamps := #{node() => milliseconds()},
Expand Down Expand Up @@ -146,14 +149,24 @@ info(Server) ->
system_info(Server) ->
gen_server:call(Server, system_info).

%% This calls blocks until the initial discovery is done
%% It also waits till the data is loaded from the remote nodes
%% This calls blocks until the initial discovery is done.
%% It also waits till the data is loaded from the remote nodes.
-spec wait_for_ready(server(), timeout()) -> ok.
wait_for_ready(Server, Timeout) ->
F = fun() -> gen_server:call(Server, wait_for_ready, Timeout) end,
Info = #{task => cets_wait_for_ready},
cets_long:run_tracked(Info, F).

%% Waits for the currect get_nodes call to return.
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
%% Just returns if there is no gen_nodes call running.
%% It is different from wait_for_ready, because it does not wait for
%% unavailable nodes to return pang.
-spec wait_for_get_nodes(server(), timeout()) -> ok.
wait_for_get_nodes(Server, Timeout) ->
F = fun() -> gen_server:call(Server, wait_for_get_nodes, Timeout) end,
Info = #{task => cets_wait_for_get_nodes},
cets_long:run_tracked(Info, F).

-spec init(term()) -> {ok, state()}.
init(Opts) ->
StartTime = erlang:system_time(millisecond),
Expand Down Expand Up @@ -181,6 +194,7 @@ init(Opts) ->
should_retry_join => false,
timer_ref => undefined,
pending_wait_for_ready => [],
pending_wait_for_get_nodes => [],
nodeup_timestamps => #{},
node_start_timestamps => #{},
nodedown_timestamps => #{},
Expand All @@ -198,6 +212,10 @@ handle_call(system_info, _From, State) ->
{reply, handle_system_info(State), State};
handle_call(wait_for_ready, From, State = #{pending_wait_for_ready := Pending}) ->
{noreply, trigger_verify_ready(State#{pending_wait_for_ready := [From | Pending]})};
handle_call(wait_for_get_nodes, _From, State = #{get_nodes_status := not_running}) ->
{reply, ok, State};
handle_call(wait_for_get_nodes, From, State = #{pending_wait_for_get_nodes := Pending}) ->
{noreply, State#{pending_wait_for_get_nodes := [From | Pending]}};
handle_call(Msg, From, State) ->
?LOG_ERROR(#{what => unexpected_call, msg => Msg, from => From}),
{reply, {error, unexpected_call}, State}.
Expand Down Expand Up @@ -279,10 +297,12 @@ handle_check(State = #{backend_module := Mod, backend_state := BackendState}) ->

-spec handle_get_nodes_result(Result, BackendState, State) -> State when
Result :: get_nodes_result(), BackendState :: backend_state(), State :: state().
handle_get_nodes_result(Result, BackendState, State) ->
handle_get_nodes_result(Result, BackendState, State = #{pending_wait_for_get_nodes := Pending}) ->
[gen_server:reply(From, ok) || From <- Pending],
State2 = State#{
backend_state := BackendState,
get_nodes_status := not_running,
pending_wait_for_get_nodes := [],
last_get_nodes_result := Result
},
State3 = set_nodes(Result, State2),
Expand Down Expand Up @@ -486,7 +506,7 @@ handle_nodedown(Node, State) ->
set_defined(connected_millisecond_duration, NodeUpTime, #{
what => nodedown,
remote_node => Node,
alive_nodes => length(nodes()) + 1,
connected_nodes => length(nodes()) + 1,
time_since_startup_in_milliseconds => time_since_startup_in_milliseconds(State)
})
),
Expand All @@ -501,7 +521,7 @@ handle_nodeup(Node, State) ->
set_defined(downtime_millisecond_duration, NodeDownTime, #{
what => nodeup,
remote_node => Node,
alive_nodes => length(nodes()) + 1,
connected_nodes => length(nodes()) + 1,
%% We report that time so we could work on minimizing that time.
%% It says how long it took to discover nodes after startup.
time_since_startup_in_milliseconds => time_since_startup_in_milliseconds(State)
Expand Down
41 changes: 38 additions & 3 deletions test/cets_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ cases() ->
status_remote_nodes_with_unknown_tables,
status_remote_nodes_with_missing_nodes,
status_conflict_nodes,
disco_wait_for_get_nodes_works,
disco_wait_for_get_nodes_blocks_and_returns,
get_nodes_request,
test_locally,
handle_down_is_called,
Expand Down Expand Up @@ -1682,6 +1684,39 @@ status_conflict_nodes(Config) ->
fun() -> maps:get(conflict_tables, cets_status:status(DiscoName)) end, [Tab2]
).

disco_wait_for_get_nodes_works(_Config) ->
F = fun(State) -> {{ok, []}, State} end,
{ok, Disco} = cets_discovery:start(#{backend_module => cets_discovery_fun, get_nodes_fn => F}),
ok = cets_discovery:wait_for_get_nodes(Disco, 5000).

disco_wait_for_get_nodes_blocks_and_returns(Config) ->
Tab = make_name(Config, 1),
{ok, _Pid} = start_local(Tab, #{}),
SignallingPid = spawn_link(fun() ->
receive
stop -> ok
end
end),
F = fun(State) ->
wait_for_down(SignallingPid),
{{ok, []}, State}
end,
{ok, Disco} = cets_discovery:start(#{backend_module => cets_discovery_fun, get_nodes_fn => F}),
cets_discovery:add_table(Disco, Tab),
%% Enter into a blocking get_nodes function
Disco ! check,
%% Do it async, because it would block is
WaitPid = spawn_link(fun() -> ok = cets_discovery:wait_for_get_nodes(Disco, 5000) end),
Cond = fun() ->
length(maps:get(pending_wait_for_get_nodes, cets_discovery:system_info(Disco)))
end,
cets_test_wait:wait_until(Cond, 1),
%% Unblock get_nodes call
SignallingPid ! stop,
%% wait_for_get_nodes returns
wait_for_down(WaitPid),
ok.

get_nodes_request(Config) ->
#{ct2 := Node2, ct3 := Node3, ct4 := Node4} = proplists:get_value(nodes, Config),
Tab = make_name(Config),
Expand Down Expand Up @@ -2296,7 +2331,7 @@ disco_logs_nodeup(Config) ->
meta := #{pid := Disco},
msg := {report, #{what := nodeup, remote_node := Node2} = R}
}} = M ->
?assert(is_integer(maps:get(alive_nodes, R)), M),
?assert(is_integer(maps:get(connected_nodes, R)), M),
?assert(is_integer(maps:get(time_since_startup_in_milliseconds, R)), M)
after 5000 ->
ct:fail(timeout)
Expand All @@ -2318,7 +2353,7 @@ disco_logs_nodedown(Config) ->
meta := #{pid := Disco},
msg := {report, #{what := nodedown, remote_node := Node2} = R}
}} = M ->
?assert(is_integer(maps:get(alive_nodes, R)), M),
?assert(is_integer(maps:get(connected_nodes, R)), M),
?assert(is_integer(maps:get(time_since_startup_in_milliseconds, R)), M),
?assert(is_integer(maps:get(connected_millisecond_duration, R)), M)
after 5000 ->
Expand Down Expand Up @@ -2349,7 +2384,7 @@ disco_logs_nodeup_after_downtime(Config) ->
downtime_millisecond_duration := Downtime
} = R}
}} = M ->
?assert(is_integer(maps:get(alive_nodes, R)), M),
?assert(is_integer(maps:get(connected_nodes, R)), M),
?assert(is_integer(Downtime), M)
after 5000 ->
ct:fail(timeout)
Expand Down