Skip to content

Commit

Permalink
Merge pull request #4138 from esl/mongoose-mnesia-helper
Browse files Browse the repository at this point in the history
Raise an error if mnesia:create_table/2 fails
  • Loading branch information
chrzaszcz authored Oct 9, 2023
2 parents 9bbbbe2 + 40e3f51 commit 507fcd5
Show file tree
Hide file tree
Showing 29 changed files with 166 additions and 176 deletions.
7 changes: 3 additions & 4 deletions src/auth/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@
-spec start(mongooseim:host_type()) -> ok.
start(HostType) ->
%% TODO: Check cluster mode
mnesia:create_table(anonymous, [{ram_copies, [node()]},
{type, bag},
{attributes, record_info(fields, anonymous)}]),
mnesia:add_table_copy(anonymous, node(), ram_copies),
mongoose_mnesia:create_table(anonymous,
[{ram_copies, [node()]}, {type, bag},
{attributes, record_info(fields, anonymous)}]),
%% The hooks are needed to add / remove users from the anonymous tables
gen_hook:add_handlers(hooks(HostType)),
ok.
Expand Down
17 changes: 7 additions & 10 deletions src/auth/ejabberd_auth_internal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,13 @@

-spec start(HostType :: mongooseim:host_type()) -> ok.
start(HostType) ->
mnesia:create_table(passwd, [{disc_copies, [node()]},
{attributes, record_info(fields, passwd)},
{storage_properties,
[{ets, [{read_concurrency, true}]}]}
]),
mnesia:create_table(reg_users_counter,
[{ram_copies, [node()]},
{attributes, record_info(fields, reg_users_counter)}]),
mnesia:add_table_copy(passwd, node(), disc_copies),
mnesia:add_table_copy(reg_users_counter, node(), ram_copies),
mongoose_mnesia:create_table(passwd,
[{disc_copies, [node()]},
{attributes, record_info(fields, passwd)},
{storage_properties, [{ets, [{read_concurrency, true}]}]}]),
mongoose_mnesia:create_table(reg_users_counter,
[{ram_copies, [node()]},
{attributes, record_info(fields, reg_users_counter)}]),
update_reg_users_counter_table(HostType),
ok.

Expand Down
9 changes: 5 additions & 4 deletions src/component/mongoose_component_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
init(_) ->
update_tables(),
%% add distributed service_component routes
mnesia:create_table(external_component,
[{attributes, record_info(fields, external_component)},
{type, bag}, {ram_copies, [node()]}]),
mnesia:add_table_copy(external_component, node(), ram_copies).
mongoose_mnesia:create_table(external_component,
[{type, bag},
{ram_copies, [node()]},
{attributes,
record_info(fields, external_component)}]).

update_tables() ->
%% delete old schema
Expand Down
9 changes: 4 additions & 5 deletions src/ejabberd_sm_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@

-spec init(map()) -> any().
init(_Opts) ->
mnesia:create_table(session,
[{ram_copies, [node()]},
{attributes, record_info(fields, session)}]),
mongoose_mnesia:create_table(session,
[{ram_copies, [node()]},
{attributes, record_info(fields, session)}]),
mnesia:add_table_index(session, usr),
mnesia:add_table_index(session, us),
mnesia:add_table_copy(session, node(), ram_copies).
mnesia:add_table_index(session, us).

-spec get_sessions() -> [ejabberd_sm:session()].
get_sessions() ->
Expand Down
8 changes: 3 additions & 5 deletions src/event_pusher/mod_event_pusher_push_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@

-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init(_HostType, _Opts) ->
mnesia:create_table(push_subscription,
[{disc_copies, [node()]},
{type, bag},
{attributes, record_info(fields, push_subscription)}]),
mnesia:add_table_copy(push_subscription, node(), disc_copies),
mongoose_mnesia:create_table(push_subscription,
[{disc_copies, [node()]}, {type, bag},
{attributes, record_info(fields, push_subscription)}]),
ok.

