Merge pull request #5123 from k32/dev/common_shard

Common EMQX shard
This commit is contained in:
k32 2021-06-28 19:52:04 +02:00 committed by GitHub
commit f6507a4d02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 42 deletions

View File

@ -23,6 +23,9 @@
-define(Otherwise, true). -define(Otherwise, true).
-define(COMMON_SHARD, emqx_common_shard).
-define(SHARED_SUB_SHARD, emqx_shared_sub_shard).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Banner %% Banner
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -134,4 +137,3 @@
}). }).
-endif. -endif.

View File

@ -90,6 +90,10 @@
-define(DEACTIVATED_ALARM, emqx_deactivated_alarm). -define(DEACTIVATED_ALARM, emqx_deactivated_alarm).
-rlog_shard({?COMMON_SHARD, ?ACTIVATED_ALARM}).
-rlog_shard({?COMMON_SHARD, ?DEACTIVATED_ALARM}).
-ifdef(TEST). -ifdef(TEST).
-compile(export_all). -compile(export_all).
-compile(nowarn_export_all). -compile(nowarn_export_all).
@ -182,7 +186,7 @@ handle_call({activate_alarm, Name, Details}, _From, State = #state{actions = Act
details = Details, details = Details,
message = normalize_message(Name, Details), message = normalize_message(Name, Details),
activate_at = erlang:system_time(microsecond)}, activate_at = erlang:system_time(microsecond)},
mnesia:dirty_write(?ACTIVATED_ALARM, Alarm), ekka_mnesia:dirty_write(?ACTIVATED_ALARM, Alarm),
do_actions(activate, Alarm, Actions), do_actions(activate, Alarm, Actions),
{reply, ok, State} {reply, ok, State}
end; end;
@ -202,9 +206,14 @@ handle_call(delete_all_deactivated_alarms, _From, State) ->
{reply, ok, State}; {reply, ok, State};
handle_call({get_alarms, all}, _From, State) -> handle_call({get_alarms, all}, _From, State) ->
Alarms = [normalize(Alarm) || {atomic, Alarms} =
ekka_mnesia:ro_transaction(
?COMMON_SHARD,
fun() ->
[normalize(Alarm) ||
Alarm <- ets:tab2list(?ACTIVATED_ALARM) Alarm <- ets:tab2list(?ACTIVATED_ALARM)
++ ets:tab2list(?DEACTIVATED_ALARM)], ++ ets:tab2list(?DEACTIVATED_ALARM)]
end),
{reply, Alarms, State}; {reply, Alarms, State};
handle_call({get_alarms, activated}, _From, State) -> handle_call({get_alarms, activated}, _From, State) ->
@ -252,7 +261,7 @@ deactivate_alarm(Details, SizeLimit, Actions, #activated_alarm{
case mnesia:dirty_first(?DEACTIVATED_ALARM) of case mnesia:dirty_first(?DEACTIVATED_ALARM) of
'$end_of_table' -> ok; '$end_of_table' -> ok;
ActivateAt2 -> ActivateAt2 ->
mnesia:dirty_delete(?DEACTIVATED_ALARM, ActivateAt2) ekka_mnesia:dirty_delete(?DEACTIVATED_ALARM, ActivateAt2)
end; end;
false -> ok false -> ok
end, end,
@ -261,8 +270,8 @@ deactivate_alarm(Details, SizeLimit, Actions, #activated_alarm{
DeActAlarm = make_deactivated_alarm(ActivateAt, Name, Details, DeActAlarm = make_deactivated_alarm(ActivateAt, Name, Details,
normalize_message(Name, Details), normalize_message(Name, Details),
erlang:system_time(microsecond)), erlang:system_time(microsecond)),
mnesia:dirty_write(?DEACTIVATED_ALARM, HistoryAlarm), ekka_mnesia:dirty_write(?DEACTIVATED_ALARM, HistoryAlarm),
mnesia:dirty_delete(?ACTIVATED_ALARM, Name), ekka_mnesia:dirty_delete(?ACTIVATED_ALARM, Name),
do_actions(deactivate, DeActAlarm, Actions). do_actions(deactivate, DeActAlarm, Actions).
make_deactivated_alarm(ActivateAt, Name, Details, Message, DeActivateAt) -> make_deactivated_alarm(ActivateAt, Name, Details, Message, DeActivateAt) ->
@ -279,7 +288,7 @@ deactivate_all_alarms() ->
details = Details, details = Details,
message = Message, message = Message,
activate_at = ActivateAt}) -> activate_at = ActivateAt}) ->
mnesia:dirty_write(?DEACTIVATED_ALARM, ekka_mnesia:dirty_write(?DEACTIVATED_ALARM,
#deactivated_alarm{ #deactivated_alarm{
activate_at = ActivateAt, activate_at = ActivateAt,
name = Name, name = Name,
@ -291,7 +300,7 @@ deactivate_all_alarms() ->
%% Delete all records from the given table, ignore result. %% Delete all records from the given table, ignore result.
clear_table(TableName) -> clear_table(TableName) ->
case mnesia:clear_table(TableName) of case ekka_mnesia:clear_table(TableName) of
{aborted, Reason} -> {aborted, Reason} ->
?LOG(warning, "Faile to clear table ~p reason: ~p", ?LOG(warning, "Faile to clear table ~p reason: ~p",
[TableName, Reason]); [TableName, Reason]);
@ -311,7 +320,7 @@ delete_expired_deactivated_alarms('$end_of_table', _Checkpoint) ->
delete_expired_deactivated_alarms(ActivatedAt, Checkpoint) -> delete_expired_deactivated_alarms(ActivatedAt, Checkpoint) ->
case ActivatedAt =< Checkpoint of case ActivatedAt =< Checkpoint of
true -> true ->
mnesia:dirty_delete(?DEACTIVATED_ALARM, ActivatedAt), ekka_mnesia:dirty_delete(?DEACTIVATED_ALARM, ActivatedAt),
NActivatedAt = mnesia:dirty_next(?DEACTIVATED_ALARM, ActivatedAt), NActivatedAt = mnesia:dirty_next(?DEACTIVATED_ALARM, ActivatedAt),
delete_expired_deactivated_alarms(NActivatedAt, Checkpoint); delete_expired_deactivated_alarms(NActivatedAt, Checkpoint);
false -> false ->

View File

@ -28,7 +28,7 @@
-define(APP, emqx). -define(APP, emqx).
-define(EMQX_SHARDS, [route_shard]). -define(EMQX_SHARDS, [?ROUTE_SHARD, ?COMMON_SHARD, ?SHARED_SUB_SHARD]).
-include("emqx_release.hrl"). -include("emqx_release.hrl").

View File

@ -51,6 +51,8 @@
-define(BANNED_TAB, ?MODULE). -define(BANNED_TAB, ?MODULE).
-rlog_shard({?COMMON_SHARD, ?BANNED_TAB}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Mnesia bootstrap %% Mnesia bootstrap
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -96,19 +98,19 @@ create(#{who := Who,
reason := Reason, reason := Reason,
at := At, at := At,
until := Until}) -> until := Until}) ->
mnesia:dirty_write(?BANNED_TAB, #banned{who = Who, ekka_mnesia:dirty_write(?BANNED_TAB, #banned{who = Who,
by = By, by = By,
reason = Reason, reason = Reason,
at = At, at = At,
until = Until}); until = Until});
create(Banned) when is_record(Banned, banned) -> create(Banned) when is_record(Banned, banned) ->
mnesia:dirty_write(?BANNED_TAB, Banned). ekka_mnesia:dirty_write(?BANNED_TAB, Banned).
-spec(delete({clientid, emqx_types:clientid()} -spec(delete({clientid, emqx_types:clientid()}
| {username, emqx_types:username()} | {username, emqx_types:username()}
| {peerhost, emqx_types:peerhost()}) -> ok). | {peerhost, emqx_types:peerhost()}) -> ok).
delete(Who) -> delete(Who) ->
mnesia:dirty_delete(?BANNED_TAB, Who). ekka_mnesia:dirty_delete(?BANNED_TAB, Who).
info(InfoKey) -> info(InfoKey) ->
mnesia:table_info(?BANNED_TAB, InfoKey). mnesia:table_info(?BANNED_TAB, InfoKey).
@ -129,7 +131,7 @@ handle_cast(Msg, State) ->
{noreply, State}. {noreply, State}.
handle_info({timeout, TRef, expire}, State = #{expiry_timer := TRef}) -> handle_info({timeout, TRef, expire}, State = #{expiry_timer := TRef}) ->
mnesia:async_dirty(fun expire_banned_items/1, [erlang:system_time(second)]), ekka_mnesia:transaction(?COMMON_SHARD, fun expire_banned_items/1, [erlang:system_time(second)]),
{noreply, ensure_expiry_timer(State), hibernate}; {noreply, ensure_expiry_timer(State), hibernate};
handle_info(Info, State) -> handle_info(Info, State) ->
@ -160,4 +162,3 @@ expire_banned_items(Now) ->
mnesia:delete_object(?BANNED_TAB, B, sticky_write); mnesia:delete_object(?BANNED_TAB, B, sticky_write);
(_, _Acc) -> ok (_, _Acc) -> ok
end, ok, ?BANNED_TAB). end, ok, ?BANNED_TAB).

