Skip to content
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

Replicate mqtt_admin table into cluster #48

Merged
merged 3 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 27 additions & 15 deletions src/emqx_dashboard_admin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

-include("emqx_dashboard.hrl").

%% Mnesia bootstrap
-export([mnesia/1]).

-boot_mnesia({mnesia, [boot]}).
-copy_mnesia({mnesia, [copy]}).

%% API Function Exports
-export([start_link/0]).

Expand All @@ -44,14 +50,29 @@
, code_change/3
]).

-spec(start_link() -> {ok, pid()} | ignore | {error, any()}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
%%--------------------------------------------------------------------
%% Mnesia bootstrap
%%--------------------------------------------------------------------

mnesia(boot) ->
ok = ekka_mnesia:create_table(mqtt_admin, [
{type, set},
{disc_copies, [node()]},
{record_name, mqtt_admin},
{attributes, record_info(fields, mqtt_admin)},
{storage_properties, [{ets, [{read_concurrency, true},
{write_concurrency, true}]}]}]);
mnesia(copy) ->
ok = ekka_mnesia:copy_table(mqtt_admin).

%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------

-spec(start_link() -> {ok, pid()} | ignore | {error, any()}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

-spec(add_user(binary(), binary(), binary()) -> ok | {error, any()}).
add_user(Username, Password, Tags) when is_binary(Username), is_binary(Password) ->
Admin = #mqtt_admin{username = Username, password = hash(Password), tags = Tags},
Expand Down Expand Up @@ -145,22 +166,13 @@ check(Username, Password) ->
%%--------------------------------------------------------------------

init([]) ->
% Create mqtt_admin table
ok = ekka_mnesia:create_table(mqtt_admin, [
{type, set},
{local_content, true}, %% local_content to avoid blocking on mnesia:wait_for_tables/2
{disc_copies, [node()]},
{record_name, mqtt_admin},
{attributes, record_info(fields, mqtt_admin)}]),
ok = ekka_mnesia:copy_table(mqtt_admin, disc_copies),
%% Wait???
%% mnesia:wait_for_tables([mqtt_admin], 5000),
% Init mqtt_admin table
mnesia:wait_for_tables([mqtt_admin], 5000),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this line which has been done in ekka

%% Init mqtt_admin table
add_default_user(binenv(default_user_username), binenv(default_user_passwd)),
{ok, state}.

handle_call(_Req, _From, State) ->
{reply, error, State}.
{reply, error, State}.

handle_cast(_Msg, State) ->
{noreply, State}.
Expand Down
2 changes: 2 additions & 0 deletions test/emqx_dashboard_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ groups() ->
].

init_per_suite(Config) ->
ekka_mnesia:start(),
Copy link
Contributor

@gilbertwong96 gilbertwong96 Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just application:load(emqx_dashboard), the ekka would start mnesia when emqx is started

emqx_dashboard_admin:mnesia(boot),
emqx_ct_helpers:start_apps([emqx, emqx_management, emqx_dashboard]),
Config.

Expand Down