Bläddra i källkod

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

Andrew Mayorov 2 år sedan
förälder
incheckning
eff149e676

+ 1 - 0
apps/emqx_auth_mnesia/src/emqx_auth_mnesia_app.erl

@@ -25,6 +25,7 @@
 start(_StartType, _StartArgs) ->
     ok = emqx_authz_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_authn:register_provider(?AUTHN_TYPE_SIMPLE, emqx_authn_mnesia),
     ok = emqx_authn:register_provider(?AUTHN_TYPE_SCRAM, emqx_authn_scram_mnesia),

+ 6 - 7
apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl

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

+ 9 - 6
apps/emqx_auth_mnesia/src/emqx_authn_scram_mnesia.erl

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

+ 5 - 7
apps/emqx_auth_mnesia/src/emqx_authz_mnesia.erl

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