View File

@ -77,6 +77,8 @@
-define(NACK(Reason), {shared_sub_nack, Reason}). -define(NACK(Reason), {shared_sub_nack, Reason}).
-define(NO_ACK, no_ack). -define(NO_ACK, no_ack).
-rlog_shard({?SHARED_SUB_SHARD, ?TAB}).
-record(state, {pmon}). -record(state, {pmon}).
-record(emqx_shared_subscription, {group, topic, subpid}). -record(emqx_shared_subscription, {group, topic, subpid}).
@ -297,7 +299,7 @@ subscribers(Group, Topic) ->
init([]) -> init([]) ->
{ok, _} = mnesia:subscribe({table, ?TAB, simple}), {ok, _} = mnesia:subscribe({table, ?TAB, simple}),
{atomic, PMon} = mnesia:transaction(fun init_monitors/0), {atomic, PMon} = ekka_mnesia:transaction(?SHARED_SUB_SHARD, fun init_monitors/0),
ok = emqx_tables:new(?SHARED_SUBS, [protected, bag]), ok = emqx_tables:new(?SHARED_SUBS, [protected, bag]),
ok = emqx_tables:new(?ALIVE_SUBS, [protected, set, {read_concurrency, true}]), ok = emqx_tables:new(?ALIVE_SUBS, [protected, set, {read_concurrency, true}]),
{ok, update_stats(#state{pmon = PMon})}. {ok, update_stats(#state{pmon = PMon})}.
@ -309,7 +311,7 @@ init_monitors() ->
end, emqx_pmon:new(), ?TAB). end, emqx_pmon:new(), ?TAB).
handle_call({subscribe, Group, Topic, SubPid}, _From, State = #state{pmon = PMon}) -> handle_call({subscribe, Group, Topic, SubPid}, _From, State = #state{pmon = PMon}) ->
mnesia:dirty_write(?TAB, record(Group, Topic, SubPid)), ekka_mnesia:dirty_write(?TAB, record(Group, Topic, SubPid)),
case ets:member(?SHARED_SUBS, {Group, Topic}) of case ets:member(?SHARED_SUBS, {Group, Topic}) of
true -> ok; true -> ok;
false -> ok = emqx_router:do_add_route(Topic, {Group, node()}) false -> ok = emqx_router:do_add_route(Topic, {Group, node()})
@ -319,7 +321,7 @@ handle_call({subscribe, Group, Topic, SubPid}, _From, State = #state{pmon = PMon
{reply, ok, update_stats(State#state{pmon = emqx_pmon:monitor(SubPid, PMon)})}; {reply, ok, update_stats(State#state{pmon = emqx_pmon:monitor(SubPid, PMon)})};
handle_call({unsubscribe, Group, Topic, SubPid}, _From, State) -> handle_call({unsubscribe, Group, Topic, SubPid}, _From, State) ->
mnesia:dirty_delete_object(?TAB, record(Group, Topic, SubPid)), ekka_mnesia:dirty_delete_object(?TAB, record(Group, Topic, SubPid)),
true = ets:delete_object(?SHARED_SUBS, {{Group, Topic}, SubPid}), true = ets:delete_object(?SHARED_SUBS, {{Group, Topic}, SubPid}),
delete_route_if_needed({Group, Topic}), delete_route_if_needed({Group, Topic}),
{reply, ok, State}; {reply, ok, State};
@ -373,7 +375,7 @@ cleanup_down(SubPid) ->
?IS_LOCAL_PID(SubPid) orelse ets:delete(?ALIVE_SUBS, SubPid), ?IS_LOCAL_PID(SubPid) orelse ets:delete(?ALIVE_SUBS, SubPid),
lists:foreach( lists:foreach(
fun(Record = #emqx_shared_subscription{topic = Topic, group = Group}) -> fun(Record = #emqx_shared_subscription{topic = Topic, group = Group}) ->
ok = mnesia:dirty_delete_object(?TAB, Record), ok = ekka_mnesia:dirty_delete_object(?TAB, Record),
true = ets:delete_object(?SHARED_SUBS, {{Group, Topic}, SubPid}), true = ets:delete_object(?SHARED_SUBS, {{Group, Topic}, SubPid}),
delete_route_if_needed({Group, Topic}) delete_route_if_needed({Group, Topic})
end, mnesia:dirty_match_object(#emqx_shared_subscription{_ = '_', subpid = SubPid})). end, mnesia:dirty_match_object(#emqx_shared_subscription{_ = '_', subpid = SubPid})).

View File

@ -44,6 +44,8 @@
, code_change/3 , code_change/3
]). ]).
-define(SN_SHARD, emqx_sn_shard).
-define(TAB, ?MODULE). -define(TAB, ?MODULE).
-record(state, {max_predef_topic_id = 0}). -record(state, {max_predef_topic_id = 0}).
@ -56,6 +58,7 @@
-boot_mnesia({mnesia, [boot]}). -boot_mnesia({mnesia, [boot]}).
-copy_mnesia({mnesia, [copy]}). -copy_mnesia({mnesia, [copy]}).
-rlog_shard({?SN_SHARD, ?TAB}).
%% @doc Create or replicate tables. %% @doc Create or replicate tables.
-spec(mnesia(boot | copy) -> ok). -spec(mnesia(boot | copy) -> ok).
@ -74,6 +77,7 @@ mnesia(copy) ->
-spec(start_link(list()) -> {ok, pid()} | ignore | {error, Reason :: term()}). -spec(start_link(list()) -> {ok, pid()} | ignore | {error, Reason :: term()}).
start_link(PredefTopics) -> start_link(PredefTopics) ->
ekka_rlog:wait_for_shards([?SN_SHARD], infinity),
gen_server:start_link({local, ?MODULE}, ?MODULE, [PredefTopics], []). gen_server:start_link({local, ?MODULE}, ?MODULE, [PredefTopics], []).
-spec(stop() -> ok). -spec(stop() -> ok).
@ -129,9 +133,9 @@ init([PredefTopics]) ->
%% {ClientId, TopicName} -> TopicId %% {ClientId, TopicName} -> TopicId
MaxPredefId = lists:foldl( MaxPredefId = lists:foldl(
fun({TopicId, TopicName}, AccId) -> fun({TopicId, TopicName}, AccId) ->
mnesia:dirty_write(#emqx_sn_registry{key = {predef, TopicId}, ekka_mnesia:dirty_write(#emqx_sn_registry{key = {predef, TopicId},
value = TopicName}), value = TopicName}),
mnesia:dirty_write(#emqx_sn_registry{key = {predef, TopicName}, ekka_mnesia:dirty_write(#emqx_sn_registry{key = {predef, TopicName},
value = TopicId}), value = TopicId}),
if TopicId > AccId -> TopicId; true -> AccId end if TopicId > AccId -> TopicId; true -> AccId end
end, 0, PredefTopics), end, 0, PredefTopics),
@ -157,7 +161,7 @@ handle_call({register, ClientId, TopicName}, _From,
mnesia:write(#emqx_sn_registry{key = {ClientId, TopicId}, mnesia:write(#emqx_sn_registry{key = {ClientId, TopicId},
value = TopicName}) value = TopicName})
end, end,
case mnesia:transaction(Fun) of case ekka_mnesia:transaction(?SN_SHARD, Fun) of
{atomic, ok} -> {atomic, ok} ->
{reply, TopicId, State}; {reply, TopicId, State};
{aborted, Error} -> {aborted, Error} ->
@ -168,7 +172,7 @@ handle_call({register, ClientId, TopicName}, _From,
handle_call({unregister, ClientId}, _From, State) -> handle_call({unregister, ClientId}, _From, State) ->
Registry = mnesia:dirty_match_object({?TAB, {ClientId, '_'}, '_'}), Registry = mnesia:dirty_match_object({?TAB, {ClientId, '_'}, '_'}),
lists:foreach(fun(R) -> mnesia:dirty_delete_object(R) end, Registry), lists:foreach(fun(R) -> ekka_mnesia:dirty_delete_object(?TAB, R) end, Registry),
{reply, ok, State}; {reply, ok, State};
handle_call(Req, _From, State) -> handle_call(Req, _From, State) ->

View File

@ -41,9 +41,10 @@ end_per_suite(_Config) ->
ok. ok.
init_per_testcase(_TestCase, Config) -> init_per_testcase(_TestCase, Config) ->
application:set_env(ekka, strict_mode, true),
ekka_mnesia:start(), ekka_mnesia:start(),
emqx_sn_registry:mnesia(boot), emqx_sn_registry:mnesia(boot),
mnesia:clear_table(emqx_sn_registry), ekka_mnesia:clear_table(emqx_sn_registry),
PredefTopics = application:get_env(emqx_sn, predefined, []), PredefTopics = application:get_env(emqx_sn, predefined, []),
{ok, _Pid} = ?REGISTRY:start_link(PredefTopics), {ok, _Pid} = ?REGISTRY:start_link(PredefTopics),
Config. Config.
@ -118,4 +119,3 @@ register_a_lot(N, Max) when N < Max ->
Topic = iolist_to_binary(["Topic", integer_to_list(N)]), Topic = iolist_to_binary(["Topic", integer_to_list(N)]),
?assertEqual(N, ?REGISTRY:register_topic(<<"ClientId">>, Topic)), ?assertEqual(N, ?REGISTRY:register_topic(<<"ClientId">>, Topic)),
register_a_lot(N+1, Max). register_a_lot(N+1, Max).

View File

@ -90,6 +90,8 @@
-define(TELEMETRY, emqx_telemetry). -define(TELEMETRY, emqx_telemetry).
-rlog_shard({?COMMON_SHARD, ?TELEMETRY}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Mnesia bootstrap %% Mnesia bootstrap
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -146,7 +148,7 @@ init([Opts]) ->
[] -> [] ->
Enabled = proplists:get_value(enabled, Opts, true), Enabled = proplists:get_value(enabled, Opts, true),
UUID = generate_uuid(), UUID = generate_uuid(),
mnesia:dirty_write(?TELEMETRY, #telemetry{id = ?UNIQUE_ID, ekka_mnesia:dirty_write(?TELEMETRY, #telemetry{id = ?UNIQUE_ID,
uuid = UUID, uuid = UUID,
enabled = Enabled}), enabled = Enabled}),
State#state{enabled = Enabled, uuid = UUID}; State#state{enabled = Enabled, uuid = UUID};
@ -162,14 +164,14 @@ init([Opts]) ->
end. end.
handle_call(enable, _From, State = #state{uuid = UUID}) -> handle_call(enable, _From, State = #state{uuid = UUID}) ->
mnesia:dirty_write(?TELEMETRY, #telemetry{id = ?UNIQUE_ID, ekka_mnesia:dirty_write(?TELEMETRY, #telemetry{id = ?UNIQUE_ID,
uuid = UUID, uuid = UUID,
enabled = true}), enabled = true}),
_ = erlang:send(self(), first_report), _ = erlang:send(self(), first_report),
{reply, ok, State#state{enabled = true}}; {reply, ok, State#state{enabled = true}};
handle_call(disable, _From, State = #state{uuid = UUID}) -> handle_call(disable, _From, State = #state{uuid = UUID}) ->
mnesia:dirty_write(?TELEMETRY, #telemetry{id = ?UNIQUE_ID, ekka_mnesia:dirty_write(?TELEMETRY, #telemetry{id = ?UNIQUE_ID,
uuid = UUID, uuid = UUID,
enabled = false}), enabled = false}),
{reply, ok, State#state{enabled = false}}; {reply, ok, State#state{enabled = false}};