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:
-export([
start/0,
announce/1,
announce/2,
supported_version/1, supported_version/2,
versions_file/1
]).
%% Internal exports (RPC)
-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]).
@ -66,7 +67,7 @@ start() ->
{rlog_shard, ?COMMON_SHARD}
]),
ok = mria:wait_for_tables([?TAB]),
announce(emqx).
announce(node(), emqx).
%% @doc Get maximum version of the backplane API supported by the node
-spec supported_version(node(), api()) -> api_version() | undefined.
@ -82,10 +83,10 @@ supported_version(Node, API) ->
supported_version(API) ->
ets:lookup_element(?TAB, {?multicall, API}, #?TAB.version).
-spec announce(atom()) -> ok.
announce(App) ->
-spec announce(node(), atom()) -> ok.
announce(Node, 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.
-spec versions_file(atom()) -> file:filename_all().
@ -96,11 +97,18 @@ versions_file(App) ->
%% 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.
announce_fun(Data) ->
announce_fun(node(), Data).
-spec announce_fun(node(), [{api(), api_version()}]) -> ok.
announce_fun(Node, Data) ->
%% Delete old records, if present:
MS = ets:fun2ms(fun(#?TAB{key = {node(), API}}) ->
{node(), API}
MS = ets:fun2ms(fun(#?TAB{key = {N, API}}) when N =:= Node ->
{N, API}
end),
OldKeys = mnesia:select(?TAB, MS, write),
_ = [
@ -109,7 +117,7 @@ announce_fun(Data) ->
],
%% Insert new records:
_ = [
mnesia:write(#?TAB{key = {node(), API}, version = Version})
mnesia:write(#?TAB{key = {Node, API}, version = Version})
|| {API, Version} <- Data
],
%% Update maximum supported version:

View File

@ -44,10 +44,11 @@ t_announce(Config) ->
meck:new(emqx_bpapi, [passthrough, no_history]),
Filename = filename:join(?config(data_dir, Config), "test.versions"),
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),
?assertMatch(4, emqx_bpapi:supported_version(node(), api2)),
?assertMatch(2, emqx_bpapi:supported_version(node(), api1)),
?assertMatch(4, emqx_bpapi:supported_version(FakeNode, api2)),
?assertMatch(2, emqx_bpapi:supported_version(FakeNode, api1)),
?assertMatch(2, emqx_bpapi:supported_version(api2)),
?assertMatch(2, emqx_bpapi:supported_version(api1)).