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

This commit is contained in:
Andrew Mayorov 2024-01-30 20:48:32 +01:00
parent 21780e2126
commit b6d77c164e
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
8 changed files with 45 additions and 36 deletions

View File

@ -93,15 +93,22 @@ default_auth_header() ->
create_default_app() ->
Now = erlang:system_time(second),
ExpiredAt = Now + timer:minutes(10),
emqx_mgmt_auth:create(
?DEFAULT_APP_ID,
?DEFAULT_APP_KEY,
?DEFAULT_APP_SECRET,
true,
ExpiredAt,
<<"default app key for test">>,
?ROLE_API_SUPERUSER
).
case
emqx_mgmt_auth:create(
?DEFAULT_APP_ID,
?DEFAULT_APP_KEY,
?DEFAULT_APP_SECRET,
true,
ExpiredAt,
<<"default app key for test">>,
?ROLE_API_SUPERUSER
)
of
{ok, App} ->
{ok, App};
{error, name_already_existed} ->
{ok, _} = emqx_mgmt_auth:read(?DEFAULT_APP_ID)
end.
delete_default_app() ->
emqx_mgmt_auth:delete(?DEFAULT_APP_ID).

View File

@ -22,12 +22,9 @@
-include_lib("emqx/include/logger.hrl").
-include_lib("stdlib/include/ms_transform.hrl").
-boot_mnesia({mnesia, [boot]}).
-behaviour(emqx_db_backup).
%% Mnesia bootstrap
-export([mnesia/1]).
-export([create_tables/0]).
-export([
add_user/4,
@ -70,7 +67,7 @@
%% Mnesia bootstrap
%%--------------------------------------------------------------------
mnesia(boot) ->
create_tables() ->
ok = mria:create_table(?ADMIN, [
{type, set},
{rlog_shard, ?DASHBOARD_SHARD},
@ -83,7 +80,8 @@ mnesia(boot) ->
{write_concurrency, true}
]}
]}
]).
]),
[?ADMIN].
%%--------------------------------------------------------------------
%% Data backup

View File

@ -26,7 +26,12 @@
-include("emqx_dashboard.hrl").
start(_StartType, _StartArgs) ->
ok = mria_rlog:wait_for_shards([?DASHBOARD_SHARD], infinity),
Tables = lists:append([
emqx_dashboard_admin:create_tables(),
emqx_dashboard_token:create_tables(),
emqx_dashboard_monitor:create_tables()
]),
ok = mria:wait_for_tables(Tables),
{ok, Sup} = emqx_dashboard_sup:start_link(),
case emqx_dashboard:start_listeners() of
ok ->

View File

@ -22,8 +22,7 @@
-behaviour(gen_server).
-boot_mnesia({mnesia, [boot]}).
-export([create_tables/0]).
-export([start_link/0]).
-export([
@ -35,8 +34,6 @@
code_change/3
]).
-export([mnesia/1]).
-export([
samplers/0,
samplers/2,
@ -67,14 +64,15 @@
data :: map()
}).
mnesia(boot) ->
create_tables() ->
ok = mria:create_table(?TAB, [
{type, set},
{local_content, true},
{storage, disc_copies},
{record_name, emqx_monit},
{attributes, record_info(fields, emqx_monit)}
]).
]),
[?TAB].
%% -------------------------------------------------------------------------------------------------
%% API

View File

@ -18,6 +18,8 @@
-include("emqx_dashboard.hrl").
-export([create_tables/0]).
-export([
sign/2,
verify/2,
@ -27,10 +29,6 @@
destroy_by_username/1
]).
-boot_mnesia({mnesia, [boot]}).
-export([mnesia/1]).
-ifdef(TEST).
-export([lookup_by_username/1, clean_expired_jwt/1]).
-endif.
@ -87,7 +85,7 @@ salt() ->
<<X:16/big-unsigned-integer>> = crypto:strong_rand_bytes(2),
iolist_to_binary(io_lib:format("~4.16.0b", [X])).
mnesia(boot) ->
create_tables() ->
ok = mria:create_table(?TAB, [
{type, set},
{rlog_shard, ?DASHBOARD_SHARD},
@ -100,7 +98,8 @@ mnesia(boot) ->
{write_concurrency, true}
]}
]}
]).
]),
[?TAB].
%%--------------------------------------------------------------------
%% jwt apply

View File

@ -28,6 +28,7 @@
-include("emqx_mgmt.hrl").
start(_Type, _Args) ->
ok = mria:wait_for_tables(emqx_mgmt_auth:create_tables()),
case emqx_mgmt_auth:init_bootstrap_file() of
ok ->
emqx_conf:add_handler([api_key], emqx_mgmt_auth),

View File

@ -22,8 +22,8 @@
-behaviour(emqx_db_backup).
%% API
-export([mnesia/1]).
-boot_mnesia({mnesia, [boot]}).
-export([create_tables/0]).
-behaviour(emqx_config_handler).
-export([
@ -70,7 +70,7 @@
-define(DEFAULT_HASH_LEN, 16).
mnesia(boot) ->
create_tables() ->
Fields = record_info(fields, ?APP),
ok = mria:create_table(?APP, [
{type, set},
@ -78,7 +78,8 @@ mnesia(boot) ->
{storage, disc_copies},
{record_name, ?APP},
{attributes, Fields}
]).
]),
[?APP].
%%--------------------------------------------------------------------
%% Data backup

View File

@ -30,8 +30,9 @@ init_suite(Apps, SetConfigs) when is_function(SetConfigs) ->
init_suite(Apps, SetConfigs, #{}).
init_suite(Apps, SetConfigs, Opts) ->
application:load(emqx_management),
emqx_common_test_helpers:start_apps(Apps ++ [emqx_dashboard], SetConfigs, Opts),
emqx_common_test_helpers:start_apps(
Apps ++ [emqx_management, emqx_dashboard], SetConfigs, Opts
),
_ = emqx_common_test_http:create_default_app(),
ok.
@ -40,8 +41,7 @@ end_suite() ->
end_suite(Apps) ->
emqx_common_test_http:delete_default_app(),
emqx_common_test_helpers:stop_apps(Apps ++ [emqx_dashboard]),
application:unload(emqx_management),
emqx_common_test_helpers:stop_apps(Apps ++ [emqx_management, emqx_dashboard]),
ok.
set_special_configs(emqx_dashboard) ->