Skip to content

Commit

Permalink
Do not wait for CETS to be ready if RDBMS is ready first in mongoose_…
Browse files Browse the repository at this point in the history
…cluster_id
  • Loading branch information
arcusfelis committed Nov 14, 2023
1 parent e8a7845 commit 2c042f8
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/mongoose_cluster_id.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ start() ->
Backend = which_backend_available(),
IntBackend = which_volatile_backend_available(),
maybe_prepare_queries(Backend),
wait_for_any_backend(Backend, IntBackend),
CachedRes = get_cached_cluster_id(IntBackend),
BackendRes = get_backend_cluster_id(),
case {CachedRes, BackendRes} of
Expand All @@ -47,6 +48,44 @@ start() ->
{error, conflict}
end.


%% If RDBMS is available before CETS - it is enough for us to continue
%% the starting procedure
wait_for_any_backend(Backend, IntBackend) ->
Alias = erlang:alias([reply]),
[wait_for_backend_promise(B, Alias) || B <- lists:sort([Backend, IntBackend])],
wait_for_first_reply(Alias).

wait_for_first_reply(Alias) ->
receive
{ready, Alias} ->
ok
end.

wait_for_backend_promise(mnesia, Alias) ->
Alias ! {ready, Alias};
wait_for_backend_promise(cets, Alias) ->
spawn_link(fun() ->

Check warning on line 68 in src/mongoose_cluster_id.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_cluster_id.erl#L68

Added line #L68 was not covered by tests
%% We have to do it, because we want to read from across the cluster
%% in the start/0 function.
ok = cets_discovery:wait_for_ready(mongoose_cets_discovery, infinity),
Alias ! {ready, Alias}

Check warning on line 72 in src/mongoose_cluster_id.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_cluster_id.erl#L71-L72

Added lines #L71 - L72 were not covered by tests
end);
wait_for_backend_promise(rdbms, Alias) ->
spawn_link(fun() ->
wait_for_rdbms(),
Alias ! {ready, Alias}

Check warning on line 77 in src/mongoose_cluster_id.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_cluster_id.erl#L75-L77

Added lines #L75 - L77 were not covered by tests
end).

wait_for_rdbms() ->
case get_backend_cluster_id(rdbms) of

Check warning on line 81 in src/mongoose_cluster_id.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_cluster_id.erl#L81

Added line #L81 was not covered by tests
{ok, _} ->
ok;

Check warning on line 83 in src/mongoose_cluster_id.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_cluster_id.erl#L83

Added line #L83 was not covered by tests
_ ->
timer:sleep(100),
wait_for_rdbms()

Check warning on line 86 in src/mongoose_cluster_id.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_cluster_id.erl#L85-L86

Added lines #L85 - L86 were not covered by tests
end.

%% Get cached version
-spec get_cached_cluster_id() -> maybe_cluster_id().
get_cached_cluster_id() ->
Expand Down Expand Up @@ -102,10 +141,7 @@ init_cache(mnesia) ->
]);
init_cache(cets) ->
cets:start(cets_cluster_id, #{}),
cets_discovery:add_table(mongoose_cets_discovery, cets_cluster_id),
%% We have to do it, because we want to read from across the cluster
%% in the start/0 function.
ok = cets_discovery:wait_for_ready(mongoose_cets_discovery, infinity).
cets_discovery:add_table(mongoose_cets_discovery, cets_cluster_id).

Check warning on line 144 in src/mongoose_cluster_id.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_cluster_id.erl#L144

Added line #L144 was not covered by tests

-spec maybe_prepare_queries(mongoose_backend()) -> ok.
maybe_prepare_queries(mnesia) -> ok;
Expand Down

0 comments on commit 2c042f8

Please sign in to comment.