Skip to content

Michal/mnesia/backup and fallback on ext backend/otp-19383 #9152

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

Closed
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
21 changes: 16 additions & 5 deletions lib/mnesia/src/mnesia_bup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ iterate(Mod, Fun, Opaque, Acc) ->
R = #restore{bup_module = Mod, bup_data = Opaque},
try read_schema_section(R) of
{R2, {Header, Schema, Rest}} ->
Ext = get_ext_types(Schema),
Ext = get_ext_types(Schema),
case mnesia:system_info(is_running) of
Status when Status == yes orelse Status == starting ->
dbg_out("Status is ~p, initializing external backends.~n", [Status]),
Backends = mnesia_schema:get_backends_to_initialize(Ext),
mnesia_schema:init_backends(Backends);
Status ->
%% Do not initialize external backends if the system is not running
%% We can land here, if system is stopped and we're creating a schema
%% this triggers an implicit backup and fallback install
dbg_out("Status is ~p, skipping external backends initialization.~n", [Status])
end,
try iter(R2, Header, Schema, Ext, Fun, Acc, Rest) of
{ok, R3, Res} ->
close_read(R3),
Expand Down Expand Up @@ -745,12 +756,12 @@ do_fallback_start(true, false) ->
?SAFE(dets:close(schema)),
TmpSchema = mnesia_lib:tab2tmp(schema),
DatSchema = mnesia_lib:tab2dat(schema),
AllLT = ?ets_match_object(LocalTabs, '_'),
?ets_delete_table(LocalTabs),
AllLT = ?ets_match_object(LocalTabs, '_'),
?ets_delete_table(LocalTabs),
case file:rename(TmpSchema, DatSchema) of
ok ->
[(LT#local_tab.swap)(LT#local_tab.name, LT) ||
LT <- AllLT, LT#local_tab.name =/= schema],
[(LT#local_tab.swap)(LT#local_tab.name, LT) ||
LT <- AllLT, LT#local_tab.name =/= schema],
file:delete(BupFile),
ok;
{error, Reason} ->
Expand Down
2 changes: 2 additions & 0 deletions lib/mnesia/src/mnesia_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ validate_record(Tab, Obj) ->
%%
%% dir -> directory path (**)
%% mnesia_status -> status | running | stopping (**)
%% ext_backends -> module list of initialized ext backends (**)
%% (**) == (Different on all nodes)
%%

Expand All @@ -458,6 +459,7 @@ other_val_1(Var) ->
{_, where_to_read} -> nowhere;
{_, where_to_write} -> [];
{_, active_replicas} -> [];
ext_backends -> [];
_ -> error
end.

Expand Down
27 changes: 21 additions & 6 deletions lib/mnesia/src/mnesia_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
info/1,
init/1,
init_backends/0,
init_backends/1,
get_backends_to_initialize/1,
insert_cstruct/3,
is_remote_member/1,
list2cs/1,
Expand Down Expand Up @@ -161,13 +163,22 @@ init(IgnoreFallback) ->
mnesia_controller:add_active_replica(schema, node()),
init_backends().


init_backends() ->
Backends = lists:foldl(fun({Alias, Mod}, Acc) ->
orddict:append(Mod, Alias, Acc)
end, orddict:new(), get_ext_types()),
[init_backend(Mod, Aliases) || {Mod, Aliases} <- Backends],
ok.
Backends = get_backends_to_initialize(get_ext_types()),
init_backends(Backends).
init_backends([]) ->
ok;
init_backends([{Mod, Aliases} = Backend | Rest]) ->
Mods = mnesia_lib:val(ext_backends),
case lists:member(Mod, Mods) of
true ->
dbg_out("Backend ~p is already initialized~n", [Backend]);
false ->
init_backend(Mod, Aliases),
mnesia_lib:set(ext_backends, [Mod | Mods]),
dbg_out("Backend ~p initialized~n", [Backend])
end,
init_backends(Rest).

init_backend(Mod, [_|_] = Aliases) ->
case Mod:init_backend() of
Expand All @@ -177,6 +188,10 @@ init_backend(Mod, [_|_] = Aliases) ->
mnesia:abort({backend_init_error, Error})
end.

get_backends_to_initialize(Ext) ->
lists:foldl(fun({Alias, Mod}, Acc) ->
orddict:append(Mod, Alias, Acc) end, orddict:new(), Ext).

exit_on_error({error, Reason}) ->
exit(Reason);
exit_on_error(GoodRes) ->
Expand Down
3 changes: 2 additions & 1 deletion lib/mnesia/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MODULES= \
mnesia_cost \
mnesia_dbn_meters \
ext_test \
ext_test_server \
mnesia_index_plugin_test \
mnesia_external_backend_test

Expand All @@ -76,7 +77,7 @@ ExampleModules = \

ERL_FILES= $(MODULES:%=%.erl) $(DocExampleModules:%=$(DocExamplesDir)/%.erl) $(ExampleModules:%=$(ExamplesDir)/%.erl)

HRL_FILES= mnesia_test_lib.hrl $(DocExamplesHrl:%=$(DocExamplesDir)/%)
HRL_FILES= mnesia_test_lib.hrl ext_test_server.hrl $(DocExamplesHrl:%=$(DocExamplesDir)/%)

TARGET_FILES= \
$(MODULES:%=$(EBIN)/%.$(EMULATOR)) $(DocExampleModules:%=$(EBIN)/%.$(EMULATOR)) $(ExampleModules:%=$(EBIN)/%.$(EMULATOR))
Expand Down
Loading
Loading