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

Don't set cluster_id if not needed #3112

Merged
merged 2 commits into from
May 14, 2021
Merged
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
46 changes: 23 additions & 23 deletions src/mongoose_cluster_id.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@
-spec start() -> maybe_cluster_id().
start() ->
init_mnesia_cache(),
maybe_prepare_queries(which_backend_available()),
run_steps(
[fun get_cached_cluster_id/0,
fun get_backend_cluster_id/0,
fun make_and_set_new_cluster_id/0],
{error, no_cluster_id_yet}).

-spec run_steps(ListOfFuns, MaybeCID) -> maybe_cluster_id() when
ListOfFuns :: [fun(() -> maybe_cluster_id())],
MaybeCID :: maybe_cluster_id().
run_steps(_, {ok, ID}) when is_binary(ID) ->
store_cluster_id(ID);
run_steps([Fun | NextFuns], {error, _}) ->
run_steps(NextFuns, Fun());
run_steps([], {error, _} = E) ->
E.
Backend = which_backend_available(),
maybe_prepare_queries(Backend),
CachedRes = get_cached_cluster_id(),
BackendRes = get_backend_cluster_id(),
case {CachedRes, BackendRes} of
{{ok, ID}, {ok, ID}} ->
{ok, ID};
{{ok, ID}, {error, _}} ->
set_new_cluster_id(ID, Backend);
{{error, _}, {ok, ID}} ->
set_new_cluster_id(ID, mnesia);
{{error, _}, {error, _}} ->
make_and_set_new_cluster_id();
{{ok, CachedID}, {ok, BackendID}} ->
?LOG_ERROR(#{what => cluster_id_setup_conflict,
text => <<"Mnesia and Backend have different cluster IDs">>,
cached_id => CachedID, backend_id => BackendID}),
{error, conflict}
end.

%% Get cached version
-spec get_cached_cluster_id() -> maybe_cluster_id().
Expand Down Expand Up @@ -103,15 +106,12 @@ which_backend_available() ->
_ -> rdbms
end.

-spec store_cluster_id(cluster_id()) -> maybe_cluster_id().
store_cluster_id(ID) ->
which_backend_available() == rdbms andalso set_new_cluster_id(ID, rdbms),
set_new_cluster_id(ID, mnesia).

-spec set_new_cluster_id(cluster_id(), mongoose_backend()) -> ok | {error, any()}.
set_new_cluster_id(ID, rdbms) ->
try execute_cluster_insert_new(ID) of
{updated, 1} -> {ok, ID}
{updated, 1} ->
set_new_cluster_id(ID, mnesia),
{ok, ID}
catch
Class:Reason:Stacktrace ->
?LOG_WARNING(#{what => cluster_id_set_failed,
Expand All @@ -133,7 +133,7 @@ set_new_cluster_id(ID, mnesia) ->
-spec get_backend_cluster_id(mongoose_backend()) -> maybe_cluster_id().
get_backend_cluster_id(rdbms) ->
try mongoose_rdbms:execute_successfully(global, cluster_select, []) of
{selected, [{Row}]} -> {ok, Row};
{selected, [{ID}]} -> {ok, ID};
{selected, []} -> {error, no_value_in_backend}
catch
Class:Reason:Stacktrace ->
Expand Down