feat(emqx-auth-mnesia): manage mria tables explicitly during startup

This commit is contained in:
Andrew Mayorov 2024-01-30 20:42:03 +01:00
parent 21a5751575
commit eff149e676
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
4 changed files with 21 additions and 20 deletions

View File

@ -25,6 +25,7 @@
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
ok = emqx_authz_mnesia:init_tables(), ok = emqx_authz_mnesia:init_tables(),
ok = emqx_authn_mnesia:init_tables(), ok = emqx_authn_mnesia:init_tables(),
ok = emqx_authn_scram_mnesia:init_tables(),
ok = emqx_authz:register_source(?AUTHZ_TYPE, emqx_authz_mnesia), ok = emqx_authz:register_source(?AUTHZ_TYPE, emqx_authz_mnesia),
ok = emqx_authn:register_provider(?AUTHN_TYPE_SIMPLE, emqx_authn_mnesia), ok = emqx_authn:register_provider(?AUTHN_TYPE_SIMPLE, emqx_authn_mnesia),
ok = emqx_authn:register_provider(?AUTHN_TYPE_SCRAM, emqx_authn_scram_mnesia), ok = emqx_authn:register_provider(?AUTHN_TYPE_SCRAM, emqx_authn_scram_mnesia),

View File

@ -55,7 +55,7 @@
do_update_user/3 do_update_user/3
]). ]).
-export([mnesia/1, init_tables/0]). -export([init_tables/0]).
-export([backup_tables/0]). -export([backup_tables/0]).
@ -69,8 +69,6 @@
is_superuser :: boolean() is_superuser :: boolean()
}). }).
-boot_mnesia({mnesia, [boot]}).
-define(TAB, ?MODULE). -define(TAB, ?MODULE).
-define(AUTHN_QSCHEMA, [ -define(AUTHN_QSCHEMA, [
{<<"like_user_id">>, binary}, {<<"like_user_id">>, binary},
@ -83,8 +81,8 @@
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc Create or replicate tables. %% @doc Create or replicate tables.
-spec mnesia(boot | copy) -> ok. -spec create_tables() -> [mria:table()].
mnesia(boot) -> create_tables() ->
ok = mria:create_table(?TAB, [ ok = mria:create_table(?TAB, [
{rlog_shard, ?AUTHN_SHARD}, {rlog_shard, ?AUTHN_SHARD},
{type, ordered_set}, {type, ordered_set},
@ -92,12 +90,13 @@ mnesia(boot) ->
{record_name, user_info}, {record_name, user_info},
{attributes, record_info(fields, user_info)}, {attributes, record_info(fields, user_info)},
{storage_properties, [{ets, [{read_concurrency, true}]}]} {storage_properties, [{ets, [{read_concurrency, true}]}]}
]). ]),
[?TAB].
%% Init %% Init
-spec init_tables() -> ok. -spec init_tables() -> ok.
init_tables() -> init_tables() ->
ok = mria_rlog:wait_for_shards([?AUTHN_SHARD], infinity). ok = mria:wait_for_tables(create_tables()).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Data backup %% Data backup

View File

@ -65,9 +65,7 @@
-type user_group() :: binary(). -type user_group() :: binary().
-export([mnesia/1]). -export([init_tables/0]).
-boot_mnesia({mnesia, [boot]}).
-record(user_info, { -record(user_info, {
user_id, user_id,
@ -84,8 +82,8 @@
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc Create or replicate tables. %% @doc Create or replicate tables.
-spec mnesia(boot | copy) -> ok. -spec create_tables() -> [mria:table()].
mnesia(boot) -> create_tables() ->
ok = mria:create_table(?TAB, [ ok = mria:create_table(?TAB, [
{rlog_shard, ?AUTHN_SHARD}, {rlog_shard, ?AUTHN_SHARD},
{type, ordered_set}, {type, ordered_set},
@ -93,7 +91,12 @@ mnesia(boot) ->
{record_name, user_info}, {record_name, user_info},
{attributes, record_info(fields, user_info)}, {attributes, record_info(fields, user_info)},
{storage_properties, [{ets, [{read_concurrency, true}]}]} {storage_properties, [{ets, [{read_concurrency, true}]}]}
]). ]),
[?TAB].
-spec init_tables() -> ok.
init_tables() ->
mria:wait_for_tables(create_tables()).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Data backup %% Data backup

View File

@ -56,7 +56,6 @@
%% Management API %% Management API
-export([ -export([
mnesia/1,
init_tables/0, init_tables/0,
store_rules/2, store_rules/2,
purge_rules/0, purge_rules/0,
@ -74,17 +73,16 @@
-compile(nowarn_export_all). -compile(nowarn_export_all).
-endif. -endif.
-boot_mnesia({mnesia, [boot]}). -spec create_tables() -> [mria:table()].
create_tables() ->
-spec mnesia(boot | copy) -> ok.
mnesia(boot) ->
ok = mria:create_table(?ACL_TABLE, [ ok = mria:create_table(?ACL_TABLE, [
{type, ordered_set}, {type, ordered_set},
{rlog_shard, ?ACL_SHARDED}, {rlog_shard, ?ACL_SHARDED},
{storage, disc_copies}, {storage, disc_copies},
{attributes, record_info(fields, ?ACL_TABLE)}, {attributes, record_info(fields, ?ACL_TABLE)},
{storage_properties, [{ets, [{read_concurrency, true}]}]} {storage_properties, [{ets, [{read_concurrency, true}]}]}
]). ]),
[?ACL_TABLE].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% emqx_authz callbacks %% emqx_authz callbacks
@ -138,7 +136,7 @@ backup_tables() -> [?ACL_TABLE].
%% Init %% Init
-spec init_tables() -> ok. -spec init_tables() -> ok.
init_tables() -> init_tables() ->
ok = mria_rlog:wait_for_shards([?ACL_SHARDED], infinity). ok = mria:wait_for_tables(create_tables()).
%% @doc Update authz rules %% @doc Update authz rules
-spec store_rules(who(), rules()) -> ok. -spec store_rules(who(), rules()) -> ok.