-spec enable(mongooseim:host_type(), UserJID :: jid:jid(), PubsubJID :: jid:jid(),
Expand Down
6 changes: 3 additions & 3 deletions src/jingle_sip/mod_jingle_sip_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init(_Host, _Opts) ->
mnesia:create_table(jingle_sip_session,
[{ram_copies, [node()]},
{attributes, record_info(fields, jingle_sip_session)}]),
mongoose_mnesia:create_table(jingle_sip_session,
[{ram_copies, [node()]},
{attributes, record_info(fields, jingle_sip_session)}]),
ok.

-spec read_session(call_id()) -> [session()].
Expand Down
7 changes: 3 additions & 4 deletions src/mam/mod_mam_mnesia_prefs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@

-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
start(HostType, Opts) ->
mnesia:create_table(mam_prefs,
[{disc_copies, [node()]},
{attributes, record_info(fields, mam_prefs)}]),
mnesia:add_table_copy(mam_prefs, node(), disc_copies),
mongoose_mnesia:create_table(mam_prefs,
[{disc_copies, [node()]},
{attributes, record_info(fields, mam_prefs)}]),
gen_hook:add_handlers(hooks(HostType, Opts)).

-spec stop(mongooseim:host_type()) -> ok.
Expand Down
7 changes: 3 additions & 4 deletions src/mod_bosh_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

-spec start() -> any().
start() ->
mnesia:create_table(bosh_session,
[{ram_copies, [node()]},
{attributes, record_info(fields, bosh_session)}]),
mnesia:add_table_copy(bosh_session, node(), ram_copies).
mongoose_mnesia:create_table(bosh_session,
[{ram_copies, [node()]},
{attributes, record_info(fields, bosh_session)}]).

%% The choice of the operation context here (transaction vs dirty,
%% see man on mnesia:activity/4 for description of contexts) and the deletion
Expand Down
11 changes: 4 additions & 7 deletions src/mod_caps.erl
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,10 @@ init_db(mnesia) ->
_ ->
mnesia:delete_table(caps_features)
end,
mnesia:create_table(caps_features,
[{disc_only_copies, [node()]},
{local_content, true},
{attributes,
record_info(fields, caps_features)}]),
mnesia:add_table_copy(caps_features, node(),
disc_only_copies).
mongoose_mnesia:create_table(caps_features,
[{disc_only_copies, [node()]},
{local_content, true},
{attributes, record_info(fields, caps_features)}]).

-spec init(list()) -> {ok, state()}.
init([HostType, #{cache_size := MaxSize, cache_life_time := LifeTime}]) ->
Expand Down
8 changes: 3 additions & 5 deletions src/mod_keystore_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init(_HostType, _Opts) ->
mnesia:create_table(key,
[{ram_copies, [node()]},
{type, set},
{attributes, record_info(fields, key)}]),
mnesia:add_table_copy(key, node(), ram_copies),
mongoose_mnesia:create_table(key,
[{ram_copies, [node()]}, {type, set},
{attributes, record_info(fields, key)}]),
ok.

-spec init_ram_key(ProposedKey) -> Result when
Expand Down
7 changes: 3 additions & 4 deletions src/mod_last_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@

-spec init(host_type(), gen_mod:module_opts()) -> ok.
init(_HostType, _Opts) ->
mnesia:create_table(last_activity,
[{disc_copies, [node()]},
{attributes,
record_info(fields, last_activity)}]),
mongoose_mnesia:create_table(last_activity,
[{disc_copies, [node()]},
{attributes, record_info(fields, last_activity)}]),
ok.

-spec get_last(host_type(), jid:luser(), jid:lserver()) ->
Expand Down
14 changes: 6 additions & 8 deletions src/mod_muc_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@
}).

init(_HostType, _Opts) ->
mnesia:create_table(muc_room,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_room)}]),
mnesia:create_table(muc_registered,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_registered)}]),
mnesia:add_table_copy(muc_room, node(), disc_copies),
mnesia:add_table_copy(muc_registered, node(), disc_copies),
mongoose_mnesia:create_table(muc_room,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_room)}]),
mongoose_mnesia:create_table(muc_registered,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_registered)}]),
mnesia:add_table_index(muc_registered, nick),
ok.

