Merge pull request #6357 from zhongwencool/4.3-sn-registry

fix: e4.4.0 failed to join e4.3.0 cluster
This commit is contained in:
zhongwencool 2021-12-03 09:32:46 +08:00 committed by GitHub
commit 9f7db2eacb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 30 deletions

View File

@ -1,6 +1,6 @@
{application, emqx_sn, {application, emqx_sn,
[{description, "EMQ X MQTT-SN Plugin"}, [{description, "EMQ X MQTT-SN Plugin"},
{vsn, "4.3.3"}, % strict semver, bump manually! {vsn, "4.3.4"}, % strict semver, bump manually!
{modules, []}, {modules, []},
{registered, []}, {registered, []},
{applications, [kernel,stdlib,esockd]}, {applications, [kernel,stdlib,esockd]},

View File

@ -1,18 +1,24 @@
%% -*-: erlang -*- %% -*-: erlang -*-
{VSN, {VSN,
[ [
{"4.3.3", [
{load_module, emqx_sn_registry, brutal_purge, soft_purge, []}
]},
{"4.3.2", [ {"4.3.2", [
{load_module, emqx_sn_gateway, brutal_purge, soft_purge, []} {load_module, emqx_sn_gateway, brutal_purge, soft_purge, []}
]}, ]},
{<<"4.3.[0-1]">>, [ {<<"4\\.3\\.[0-1]">>, [
{restart_application, emqx_sn} {restart_application, emqx_sn}
]} ]}
], ],
[ [
{"4.3.3", [
{load_module, emqx_sn_registry, brutal_purge, soft_purge, []}
]},
{"4.3.2", [ {"4.3.2", [
{load_module, emqx_sn_gateway, brutal_purge, soft_purge, []} {load_module, emqx_sn_gateway, brutal_purge, soft_purge, []}
]}, ]},
{<<"4.3.[0-1]">>, [ {<<"4\\.3\\.[0-1]">>, [
{restart_application, emqx_sn} {restart_application, emqx_sn}
]} ]}
] ]

View File

@ -44,32 +44,16 @@
, code_change/3 , code_change/3
]). ]).
-ifdef(TEST).
-export([create_table/0]).
-endif.
-define(TAB, ?MODULE). -define(TAB, ?MODULE).
-record(state, {max_predef_topic_id = 0}). -record(state, {max_predef_topic_id = 0}).
-record(emqx_sn_registry, {key, value}). -record(emqx_sn_registry, {key, value}).
%% Mnesia bootstrap
-export([mnesia/1]).
-boot_mnesia({mnesia, [boot]}).
-copy_mnesia({mnesia, [copy]}).
%% @doc Create or replicate tables.
-spec(mnesia(boot | copy) -> ok).
mnesia(boot) ->
%% Optimize storage
StoreProps = [{ets, [{read_concurrency, true}]}],
ok = ekka_mnesia:create_table(?MODULE, [
{attributes, record_info(fields, emqx_sn_registry)},
{ram_copies, [node()]},
{storage_properties, StoreProps}]);
mnesia(copy) ->
ok = ekka_mnesia:copy_table(?MODULE, ram_copies).
%%----------------------------------------------------------------------------- %%-----------------------------------------------------------------------------
-spec(start_link(list()) -> {ok, pid()} | ignore | {error, Reason :: term()}). -spec(start_link(list()) -> {ok, pid()} | ignore | {error, Reason :: term()}).
@ -123,6 +107,7 @@ unregister_topic(ClientId) ->
%%----------------------------------------------------------------------------- %%-----------------------------------------------------------------------------
init([PredefTopics]) -> init([PredefTopics]) ->
create_table(),
%% {predef, TopicId} -> TopicName %% {predef, TopicId} -> TopicName
%% {predef, TopicName} -> TopicId %% {predef, TopicName} -> TopicId
%% {ClientId, TopicId} -> TopicName %% {ClientId, TopicId} -> TopicName
@ -137,6 +122,15 @@ init([PredefTopics]) ->
end, 0, PredefTopics), end, 0, PredefTopics),
{ok, #state{max_predef_topic_id = MaxPredefId}}. {ok, #state{max_predef_topic_id = MaxPredefId}}.
create_table() ->
%% Optimize storage
StoreProps = [{ets, [{read_concurrency, true}]}],
ok = ekka_mnesia:create_table(?MODULE, [
{attributes, record_info(fields, emqx_sn_registry)},
{ram_copies, [node()]},
{storage_properties, StoreProps}]),
ok = ekka_mnesia:copy_table(?MODULE, ram_copies).
handle_call({register, ClientId, TopicName}, _From, handle_call({register, ClientId, TopicName}, _From,
State = #state{max_predef_topic_id = PredefId}) -> State = #state{max_predef_topic_id = PredefId}) ->
case lookup_topic_id(ClientId, TopicName) of case lookup_topic_id(ClientId, TopicName) of

View File

@ -42,7 +42,7 @@ end_per_suite(_Config) ->
init_per_testcase(_TestCase, Config) -> init_per_testcase(_TestCase, Config) ->
ekka_mnesia:start(), ekka_mnesia:start(),
emqx_sn_registry:mnesia(boot), emqx_sn_registry:create_table(),
mnesia:clear_table(emqx_sn_registry), 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),
@ -118,4 +118,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).