fix: bpapi announce's bad node name

This commit is contained in:
zhongwencool 2024-02-22 14:55:11 +08:00
parent 8e47503f7d
commit ccf607ee2c
2 changed files with 21 additions and 12 deletions

View File

@ -18,14 +18,15 @@
%% API: %% API:
-export([ -export([
start/0, start/0,
announce/1, announce/2,
supported_version/1, supported_version/2, supported_version/1, supported_version/2,
versions_file/1 versions_file/1
]). ]).
%% Internal exports (RPC) %% Internal exports (RPC)
-export([ -export([
announce_fun/1 announce_fun/1,
announce_fun/2
]). ]).
-export_type([api/0, api_version/0, var_name/0, call/0, rpc/0, bpapi_meta/0]). -export_type([api/0, api_version/0, var_name/0, call/0, rpc/0, bpapi_meta/0]).
@ -66,7 +67,7 @@ start() ->
{rlog_shard, ?COMMON_SHARD} {rlog_shard, ?COMMON_SHARD}
]), ]),
ok = mria:wait_for_tables([?TAB]), ok = mria:wait_for_tables([?TAB]),
announce(emqx). announce(node(), emqx).
%% @doc Get maximum version of the backplane API supported by the node %% @doc Get maximum version of the backplane API supported by the node
-spec supported_version(node(), api()) -> api_version() | undefined. -spec supported_version(node(), api()) -> api_version() | undefined.
@ -82,10 +83,10 @@ supported_version(Node, API) ->
supported_version(API) -> supported_version(API) ->
ets:lookup_element(?TAB, {?multicall, API}, #?TAB.version). ets:lookup_element(?TAB, {?multicall, API}, #?TAB.version).
-spec announce(atom()) -> ok. -spec announce(node(), atom()) -> ok.
announce(App) -> announce(Node, App) ->
{ok, Data} = file:consult(?MODULE:versions_file(App)), {ok, Data} = file:consult(?MODULE:versions_file(App)),
{atomic, ok} = mria:transaction(?COMMON_SHARD, fun ?MODULE:announce_fun/1, [Data]), {atomic, ok} = mria:transaction(?COMMON_SHARD, fun ?MODULE:announce_fun/2, [Node, Data]),
ok. ok.
-spec versions_file(atom()) -> file:filename_all(). -spec versions_file(atom()) -> file:filename_all().
@ -96,11 +97,18 @@ versions_file(App) ->
%% Internal functions %% Internal functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Attention:
%% This function is just to prevent errors when being called during a rolling upgrade
%% if the version is less than 5.5.0. Its 'node' parameter is wrong!
-spec announce_fun([{api(), api_version()}]) -> ok. -spec announce_fun([{api(), api_version()}]) -> ok.
announce_fun(Data) -> announce_fun(Data) ->
announce_fun(node(), Data).
-spec announce_fun(node(), [{api(), api_version()}]) -> ok.
announce_fun(Node, Data) ->
%% Delete old records, if present: %% Delete old records, if present:
MS = ets:fun2ms(fun(#?TAB{key = {node(), API}}) -> MS = ets:fun2ms(fun(#?TAB{key = {N, API}}) when N =:= Node ->
{node(), API} {N, API}
end), end),
OldKeys = mnesia:select(?TAB, MS, write), OldKeys = mnesia:select(?TAB, MS, write),
_ = [ _ = [
@ -109,7 +117,7 @@ announce_fun(Data) ->
], ],
%% Insert new records: %% Insert new records:
_ = [ _ = [
mnesia:write(#?TAB{key = {node(), API}, version = Version}) mnesia:write(#?TAB{key = {Node, API}, version = Version})
|| {API, Version} <- Data || {API, Version} <- Data
], ],
%% Update maximum supported version: %% Update maximum supported version:

View File

@ -44,10 +44,11 @@ t_announce(Config) ->
meck:new(emqx_bpapi, [passthrough, no_history]), meck:new(emqx_bpapi, [passthrough, no_history]),
Filename = filename:join(?config(data_dir, Config), "test.versions"), Filename = filename:join(?config(data_dir, Config), "test.versions"),
meck:expect(emqx_bpapi, versions_file, fun(_) -> Filename end), meck:expect(emqx_bpapi, versions_file, fun(_) -> Filename end),
?assertMatch(ok, emqx_bpapi:announce(emqx)), FakeNode = 'fake-node@127.0.0.1',
?assertMatch(ok, emqx_bpapi:announce(FakeNode, emqx)),
timer:sleep(100), timer:sleep(100),
?assertMatch(4, emqx_bpapi:supported_version(node(), api2)), ?assertMatch(4, emqx_bpapi:supported_version(FakeNode, api2)),
?assertMatch(2, emqx_bpapi:supported_version(node(), api1)), ?assertMatch(2, emqx_bpapi:supported_version(FakeNode, api1)),
?assertMatch(2, emqx_bpapi:supported_version(api2)), ?assertMatch(2, emqx_bpapi:supported_version(api2)),
?assertMatch(2, emqx_bpapi:supported_version(api1)). ?assertMatch(2, emqx_bpapi:supported_version(api1)).