Expand Down
7 changes: 3 additions & 4 deletions src/mod_private_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@
-record(private_storage, {usns, xml}).

init(_HostType, _Opts) ->
mnesia:create_table(private_storage,
[{disc_only_copies, [node()]},
{attributes, record_info(fields, private_storage)}]),
mnesia:add_table_copy(private_storage, node(), disc_only_copies),
mongoose_mnesia:create_table(private_storage,
[{disc_only_copies, [node()]},
{attributes, record_info(fields, private_storage)}]),
ok.

multi_set_data(_HostType, LUser, LServer, NS2XML) ->
Expand Down
9 changes: 4 additions & 5 deletions src/mod_register.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@
start(HostType, #{iqdisc := IQDisc}) ->
[gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_REGISTER, Component, Fn, #{}, IQDisc) ||
{Component, Fn} <- iq_handlers()],
mnesia:create_table(mod_register_ip,
[{ram_copies, [node()]},
{local_content, true},
{attributes, [key, value]}]),
mnesia:add_table_copy(mod_register_ip, node(), ram_copies),
mongoose_mnesia:create_table(mod_register_ip,
[{ram_copies, [node()]},
{local_content, true},
{attributes, [key, value]}]),
ok.

-spec stop(mongooseim:host_type()) -> ok.
Expand Down
12 changes: 6 additions & 6 deletions src/mod_roster_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@

-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init(_HostType, _Opts) ->
mnesia:create_table(roster,
[{disc_copies, [node()]},
{attributes, record_info(fields, roster)}]),
mnesia:create_table(roster_version,
[{disc_copies, [node()]},
{attributes, record_info(fields, roster_version)}]),
mongoose_mnesia:create_table(roster,
[{disc_copies, [node()]},
{attributes, record_info(fields, roster)}]),
mongoose_mnesia:create_table(roster_version,
[{disc_copies, [node()]},
{attributes, record_info(fields, roster_version)}]),
mnesia:add_table_index(roster, us),
mnesia:add_table_index(roster_version, us),
ok.
Expand Down
5 changes: 2 additions & 3 deletions src/mongoose_cluster_id.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,12 @@ init_cache() ->
init_cache(which_volatile_backend_available()).

init_cache(mnesia) ->
mnesia:create_table(mongoose_cluster_id,
mongoose_mnesia:create_table(mongoose_cluster_id,
[{type, set},
{record_name, mongoose_cluster_id},
{attributes, record_info(fields, mongoose_cluster_id)},
{ram_copies, [node()]}
]),
mnesia:add_table_copy(mongoose_cluster_id, node(), ram_copies);
]);
init_cache(cets) ->
cets:start(cets_cluster_id, #{}),
cets_discovery:add_table(mongoose_cets_discovery, cets_cluster_id),
Expand Down
46 changes: 46 additions & 0 deletions src/mongoose_mnesia.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-module(mongoose_mnesia).
-export([create_table/2]).

-include("mongoose.hrl").

%% @doc A wrapper around `mnesia:create_table/2':
%% - Automatically adds table copies.
%% - Checks that mnesia is running or fail with an error.
%% - Checks result of create_table
-spec create_table(atom(), list()) ->
{atomic, ok} | {aborted, {already_exists, atom()}}.
create_table(Table, Opts) ->
case mnesia:system_info(is_running) of
no ->
report_mnesia_table_error(Table, Opts, mnesia_not_running);
yes ->
Res = mnesia:create_table(Table, Opts),
check_create_table_result(Table, Opts, Res),
Res
end.

check_create_table_result(_Table, _Opts, {atomic, ok}) ->
ok;
check_create_table_result(Table, Opts, {aborted, {already_exists, Table}}) ->
[maybe_add_copies(Table, Opts, Type) || Type <- table_types()],
ok;
check_create_table_result(Table, Opts, Res) ->
report_mnesia_table_error(Table, Opts, Res).

table_types() ->
[disc_copies, disc_only_copies, ram_copies].

report_mnesia_table_error(Table, Opts, Res) ->
?LOG_CRITICAL(#{what => mnesia_create_table_failed,
table => Table, create_opts => Opts, reason => Res,
schema_nodes => catch mnesia:table_info(schema, disc_copies)}),
error({mnesia_create_table_failed, Table, Res}).

maybe_add_copies(Table, Opts, Type) ->
case proplists:get_value(Type, Opts) of
[_|_] ->
mnesia:add_table_copy(Table, node(), Type),
ok;
_ ->
ok
end.
3 changes: 1 addition & 2 deletions src/mongoose_node_num_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
num :: mongoose_node_num:node_num() }).

init() ->
mnesia:create_table(node_num,
mongoose_mnesia:create_table(node_num,
[{ram_copies, [node()]}, {type, set},
{attributes, record_info(fields, node_num)}]),
mnesia:add_table_index(node_num, num),
mnesia:add_table_copy(node_num, node(), ram_copies),
register_node(node()),
[#node_num{num = Num}] = mnesia:dirty_read(node_num, node()),
mongoose_node_num:set_node_num(Num),
Expand Down
7 changes: 3 additions & 4 deletions src/muc/mongoose_muc_online_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
start(_HostType, _Opts) ->
mnesia:create_table(muc_online_room,
[{ram_copies, [node()]},
{attributes, record_info(fields, muc_online_room)}]),
mnesia:add_table_copy(muc_online_room, node(), ram_copies),
mongoose_mnesia:create_table(muc_online_room,
[{ram_copies, [node()]},
{attributes, record_info(fields, muc_online_room)}]),
ok.

-spec stop(mongooseim:host_type()) -> ok.
Expand Down
33 changes: 9 additions & 24 deletions src/muc_light/mod_muc_light_db_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -248,32 +248,17 @@ force_clear() ->

-spec init_tables() -> ok.
init_tables() ->
create_table(muc_light_room,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_light_room)}]),
create_table(muc_light_user_room,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_light_user_room)},
{type, bag}]),
create_table(muc_light_blocking,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_light_blocking)},
{type, bag}]),
mongoose_mnesia:create_table(muc_light_room,
[{disc_copies, [node()]},
{attributes, record_info(fields, muc_light_room)}]),
mongoose_mnesia:create_table(muc_light_user_room,
[{disc_copies, [node()]}, {type, bag},
{attributes, record_info(fields, muc_light_user_room)}]),
mongoose_mnesia:create_table(muc_light_blocking,
[{disc_copies, [node()]}, {type, bag},
{attributes, record_info(fields, muc_light_blocking)}]),
ok.

-spec create_table(Name :: atom(), TabDef :: list()) -> ok.
create_table(Name, TabDef) ->
case mnesia:create_table(Name, TabDef) of
{atomic, ok} -> ok;
{aborted, exists} -> ok;
{aborted, {already_exists, _}} -> ok
end,
case mnesia:add_table_copy(Name, node(), disc_copies) of
{atomic, ok} -> ok;
{aborted, exists} -> ok;
{aborted, {already_exists, _, _}} -> ok
end.

%% ------------------------ General room management ------------------------

%% Expects config to have unique fields!
Expand Down
8 changes: 3 additions & 5 deletions src/offline/mod_offline_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@

-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init(_HostType, _Opts) ->
mnesia:create_table(offline_msg,
[{disc_only_copies, [node()]},
{type, bag},
{attributes, record_info(fields, offline_msg)}]),
mnesia:add_table_copy(offline_msg, node(), disc_only_copies),
mongoose_mnesia:create_table(offline_msg,
[{disc_only_copies, [node()]}, {type, bag},
{attributes, record_info(fields, offline_msg)}]),
upgrade_table(),
ok.

Expand Down
Loading

0 comments on commit 507fcd5

Please sign in to comment.